Changeset 4994692 in opengl-game for vulkan-game.cpp


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.