Skip to content

Commit 17a9ec8

Browse files
committed
Basic lighting
1 parent 7732689 commit 17a9ec8

6 files changed

Lines changed: 49 additions & 8 deletions

File tree

camera.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ class Camera {
1717
return viewProj;
1818
}
1919

20+
glm::mat4 getView() {
21+
return view;
22+
}
23+
2024
virtual void update() {
2125
viewProj = projection * view;
2226
}

main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ int main(int argc, char** argv) {
131131
glm::mat4 modelViewProj = camera.getViewProj() * model;
132132

133133
int modelViewProjMatrixLocation = GLCALL(glGetUniformLocation(shader.getShaderId(), "u_modelViewProj"));
134+
int modelViewLocation = GLCALL(glGetUniformLocation(shader.getShaderId(), "u_modelView"));
135+
int invModelViewLocation = GLCALL(glGetUniformLocation(shader.getShaderId(), "u_invModelView"));
134136

135137
// Wireframe
136138
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
@@ -231,10 +233,14 @@ int main(int argc, char** argv) {
231233
camera.update();
232234
model = glm::rotate(model, 1.0f*delta, glm::vec3(0, 1, 0));
233235
modelViewProj = camera.getViewProj() * model;
236+
glm::mat4 modelView = camera.getView() * model;
237+
glm::mat4 invModelView = glm::transpose(glm::inverse(modelView));
234238

235239
vertexBuffer.bind();
236240
indexBuffer.bind();
237241
GLCALL(glUniformMatrix4fv(modelViewProjMatrixLocation, 1, GL_FALSE, &modelViewProj[0][0]));
242+
GLCALL(glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, &modelView[0][0]));
243+
GLCALL(glUniformMatrix4fv(invModelViewLocation, 1, GL_FALSE, &invModelView[0][0]));
238244
GLCALL(glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT, 0));
239245
indexBuffer.unbind();
240246
vertexBuffer.unbind();

shaders/basic.fs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@
22

33
layout(location = 0) out vec4 f_color;
44

5-
in vec4 v_color;
5+
in vec3 v_normal;
6+
in vec3 v_position;
67

78
void main()
89
{
9-
f_color = v_color;
10+
// Vector from fragment to camera (camera always at 0,0,0)
11+
vec3 view = normalize(-v_position);
12+
vec3 light = normalize(vec3(1.0f, 1.0f, 1.0f));
13+
vec3 normal = normalize(v_normal);
14+
vec3 color = vec3(0.4f, 0.2f, 0.1f);
15+
vec3 reflection = reflect(-light, normal);
16+
17+
vec3 ambient = color * 0.2;
18+
vec3 diffuse = max(dot(normal, light), 0.0) * color;
19+
vec3 specular = pow(max(dot(reflection, view), 0.0), 4.0) * color;
20+
21+
f_color = vec4(ambient + diffuse + specular, 1.0f);
1022
}

shaders/basic.vs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
layout(location = 0) in vec3 a_position;
44
layout(location = 1) in vec3 a_normal;
55

6-
out vec4 v_color;
6+
out vec3 v_normal;
7+
out vec3 v_position;
78

89
uniform mat4 u_modelViewProj;
10+
uniform mat4 u_modelView;
11+
uniform mat4 u_invModelView;
912

1013
void main()
1114
{
1215
gl_Position = u_modelViewProj * vec4(a_position, 1.0f);
13-
v_color = vec4(a_normal, 1.0f);
16+
v_normal = mat3(u_invModelView) * a_normal;
17+
v_position = vec3(u_modelView * vec4(a_position, 1.0f));
1418
}

shaders_old/basic.fs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
#version 120
22

3-
varying vec4 v_color;
3+
varying vec3 v_normal;
4+
varying vec3 v_position;
45

56
void main()
67
{
7-
gl_FragColor = v_color;
8+
vec3 view = normalize(-v_position);
9+
vec3 light = normalize(vec3(1.0f, 1.0f, 1.0f));
10+
vec3 normal = normalize(v_normal);
11+
vec3 color = vec3(0.4f, 0.2f, 0.1f);
12+
vec3 reflection = reflect(-light, normal);
13+
14+
vec3 ambient = color * 0.2;
15+
vec3 diffuse = max(dot(normal, light), 0.0) * color;
16+
vec3 specular = pow(max(dot(reflection, view), 0.0), 4.0) * color;
17+
18+
gl_FragColor = vec4(ambient + diffuse + specular, 1.0f);
819
}

shaders_old/basic.vs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
attribute vec3 a_position;
44
attribute vec3 a_normal;
55

6-
varying vec4 v_color;
6+
varying vec3 v_normal;
7+
varying vec3 v_position;
78

89
uniform mat4 u_modelViewProj;
10+
uniform mat4 u_modelView;
11+
uniform mat4 u_invModelView;
912

1013
void main()
1114
{
1215
gl_Position = u_modelViewProj * vec4(a_position, 1.0f);
13-
v_color = vec4(a_normal, 1.0f);
16+
v_normal = mat3(u_invModelView) * a_normal;
17+
v_position = vec3(u_modelView * vec4(a_position, 1.0f));
1418
}

0 commit comments

Comments
 (0)