2024-07-08 00:44:20 +00:00
|
|
|
#include "private/rendering.h"
|
2024-07-27 12:10:19 +00:00
|
|
|
#include "rendering/sprites.h"
|
|
|
|
#include "rendering/triangles.h"
|
|
|
|
#include "rendering/circles.h"
|
2024-07-08 00:44:20 +00:00
|
|
|
#include "context.h"
|
|
|
|
#include "textures.h"
|
|
|
|
|
|
|
|
#include <SDL2/SDL.h>
|
2024-07-09 12:35:54 +00:00
|
|
|
#include <glad/glad.h>
|
2024-07-27 12:10:19 +00:00
|
|
|
#include <stb_ds.h>
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-07-10 16:15:28 +00:00
|
|
|
#include <stddef.h>
|
2024-07-08 00:44:20 +00:00
|
|
|
#include <tgmath.h>
|
|
|
|
|
|
|
|
|
|
|
|
void render_queue_clear(void) {
|
|
|
|
/* since i don't intend to free the queues, */
|
|
|
|
/* it's faster and simpler to just "start over" */
|
|
|
|
/* and start overwriting the existing data */
|
2024-07-16 02:31:54 +00:00
|
|
|
arrsetlen(ctx.render_queue_2d, 0);
|
2024-07-10 16:15:28 +00:00
|
|
|
|
2024-07-14 13:04:12 +00:00
|
|
|
for (size_t i = 0; i < hmlenu(ctx.uncolored_mesh_batches); ++i)
|
|
|
|
arrsetlen(ctx.uncolored_mesh_batches[i].value.primitives, 0);
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* rectangle */
|
|
|
|
void push_rectangle(t_frect rect, t_color color) {
|
|
|
|
struct rect_primitive rectangle = {
|
|
|
|
.rect = rect,
|
|
|
|
.color = color,
|
|
|
|
};
|
|
|
|
|
2024-07-16 02:31:54 +00:00
|
|
|
struct primitive_2d primitive = {
|
|
|
|
.type = PRIMITIVE_2D_RECT,
|
|
|
|
.rect = rectangle,
|
|
|
|
};
|
|
|
|
|
|
|
|
arrput(ctx.render_queue_2d, primitive);
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-12 18:10:25 +00:00
|
|
|
static void upload_quad_vertices(t_frect rect) {
|
|
|
|
/* client memory needs to be reachable on glDraw*, so */
|
|
|
|
static float vertices[6 * 2];
|
|
|
|
|
|
|
|
vertices[0] = rect.x; vertices[1] = rect.y;
|
|
|
|
vertices[2] = rect.x; vertices[3] = rect.y + rect.h;
|
|
|
|
vertices[4] = rect.x + rect.w; vertices[5] = rect.y + rect.h;
|
|
|
|
vertices[6] = rect.x + rect.w; vertices[7] = rect.y + rect.h;
|
|
|
|
vertices[8] = rect.x + rect.w; vertices[9] = rect.y;
|
|
|
|
vertices[10] = rect.x; vertices[11] = rect.y;
|
|
|
|
|
|
|
|
glVertexPointer(2, GL_FLOAT, 0, (void *)&vertices);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-27 12:10:19 +00:00
|
|
|
static void render_rectangle(const struct rect_primitive *rectangle) {
|
2024-07-12 18:10:25 +00:00
|
|
|
glColor4ub(rectangle->color.r, rectangle->color.g,
|
|
|
|
rectangle->color.b, rectangle->color.a);
|
|
|
|
|
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
upload_quad_vertices(rectangle->rect);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-07-12 18:10:25 +00:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
|
|
glDisableClientState(GL_VERTEX_ARRAY);
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-27 12:10:19 +00:00
|
|
|
static void render_2d(void) {
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glDisable(GL_CULL_FACE);
|
|
|
|
glDepthFunc(GL_LESS);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-07-27 12:10:19 +00:00
|
|
|
const size_t render_queue_len = arrlenu(ctx.render_queue_2d);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-07-27 12:10:19 +00:00
|
|
|
size_t batch_count = 0;
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-07-27 12:10:19 +00:00
|
|
|
for (size_t i = 0; i < render_queue_len; ++i) {
|
|
|
|
const struct primitive_2d *current = &ctx.render_queue_2d[i];
|
2024-07-16 02:31:54 +00:00
|
|
|
|
|
|
|
switch (current->type) {
|
2024-07-27 12:10:19 +00:00
|
|
|
case PRIMITIVE_2D_SPRITE: {
|
|
|
|
const struct sprite_batch batch =
|
|
|
|
collect_sprite_batch(current, render_queue_len - i);
|
|
|
|
|
|
|
|
if (batch.blend) {
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2024-07-27 12:33:48 +00:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthFunc(GL_ALWAYS);
|
2024-07-27 13:55:38 +00:00
|
|
|
glDepthRange((double)batch_count / UINT16_MAX, 1.0);
|
2024-07-27 12:33:48 +00:00
|
|
|
glDisable(GL_ALPHA_TEST);
|
2024-07-27 12:10:19 +00:00
|
|
|
|
|
|
|
render_sprites(current, batch.size, false);
|
|
|
|
} else {
|
|
|
|
glDisable(GL_BLEND);
|
2024-07-27 12:33:48 +00:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthFunc(GL_LESS);
|
|
|
|
glDepthRange((double)batch_count / UINT16_MAX, 1.0);
|
|
|
|
glDisable(GL_ALPHA_TEST);
|
2024-07-27 12:10:19 +00:00
|
|
|
|
|
|
|
render_sprites(current, batch.size, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
i += batch.size - 1; ++batch_count;
|
2024-07-16 02:31:54 +00:00
|
|
|
break;
|
2024-07-27 12:10:19 +00:00
|
|
|
}
|
2024-07-16 02:31:54 +00:00
|
|
|
case PRIMITIVE_2D_RECT:
|
|
|
|
render_rectangle(¤t->rect);
|
|
|
|
break;
|
|
|
|
case PRIMITIVE_2D_CIRCLE:
|
|
|
|
render_circle(¤t->circle);
|
|
|
|
break;
|
|
|
|
}
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-09 12:35:54 +00:00
|
|
|
static void render_space(void) {
|
2024-07-27 12:10:19 +00:00
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDisable(GL_ALPHA_TEST);
|
|
|
|
glDepthFunc(GL_LESS);
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2024-07-12 18:10:25 +00:00
|
|
|
glEnable(GL_TEXTURE_2D);
|
2024-07-10 16:15:28 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
|
|
|
|
/* solid white, no modulation */
|
|
|
|
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
|
|
|
|
|
2024-07-14 13:04:12 +00:00
|
|
|
for (size_t i = 0; i < hmlenu(ctx.uncolored_mesh_batches); ++i) {
|
|
|
|
draw_uncolored_space_traingle_batch(&ctx.uncolored_mesh_batches[i].value,
|
|
|
|
ctx.uncolored_mesh_batches[i].key);
|
2024-07-10 16:15:28 +00:00
|
|
|
}
|
2024-07-09 12:35:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-08 00:44:20 +00:00
|
|
|
void render(void) {
|
2024-07-14 13:04:12 +00:00
|
|
|
textures_update_atlas(&ctx.texture_cache);
|
2024-07-10 16:15:28 +00:00
|
|
|
|
2024-07-12 18:10:25 +00:00
|
|
|
/* fit rendering context onto the resizable screen */
|
2024-07-27 14:10:08 +00:00
|
|
|
if (ctx.window_size_has_changed) {
|
|
|
|
if ((float)ctx.window_w / (float)ctx.window_h > RENDER_BASE_RATIO) {
|
|
|
|
float ratio = (float)ctx.window_h / (float)RENDER_BASE_HEIGHT;
|
|
|
|
int w = (int)((float)RENDER_BASE_WIDTH * ratio);
|
|
|
|
glViewport(
|
|
|
|
ctx.window_w / 2 - w / 2,
|
|
|
|
0,
|
|
|
|
w,
|
|
|
|
ctx.window_h
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
float ratio = (float)ctx.window_w / (float)RENDER_BASE_WIDTH;
|
|
|
|
int h = (int)((float)RENDER_BASE_HEIGHT * ratio);
|
|
|
|
glViewport(
|
|
|
|
0,
|
|
|
|
ctx.window_h / 2 - h / 2,
|
|
|
|
ctx.window_w,
|
|
|
|
h
|
|
|
|
);
|
|
|
|
}
|
2024-07-12 18:10:25 +00:00
|
|
|
}
|
|
|
|
|
2024-07-09 12:35:54 +00:00
|
|
|
glClearColor((1.0f / 255) * 230,
|
|
|
|
(1.0f / 255) * 230,
|
|
|
|
(1.0f / 255) * 230, 1);
|
|
|
|
|
2024-07-10 16:15:28 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT |
|
|
|
|
GL_DEPTH_BUFFER_BIT |
|
|
|
|
GL_STENCIL_BUFFER_BIT);
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-07-12 18:10:25 +00:00
|
|
|
{
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
2024-07-10 16:15:28 +00:00
|
|
|
|
2024-07-27 12:33:48 +00:00
|
|
|
render_space();
|
2024-07-12 18:10:25 +00:00
|
|
|
}
|
2024-07-08 00:44:20 +00:00
|
|
|
|
2024-07-12 18:10:25 +00:00
|
|
|
{
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
2024-07-27 12:33:48 +00:00
|
|
|
glOrtho(0, RENDER_BASE_WIDTH, RENDER_BASE_HEIGHT, 0, -1, 1);
|
2024-07-10 16:15:28 +00:00
|
|
|
|
2024-07-12 18:10:25 +00:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
2024-07-16 02:31:54 +00:00
|
|
|
|
2024-07-27 12:33:48 +00:00
|
|
|
render_2d();
|
2024-07-12 18:10:25 +00:00
|
|
|
}
|
2024-07-10 16:15:28 +00:00
|
|
|
|
2024-07-09 12:35:54 +00:00
|
|
|
SDL_GL_SwapWindow(ctx.window);
|
2024-07-08 00:44:20 +00:00
|
|
|
}
|