Changeset baa5848 in opengl-game


Ignore:
Timestamp:
Apr 9, 2018, 2:01:04 AM (7 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
7280257
Parents:
f70ab75
Message:

Create a new rendering algorithm that supports a variable number of objects in the scene.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    rf70ab75 rbaa5848  
    4242struct SceneObject {
    4343   mat4 model_mat;
     44   GLuint shader_program;
    4445};
    4546
     
    6263
    6364SceneObject* clickedObject = NULL;
    64 SceneObject* selectedObject = NULL;
     65SceneObject* selectedObject;
    6566
    6667double fps;
     
    269270   // triangle
    270271   objects.push_back(SceneObject());
    271 
    272    T_model = translate(mat4(), vec3(0.25f, 0.0f, 0.0f));
     272   objects[0].shader_program = 0;
     273
     274   T_model = translate(mat4(), vec3(0.45f, 0.0f, 0.0f));
    273275   R_model = rotate(mat4(), 0.0f, vec3(0.0f, 1.0f, 0.0f));
    274276   objects[0].model_mat = T_model*R_model;
     
    284286   // square
    285287   objects.push_back(SceneObject());
     288   objects[1].shader_program = 0;
    286289
    287290   T_model = translate(mat4(), vec3(-0.5f, 0.0f, -1.00f));
     
    459462   glBindBufferRange(GL_UNIFORM_BUFFER, ub_index2, ubo, sizeof(float) * 16, sizeof(float) * 16);
    460463
     464   objects[0].shader_program = shader_program;
     465   objects[1].shader_program = shader_program2;
     466
     467   vector<int> program1_objects, program2_objects;
     468   vector<int>::iterator it;
     469
    461470   bool cam_moved = false;
    462471
     
    485494      }
    486495
     496      program1_objects.clear();
     497      program2_objects.clear();
     498
     499      // Handle events (Ideally, move all event-handling code
     500      // before the render code)
     501
     502      clickedObject = NULL;
     503      glfwPollEvents();
     504
    487505      if (clickedObject == &objects[0]) {
    488506         selectedObject = &objects[0];
    489507      }
    490 
    491       // At some point, I should change this to only rebind the buffer once per click, not once per frame
    492       glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    493       if (selectedObject == &objects[0]) {
    494          glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors_new, GL_STATIC_DRAW);
    495       }
    496       else {
    497          glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
     508      if (clickedObject == &objects[1]) {
     509         selectedObject = &objects[1];
     510      }
     511
     512      if (selectedObject == &objects[1] &&
     513         objects[1].shader_program == shader_program2) {
     514         objects[1].shader_program = shader_program;
     515      } else if (selectedObject != &objects[1] &&
     516         objects[1].shader_program == shader_program) {
     517         objects[1].shader_program = shader_program2;
     518      }
     519
     520      // group scene objects by shader
     521      for (int i=0; i < objects.size(); i++) {
     522         if (objects[i].shader_program == shader_program) {
     523            program1_objects.push_back(i);
     524         } else if (objects[i].shader_program == shader_program2) {
     525            program2_objects.push_back(i);
     526         }
    498527      }
    499528
     
    506535
    507536      glUseProgram(shader_program);
    508 
    509       // Since every object will have a different model matrix, maybe it shouldn't be a uniform
    510 
    511       // this is temporary.
    512       // It's needed to offset the code for the recoloring of the square working during click detection
    513       glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[0].model_mat));
    514 
    515537      glBindVertexArray(vao);
    516538
    517       glDrawArrays(GL_TRIANGLES, 0, numPoints);
    518 
    519       if (clickedObject == &objects[1]) {
    520          selectedObject = &objects[1];
    521       }
    522 
    523       if (selectedObject == &objects[1]) {
    524          glUseProgram(shader_program);
    525 
    526          // this is temporary.
    527          // It's needed to get the recoloring of the square working during click detection
    528          glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[1].model_mat));
    529 
    530          glBindVertexArray(vao2);
    531 
    532          glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
    533          glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
    534       } else {
    535          glUseProgram(shader_program2);
    536 
    537          glBindVertexArray(vao2);
    538 
    539          glBindBuffer(GL_ARRAY_BUFFER, vt_vbo);
    540          glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);
    541       }
    542 
    543       glDrawArrays(GL_TRIANGLES, 0, numPoints2);
    544 
    545       clickedObject = NULL;
    546 
    547       glfwPollEvents();
     539      for (it=program1_objects.begin(); it != program1_objects.end(); it++) {
     540         glUniformMatrix4fv(model_test_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
     541
     542         if (selectedObject == &objects[*it]) {
     543            if (*it == 1) {
     544               glBindBuffer(GL_ARRAY_BUFFER, points2_vbo);
     545               glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     546
     547               glBindBuffer(GL_ARRAY_BUFFER, colors2_vbo);
     548               glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     549
     550               glDrawArrays(GL_TRIANGLES, 0, numPoints2);
     551            } else {
     552               glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
     553               glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     554
     555               glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
     556               glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors_new, GL_STATIC_DRAW);
     557               glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     558
     559               glDrawArrays(GL_TRIANGLES, 0, numPoints);
     560            }
     561         } else {
     562            glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
     563            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     564
     565            glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
     566            glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
     567            glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     568
     569            glDrawArrays(GL_TRIANGLES, 0, numPoints);
     570         }
     571      }
     572
     573      glUseProgram(shader_program2);
     574      glBindVertexArray(vao2);
     575
     576      for (it = program2_objects.begin(); it != program2_objects.end(); it++) {
     577         glUniformMatrix4fv(model_mat_loc, 1, GL_FALSE, value_ptr(objects[*it].model_mat));
     578
     579         glDrawArrays(GL_TRIANGLES, 0, numPoints2);
     580      }
     581
    548582      glfwSwapBuffers(window);
    549583
Note: See TracChangeset for help on using the changeset viewer.