Skip to content

Commit f7bba9d

Browse files
committed
lesson2.2
1 parent 2c19345 commit f7bba9d

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

lesson2.2/main.nim

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ proc main(): void =
110110
glEnableVertexAttribArray(0);
111111
glBindVertexArray(0);
112112

113+
var camera = newCamera(position = vec3(0.0f,0f,3f))
113114

114115
var lightingShader = newShader(getAppDir()/"programs/vertex.glsl", getAppDir()/"programs/fragment.glsl")
115116

@@ -118,8 +119,9 @@ proc main(): void =
118119
var lightPos: Vec3[GLfloat] = vec3(1.2f, 1.0f, 2.0f)
119120
var lightPosLoc = glGetUniformLocation(lightingShader.id, "lightPos")
120121
glUniform3f(lightPosLoc, lightPos.x, lightPos.y, lightPos.z)
122+
var viewPosLoc = glGetUniformLocation(lightingShader.id, "viewPos");
123+
glUniform3f(viewPosLoc, camera.position.x, camera.position.y, camera.position.z)
121124

122-
var camera = newCamera(position = vec3(0.0f,0f,3f))
123125
glClearColor(0f, 0f, 0f, 1f)
124126
glEnable(GL_DEPTH_TEST)
125127

@@ -152,6 +154,9 @@ proc main(): void =
152154
# Кубик
153155
lightingShader.use()
154156

157+
viewPosLoc = glGetUniformLocation(lightingShader.id, "viewPos");
158+
glUniform3f(viewPosLoc, camera.position.x, camera.position.y, camera.position.z)
159+
155160
lightPos = vec3(1.2f, 1.0f, 2.0f)
156161
lightPosLoc = glGetUniformLocation(lightingShader.id, "lightPos")
157162
glUniform3f(lightPosLoc, lightPos.x, lightPos.y, lightPos.z)

lesson2.2/programs/fragment.glsl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,26 @@ in vec3 FragPos;
88
uniform vec3 objectColor;
99
uniform vec3 lightColor;
1010
uniform vec3 lightPos;
11+
uniform vec3 viewPos;
1112

1213
void main()
1314
{
15+
// Const
16+
float specularStrength = 0.5f;
1417
float ambientStrength = 0.1f;
18+
1519
vec3 ambient = ambientStrength * lightColor;
1620

1721
vec3 norm = normalize(Normal);
1822
vec3 lightDir = normalize(lightPos - FragPos);
1923
float diff = max(dot(norm, lightDir), 0.0);
2024
vec3 diffuse = diff * lightColor;
2125

22-
vec3 result = (ambient + diffuse) * objectColor;
26+
vec3 viewDir = normalize(viewPos - FragPos);
27+
vec3 reflectDir = reflect(-lightDir, norm);
28+
float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
29+
vec3 specular = specularStrength * spec * lightColor;
30+
31+
vec3 result = (ambient + diffuse + specular) * objectColor;
2332
color = vec4(result, 1.0f);
2433
}

lesson2.2/programs/vertex.glsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ void main()
1313
{
1414
gl_Position = projection * view * model * vec4(position, 1.0f);
1515
FragPos = vec3(model * vec4(position, 1.0f));
16-
Normal = normal;
16+
Normal = mat3(transpose(inverse(model))) * normal;
1717
}

0 commit comments

Comments
 (0)