Changeset 2ba5617 in opengl-game


Ignore:
Timestamp:
Mar 26, 2020, 3:41:42 AM (4 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
bf4744d
Parents:
2ff4d3e
Message:

Center each object before, rather than after, it is copied to the GPU so that the centered vertices are copied, update an object's center when its model matrix is modified in updateObject(), mark an asteroid object as deleted when it moves offscreen, and randomize the initial z-coordinate for a new asteroid

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    r2ff4d3e r2ba5617  
    683683      }
    684684
    685       // TODO: Remove this block of code and correctly update the center of all objects when they are transformed
    686       if (gui->keyPressed(SDL_SCANCODE_X)) {
    687          if (asteroidObjects.size() > 0 && !asteroidObjects[0].ssbo.deleted) {
    688             asteroidObjects[0].model_transform = translate(mat4(1.0f), vec3(0.0f, 0.0f, this->asteroidSpeed * this->elapsedTime))
    689                * asteroidObjects[0].model_transform;
    690 
    691             vec3 obj_center = vec3(asteroid_VP_mats.view * vec4(asteroidObjects[0].center, 1.0f));
    692 
    693             float closest = obj_center.z - asteroidObjects[0].radius;
    694             cout << closest << " ? " << -NEAR_CLIP << endl;
    695 
    696             updateObject(asteroidObjects, asteroidPipeline, 0);
    697          }
    698       }
    699 
    700685      renderUI();
    701686      renderScene();
     
    717702   }
    718703
    719    for (int i = 0; i < asteroidObjects.size(); i++) {
    720       if (!asteroidObjects[i].ssbo.deleted) {
    721          asteroidObjects[i].model_transform =
    722             translate(mat4(1.0f), vec3(0.0f, 0.0f, this->asteroidSpeed * this->elapsedTime)) *
    723             asteroidObjects[i].model_transform;
     704   for (int i = 0; i < this->asteroidObjects.size(); i++) {
     705      if (!this->asteroidObjects[i].ssbo.deleted) {
     706         vec3 objCenter = vec3(viewMat * vec4(this->asteroidObjects[i].center, 1.0f));
     707
     708         if ((objCenter.z - this->asteroidObjects[i].radius) > -NEAR_CLIP) {
     709            this->asteroidObjects[i].ssbo.deleted = true;
     710         } else {
     711            this->asteroidObjects[i].model_transform =
     712               translate(mat4(1.0f), vec3(0.0f, 0.0f, this->asteroidSpeed * this->elapsedTime)) *
     713               this->asteroidObjects[i].model_transform;
     714         }
    724715
    725716         updateObject(asteroidObjects, asteroidPipeline, i);
     
    794785         }, true);
    795786
    796       // translate(mat4(1.0f), vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f))) *
    797       // translate(mat4(1.0f), vec3(0.0504826f, -1.2f, 1.0f)) *
     787      // This accounts for the scaling in model_base.
     788      // Dividing by 8 instead of 10 since the bounding radius algorithm
     789      // under-calculates the true value.
     790      // TODO: Figure out the best way to take scaling into account when calculating the radius
     791      // Keep in mind that the main complicating factor is the currently poor radius calculation
     792      asteroidObjects.back().radius /= 8.0f;
     793
    798794      asteroidObjects.back().model_base =
    799          translate(mat4(1.0f), vec3(getRandomNum(-1.3f, 1.3f), -1.2f, -2.0f)) *
     795         translate(mat4(1.0f), vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f))) *
    800796         rotate(mat4(1.0f), radians(60.0f), vec3(1.0f, 1.0f, -1.0f)) *
    801797         scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
  • vulkan-game.hpp

    r2ff4d3e r2ba5617  
    6161   mat4 model_base;
    6262   mat4 model_transform;
    63    vec3 center;
    64    float radius;
     63   vec3 center; // currently only matters for asteroids
     64   float radius; // currently only matters for asteroids
    6565};
    6666
     
    159159      // Or maybe create a higher level wrapper around GraphicsPipeline_Vulkan to hold things like
    160160      // the objects vector, the ubo, and the ssbo
     161
     162      // TODO: Rename *_VP_mats to *_uniforms and possibly use different types for each one
     163      // if there is a need to add other uniform variables to one or more of the shaders
    161164
    162165      GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject> modelPipeline;
     
    263266// and to change the model matrix later by setting model_transform and then calling updateObject()
    264267// Figure out a better way to allow the model matrix to be set during objecting creation
     268
     269// TODO: Maybe return a reference to the object from this method if I decide that updating it
     270// immediately after creation is a good idea (such as setting model_base)
     271// Currently, model_base is set like this in a few places and the radius is set for asteroids
     272// to account for scaling
    265273template<class VertexType, class SSBOType>
    266274void VulkanGame::addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
     
    268276      const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
    269277      bool pipelinesCreated) {
     278   // TODO: Use the model field of ssbo to set the object's model_base
     279   // currently, the passed in model is useless since it gets overridden in updateObject() anyway
    270280   size_t numVertices = pipeline.getNumVertices();
    271281
     
    275285
    276286   objects.push_back({ vertices, indices, ssbo, mat4(1.0f), mat4(1.0f) });
    277    centerObject(objects.back());
    278 
    279    bool storageBufferResized = pipeline.addObject(vertices, indices, ssbo, commandPool, graphicsQueue);
     287
     288   SceneObject<VertexType, SSBOType>& obj = objects.back();
     289   centerObject(obj);
     290
     291   bool storageBufferResized = pipeline.addObject(obj.vertices, obj.indices, obj.ssbo, commandPool, graphicsQueue);
    280292
    281293   if (pipelinesCreated) {
     
    304316
    305317   obj.ssbo.model = obj.model_transform * obj.model_base;
    306 
    307    // could probably re-calculate the object center here based on model
    308    // I think the center should be calculated by using model * vec3(0, 0, 0)
    309    // model_base is currently only used to set the original location of the ship and asteroids
     318   obj.center = vec3(obj.ssbo.model * vec4(0.0f, 0.0f, 0.0f, 1.0f));
    310319
    311320   pipeline.updateObject(index, obj.ssbo);
     
    379388   }
    380389
    381    object.radius = std::max(center.x, center.y);
    382    object.radius = std::max(object.radius, center.z);
     390   object.radius = std::max(max_x - center.x, max_y - center.y);
     391   object.radius = std::max(object.radius, max_z - center.z);
     392
    383393   object.center = vec3(0.0f, 0.0f, 0.0f);
    384394}
Note: See TracChangeset for help on using the changeset viewer.