2024-07-27 12:10:19 +00:00
|
|
|
/* a rendering.c mixin */
|
|
|
|
#ifndef SPRITES_H
|
|
|
|
#define SPRITES_H
|
|
|
|
|
|
|
|
#include "../textures.h"
|
|
|
|
#include "../rendering.h"
|
|
|
|
#include "../context.h"
|
2024-07-28 11:39:23 +00:00
|
|
|
#include "../util.h"
|
2024-07-27 12:10:19 +00:00
|
|
|
#include "quad_element_buffer.h"
|
|
|
|
|
|
|
|
#include <stb_ds.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2024-07-28 13:06:47 +00:00
|
|
|
/* interleaved vertex array data */
|
|
|
|
/* TODO: use int16_t for uvs */
|
|
|
|
/* TODO: int16_t could be used for positioning, but we would need to have more CPU calcs */
|
|
|
|
struct sprite_primitive_payload {
|
|
|
|
/* upper-left */
|
|
|
|
t_fvec2 v0;
|
|
|
|
t_fvec2 uv0;
|
|
|
|
t_color c0;
|
|
|
|
/* bottom-left */
|
|
|
|
t_fvec2 v1;
|
|
|
|
t_fvec2 uv1;
|
|
|
|
t_color c1;
|
|
|
|
/* bottom-right */
|
|
|
|
t_fvec2 v2;
|
|
|
|
t_fvec2 uv2;
|
|
|
|
t_color c2;
|
|
|
|
/* upper-right */
|
|
|
|
t_fvec2 v3;
|
|
|
|
t_fvec2 uv3;
|
|
|
|
t_color c3;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct sprite_primitive_payload_without_color {
|
|
|
|
/* upper-left */
|
|
|
|
t_fvec2 v0;
|
|
|
|
t_fvec2 uv0;
|
|
|
|
/* bottom-left */
|
|
|
|
t_fvec2 v1;
|
|
|
|
t_fvec2 uv1;
|
|
|
|
/* bottom-right */
|
|
|
|
t_fvec2 v2;
|
|
|
|
t_fvec2 uv2;
|
|
|
|
/* upper-right */
|
|
|
|
t_fvec2 v3;
|
|
|
|
t_fvec2 uv3;
|
|
|
|
};
|
|
|
|
|
2024-07-27 12:10:19 +00:00
|
|
|
/*
|
|
|
|
* an implementation note:
|
|
|
|
* try to avoid doing expensive work in the push functions,
|
|
|
|
* because they will be called multiple times in the main loop
|
|
|
|
* before anything is really rendered
|
|
|
|
*/
|
2024-07-27 12:33:48 +00:00
|
|
|
/* TODO: it might make sense to infer alpha channel presence / meaningfulness for textures in atlas */
|
2024-07-28 11:39:23 +00:00
|
|
|
/* so that they are rendered with no blend / batched in a way to reduce overdraw automatically */
|
2024-07-27 12:10:19 +00:00
|
|
|
void push_sprite(char *path, t_frect rect) {
|
|
|
|
struct sprite_primitive sprite = {
|
|
|
|
.rect = rect,
|
|
|
|
.color = (t_color) { 255, 255, 255, 255 },
|
|
|
|
.rotation = 0.0,
|
|
|
|
.texture_key = textures_get_key(&ctx.texture_cache, path),
|
|
|
|
.flip_x = false,
|
|
|
|
.flip_y = false,
|
|
|
|
.blend = true,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct primitive_2d primitive = {
|
|
|
|
.type = PRIMITIVE_2D_SPRITE,
|
|
|
|
.sprite = sprite,
|
|
|
|
};
|
|
|
|
|
|
|
|
arrput(ctx.render_queue_2d, primitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void push_sprite_ex(t_frect rect, t_push_sprite_args args) {
|
|
|
|
struct sprite_primitive sprite = {
|
|
|
|
.rect = rect,
|
|
|
|
.color = args.color,
|
|
|
|
.rotation = args.rotation,
|
|
|
|
.texture_key = textures_get_key(&ctx.texture_cache, args.path),
|
|
|
|
.flip_x = args.flip_x,
|
|
|
|
.flip_y = args.flip_y,
|
|
|
|
.blend = args.blend,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct primitive_2d primitive = {
|
|
|
|
.type = PRIMITIVE_2D_SPRITE,
|
|
|
|
.sprite = sprite,
|
|
|
|
};
|
|
|
|
|
|
|
|
arrput(ctx.render_queue_2d, primitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct sprite_batch {
|
|
|
|
int atlas_id;
|
2024-07-28 13:25:25 +00:00
|
|
|
size_t size; /* how many primitives are in current batch */
|
|
|
|
bool blend; /* whether it's blended or not */
|
|
|
|
bool constant_colored; /* whether colored batch is uniformly colored */
|
2024-07-27 12:10:19 +00:00
|
|
|
} collect_sprite_batch(const struct primitive_2d *primitives, size_t len) {
|
|
|
|
/* assumes that first primitive is already a sprite */
|
2024-07-28 13:25:25 +00:00
|
|
|
struct sprite_batch batch = {
|
2024-07-27 12:10:19 +00:00
|
|
|
.atlas_id =
|
|
|
|
textures_get_atlas_id(&ctx.texture_cache, primitives[0].sprite.texture_key),
|
|
|
|
.blend = primitives[0].sprite.blend,
|
2024-07-28 13:25:25 +00:00
|
|
|
.constant_colored = true,
|
2024-07-27 12:10:19 +00:00
|
|
|
};
|
|
|
|
|
2024-07-28 13:25:25 +00:00
|
|
|
const t_color uniform_color = primitives[0].sprite.color;
|
|
|
|
|
2024-07-27 12:10:19 +00:00
|
|
|
/* batch size is clamped so that reallocated short indices could be used */
|
|
|
|
if (len >= QUAD_ELEMENT_BUFFER_LENGTH)
|
|
|
|
len = QUAD_ELEMENT_BUFFER_LENGTH;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
const struct primitive_2d *const current = &primitives[i];
|
|
|
|
|
|
|
|
/* don't touch things other than sprites */
|
|
|
|
if (current->type != PRIMITIVE_2D_SPRITE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* only collect the same blend modes */
|
2024-07-28 13:25:25 +00:00
|
|
|
if (current->sprite.blend != batch.blend)
|
2024-07-27 12:10:19 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* only collect the same texture atlases */
|
|
|
|
if (textures_get_atlas_id(&ctx.texture_cache, current->sprite.texture_key)
|
2024-07-28 13:25:25 +00:00
|
|
|
!= batch.atlas_id)
|
2024-07-27 12:10:19 +00:00
|
|
|
break;
|
|
|
|
|
2024-07-28 13:25:25 +00:00
|
|
|
/* if all are modulated the same we can skip sending the color data */
|
|
|
|
if (batch.constant_colored && (current->sprite.color.r != uniform_color.r ||
|
|
|
|
current->sprite.color.g != uniform_color.g ||
|
|
|
|
current->sprite.color.b != uniform_color.b ||
|
|
|
|
current->sprite.color.a != uniform_color.a ))
|
|
|
|
batch.constant_colored = false;
|
2024-07-28 13:06:47 +00:00
|
|
|
|
2024-07-28 13:25:25 +00:00
|
|
|
++batch.size;
|
2024-07-27 12:10:19 +00:00
|
|
|
}
|
|
|
|
|
2024-07-28 13:25:25 +00:00
|
|
|
return batch;
|
2024-07-27 12:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* assumes that orthogonal matrix setup is done already */
|
|
|
|
static void render_sprites(const struct primitive_2d primitives[],
|
2024-07-28 13:06:47 +00:00
|
|
|
const struct sprite_batch batch)
|
2024-07-27 12:10:19 +00:00
|
|
|
{
|
2024-07-28 11:39:23 +00:00
|
|
|
/* single vertex array is used for every batch with NULL glBufferData() trick at the end */
|
2024-07-27 12:10:19 +00:00
|
|
|
static GLuint vertex_array = 0;
|
|
|
|
if (vertex_array == 0)
|
|
|
|
glGenBuffers(1, &vertex_array);
|
|
|
|
|
2024-07-28 13:06:47 +00:00
|
|
|
if (batch.blend) {
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
glDepthFunc(GL_ALWAYS);
|
|
|
|
} else {
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
glDepthFunc(GL_LESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t payload_size;
|
2024-07-28 13:25:25 +00:00
|
|
|
if (!batch.constant_colored)
|
2024-07-28 13:06:47 +00:00
|
|
|
payload_size = sizeof (struct sprite_primitive_payload);
|
|
|
|
else
|
|
|
|
payload_size = sizeof (struct sprite_primitive_payload_without_color);
|
|
|
|
|
2024-07-27 12:10:19 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vertex_array);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER,
|
2024-07-28 13:06:47 +00:00
|
|
|
payload_size * batch.size,
|
2024-07-27 12:10:19 +00:00
|
|
|
NULL,
|
|
|
|
GL_STREAM_DRAW);
|
|
|
|
|
|
|
|
const t_rect dims =
|
|
|
|
textures_get_dims(&ctx.texture_cache, primitives->sprite.texture_key);
|
|
|
|
|
|
|
|
/* vertex population over a mapped buffer */
|
|
|
|
{
|
|
|
|
/* TODO: check errors, ensure alignment ? */
|
2024-07-28 13:06:47 +00:00
|
|
|
void *const payload = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
2024-07-27 12:10:19 +00:00
|
|
|
|
2024-07-28 13:06:47 +00:00
|
|
|
for (size_t i = 0; i < batch.size; ++i) {
|
|
|
|
const size_t cur = !batch.blend ? batch.size - i - 1: i; /* render opaques front to back */
|
2024-07-27 12:10:19 +00:00
|
|
|
const struct sprite_primitive sprite = primitives[cur].sprite;
|
|
|
|
|
2024-07-28 11:39:23 +00:00
|
|
|
const t_rect srcrect =
|
|
|
|
textures_get_srcrect(&ctx.texture_cache, primitives[cur].sprite.texture_key);
|
|
|
|
|
|
|
|
const float wr = (float)srcrect.w / (float)dims.w;
|
|
|
|
const float hr = (float)srcrect.h / (float)dims.h;
|
|
|
|
const float xr = (float)srcrect.x / (float)dims.w;
|
|
|
|
const float yr = (float)srcrect.y / (float)dims.h;
|
|
|
|
|
2024-07-28 13:06:47 +00:00
|
|
|
t_fvec2 uv0 = { xr + wr * sprite.flip_x, yr + hr * sprite.flip_y };
|
|
|
|
t_fvec2 uv1 = { xr + wr * sprite.flip_x, yr + hr * !sprite.flip_y };
|
|
|
|
t_fvec2 uv2 = { xr + wr * !sprite.flip_x, yr + hr * !sprite.flip_y };
|
|
|
|
t_fvec2 uv3 = { xr + wr * !sprite.flip_x, yr + hr * sprite.flip_y };
|
|
|
|
|
|
|
|
t_fvec2 v0, v1, v2, v3;
|
|
|
|
|
|
|
|
/* todo: fast PI/2 degree divisible rotations? */
|
2024-07-28 11:39:23 +00:00
|
|
|
if (sprite.rotation == 0.0f) {
|
2024-07-28 13:06:47 +00:00
|
|
|
/* non-rotated case */
|
|
|
|
|
|
|
|
v0 = (t_fvec2){ sprite.rect.x, sprite.rect.y };
|
|
|
|
v1 = (t_fvec2){ sprite.rect.x, sprite.rect.y + sprite.rect.h };
|
|
|
|
v2 = (t_fvec2){ sprite.rect.x + sprite.rect.w, sprite.rect.y + sprite.rect.h };
|
|
|
|
v3 = (t_fvec2){ sprite.rect.x + sprite.rect.w, sprite.rect.y };
|
|
|
|
|
|
|
|
} else if (sprite.rect.w == sprite.rect.h) {
|
|
|
|
/* rotated square case */
|
2024-07-28 11:39:23 +00:00
|
|
|
|
|
|
|
const t_fvec2 c = frect_center(sprite.rect);
|
|
|
|
const t_fvec2 t = fast_cossine(sprite.rotation + (float)M_PI_4);
|
|
|
|
const t_fvec2 d = {
|
|
|
|
.x = t.x * sprite.rect.w * (float)M_SQRT1_2,
|
|
|
|
.y = t.y * sprite.rect.h * (float)M_SQRT1_2,
|
|
|
|
};
|
|
|
|
|
2024-07-28 13:06:47 +00:00
|
|
|
v0 = (t_fvec2){ c.x - d.x, c.y - d.y };
|
|
|
|
v1 = (t_fvec2){ c.x - d.y, c.y + d.x };
|
|
|
|
v2 = (t_fvec2){ c.x + d.x, c.y + d.y };
|
|
|
|
v3 = (t_fvec2){ c.x + d.y, c.y - d.x };
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* rotated non-square case*/
|
|
|
|
|
|
|
|
CRY("Rotation", "Unimplemented");
|
|
|
|
}
|
|
|
|
|
2024-07-28 13:25:25 +00:00
|
|
|
if (!batch.constant_colored)
|
2024-07-28 13:06:47 +00:00
|
|
|
((struct sprite_primitive_payload *)payload)[i] = (struct sprite_primitive_payload) {
|
|
|
|
.v0 = v0,
|
|
|
|
.v1 = v1,
|
|
|
|
.v2 = v2,
|
|
|
|
.v3 = v3,
|
|
|
|
|
|
|
|
.uv0 = uv0,
|
|
|
|
.uv1 = uv1,
|
|
|
|
.uv2 = uv2,
|
|
|
|
.uv3 = uv3,
|
2024-07-28 11:39:23 +00:00
|
|
|
|
|
|
|
/* equal for all (flat shaded) */
|
|
|
|
.c0 = sprite.color,
|
|
|
|
.c1 = sprite.color,
|
|
|
|
.c2 = sprite.color,
|
|
|
|
.c3 = sprite.color,
|
|
|
|
};
|
2024-07-28 13:06:47 +00:00
|
|
|
else
|
|
|
|
((struct sprite_primitive_payload_without_color *)payload)[i] = (struct sprite_primitive_payload_without_color) {
|
|
|
|
.v0 = v0,
|
|
|
|
.v1 = v1,
|
|
|
|
.v2 = v2,
|
|
|
|
.v3 = v3,
|
|
|
|
|
|
|
|
.uv0 = uv0,
|
|
|
|
.uv1 = uv1,
|
|
|
|
.uv2 = uv2,
|
|
|
|
.uv3 = uv3,
|
|
|
|
};
|
2024-07-27 12:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
glUnmapBuffer(GL_ARRAY_BUFFER);
|
|
|
|
}
|
|
|
|
|
2024-07-28 13:06:47 +00:00
|
|
|
GLsizei off;
|
|
|
|
GLsizei voff;
|
|
|
|
GLsizei uvoff;
|
|
|
|
|
2024-07-28 13:25:25 +00:00
|
|
|
if (!batch.constant_colored) {
|
2024-07-28 13:06:47 +00:00
|
|
|
off = offsetof(struct sprite_primitive_payload, v1);
|
|
|
|
voff = offsetof(struct sprite_primitive_payload, v0);
|
|
|
|
uvoff = offsetof(struct sprite_primitive_payload, uv0);
|
|
|
|
} else {
|
|
|
|
off = offsetof(struct sprite_primitive_payload_without_color, v1);
|
|
|
|
voff = offsetof(struct sprite_primitive_payload_without_color, v0);
|
|
|
|
uvoff = offsetof(struct sprite_primitive_payload_without_color, uv0);
|
|
|
|
}
|
|
|
|
|
2024-07-27 12:10:19 +00:00
|
|
|
/* vertex specification */
|
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glVertexPointer(2,
|
|
|
|
GL_FLOAT,
|
2024-07-28 13:06:47 +00:00
|
|
|
off,
|
|
|
|
(void *)(size_t)voff);
|
2024-07-27 12:10:19 +00:00
|
|
|
|
|
|
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
glClientActiveTexture(GL_TEXTURE0);
|
|
|
|
glTexCoordPointer(2,
|
|
|
|
GL_FLOAT,
|
2024-07-28 13:06:47 +00:00
|
|
|
off,
|
|
|
|
(void *)(size_t)uvoff);
|
|
|
|
|
2024-07-28 13:25:25 +00:00
|
|
|
if (!batch.constant_colored) {
|
2024-07-28 13:06:47 +00:00
|
|
|
glEnableClientState(GL_COLOR_ARRAY);
|
|
|
|
glColorPointer(4,
|
|
|
|
GL_UNSIGNED_BYTE,
|
|
|
|
off,
|
|
|
|
(void *)offsetof(struct sprite_primitive_payload, c0));
|
|
|
|
} else
|
2024-07-28 13:25:25 +00:00
|
|
|
glColor4ub(primitives[0].sprite.color.r,
|
|
|
|
primitives[0].sprite.color.g,
|
|
|
|
primitives[0].sprite.color.b,
|
|
|
|
primitives[0].sprite.color.a);
|
2024-07-27 12:10:19 +00:00
|
|
|
|
|
|
|
textures_bind(&ctx.texture_cache, primitives->sprite.texture_key, GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
bind_quad_element_buffer();
|
|
|
|
|
2024-07-28 13:06:47 +00:00
|
|
|
glDrawElements(GL_TRIANGLES, 6 * (GLsizei)batch.size, GL_UNSIGNED_SHORT, NULL);
|
2024-07-27 12:10:19 +00:00
|
|
|
|
|
|
|
/* clear the state */
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, 0, NULL, GL_STREAM_DRAW);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glDisableClientState(GL_COLOR_ARRAY);
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|