Changeset 0fe8433 in opengl-game


Ignore:
Timestamp:
Dec 24, 2019, 2:57:03 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
3e8cc8b
Parents:
cd1cb0f
Message:

Create an addObject() method in VulkanGame (which wraps the old addObject() method in GraphicsPipeline_Vulkan), and move the list of objects and other code to manage objects from the pipeline to the VulkanGame class

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • graphics-pipeline_vulkan.hpp

    rcd1cb0f r0fe8433  
    2929   vector<VkDescriptorBufferInfo>* bufferDataList;
    3030   VkDescriptorImageInfo* imageData;
    31 };
    32 
    33 // TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
    34 // TODO: Create a typedef for index type so I can easily change uin16_t to something else later
    35 template<class VertexType>
    36 struct SceneObject {
    37    vector<VertexType> vertices;
    38    vector<uint16_t> indices;
    39 
    40    mat4 model_base;
    41    mat4 model_transform;
    4231};
    4332
     
    5039      ~GraphicsPipeline_Vulkan();
    5140
     41      size_t getNumVertices();
     42
    5243      void updateRenderPass(VkRenderPass renderPass);
    5344
     
    6657      void createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage);
    6758
    68       vector<SceneObject<VertexType>>& getObjects();
    69       void addObject(const vector<VertexType>& vertices, vector<uint16_t> indices, VkCommandPool commandPool,
     59      void addVertices(const vector<VertexType>& vertices, vector<uint16_t> indices, VkCommandPool commandPool,
    7060         VkQueue graphicsQueue);
    7161
     
    10090      VkDeviceMemory indexBufferMemory;
    10191
    102       // TODO: The objects vector isn't used at all in this class, except in the method that returns
    103       // the number of objects. Move this vector and the SceneObject declaration into VulkanGame, esp.
    104       // since I'll be adding other object-specific fields such as transforms to SceneObject later
    105       vector<SceneObject<VertexType>> objects;
    106 
    10792      VkShaderModule createShaderModule(const vector<char>& code);
    10893      vector<char> readFile(const string& filename);
     
    151136template<class VertexType>
    152137GraphicsPipeline_Vulkan<VertexType>::~GraphicsPipeline_Vulkan() {
     138}
     139
     140template<class VertexType>
     141size_t GraphicsPipeline_Vulkan<VertexType>::getNumVertices() {
     142   return numVertices;
    153143}
    154144
     
    422412
    423413template<class VertexType>
    424 vector<SceneObject<VertexType>>& GraphicsPipeline_Vulkan<VertexType>::getObjects() {
    425    return objects;
    426 }
    427 
    428 template<class VertexType>
    429 void GraphicsPipeline_Vulkan<VertexType>::addObject(const vector<VertexType>& vertices, vector<uint16_t> indices,
     414void GraphicsPipeline_Vulkan<VertexType>::addVertices(const vector<VertexType>& vertices, vector<uint16_t> indices,
    430415      VkCommandPool commandPool, VkQueue graphicsQueue) {
    431416
     
    436421      resizeIndexBuffer(commandPool, graphicsQueue);
    437422   }
    438 
    439    for (uint16_t& idx : indices) {
    440       idx += numVertices;
    441    }
    442    objects.push_back({ vertices, indices, mat4(1.0f), mat4(1.0f) });
    443423
    444424   VulkanUtils::copyDataToBuffer(device, physicalDevice, commandPool, vertices, vertexBuffer, numVertices,
  • vulkan-game.cpp

    rcd1cb0f r0fe8433  
    207207      VK_SHADER_STAGE_FRAGMENT_BIT, &floorTextureImageDescriptor);
    208208
    209    modelPipeline.addObject({
     209   addObject(modelObjects, modelPipeline,
     210      {
    210211         {{-0.5f, -0.5f, -2.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
    211212         {{ 0.5f, -0.5f, -2.0f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
     
    214215      }, {
    215216         0, 1, 2, 2, 3, 0
    216       }, commandPool, graphicsQueue);
    217 
    218    modelPipeline.addObject({
     217      });
     218
     219   addObject(modelObjects, modelPipeline,
     220      {
    219221         {{-0.5f, -0.5f, -1.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
    220222         {{ 0.5f, -0.5f, -1.5f}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
     
    223225      }, {
    224226         0, 1, 2, 2, 3, 0
    225       }, commandPool, graphicsQueue);
     227      });
    226228
    227229   modelPipeline.createDescriptorSetLayout();
     
    236238      VK_SHADER_STAGE_FRAGMENT_BIT, &sdlOverlayImageDescriptor);
    237239
    238    overlayPipeline.addObject({
     240   addObject(overlayObjects, overlayPipeline,
     241      {
    239242         {{-1.0f,  1.0f,  0.0f}, {0.0f, 1.0f}},
    240243         {{ 1.0f,  1.0f,  0.0f}, {1.0f, 1.0f}},
     
    243246      }, {
    244247         0, 1, 2, 2, 3, 0
    245       }, commandPool, graphicsQueue);
     248      });
    246249
    247250   overlayPipeline.createDescriptorSetLayout();
     
    267270   // TODO: With the normals, indexing basically becomes pointless since no vertices will have exactly
    268271   // the same data. Add an option to make some pipelines not use indexing
    269    shipPipeline.addObject(
     272   addObject(shipObjects, shipPipeline,
    270273      centerObject<ShipVertex>(
    271       addObjectIndex<ShipVertex>(shipPipeline.getObjects().size(),
     274      addObjectIndex<ShipVertex>(shipObjects.size(),
    272275      addVertexNormals<ShipVertex>({
    273276         //back
     
    496499         132, 133, 134,
    497500         135, 136, 137,
    498       }, commandPool, graphicsQueue);
     501      });
    499502
    500503   shipPipeline.createDescriptorSetLayout();
     
    509512   createSyncObjects();
    510513
    511    shipPipeline.getObjects()[0].model_base =
     514   shipObjects[0].model_base =
    512515      translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f)) *
    513516      scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
     
    575578               } else if (e.key.keycode == SDL_SCANCODE_SPACE) {
    576579                  cout << "Adding a plane" << endl;
    577                   float zOffset = -2.0f + (0.5f * modelPipeline.getObjects().size());
     580                  float zOffset = -2.0f + (0.5f * modelObjects.size());
    578581
    579582                  vkDeviceWaitIdle(device);
    580583                  vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
    581584
    582                   modelPipeline.addObject({
    583                      {{-0.5f, -0.5f,  zOffset}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
    584                      {{ 0.5f, -0.5f,  zOffset}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
    585                      {{ 0.5f,  0.5f,  zOffset}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
    586                      {{-0.5f,  0.5f,  zOffset}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
    587                   }, {
    588                      0, 1, 2, 2, 3, 0
    589                   }, commandPool, graphicsQueue);
     585                  addObject(modelObjects, modelPipeline,
     586                     {
     587                        {{-0.5f, -0.5f,  zOffset}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
     588                        {{ 0.5f, -0.5f,  zOffset}, {0.0f, 1.0f, 0.0f}, {1.0f, 1.0f}},
     589                        {{ 0.5f,  0.5f,  zOffset}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f}},
     590                        {{-0.5f,  0.5f,  zOffset}, {1.0f, 1.0f, 1.0f}, {0.0f, 0.0f}}
     591                     }, {
     592                        0, 1, 2, 2, 3, 0
     593                     });
    590594
    591595                  createCommandBuffers();
     
    615619
    616620      if (gui->keyPressed(SDL_SCANCODE_LEFT)) {
    617          transformObject(shipPipeline.getObjects()[0], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)));
     621         transformObject(shipObjects[0], translate(mat4(1.0f), vec3(-0.01f, 0.0f, 0.0f)));
    618622      } else if (gui->keyPressed(SDL_SCANCODE_RIGHT)) {
    619          transformObject(shipPipeline.getObjects()[0], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)));
     623         transformObject(shipObjects[0], translate(mat4(1.0f), vec3(0.01f, 0.0f, 0.0f)));
    620624      }
    621625
     
    641645      rotate(mat4(1.0f), time * radians(90.0f), vec3(0.0f, 0.0f, 1.0f));
    642646
    643    so_Ship.model = shipPipeline.getObjects()[0].model_transform * shipPipeline.getObjects()[0].model_base;
     647   so_Ship.model = shipObjects[0].model_transform * shipObjects[0].model_base;
    644648
    645649   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_scenePipeline[currentImage], object_VP_mats);
  • vulkan-game.hpp

    rcd1cb0f r0fe8433  
    4040};
    4141
     42// TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference
     43// TODO: Create a typedef for index type so I can easily change uin16_t to something else later
     44template<class VertexType>
     45struct SceneObject {
     46   vector<VertexType> vertices;
     47   vector<uint16_t> indices;
     48
     49   mat4 model_base;
     50   mat4 model_transform;
     51};
     52
    4253struct UBO_VP_mats {
    4354   alignas(16) mat4 view;
     
    6576      vec3 cam_pos;
    6677
    67       UBO_VP_mats object_VP_mats;
    68       SBO_SceneObject so_Object;
    69 
    70       UBO_VP_mats ship_VP_mats;
    71       SBO_SceneObject so_Ship;
    72 
    7378      GameGui* gui;
    74 
    75       GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
    76       GraphicsPipeline_Vulkan<OverlayVertex> overlayPipeline;
    77       GraphicsPipeline_Vulkan<ShipVertex> shipPipeline;
    7879
    7980      SDL_version sdlVersion;
     
    107108      VkSampler textureSampler;
    108109
    109       // TODO: I should probably rename the uniformBuffer* and storageBuffer*
    110       // variables to better reflect the data they hold
    111 
    112       vector<VkBuffer> uniformBuffers_scenePipeline;
    113       vector<VkDeviceMemory> uniformBuffersMemory_scenePipeline;
    114 
    115       vector<VkDescriptorBufferInfo> uniformBufferInfoList_scenePipeline;
    116 
    117       vector<VkBuffer> storageBuffers_scenePipeline;
    118       vector<VkDeviceMemory> storageBuffersMemory_scenePipeline;
    119 
    120       vector<VkDescriptorBufferInfo> storageBufferInfoList_scenePipeline;
    121 
    122       vector<VkBuffer> uniformBuffers_shipPipeline;
    123       vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
    124 
    125       vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
    126 
    127       vector<VkBuffer> storageBuffers_shipPipeline;
    128       vector<VkDeviceMemory> storageBuffersMemory_shipPipeline;
    129 
    130       vector<VkDescriptorBufferInfo> storageBufferInfoList_shipPipeline;
    131 
    132110      VulkanImage floorTextureImage;
    133111      VkDescriptorImageInfo floorTextureImageDescriptor;
     
    148126
    149127      bool framebufferResized;
     128
     129      // TODO: I should probably rename the uniformBuffer* and storageBuffer*
     130      // variables to better reflect the data they hold
     131
     132      GraphicsPipeline_Vulkan<OverlayVertex> overlayPipeline;
     133
     134      vector<SceneObject<OverlayVertex>> overlayObjects;
     135
     136      // TODO: Rename all the variables related to modelPipeline to use the same pipelie name
     137
     138      GraphicsPipeline_Vulkan<ModelVertex> modelPipeline;
     139
     140      vector<SceneObject<ModelVertex>> modelObjects;
     141
     142      vector<VkBuffer> uniformBuffers_scenePipeline;
     143      vector<VkDeviceMemory> uniformBuffersMemory_scenePipeline;
     144
     145      vector<VkDescriptorBufferInfo> uniformBufferInfoList_scenePipeline;
     146
     147      vector<VkBuffer> storageBuffers_scenePipeline;
     148      vector<VkDeviceMemory> storageBuffersMemory_scenePipeline;
     149
     150      vector<VkDescriptorBufferInfo> storageBufferInfoList_scenePipeline;
     151
     152      UBO_VP_mats object_VP_mats;
     153      SBO_SceneObject so_Object;
     154
     155      GraphicsPipeline_Vulkan<ShipVertex> shipPipeline;
     156
     157      vector<SceneObject<ShipVertex>> shipObjects;
     158
     159      vector<VkBuffer> uniformBuffers_shipPipeline;
     160      vector<VkDeviceMemory> uniformBuffersMemory_shipPipeline;
     161
     162      vector<VkDescriptorBufferInfo> uniformBufferInfoList_shipPipeline;
     163
     164      vector<VkBuffer> storageBuffers_shipPipeline;
     165      vector<VkDeviceMemory> storageBuffersMemory_shipPipeline;
     166
     167      vector<VkDescriptorBufferInfo> storageBufferInfoList_shipPipeline;
     168
     169      UBO_VP_mats ship_VP_mats;
     170      SBO_SceneObject so_Ship;
    150171
    151172      bool initWindow(int width, int height, unsigned char guiFlags);
     
    181202
    182203      template<class VertexType>
     204      void addObject(vector<SceneObject<VertexType>>& objects, GraphicsPipeline_Vulkan<VertexType>& pipeline,
     205         const vector<VertexType>& vertices, vector<uint16_t> indices);
     206
     207      template<class VertexType>
    183208      vector<VertexType> addVertexNormals(vector<VertexType> vertices);
    184209
     
    205230            void* pUserData);
    206231};
     232
     233template<class VertexType>
     234void VulkanGame::addObject(vector<SceneObject<VertexType>>& objects, GraphicsPipeline_Vulkan<VertexType>& pipeline,
     235      const vector<VertexType>& vertices, vector<uint16_t> indices) {
     236   size_t numVertices = pipeline.getNumVertices();
     237
     238   for (uint16_t& idx : indices) {
     239      idx += numVertices;
     240   }
     241
     242   objects.push_back({ vertices, indices, mat4(1.0f), mat4(1.0f) });
     243
     244   pipeline.addVertices(vertices, indices, commandPool, graphicsQueue);
     245}
    207246
    208247template<class VertexType>
Note: See TracChangeset for help on using the changeset viewer.