Skip to content

Commit

Permalink
Renderers: apply additional lightmap texcoord corrections when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ec- committed Feb 5, 2024
1 parent bb6d29f commit 86ce720
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ jobs:
- name: Build
if: ${{ github.event_name != 'release' || matrix.btype != 'Debug' }} # skip Debug configuration in Release build
run: |
make ${{ matrix.rule }} -j 4 CC=${{ matrix.cc }} DESTDIR=bin INSTALL=ginstall USE_RENDERER_DLOPEN=0 RENDERER_DEFAULT=vulkan CNAME=quake3e-vulkan BUILD_SERVER=0 STRIP=echo
make ${{ matrix.rule }} -j 4 CC=${{ matrix.cc }} DESTDIR=bin INSTALL=ginstall USE_RENDERER_DLOPEN=0 RENDERER_DEFAULT=vulkan CNAME=quake3e-vulkan BUILD_SERVER=0
make clean ARCH=${{ matrix.arch }}
make ${{ matrix.rule }} -j 4 CC=${{ matrix.cc }} DESTDIR=bin INSTALL=ginstall USE_RENDERER_DLOPEN=0 RENDERER_DEFAULT=opengl STRIP=echo
make ${{ matrix.rule }} -j 4 CC=${{ matrix.cc }} DESTDIR=bin INSTALL=ginstall USE_RENDERER_DLOPEN=0 RENDERER_DEFAULT=opengl
- uses: actions/upload-artifact@v4
if: matrix.cc == 'clang' && matrix.btype == 'release'
Expand Down
2 changes: 1 addition & 1 deletion code/renderer/tr_bsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ static void ParseTriSurf( const dsurface_t *ds, const drawVert_t *verts, msurfac
if ( lightmapNum >= 0 && tr.mergeLightmaps ) {
lightmapNum = R_GetLightmapCoords( lightmapNum, &lightmapX, &lightmapY );
} else {
lightmapX = lightmapY = 0;
lightmapX = lightmapY = 0.0f;
}

tr.lightmapOffset[0] = lightmapX;
Expand Down
1 change: 1 addition & 0 deletions code/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ typedef enum {
TMOD_ENTITY_TRANSLATE,
TMOD_OFFSET,
TMOD_SCALE_OFFSET,
TMOD_OFFSET_SCALE,
} texMod_t;

#define MAX_SHADER_DEFORMS 3
Expand Down
8 changes: 8 additions & 0 deletions code/renderer/tr_shade.c
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,14 @@ void R_ComputeTexCoords( const int b, const textureBundle_t *bundle ) {
src = dst;
break;

case TMOD_OFFSET_SCALE:
for ( i = 0; i < tess.numVertexes; i++ ) {
dst[i][0] = (src[i][0] + bundle->texMods[tm].offset[0]) * bundle->texMods[tm].scale[0];
dst[i][1] = (src[i][1] + bundle->texMods[tm].offset[1]) * bundle->texMods[tm].scale[1];
}
src = dst;
break;

case TMOD_STRETCH:
RB_CalcStretchTexCoords( &bundle->texMods[tm].wave, (float *)src, (float *) dst );
src = dst;
Expand Down
70 changes: 50 additions & 20 deletions code/renderer/tr_shader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,7 @@ FinishStage
*/
static void FinishStage( shaderStage_t *stage )
{
int i;
int i, n;

if ( !tr.mergeLightmaps ) {
return;
Expand All @@ -1705,28 +1705,58 @@ static void FinishStage( shaderStage_t *stage )
for ( i = 0; i < ARRAY_LEN( stage->bundle ); i++ ) {
textureBundle_t *bundle = &stage->bundle[i];
// offset lightmap coordinates
if ( bundle->lightmap >= LIGHTMAP_INDEX_OFFSET && bundle->tcGen == TCGEN_LIGHTMAP ) {
texModInfo_t *tmi = &bundle->texMods[bundle->numTexMods];
float x, y;
const int lightmapIndex = R_GetLightmapCoords( bundle->lightmap - LIGHTMAP_INDEX_OFFSET, &x, &y );
bundle->image[0] = tr.lightmaps[lightmapIndex];
tmi->type = TMOD_OFFSET;
tmi->offset[0] = x - tr.lightmapOffset[0];
tmi->offset[1] = y - tr.lightmapOffset[1];
bundle->numTexMods++;
if ( bundle->lightmap >= LIGHTMAP_INDEX_OFFSET ) {
if ( bundle->tcGen == TCGEN_LIGHTMAP ) {
texModInfo_t * tmi = &bundle->texMods[bundle->numTexMods];
float x, y;
const int lightmapIndex = R_GetLightmapCoords( bundle->lightmap - LIGHTMAP_INDEX_OFFSET, &x, &y );
bundle->image[0] = tr.lightmaps[lightmapIndex];
tmi->type = TMOD_OFFSET;
tmi->offset[0] = x - tr.lightmapOffset[0];
tmi->offset[1] = y - tr.lightmapOffset[1];
bundle->numTexMods++;
}
continue;
}

// adjust texture coordinates to map on proper lightmap
if ( bundle->lightmap == LIGHTMAP_INDEX_SHADER && bundle->tcGen != TCGEN_LIGHTMAP ) {
texModInfo_t *tmi = &bundle->texMods[bundle->numTexMods];
tmi->type = TMOD_SCALE_OFFSET;
tmi->scale[0] = tr.lightmapScale[0];
tmi->scale[1] = tr.lightmapScale[1];
tmi->offset[0] = tr.lightmapOffset[0];
tmi->offset[1] = tr.lightmapOffset[1];
bundle->numTexMods++;
continue;
// adjust texture coordinates to map on proper lightmap
if ( bundle->lightmap == LIGHTMAP_INDEX_SHADER ) {
if ( bundle->tcGen != TCGEN_LIGHTMAP ) {
texModInfo_t *tmi = &bundle->texMods[bundle->numTexMods];
tmi->type = TMOD_SCALE_OFFSET;
tmi->scale[0] = tr.lightmapScale[0];
tmi->scale[1] = tr.lightmapScale[1];
tmi->offset[0] = tr.lightmapOffset[0];
tmi->offset[1] = tr.lightmapOffset[1];
bundle->numTexMods++;
} else {
for ( n = 0; n < bundle->numTexMods; n++ ) {
texModInfo_t *tmi = &bundle->texMods[n];
if ( tmi->type == TMOD_TRANSFORM ) {
tmi->translate[0] *= tr.lightmapScale[0];
tmi->translate[1] *= tr.lightmapScale[1];
} else {
// TODO: correct other transformations?
}
}
}
continue;
}
// revert lightmap texcoord correction if needed
if ( bundle->lightmap == LIGHTMAP_INDEX_NONE ) {
if ( bundle->tcGen == TCGEN_LIGHTMAP ) {
texModInfo_t *tmi;
for ( n = bundle->numTexMods; n > 0; --n ) {
bundle->texMods[n] = bundle->texMods[n - 1];
}
tmi = &bundle->texMods[0];
tmi->type = TMOD_OFFSET_SCALE;
tmi->offset[0] = -tr.lightmapOffset[0];
tmi->offset[1] = -tr.lightmapOffset[1];
tmi->scale[0] = 1.0f / tr.lightmapScale[0];
tmi->scale[1] = 1.0f / tr.lightmapScale[1];
bundle->numTexMods++;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions code/renderer/tr_vbo.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,7 @@ static qboolean isStaticTCmod( const textureBundle_t *bundle )
case TMOD_TRANSFORM:
case TMOD_OFFSET:
case TMOD_SCALE_OFFSET:
case TMOD_OFFSET_SCALE:
break;
default:
return qfalse;
Expand Down
2 changes: 1 addition & 1 deletion code/renderervk/tr_bsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ static void ParseTriSurf( const dsurface_t *ds, const drawVert_t *verts, msurfac
if ( lightmapNum >= 0 && tr.mergeLightmaps ) {
lightmapNum = R_GetLightmapCoords( lightmapNum, &lightmapX, &lightmapY );
} else {
lightmapX = lightmapY = 0;
lightmapX = lightmapY = 0.0f;
}

tr.lightmapOffset[0] = lightmapX;
Expand Down
1 change: 1 addition & 0 deletions code/renderervk/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ typedef enum {
TMOD_ENTITY_TRANSLATE,
TMOD_OFFSET,
TMOD_SCALE_OFFSET,
TMOD_OFFSET_SCALE,
} texMod_t;

#define MAX_SHADER_DEFORMS 3
Expand Down
8 changes: 8 additions & 0 deletions code/renderervk/tr_shade.c
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,14 @@ void R_ComputeTexCoords( const int b, const textureBundle_t *bundle ) {
src = dst;
break;

case TMOD_OFFSET_SCALE:
for ( i = 0; i < tess.numVertexes; i++ ) {
dst[i][0] = (src[i][0] + bundle->texMods[tm].offset[0]) * bundle->texMods[tm].scale[0];
dst[i][1] = (src[i][1] + bundle->texMods[tm].offset[1]) * bundle->texMods[tm].scale[1];
}
src = dst;
break;

case TMOD_STRETCH:
RB_CalcStretchTexCoords( &bundle->texMods[tm].wave, (float *)src, (float *) dst );
src = dst;
Expand Down
67 changes: 48 additions & 19 deletions code/renderervk/tr_shader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1693,7 +1693,7 @@ FinishStage
*/
static void FinishStage( shaderStage_t *stage )
{
int i;
int i, n;

if ( !tr.mergeLightmaps ) {
return;
Expand All @@ -1702,29 +1702,58 @@ static void FinishStage( shaderStage_t *stage )
for ( i = 0; i < ARRAY_LEN( stage->bundle ); i++ ) {
textureBundle_t *bundle = &stage->bundle[i];
// offset lightmap coordinates
if ( bundle->lightmap >= LIGHTMAP_INDEX_OFFSET && bundle->tcGen == TCGEN_LIGHTMAP ) {
texModInfo_t *tmi = &bundle->texMods[bundle->numTexMods];
float x, y;
const int lightmapIndex = R_GetLightmapCoords( bundle->lightmap - LIGHTMAP_INDEX_OFFSET, &x, &y );
bundle->image[0] = tr.lightmaps[lightmapIndex];
tmi->type = TMOD_OFFSET;
tmi->offset[0] = x - tr.lightmapOffset[0];
tmi->offset[1] = y - tr.lightmapOffset[1];
bundle->numTexMods++;
if ( bundle->lightmap >= LIGHTMAP_INDEX_OFFSET ) {
if ( bundle->tcGen == TCGEN_LIGHTMAP ) {
texModInfo_t *tmi = &bundle->texMods[bundle->numTexMods];
float x, y;
const int lightmapIndex = R_GetLightmapCoords( bundle->lightmap - LIGHTMAP_INDEX_OFFSET, &x, &y );
bundle->image[0] = tr.lightmaps[lightmapIndex];
tmi->type = TMOD_OFFSET;
tmi->offset[0] = x - tr.lightmapOffset[0];
tmi->offset[1] = y - tr.lightmapOffset[1];
bundle->numTexMods++;
}
continue;
}

// adjust texture coordinates to map on proper lightmap
if ( bundle->lightmap == LIGHTMAP_INDEX_SHADER && bundle->tcGen != TCGEN_LIGHTMAP ) {
texModInfo_t *tmi = &bundle->texMods[bundle->numTexMods];
tmi->type = TMOD_SCALE_OFFSET;
tmi->scale[0] = tr.lightmapScale[0];
tmi->scale[1] = tr.lightmapScale[1];
tmi->offset[0] = tr.lightmapOffset[0];
tmi->offset[1] = tr.lightmapOffset[1];
bundle->numTexMods++;
if ( bundle->lightmap == LIGHTMAP_INDEX_SHADER ) {
if ( bundle->tcGen != TCGEN_LIGHTMAP ) {
texModInfo_t *tmi = &bundle->texMods[bundle->numTexMods];
tmi->type = TMOD_SCALE_OFFSET;
tmi->scale[0] = tr.lightmapScale[0];
tmi->scale[1] = tr.lightmapScale[1];
tmi->offset[0] = tr.lightmapOffset[0];
tmi->offset[1] = tr.lightmapOffset[1];
bundle->numTexMods++;
} else {
for ( n = 0; n < bundle->numTexMods; n++ ) {
texModInfo_t *tmi = &bundle->texMods[n];
if ( tmi->type == TMOD_TRANSFORM ) {
tmi->translate[0] *= tr.lightmapScale[0];
tmi->translate[1] *= tr.lightmapScale[1];
} else {
// TODO: correct other transformations?
}
}
}
continue;
}
// revert lightmap texcoord correction if needed
if ( bundle->lightmap == LIGHTMAP_INDEX_NONE ) {
if ( bundle->tcGen == TCGEN_LIGHTMAP ) {
texModInfo_t *tmi;
for ( n = bundle->numTexMods; n > 0; --n ) {
bundle->texMods[n] = bundle->texMods[n - 1];
}
tmi = &bundle->texMods[0];
tmi->type = TMOD_OFFSET_SCALE;
tmi->offset[0] = -tr.lightmapOffset[0];
tmi->offset[1] = -tr.lightmapOffset[1];
tmi->scale[0] = 1.0f / tr.lightmapScale[0];
tmi->scale[1] = 1.0f / tr.lightmapScale[1];
bundle->numTexMods++;
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions code/renderervk/vk_vbo.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ static qboolean isStaticTCmod( const textureBundle_t *bundle )
case TMOD_TRANSFORM:
case TMOD_OFFSET:
case TMOD_SCALE_OFFSET:
case TMOD_OFFSET_SCALE:
break;
default:
return qfalse;
Expand Down

0 comments on commit 86ce720

Please sign in to comment.