Skip to content

Commit

Permalink
Support (bad support) RGBA lightmaps!
Browse files Browse the repository at this point in the history
Support (bad support)  RGBA lightmaps!
  • Loading branch information
UnrealKaraulov committed Jan 27, 2024
1 parent 5eac869 commit 3be6f85
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 43 deletions.
98 changes: 75 additions & 23 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3590,21 +3590,43 @@ bool Bsp::load_lumps(std::string fpath)

update_lump_pointers();


std::set<int> tmp_offsets;
int lightmap3_bytes = 0;
for (int i = 0; i < faceCount; i++)
{
if (faces[i].nLightmapOffset >= 0)
int light_offset = faces[i].nLightmapOffset;

if (light_offset >= 0 && !tmp_offsets.count(light_offset))
{
tmp_offsets.insert(light_offset);
lightmap3_bytes += GetFaceLightmapSizeBytes(this, i);
}
}

int lightmap1_bytes = lightmap3_bytes / 3;
int lightmap1_bytes = lightmap3_bytes / sizeof(COLOR3);
int lightmap4_bytes = lightmap1_bytes * sizeof(COLOR4);

is_colored_lightmap = lightdata == NULL || abs(lightmap1_bytes - lightDataLength) > abs(lightmap3_bytes - lightDataLength);

bool is_fuck_rgba_lightmap = false;

if (is_colored_lightmap && lightdata != NULL)
{
if (abs(lightmap3_bytes - lightDataLength) > abs(lightmap4_bytes - lightDataLength))
{
is_fuck_rgba_lightmap = true;
if (g_settings.verboseLogs)
{
print_log("fuck rgba lightmaps detected\n");
}
}
}

if (g_settings.verboseLogs)
print_log(get_localized_string(LANG_0102), !is_colored_lightmap ? "monochrome" : "colored");
{
//print_log(get_localized_string(LANG_0102), !is_colored_lightmap ? "monochrome" : "colored");
print_log("Light: {} [mono {}, color {}, map has {}]\n", !is_colored_lightmap ? "monochrome" : "colored", lightmap1_bytes, lightmap3_bytes, lightDataLength);
}

int textures_bytes = 0;
int textures_no_pal_bytes = 0;
Expand All @@ -3617,13 +3639,15 @@ bool Bsp::load_lumps(std::string fpath)
continue;

BSPMIPTEX* tex = ((BSPMIPTEX*)(textures + iStartOffset));
if (tex->nOffsets[0] > textureDataLength)

int data_offset = tex->nOffsets[0];

if (data_offset > textureDataLength)
continue;

textures_bytes += sizeof(BSPMIPTEX);
textures_no_pal_bytes += sizeof(BSPMIPTEX);

if (tex->nOffsets[0] > 0)
if (data_offset > 0)
{
textures_bytes += sizeof(short);
textures_no_pal_bytes += sizeof(short);
Expand All @@ -3638,9 +3662,13 @@ bool Bsp::load_lumps(std::string fpath)
}
}

is_texture_pal = textureCount == 0 || textures_no_pal_bytes == textures_bytes || abs(textures_no_pal_bytes - textureDataLength) > abs(textures_bytes - textureDataLength);;
is_texture_pal = textureCount == 0 || textures_no_pal_bytes == textures_bytes || abs(textures_no_pal_bytes - textureDataLength) > abs(textures_bytes - textureDataLength);

if (g_settings.verboseLogs)
print_log("Embedded Textures: {}\n", !is_texture_pal ? "quake pal" : "has pal");
{
//print_log("Embedded Textures: {}\n", !is_texture_pal ? "quake pal" : "has pal");
print_log("Embedded Textures: {} [pal:{}, nopal:{}, map has:{}]\n", !is_texture_pal ? "quake pal" : "has pal", textures_bytes, textures_no_pal_bytes, textureDataLength);
}

if (!is_colored_lightmap)
{
Expand All @@ -3653,7 +3681,8 @@ bool Bsp::load_lumps(std::string fpath)
newLight[m] = COLOR3(lumps[LUMP_LIGHTING][m], lumps[LUMP_LIGHTING][m], lumps[LUMP_LIGHTING][m]);
}

delete lumps[LUMP_LIGHTING];
if (replacedLump[LUMP_LIGHTING])
delete lumps[LUMP_LIGHTING];

lumps[LUMP_LIGHTING] = (unsigned char*)newLight;
bsp_header.lump[LUMP_LIGHTING].nLength = lightPixels * sizeof(COLOR3);
Expand All @@ -3664,6 +3693,30 @@ bool Bsp::load_lumps(std::string fpath)
faces[n].nLightmapOffset = faces[n].nLightmapOffset * sizeof(COLOR3);
}
}
else if (is_fuck_rgba_lightmap)
{
int lightPixels = bsp_header.lump[LUMP_LIGHTING].nLength / sizeof(COLOR4);

COLOR3* newLight = new COLOR3[lightPixels];
COLOR4* oldLight = (COLOR4*)lumps[LUMP_LIGHTING];

for (int m = 0; m < lightPixels; m++)
{
newLight[m] = oldLight[m].rgb(COLOR3(0, 0, 0));
}

if (replacedLump[LUMP_LIGHTING])
delete lumps[LUMP_LIGHTING];

lumps[LUMP_LIGHTING] = (unsigned char*)newLight;
bsp_header.lump[LUMP_LIGHTING].nLength = lightPixels * sizeof(COLOR3);


for (int n = 0; n < faceCount; n++)
{
faces[n].nLightmapOffset = (faces[n].nLightmapOffset / sizeof(COLOR4)) * sizeof(COLOR3);
}
}

originCrc32 = crc32;

Expand Down Expand Up @@ -5273,7 +5326,7 @@ int Bsp::add_texture(const char* oldname, unsigned char* data, int width, int he
int colorCount = 0;


if (!is_bsp2 && !is_bsp29 && !force_quake_pal)
if (is_texture_pal && !force_quake_pal)
{
texDataSize += width * height + sizeof(short) /* palette count */ + sizeof(COLOR3) * 256;
}
Expand All @@ -5290,23 +5343,22 @@ int Bsp::add_texture(const char* oldname, unsigned char* data, int width, int he
COLOR3* src = (COLOR3*)data;

// If custom pal || quake || force quake
if (is_bsp2 || is_bsp29 || force_quake_pal || custompal)
if (!is_texture_pal || force_quake_pal)
{
colorCount = 256;
if (custompal)
{
memcpy(palette, custompal, 256 * sizeof(COLOR3));
colorCount = 256;
}
if (is_bsp2 || is_bsp29 || force_quake_pal)
else
{
if (!custompal)
memcpy(palette, g_settings.palette_data, 256 * sizeof(COLOR3));
Quantizer* tmpCQuantizer = new Quantizer(256, 8);
tmpCQuantizer->SetColorTable(palette, 256);
tmpCQuantizer->ApplyColorTable((COLOR3*)data, width * height);
delete tmpCQuantizer;
colorCount = 256;
memcpy(palette, g_settings.palette_data, 256 * sizeof(COLOR3));
}
Quantizer* tmpCQuantizer = new Quantizer(256, 8);
tmpCQuantizer->SetColorTable(palette, 256);
tmpCQuantizer->ApplyColorTable((COLOR3*)data, width * height);
delete tmpCQuantizer;
colorCount = 256;
}

// create pallete and full-rez mipmap
Expand Down Expand Up @@ -5395,7 +5447,7 @@ int Bsp::add_texture(const char* oldname, unsigned char* data, int width, int he

size_t palleteOffset = oldtex->nOffsets[3] + (width >> 3) * (height >> 3);

if (!is_bsp2 && !is_bsp29 && !force_quake_pal)
if (is_texture_pal && !force_quake_pal)
{
palleteOffset += sizeof(short) /* pal count */;
memcpy(textures + newTexOffset + palleteOffset, palette, sizeof(COLOR3) * 256);
Expand Down Expand Up @@ -5475,7 +5527,7 @@ int Bsp::add_texture(const char* oldname, unsigned char* data, int width, int he
size_t palleteOffset = newMipTex->nOffsets[3] + (width >> 3) * (height >> 3) + sizeof(short);
unsigned char* paletteCount = newTexData + newTexOffset + (palleteOffset - sizeof(short));

if (!is_bsp2 && !is_bsp29 && !force_quake_pal)
if (is_texture_pal && !force_quake_pal)
{
memcpy(newTexData + newTexOffset + palleteOffset, palette, sizeof(COLOR3) * 256);
}
Expand Down
1 change: 1 addition & 0 deletions src/bsp/bsplimits.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#define MAXTEXTURENAME 16
#define MIPLEVELS 4

#define MAX_MAP_HULLS 4
#define MAX_MAP_PLANES 65535
#define MAX_MAP_TEXINFOS 32767 // Can be 65535 if unsigned short?
Expand Down
12 changes: 0 additions & 12 deletions src/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,18 +277,6 @@ int getTextureSizeInBytes(BSPMIPTEX* bspTexture, bool palette)
return sz;
}

float clamp(float val, float min, float max)
{
if (val > max)
{
return max;
}
else if (val < min)
{
return min;
}
return val;
}

vec3 parseVector(const std::string& s)
{
Expand Down
2 changes: 0 additions & 2 deletions src/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,6 @@ std::string trimSpaces(const std::string& str);

int getTextureSizeInBytes(BSPMIPTEX* bspTexture, bool palette = false);

float clamp(float val, float min, float max);

vec3 parseVector(const std::string& s);

bool IsEntNotSupportAngles(std::string& entname);
Expand Down
14 changes: 14 additions & 0 deletions src/util/vectors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,3 +888,17 @@ void VectorIRotate(const vec3& in1, const float in2[3][4], vec3& out)
out[1] = in1[0] * in2[0][1] + in1[1] * in2[1][1] + in1[2] * in2[2][1];
out[2] = in1[0] * in2[0][2] + in1[1] * in2[1][2] + in1[2] * in2[2][2];
}


float clamp(float val, float min, float max)
{
if (val > max)
{
return max;
}
else if (val < min)
{
return min;
}
return val;
}
25 changes: 19 additions & 6 deletions src/util/vectors.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#define ON_EPSILON 0.03125f


float clamp(float val, float min, float max);

#define CLAMP(v, min, max) if (v < min) { v = min; } else if (v > max) { v = max; }

struct COLOR3
Expand All @@ -32,26 +34,37 @@ struct COLOR4
{}
COLOR4(const COLOR3& c) : r(c.r), g(c.g), b(c.b), a(255)
{}

COLOR3 rgb(COLOR3 background) {
float alpha = a / 255.0;
unsigned char r_new = clamp((1 - alpha) * r + alpha * background.r, 0.0, 255.0f);
unsigned char g_new = clamp((1 - alpha) * g + alpha * background.g, 0.0, 255.0f);
unsigned char b_new = clamp((1 - alpha) * b + alpha * background.b, 0.0, 255.0f);
return COLOR3(r_new, g_new, b_new);
}
COLOR3 rgb() {
return COLOR3(r, g, b);
}
};


struct vec3
{
float x, y, z;

void Copy(const vec3& other)
void Copy(const vec3& other)
{
x = other.x;
y = other.y;
z = other.z;
}
vec3& operator =(const vec3& other)
{
Copy(other);
Copy(other);
return *this;
}

vec3(const vec3& other)
vec3(const vec3& other)
{
Copy(other);
}
Expand Down Expand Up @@ -87,7 +100,7 @@ struct vec3
std::string toString();
vec3 flip(); // flip from opengl to Half-life coordinate system and vice versa
vec3 flipUV(); // flip from opengl to Half-life coordinate system and vice versa
vec3 unflip();
vec3 unflip();
vec3 unflipUV();

void operator-=(const vec3& v);
Expand Down Expand Up @@ -158,7 +171,7 @@ struct pairHash {
seed ^= hasher(p.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
return seed;
}
};
};

vec3 operator-(vec3 v1, const vec3& v2);
vec3 operator+(vec3 v1, const vec3& v2);
Expand Down Expand Up @@ -260,7 +273,7 @@ struct vec4
if (abs(w) < EPSILON)
w = +0.0f;
}
vec4(const COLOR4& c ) : x(c.r / 255.0f), y(c.g / 255.0f), z(c.b / 255.0f), w(c.a / 255.0f)
vec4(const COLOR4& c) : x(c.r / 255.0f), y(c.g / 255.0f), z(c.b / 255.0f), w(c.a / 255.0f)
{
if (abs(x) < EPSILON)
x = +0.0f;
Expand Down

0 comments on commit 3be6f85

Please sign in to comment.