Changeset 147ac6d in opengl-game


Ignore:
Timestamp:
Mar 20, 2018, 1:24:24 AM (7 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
5c9d193
Parents:
df652d5
Message:

Remove the clicked field of the SceneObject and instead use a clickedObject variable to keep track of which object was just clicked.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    rdf652d5 r147ac6d  
    2828struct SceneObject {
    2929   mat4 model_mat;
    30    bool clicked;
    3130};
    3231
     
    4241vec3 cam_pos;
    4342
    44 int colors_i = 0;
    45 
    4643mat4 view_mat;
    4744mat4 proj_mat;
     
    4946vector<SceneObject> objects;
    5047vector<ObjectFace> faces;
     48
     49SceneObject* clickedObject = NULL;
     50SceneObject* selectedObject = NULL;
    5151
    5252bool insideTriangle(vec3 p, ObjectFace* face);
     
    198198   if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
    199199      cout << "Mouse clicked (" << mouse_x << "," << mouse_y << ")" << endl;
     200      selectedObject = NULL;
    200201
    201202      float x = (2.0f*mouse_x) / width - 1.0f;
     
    273274      printVector("Intersection", intersection);
    274275
    275       obj->clicked = insideTriangle(intersection, face);
    276       cout << (obj->clicked ? "true" : "false") << endl;
     276      if (insideTriangle(intersection, face)) {
     277         clickedObject = obj;
     278      }
     279      cout << (obj == clickedObject ? "true" : "false") << endl;
    277280   }
    278281}
     
    429432   // triangle
    430433   objects.push_back(SceneObject());
    431    objects[0].clicked = false;
    432434
    433435   /*
     
    448450   // square
    449451   objects.push_back(SceneObject());
    450    objects[1].clicked = false;
    451452
    452453   // mat4 T_model2 = translate(mat4(), vec3(-1.0f, 0.0f, 0.0f));
     
    596597      }
    597598
    598       if (objects[0].clicked) {
    599          glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
    600 
    601          if (colors_i == 0) {
    602             glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors_new, GL_STATIC_DRAW);
    603             colors_i = 1;
    604          } else {
    605             glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
    606             colors_i = 0;
    607          }
     599      if (clickedObject == &objects[0]) {
     600         selectedObject = &objects[0];
     601      }
     602
     603      // At some point, I should change this to only rebind the buffer once per click, not once per frame
     604      glBindBuffer(GL_ARRAY_BUFFER, colors_vbo);
     605      if (selectedObject == &objects[0]) {
     606         glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors_new, GL_STATIC_DRAW);
     607      }
     608      else {
     609         glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
    608610      }
    609611
     
    625627      glDrawArrays(GL_TRIANGLES, 0, numPoints);
    626628
    627       if (objects[1].clicked) {
     629      if (clickedObject == &objects[1]) {
    628630         squareSelected = !squareSelected;
    629       }
    630 
    631       if (squareSelected) {
     631         selectedObject = &objects[1];
     632      }
     633
     634      if (selectedObject == &objects[1]) {
    632635         glUseProgram(shader_program);
    633636
     
    651654      glDrawArrays(GL_TRIANGLES, 0, numPoints2);
    652655
    653       for (vector<SceneObject>::iterator it = objects.begin(); it != objects.end(); it++) {
    654          it->clicked = false;
    655       }
     656      clickedObject = NULL;
    656657
    657658      glfwPollEvents();
Note: See TracChangeset for help on using the changeset viewer.