55#define STB_IMAGE_IMPLEMENTATION
66#include < stb/stb_image.h>
77
8+ #define GLM_FORCE_DEPTH_ZERO_TO_ONE
9+ #include < glm/glm/ext/matrix_transform.hpp>
10+ #include < glm/glm/gtc/matrix_transform.hpp>
11+
812#include " logger.h"
913#include " vulkan_base/vulkan_base.h"
1014#include " model.h"
@@ -193,7 +197,7 @@ void initApplication(SDL_Window* window) {
193197 vertexInputBinding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
194198 vertexInputBinding.stride = sizeof (float ) * 7 ;
195199 spritePipeline = createPipeline (context, " ../shaders/texture_vert.spv" , " ../shaders/texture_frag.spv" , renderPass, swapchain.width , swapchain.height ,
196- vertexAttributeDescriptions, ARRAY_COUNT (vertexAttributeDescriptions), &vertexInputBinding, 1 , &descriptorLayout);
200+ vertexAttributeDescriptions, ARRAY_COUNT (vertexAttributeDescriptions), &vertexInputBinding, 1 , &descriptorLayout, 0 );
197201
198202
199203 VkVertexInputAttributeDescription modelAttributeDescriptions[2 ] = {};
@@ -209,8 +213,12 @@ void initApplication(SDL_Window* window) {
209213 modelInputBinding.binding = 0 ;
210214 modelInputBinding.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
211215 modelInputBinding.stride = sizeof (float ) * 6 ;
216+ VkPushConstantRange pushConstant = {};
217+ pushConstant.offset = 0 ;
218+ pushConstant.size = sizeof (glm::mat4);
219+ pushConstant.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
212220 modelPipeline = createPipeline (context, " ../shaders/model_vert.spv" , " ../shaders/model_frag.spv" , renderPass, swapchain.width , swapchain.height ,
213- modelAttributeDescriptions, ARRAY_COUNT (modelAttributeDescriptions), &modelInputBinding, 0 , 0 );
221+ modelAttributeDescriptions, ARRAY_COUNT (modelAttributeDescriptions), &modelInputBinding, 0 , 0 , &pushConstant );
214222
215223 for (uint32_t i = 0 ; i < ARRAY_COUNT (fences); ++i) {
216224 VkFenceCreateInfo createInfo = { VK_STRUCTURE_TYPE_FENCE_CREATE_INFO };
@@ -263,8 +271,22 @@ void recreateSwapchain() {
263271 recreateRenderPass ();
264272}
265273
274+ // https://nlguillemot.wordpress.com/2016/12/07/reversed-z-in-opengl/
275+ glm::mat4 getProjectionInverseZ (float fovy, float width, float height, float zNear) {
276+ float f = 1 .0f / tanf (fovy / 2 .0f );
277+ float aspect = width / height;
278+ return glm::mat4 (
279+ f / aspect, 0 .0f , 0 .0f , 0 .0f ,
280+ 0 .0f , -f, 0 .0f , 0 .0f , // -f to flip y axis
281+ 0 .0f , 0 .0f , 0 .0f , 1 .0f ,
282+ 0 .0f , 0 .0f , zNear, 0 .0f
283+ );
284+ }
285+
266286void renderApplication () {
267287 static float greenChannel = 0 .0f ;
288+ static float time = 0 .0f ;
289+ time += 0 .01f ;
268290 greenChannel += 0 .01f ;
269291 if (greenChannel > 1 .0f ) greenChannel = 0 .0f ;
270292 uint32_t imageIndex = 0 ;
@@ -313,10 +335,20 @@ void renderApplication() {
313335 vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, spritePipeline.pipelineLayout, 0, 1, &descriptorSet, 0, 0);
314336 vkCmdDrawIndexed(commandBuffer, ARRAY_COUNT(indexData), 1, 0, 0, 0);
315337#else
338+ glm::mat4 translationMatrix = glm::translate (glm::mat4 (1 .0f ), glm::vec3 (0 .0f , 0 .0f , 2 .0f ));
339+ glm::mat4 scaleMatrix = glm::scale (glm::mat4 (1 .0f ), glm::vec3 (1 .0f ));
340+ glm::mat4 rotationMatrix = glm::rotate (glm::mat4 (1 .0f ), -time, glm::vec3 (0 .0f , 1 .0f , 0 .0f ));
341+ glm::mat4 modelMatrix = translationMatrix * scaleMatrix * rotationMatrix;
342+
343+ // glm::mat4 projection = glm::ortho(0.0f, (float)swapchain.width, 0.0f, (float)swapchain.height, 0.0f, 1000.0f);
344+ glm::mat4 projection = getProjectionInverseZ (glm::radians (80 .0f ), swapchain.width , swapchain.height , 0 .01f );
345+ glm::mat4 modelViewProj = projection * modelMatrix;
346+
316347 vkCmdBindPipeline (commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, modelPipeline.pipeline );
317348 VkDeviceSize offset = 0 ;
318349 vkCmdBindVertexBuffers (commandBuffer, 0 , 1 , &model.vertexBuffer .buffer , &offset);
319350 vkCmdBindIndexBuffer (commandBuffer, model.indexBuffer .buffer , 0 , VK_INDEX_TYPE_UINT16);
351+ vkCmdPushConstants (commandBuffer, modelPipeline.pipelineLayout , VK_SHADER_STAGE_VERTEX_BIT, 0 , sizeof (modelViewProj), &modelViewProj);
320352 vkCmdDrawIndexed (commandBuffer, model.numIndices , 1 , 0 , 0 , 0 );
321353#endif
322354
0 commit comments