Changeset 9dd2eb7 in opengl-game
- Timestamp:
- Apr 28, 2018, 2:29:20 AM (7 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 9f4986b
- Parents:
- d9f99b2
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
color.frag
rd9f99b2 r9dd2eb7 1 1 #version 410 2 2 3 in vec3 color;3 in vec3 position_eye, normal_eye, color, light_position_eye; 4 4 5 5 out vec4 frag_color; 6 6 7 // fixed point light properties 8 vec3 Ls = vec3(1.0, 1.0, 1.0); // white specular colour 9 vec3 Ld = vec3(0.7, 0.7, 0.7); // dull white diffuse light colour 10 vec3 La = vec3(0.2, 0.2, 0.2); // grey ambient colour 11 12 // surface reflectance 13 vec3 Ks = vec3(1.0, 1.0, 1.0); // fully reflect specular light 14 vec3 Kd = vec3(1.0, 0.5, 0.0); // orange diffuse surface reflectance 15 vec3 Ka = vec3(0.2, 0.2, 0.2); // fully reflect ambient light 16 float specular_exponent = 100.0; // specular 'power' 17 7 18 void main() { 8 frag_color = vec4(color, 1.0); 19 // ambient intensity 20 vec3 Ia = La * Ka; 21 22 vec3 direction_to_light_eye = normalize(light_position_eye - position_eye); 23 float dot_prod = max(dot(direction_to_light_eye, normal_eye), 0.0); 24 25 // diffuse intensity 26 vec3 Id = Ls * color * dot_prod; 27 28 vec3 reflection_eye = reflect(-direction_to_light_eye, normal_eye); 29 vec3 surface_to_viewer_eye = normalize(-position_eye); 30 float dot_prod_specular = max(dot(reflection_eye, surface_to_viewer_eye), 0.0); 31 float specular_factor = pow(dot_prod_specular, specular_exponent); 32 33 // specular intensity 34 vec3 Is = Ls * Ks * specular_factor; 35 36 frag_color = vec4(Is + Id + Ia, 1.0); 9 37 } -
color.vert
rd9f99b2 r9dd2eb7 5 5 layout(location = 0) in vec3 vertex_position; 6 6 layout(location = 1) in vec3 vertex_color; 7 layout(location = 2) in vec3 vertex_normal; 7 8 8 out vec3 color; 9 out vec3 position_eye, normal_eye, color, light_position_eye; 10 11 // fixed point light position 12 vec3 light_position_world = vec3(0.0, 0.0, 2.0); 9 13 10 14 void main() { 15 position_eye = vec3(view * model * vec4(vertex_position, 1.0)); 16 normal_eye = vec3(view * model * vec4 (vertex_normal, 0.0)); 11 17 color = vertex_color; 12 gl_Position = proj * view * model * vec4(vertex_position, 1.0); 18 light_position_eye = vec3(view * vec4(light_position_world, 1.0)); 19 20 gl_Position = proj * vec4(position_eye, 1.0); 13 21 } -
new-game.cpp
rd9f99b2 r9dd2eb7 40 40 vector<GLfloat> colors; 41 41 vector<GLfloat> texcoords; 42 vector<GLfloat> normals; 42 43 vector<GLfloat> selected_colors; 43 44 }; … … 240 241 1.0f, 0.0f 241 242 }; 243 objects[0].normals = { 244 0.0f, 0.0f, 1.0f, 245 0.0f, 0.0f, 1.0f, 246 0.0f, 0.0f, 1.0f, 247 0.0f, 0.0f, -1.0f, 248 0.0f, 0.0f, -1.0f, 249 0.0f, 0.0f, -1.0f, 250 }; 242 251 objects[0].selected_colors = { 243 252 0.0f, 1.0f, 0.0f, … … 284 293 1.0f, 0.0f 285 294 }; 295 objects[1].normals = { 296 0.0f, 0.0f, 1.0f, 297 0.0f, 0.0f, 1.0f, 298 0.0f, 0.0f, 1.0f, 299 0.0f, 0.0f, 1.0f, 300 0.0f, 0.0f, 1.0f, 301 0.0f, 0.0f, 1.0f, 302 }; 286 303 objects[1].selected_colors = { 287 304 0.0f, 0.9f, 0.9f, … … 353 370 } 354 371 372 GLuint normals_vbo = 0; 373 glGenBuffers(1, &normals_vbo); 374 glBindBuffer(GL_ARRAY_BUFFER, normals_vbo); 375 glBufferData(GL_ARRAY_BUFFER, points_buffer_size, NULL, GL_DYNAMIC_DRAW); 376 377 offset = 0; 378 for (obj_it = objects.begin(); obj_it != objects.end(); obj_it++) { 379 glBufferSubData(GL_ARRAY_BUFFER, offset, obj_it->normals.size() * sizeof(GLfloat), &obj_it->normals[0]); 380 offset += obj_it->normals.size() * sizeof(GLfloat); 381 } 382 355 383 GLuint vao = 0; 356 384 glGenVertexArrays(1, &vao); … … 359 387 glEnableVertexAttribArray(0); 360 388 glEnableVertexAttribArray(1); 389 glEnableVertexAttribArray(2); 361 390 362 391 GLuint vao2 = 0; … … 366 395 glEnableVertexAttribArray(0); 367 396 glEnableVertexAttribArray(1); 397 glEnableVertexAttribArray(2); 368 398 369 399 // I can create a vbo to store all points for all models, … … 459 489 double previous_seconds = glfwGetTime(); 460 490 491 // This draws wireframes. Useful for seeing separate faces and occluded objects. 492 //glPolygonMode(GL_FRONT, GL_LINE); 493 461 494 while (!glfwWindowShouldClose(window)) { 462 495 double current_seconds = glfwGetTime(); … … 535 568 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset); 536 569 570 glBindBuffer(GL_ARRAY_BUFFER, normals_vbo); 571 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset); 572 537 573 glDrawArrays(GL_TRIANGLES, 0, objects[*it].num_points); 538 574 } … … 549 585 glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo); 550 586 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, objects[*it].texture_vbo_offset); 587 588 glBindBuffer(GL_ARRAY_BUFFER, normals_vbo); 589 glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset); 551 590 552 591 glDrawArrays(GL_TRIANGLES, 0, objects[*it].num_points); -
opengl-notes.txt
rd9f99b2 r9dd2eb7 1 1 Default bounds 2 2 3 1 13 1 -1 4 4 | / 5 5 | / … … 11 11 / | 12 12 / | 13 -1-113 1 -1 14 14 15 15 Matrices -
texture.frag
rd9f99b2 r9dd2eb7 4 4 5 5 in vec2 texture_coordinates; 6 in vec3 position_eye, normal_eye, light_position_eye; 6 7 7 8 out vec4 frag_color; 8 9 10 // fixed point light properties 11 vec3 Ls = vec3(1.0, 1.0, 1.0); // white specular colour 12 vec3 Ld = vec3(0.7, 0.7, 0.7); // dull white diffuse light colour 13 vec3 La = vec3(0.2, 0.2, 0.2); // grey ambient colour 14 15 // surface reflectance 16 vec3 Ks = vec3(1.0, 1.0, 1.0); // fully reflect specular light 17 vec3 Kd = vec3(1.0, 0.5, 0.0); // orange diffuse surface reflectance 18 vec3 Ka = vec3(0.2, 0.2, 0.2); // fully reflect ambient light 19 float specular_exponent = 100.0; // specular 'power' 20 9 21 void main() { 10 22 vec4 texel = texture(basic_texture, texture_coordinates); 11 frag_color = texel; 23 24 // ambient intensity 25 vec3 Ia = La * Ka; 26 27 vec3 direction_to_light_eye = normalize(light_position_eye - position_eye); 28 float dot_prod = max(dot(direction_to_light_eye, normal_eye), 0.0); 29 30 // diffuse intensity 31 vec3 Id = Ls * vec3(texel) * dot_prod; 32 33 vec3 reflection_eye = reflect(-direction_to_light_eye, normal_eye); 34 vec3 surface_to_viewer_eye = normalize(-position_eye); 35 float dot_prod_specular = max(dot(reflection_eye, surface_to_viewer_eye), 0.0); 36 float specular_factor = pow(dot_prod_specular, specular_exponent); 37 38 // specular intensity 39 vec3 Is = Ls * Ks * specular_factor; 40 41 frag_color = vec4(Is + Id + Ia, 1.0); 12 42 } -
texture.vert
rd9f99b2 r9dd2eb7 5 5 layout(location = 0) in vec3 vertex_position; 6 6 layout(location = 1) in vec2 vt; 7 layout(location = 2) in vec3 vertex_normal; 7 8 8 9 out vec2 texture_coordinates; 10 out vec3 position_eye, normal_eye, light_position_eye; 11 12 // fixed point light position 13 vec3 light_position_world = vec3(0.0, 0.0, 2.0); 9 14 10 15 void main() { 16 position_eye = vec3(view * model * vec4(vertex_position, 1.0)); 17 normal_eye = vec3(view * model * vec4 (vertex_normal, 0.0)); 11 18 texture_coordinates = vt; 12 gl_Position = proj * view * model * vec4(vertex_position, 1.0); 19 light_position_eye = vec3(view * vec4(light_position_world, 1.0)); 20 21 gl_Position = proj * vec4(position_eye, 1.0); 13 22 }
Note:
See TracChangeset
for help on using the changeset viewer.