Skip to content

Commit

Permalink
glwidgets, ddsData, bindTexturePatch: Reduce the logging spam, improv…
Browse files Browse the repository at this point in the history
…e comments in the thingie that reads the average texture color. Add a `pixel_data_present` field to make sure we only check for green normalmaps when the pixel data is there, avoiding false positives.
  • Loading branch information
Swyter committed May 8, 2024
1 parent 9672d28 commit dfaa295
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
17 changes: 11 additions & 6 deletions bindTexturePatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,21 +227,26 @@ bool GLWidget::myBindTexture(const QString &fileName, DdsData &data)
free(pixels);

uint8_t decodedPixels[4u * 4u * sizeof(uint32_t)] = {0};

/* swy: if it does not fix in our small buffer (we're interested in tiny mip sizes like 1x1 or 4x4 pixels), then ignore it */
if ((w * h * sizeof(uint32_t)) <= sizeof(decodedPixels)) {
/* swy: grab the decoded pixel data of the last mipmap; which should contain a single pixel with the average color, useful to analyze the texture contents for the normalmap encoding and RGBA channel usage */
glGetTexImage(GL_TEXTURE_2D, max((int) ddsHeader.dwMipMapCount - 1, 0), GL_RGBA, GL_UNSIGNED_BYTE, &decodedPixels); /* swy: make sure we pick mip index zero when there's only a single mipmap, instead of -1; which will cause a crash */
/* swy: grab the decoded pixel data of the last mipmap; which should contain a single pixel with the average color,
useful to analyze the texture contents for the normalmap encoding and RGBA channel usage */
glGetTexImage(GL_TEXTURE_2D, max((int) ddsHeader.dwMipMapCount - 1, 0), GL_RGBA, GL_UNSIGNED_BYTE, &decodedPixels); /* swy: make sure we pick mip index zero when there's only a single mipmap, instead of -1; which will cause a crash */

qDebug("glGetTexImage: %hhx, %hhx, %hhx, %hhx, %hhx, %hhx, %hhx, %hhx", decodedPixels[0], decodedPixels[1], decodedPixels[2], decodedPixels[3], decodedPixels[4], decodedPixels[5], decodedPixels[6], decodedPixels[7]);
qDebug() << fileName;
qDebug("glGetTexImage: %hhx, %hhx, %hhx, %hhx, %hhx, %hhx, %hhx, %hhx", decodedPixels[0], decodedPixels[1], decodedPixels[2], decodedPixels[3], decodedPixels[4], decodedPixels[5], decodedPixels[6], decodedPixels[7]);
qDebug() << fileName;
data.pixel_data_present = true;
} else {
qDebug("glGetTexImage: couldn't retrieve the decoded pixel data. mipcount=%u, w=%u, h=%u", ddsHeader.dwMipMapCount, w, h);
qDebug("glGetTexImage: couldn't retrieve the decoded pixel data. mipcount=%u, w=%u, h=%u", ddsHeader.dwMipMapCount, w, h);
data.pixel_data_present = false;
}

data.r = decodedPixels[0] / 256.f,
data.g = decodedPixels[1] / 256.f,
data.b = decodedPixels[2] / 256.f,
data.a = decodedPixels[3] / 256.f;
qDebug("glGetTexImage: float %f %f %f %f", data.r, data.g, data.b, data.a);
qDebug("glGetTexImage: float %f %f %f %f. mipcount=%u, w=%u, h=%u", data.r, data.g, data.b, data.a, ddsHeader.dwMipMapCount, w, h);

qgl_dds_cache()->insert(fileName, data);
return true;
Expand Down
1 change: 1 addition & 0 deletions ddsData.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ typedef struct {
int filesize;
int ddxversion;
int location;
bool pixel_data_present;
float r, g, b, a; /* swy: average color of the last mipmap; for content detection like distinguishing between green and blue normalmaps */
} DdsData;

Expand Down
8 changes: 1 addition & 7 deletions glwidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1056,19 +1056,13 @@ void GLWidget::setTextureName(QString s, int origin, int texUnit){
/* swy: try to guess the normal map channel encoding from the average color */
/* - green normal map RGBA/0X0Y: ~0 , ~0.50196, ~0, ~0.50196 */
/* - blue normal map RGBA/XY__: ~0.50196, ~0.50196, ~1.0, ~1.0 */
if (ta && /* swy: only formats with alpha channel like DXT3 and DXT5 make sense */
if (ta && data.pixel_data_present && /* swy: only formats with alpha channel like DXT3 and DXT5 make sense */
data.r <= 0.1f && data.b <= 0.1f && /* swy: R_B_ should be almost zero/black */
(data.g >= 0.4f && data.g <= 0.6f) && /* swy: _G_A should contains the normals which should be around 0.5 */
(data.a >= 0.4f && data.a <= 0.6f))
{
nmgreen=true;
qDebug("green nm!\n");
}
else
{
nmgreen=false;
qDebug("blue nm!\n");
}

//qDebug("Settexture: ta=%d, name=%s",data.ddxversion,s.toLatin1().data());
} else {
Expand Down

0 comments on commit dfaa295

Please sign in to comment.