Changeset 95595de in opengl-game


Ignore:
Timestamp:
Jul 2, 2018, 2:48:01 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
ebaa95c
Parents:
58088c0
Message:

Update each object's bounding center as the object moves, and remove asteroids from the scene once they leave the visible area.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    r58088c0 r95595de  
    3636struct SceneObject {
    3737   unsigned int id;
     38
     39   // Currently, model_transform should only have translate, and rotation and scale need to be done in model_base since
     40   // they need to be done when the object is at the origin. I should change this to have separate scale, rotate, and translate
     41   // matrices for each object that can be updated independently and then applied to the object in that order.
    3842   mat4 model_mat, model_base, model_transform;
     43   mat4 translate_mat; // beginning of doing what's mentioned above
    3944   GLuint shader_program;
    4045   unsigned int num_points;
     
    95100                  GLuint ubo,
    96101                  GLuint model_mat_idx_vbo);
    97 void removeObjectFromScene(int objectId, GLuint ubo);
     102void removeObjectFromScene(SceneObject& obj, GLuint ubo);
    98103
    99104void calculateObjectBoundingBox(SceneObject& obj);
     
    186191float FAR_CLIP = 100.0f;
    187192
    188 // Should really have some array or struct of UI-related variables
     193// TODO: Should really have some array or struct of UI-related variables
    189194bool isRunning = true;
    190195
     
    795800   obj.model_base = T_model * R_model * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
    796801
     802   obj.translate_mat = T_model;
     803
    797804   addObjectToSceneDuringInit(obj);
    798805
     
    951958
    952959         frame_count++;
    953       }
    954 
    955       elapsed_seconds_spawn += elapsed_seconds;
    956       if (elapsed_seconds_spawn > 0.5f) {
    957          spawnAsteroid(vec3(getRandomNum(-1.3f, 1.3f), getRandomNum(-3.0f, -1.0f), getRandomNum(-5.5f, -4.5f)), color_sp,
    958             shaderBufferInfo,
    959             points_vbo,
    960             colors_vbo,
    961             selected_colors_vbo,
    962             texcoords_vbo,
    963             normals_vbo,
    964             ubo,
    965             model_mat_idx_vbo);
    966 
    967          elapsed_seconds_spawn = 0.0f;
    968960      }
    969961
     
    996988
    997989      if (curState == STATE_GAME) {
     990
     991         elapsed_seconds_spawn += elapsed_seconds;
     992         if (elapsed_seconds_spawn > 0.5f) {
     993            spawnAsteroid(vec3(getRandomNum(-1.3f, 1.3f), getRandomNum(-3.0f, -1.0f), getRandomNum(-5.5f, -4.5f)), color_sp,
     994               shaderBufferInfo,
     995               points_vbo,
     996               colors_vbo,
     997               selected_colors_vbo,
     998               texcoords_vbo,
     999               normals_vbo,
     1000               ubo,
     1001               model_mat_idx_vbo);
     1002
     1003            elapsed_seconds_spawn -= 0.5f;
     1004         }
     1005
    9981006         /*
    9991007         if (clickedObject == &objects[0]) {
     
    10271035            if (!objects[i].deleted) {
    10281036               transformObject(objects[i], translate(mat4(1.0f), vec3(0.0f, 0.0f, 0.04f)), ubo);
     1037
     1038               // TODO: Should really compare against the near clipping plane, but also account for
     1039               // the camera's translation and rotation
     1040               if (objects[i].bounding_center.z - objects[i].bounding_radius > 3.0f) {
     1041                  removeObjectFromScene(objects[i], ubo);
     1042               }
    10291043            }
    10301044         }
    10311045
    10321046         if (key_state[GLFW_KEY_SPACE] == GLFW_PRESS) {
    1033             removeObjectFromScene(0, ubo);
     1047            removeObjectFromScene(objects[0], ubo);
    10341048         }
    10351049      }
     
    13571371
    13581372   calculateObjectBoundingBox(obj);
     1373
     1374   obj.bounding_center = vec3(obj.translate_mat * vec4(obj.bounding_center, 1.0f));
    13591375
    13601376   objects.push_back(obj);
     
    13981414}
    13991415
    1400 void removeObjectFromScene(int objectId, GLuint ubo) {
    1401    SceneObject& obj = objects[objectId];
    1402 
     1416void removeObjectFromScene(SceneObject& obj, GLuint ubo) {
    14031417   if (!obj.deleted) {
    14041418      // Move the object outside the render bounds of the scene so it doesn't get rendered
     
    14471461   GLfloat radius_z = max_z - obj.bounding_center.z;
    14481462
     1463   // This actually underestimates the radius. Might need to be fixed at some point.
    14491464   obj.bounding_radius = radius_x;
    14501465   if (obj.bounding_radius < radius_y)
     
    16551670   obj.model_mat = obj.model_transform * obj.model_base;
    16561671
     1672   obj.bounding_center = vec3(transform * vec4(obj.bounding_center, 1.0f));
     1673
    16571674   glBindBuffer(GL_UNIFORM_BUFFER, ubo);
    16581675   glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat));
     
    18961913   obj.model_base = T * R * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
    18971914
     1915   obj.translate_mat = T;
     1916
    18981917   addObjectToScene(obj, shaderBufferInfo,
    18991918                  points_vbo,
Note: See TracChangeset for help on using the changeset viewer.