remove junky UncoloredSpaceTriangle union, hide vertex generation from generic triangle implementation

This commit is contained in:
veclavtalica 2024-11-03 23:08:53 +03:00
parent 26c75ffd7c
commit d794ca862f
3 changed files with 48 additions and 62 deletions

View File

@ -83,32 +83,19 @@ typedef struct Primitive2D {
} Primitive2D;
/* union for in-place recalculation of texture coordinates */
union UncoloredSpaceTriangle {
/* pending for sending, uvs are not final as texture atlases could update */
struct UncoloredSpaceTrianglePrimitive {
Vec3 v0;
Vec2 uv0; /* in pixels */
Vec3 v1;
Vec2 uv1; /* in pixels */
Vec3 v2;
Vec2 uv2; /* in pixels */
} primitive;
/* TODO: have it packed? */
/* structure that is passed in opengl vertex array */
struct UncoloredSpaceTrianglePayload {
Vec3 v0;
Vec2 uv0;
Vec3 v1;
Vec2 uv1;
Vec3 v2;
Vec2 uv2;
} payload;
};
/* needs to be later resolved in texture atlas */
typedef struct UncoloredSpaceTriangle {
Vec3 v0;
Vec2 uv0; /* in pixels */
Vec3 v1;
Vec2 uv1; /* in pixels */
Vec3 v2;
Vec2 uv2; /* in pixels */
} UncoloredSpaceTriangle;
/* batch of primitives with overlapping properties */
typedef struct MeshBatch {
uint8_t *primitives;
uint8_t *primitives; /* note: interpretation of it is arbitrary */
} MeshBatch;
/* TODO: use atlas id instead */

View File

@ -780,29 +780,57 @@ void finally_draw_uncolored_space_traingle_batch(const MeshBatch *batch,
const TextureKey texture_key,
const VertexBuffer buffer)
{
const size_t primitives_len = arrlenu(batch->primitives);
/* nothing to do */
if (primitives_len == 0)
return;
const Rect srcrect = textures_get_srcrect(&ctx.texture_cache, texture_key);
const Rect dims = textures_get_dims(&ctx.texture_cache, texture_key);
const float wr = srcrect.w / dims.w;
const float hr = srcrect.h / dims.h;
const float xr = srcrect.x / dims.w;
const float yr = srcrect.y / dims.h;
/* update pixel-based uvs to correspond with texture atlases */
for (size_t i = 0; i < primitives_len; ++i) {
UncoloredSpaceTriangle *payload =
&((UncoloredSpaceTriangle *)(void *)batch->primitives)[i];
payload->uv0.x = xr + ((float)payload->uv0.x / srcrect.w) * wr;
payload->uv0.y = yr + ((float)payload->uv0.y / srcrect.h) * hr;
payload->uv1.x = xr + ((float)payload->uv1.x / srcrect.w) * wr;
payload->uv1.y = yr + ((float)payload->uv1.y / srcrect.h) * hr;
payload->uv2.x = xr + ((float)payload->uv2.x / srcrect.w) * wr;
payload->uv2.y = yr + ((float)payload->uv2.y / srcrect.h) * hr;
}
specify_vertex_buffer(buffer, batch->primitives, primitives_len * sizeof (UncoloredSpaceTriangle));
DeferredCommandDraw command = {0};
command.vertices = (AttributeArrayPointer) {
.arity = 3,
.type = GL_FLOAT,
.stride = offsetof(struct UncoloredSpaceTrianglePayload, v1),
.offset = offsetof(struct UncoloredSpaceTrianglePayload, v0),
.stride = offsetof(UncoloredSpaceTriangle, v1),
.offset = offsetof(UncoloredSpaceTriangle, v0),
.buffer = buffer
};
command.texcoords = (AttributeArrayPointer) {
.arity = 2,
.type = GL_FLOAT,
.stride = offsetof(struct UncoloredSpaceTrianglePayload, v1),
.offset = offsetof(struct UncoloredSpaceTrianglePayload, uv0),
.stride = offsetof(UncoloredSpaceTriangle, v1),
.offset = offsetof(UncoloredSpaceTriangle, uv0),
.buffer = buffer
};
command.textured = true;
command.texture_key = texture_key;
const GLsizei primitives_len = (GLsizei)arrlenu(batch->primitives);
command.primitive_count = 3 * primitives_len;
command.primitive_count = (GLsizei)(3 * primitives_len);
DeferredCommand final_command = {
.type = DEFERRED_COMMAND_TYPE_DRAW,

View File

@ -23,19 +23,19 @@ void draw_triangle(const char *path,
if (!batch_p) {
struct MeshBatch item = {0};
hmput(ctx.uncolored_mesh_batches, texture_key, item);
batch_p = &ctx.uncolored_mesh_batches[hmlenu(ctx.uncolored_mesh_batches) - 1]; /* TODO: can last index be used? */
batch_p = &ctx.uncolored_mesh_batches[hmlenu(ctx.uncolored_mesh_batches) - 1];
}
union UncoloredSpaceTriangle triangle = { .primitive = {
UncoloredSpaceTriangle const triangle = {
.v0 = v0,
.v1 = v1,
.v2 = v2,
.uv1 = uv1,
.uv0 = uv0,
.uv2 = uv2,
}};
};
union UncoloredSpaceTriangle *triangles = (union UncoloredSpaceTriangle *)(void *)batch_p->value.primitives;
UncoloredSpaceTriangle *triangles = (UncoloredSpaceTriangle *)(void *)batch_p->value.primitives;
arrpush(triangles, triangle);
batch_p->value.primitives = (uint8_t *)triangles;
@ -47,34 +47,5 @@ void draw_uncolored_space_traingle_batch(struct MeshBatch *batch,
{
VertexBuffer const vertex_array = get_scratch_vertex_array();
const size_t primitives_len = arrlenu(batch->primitives);
/* nothing to do */
if (primitives_len == 0)
return;
const Rect srcrect = textures_get_srcrect(&ctx.texture_cache, texture_key);
const Rect dims = textures_get_dims(&ctx.texture_cache, texture_key);
const float wr = srcrect.w / dims.w;
const float hr = srcrect.h / dims.h;
const float xr = srcrect.x / dims.w;
const float yr = srcrect.y / dims.h;
/* update pixel-based uvs to correspond with texture atlases */
for (size_t i = 0; i < primitives_len; ++i) {
struct UncoloredSpaceTrianglePayload *payload =
&((union UncoloredSpaceTriangle *)(void *)batch->primitives)[i].payload;
payload->uv0.x = xr + ((float)payload->uv0.x / srcrect.w) * wr;
payload->uv0.y = yr + ((float)payload->uv0.y / srcrect.h) * hr;
payload->uv1.x = xr + ((float)payload->uv1.x / srcrect.w) * wr;
payload->uv1.y = yr + ((float)payload->uv1.y / srcrect.h) * hr;
payload->uv2.x = xr + ((float)payload->uv2.x / srcrect.w) * wr;
payload->uv2.y = yr + ((float)payload->uv2.y / srcrect.h) * hr;
}
specify_vertex_buffer(vertex_array, batch->primitives, primitives_len * sizeof (struct UncoloredSpaceTrianglePayload));
finally_draw_uncolored_space_traingle_batch(batch, texture_key, vertex_array);
}