@@ -59,7 +59,7 @@ float lastY = HEIGHT / 2.0f;
59
59
float ambientStrength = 0 .1f ;
60
60
float lightStrength = 0 .1f ;
61
61
float specularStrength = 0 .1f ;
62
- float specularIntensity = 64 ;
62
+ float specularIntensity = 32 ;
63
63
64
64
// Float array for cube, each point has 3 coordinated (x,y,z)
65
65
float vertices[] = {
@@ -106,7 +106,58 @@ float vertices[] = {
106
106
-0 .5f , 0 .5f , -0 .5f ,
107
107
};
108
108
109
- GLfloat texture[] =
109
+ float normals[] =
110
+ {
111
+ // Negetive Z axis
112
+ 0 .0f , 0 .0f , -1 .0f ,
113
+ 0 .0f , 0 .0f , -1 .0f ,
114
+ 0 .0f , 0 .0f , -1 .0f ,
115
+ 0 .0f , 0 .0f , -1 .0f ,
116
+ 0 .0f , 0 .0f , -1 .0f ,
117
+ 0 .0f , 0 .0f , -1 .0f ,
118
+
119
+ // Positive Z axis
120
+ 0 .0f , 0 .0f , 1 .0f ,
121
+ 0 .0f , 0 .0f , 1 .0f ,
122
+ 0 .0f , 0 .0f , 1 .0f ,
123
+ 0 .0f , 0 .0f , 1 .0f ,
124
+ 0 .0f , 0 .0f , 1 .0f ,
125
+ 0 .0f , 0 .0f , 1 .0f ,
126
+
127
+ // Negetive X axis
128
+ -1 .0f , 0 .0f , 0 .0f ,
129
+ -1 .0f , 0 .0f , 0 .0f ,
130
+ -1 .0f , 0 .0f , 0 .0f ,
131
+ -1 .0f , 0 .0f , 0 .0f ,
132
+ -1 .0f , 0 .0f , 0 .0f ,
133
+ -1 .0f , 0 .0f , 0 .0f ,
134
+
135
+ // Positive X axis
136
+ 1 .0f , 0 .0f , 0 .0f ,
137
+ 1 .0f , 0 .0f , 0 .0f ,
138
+ 1 .0f , 0 .0f , 0 .0f ,
139
+ 1 .0f , 0 .0f , 0 .0f ,
140
+ 1 .0f , 0 .0f , 0 .0f ,
141
+ 1 .0f , 0 .0f , 0 .0f ,
142
+
143
+ // Negetive Y axis
144
+ 0 .0f , -1 .0f , 0 .0f ,
145
+ 0 .0f , -1 .0f , 0 .0f ,
146
+ 0 .0f , -1 .0f , 0 .0f ,
147
+ 0 .0f , -1 .0f , 0 .0f ,
148
+ 0 .0f , -1 .0f , 0 .0f ,
149
+ 0 .0f , -1 .0f , 0 .0f ,
150
+
151
+ // Positive Y axis
152
+ 0 .0f , 1 .0f , 0 .0f ,
153
+ 0 .0f , 1 .0f , 0 .0f ,
154
+ 0 .0f , 1 .0f , 0 .0f ,
155
+ 0 .0f , 1 .0f , 0 .0f ,
156
+ 0 .0f , 1 .0f , 0 .0f ,
157
+ 0 .0f , 1 .0f , 0 .0f ,
158
+ };
159
+
160
+ float texture[] =
110
161
{
111
162
0 .0f , 0 .0f ,
112
163
1 .0f , 0 .0f ,
@@ -192,29 +243,29 @@ void processInput(GLFWwindow* window)
192
243
{
193
244
// A Cheap trick to create a more real lighting
194
245
// The light will always be a little brighter then objects
195
- ambientStrength += 0.01 ;
246
+ // ambientStrength += 0.01;
196
247
lightStrength += 0.01 ;
197
- specularStrength += 0.01 ;
248
+ // specularStrength += 0.01;
198
249
// ambientStrength += 0.01f;
199
250
if (lightStrength > 1 .0f )
200
251
lightStrength = 1 .0f ;
201
- if (ambientStrength > 0 .8f )
202
- ambientStrength = 0 .8f ;
203
- if (specularStrength > 1 .0f )
204
- specularStrength = 1 .0f ;
252
+ // if(ambientStrength > 0.8f)
253
+ // ambientStrength = 0.8f;
254
+ // if(specularStrength > 1.0f)
255
+ // specularStrength = 1.0f;
205
256
}
206
257
// If user presse DOWN key button - decrease lighting on objects
207
258
if (glfwGetKey (window, GLFW_KEY_DOWN) == GLFW_PRESS)
208
259
{
209
- ambientStrength -= 0 .01f ;
260
+ // ambientStrength -= 0.01f;
210
261
lightStrength -= 0 .01f ;
211
262
// ambientStrength -= 0.01f;
212
263
if (lightStrength <= 0 .0f )
213
264
lightStrength = 0 .0f ;
214
- if (ambientStrength <= 0 .0f )
215
- ambientStrength = 0 .0f ;
216
- if (specularStrength <= 0 .0f )
217
- specularStrength = 0 .0f ;
265
+ // if(ambientStrength <= 0.0f)
266
+ // ambientStrength = 0.0f;
267
+ // if(specularStrength <= 0.0f)
268
+ // specularStrength = 0.0f;
218
269
}
219
270
// If user presses LEFT_SHIFT key button - increase camera movement
220
271
if (glfwGetKey (window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
@@ -246,28 +297,32 @@ int main()
246
297
Shader lightShader (LIGHT_V_SHADER_PATH.c_str (), LIGHT_F_SHADER_PATH.c_str ());
247
298
248
299
// Create new Mesh object
249
- Mesh mesh (0 , vertices, sizeof (vertices), nullptr , 0 , texture, sizeof (texture), indices, sizeof (indices), GL_FLOAT, false , 0 );
300
+ // Mesh mesh(0, vertices, sizeof(vertices), nullptr, 0, texture, sizeof(texture), indices, sizeof(indices), GL_FLOAT, false, 0);
301
+ Mesh mesh (0 , vertices, sizeof (vertices), GL_FLOAT, false , 0 );
302
+ mesh.setNormals (normals);
303
+ mesh.setIndices (indices, sizeof (indices));
304
+ mesh.setTextureCoordinates (texture, sizeof (texture));
250
305
// Initialise VAO (vertex array object) and VBO (vertex buffer object) for mesh
251
306
mesh.initialise (GL_FLOAT, GL_FALSE);
252
307
// Add indices (elements) attribute to mesh
253
308
mesh.addAttribute (0 , GL_ELEMENT_ARRAY_BUFFER, 3 , 0 , 0 , indices, sizeof (indices), &mesh.elementBuffer );
254
309
// Add texture attribute to mesh
255
- mesh.addAttribute (1 , GL_ARRAY_BUFFER, 2 , GL_FLOAT, GL_FALSE, texture , sizeof (texture ), &mesh.textureBuffer );
310
+ mesh.addAttribute (1 , GL_ARRAY_BUFFER, 3 , GL_FLOAT, false , normals , sizeof (normals ), &mesh.normalsBuffer );
256
311
// Unbind mesh from global state
257
312
mesh.unbind ();
258
313
259
- Mesh light (0 , nullptr , 0 , nullptr , 0 , nullptr , 0 , nullptr , 0 , GL_FLOAT, false , 0 );
314
+ Mesh light (0 , nullptr , 0 , GL_FLOAT, false , 0 );
260
315
light.initialise (GL_FLOAT, GL_FALSE);
261
316
262
317
// Use colorShader program before setting uniforms
263
318
colorShader.use ();
264
- colorShader.setVec3 (" lightColor" , glm::value_ptr (colors[0 ]));
319
+ // colorShader.setVec3("lightColor", glm::value_ptr(colors[0]));
265
320
// Tell OpenGL to enable depth buffer
266
- colorShader. enableDepth ();
321
+ Shader:: enableDepth ();
267
322
268
323
// Use lightShader program before setting uniforms
269
- lightShader.use ();
270
- lightShader.setVec3 (" color" , glm::value_ptr (colors[0 ]));
324
+ // lightShader.use();
325
+ // lightShader.setVec3("color", glm::value_ptr(colors[0]));
271
326
272
327
// Main loop
273
328
// Run until window should close
@@ -292,10 +347,10 @@ int main()
292
347
// Vector 3 (x,y,z) to specify the effect of the rotation on each of the axes
293
348
glm::vec3 rotationVec;
294
349
unsigned int moduloVal = 6 ;
295
- unsigned int lightCubeLocationIndex = 0 ;
350
+ unsigned int lightCubeIndex = 0 ;
296
351
glm::vec3 lightPosition;
297
352
// If index devides by 3, cube will be a light source
298
- if (i == lightCubeLocationIndex )
353
+ if (i == lightCubeIndex )
299
354
{
300
355
lightShader.use ();
301
356
// Set perspective projection matrix
@@ -308,6 +363,7 @@ int main()
308
363
model = glm::translate (model, lightPosition);
309
364
lightShader.setMatrix4fv (" model" , 1 , GL_FALSE, glm::value_ptr (model));
310
365
// Set light object's light strength
366
+ lightShader.setVec3 (" color" , glm::value_ptr (colors[lightCubeIndex]));
311
367
lightShader.setFloat (" strength" , lightStrength);
312
368
}
313
369
else
@@ -317,28 +373,32 @@ int main()
317
373
// Update uniform matrix in colorShader program
318
374
colorShader.use ();
319
375
colorShader.setVec3 (" objectColor" , glm::value_ptr (colors[i]));
320
- // Set light source location in space
321
- colorShader.setVec3 (" lightPos" , glm::value_ptr (lightPosition));
376
+ colorShader.setVec3 (" viewPosition" , glm::value_ptr (camera.getPosition ()));
377
+ colorShader.setVec3 (" light.position" , glm::value_ptr (lightPosition));
378
+ colorShader.setVec3 (" light.ambient" , glm::value_ptr (glm::vec3 (1 .0f ) * lightStrength));
379
+ colorShader.setVec3 (" light.diffuse" , glm::value_ptr (glm::vec3 (1 .0f ) * lightStrength));
380
+ colorShader.setVec3 (" light.specular" , glm::value_ptr (glm::vec3 (1 .0f ) * lightStrength));
381
+
382
+ colorShader.setVec3 (" material.ambient" , glm::value_ptr (glm::vec3 (0.5 ) * colors[i] * lightStrength));
383
+ colorShader.setVec3 (" material.diffuse" , glm::value_ptr (glm::vec3 (0.8 ) * colors[i] * lightStrength));
384
+ colorShader.setVec3 (" material.specular" , glm::value_ptr (glm::vec3 (1.0 ) * colors[i] * lightStrength));
385
+ colorShader.setFloat (" material.shininess" , specularIntensity);
322
386
// Calculate and set object normal vector so we can calculate in shader how much the light hits the object
323
387
colorShader.setMatrix3fv (" normalMatrix" , 1 , GL_TRUE, glm::value_ptr (glm::mat3 (glm::inverse (model))));
324
- colorShader.setFloat (" ambientStrength" , ambientStrength);
325
- colorShader.setFloat (" specularStrength" , specularStrength);
326
- colorShader.setFloat (" specularIntensity" , specularIntensity);
327
388
// Set perspective projection matrix
328
389
projection = glm::perspective (glm::radians (camera.getFov ()), WIDTH / HEIGHT, 0 .1f , 100 .0f );
329
390
colorShader.setMatrix4fv (" projection" , 1 , GL_FALSE, glm::value_ptr (projection));
330
391
// Set view matrix
331
392
view = camera.calculateLookAtMatrix (camera.getPosition (), camera.getPosition () + camera.getFront (), camera.getUp ());
332
393
colorShader.setMatrix4fv (" view" , 1 , GL_FALSE, glm::value_ptr (view));
333
- // model = glm::rotate(model, glm::radians(angle), rotationVec);
394
+ model = glm::rotate (model, glm::radians (angle), rotationVec);
334
395
colorShader.setMatrix4fv (" model" , 1 , GL_FALSE, glm::value_ptr (model));
335
396
}
336
397
// Render model
337
398
mesh.render (GL_TRIANGLES, 36 , GL_UNSIGNED_INT, nullptr );
338
399
}
339
400
340
- // Render triangle
341
- mesh.render (GL_TRIANGLES, 36 , GL_UNSIGNED_INT, nullptr );
401
+ // mesh.render(GL_TRIANGLES, 36, GL_UNSIGNED_INT, nullptr);
342
402
343
403
mesh.unbind ();
344
404
0 commit comments