From c529e6ee4e7c36b2d3f6913af832d1d49e079534 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Sat, 27 Jul 2024 15:33:48 +0300 Subject: [PATCH] rendering.c: correct-er order and settings of of 2d and 3d --- src/main.c | 6 ++++-- src/rendering.c | 31 +++++++++++++++++-------------- src/rendering/sprites.h | 3 ++- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/main.c b/src/main.c index 33aae14..910c040 100644 --- a/src/main.c +++ b/src/main.c @@ -165,11 +165,12 @@ static bool initialize(void) { return false; } - SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5); SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG); + SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); /* init got far enough to create a window */ ctx.window = SDL_CreateWindow("emerald", @@ -192,6 +193,7 @@ static bool initialize(void) { } SDL_GL_MakeCurrent(ctx.window, ctx.gl_context); + SDL_GL_SetSwapInterval(1); int glad_status = gladLoadGL(); if (glad_status == 0) { diff --git a/src/rendering.c b/src/rendering.c index 01d4312..1acecc2 100644 --- a/src/rendering.c +++ b/src/rendering.c @@ -86,16 +86,19 @@ static void render_2d(void) { collect_sprite_batch(current, render_queue_len - i); if (batch.blend) { - glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_ALWAYS); + glDisable(GL_ALPHA_TEST); render_sprites(current, batch.size, false); } else { - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_DEPTH_TEST); - glDepthRange((double)batch_count / 65536, 1.0); glDisable(GL_BLEND); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + glDepthRange((double)batch_count / UINT16_MAX, 1.0); + glDisable(GL_ALPHA_TEST); render_sprites(current, batch.size, true); } @@ -167,6 +170,16 @@ void render(void) { GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + { + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + render_space(); + } + { glMatrixMode(GL_PROJECTION); glLoadIdentity(); @@ -178,15 +191,5 @@ void render(void) { render_2d(); } - { - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - render_space(); - } - SDL_GL_SwapWindow(ctx.window); } diff --git a/src/rendering/sprites.h b/src/rendering/sprites.h index 3340950..0cfef9a 100644 --- a/src/rendering/sprites.h +++ b/src/rendering/sprites.h @@ -18,7 +18,8 @@ * because they will be called multiple times in the main loop * before anything is really rendered */ -/* sprite */ +/* TODO: it might make sense to infer alpha channel presence / meaningfulness for textures in atlas */ +/* so that they are rendered with no blend / batched in a way to reduce overdraw automatically */ void push_sprite(char *path, t_frect rect) { struct sprite_primitive sprite = { .rect = rect,