Squashed commit of the following:

commit 88aed48fa7
Author: jacekpoz <jacekpoz@proton.me>
Date:   Thu Aug 8 18:31:22 2024 +0200

    make ellipses work (thanks krizej!!!)

commit 78cca6becd
Author: jacekpoz <jacekpoz@proton.me>
Date:   Thu Aug 8 17:42:19 2024 +0200

    some tests
This commit is contained in:
jacekpoz 2024-08-08 19:14:50 +02:00
parent b39df59588
commit b2052fb6c4
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
6 changed files with 68 additions and 10 deletions

View file

@ -1,9 +1,19 @@
#version 450
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) in vec2 uv;
layout(location = 0) out vec4 outColor;
layout(location = 0) in vec3 fragColor;
layout(location = 1) in vec2 position;
void main() {
if (shapeType == PTK_COMPONENT_TYPE_ELLIPSE) {
if (length(uv - vec2(0.5)) > 0.5) {
discard;
}
}
outColor = vec4(fragColor, 1.0);
}

View file

@ -7,13 +7,20 @@ 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 inUv;
layout(location = 0) out vec3 fragColor;
layout(location = 1) out vec2 outPosition;
layout(location = 0) out vec2 outPosition;
layout(location = 1) out vec3 fragColor;
layout(location = 2) out int outShapeType;
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;
}

View file

@ -10,17 +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) {
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);
}
@ -28,12 +37,12 @@ void _vk_triangle(PtkTriangle *triangle) {
void vk_triangle(PtkTriangle *triangle) {
PTK_VEC_ADD(PtkHandle, m_components, triangle);
_vk_triangle(triangle);
_vk_triangle(triangle, PTK_COMPONENT_TYPE_TRIANGLE, 0);
vk_transfer_vertex_data();
}
void vk_rect(PtkRect *rect) {
PTK_VEC_ADD(PtkHandle, m_components, rect);
void _vk_rect(PtkRect *rect, PtkComponentType type) {
vec2 t1_positions[3];
// top left
memcpy(t1_positions[0], &(vec2){ rect->top_left[0], rect->top_left[1] }, sizeof(vec2));
@ -43,7 +52,7 @@ void vk_rect(PtkRect *rect) {
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);
_vk_triangle(t1, type, 0);
vec2 t2_positions[3];
// bottom left
@ -54,13 +63,30 @@ void vk_rect(PtkRect *rect) {
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);
_vk_triangle(t2, type, 1);
vk_transfer_vertex_data();
}
void vk_rect(PtkRect *rect) {
PTK_VEC_ADD(PtkHandle, m_components, rect);
_vk_rect(rect, PTK_COMPONENT_TYPE_RECT);
}
void vk_ellipse(PtkEllipse *ellipse) {
PTK_VEC_ADD(PtkHandle, m_components, ellipse);
vec2 top_left = {
ellipse->center[0] - ellipse->radii[0],
ellipse->center[1] - ellipse->radii[1],
};
vec2 size = {
ellipse->radii[0] * 2.0f,
ellipse->radii[1] * 2.0f,
};
_vk_rect((PtkRect *)ptk_rect(top_left, size, ellipse->color), PTK_COMPONENT_TYPE_ELLIPSE);
}
void vk_components_cleanup(void) {

View file

@ -7,6 +7,8 @@
typedef struct {
vec2 pos;
vec3 color;
PtkComponentType shape_type;
vec2 uv;
} Vertex;
PTK_VEC_DEFINE(Vertex);

View file

@ -99,6 +99,18 @@ PTK_VEC(VkVertexInputAttributeDescription) m_vertex_attribute_descriptions = PTK
.format = VK_FORMAT_R32G32B32_SFLOAT,
.offset = offsetof(Vertex, color),
},
(VkVertexInputAttributeDescription){
.location = 2,
.binding = 0,
.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;

View file

@ -10,6 +10,7 @@ TEST_START()
int run_result = ptk_run(PTK_BOX(
ptk_triangle((vec2 []){{400.0f, 50.f}, {700.0f, 500.0f}, {100.0f, 500.0f}}, (vec3){0.0f, 1.0f, 0.0f}),
ptk_rect((vec2){200.0f, 200.0f}, (vec2){300.0f, 300.0f}, (vec3){1.0f, 0.0f, 0.0f}),
ptk_ellipse((vec2){600.0f, 100.0f}, (vec2){50.0f, 25.0f}, (vec3){0.0f, 0.0f, 1.0f}),
));
TEST_ASSERT(run_result == 0, "ptk_run() failed");
});