Changeset 9dd2eb7 in opengl-game


Ignore:
Timestamp:
Apr 28, 2018, 2:29:20 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
9f4986b
Parents:
d9f99b2
Message:

Implement Phong shading in the color and texture shaders

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • color.frag

    rd9f99b2 r9dd2eb7  
    11#version 410
    22
    3 in vec3 color;
     3in vec3 position_eye, normal_eye, color, light_position_eye;
    44
    55out vec4 frag_color;
    66
     7// fixed point light properties
     8vec3 Ls = vec3(1.0, 1.0, 1.0); // white specular colour
     9vec3 Ld = vec3(0.7, 0.7, 0.7); // dull white diffuse light colour
     10vec3 La = vec3(0.2, 0.2, 0.2); // grey ambient colour
     11
     12// surface reflectance
     13vec3 Ks = vec3(1.0, 1.0, 1.0); // fully reflect specular light
     14vec3 Kd = vec3(1.0, 0.5, 0.0); // orange diffuse surface reflectance
     15vec3 Ka = vec3(0.2, 0.2, 0.2); // fully reflect ambient light
     16float specular_exponent = 100.0; // specular 'power'
     17
    718void 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);
    937}
  • color.vert

    rd9f99b2 r9dd2eb7  
    55layout(location = 0) in vec3 vertex_position;
    66layout(location = 1) in vec3 vertex_color;
     7layout(location = 2) in vec3 vertex_normal;
    78
    8 out vec3 color;
     9out vec3 position_eye, normal_eye, color, light_position_eye;
     10
     11// fixed point light position
     12vec3 light_position_world = vec3(0.0, 0.0, 2.0);
    913
    1014void main() {
     15  position_eye = vec3(view * model * vec4(vertex_position, 1.0));
     16  normal_eye = vec3(view * model * vec4 (vertex_normal, 0.0));
    1117  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);
    1321}
  • new-game.cpp

    rd9f99b2 r9dd2eb7  
    4040   vector<GLfloat> colors;
    4141   vector<GLfloat> texcoords;
     42   vector<GLfloat> normals;
    4243   vector<GLfloat> selected_colors;
    4344};
     
    240241      1.0f, 0.0f
    241242   };
     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   };
    242251   objects[0].selected_colors = {
    243252      0.0f, 1.0f, 0.0f,
     
    284293      1.0f, 0.0f
    285294   };
     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   };
    286303   objects[1].selected_colors = {
    287304      0.0f, 0.9f, 0.9f,
     
    353370   }
    354371
     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
    355383   GLuint vao = 0;
    356384   glGenVertexArrays(1, &vao);
     
    359387   glEnableVertexAttribArray(0);
    360388   glEnableVertexAttribArray(1);
     389   glEnableVertexAttribArray(2);
    361390
    362391   GLuint vao2 = 0;
     
    366395   glEnableVertexAttribArray(0);
    367396   glEnableVertexAttribArray(1);
     397   glEnableVertexAttribArray(2);
    368398
    369399   // I can create a vbo to store all points for all models,
     
    459489   double previous_seconds = glfwGetTime();
    460490
     491   // This draws wireframes. Useful for seeing separate faces and occluded objects.
     492   //glPolygonMode(GL_FRONT, GL_LINE);
     493
    461494   while (!glfwWindowShouldClose(window)) {
    462495      double current_seconds = glfwGetTime();
     
    535568         glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset);
    536569
     570         glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
     571         glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, objects[*it].vertex_vbo_offset);
     572
    537573         glDrawArrays(GL_TRIANGLES, 0, objects[*it].num_points);
    538574      }
     
    549585         glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
    550586         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);
    551590
    552591         glDrawArrays(GL_TRIANGLES, 0, objects[*it].num_points);
  • opengl-notes.txt

    rd9f99b2 r9dd2eb7  
    11Default bounds
    22
    3            1   1
     3           1   -1
    44          |   /
    55          |  /
     
    1111       /  |
    1212      /   |
    13      -1   -1
     13     1    -1
    1414
    1515Matrices
  • texture.frag

    rd9f99b2 r9dd2eb7  
    44
    55in vec2 texture_coordinates;
     6in vec3 position_eye, normal_eye, light_position_eye;
    67
    78out vec4 frag_color;
    89
     10// fixed point light properties
     11vec3 Ls = vec3(1.0, 1.0, 1.0); // white specular colour
     12vec3 Ld = vec3(0.7, 0.7, 0.7); // dull white diffuse light colour
     13vec3 La = vec3(0.2, 0.2, 0.2); // grey ambient colour
     14
     15// surface reflectance
     16vec3 Ks = vec3(1.0, 1.0, 1.0); // fully reflect specular light
     17vec3 Kd = vec3(1.0, 0.5, 0.0); // orange diffuse surface reflectance
     18vec3 Ka = vec3(0.2, 0.2, 0.2); // fully reflect ambient light
     19float specular_exponent = 100.0; // specular 'power'
     20
    921void main() {
    1022  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);
    1242}
  • texture.vert

    rd9f99b2 r9dd2eb7  
    55layout(location = 0) in vec3 vertex_position;
    66layout(location = 1) in vec2 vt;
     7layout(location = 2) in vec3 vertex_normal;
    78
    89out vec2 texture_coordinates;
     10out vec3 position_eye, normal_eye, light_position_eye;
     11
     12// fixed point light position
     13vec3 light_position_world = vec3(0.0, 0.0, 2.0);
    914
    1015void main() {
     16  position_eye = vec3(view * model * vec4(vertex_position, 1.0));
     17  normal_eye = vec3(view * model * vec4 (vertex_normal, 0.0));
    1118  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);
    1322}
Note: See TracChangeset for help on using the changeset viewer.