Changeset 845a2cb in opengl-game
- Timestamp:
- Apr 22, 2020, 12:58:07 AM (5 years ago)
- Branches:
- points-test
- Parents:
- 73a10ca
- Files:
-
- 2 added
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
gl-shaders/explosion.vert
r73a10ca r845a2cb 19 19 20 20 void main() { 21 /* 21 22 float duration = 0.5; 22 23 float t = cur_time - explosion_start_time[ubo_index] - start_time; … … 36 37 } 37 38 38 vec3 p = vec3(0.0, 0.0, 0.0);// this is the center of the explosion39 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 going39 // 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; 41 42 42 43 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 44 48 } -
graphics-pipeline_vulkan.hpp
r73a10ca r845a2cb 49 49 // if it will never change, just pass it in the constructor and save it 50 50 // If it does change, I could add an updateSwapchainImageCount() function 51 GraphicsPipeline_Vulkan(VkP hysicalDevice physicalDevice, VkDevice device, VkRenderPass renderPass,52 V iewport viewport, vector<VkImage>& swapChainImages,51 GraphicsPipeline_Vulkan(VkPrimitiveTopology topology, VkPhysicalDevice physicalDevice, VkDevice device, 52 VkRenderPass renderPass, Viewport viewport, vector<VkImage>& swapChainImages, 53 53 size_t vertexCapacity, size_t indexCapacity, size_t objectCapacity); 54 54 ~GraphicsPipeline_Vulkan(); … … 86 86 87 87 private: 88 VkPrimitiveTopology topology; 88 89 VkPhysicalDevice physicalDevice; 89 90 VkDevice device; … … 136 137 template<class VertexType, class SSBOType> 137 138 GraphicsPipeline_Vulkan<VertexType, SSBOType>::GraphicsPipeline_Vulkan( 138 VkP hysicalDevice physicalDevice, VkDevice device,139 VkPrimitiveTopology topology, VkPhysicalDevice physicalDevice, VkDevice device, 139 140 VkRenderPass renderPass, Viewport viewport, vector<VkImage>& swapChainImages, 140 141 size_t vertexCapacity, size_t indexCapacity, size_t objectCapacity) { 142 this->topology = topology; 141 143 this->physicalDevice = physicalDevice; 142 144 this->device = device; … … 274 276 VkPipelineInputAssemblyStateCreateInfo inputAssembly = {}; 275 277 inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO; 276 inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;278 inputAssembly.topology = this->topology; 277 279 inputAssembly.primitiveRestartEnable = VK_FALSE; 278 280 -
main-vulkan.cpp
r73a10ca r845a2cb 25 25 26 26 try { 27 game.run(800, 600, GUI_FLAGS_WINDOW_FULLSCREEN);27 game.run(800, 600, 0); 28 28 } catch (const exception& e) { 29 29 cerr << e.what() << endl; -
new-game.cpp
r73a10ca r845a2cb 254 254 255 255 const int KEY_STATE_UNCHANGED = -1; 256 /*** START OF REFACTORED CODE ***/ 256 257 const bool FULLSCREEN = false; 257 258 const int EXPLOSION_PARTICLE_COUNT = 300; 259 /*** END OF REFACTORED CODE ***/ 258 260 unsigned int MAX_UNIFORMS = 0; // Requires OpenGL constants only available at runtime, so it can't be const 259 261 … … 262 264 263 265 /*** START OF REFACTORED CODE ***/ 264 int windowWidth = 640;265 int windowHeight = 480;266 int windowWidth = 800; 267 int windowHeight = 600; 266 268 267 269 vec3 cam_pos; -
vulkan-game.cpp
r73a10ca r845a2cb 3 3 #include <array> 4 4 #include <iostream> 5 #include <numeric> 5 6 #include <set> 6 7 … … 28 29 this->asteroid_VP_mats = {}; 29 30 this->laser_VP_mats = {}; 31 this->explosion_UBO = {}; 30 32 } 31 33 … … 519 521 }, false); 520 522 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 521 528 shipPipeline.createDescriptorSetLayout(); 522 529 shipPipeline.createPipeline("shaders/ship-vert.spv", "shaders/ship-frag.spv"); … … 559 566 laserPipeline.createDescriptorSets(swapChainImages); 560 567 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 561 584 cout << "Created all the graphics pipelines" << endl; 562 585 … … 564 587 565 588 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;571 589 } 572 590 573 591 void 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, 575 594 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, swapChainImages, 4, 6, 0); 576 595 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, 578 598 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, swapChainImages, 16, 24, 10); 579 599 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, 581 602 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, swapChainImages, 138, 138, 10); 582 603 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, 584 606 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, swapChainImages, 24, 36, 10); 585 607 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, 587 610 { 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); 588 616 } 589 617 … … 616 644 laser_VP_mats.view = viewMat; 617 645 laser_VP_mats.proj = projMat; 646 647 explosion_UBO.view = viewMat; 648 explosion_UBO.proj = projMat; 618 649 } 619 650 … … 732 763 break; 733 764 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; 735 766 break; 736 767 default: … … 813 844 vec3 objCenter = vec3(viewMat * vec4(asteroid.center, 1.0f)); 814 845 815 if ( (objCenter.z - asteroid.radius) > -NEAR_CLIP ||asteroid.ssbo.hp <= 0.0f) {846 if (asteroid.ssbo.hp <= 0.0f) { 816 847 asteroid.ssbo.deleted = true; 817 848 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; 819 860 } else { 820 861 asteroid.model_transform = … … 933 974 } 934 975 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 935 984 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_modelPipeline[currentImage], 0, object_VP_mats); 936 985 … … 940 989 941 990 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_laserPipeline[currentImage], 0, laser_VP_mats); 991 992 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_explosionPipeline[currentImage], 0, explosion_UBO); 942 993 } 943 994 … … 1046 1097 asteroidPipeline.cleanupBuffers(); 1047 1098 laserPipeline.cleanupBuffers(); 1099 explosionPipeline.cleanupBuffers(); 1048 1100 1049 1101 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { … … 1538 1590 asteroidPipeline.createRenderCommands(commandBuffers[i], i); 1539 1591 laserPipeline.createRenderCommands(commandBuffers[i], i); 1592 explosionPipeline.createRenderCommands(commandBuffers[i], i); 1540 1593 1541 1594 // Always render this pipeline last … … 1571 1624 } 1572 1625 1573 void VulkanGame::addLaser( 1626 void VulkanGame::addLaser(vec3 start, vec3 end, vec3 color, float width) { 1574 1627 vec3 ray = end - start; 1575 1628 float length = glm::length(ray); … … 1792 1845 } 1793 1846 1847 void 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 1794 1884 // TODO: Fix the crash that happens when alt-tabbing 1795 1885 void VulkanGame::recreateSwapChain() { … … 1853 1943 laserPipeline.createDescriptorPool(swapChainImages); 1854 1944 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); 1855 1953 1856 1954 createCommandBuffers(); … … 1871 1969 asteroidPipeline.cleanup(); 1872 1970 laserPipeline.cleanup(); 1971 explosionPipeline.cleanup(); 1873 1972 1874 1973 for (size_t i = 0; i < uniformBuffers_modelPipeline.size(); i++) { … … 1892 1991 } 1893 1992 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 1894 1998 vkDestroyRenderPass(device, renderPass, nullptr); 1895 1999 -
vulkan-game.hpp
r73a10ca r845a2cb 57 57 }; 58 58 59 struct UBO_VP_mats { 60 alignas(16) mat4 view; 61 alignas(16) mat4 proj; 59 struct ExplosionVertex { 60 vec3 particleStartVelocity; 61 float particleStartTime; 62 unsigned int objIndex; 62 63 }; 63 64 … … 76 77 alignas(4) vec3 color; 77 78 alignas(4) unsigned int deleted; 79 }; 80 81 struct 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 88 struct UBO_VP_mats { 89 alignas(16) mat4 view; 90 alignas(16) mat4 proj; 91 }; 92 93 struct UBO_Explosion { 94 alignas(16) mat4 view; 95 alignas(16) mat4 proj; 96 alignas(4) float cur_time; 78 97 }; 79 98 … … 175 194 176 195 private: 196 // TODO: Make these consts static 197 177 198 const int MAX_FRAMES_IN_FLIGHT; 178 199 … … 180 201 const float FAR_CLIP = 100.0f; 181 202 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; 182 205 183 206 vec3 cam_pos; … … 285 308 286 309 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; 287 319 288 320 vector<BaseEffectOverTime*> effects; … … 334 366 void createSyncObjects(); 335 367 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 342 368 // TODO: Since addObject() returns a reference to the new object now, 343 369 // stop using objects.back() to access the object that was just created … … 366 392 void centerObject(SceneObject<VertexType, SSBOType>& object); 367 393 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 368 402 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags, 369 403 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, … … 380 414 void* pUserData); 381 415 }; 416 417 // Start of specialized no-op functions 418 419 template<> 420 inline void VulkanGame::centerObject(SceneObject<ExplosionVertex, SSBO_Explosion>& object) { 421 } 422 423 // End of specialized no-op functions 382 424 383 425 // TODO: Right now, it's basically necessary to pass the identity matrix in for ssbo.model … … 407 449 SceneObject<VertexType, SSBOType>& obj = objects.back(); 408 450 409 if (!is_same_v<VertexType, LaserVertex> ) {451 if (!is_same_v<VertexType, LaserVertex> && !is_same_v<VertexType, ExplosionVertex>) { 410 452 centerObject(obj); 411 453 }
Note:
See TracChangeset
for help on using the changeset viewer.