Changeset 4a9416a in opengl-game for vulkan-game.cpp
- Timestamp:
- Apr 26, 2020, 9:50:00 PM (4 years ago)
- Branches:
- feature/imgui-sdl, master
- Children:
- b8d4456
- Parents:
- 52a02e6
- git-author:
- Dmitry Portnoy <dmitry.portnoy@…> (04/26/20 19:00:18)
- git-committer:
- Dmitry Portnoy <dmitry.portnoy@…> (04/26/20 21:50:00)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
r52a02e6 r4a9416a 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 … … 564 566 laserPipeline.createDescriptorSets(swapChainImages); 565 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 566 584 cout << "Created all the graphics pipelines" << endl; 567 585 … … 591 609 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, physicalDevice, device, renderPass, 592 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); 593 616 } 594 617 … … 621 644 laser_VP_mats.view = viewMat; 622 645 laser_VP_mats.proj = projMat; 646 647 explosion_UBO.view = viewMat; 648 explosion_UBO.proj = projMat; 623 649 } 624 650 … … 691 717 vec3(-0.21f, -1.19f, 1.76f) + offset, 692 718 vec3(-0.21f, -1.19f, -3.0f) + offset, 693 vec3(1.0f, 0.0f, 0.0f), 0.03f);719 LASER_COLOR, 0.03f); 694 720 695 721 leftLaserIdx = laserObjects.size() - 1; … … 700 726 vec3(0.21f, -1.19f, 1.76f) + offset, 701 727 vec3(0.21f, -1.19f, -3.0f) + offset, 702 vec3(0.0f, 1.0f, 0.0f), 0.03f);728 LASER_COLOR, 0.03f); 703 729 704 730 rightLaserIdx = laserObjects.size() - 1; … … 818 844 vec3 objCenter = vec3(viewMat * vec4(asteroid.center, 1.0f)); 819 845 820 if ( (objCenter.z - asteroid.radius) > -NEAR_CLIP ||asteroid.ssbo.hp <= 0.0f) {846 if (asteroid.ssbo.hp <= 0.0f) { 821 847 asteroid.ssbo.deleted = true; 822 848 823 // 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; 824 860 } else { 825 861 asteroid.model_transform = … … 914 950 } 915 951 952 for (SceneObject<ExplosionVertex, SSBO_Explosion>& explosion : this->explosionObjects) { 953 if (!explosion.ssbo.deleted) { 954 if (curTime > (explosion.ssbo.explosionStartTime + explosion.ssbo.explosionDuration)) { 955 explosion.ssbo.deleted = true; 956 explosion.modified = true; 957 } 958 } 959 } 960 916 961 for (size_t i = 0; i < shipObjects.size(); i++) { 917 962 if (shipObjects[i].modified) { … … 938 983 } 939 984 985 for (size_t i = 0; i < explosionObjects.size(); i++) { 986 if (explosionObjects[i].modified) { 987 updateObject(explosionObjects, explosionPipeline, i); 988 } 989 } 990 991 explosion_UBO.cur_time = curTime; 992 940 993 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_modelPipeline[currentImage], 0, object_VP_mats); 941 994 … … 945 998 946 999 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_laserPipeline[currentImage], 0, laser_VP_mats); 1000 1001 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_explosionPipeline[currentImage], 0, explosion_UBO); 947 1002 } 948 1003 … … 1051 1106 asteroidPipeline.cleanupBuffers(); 1052 1107 laserPipeline.cleanupBuffers(); 1108 explosionPipeline.cleanupBuffers(); 1053 1109 1054 1110 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { … … 1543 1599 asteroidPipeline.createRenderCommands(commandBuffers[i], i); 1544 1600 laserPipeline.createRenderCommands(commandBuffers[i], i); 1601 explosionPipeline.createRenderCommands(commandBuffers[i], i); 1545 1602 1546 1603 // Always render this pipeline last … … 1797 1854 } 1798 1855 1856 void VulkanGame::addExplosion(mat4 model_mat, float duration, float cur_time) { 1857 vector<ExplosionVertex> vertices; 1858 vertices.reserve(EXPLOSION_PARTICLE_COUNT); 1859 1860 float particle_start_time = 0.0f; 1861 1862 for (int i = 0; i < EXPLOSION_PARTICLE_COUNT; i++) { 1863 float randx = ((float)rand() / (float)RAND_MAX) - 0.5f; 1864 float randy = ((float)rand() / (float)RAND_MAX) - 0.5f; 1865 1866 vertices.push_back({ vec3(randx, randy, 0.0f), particle_start_time}); 1867 1868 particle_start_time += .01f; 1869 // TODO: Get this working 1870 // particle_start_time += 1.0f * EXPLOSION_PARTICLE_COUNT / duration 1871 } 1872 1873 // Fill the indices with the the first EXPLOSION_PARTICLE_COUNT ints 1874 vector<uint16_t> indices(EXPLOSION_PARTICLE_COUNT); 1875 iota(indices.begin(), indices.end(), 0); 1876 1877 SceneObject<ExplosionVertex, SSBO_Explosion>& explosion = addObject( 1878 explosionObjects, explosionPipeline, 1879 addObjectIndex(explosionObjects.size(), vertices), 1880 indices, { 1881 mat4(1.0f), 1882 cur_time, 1883 duration, 1884 false 1885 }, true); 1886 1887 explosion.model_base = model_mat; 1888 explosion.model_transform = mat4(1.0f); 1889 1890 explosion.modified = true; 1891 } 1892 1799 1893 // TODO: Fix the crash that happens when alt-tabbing 1800 1894 void VulkanGame::recreateSwapChain() { … … 1858 1952 laserPipeline.createDescriptorPool(swapChainImages); 1859 1953 laserPipeline.createDescriptorSets(swapChainImages); 1954 1955 createBufferSet(sizeof(UBO_Explosion), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 1956 uniformBuffers_explosionPipeline, uniformBuffersMemory_explosionPipeline, uniformBufferInfoList_explosionPipeline); 1957 1958 explosionPipeline.updateRenderPass(renderPass); 1959 explosionPipeline.createPipeline("shaders/explosion-vert.spv", "shaders/explosion-frag.spv"); 1960 explosionPipeline.createDescriptorPool(swapChainImages); 1961 explosionPipeline.createDescriptorSets(swapChainImages); 1860 1962 1861 1963 createCommandBuffers(); … … 1876 1978 asteroidPipeline.cleanup(); 1877 1979 laserPipeline.cleanup(); 1980 explosionPipeline.cleanup(); 1878 1981 1879 1982 for (size_t i = 0; i < uniformBuffers_modelPipeline.size(); i++) { … … 1897 2000 } 1898 2001 2002 for (size_t i = 0; i < uniformBuffers_explosionPipeline.size(); i++) { 2003 vkDestroyBuffer(device, uniformBuffers_explosionPipeline[i], nullptr); 2004 vkFreeMemory(device, uniformBuffersMemory_explosionPipeline[i], nullptr); 2005 } 2006 1899 2007 vkDestroyRenderPass(device, renderPass, nullptr); 1900 2008
Note:
See TracChangeset
for help on using the changeset viewer.