calculate normal matrix once in cpp (performance),

added bool variable to turn light on/off (still needs button),
This commit is contained in:
Anakin 2017-01-17 20:18:04 +01:00
parent f5863752e2
commit a521dfc292
4 changed files with 34 additions and 17 deletions

View File

@ -38,7 +38,7 @@ private:
} m_rotDirections; } m_rotDirections;
struct { struct {
QVector3D position = { 1,1,1 }; QVector3D position = { 10,10,10 };
QVector3D intensities = { 1,0.25,0.25 }; QVector3D intensities = { 1,0.25,0.25 };
} m_light; } m_light;
@ -50,6 +50,7 @@ private:
QQuaternion m_rotation; QQuaternion m_rotation;
bool m_wireframe = false; bool m_wireframe = false;
bool m_lightOn = true;
protected: protected:
void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE; void mousePressEvent(QMouseEvent *e) Q_DECL_OVERRIDE;

View File

@ -4,10 +4,8 @@ precision mediump int;
precision mediump float; precision mediump float;
#endif #endif
//uniform mat3 n_matrix; uniform mat3 n_matrix;
uniform sampler2D texture; uniform sampler2D texture;
uniform mat4 norm_matrix;
uniform mat4 m_matrix;
uniform struct Light { uniform struct Light {
vec3 position; vec3 position;
@ -15,6 +13,7 @@ uniform struct Light {
} light; } light;
uniform bool b_transparent; uniform bool b_transparent;
uniform bool b_light;
varying vec2 v_texcoord; varying vec2 v_texcoord;
varying vec3 v_position; varying vec3 v_position;
@ -22,21 +21,34 @@ varying vec3 v_normal;
void main() void main()
{ {
mat3 n_matrix = transpose(inverse(mat3(norm_matrix * m_matrix))); // variables
float brightness;
if(b_light == true)
{
// calculate normals in worldspace
vec3 normal = normalize(n_matrix * v_normal); vec3 normal = normalize(n_matrix * v_normal);
//get the surface - light vector (cause this is a candel)
vec3 surfaceToLight = light.position - v_position; vec3 surfaceToLight = light.position - v_position;
float brightness = dot(normal, surfaceToLight) / (length(surfaceToLight) * length(normal)); // calculate the brightness depending on the angle
brightness = dot(normal, surfaceToLight) / (length(surfaceToLight) * length(normal));
brightness = clamp(brightness, 0, 1); brightness = clamp(brightness, 0, 1);
}
else
{
brightness = 1;
light.intensities = vec3(1,1,1);
}
// Set fragment color from texture // get fragment color from texture
vec4 surfaceColor = vec4(texture2D(texture, v_texcoord)); vec4 surfaceColor = vec4(texture2D(texture, v_texcoord));
// if not transparent, ignore alpha value and set it to 1
if(!b_transparent) if(!b_transparent)
{
surfaceColor.a = 1.0f; surfaceColor.a = 1.0f;
}
// pass the data to ogl
gl_FragColor = vec4(brightness * light.intensities * surfaceColor.rgb, surfaceColor.a); gl_FragColor = vec4(brightness * light.intensities * surfaceColor.rgb, surfaceColor.a);
} }

View File

@ -191,6 +191,9 @@ void GeometryEngine::drawGeometry(QOpenGLShaderProgram *program, bool wireframe)
// Set model matrix // Set model matrix
program->setUniformValue("m_matrix", it.modelMatrix); program->setUniformValue("m_matrix", it.modelMatrix);
// Set normal matrix
program->setUniformValue("n_matrix", (normMatrix * it.modelMatrix).normalMatrix());
// decide if this is transparent // decide if this is transparent
program->setUniformValue("b_transparent", tmp_transparent); program->setUniformValue("b_transparent", tmp_transparent);

View File

@ -226,7 +226,8 @@ void OglViewerWidget::paintGL()
// Set view-projection matrix // Set view-projection matrix
m_program.setUniformValue("vp_matrix", m_projection * view); m_program.setUniformValue("vp_matrix", m_projection * view);
// Set Light // Set Light values
m_program.setUniformValue("b_light", 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);