support normal map now,

support "glow" now,
update preview.jpg
This commit is contained in:
Anakin 2017-02-06 14:53:05 +01:00
parent 541a975624
commit 06d403d546
6 changed files with 114 additions and 83 deletions

View File

@ -1,21 +1,26 @@
#version 450
#ifdef GL_ES #ifdef GL_ES
// Set default precision to medium // Set default precision to medium
precision mediump int; precision mediump int;
precision mediump float; precision mediump float;
#endif #endif
uniform mat3 n_matrix; uniform mat3 normalMatrix;
uniform vec3 cameraPosition; uniform vec3 cameraPosition;
uniform sampler2D texture; uniform sampler2D tx0;
uniform sampler2D secondTexture; uniform sampler2D tx1;
uniform float materialShininess;
uniform vec3 materialSpecularColor;
uniform bool b_transparent; uniform struct Material {
uniform bool b_specular; float shininess;
uniform bool b_normalmap; vec3 specularColor;
uniform bool b_light; bool isTransparent;
bool hasSpecularmap;
bool hasNormalmap;
bool isGlow;
} material;
uniform bool useLight;
uniform struct Light { uniform struct Light {
vec4 position; vec4 position;
@ -24,82 +29,99 @@ uniform struct Light {
float ambientCoefficient; float ambientCoefficient;
} light; } light;
attribute vec3 a_polyNorm;
attribute vec3 a_polyTan;
attribute vec3 a_polyBiTan;
varying vec2 v_surfaceUV; varying vec2 v_surfaceUV;
varying vec3 v_surfacePosition; varying vec3 v_surfacePosition;
varying vec3 v_surfaceNormal; varying vec3 v_surfaceNormal;
varying vec3 v_polyNorm;
varying vec3 v_polyTan;
varying vec3 v_polyBiTan;
void main() void main()
{ {
if(b_light) if(useLight && !material.isGlow)
{ {
// some values // get the color and undo gamma correction
mat3 tbn = transpose(mat3(a_polyTan, a_polyBiTan, a_polyNorm)); vec4 surfaceColor = vec4(texture2D(tx0, v_surfaceUV));
vec3 finalNormal = normalize(n_matrix * v_surfaceNormal);
// if(b_normalmap)
// {
// finalNormal = texture2D(secondTexture, v_surfaceUV).rgb;
// finalNormal = normalize(finalNormal * 2.0 -1.0)
// }
vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV));
surfaceColor.rgb = pow(surfaceColor.rgb, vec3(2.2)); surfaceColor.rgb = pow(surfaceColor.rgb, vec3(2.2));
vec3 surfaceToLight; // attenutation depending on the distance to the light
float attenuation;
// directional light
if(light.position.w == 0.0f)
{
surfaceToLight = normalize(light.position.xyz);
}
// point light
else
{
surfaceToLight = normalize(light.position.xyz - v_surfacePosition);
}
float distanceToLight = length(light.position.xyz - v_surfacePosition); float distanceToLight = length(light.position.xyz - v_surfacePosition);
attenuation = 1.0 / (1.0 + light.attenuationFactor * pow(distanceToLight, 2)); float attenuation = 1.0 / (1.0 + light.attenuationFactor * pow(distanceToLight, 2));
// normal vector
vec3 normal = normalize(normalMatrix * v_surfaceNormal);
// direction from surface to light depending on the light type
vec3 surfaceToLight;
if(light.position.w == 0.0) // directional light
surfaceToLight = normalize(light.position.xyz);
else // point light
surfaceToLight = normalize(light.position.xyz - v_surfacePosition);
// direction from surface to camera
vec3 surfaceToCamera = normalize(cameraPosition - v_surfacePosition); vec3 surfaceToCamera = normalize(cameraPosition - v_surfacePosition);
// ambient // adjust the values if material has normal map
if(material.hasNormalmap)
{
vec3 surfaceTangent = normalize(normalMatrix * v_polyTan);
vec3 surfaceBitangent = normalize(normalMatrix * -v_polyBiTan);
vec3 surfaceNormal = normalize(normalMatrix * v_surfaceNormal);
mat3 tbn = transpose(mat3(surfaceTangent, surfaceBitangent, surfaceNormal));
normal = texture2D(tx1, v_surfaceUV).rgb;
normal = normalize(normal * 2.0 -1.0);
surfaceToLight = tbn * surfaceToLight;
surfaceToCamera = tbn * surfaceToCamera;
}
/////////////////////////////////////////////////////////////////////////////////////
// ambient component
vec3 ambient = light.ambientCoefficient * surfaceColor.rgb * light.intensities; vec3 ambient = light.ambientCoefficient * surfaceColor.rgb * light.intensities;
// diffuse
float diffuseCoefficient = max(0.0, dot(finalNormal, surfaceToLight)); /////////////////////////////////////////////////////////////////////////////////////
// diffuse component
float diffuseCoefficient = max(0.0, dot(normal, surfaceToLight));
vec3 diffuse = diffuseCoefficient * surfaceColor.rgb * light.intensities; vec3 diffuse = diffuseCoefficient * surfaceColor.rgb * light.intensities;
// specular
/////////////////////////////////////////////////////////////////////////////////////
// specular component
float specularCoefficient = 0.0; float specularCoefficient = 0.0;
if(diffuseCoefficient > 0.0) if(diffuseCoefficient > 0.0)
specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, finalNormal))), materialShininess); specularCoefficient = pow(max(0.0, dot(surfaceToCamera, reflect(-surfaceToLight, normal))), material.shininess);
vec3 specColor;
if(b_specular) float specularWeight = 1;
specColor = vec3(surfaceColor.a); if(material.hasSpecularmap)
else specularWeight = surfaceColor.a;
specColor = materialSpecularColor; vec3 specColor = specularWeight * material.specularColor;
vec3 specular = specularCoefficient * specColor * light.intensities; vec3 specular = specularCoefficient * specColor * light.intensities;
// linear color before gamma correction) /////////////////////////////////////////////////////////////////////////////////////
// linear color before gamma correction
vec3 linearColor = ambient + attenuation * (diffuse + specular); vec3 linearColor = ambient + attenuation * (diffuse + specular);
// final color after gama correction /////////////////////////////////////////////////////////////////////////////////////
// gama correction
vec3 gamma = vec3(1.0/2.2); vec3 gamma = vec3(1.0/2.2);
if(!b_transparent)
surfaceColor.a = 1.0f; if(!material.isTransparent)
surfaceColor.a = 1.0;
gl_FragColor = vec4(pow(linearColor, gamma), surfaceColor.a); gl_FragColor = vec4(pow(linearColor, gamma), surfaceColor.a);
} }
// don't use light
else else
{ {
vec4 surfaceColor = vec4(texture2D(texture, v_surfaceUV)); vec4 surfaceColor = vec4(texture2D(tx0, v_surfaceUV));
if(!b_transparent)
surfaceColor.a = 1.0f; if(!material.isTransparent)
surfaceColor.a = 1.0;
gl_FragColor = surfaceColor; gl_FragColor = surfaceColor;
} }

View File

@ -1,29 +1,39 @@
#version 450
#ifdef GL_ES #ifdef GL_ES
// Set default precision to medium // Set default precision to medium
precision mediump int; precision mediump int;
precision mediump float; precision mediump float;
#endif #endif
uniform mat4 vp_matrix; uniform mat4 viewProjection;
uniform mat4 norm_matrix; uniform mat4 normalizeModel;
uniform mat4 m_matrix; uniform mat4 modelMatrix;
attribute vec4 a_position; attribute vec4 a_position;
attribute vec2 a_texcoord; attribute vec2 a_texcoord;
attribute vec3 a_normal; attribute vec3 a_normal;
attribute vec3 a_polyNorm;
attribute vec3 a_polyTan;
attribute vec3 a_polyBiTan;
varying vec2 v_surfaceUV; varying vec2 v_surfaceUV;
varying vec3 v_surfacePosition; varying vec3 v_surfacePosition;
varying vec3 v_surfaceNormal; varying vec3 v_surfaceNormal;
varying vec3 v_polyNorm;
varying vec3 v_polyTan;
varying vec3 v_polyBiTan;
void main() void main()
{ {
// Calculate vertex position in screen space // Calculate vertex position in screen space
gl_Position = vp_matrix * norm_matrix * m_matrix * a_position; gl_Position = viewProjection * normalizeModel * modelMatrix * a_position;
// Pass data to fragment shader // Pass data to fragment shader
// Value will be automatically interpolated to fragments inside polygon faces // Value will be automatically interpolated to fragments inside polygon faces
v_surfaceUV = a_texcoord; v_surfaceUV = a_texcoord;
v_surfacePosition = vec3(norm_matrix * m_matrix * a_position); v_surfacePosition = vec3(normalizeModel * modelMatrix * a_position);
v_surfaceNormal = a_normal; v_surfaceNormal = a_normal;
v_polyNorm = a_polyNorm;
v_polyTan = a_polyTan;
v_polyBiTan = a_polyBiTan;
} }

View File

@ -5,7 +5,6 @@
#include "..\Header\OutputDevice.h" #include "..\Header\OutputDevice.h"
#include <QRegExp> #include <QRegExp>
#include "..\Header\Profiler.h"
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// constructor/destructor // constructor/destructor
@ -119,11 +118,11 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
float maxExtent = std::max(std::max(m_boundings.extents[0], m_boundings.extents[1]), m_boundings.extents[2]); float maxExtent = std::max(std::max(m_boundings.extents[0], m_boundings.extents[1]), m_boundings.extents[2]);
normMatrix.scale(1 / maxExtent); normMatrix.scale(1 / maxExtent);
normMatrix.translate(-m_boundings.center[0], -m_boundings.center[1], -m_boundings.center[2]); normMatrix.translate(-m_boundings.center[0], -m_boundings.center[1], -m_boundings.center[2]);
program->setUniformValue("norm_matrix", normMatrix); program->setUniformValue("normalizeModel", normMatrix);
// Allways use texture unit 0 and 1 // Allways use texture unit 0 and 1
program->setUniformValue("texture", 0); program->setUniformValue("tx0", 0);
program->setUniformValue("secondTexture", 1); program->setUniformValue("tx1", 1);
//setup the pipeline //setup the pipeline
setupPipeline(program); setupPipeline(program);
@ -135,6 +134,7 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
bool tmp_transparent(false); bool tmp_transparent(false);
bool tmp_specular(false); bool tmp_specular(false);
bool tmp_normalmap(false); bool tmp_normalmap(false);
bool tmp_glow(false);
float shininess(0.0); float shininess(0.0);
QVector3D specularColor; QVector3D specularColor;
@ -155,6 +155,9 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
m_materials->at(it.textureIndex).texture1->bind(1); m_materials->at(it.textureIndex).texture1->bind(1);
} }
} }
if (m_materials->at(it.textureIndex).flags[0] || m_materials->at(it.textureIndex).flags[1] || m_materials->at(it.textureIndex).rendertype == 1)
tmp_glow = true;
} }
else else
{ {
@ -163,19 +166,18 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
} }
// Set model matrix // Set model matrix
program->setUniformValue("m_matrix", it.modelMatrix); program->setUniformValue("modelMatrix", it.modelMatrix);
// Set normal matrix // Set normal matrix
program->setUniformValue("n_matrix", (normMatrix * it.modelMatrix).normalMatrix()); program->setUniformValue("normalMatrix", (normMatrix * it.modelMatrix).normalMatrix());
// set some more values
program->setUniformValue("b_transparent", tmp_transparent);
program->setUniformValue("b_specular", tmp_specular);
program->setUniformValue("b_normalmap", tmp_normalmap);
// set some material attributes // set some material attributes
program->setUniformValue("materialShininess", shininess); program->setUniformValue("material.shininess", shininess);
program->setUniformValue("materialSpecularColor", specularColor); program->setUniformValue("material.specularColor", specularColor);
program->setUniformValue("material.isTransparent", tmp_transparent);
program->setUniformValue("material.hasSpecularmap", tmp_specular);
program->setUniformValue("material.hasNormalmap", tmp_normalmap);
program->setUniformValue("material.isGlow", tmp_glow);
// Draw cube geometry using indices from VBO 1 // Draw cube geometry using indices from VBO 1
glDrawElements(GL_TRIANGLES, it.size, GL_UNSIGNED_INT, (void*)(it.offset * sizeof(GLuint))); glDrawElements(GL_TRIANGLES, it.size, GL_UNSIGNED_INT, (void*)(it.offset * sizeof(GLuint)));
@ -185,7 +187,6 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program)
void GeometryEngine::loadFile(QString filePath) void GeometryEngine::loadFile(QString filePath)
{ {
TIC("Start");
// cleanup old stuff and recreate buffers // cleanup old stuff and recreate buffers
clearData(); clearData();
m_arrayBuf.create(); m_arrayBuf.create();
@ -260,7 +261,5 @@ void GeometryEngine::loadFile(QString filePath)
clearData(); clearData();
OutputDevice::getInstance()->print(QString(e.what()), 2); OutputDevice::getInstance()->print(QString(e.what()), 2);
} }
TOC("End");
} }

View File

@ -921,7 +921,7 @@ void MshFile::loadTexture(QOpenGLTexture *& destination, QString filepath, QStri
if (!loadSuccess) if (!loadSuccess)
{ {
OutputDevice::getInstance()->print("WARNING: texture not found or corrupted: " + filename, 1); OutputDevice::getInstance()->print("WARNING: texture not found or corrupted: " + filename, 1);
//TODO: use the correct diffuse color or return with null
img = QImage(1, 1, QImage::Format_RGB32); img = QImage(1, 1, QImage::Format_RGB32);
img.fill(QColor(m_materials->back().diffuseColor[0] * 255, m_materials->back().diffuseColor[1] * 255, m_materials->back().diffuseColor[2] * 255)); img.fill(QColor(m_materials->back().diffuseColor[0] * 255, m_materials->back().diffuseColor[1] * 255, m_materials->back().diffuseColor[2] * 255));
filename += " *"; filename += " *";

View File

@ -143,10 +143,10 @@ void OglViewerWidget::paintGL()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set view-projection matrix // Set view-projection matrix
m_program.setUniformValue("vp_matrix", m_projection * m_camera->getMatrix()); m_program.setUniformValue("viewProjection", m_projection * m_camera->getMatrix());
// Set Light values // Set Light values
m_program.setUniformValue("b_light", m_lightOn); m_program.setUniformValue("useLight", m_lightOn);
m_program.setUniformValue("light.position", m_light.position); m_program.setUniformValue("light.position", m_light.position);
m_program.setUniformValue("light.intensities", m_light.intensities); m_program.setUniformValue("light.intensities", m_light.intensities);
m_program.setUniformValue("light.attenuationFactor", m_light.attenuationFactor); m_program.setUniformValue("light.attenuationFactor", m_light.attenuationFactor);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 43 KiB

After

Width:  |  Height:  |  Size: 50 KiB