Changeset 845a2cb in opengl-game


Ignore:
Timestamp:
Apr 22, 2020, 12:58:07 AM (4 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
points-test
Parents:
73a10ca
Message:

test point size

Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • gl-shaders/explosion.vert

    r73a10ca r845a2cb  
    1919
    2020void main() {
     21   /*
    2122   float duration = 0.5;
    2223   float t = cur_time - explosion_start_time[ubo_index] - start_time;
     
    3637   }
    3738
    38    vec3 p = vec3(0.0, 0.0, 0.0); //  this is the center of the explosion
    39    vec3 a = vec3(0.0, 0.1, 0.0);
    40    p += normalize(v_i) * mod(t, duration) / duration * 0.3; // allow time to loop around so particle emitter keeps going
     39   //  this is the center of the explosion
     40   // allow time to loop around so particle emitter keeps going
     41   vec3 p = normalize(v_i) * mod(t, duration) / duration * 0.3;
    4142
    4243   gl_Position = proj * view * model_mats[ubo_index] * vec4(p, 1.0);
    43    gl_PointSize = 15.0; // size in pixels
     44   */
     45   opacity = 1.0;
     46   gl_Position = vec4(0.0, 0.0, 0.0, 1.0);
     47   gl_PointSize = 100.0; // size in pixels
    4448}
  • graphics-pipeline_vulkan.hpp

    r73a10ca r845a2cb  
    4949      // if it will never change, just pass it in the constructor and save it
    5050      // If it does change, I could add an updateSwapchainImageCount() function
    51       GraphicsPipeline_Vulkan(VkPhysicalDevice physicalDevice, VkDevice device, VkRenderPass renderPass,
    52          Viewport viewport, vector<VkImage>& swapChainImages,
     51      GraphicsPipeline_Vulkan(VkPrimitiveTopology topology, VkPhysicalDevice physicalDevice, VkDevice device,
     52         VkRenderPass renderPass, Viewport viewport, vector<VkImage>& swapChainImages,
    5353         size_t vertexCapacity, size_t indexCapacity, size_t objectCapacity);
    5454      ~GraphicsPipeline_Vulkan();
     
    8686   
    8787   private:
     88      VkPrimitiveTopology topology;
    8889      VkPhysicalDevice physicalDevice;
    8990      VkDevice device;
     
    136137template<class VertexType, class SSBOType>
    137138GraphicsPipeline_Vulkan<VertexType, SSBOType>::GraphicsPipeline_Vulkan(
    138       VkPhysicalDevice physicalDevice, VkDevice device,
     139      VkPrimitiveTopology topology, VkPhysicalDevice physicalDevice, VkDevice device,
    139140      VkRenderPass renderPass, Viewport viewport, vector<VkImage>& swapChainImages,
    140141      size_t vertexCapacity, size_t indexCapacity, size_t objectCapacity) {
     142   this->topology = topology;
    141143   this->physicalDevice = physicalDevice;
    142144   this->device = device;
     
    274276   VkPipelineInputAssemblyStateCreateInfo inputAssembly = {};
    275277   inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
    276    inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
     278   inputAssembly.topology = this->topology;
    277279   inputAssembly.primitiveRestartEnable = VK_FALSE;
    278280
  • main-vulkan.cpp

    r73a10ca r845a2cb  
    2525
    2626   try {
    27       game.run(800, 600, GUI_FLAGS_WINDOW_FULLSCREEN);
     27      game.run(800, 600, 0);
    2828   } catch (const exception& e) {
    2929      cerr << e.what() << endl;
  • new-game.cpp

    r73a10ca r845a2cb  
    254254
    255255const int KEY_STATE_UNCHANGED = -1;
     256/*** START OF REFACTORED CODE ***/
    256257const bool FULLSCREEN = false;
    257258const int EXPLOSION_PARTICLE_COUNT = 300;
     259/*** END OF REFACTORED CODE ***/
    258260unsigned int MAX_UNIFORMS = 0; // Requires OpenGL constants only available at runtime, so it can't be const
    259261
     
    262264
    263265/*** START OF REFACTORED CODE ***/
    264 int windowWidth = 640;
    265 int windowHeight = 480;
     266int windowWidth = 800;
     267int windowHeight = 600;
    266268
    267269vec3 cam_pos;
  • vulkan-game.cpp

    r73a10ca r845a2cb  
    33#include <array>
    44#include <iostream>
     5#include <numeric>
    56#include <set>
    67
     
    2829   this->asteroid_VP_mats = {};
    2930   this->laser_VP_mats = {};
     31   this->explosion_UBO = {};
    3032}
    3133
     
    519521      }, false);
    520522
     523   ship.model_base =
     524      translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f)) *
     525      scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
     526   ship.modified = true;
     527
    521528   shipPipeline.createDescriptorSetLayout();
    522529   shipPipeline.createPipeline("shaders/ship-vert.spv", "shaders/ship-frag.spv");
     
    559566   laserPipeline.createDescriptorSets(swapChainImages);
    560567
     568   explosionPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ExplosionVertex::particleStartVelocity));
     569   explosionPipeline.addAttribute(VK_FORMAT_R32_SFLOAT, offset_of(&ExplosionVertex::particleStartTime));
     570   explosionPipeline.addAttribute(VK_FORMAT_R32_UINT, offset_of(&ExplosionVertex::objIndex));
     571
     572   createBufferSet(sizeof(UBO_Explosion), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
     573      uniformBuffers_explosionPipeline, uniformBuffersMemory_explosionPipeline, uniformBufferInfoList_explosionPipeline);
     574
     575   explosionPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
     576      VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_explosionPipeline);
     577   explosionPipeline.addStorageDescriptor(VK_SHADER_STAGE_VERTEX_BIT);
     578
     579   explosionPipeline.createDescriptorSetLayout();
     580   explosionPipeline.createPipeline("shaders/explosion-vert.spv", "shaders/explosion-frag.spv");
     581   explosionPipeline.createDescriptorPool(swapChainImages);
     582   explosionPipeline.createDescriptorSets(swapChainImages);
     583
    561584   cout << "Created all the graphics pipelines" << endl;
    562585
     
    564587
    565588   createSyncObjects();
    566 
    567    ship.model_base =
    568       translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f)) *
    569       scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f));
    570    ship.modified = true;
    571589}
    572590
    573591void VulkanGame::initGraphicsPipelines() {
    574    overlayPipeline = GraphicsPipeline_Vulkan<OverlayVertex, void*>(physicalDevice, device, renderPass,
     592   overlayPipeline = GraphicsPipeline_Vulkan<OverlayVertex, void*>(
     593      VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, physicalDevice, device, renderPass,
    575594      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, swapChainImages, 4, 6, 0);
    576595
    577    modelPipeline = GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject>(physicalDevice, device, renderPass,
     596   modelPipeline = GraphicsPipeline_Vulkan<ModelVertex, SSBO_ModelObject>(
     597      VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, physicalDevice, device, renderPass,
    578598      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, swapChainImages, 16, 24, 10);
    579599
    580    shipPipeline = GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject>(physicalDevice, device, renderPass,
     600   shipPipeline = GraphicsPipeline_Vulkan<ShipVertex, SSBO_ModelObject>(
     601      VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, physicalDevice, device, renderPass,
    581602      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, swapChainImages, 138, 138, 10);
    582603
    583    asteroidPipeline = GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid>(physicalDevice, device, renderPass,
     604   asteroidPipeline = GraphicsPipeline_Vulkan<AsteroidVertex, SSBO_Asteroid>(
     605      VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, physicalDevice, device, renderPass,
    584606      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, swapChainImages, 24, 36, 10);
    585607
    586    laserPipeline = GraphicsPipeline_Vulkan<LaserVertex, SSBO_Laser>(physicalDevice, device, renderPass,
     608   laserPipeline = GraphicsPipeline_Vulkan<LaserVertex, SSBO_Laser>(
     609      VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, physicalDevice, device, renderPass,
    587610      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, swapChainImages, 8, 18, 2);
     611
     612   explosionPipeline = GraphicsPipeline_Vulkan<ExplosionVertex, SSBO_Explosion>(
     613      VK_PRIMITIVE_TOPOLOGY_POINT_LIST, physicalDevice, device, renderPass,
     614      { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height },
     615      swapChainImages, EXPLOSION_PARTICLE_COUNT, EXPLOSION_PARTICLE_COUNT, 2);
    588616}
    589617
     
    616644   laser_VP_mats.view = viewMat;
    617645   laser_VP_mats.proj = projMat;
     646
     647   explosion_UBO.view = viewMat;
     648   explosion_UBO.proj = projMat;
    618649}
    619650
     
    732763               break;
    733764            case UI_EVENT_UNKNOWN:
    734                cout << "Unknown event type: 0x" << hex << e.unknown.eventType << dec << endl;
     765               //cout << "Unknown event type: 0x" << hex << e.unknown.eventType << dec << endl;
    735766               break;
    736767            default:
     
    813844         vec3 objCenter = vec3(viewMat * vec4(asteroid.center, 1.0f));
    814845
    815          if ((objCenter.z - asteroid.radius) > -NEAR_CLIP || asteroid.ssbo.hp <= 0.0f) {
     846         if (asteroid.ssbo.hp <= 0.0f) {
    816847            asteroid.ssbo.deleted = true;
    817848
    818             // TODO: Create explosion here
     849            // TODO: Optimize this so I don't recalculate the camera rotation every time
     850            // TODO: Also, avoid re-declaring cam_pitch
     851            float cam_pitch = -50.0f;
     852            mat4 pitch_mat = rotate(mat4(1.0f), radians(cam_pitch), vec3(1.0f, 0.0f, 0.0f));
     853            mat4 model_mat = translate(mat4(1.0f), asteroid.center) * pitch_mat;
     854
     855            addExplosion(model_mat, 0.5f, curTime);
     856
     857            // TODO: Increment player's score here
     858         } else if ((objCenter.z - asteroid.radius) > -NEAR_CLIP) {
     859            asteroid.ssbo.deleted = true;
    819860         } else {
    820861            asteroid.model_transform =
     
    933974   }
    934975
     976   for (size_t i = 0; i < explosionObjects.size(); i++) {
     977      if (explosionObjects[i].modified) {
     978         updateObject(explosionObjects, explosionPipeline, i);
     979      }
     980   }
     981
     982   explosion_UBO.cur_time = curTime;
     983
    935984   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_modelPipeline[currentImage], 0, object_VP_mats);
    936985
     
    940989
    941990   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_laserPipeline[currentImage], 0, laser_VP_mats);
     991
     992   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_explosionPipeline[currentImage], 0, explosion_UBO);
    942993}
    943994
     
    10461097   asteroidPipeline.cleanupBuffers();
    10471098   laserPipeline.cleanupBuffers();
     1099   explosionPipeline.cleanupBuffers();
    10481100
    10491101   for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
     
    15381590      asteroidPipeline.createRenderCommands(commandBuffers[i], i);
    15391591      laserPipeline.createRenderCommands(commandBuffers[i], i);
     1592      explosionPipeline.createRenderCommands(commandBuffers[i], i);
    15401593
    15411594      // Always render this pipeline last
     
    15711624}
    15721625
    1573 void VulkanGame::addLaser( vec3 start, vec3 end, vec3 color, float width) {
     1626void VulkanGame::addLaser(vec3 start, vec3 end, vec3 color, float width) {
    15741627   vec3 ray = end - start;
    15751628   float length = glm::length(ray);
     
    17921845}
    17931846
     1847void VulkanGame::addExplosion(mat4 model_mat, float duration, float cur_time) {
     1848   cout << "Adding explosion..." << endl;
     1849
     1850   vector<ExplosionVertex> vertices;
     1851   vertices.reserve(EXPLOSION_PARTICLE_COUNT);
     1852
     1853   float particlestart_time = 0.0f;
     1854
     1855   for (int i = 0; i < EXPLOSION_PARTICLE_COUNT; i++) {
     1856      float randx = ((float)rand() / (float)RAND_MAX) - 0.5f;
     1857      float randy = ((float)rand() / (float)RAND_MAX) - 0.5f;
     1858
     1859      vertices.push_back({ vec3(randx, randy, 0.0f), particlestart_time});
     1860
     1861      particlestart_time += 0.01f;
     1862   }
     1863
     1864   // Fill the indices with the the first EXPLOSION_PARTICLE_COUNT ints
     1865   vector<uint16_t> indices(EXPLOSION_PARTICLE_COUNT);
     1866   iota(indices.begin(), indices.end(), 0);
     1867
     1868   SceneObject<ExplosionVertex, SSBO_Explosion>& explosion = addObject(
     1869      explosionObjects, explosionPipeline,
     1870      addObjectIndex(explosionObjects.size(), vertices),
     1871      indices, {
     1872         mat4(1.0f),
     1873         cur_time,
     1874         duration,
     1875         false
     1876      }, true);
     1877
     1878   explosion.model_base = model_mat;
     1879   explosion.model_transform = mat4(1.0f);
     1880
     1881   explosion.modified = true;
     1882}
     1883
    17941884// TODO: Fix the crash that happens when alt-tabbing
    17951885void VulkanGame::recreateSwapChain() {
     
    18531943   laserPipeline.createDescriptorPool(swapChainImages);
    18541944   laserPipeline.createDescriptorSets(swapChainImages);
     1945
     1946   createBufferSet(sizeof(UBO_Explosion), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
     1947      uniformBuffers_explosionPipeline, uniformBuffersMemory_explosionPipeline, uniformBufferInfoList_explosionPipeline);
     1948
     1949   explosionPipeline.updateRenderPass(renderPass);
     1950   explosionPipeline.createPipeline("shaders/explosion-vert.spv", "shaders/explosion-frag.spv");
     1951   explosionPipeline.createDescriptorPool(swapChainImages);
     1952   explosionPipeline.createDescriptorSets(swapChainImages);
    18551953
    18561954   createCommandBuffers();
     
    18711969   asteroidPipeline.cleanup();
    18721970   laserPipeline.cleanup();
     1971   explosionPipeline.cleanup();
    18731972
    18741973   for (size_t i = 0; i < uniformBuffers_modelPipeline.size(); i++) {
     
    18921991   }
    18931992
     1993   for (size_t i = 0; i < uniformBuffers_explosionPipeline.size(); i++) {
     1994      vkDestroyBuffer(device, uniformBuffers_explosionPipeline[i], nullptr);
     1995      vkFreeMemory(device, uniformBuffersMemory_explosionPipeline[i], nullptr);
     1996   }
     1997
    18941998   vkDestroyRenderPass(device, renderPass, nullptr);
    18951999
  • vulkan-game.hpp

    r73a10ca r845a2cb  
    5757};
    5858
    59 struct UBO_VP_mats {
    60    alignas(16) mat4 view;
    61    alignas(16) mat4 proj;
     59struct ExplosionVertex {
     60   vec3 particleStartVelocity;
     61   float particleStartTime;
     62   unsigned int objIndex;
    6263};
    6364
     
    7677   alignas(4) vec3 color;
    7778   alignas(4) unsigned int deleted;
     79};
     80
     81struct SSBO_Explosion {
     82   alignas(16) mat4 model;
     83   alignas(4) float explosionStartTime;
     84   alignas(4) float explosionDuration;
     85   alignas(4) unsigned int deleted;
     86};
     87
     88struct UBO_VP_mats {
     89   alignas(16) mat4 view;
     90   alignas(16) mat4 proj;
     91};
     92
     93struct UBO_Explosion {
     94   alignas(16) mat4 view;
     95   alignas(16) mat4 proj;
     96   alignas(4) float cur_time;
    7897};
    7998
     
    175194
    176195   private:
     196      // TODO: Make these consts static
     197
    177198      const int MAX_FRAMES_IN_FLIGHT;
    178199
     
    180201      const float FAR_CLIP = 100.0f;
    181202      const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
     203
     204      const int EXPLOSION_PARTICLE_COUNT = 300;
    182205
    183206      vec3 cam_pos;
     
    285308
    286309      UBO_VP_mats laser_VP_mats;
     310
     311      GraphicsPipeline_Vulkan<ExplosionVertex, SSBO_Explosion> explosionPipeline;
     312      vector<SceneObject<ExplosionVertex, SSBO_Explosion>> explosionObjects;
     313
     314      vector<VkBuffer> uniformBuffers_explosionPipeline;
     315      vector<VkDeviceMemory> uniformBuffersMemory_explosionPipeline;
     316      vector<VkDescriptorBufferInfo> uniformBufferInfoList_explosionPipeline;
     317
     318      UBO_Explosion explosion_UBO;
    287319
    288320      vector<BaseEffectOverTime*> effects;
     
    334366      void createSyncObjects();
    335367
    336       void addLaser(vec3 start, vec3 end, vec3 color, float width);
    337       void translateLaser(size_t index, const vec3& translation);
    338       void updateLaserTarget(size_t index);
    339       bool getLaserAndAsteroidIntersection(SceneObject<AsteroidVertex, SSBO_Asteroid>& asteroid,
    340             vec3& start, vec3& end, vec3& intersection);
    341 
    342368      // TODO: Since addObject() returns a reference to the new object now,
    343369      // stop using objects.back() to access the object that was just created
     
    366392      void centerObject(SceneObject<VertexType, SSBOType>& object);
    367393
     394      void addLaser(vec3 start, vec3 end, vec3 color, float width);
     395      void translateLaser(size_t index, const vec3& translation);
     396      void updateLaserTarget(size_t index);
     397      bool getLaserAndAsteroidIntersection(SceneObject<AsteroidVertex, SSBO_Asteroid>& asteroid,
     398            vec3& start, vec3& end, vec3& intersection);
     399
     400      void addExplosion(mat4 model_mat, float duration, float cur_time);
     401
    368402      void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags,
    369403            vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
     
    380414            void* pUserData);
    381415};
     416
     417// Start of specialized no-op functions
     418
     419template<>
     420inline void VulkanGame::centerObject(SceneObject<ExplosionVertex, SSBO_Explosion>& object) {
     421}
     422
     423// End of specialized no-op functions
    382424
    383425// TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model
     
    407449   SceneObject<VertexType, SSBOType>& obj = objects.back();
    408450
    409    if (!is_same_v<VertexType, LaserVertex>) {
     451   if (!is_same_v<VertexType, LaserVertex> && !is_same_v<VertexType, ExplosionVertex>) {
    410452      centerObject(obj);
    411453   }
Note: See TracChangeset for help on using the changeset viewer.