From fb94881f9f888e1b5bad425bf19ca71bdc8d14b1 Mon Sep 17 00:00:00 2001 From: kichikuou Date: Sat, 12 Jun 2021 22:20:46 +0900 Subject: [PATCH] Remove flip_y field from texture It was introduced to flip texture image rendered by DrawDungeon when presenting on the screen. In GALZOO this approach does not work because dungeon texture is also used as the source image of gfx functions. After this, DrawDungeon directly generates vertically flipped image, by tweaking the projection matrix. --- include/gfx/gfx.h | 1 - src/dungeon/dungeon.c | 17 +++++++++-------- src/video.c | 10 ---------- 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/include/gfx/gfx.h b/include/gfx/gfx.h index 5222c45..e93d2d1 100644 --- a/include/gfx/gfx.h +++ b/include/gfx/gfx.h @@ -38,7 +38,6 @@ typedef struct texture { mat4 world_transform; int w, h; bool has_alpha; - bool flip_y; int alpha_mod; enum draw_method draw_method; } Texture; diff --git a/src/dungeon/dungeon.c b/src/dungeon/dungeon.c index 3cb1003..976947e 100644 --- a/src/dungeon/dungeon.c +++ b/src/dungeon/dungeon.c @@ -50,11 +50,8 @@ struct dungeon_context *dungeon_context_create(int surface) if (!sp) VM_ERROR("DrawDungeon.Init: invalid surface %d", surface); - // Dungeon scene will be rendered to this texture. Unlike other textures, - // its (0,0) is at the bottom-left, so it needs to be flipped vertically - // when displayed on the screen. + // Dungeon scene will be rendered to this texture. struct texture *texture = sprite_get_texture(sp); - texture->flip_y = true; glGenRenderbuffers(1, &ctx->depth_buffer); glBindRenderbuffer(GL_RENDERBUFFER, ctx->depth_buffer); @@ -80,10 +77,6 @@ void dungeon_context_free(struct dungeon_context *ctx) glDeleteRenderbuffers(1, &ctx->depth_buffer); dungeon_map_free(ctx->map); - struct sact_sprite *sp = sact_get_sprite(ctx->surface); - if (sp) - sprite_get_texture(sp)->flip_y = false; - free(ctx); } @@ -226,6 +219,13 @@ void dungeon_render(struct dungeon_context *ctx) model_view_matrix(&ctx->camera, view_transform); glm_perspective(M_PI / 3.0, (float)texture->w / texture->h, 0.5, 100.0, proj_transform); + // Tweak the projection transform so that the rendering result is vertically + // flipped. If we render the scene normally, the resulting image will be + // bottom-up (the first pixel is at the bottom-left), but we want a top-down + // image (the first pixel is at the top-left). + proj_transform[1][1] *= -1; + glFrontFace(GL_CW); + int dgn_x = round(ctx->camera.pos[0] / 2.0); int dgn_y = round(ctx->camera.pos[1] / 2.0); int dgn_z = round(ctx->camera.pos[2] / -2.0); @@ -233,6 +233,7 @@ void dungeon_render(struct dungeon_context *ctx) struct dgn_cell **cells = dgn_get_visible_cells(ctx->dgn, dgn_x, dgn_y, dgn_z, &nr_cells); dungeon_renderer_render(ctx->renderer, cells, nr_cells, view_transform, proj_transform); + glFrontFace(GL_CCW); glDisable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, 0); diff --git a/src/video.c b/src/video.c index e023df3..0cd6644 100644 --- a/src/video.c +++ b/src/video.c @@ -234,7 +234,6 @@ static void main_surface_init(int w, int h) main_surface.w = w; main_surface.h = h; main_surface.has_alpha = true; - main_surface.flip_y = false; main_surface.alpha_mod = 255; main_surface.draw_method = DRAW_METHOD_NORMAL; @@ -371,10 +370,6 @@ void gfx_render_texture(struct texture *t, Rectangle *r) t->world_transform[3][0] = r ? r->x : 0; t->world_transform[3][1] = r ? r->y : 0; - if (t->flip_y) { - t->world_transform[3][1] += t->world_transform[1][1]; - t->world_transform[1][1] *= -1; - } struct gfx_render_job job = { .shader = &default_shader.s, @@ -385,10 +380,6 @@ void gfx_render_texture(struct texture *t, Rectangle *r) }; gfx_render(&job); - if (t->flip_y) { - t->world_transform[1][1] *= -1; - t->world_transform[3][1] -= t->world_transform[1][1]; - } if (t->draw_method != DRAW_METHOD_NORMAL || t->alpha_mod != 255) glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO); } @@ -412,7 +403,6 @@ static void init_texture(struct texture *t, int w, int h) t->h = h; t->has_alpha = true; - t->flip_y = false; t->alpha_mod = 255; t->draw_method = DRAW_METHOD_NORMAL;