2024-09-18 17:36:00 +00:00
|
|
|
#include "twn_rendering.h"
|
2024-09-16 06:07:01 +00:00
|
|
|
#include "twn_rendering_c.h"
|
2024-09-16 13:17:00 +00:00
|
|
|
#include "twn_engine_context_c.h"
|
2024-09-16 06:07:01 +00:00
|
|
|
#include "twn_textures_c.h"
|
|
|
|
|
|
|
|
#include <stb_ds.h>
|
|
|
|
|
|
|
|
|
|
|
|
/* TODO: automatic handling of repeating textures */
|
|
|
|
/* for that we could allocate a loner texture */
|
|
|
|
void unfurl_triangle(const char *path,
|
2024-09-23 17:43:16 +00:00
|
|
|
Vec3 v0,
|
|
|
|
Vec3 v1,
|
|
|
|
Vec3 v2,
|
|
|
|
Vec2sh uv0,
|
|
|
|
Vec2sh uv1,
|
|
|
|
Vec2sh uv2)
|
2024-09-16 06:07:01 +00:00
|
|
|
{
|
2024-09-23 17:43:16 +00:00
|
|
|
const TextureKey texture_key = textures_get_key(&ctx.texture_cache, path);
|
2024-09-16 06:07:01 +00:00
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
struct MeshBatchItem *batch_p = hmgetp_null(ctx.uncolored_mesh_batches, texture_key);
|
2024-09-16 06:07:01 +00:00
|
|
|
if (!batch_p) {
|
2024-09-23 17:43:16 +00:00
|
|
|
struct MeshBatch item = {0};
|
2024-09-16 06:07:01 +00:00
|
|
|
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? */
|
|
|
|
}
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
union UncoloredSpaceTriangle triangle = { .primitive = {
|
2024-09-16 06:07:01 +00:00
|
|
|
.v0 = v0,
|
|
|
|
.v1 = v1,
|
|
|
|
.v2 = v2,
|
2024-09-23 17:43:16 +00:00
|
|
|
.uv1 = m_vec2_from(uv1),
|
|
|
|
.uv0 = m_vec2_from(uv0),
|
|
|
|
.uv2 = m_vec2_from(uv2),
|
2024-09-16 06:07:01 +00:00
|
|
|
}};
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
union UncoloredSpaceTriangle *triangles = (union UncoloredSpaceTriangle *)batch_p->value.primitives;
|
2024-09-16 06:07:01 +00:00
|
|
|
|
|
|
|
arrpush(triangles, triangle);
|
|
|
|
batch_p->value.primitives = (uint8_t *)triangles;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
void draw_uncolored_space_traingle_batch(struct MeshBatch *batch,
|
|
|
|
TextureKey texture_key)
|
2024-09-16 06:07:01 +00:00
|
|
|
{
|
2024-09-23 17:43:16 +00:00
|
|
|
static VertexBuffer vertex_array = 0;
|
2024-09-16 06:07:01 +00:00
|
|
|
if (vertex_array == 0)
|
|
|
|
vertex_array = create_vertex_buffer();
|
|
|
|
|
|
|
|
const size_t primitives_len = arrlenu(batch->primitives);
|
|
|
|
|
|
|
|
/* nothing to do */
|
|
|
|
if (primitives_len == 0)
|
|
|
|
return;
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
const Rect srcrect = textures_get_srcrect(&ctx.texture_cache, texture_key);
|
|
|
|
const Rect dims = textures_get_dims(&ctx.texture_cache, texture_key);
|
2024-09-16 06:07:01 +00:00
|
|
|
|
|
|
|
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) {
|
2024-09-23 17:43:16 +00:00
|
|
|
struct UncoloredSpaceTrianglePayload *payload =
|
|
|
|
&((union UncoloredSpaceTriangle *)batch->primitives)[i].payload;
|
2024-09-16 06:07:01 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-09-23 17:43:16 +00:00
|
|
|
specify_vertex_buffer(vertex_array, batch->primitives, primitives_len * sizeof (struct UncoloredSpaceTrianglePayload));
|
2024-09-16 06:07:01 +00:00
|
|
|
|
|
|
|
finally_draw_uncolored_space_traingle_batch(batch, texture_key, vertex_array);
|
|
|
|
}
|