Changeset 4994692 in opengl-game
- Timestamp:
- Apr 19, 2020, 1:23:02 AM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 5ba732a
- Parents:
- 6385d0f
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
main-vulkan.cpp
r6385d0f r4994692 11 11 12 12 int main(int argc, char* argv[]) { 13 // TODO: Add date & time to the crash log 13 14 CrashLogger logger(__main, argc, argv); 14 15 -
new-game.cpp
r6385d0f r4994692 501 501 defineModelGroupAttrib(modelGroups[TYPE_SHIP], "ubo_index", ATTRIB_OBJECT_VARYING, 502 502 1, GL_UNSIGNED_INT, offset_of(&SceneObject::ubo_offset)); 503 /*** END OF REFACTORED CODE ***/504 503 505 504 defineModelGroupUniform(modelGroups[TYPE_SHIP], "view", ATTRIB_UNIFORM, … … 510 509 initModelGroupAttribs(modelGroups[TYPE_SHIP]); 511 510 512 /*** START OF REFACTORED CODE ***/513 511 modelGroups[TYPE_ASTEROID] = createModelGroup( 514 512 loadShaderProgram("gl-shaders/asteroid.vert", "gl-shaders/asteroid.frag")); … … 523 521 defineModelGroupAttrib(modelGroups[TYPE_ASTEROID], "ubo_index", ATTRIB_OBJECT_VARYING, 524 522 1, GL_UNSIGNED_INT, offset_of(&SceneObject::ubo_offset)); 525 /*** END OF REFACTORED CODE ***/526 523 527 524 defineModelGroupUniform(modelGroups[TYPE_ASTEROID], "view", ATTRIB_UNIFORM, … … 532 529 initModelGroupAttribs(modelGroups[TYPE_ASTEROID]); 533 530 534 /*** START OF REFACTORED CODE ***/535 531 modelGroups[TYPE_LASER] = createModelGroup( 536 532 loadShaderProgram("gl-shaders/laser.vert", "gl-shaders/laser.frag")); … … 543 539 defineModelGroupAttrib(modelGroups[TYPE_LASER], "ubo_index", ATTRIB_OBJECT_VARYING, 544 540 1, GL_UNSIGNED_INT, offset_of(&SceneObject::ubo_offset)); 545 /*** END OF REFACTORED CODE ***/546 541 547 542 defineModelGroupUniform(modelGroups[TYPE_LASER], "view", ATTRIB_UNIFORM, … … 553 548 554 549 initModelGroupAttribs(modelGroups[TYPE_LASER]); 555 556 /*** START OF REFACTORED CODE ***/ 550 /*** END OF REFACTORED CODE ***/ 551 557 552 modelGroups[TYPE_EXPLOSION] = createModelGroup( 558 553 loadShaderProgram("gl-shaders/explosion.vert", "gl-shaders/explosion.frag")); … … 565 560 defineModelGroupAttrib(modelGroups[TYPE_EXPLOSION], "ubo_index", ATTRIB_OBJECT_VARYING, 566 561 1, GL_UNSIGNED_INT, offset_of(&SceneObject::ubo_offset)); 567 /*** END OF REFACTORED CODE ***/568 562 569 563 defineModelGroupUniform(modelGroups[TYPE_EXPLOSION], "cur_time", ATTRIB_UNIFORM, … … 829 823 830 824 leftLaser = createLaser( 831 vec3(-0.21f, -1.19f, 1.76f) +offset,832 vec3(-0.21f, -1.19f, -3.0f) +offset,825 vec3(-0.21f, -1.19f, 1.76f) + offset, 826 vec3(-0.21f, -1.19f, -3.0f) + offset, 833 827 vec3(0.0f, 1.0f, 0.0f), 0.03f); 834 828 addObjectToScene(leftLaser, shaderBufferInfo, modelGroups, ubo); … … 1887 1881 // To project point P onto line AB: 1888 1882 // projection = A + dot(AP,AB) / dot(AB,AB) * AB 1889 vec3 projOnLaser = start + glm::dot(cam_pos -start, ray) / (length*length) * ray;1883 vec3 projOnLaser = start + glm::dot(cam_pos - start, ray) / (length * length) * ray; 1890 1884 vec3 laserToCam = cam_pos - projOnLaser; 1891 1885 … … 1944 1938 } 1945 1939 1940 /*** START OF REFACTORED CODE ***/ 1946 1941 void defineModelGroupUniform(ShaderModelGroup& modelGroup, string name, AttribType attribType, 1947 1942 GLint size, UniformType type, GLfloat* data) { … … 1957 1952 1958 1953 void initModelGroupAttribs(ShaderModelGroup& modelGroup) { 1959 /*** START OF REFACTORED CODE ***/1960 1954 glBindVertexArray(modelGroup.vao); 1961 1955 1962 1956 map<string, AttribInfo>::iterator it; 1963 1957 for (it = modelGroup.attribs.begin(); it != modelGroup.attribs.end(); it++) { 1964 /*** END OF REFACTORED CODE ***/1965 1958 if (it->second.attribType == ATTRIB_UNIFORM) { 1966 1959 it->second.buffer = glGetUniformLocation(modelGroup.shaderProgram, it->first.c_str()); 1967 /*** START OF REFACTORED CODE ***/1968 1960 } else { 1969 1961 glEnableVertexAttribArray(it->second.index); … … 1984 1976 } 1985 1977 } 1986 /*** START OF REFACTORED CODE ***/ 1987 } 1978 } 1979 /*** END OF REFACTORED CODE ***/ 1988 1980 1989 1981 void bindUniformData(AttribInfo& attrib) { -
vulkan-game.cpp
r6385d0f r4994692 12 12 using namespace std; 13 13 14 // TODO: Update all occurances of instance variables to use this-> 15 14 16 VulkanGame::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 = {}; 27 29 } 28 30 … … 226 228 modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 227 229 VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_modelPipeline); 228 modelPipeline.addStorageDescriptor(); 229 230 modelPipeline.addStorageDescriptor(VK_SHADER_STAGE_VERTEX_BIT); 230 231 modelPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 231 232 VK_SHADER_STAGE_FRAGMENT_BIT, &floorTextureImageDescriptor); 232 233 233 addObject(modelObjects, modelPipeline, 234 SceneObject<ModelVertex, SSBO_ModelObject>* texturedSquare = nullptr; 235 236 texturedSquare = &addObject(modelObjects, modelPipeline, 234 237 addObjectIndex<ModelVertex>(modelObjects.size(), { 235 238 {{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}}, … … 243 246 }, false); 244 247 245 modelObjects.back().model_base =248 texturedSquare->model_base = 246 249 translate(mat4(1.0f), vec3(0.0f, 0.0f, -2.0f)); 247 248 250 updateObject(modelObjects, modelPipeline, modelObjects.size() - 1); 249 251 250 addObject(modelObjects, modelPipeline,252 texturedSquare = &addObject(modelObjects, modelPipeline, 251 253 addObjectIndex<ModelVertex>(modelObjects.size(), { 252 254 {{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}}, … … 260 262 }, false); 261 263 262 modelObjects.back().model_base =264 texturedSquare->model_base = 263 265 translate(mat4(1.0f), vec3(0.0f, 0.0f, -1.5f)); 264 265 266 updateObject(modelObjects, modelPipeline, modelObjects.size() - 1); 266 267 … … 280 281 shipPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 281 282 VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_shipPipeline); 282 shipPipeline.addStorageDescriptor( );283 shipPipeline.addStorageDescriptor(VK_SHADER_STAGE_VERTEX_BIT); 283 284 284 285 // TODO: With the normals, indexing basically becomes pointless since no vertices will have exactly 285 286 // 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, 287 288 addObjectIndex<ShipVertex>(shipObjects.size(), 288 289 addVertexNormals<ShipVertex>({ … … 532 533 asteroidPipeline.addDescriptorInfo(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 533 534 VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_asteroidPipeline); 534 asteroidPipeline.addStorageDescriptor( );535 asteroidPipeline.addStorageDescriptor(VK_SHADER_STAGE_VERTEX_BIT); 535 536 536 537 asteroidPipeline.createDescriptorSetLayout(); … … 545 546 createSyncObjects(); 546 547 547 ship Objects[0].model_base =548 ship.model_base = 548 549 translate(mat4(1.0f), vec3(0.0f, -1.2f, 1.65f)) * 549 550 scale(mat4(1.0f), vec3(0.1f, 0.1f, 0.1f)); 550 551 551 updateObject(shipObjects, shipPipeline, 0); 552 552 } … … 568 568 // TODO: Maybe changes the name to initScene() or something similar 569 569 void VulkanGame::initMatrices() { 570 cam_pos = vec3(0.0f, 0.0f, 2.0f);570 this->cam_pos = vec3(0.0f, 0.0f, 2.0f); 571 571 572 572 float cam_yaw = 0.0f; … … 577 577 578 578 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)); 580 580 viewMat = R_view * T_view; 581 581 … … 625 625 break; 626 626 case UI_EVENT_KEYDOWN: 627 if (e.key.repeat) { 628 break; 629 } 630 627 631 if (e.key.keycode == SDL_SCANCODE_ESCAPE) { 628 632 quit = true; … … 631 635 float zOffset = -2.0f + (0.5f * modelObjects.size()); 632 636 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 = 646 651 translate(mat4(1.0f), vec3(0.0f, 0.0f, zOffset)); 647 648 652 updateObject(modelObjects, modelPipeline, modelObjects.size() - 1); 649 653 } else { … … 671 675 // Check which keys are held down 672 676 677 SceneObject<ShipVertex, SSBO_ModelObject>& ship = shipObjects[0]; 678 673 679 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)) 675 683 * shipObjects[0].model_transform; 676 684 677 685 updateObject(shipObjects, shipPipeline, 0); 678 686 } 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)) 680 690 * shipObjects[0].model_transform; 681 691 … … 721 731 this->lastSpawn_asteroid = this->curTime; 722 732 723 addObject(asteroidObjects, asteroidPipeline, 733 SceneObject<AsteroidVertex, SSBO_Asteroid>& asteroid = addObject( 734 asteroidObjects, asteroidPipeline, 724 735 addObjectIndex<AsteroidVertex>(asteroidObjects.size(), 725 736 addVertexNormals<AsteroidVertex>({ … … 782 793 mat4(1.0f), 783 794 10.0f, 784 0795 false 785 796 }, true); 786 797 … … 790 801 // TODO: Figure out the best way to take scaling into account when calculating the radius 791 802 // Keep in mind that the main complicating factor is the currently poor radius calculation 792 asteroid Objects.back().radius /= 8.0f;793 794 asteroid Objects.back().model_base =803 asteroid.radius /= 8.0f; 804 805 asteroid.model_base = 795 806 translate(mat4(1.0f), vec3(getRandomNum(-1.3f, 1.3f), -1.2f, getRandomNum(-5.5f, -4.5f))) * 796 807 rotate(mat4(1.0f), radians(60.0f), vec3(1.0f, 1.0f, -1.0f)) * … … 900 911 cleanupSwapChain(); 901 912 913 VulkanUtils::destroyVulkanImage(device, sdlOverlayImage); 902 914 VulkanUtils::destroyVulkanImage(device, floorTextureImage); 903 VulkanUtils::destroyVulkanImage(device, sdlOverlayImage);904 915 905 916 vkDestroySampler(device, textureSampler, nullptr); … … 1282 1293 createTextureSampler(); 1283 1294 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 1284 1304 VulkanUtils::createVulkanImageFromFile(device, physicalDevice, commandPool, "textures/texture.jpg", 1285 1305 floorTextureImage, graphicsQueue); … … 1289 1309 floorTextureImageDescriptor.imageView = floorTextureImage.imageView; 1290 1310 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;1298 1311 } 1299 1312 -
vulkan-game.hpp
r6385d0f r4994692 51 51 }; 52 52 53 struct UBO_VP_mats { 54 alignas(16) mat4 view; 55 alignas(16) mat4 proj; 56 }; 57 58 struct SSBO_ModelObject { 59 alignas(16) mat4 model; 60 }; 61 62 struct SSBO_Asteroid { 63 alignas(16) mat4 model; 64 alignas(4) float hp; 65 alignas(4) unsigned int deleted; 66 }; 67 53 68 // TODO: Change the index type to uint32_t and check the Vulkan Tutorial loading model section as a reference 54 69 // TODO: Create a typedef for index type so I can easily change uin16_t to something else later 70 // TODO: Maybe create a typedef for each of the templated SceneObject types 55 71 template<class VertexType, class SSBOType> 56 72 struct SceneObject { … … 61 77 mat4 model_base; 62 78 mat4 model_transform; 79 80 // TODO: Figure out if I should make child classes that have these fields instead of putting them in the 81 // parent class 63 82 vec3 center; // currently only matters for asteroids 64 83 float radius; // currently only matters for asteroids 65 84 }; 66 85 67 struct UBO_VP_mats { 68 alignas(16) mat4 view; 69 alignas(16) mat4 proj; 70 }; 71 72 struct SSBO_ModelObject { 73 alignas(16) mat4 model; 74 }; 75 76 struct SSBO_Asteroid { 77 alignas(16) mat4 model; 78 alignas(4) float hp; 79 alignas(4) unsigned int deleted; 80 }; 81 82 // Have to figure out how to include an optional ssbo parameter for each object 86 // TODO: Have to figure out how to include an optional ssbo parameter for each object 83 87 // Could probably use the same approach to make indices optional 88 // Figure out if there are sufficient use cases to make either of these optional or is it fine to make 89 // them mamdatory 84 90 85 91 class VulkanGame { … … 131 137 VkSampler textureSampler; 132 138 139 VulkanImage sdlOverlayImage; 140 VkDescriptorImageInfo sdlOverlayImageDescriptor; 141 133 142 VulkanImage floorTextureImage; 134 143 VkDescriptorImageInfo floorTextureImageDescriptor; 135 136 VulkanImage sdlOverlayImage;137 VkDescriptorImageInfo sdlOverlayImageDescriptor;138 144 139 145 TTF_Font* font; … … 230 236 void createSyncObjects(); 231 237 238 // TODO: Since addObject() returns a reference to the new object now, 239 // stop using objects.back() to access the object that was just created 232 240 template<class VertexType, class SSBOType> 233 void addObject(vector<SceneObject<VertexType, SSBOType>>& objects, 234 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, 235 const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo, 236 bool pipelinesCreated); 241 SceneObject<VertexType, SSBOType>& addObject( 242 vector<SceneObject<VertexType, SSBOType>>& objects, 243 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, 244 const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo, 245 bool pipelinesCreated); 237 246 238 247 template<class VertexType, class SSBOType> 239 248 void updateObject(vector<SceneObject<VertexType, SSBOType>>& objects, 240 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index);249 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, size_t index); 241 250 242 251 template<class VertexType> … … 250 259 251 260 void createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags, 252 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList); 261 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, 262 vector<VkDescriptorBufferInfo>& bufferInfoList); 253 263 254 264 void recreateSwapChain(); … … 272 282 // to account for scaling 273 283 template<class VertexType, class SSBOType> 274 void VulkanGame::addObject(vector<SceneObject<VertexType, SSBOType>>& objects, 284 SceneObject<VertexType, SSBOType>& VulkanGame::addObject( 285 vector<SceneObject<VertexType, SSBOType>>& objects, 275 286 GraphicsPipeline_Vulkan<VertexType, SSBOType>& pipeline, 276 287 const vector<VertexType>& vertices, vector<uint16_t> indices, SSBOType ssbo, … … 289 300 centerObject(obj); 290 301 291 bool storageBufferResized = pipeline.addObject(obj.vertices, obj.indices, obj.ssbo, commandPool, graphicsQueue); 302 bool storageBufferResized = pipeline.addObject(obj.vertices, obj.indices, obj.ssbo, 303 this->commandPool, this->graphicsQueue); 292 304 293 305 if (pipelinesCreated) { … … 307 319 createCommandBuffers(); 308 320 } 321 322 return obj; 309 323 } 310 324
Note:
See TracChangeset
for help on using the changeset viewer.