Skip to content

Commit 5fb65d9

Browse files
committed
Matrix projections
1 parent f0e52b8 commit 5fb65d9

7 files changed

Lines changed: 48 additions & 5 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[submodule "libs/cgltf"]
88
path = libs/cgltf
99
url = https://github.com/jkuhlmann/cgltf.git
10+
[submodule "libs/glm"]
11+
path = libs/glm
12+
url = https://github.com/g-truc/glm.git

libs/glm

Submodule glm added at cc98465

shaders/model_vert.glsl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
layout(location = 0) in vec3 in_position;
44
layout(location = 1) in vec3 in_normal;
55

6+
layout(push_constant) uniform pushConstants {
7+
mat4 modelViewProj;
8+
} u_pushConstants;
9+
610
layout(location = 0) out vec3 out_normal;
711

812
void main() {
9-
gl_Position = vec4(in_position.x, -in_position.y, in_position.z, 1.0);
13+
mat4 modelViewProj = u_pushConstants.modelViewProj;
14+
gl_Position = modelViewProj * vec4(in_position.x, in_position.y, in_position.z, 1.0);
1015
out_normal = in_normal;
1116
}

shaders/model_vert.spv

344 Bytes
Binary file not shown.

src/main.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
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+
266286
void 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

src/vulkan_base/vulkan_base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ void uploadDataToImage(VulkanContext* context, VulkanImage* image, void* data, s
7373
void destroyImage(VulkanContext* context, VulkanImage* image);
7474

7575
VulkanPipeline createPipeline(VulkanContext* context, const char* vertexShaderFilename, const char* fragmentShaderFilename, VkRenderPass renderPass, uint32_t width, uint32_t height,
76-
VkVertexInputAttributeDescription* attributes, uint32_t numAttributes, VkVertexInputBindingDescription* binding, uint32_t numSetLayouts, VkDescriptorSetLayout* setLayouts);
76+
VkVertexInputAttributeDescription* attributes, uint32_t numAttributes, VkVertexInputBindingDescription* binding, uint32_t numSetLayouts, VkDescriptorSetLayout* setLayouts, VkPushConstantRange* pushConstant);
7777
void destroyPipeline(VulkanContext* context, VulkanPipeline* pipeline);

src/vulkan_base/vulkan_pipeline.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ VkShaderModule createShaderModule(VulkanContext* context, const char* shaderFile
2828
}
2929

3030
VulkanPipeline createPipeline(VulkanContext* context, const char* vertexShaderFilename, const char* fragmentShaderFilename, VkRenderPass renderPass, uint32_t width, uint32_t height,
31-
VkVertexInputAttributeDescription* attributes, uint32_t numAttributes, VkVertexInputBindingDescription* binding, uint32_t numSetLayouts, VkDescriptorSetLayout* setLayouts) {
31+
VkVertexInputAttributeDescription* attributes, uint32_t numAttributes, VkVertexInputBindingDescription* binding, uint32_t numSetLayouts, VkDescriptorSetLayout* setLayouts, VkPushConstantRange* pushConstant) {
3232
VkShaderModule vertexShaderModule = createShaderModule(context, vertexShaderFilename);
3333
VkShaderModule fragmentShaderModule = createShaderModule(context, fragmentShaderFilename);
3434

@@ -88,6 +88,8 @@ VulkanPipeline createPipeline(VulkanContext* context, const char* vertexShaderFi
8888
VkPipelineLayoutCreateInfo createInfo = { VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO };
8989
createInfo.setLayoutCount = numSetLayouts;
9090
createInfo.pSetLayouts = setLayouts;
91+
createInfo.pushConstantRangeCount = pushConstant ? 1 : 0;
92+
createInfo.pPushConstantRanges = pushConstant;
9193
VKA(vkCreatePipelineLayout(context->device, &createInfo, 0, &pipelineLayout));
9294
}
9395

0 commit comments

Comments
 (0)