rendering.c: correct-er order and settings of of 2d and 3d

This commit is contained in:
veclav talica 2024-07-27 15:33:48 +03:00
parent b5c8f7100a
commit c529e6ee4e
3 changed files with 23 additions and 17 deletions

View File

@ -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) {

View File

@ -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);
}

View File

@ -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,