implemented light into cpp,
bug: everything is dark, need to calculate the normal matrix once in cpp and not for every pixel
This commit is contained in:
parent
86c0559fdc
commit
1c962a481f
|
@ -37,6 +37,11 @@ private:
|
||||||
bool z = true;
|
bool z = true;
|
||||||
} m_rotDirections;
|
} m_rotDirections;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
QVector3D position = { 1,1,1 };
|
||||||
|
QVector3D intensities = { 1,1,1 };
|
||||||
|
} m_light;
|
||||||
|
|
||||||
QOpenGLShaderProgram m_program;
|
QOpenGLShaderProgram m_program;
|
||||||
GeometryEngine *m_dataEngine;
|
GeometryEngine *m_dataEngine;
|
||||||
|
|
||||||
|
|
|
@ -6,10 +6,11 @@ precision mediump float;
|
||||||
|
|
||||||
uniform mat3 n_matrix;
|
uniform mat3 n_matrix;
|
||||||
uniform sampler2D texture;
|
uniform sampler2D texture;
|
||||||
|
uniform mat4 m_matrix;
|
||||||
|
|
||||||
uniform struct Light {
|
uniform struct Light {
|
||||||
vec3 position;
|
vec3 position;
|
||||||
vec3 intensities;
|
vec3 intensities;
|
||||||
} light;
|
} light;
|
||||||
|
|
||||||
uniform bool b_transparent;
|
uniform bool b_transparent;
|
||||||
|
@ -20,13 +21,13 @@ varying vec3 v_normal;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
|
mat3 normalMatrix = transpose(inverse(mat3(m_matrix)));
|
||||||
|
vec3 normal = normalize(n_matrix * v_normal);
|
||||||
|
|
||||||
vec3 normal = normalize(n_matrix * v_normal);
|
vec3 surfaceToLight = light.position - v_position;
|
||||||
|
|
||||||
vec3 surfaceToLight = light.position - v_position;
|
float brightness = dot(normal, surfaceToLight) / (length(surfaceToLight) * length(normal));
|
||||||
|
brightness = clamp(brightness, 0, 1);
|
||||||
float brightness = dot(normal, surfaceToLight) / (length(surfaceToLight) * length(normal));
|
|
||||||
brightness = clamp(brightness, 0, 1);
|
|
||||||
|
|
||||||
// Set fragment color from texture
|
// Set fragment color from texture
|
||||||
vec4 surfaceColor = vec4(texture2D(texture, v_texcoord));
|
vec4 surfaceColor = vec4(texture2D(texture, v_texcoord));
|
||||||
|
|
|
@ -225,6 +225,10 @@ 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
|
||||||
|
m_program.setUniformValue("light.position", m_light.position);
|
||||||
|
m_program.setUniformValue("light.intensities", m_light.intensities);
|
||||||
|
|
||||||
// Draw cube geometry
|
// Draw cube geometry
|
||||||
m_dataEngine->drawGeometry(&m_program, m_wireframe);
|
m_dataEngine->drawGeometry(&m_program, m_wireframe);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue