Changeset 9dd2eb7 in opengl-game for new-game.cpp


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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);
Note: See TracChangeset for help on using the changeset viewer.