Index: new-game.cpp
===================================================================
--- new-game.cpp	(revision 3d06b4ea32641df99a867728c79b5e7f483c760a)
+++ new-game.cpp	(revision 95595de879dfbdd0c0e918874915aa4492a12565)
@@ -36,5 +36,10 @@
 struct SceneObject {
    unsigned int id;
+
+   // Currently, model_transform should only have translate, and rotation and scale need to be done in model_base since
+   // they need to be done when the object is at the origin. I should change this to have separate scale, rotate, and translate
+   // matrices for each object that can be updated independently and then applied to the object in that order.
    mat4 model_mat, model_base, model_transform;
+   mat4 translate_mat; // beginning of doing what's mentioned above
    GLuint shader_program;
    unsigned int num_points;
@@ -95,5 +100,5 @@
                   GLuint ubo,
                   GLuint model_mat_idx_vbo);
-void removeObjectFromScene(int objectId, GLuint ubo);
+void removeObjectFromScene(SceneObject& obj, GLuint ubo);
 
 void calculateObjectBoundingBox(SceneObject& obj);
@@ -186,5 +191,5 @@
 float FAR_CLIP = 100.0f;
 
-// Should really have some array or struct of UI-related variables
+// TODO: Should really have some array or struct of UI-related variables
 bool isRunning = true;
 
@@ -795,4 +800,6 @@
    obj.model_base = T_model * R_model * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
 
+   obj.translate_mat = T_model;
+
    addObjectToSceneDuringInit(obj);
 
@@ -951,19 +958,4 @@
 
          frame_count++;
-      }
-
-      elapsed_seconds_spawn += elapsed_seconds;
-      if (elapsed_seconds_spawn > 0.5f) {
-         spawnAsteroid(vec3(getRandomNum(-1.3f, 1.3f), getRandomNum(-3.0f, -1.0f), getRandomNum(-5.5f, -4.5f)), color_sp,
-            shaderBufferInfo,
-            points_vbo,
-            colors_vbo,
-            selected_colors_vbo,
-            texcoords_vbo,
-            normals_vbo,
-            ubo,
-            model_mat_idx_vbo);
-
-         elapsed_seconds_spawn = 0.0f;
       }
 
@@ -996,4 +988,20 @@
 
       if (curState == STATE_GAME) {
+
+         elapsed_seconds_spawn += elapsed_seconds;
+         if (elapsed_seconds_spawn > 0.5f) {
+            spawnAsteroid(vec3(getRandomNum(-1.3f, 1.3f), getRandomNum(-3.0f, -1.0f), getRandomNum(-5.5f, -4.5f)), color_sp,
+               shaderBufferInfo,
+               points_vbo,
+               colors_vbo,
+               selected_colors_vbo,
+               texcoords_vbo,
+               normals_vbo,
+               ubo,
+               model_mat_idx_vbo);
+
+            elapsed_seconds_spawn -= 0.5f;
+         }
+
          /*
          if (clickedObject == &objects[0]) {
@@ -1027,9 +1035,15 @@
             if (!objects[i].deleted) {
                transformObject(objects[i], translate(mat4(1.0f), vec3(0.0f, 0.0f, 0.04f)), ubo);
+
+               // TODO: Should really compare against the near clipping plane, but also account for
+               // the camera's translation and rotation
+               if (objects[i].bounding_center.z - objects[i].bounding_radius > 3.0f) {
+                  removeObjectFromScene(objects[i], ubo);
+               }
             }
          }
 
          if (key_state[GLFW_KEY_SPACE] == GLFW_PRESS) {
-            removeObjectFromScene(0, ubo);
+            removeObjectFromScene(objects[0], ubo);
          }
       }
@@ -1357,4 +1371,6 @@
 
    calculateObjectBoundingBox(obj);
+
+   obj.bounding_center = vec3(obj.translate_mat * vec4(obj.bounding_center, 1.0f));
 
    objects.push_back(obj);
@@ -1398,7 +1414,5 @@
 }
 
-void removeObjectFromScene(int objectId, GLuint ubo) {
-   SceneObject& obj = objects[objectId];
-
+void removeObjectFromScene(SceneObject& obj, GLuint ubo) {
    if (!obj.deleted) {
       // Move the object outside the render bounds of the scene so it doesn't get rendered
@@ -1447,4 +1461,5 @@
    GLfloat radius_z = max_z - obj.bounding_center.z;
 
+   // This actually underestimates the radius. Might need to be fixed at some point.
    obj.bounding_radius = radius_x;
    if (obj.bounding_radius < radius_y)
@@ -1655,4 +1670,6 @@
    obj.model_mat = obj.model_transform * obj.model_base;
 
+   obj.bounding_center = vec3(transform * vec4(obj.bounding_center, 1.0f));
+
    glBindBuffer(GL_UNIFORM_BUFFER, ubo);
    glBufferSubData(GL_UNIFORM_BUFFER, obj.ubo_offset * sizeof(mat4), sizeof(mat4), value_ptr(obj.model_mat));
@@ -1896,4 +1913,6 @@
    obj.model_base = T * R * scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
 
+   obj.translate_mat = T;
+
    addObjectToScene(obj, shaderBufferInfo,
                   points_vbo,
