From 88aed48fa72d3cd427e24afa027c9577a86a7108 Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Thu, 8 Aug 2024 18:31:22 +0200 Subject: [PATCH] make ellipses work (thanks krizej!!!) --- shaders/shader.frag.glsl | 8 +++++--- shaders/shader.vert.glsl | 6 ++++-- src/ptk_vk/components.c | 16 ++++++++++++---- src/ptk_vk/components.h | 2 +- src/ptk_vk/init.c | 6 ++++++ 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/shaders/shader.frag.glsl b/shaders/shader.frag.glsl index b58870b..10988bb 100644 --- a/shaders/shader.frag.glsl +++ b/shaders/shader.frag.glsl @@ -5,13 +5,15 @@ int PTK_COMPONENT_TYPE_ELLIPSE = 3; layout(location = 0) in vec2 position; layout(location = 1) in vec3 fragColor; layout(location = 2) flat in int shapeType; -layout(location = 3) flat in vec2 radii; +layout(location = 3) in vec2 uv; layout(location = 0) out vec4 outColor; void main() { - if (shapeType == PTK_COMPONENT_TYPE_ELLIPSE && length(position) <= 0.5) { - discard; + if (shapeType == PTK_COMPONENT_TYPE_ELLIPSE) { + if (length(uv - vec2(0.5)) > 0.5) { + discard; + } } outColor = vec4(fragColor, 1.0); } diff --git a/shaders/shader.vert.glsl b/shaders/shader.vert.glsl index a59c5ca..ba33458 100644 --- a/shaders/shader.vert.glsl +++ b/shaders/shader.vert.glsl @@ -8,17 +8,19 @@ layout(binding = 0) uniform UniformBufferObject { layout(location = 0) in vec2 inPosition; layout(location = 1) in vec3 inColor; layout(location = 2) in int inShapeType; -layout(location = 3) in vec2 inRadii; +layout(location = 3) in vec2 inUv; layout(location = 0) out vec2 outPosition; layout(location = 1) out vec3 fragColor; layout(location = 2) out int outShapeType; -layout(location = 3) out vec2 outRadii; +layout(location = 3) out vec2 outUv; void main() { vec2 normalized = ((inPosition / ubo.initialWindowSize) - 0.5) * 2.0; gl_Position = vec4(normalized, 0.0, 1.0); + fragColor = inColor; outPosition = normalized; outShapeType = inShapeType; + outUv = inUv; } diff --git a/src/ptk_vk/components.c b/src/ptk_vk/components.c index feaac22..98da8c4 100644 --- a/src/ptk_vk/components.c +++ b/src/ptk_vk/components.c @@ -10,18 +10,26 @@ PTK_VEC(PtkHandle) m_components; PTK_VEC(Vertex) g_vertices; +static const vec2 uvs[] = { + {0, 0}, + {0, 1}, + {1, 0}, + {1, 1}, +}; + void vk_components_init(void) { m_components = PTK_VEC_NEW(PtkHandle, 1); g_vertices = PTK_VEC_NEW(Vertex, 1); } -void _vk_triangle(PtkTriangle *triangle, PtkComponentType type) { +void _vk_triangle(PtkTriangle *triangle, PtkComponentType type, size_t uvs_offset) { for (size_t i = 0; i < 3; ++i) { Vertex v; memcpy(v.pos, triangle->vertices[i], sizeof(vec2)); memcpy(v.color, triangle->color, sizeof(vec3)); v.shape_type = type; + memcpy(v.uv, uvs[i + uvs_offset], sizeof(vec2)); PTK_VEC_ADD(Vertex, g_vertices, v); } @@ -30,7 +38,7 @@ void _vk_triangle(PtkTriangle *triangle, PtkComponentType type) { void vk_triangle(PtkTriangle *triangle) { PTK_VEC_ADD(PtkHandle, m_components, triangle); - _vk_triangle(triangle, PTK_COMPONENT_TYPE_TRIANGLE); + _vk_triangle(triangle, PTK_COMPONENT_TYPE_TRIANGLE, 0); vk_transfer_vertex_data(); } @@ -44,7 +52,7 @@ void _vk_rect(PtkRect *rect, PtkComponentType type) { memcpy(t1_positions[2], &(vec2){ rect->top_left[0], rect->top_left[1] + rect->size[1] }, sizeof(vec2)); PtkTriangle *t1 = (PtkTriangle *)ptk_triangle((vec2 *)t1_positions, rect->color); - _vk_triangle(t1, type); + _vk_triangle(t1, type, 0); vec2 t2_positions[3]; // bottom left @@ -55,7 +63,7 @@ void _vk_rect(PtkRect *rect, PtkComponentType type) { memcpy(t2_positions[2], &(vec2){ rect->top_left[0] + rect->size[0], rect->top_left[1] + rect->size[1] }, sizeof(vec2)); PtkTriangle *t2 = (PtkTriangle *)ptk_triangle((vec2 *)t2_positions, rect->color); - _vk_triangle(t2, type); + _vk_triangle(t2, type, 1); vk_transfer_vertex_data(); } diff --git a/src/ptk_vk/components.h b/src/ptk_vk/components.h index 2783757..5fa3995 100644 --- a/src/ptk_vk/components.h +++ b/src/ptk_vk/components.h @@ -8,7 +8,7 @@ typedef struct { vec2 pos; vec3 color; PtkComponentType shape_type; - vec2 radii; + vec2 uv; } Vertex; PTK_VEC_DEFINE(Vertex); diff --git a/src/ptk_vk/init.c b/src/ptk_vk/init.c index b532dea..29b6de0 100644 --- a/src/ptk_vk/init.c +++ b/src/ptk_vk/init.c @@ -105,6 +105,12 @@ PTK_VEC(VkVertexInputAttributeDescription) m_vertex_attribute_descriptions = PTK .format = VK_FORMAT_R32_SINT, .offset = offsetof(Vertex, shape_type), }, + (VkVertexInputAttributeDescription){ + .location = 3, + .binding = 0, + .format = VK_FORMAT_R32G32_SINT, + .offset = offsetof(Vertex, uv), + }, }); static VkBuffer m_vertex_buffer;