Changeset 4994692 in opengl-game


Ignore:
Timestamp:
Apr 19, 2020, 1:23:02 AM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
5ba732a
Parents:
6385d0f
Message:

Change VulkanGame::addObject() to return a reference to the newly-created object

Files:
4 edited

Legend:

Unmodified
Added
Removed
  • main-vulkan.cpp

    r6385d0f r4994692  
    1111
    1212int main(int argc, char* argv[]) {
     13   // TODO: Add date & time to the crash log
    1314   CrashLogger logger(__main, argc, argv);
    1415
  • new-game.cpp

    r6385d0f r4994692  
    501501   defineModelGroupAttrib(modelGroups[TYPE_SHIP], "ubo_index", ATTRIB_OBJECT_VARYING,
    502502      1, GL_UNSIGNED_INT, offset_of(&SceneObject::ubo_offset));
    503 /*** END OF REFACTORED CODE ***/
    504503
    505504   defineModelGroupUniform(modelGroups[TYPE_SHIP], "view", ATTRIB_UNIFORM,
     
    510509   initModelGroupAttribs(modelGroups[TYPE_SHIP]);
    511510
    512 /*** START OF REFACTORED CODE ***/
    513511   modelGroups[TYPE_ASTEROID] = createModelGroup(
    514512      loadShaderProgram("gl-shaders/asteroid.vert", "gl-shaders/asteroid.frag"));
     
    523521   defineModelGroupAttrib(modelGroups[TYPE_ASTEROID], "ubo_index", ATTRIB_OBJECT_VARYING,
    524522      1, GL_UNSIGNED_INT, offset_of(&SceneObject::ubo_offset));
    525 /*** END OF REFACTORED CODE ***/
    526523
    527524   defineModelGroupUniform(modelGroups[TYPE_ASTEROID], "view", ATTRIB_UNIFORM,
     
    532529   initModelGroupAttribs(modelGroups[TYPE_ASTEROID]);
    533530
    534 /*** START OF REFACTORED CODE ***/
    535531   modelGroups[TYPE_LASER] = createModelGroup(
    536532      loadShaderProgram("gl-shaders/laser.vert", "gl-shaders/laser.frag"));
     
    543539   defineModelGroupAttrib(modelGroups[TYPE_LASER], "ubo_index", ATTRIB_OBJECT_VARYING,
    544540      1, GL_UNSIGNED_INT, offset_of(&SceneObject::ubo_offset));
    545 /*** END OF REFACTORED CODE ***/
    546541
    547542   defineModelGroupUniform(modelGroups[TYPE_LASER], "view", ATTRIB_UNIFORM,
     
    553548
    554549   initModelGroupAttribs(modelGroups[TYPE_LASER]);
    555 
    556 /*** START OF REFACTORED CODE ***/
     550/*** END OF REFACTORED CODE ***/
     551
    557552   modelGroups[TYPE_EXPLOSION] = createModelGroup(
    558553      loadShaderProgram("gl-shaders/explosion.vert", "gl-shaders/explosion.frag"));
     
    565560   defineModelGroupAttrib(modelGroups[TYPE_EXPLOSION], "ubo_index", ATTRIB_OBJECT_VARYING,
    566561      1, GL_UNSIGNED_INT, offset_of(&SceneObject::ubo_offset));
    567 /*** END OF REFACTORED CODE ***/
    568562
    569563   defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "cur_time", ATTRIB_UNIFORM,
     
    829823
    830824            leftLaser = createLaser(
    831                vec3(-0.21f, -1.19f, 1.76f)+offset,
    832                vec3(-0.21f, -1.19f, -3.0f)+offset,
     825               vec3(-0.21f, -1.19f, 1.76f) + offset,
     826               vec3(-0.21f, -1.19f, -3.0f) + offset,
    833827               vec3(0.0f, 1.0f, 0.0f), 0.03f);
    834828            addObjectToScene(leftLaser, shaderBufferInfo, modelGroups, ubo);
     
    18871881   // To project point P onto line AB:
    18881882   // projection = A + dot(AP,AB) / dot(AB,AB) * AB
    1889    vec3 projOnLaser = start + glm::dot(cam_pos-start, ray) / (length*length) * ray;
     1883   vec3 projOnLaser = start + glm::dot(cam_pos - start, ray) / (length * length) * ray;
    18901884   vec3 laserToCam = cam_pos - projOnLaser;
    18911885
     
    19441938}
    19451939
     1940/*** START OF REFACTORED CODE ***/
    19461941void defineModelGroupUniform(ShaderModelGroup& modelGroup, string name, AttribType attribType,
    19471942                  GLint size, UniformType type, GLfloat* data) {
     
    19571952
    19581953void initModelGroupAttribs(ShaderModelGroup& modelGroup) {
    1959 /*** START OF REFACTORED CODE ***/
    19601954   glBindVertexArray(modelGroup.vao);
    19611955
    19621956   map<string, AttribInfo>::iterator it;
    19631957   for (it = modelGroup.attribs.begin(); it != modelGroup.attribs.end(); it++) {
    1964 /*** END OF REFACTORED CODE ***/
    19651958      if (it->second.attribType == ATTRIB_UNIFORM) {
    19661959         it->second.buffer = glGetUniformLocation(modelGroup.shaderProgram, it->first.c_str());
    1967 /*** START OF REFACTORED CODE ***/
    19681960      } else {
    19691961         glEnableVertexAttribArray(it->second.index);
     
    19841976      }
    19851977   }
    1986 /*** START OF REFACTORED CODE ***/
    1987 }
     1978}
     1979/*** END OF REFACTORED CODE ***/
    19881980
    19891981void bindUniformData(AttribInfo& attrib) {
  • vulkan-game.cpp

    r6385d0f r4994692  
    1212using namespace std;
    1313
     14// TODO: Update all occurances of instance variables to use this->
     15
    1416VulkanGame::VulkanGame(int maxFramesInFlight) : MAX_FRAMES_IN_FLIGHT(maxFramesInFlight) {
    15    gui = nullptr;
    16    window = nullptr;
    17    font = nullptr;
    18    fontSDLTexture = nullptr;
    19    imageSDLTexture = nullptr;
    20 
    21    currentFrame = 0;
    22    framebufferResized = false;
    23 
    24    object_VP_mats = {};
    25    ship_VP_mats = {};
    26    asteroid_VP_mats = {};
     17   this->gui = nullptr;
     18   this->window = nullptr;
     19   this->font = nullptr;
     20   this->fontSDLTexture = nullptr;
     21   this->imageSDLTexture = nullptr;
     22
     23   this->currentFrame = 0;
     24   this->framebufferResized = false;
     25
     26   this->object_VP_mats = {};
     27   this->ship_VP_mats = {};
     28   this->asteroid_VP_mats = {};
    2729}
    2830
     
    226228   modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
    227229      VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_modelPipeline);
    228    modelPipeline.addStorageDescriptor();
    229 
     230   modelPipeline.addStorageDescriptor(VK_SHADER_STAGE_VERTEX_BIT);
    230231   modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
    231232      VK_SHADER_STAGE_FRAGMENT_BIT, &floorTextureImageDescriptor);
    232233
    233    addObject(modelObjects, modelPipeline,
     234   SceneObject<ModelVertex, SSBO_ModelObject>* texturedSquare = nullptr;
     235
     236   texturedSquare = &addObject(modelObjects, modelPipeline,
    234237      addObjectIndex<ModelVertex>(modelObjects.size(), {
    235238         {{-0.5f, -0.5f,  0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
     
    243246      }, false);
    244247
    245    modelObjects.back().model_base =
     248   texturedSquare->model_base =
    246249      translate(mat4(1.0f), vec3(0.0f, 0.0f, -2.0f));
    247 
    248250   updateObject(modelObjects, modelPipeline, modelObjects.size() - 1);
    249251
    250    addObject(modelObjects, modelPipeline,
     252   texturedSquare = &addObject(modelObjects, modelPipeline,
    251253      addObjectIndex<ModelVertex>(modelObjects.size(), {
    252254         {{-0.5f, -0.5f,  0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
     
    260262      }, false);
    261263
    262    modelObjects.back().model_base =
     264   texturedSquare->model_base =
    263265      translate(mat4(1.0f), vec3(0.0f, 0.0f, -1.5f));
    264 
    265266   updateObject(modelObjects, modelPipeline, modelObjects.size() - 1);
    266267
     
    280281   shipPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
    281282      VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_shipPipeline);
    282    shipPipeline.addStorageDescriptor();
     283   shipPipeline.addStorageDescriptor(VK_SHADER_STAGE_VERTEX_BIT);
    283284
    284285   // TODO: With the normals, indexing basically becomes pointless since no vertices will have exactly
    285286   // the same data. Add an option to make some pipelines not use indexing
    286    addObject(shipObjects, shipPipeline,
     287   SceneObject<ShipVertex, SSBO_ModelObject>& ship = addObject(shipObjects, shipPipeline,
    287288      addObjectIndex<ShipVertex>(shipObjects.size(),
    288289      addVertexNormals<ShipVertex>({
     
    532533   asteroidPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
    533534      VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_asteroidPipeline);
    534    asteroidPipeline.addStorageDescriptor();
     535   asteroidPipeline.addStorageDescriptor(VK_SHADER_STAGE_VERTEX_BIT);
    535536
    536537   asteroidPipeline.createDescriptorSetLayout();
     
    545546   createSyncObjects();
    546547
    547    shipObjects[0].model_base =
     548   ship.model_base =
    548549      translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f)) *
    549550      scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
    550 
    551551   updateObject(shipObjects, shipPipeline, 0);
    552552}
     
    568568// TODO: Maybe changes the name to initScene() or something similar
    569569void VulkanGame::initMatrices() {
    570    cam_pos = vec3(0.0f, 0.0f, 2.0f);
     570   this->cam_pos = vec3(0.0f, 0.0f, 2.0f);
    571571
    572572   float cam_yaw = 0.0f;
     
    577577
    578578   mat4 R_view = pitch_mat * yaw_mat;
    579    mat4 T_view = translate(mat4(1.0f), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
     579   mat4 T_view = translate(mat4(1.0f), vec3(-this->cam_pos.x, -this->cam_pos.y, -this->cam_pos.z));
    580580   viewMat = R_view * T_view;
    581581
     
    625625               break;
    626626            case UI_EVENT_KEYDOWN:
     627               if (e.key.repeat) {
     628                  break;
     629               }
     630
    627631               if (e.key.keycode == SDL_SCANCODE_ESCAPE) {
    628632                  quit = true;
     
    631635                  float zOffset = -2.0f + (0.5f * modelObjects.size());
    632636
    633                   addObject(modelObjects, modelPipeline,
    634                      addObjectIndex<ModelVertex>(modelObjects.size(), {
    635                         {{-0.5f, -0.5f,  0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
    636                         {{ 0.5f, -0.5f,  0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
    637                         {{ 0.5f,  0.5f,  0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
    638                         {{-0.5f,  0.5f,  0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
    639                      }), {
    640                         0, 1, 2, 2, 3, 0
    641                      }, {
    642                         mat4(1.0f)
    643                      }, true);
    644 
    645                   modelObjects.back().model_base =
     637                  SceneObject<ModelVertex, SSBO_ModelObject>& texturedSquare =
     638                     addObject(modelObjects, modelPipeline,
     639                        addObjectIndex<ModelVertex>(modelObjects.size(), {
     640                           {{-0.5f, -0.5f,  0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
     641                           {{ 0.5f, -0.5f,  0.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
     642                           {{ 0.5f,  0.5f,  0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
     643                           {{-0.5f,  0.5f,  0.0f}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
     644                        }), {
     645                           0, 1, 2, 2, 3, 0
     646                        }, {
     647                           mat4(1.0f)
     648                        }, true);
     649
     650                  texturedSquare.model_base =
    646651                     translate(mat4(1.0f), vec3(0.0f, 0.0f, zOffset));
    647 
    648652                  updateObject(modelObjects, modelPipeline, modelObjects.size() - 1);
    649653               } else {
     
    671675      // Check which keys are held down
    672676
     677      SceneObject<ShipVertex, SSBO_ModelObject>& ship = shipObjects[0];
     678
    673679      if (gui->keyPressed(SDL_SCANCODE_LEFT)) {
    674          shipObjects[0].model_transform = translate(mat4(1.0f), vec3(-this->shipSpeed * this->elapsedTime, 0.0f, 0.0f))
     680         float distance = -this->shipSpeed * this->elapsedTime;
     681
     682         ship.model_transform = translate(mat4(1.0f), vec3(distance, 0.0f, 0.0f))
    675683            * shipObjects[0].model_transform;
    676684
    677685         updateObject(shipObjects, shipPipeline, 0);
    678686      } else if (gui->keyPressed(SDL_SCANCODE_RIGHT)) {
    679          shipObjects[0].model_transform = translate(mat4(1.0f), vec3(this->shipSpeed * this->elapsedTime, 0.0f, 0.0f))
     687         float distance = this->shipSpeed * this->elapsedTime;
     688
     689         ship.model_transform = translate(mat4(1.0f), vec3(distance, 0.0f, 0.0f))
    680690            * shipObjects[0].model_transform;
    681691
     
    721731      this->lastSpawn_asteroid = this->curTime;
    722732
    723       addObject(asteroidObjects, asteroidPipeline,
     733      SceneObject<AsteroidVertex, SSBO_Asteroid>& asteroid = addObject(
     734         asteroidObjects, asteroidPipeline,
    724735         addObjectIndex<AsteroidVertex>(asteroidObjects.size(),
    725736            addVertexNormals<AsteroidVertex>({
     
    782793            mat4(1.0f),
    783794            10.0f,
    784             0
     795            false
    785796         }, true);
    786797
     
    790801      // TODO: Figure out the best way to take scaling into account when calculating the radius
    791802      // Keep in mind that the main complicating factor is the currently poor radius calculation
    792       asteroidObjects.back().radius /= 8.0f;
    793 
    794       asteroidObjects.back().model_base =
     803      asteroid.radius /= 8.0f;
     804
     805      asteroid.model_base =
    795806         translate(mat4(1.0f), vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f))) *
    796807         rotate(mat4(1.0f), radians(60.0f), vec3(1.0f, 1.0f, -1.0f)) *
     
    900911   cleanupSwapChain();
    901912
     913   VulkanUtils::destroyVulkanImage(device, sdlOverlayImage);
    902914   VulkanUtils::destroyVulkanImage(device, floorTextureImage);
    903    VulkanUtils::destroyVulkanImage(device, sdlOverlayImage);
    904915
    905916   vkDestroySampler(device, textureSampler, nullptr);
     
    12821293   createTextureSampler();
    12831294
     1295   // TODO: Move all images/textures somewhere into the assets folder
     1296
     1297   VulkanUtils::createVulkanImageFromSDLTexture(device, physicalDevice, uiOverlay, sdlOverlayImage);
     1298
     1299   sdlOverlayImageDescriptor = {};
     1300   sdlOverlayImageDescriptor.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
     1301   sdlOverlayImageDescriptor.imageView = sdlOverlayImage.imageView;
     1302   sdlOverlayImageDescriptor.sampler = textureSampler;
     1303
    12841304   VulkanUtils::createVulkanImageFromFile(device, physicalDevice, commandPool, "textures/texture.jpg",
    12851305      floorTextureImage, graphicsQueue);
     
    12891309   floorTextureImageDescriptor.imageView = floorTextureImage.imageView;
    12901310   floorTextureImageDescriptor.sampler = textureSampler;
    1291 
    1292    VulkanUtils::createVulkanImageFromSDLTexture(device, physicalDevice, uiOverlay, sdlOverlayImage);
    1293 
    1294    sdlOverlayImageDescriptor = {};
    1295    sdlOverlayImageDescriptor.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
    1296    sdlOverlayImageDescriptor.imageView = sdlOverlayImage.imageView;
    1297    sdlOverlayImageDescriptor.sampler = textureSampler;
    12981311}
    12991312
  • vulkan-game.hpp

    r6385d0f r4994692  
    5151};
    5252
     53struct UBO_VP_mats {
     54   alignas(16) mat4 view;
     55   alignas(16) mat4 proj;
     56};
     57
     58struct SSBO_ModelObject {
     59   alignas(16) mat4 model;
     60};
     61
     62struct SSBO_Asteroid {
     63   alignas(16) mat4 model;
     64   alignas(4) float hp;
     65   alignas(4) unsigned int deleted;
     66};
     67
    5368// TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
    5469// TODO: Create a typedef for index type so I can easily change uin16_t to something else later
     70// TODO: Maybe create a typedef for each of the templated SceneObject types
    5571template<class VertexType, class SSBOType>
    5672struct SceneObject {
     
    6177   mat4 model_base;
    6278   mat4 model_transform;
     79
     80   // TODO: Figure out if I should make child classes that have these fields instead of putting them in the
     81   // parent class
    6382   vec3 center; // currently only matters for asteroids
    6483   float radius; // currently only matters for asteroids
    6584};
    6685
    67 struct UBO_VP_mats {
    68    alignas(16) mat4 view;
    69    alignas(16) mat4 proj;
    70 };
    71 
    72 struct SSBO_ModelObject {
    73    alignas(16) mat4 model;
    74 };
    75 
    76 struct SSBO_Asteroid {
    77    alignas(16) mat4 model;
    78    alignas(4) float hp;
    79    alignas(4) unsigned int deleted;
    80 };
    81 
    82 // Have to figure out how to include an optional ssbo parameter for each object
     86// TODO: Have to figure out how to include an optional ssbo parameter for each object
    8387// Could probably use the same approach to make indices optional
     88// Figure out if there are sufficient use cases to make either of these optional or is it fine to make
     89// them mamdatory
    8490
    8591class VulkanGame {
     
    131137      VkSampler textureSampler;
    132138
     139      VulkanImage sdlOverlayImage;
     140      VkDescriptorImageInfo sdlOverlayImageDescriptor;
     141
    133142      VulkanImage floorTextureImage;
    134143      VkDescriptorImageInfo floorTextureImageDescriptor;
    135 
    136       VulkanImage sdlOverlayImage;
    137       VkDescriptorImageInfo sdlOverlayImageDescriptor;
    138144
    139145      TTF_Font* font;
     
    230236      void createSyncObjects();
    231237
     238      // TODO: Since addObject() returns a reference to the new object now,
     239      // stop using objects.back() to access the object that was just created
    232240      template<class VertexType, class SSBOType>
    233       void addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
    234          GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
    235          const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
    236          bool pipelinesCreated);
     241      SceneObject<VertexType, SSBOType>& addObject(
     242            vector<SceneObject<VertexType, SSBOType>>& objects,
     243            GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
     244            const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
     245            bool pipelinesCreated);
    237246
    238247      template<class VertexType, class SSBOType>
    239248      void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects,
    240          GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index);
     249            GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index);
    241250
    242251      template<class VertexType>
     
    250259
    251260      void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
    252          vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList);
     261            vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
     262            vector<VkDescriptorBufferInfo>& bufferInfoList);
    253263
    254264      void recreateSwapChain();
     
    272282// to account for scaling
    273283template<class VertexType, class SSBOType>
    274 void VulkanGame::addObject(vector<SceneObject<VertexType, SSBOType>>& objects,
     284SceneObject<VertexType, SSBOType>& VulkanGame::addObject(
     285      vector<SceneObject<VertexType, SSBOType>>& objects,
    275286      GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline,
    276287      const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo,
     
    289300   centerObject(obj);
    290301
    291    bool storageBufferResized = pipeline.addObject(obj.vertices, obj.indices, obj.ssbo, commandPool, graphicsQueue);
     302   bool storageBufferResized = pipeline.addObject(obj.vertices, obj.indices, obj.ssbo,
     303      this->commandPool, this->graphicsQueue);
    292304
    293305   if (pipelinesCreated) {
     
    307319      createCommandBuffers();
    308320   }
     321
     322   return obj;
    309323}
    310324
Note: See TracChangeset for help on using the changeset viewer.