aboutsummaryrefslogtreecommitdiff
path: root/src/opengl_widget.cc
diff options
context:
space:
mode:
authorccolin2020-12-26 21:46:12 +0100
committerccolin2020-12-26 21:46:12 +0100
commitef37119f4e94c83a6357ebc0b94a39e4e53b20d7 (patch)
tree87169f8b50d5a2013acad8cf4ac4661ecb7f57a7 /src/opengl_widget.cc
parenta6d3f3d779b031f8ca70510abe90fade909a8d0d (diff)
add some sunlight
Diffstat (limited to 'src/opengl_widget.cc')
-rw-r--r--src/opengl_widget.cc21
1 files changed, 18 insertions, 3 deletions
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());
}