billboards!

This commit is contained in:
veclavtalica
2025-01-05 19:46:05 +03:00
parent 7c0bf39f12
commit 3bfa86066e
17 changed files with 626 additions and 413 deletions

View File

@ -17,6 +17,7 @@ void draw_triangle(const char *path,
Vec2 uv1,
Vec2 uv2)
{
// TODO: order drawing by atlas id as well, so that texture rebinding is not as common
const TextureKey texture_key = textures_get_key(&ctx.texture_cache, path);
struct MeshBatchItem *batch_p = hmgetp_null(ctx.uncolored_mesh_batches, texture_key);
@ -42,10 +43,67 @@ void draw_triangle(const char *path,
}
void draw_uncolored_space_traingle_batch(struct MeshBatch *batch,
TextureKey texture_key)
void finally_draw_uncolored_space_traingle_batch(const MeshBatch *batch,
const TextureKey texture_key)
{
VertexBuffer const vertex_array = get_scratch_vertex_array();
const size_t primitives_len = arrlenu(batch->primitives);
finally_draw_uncolored_space_traingle_batch(batch, texture_key, vertex_array);
/* nothing to do */
if (primitives_len == 0)
return;
VertexBuffer const buffer = get_scratch_vertex_array();
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(UncoloredSpaceTriangle, v1),
.offset = offsetof(UncoloredSpaceTriangle, v0),
.buffer = buffer
};
command.texcoords = (AttributeArrayPointer) {
.arity = 2,
.type = GL_FLOAT,
.stride = offsetof(UncoloredSpaceTriangle, v1),
.offset = offsetof(UncoloredSpaceTriangle, uv0),
.buffer = buffer
};
command.textured = true;
command.texture_key = texture_key;
command.primitive_count = (GLsizei)(3 * primitives_len);
DeferredCommand final_command = {
.type = DEFERRED_COMMAND_TYPE_DRAW,
.draw = command
};
arrpush(deferred_commands, final_command);
}