Skip to content

Commit a8782f0

Browse files
committed
tr_image: rework the alpha detection
1 parent 4a6dfa5 commit a8782f0

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

src/engine/renderer/tr_image.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,30 +1054,44 @@ void R_UploadImage( const char *name, const byte **dataArray, int numLayers, int
10541054
}
10551055
else
10561056
{
1057-
// scan the texture for each channel's max values
1058-
// and verify if the alpha channel is being used or not
1057+
// lightmap does not have alpha channel
1058+
if ( image->bits & IF_LIGHTMAP )
1059+
{
1060+
internalFormat = GL_RGB8;
1061+
}
1062+
else
1063+
{
1064+
internalFormat = GL_RGBA8;
1065+
}
1066+
}
10591067

1060-
c = image->width * image->height;
1061-
scan = dataArray[0];
1068+
// Detect formats.
1069+
if ( dataArray )
1070+
{
1071+
if ( internalFormat == GL_RGBA8 )
1072+
{
1073+
/* Scan the texture for alpha channel's max values
1074+
and verify if the alpha channel is being used or not. */
10621075

1063-
// lightmap does not have alpha channel
1076+
c = image->width * image->height;
1077+
scan = dataArray[0];
10641078

1065-
// normalmap may have the heightmap in the alpha channel
1066-
// opaque alpha channel means no displacement, so we can enable
1067-
// alpha channel everytime it is used, even for normalmap
1079+
/* A normalmap may have the heightmap in the alpha channel,
1080+
an opaque alpha channel means no displacement, so we can enable
1081+
the alpha channel everytime it is used, even for normalmap. */
10681082

1069-
internalFormat = GL_RGB8;
1083+
internalFormat = GL_RGB8;
10701084

1071-
if ( !( image->bits & IF_LIGHTMAP ) )
1072-
{
1073-
for ( i = 0; i < c; i++ )
1085+
for ( i = 0; i < c * 4; i += 4 )
10741086
{
1075-
if ( scan[ i * 4 + 3 ] != 255 )
1087+
if ( scan[ i + 3 ] != 255 )
10761088
{
10771089
internalFormat = GL_RGBA8;
10781090
break;
10791091
}
10801092
}
1093+
1094+
internalFormat = hasAlpha ? GL_RGBA8 : GL_RGB8;
10811095
}
10821096
}
10831097

0 commit comments

Comments
 (0)