Skip to content

Commit 4ad08d8

Browse files
committed
Vulkan_Helpers: Send matrices to GPU to be calculated
1 parent a5c2e02 commit 4ad08d8

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed
300 Bytes
Binary file not shown.

Vulkan_Helpers/assets/shaders/triangle.vert.glsl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ layout(buffer_reference, std430) readonly buffer VertexBuffer{
2020
//push constants block
2121
layout(push_constant) uniform constants
2222
{
23-
mat4 render_matrix;
23+
mat4 modelMatrix;
24+
mat4 viewMatrix;
25+
mat4 projectionMatrix;
2426
VertexBuffer vertexBuffer;
2527
} PushConstants;
2628

@@ -30,7 +32,7 @@ void main()
3032
Vertex v = PushConstants.vertexBuffer.vertices[gl_VertexIndex];
3133

3234
//output data
33-
gl_Position = PushConstants.render_matrix *vec4(v.position, 1.0f);
35+
gl_Position = PushConstants.projectionMatrix * PushConstants.viewMatrix * PushConstants.modelMatrix * vec4(v.position, 1.0);
3436
outColor = v.color;
3537
outUV = vec2(v.uv_x, v.uv_y);
3638
}

Vulkan_Helpers/src/vk_custom_types.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ struct GPUMeshBuffers {
4848
};
4949

5050
struct GPUDrawPushConstants {
51-
math::float4x4 worldMatrix;
51+
math::float4x4 modelMatrix;
52+
math::float4x4 viewMatrix;
53+
math::float4x4 projectionMatrix;
5254
VkDeviceAddress vertexBufferAddress;
5355
};

Vulkan_Helpers/src/vk_engine.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,10 @@ SDL_AppResult VulkanEngine::DrawGeometry(const VkCommandBuffer& commandBuffer) {
736736

737737
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, meshPipelineLayout, 0, 1, &imageSet, 0, nullptr);
738738

739+
//Uniforms
740+
// > Model Matrix
741+
math::float4x4 model = math::float4x4(1.0f);
742+
739743
// > View Matrix
740744
constexpr float radius = 10.0f;
741745
float camX = sinf(static_cast<float>(SDL_GetTicks()) / 1000.0f) * radius;
@@ -748,15 +752,14 @@ SDL_AppResult VulkanEngine::DrawGeometry(const VkCommandBuffer& commandBuffer) {
748752
// > Projection Matrix
749753
math::int2 screenSize;
750754
SDL_GetWindowSize(window, &screenSize.x, &screenSize.y);
751-
math::float4x4 projection = perspective(math::degrees(45.0f).radians(),
752-
static_cast<float>(screenSize.x) / static_cast<float>(screenSize.y),
753-
0.1f, 1000.0f);
754-
755-
// invert the Y direction on projection matrix so that we are more similar to opengl and gltf axis
756-
// projection[1][1] *= -1;
755+
math::float4x4 proj = perspective(math::degrees(45.0f).radians(),
756+
static_cast<float>(screenSize.x) / static_cast<float>(screenSize.y),
757+
0.1f, 100.0f);
757758

758759
const GPUDrawPushConstants pushConstants{
759-
.worldMatrix = view * projection,
760+
.modelMatrix = model,
761+
.viewMatrix = view,
762+
.projectionMatrix = proj,
760763
.vertexBufferAddress = meshes[selectedMeshIndex]->meshBuffers.vertexBufferAddress,
761764
};
762765

0 commit comments

Comments
 (0)