From f2535de9a92f67901564fe35b7fe0dec1953232f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Gasi=C7=B9ski?= Date: Sun, 14 Jul 2024 20:36:07 +0200 Subject: [PATCH] Add colours to the mesh --- src/shaders/shader.frag | 4 +++- src/shaders/shader.vert | 5 ++++- src/utilities.zig | 5 ++++- src/vulkan_renderer.zig | 24 +++++++++++++++--------- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/shaders/shader.frag b/src/shaders/shader.frag index 40de65c..76d73d7 100644 --- a/src/shaders/shader.frag +++ b/src/shaders/shader.frag @@ -1,8 +1,10 @@ #version 450 +layout(location = 0) in vec3 fragCol; + // Final output output (must also have location) layout(location = 0) out vec4 outColour; void main() { - outColour = vec4(1.0, 0.0, 0.0, 1.0); + outColour = vec4(fragCol, 1.0); } diff --git a/src/shaders/shader.vert b/src/shaders/shader.vert index 340b1bc..eceb2d8 100644 --- a/src/shaders/shader.vert +++ b/src/shaders/shader.vert @@ -1,8 +1,11 @@ #version 450 layout(location = 0) in vec3 pos; +layout(location = 1) in vec3 col; + +layout(location = 0) out vec3 fragCol; void main() { gl_Position = vec4(pos, 1.0); - // fragColour = colours[gl_VertexIndex]; + fragCol = col; } diff --git a/src/utilities.zig b/src/utilities.zig index fa766d5..6b62c5e 100644 --- a/src/utilities.zig +++ b/src/utilities.zig @@ -3,10 +3,13 @@ const vk = @import("vulkan"); pub const device_extensions = [_][*:0]const u8{vk.extensions.khr_swapchain.name}; +pub const Vector3 = @Vector(3, f32); + // Vertex data representation pub const Vertex = struct { // Vertex position (x, y, z) - pos: @Vector(3, f32), + pos: Vector3, + col: Vector3, }; pub const QueueFamilyIndices = struct { diff --git a/src/vulkan_renderer.zig b/src/vulkan_renderer.zig index bf41aa8..b3cd448 100644 --- a/src/vulkan_renderer.zig +++ b/src/vulkan_renderer.zig @@ -104,9 +104,12 @@ pub const VulkanRenderer = struct { // Create mesh var mesh_vertices = [_]Vertex{ - .{ .pos = .{ 0.0, -0.4, 0.0 } }, - .{ .pos = .{ 0.4, 0.4, 0.0 } }, - .{ .pos = .{ -0.4, 0.4, 0.0 } }, + .{ .pos = .{ -0.5, -0.5, 0.0 }, .col = .{ 1.0, 0.0, 0.0 } }, + .{ .pos = .{ 0.5, -0.5, 0.0 }, .col = .{ 0.0, 1.0, 0.0 } }, + .{ .pos = .{ 0.5, 0.5, 0.0 }, .col = .{ 0.0, 0.0, 1.0 } }, + .{ .pos = .{ 0.5, 0.5, 0.0 }, .col = .{ 0.0, 0.0, 1.0 } }, + .{ .pos = .{ -0.5, 0.5, 0.0 }, .col = .{ 0.0, 1.0, 0.0 } }, + .{ .pos = .{ -0.5, -0.5, 0.0 }, .col = .{ 1.0, 0.0, 0.0 } }, }; self.first_mesh = try Mesh.new(self.instance, self.physical_device, self.device, &mesh_vertices); @@ -154,7 +157,6 @@ pub const VulkanRenderer = struct { // Submit command buffer to queue try self.device.queueSubmit(self.graphics_queue.handle, 1, @ptrCast(&submit_info), self.draw_fences[self.current_frame]); - // try self.device.queueSubmit(self.graphics_queue.handle, 1, @ptrCast(&submit_info), self.frame_fence); // -- Present rendered image to screen const present_info: vk.PresentInfoKHR = .{ @@ -506,6 +508,13 @@ pub const VulkanRenderer = struct { .format = vk.Format.r32g32b32_sfloat, // Format the data will take (also helps define size of data) .offset = @offsetOf(Vertex, "pos"), // Where this attribute is defined in data for a single vertex }, + // Colour attribute + .{ + .binding = 0, + .location = 1, + .format = vk.Format.r32g32b32_sfloat, + .offset = @offsetOf(Vertex, "col"), + }, }; // -- Vertex input -- @@ -1044,16 +1053,13 @@ fn getDebugUtilsCreateInfo() vk.DebugUtilsMessengerCreateInfoEXT { }; } -// message_severity: vk.DebugUtilsMessageSeverityFlagsEXT, -// message_types: vk.DebugUtilsMessageTypeFlagsEXT, -// p_callback_data: ?*const vk.DebugUtilsMessengerCallbackDataEXT, -// p_user_data: ?*anyopaque, fn debugCallback( message_severity: vk.DebugUtilsMessageSeverityFlagsEXT, message_types: vk.DebugUtilsMessageTypeFlagsEXT, p_callback_data: ?*const vk.DebugUtilsMessengerCallbackDataEXT, - _: ?*anyopaque, + p_user_data: ?*anyopaque, ) callconv(vk.vulkan_call_conv) vk.Bool32 { + _ = p_user_data; const severity = getMessageSeverityLabel(message_severity); const message_type = getMessageTypeLabel(message_types);