From ef37119f4e94c83a6357ebc0b94a39e4e53b20d7 Mon Sep 17 00:00:00 2001 From: ccolin Date: Sat, 26 Dec 2020 21:46:12 +0100 Subject: add some sunlight --- src/opengl_widget.cc | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'src/opengl_widget.cc') diff --git a/src/opengl_widget.cc b/src/opengl_widget.cc index ca216b3..42bc612 100644 --- a/src/opengl_widget.cc +++ b/src/opengl_widget.cc @@ -7,9 +7,12 @@ static const GLchar *vertex_shader_source = R"glsl( #version 330 core layout(location = 0) in vec3 in_pos; -layout(location = 1) in vec2 in_uv; +layout(location = 1) in vec3 in_norm; +layout(location = 2) in vec2 in_uv; +out vec3 norm; out vec2 uv; +out vec3 frag_pos; uniform mat4 proj; uniform mat4 view; @@ -17,20 +20,32 @@ uniform mat4 model; void main() { gl_Position = proj * view * model * vec4(in_pos, 1.0); + norm = in_norm; uv = in_uv; + frag_pos = vec3(model * vec4(in_pos, 1.0)); } )glsl"; static const GLchar *fragment_shader_source = R"glsl( #version 330 core +in vec3 norm; in vec2 uv; +in vec3 frag_pos; + out vec4 final_col; uniform sampler2D tex; void main() { - final_col = texture(tex, uv); + vec3 light_col = vec3(1, .964, .783); + vec3 ambient = light_col * .2; + + vec3 light_dir = normalize(vec3(5, 10, -8)); + float diff = max(dot(normalize(norm), light_dir), 0.0); + vec3 diffuse = diff * light_col; + + final_col = texture(tex, uv) * vec4(ambient + diffuse, 1); } )glsl"; @@ -139,7 +154,7 @@ void OpenGLWidget::initializeGL() { void OpenGLWidget::resizeGL(int w, int h) { QMatrix4x4 projection; - projection.perspective(FOV, (float) w/h, .1, 1000); + projection.perspective(FOV, (float) w/h, .01, 1000); glUniformMatrix4fv(proj_attr, 1, GL_FALSE, projection.data()); } -- cgit v1.2.3-70-g09d2