From e3abcffeb01fed31ce37ff0f014fedd347ff1cc0 Mon Sep 17 00:00:00 2001 From: Zack Middleton Date: Sat, 10 Feb 2024 14:38:18 -0600 Subject: [PATCH] OpenGL2: Fix using merged lightmaps with tcGen environment tcGen environment generates texcoords in range of 0.0 to 1.0 and they need to be offset to the position/size in the merged lightmap atlas. This also needs to be after tcMods so they apply for the original range. This fixes tcMod scale used by main_q3abanner and shinygrate1_4. This issue was visible on the blue monitor (comp3c) near the plasmagun in q3dm0 and the Quake III Arena banner in q3dm17. Affected shaders in Quake 3 and Team Arena maps: textures/base_wall/comp3 textures/base_wall/comp3b textures/base_wall/comp3b_dark textures/base_wall/comp3c textures/base_wall/main_q3abanner textures/base_wall/shinygrate1_4 textures/sfx/teslacoil All of the shaders are used by q3dm0 but other maps also use some. --- code/renderergl2/tr_shader.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/code/renderergl2/tr_shader.c b/code/renderergl2/tr_shader.c index cfd87f05..ba075510 100644 --- a/code/renderergl2/tr_shader.c +++ b/code/renderergl2/tr_shader.c @@ -2944,8 +2944,8 @@ static void FixFatLightmapTexCoords(void) break; } - // fix tcMod transform for internal lightmaps, it may be used by q3map2 lightstyles if ( pStage->bundle[0].isLightmap ) { + // fix tcMod transform for internal lightmaps, it may be used by q3map2 lightstyles for ( i = 0; i < pStage->bundle[0].numTexMods; i++ ) { tmi = &pStage->bundle[0].texMods[i]; @@ -2954,11 +2954,32 @@ static void FixFatLightmapTexCoords(void) tmi->translate[1] /= (float)tr.fatLightmapRows; } } + + // fix tcGen environment for internal lightmaps to be limited to the sub-image of the atlas + // this is done last so other tcMods are applied first in the 0.0 to 1.0 space + if ( pStage->bundle[0].tcGen == TCGEN_ENVIRONMENT_MAPPED ) { + if ( pStage->bundle[0].numTexMods == TR_MAX_TEXMODS ) { + ri.Printf( PRINT_DEVELOPER, "WARNING: too many tcmods to fix lightmap texcoords for r_mergeLightmaps in shader '%s'", shader.name ); + } else { + tmi = &pStage->bundle[0].texMods[pStage->bundle[0].numTexMods]; + pStage->bundle[0].numTexMods++; + + tmi->matrix[0][0] = 1.0f / tr.fatLightmapCols; + tmi->matrix[0][1] = 0; + tmi->matrix[1][0] = 0; + tmi->matrix[1][1] = 1.0f / tr.fatLightmapRows; + + tmi->translate[0] = ( lightmapnum % tr.fatLightmapCols ) / (float)tr.fatLightmapCols; + tmi->translate[1] = ( lightmapnum / tr.fatLightmapCols ) / (float)tr.fatLightmapRows; + + tmi->type = TMOD_TRANSFORM; + } + } } // add a tcMod transform for external lightmaps to convert back to the original texcoords else if ( pStage->bundle[0].tcGen == TCGEN_LIGHTMAP ) { if ( pStage->bundle[0].numTexMods == TR_MAX_TEXMODS ) { - ri.Printf( PRINT_DEVELOPER, "WARNING: too many tcmods to fix external lightmap texcoords for r_mergeLightmaps in shader '%s'", shader.name ); + ri.Printf( PRINT_DEVELOPER, "WARNING: too many tcmods to fix lightmap texcoords for r_mergeLightmaps in shader '%s'", shader.name ); } else { size = pStage->bundle[0].numTexMods * sizeof( texModInfo_t ); @@ -2974,8 +2995,8 @@ static void FixFatLightmapTexCoords(void) tmi->matrix[1][0] = 0; tmi->matrix[1][1] = tr.fatLightmapRows; - tmi->translate[0] = -(lightmapnum % tr.fatLightmapCols); - tmi->translate[1] = -(lightmapnum / tr.fatLightmapCols); + tmi->translate[0] = -( lightmapnum % tr.fatLightmapCols ); + tmi->translate[1] = -( lightmapnum / tr.fatLightmapCols ); tmi->type = TMOD_TRANSFORM; }