diff --git a/src/libtrx/gfx/3d/3d_renderer.c b/src/libtrx/gfx/3d/3d_renderer.c index c07c08951..1ce6c00ff 100644 --- a/src/libtrx/gfx/3d/3d_renderer.c +++ b/src/libtrx/gfx/3d/3d_renderer.c @@ -28,7 +28,20 @@ struct GFX_3D_RENDERER { GLint loc_color_key_enabled; }; +static void M_Flush(GFX_3D_RENDERER *renderer); static void M_SelectTextureImpl(GFX_3D_RENDERER *renderer, int texture_num); +static void M_RestoreTexture(GFX_3D_RENDERER *renderer); + +static void M_Flush(GFX_3D_RENDERER *const renderer) +{ + glLineWidth(renderer->config->line_width); + glPolygonMode( + GL_FRONT_AND_BACK, + renderer->config->enable_wireframe ? GL_LINE : GL_FILL); + GFX_GL_CheckError(); + + GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream); +} static void M_SelectTextureImpl( GFX_3D_RENDERER *const renderer, const int texture_num) @@ -113,8 +126,6 @@ GFX_3D_RENDERER *GFX_3D_Renderer_Create(void) &model_view[0][0]); GFX_3D_VertexStream_Init(&renderer->vertex_stream); - GFX_GL_CheckError(); - return renderer; } @@ -134,12 +145,6 @@ void GFX_3D_Renderer_RenderBegin(GFX_3D_RENDERER *const renderer) ASSERT(renderer != NULL); glEnable(GL_BLEND); - glLineWidth(renderer->config->line_width); - glPolygonMode( - GL_FRONT_AND_BACK, - renderer->config->enable_wireframe ? GL_LINE : GL_FILL); - GFX_GL_CheckError(); - renderer->vertex_stream.rendered_count = 0; GFX_GL_Program_Bind(&renderer->program); @@ -173,14 +178,13 @@ void GFX_3D_Renderer_RenderBegin(GFX_3D_RENDERER *const renderer) void GFX_3D_Renderer_RenderEnd(GFX_3D_RENDERER *const renderer) { ASSERT(renderer != NULL); - GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream); - - GFX_GL_CheckError(); + M_Flush(renderer); } void GFX_3D_Renderer_ClearDepth(GFX_3D_RENDERER *const renderer) { - GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream); + ASSERT(renderer != NULL); + M_Flush(renderer); glClear(GL_DEPTH_BUFFER_BIT); GFX_GL_CheckError(); } @@ -194,7 +198,6 @@ int GFX_3D_Renderer_RegisterEnvironmentMap(GFX_3D_RENDERER *const renderer) renderer->env_map_texture = texture; M_RestoreTexture(renderer); - GFX_GL_CheckError(); return GFX_ENV_MAP_TEXTURE; } @@ -231,7 +234,7 @@ void GFX_3D_Renderer_FillEnvironmentMap(GFX_3D_RENDERER *const renderer) GFX_GL_TEXTURE *const env_map = renderer->env_map_texture; if (env_map != NULL) { - GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream); + M_Flush(renderer); GFX_GL_Texture_LoadFromBackBuffer(env_map); M_RestoreTexture(renderer); } @@ -257,7 +260,6 @@ int GFX_3D_Renderer_RegisterTexturePage( M_RestoreTexture(renderer); - GFX_GL_CheckError(); return texture_num; } @@ -317,7 +319,7 @@ void GFX_3D_Renderer_SelectTexture( GFX_3D_RENDERER *const renderer, int texture_num) { ASSERT(renderer != NULL); - GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream); + M_Flush(renderer); renderer->selected_texture_num = texture_num; M_SelectTextureImpl(renderer, texture_num); } @@ -326,7 +328,7 @@ void GFX_3D_Renderer_SetPrimType( GFX_3D_RENDERER *const renderer, GFX_3D_PRIM_TYPE value) { ASSERT(renderer != NULL); - GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream); + M_Flush(renderer); GFX_3D_VertexStream_SetPrimType(&renderer->vertex_stream, value); } @@ -334,7 +336,7 @@ void GFX_3D_Renderer_SetTextureFilter( GFX_3D_RENDERER *const renderer, GFX_TEXTURE_FILTER filter) { ASSERT(renderer != NULL); - GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream); + M_Flush(renderer); GFX_GL_Sampler_Parameteri( &renderer->sampler, GL_TEXTURE_MAG_FILTER, filter == GFX_TF_BILINEAR ? GL_LINEAR : GL_NEAREST); @@ -351,28 +353,31 @@ void GFX_3D_Renderer_SetDepthWritesEnabled( GFX_3D_RENDERER *const renderer, const bool is_enabled) { ASSERT(renderer != NULL); - GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream); + M_Flush(renderer); glDepthMask(is_enabled ? GL_TRUE : GL_FALSE); + GFX_GL_CheckError(); } void GFX_3D_Renderer_SetDepthTestEnabled( GFX_3D_RENDERER *const renderer, const bool is_enabled) { ASSERT(renderer != NULL); - GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream); + M_Flush(renderer); if (is_enabled) { glEnable(GL_DEPTH_TEST); } else { glDisable(GL_DEPTH_TEST); } + GFX_GL_CheckError(); } void GFX_3D_Renderer_SetDepthBufferEnabled( GFX_3D_RENDERER *renderer, bool is_enabled) { ASSERT(renderer != NULL); - GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream); + M_Flush(renderer); glDepthFunc(is_enabled ? GL_LEQUAL : GL_ALWAYS); + GFX_GL_CheckError(); } void GFX_3D_Renderer_SetBlendingMode( @@ -382,32 +387,35 @@ void GFX_3D_Renderer_SetBlendingMode( if (renderer->selected_blend_mode == blend_mode) { return; } - GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream); + M_Flush(renderer); GFX_GL_Program_Bind(&renderer->program); switch (blend_mode) { case GFX_BLEND_MODE_OFF: glBlendFunc(GL_ONE, GL_ZERO); + GFX_GL_CheckError(); GFX_GL_Program_Uniform1i( &renderer->program, renderer->loc_color_key_enabled, false); break; case GFX_BLEND_MODE_NORMAL: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + GFX_GL_CheckError(); GFX_GL_Program_Uniform1i( &renderer->program, renderer->loc_color_key_enabled, false); break; case GFX_BLEND_MODE_MULTIPLY: glBlendFunc(GL_DST_COLOR, GL_SRC_COLOR); + GFX_GL_CheckError(); GFX_GL_Program_Uniform1i( &renderer->program, renderer->loc_color_key_enabled, false); break; case GFX_BLEND_MODE_COLOR_KEY: glBlendFunc(GL_ONE, GL_ZERO); + GFX_GL_CheckError(); GFX_GL_Program_Uniform1i( &renderer->program, renderer->loc_color_key_enabled, true); break; } - GFX_GL_CheckError(); renderer->selected_blend_mode = blend_mode; } @@ -415,7 +423,7 @@ void GFX_3D_Renderer_SetTexturingEnabled( GFX_3D_RENDERER *const renderer, const bool is_enabled) { ASSERT(renderer != NULL); - GFX_3D_VertexStream_RenderPending(&renderer->vertex_stream); + M_Flush(renderer); GFX_GL_Program_Bind(&renderer->program); GFX_GL_Program_Uniform1i( &renderer->program, renderer->loc_texturing_enabled, is_enabled);