Skip to content

Commit

Permalink
Update detect lightmap colors, is texture paletted
Browse files Browse the repository at this point in the history
Also added external palette files. Fix crash in language files.
  • Loading branch information
UnrealKaraulov committed Jan 26, 2024
1 parent 95ef7ab commit 5eac869
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 160 deletions.
5 changes: 3 additions & 2 deletions resources/bspguy.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ window_height=480
window_x=300
window_y=300
window_maximized=0
save_windows=0
save_windows=1
debug_open=0
keyvalue_open=0
transform_open=0
Expand Down Expand Up @@ -83,4 +83,5 @@ MAX_MAP_TEXTURES=4096
MAX_MAP_LIGHTDATA=64
MAX_TEXTURE_DIMENSION=1024
TEXTURE_STEP=16
language=EN
language=EN
palette=quake_1
4 changes: 2 additions & 2 deletions resources/languages/language.ini
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ LANG_0099 = Using extended marksurfaces [32 bit]\n
LANG_0100 = Using extended edges [32 bit]\n
LANG_0101 = mem size : {} - {} - {} or {}\n
LANG_0102 = lighting: {}\n
LANG_0103 = {}.bsp ent data (line {}): Unexpected '{'\n
LANG_0104 = {}.bsp ent data (line {}): Unexpected '}'\n
LANG_0103 = {}.bsp ent data (line {}): Unexpected '{{'\n
LANG_0104 = {}.bsp ent data (line {}): Unexpected '}}'\n
LANG_0105 = Found unknown classname entity. Skip it.\n
LANG_0106 = First entity has classname different from 'woldspawn', we do fixup it\n
LANG_0107 = {:>12}
Expand Down
4 changes: 2 additions & 2 deletions resources/languages/language_ru.ini
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ LANG_0099 = Используются расширенные марк-сурфе
LANG_0100 = Используются расширенные рёбра [32 бита]\n
LANG_0101 = размера памяти: {} - {} - {} или {}\n
LANG_0102 = освещения: {}\n
LANG_0103 = Список сущностей {}.bsp (строка {}): Неожиданный символ '{'\n
LANG_0104 = Список сущностей {}.bsp (строка {}): Неожиданный символ '}'\n
LANG_0103 = Список сущностей {}.bsp (строка {}): Неожиданный символ '{{'\n
LANG_0104 = Список сущностей {}.bsp (строка {}): Неожиданный символ '}}'\n
LANG_0105 = Найдена сущность с неизвестным классом. Пропущено.\n
LANG_0106 = Первая сущность имеет класс, отличный от 'worldspawn', исправляем\n
LANG_0107 = {:>12}
Expand Down
Binary file added resources/palettes/half_life.pal
Binary file not shown.
Binary file added resources/palettes/laser_arena.pal
Binary file not shown.
Binary file added resources/palettes/quake_1.pal
Binary file not shown.
130 changes: 35 additions & 95 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ Bsp::Bsp(std::string fpath)
is_broken_clipnodes = false;
is_blue_shift = false;
is_colored_lightmap = true;
is_texture_pal = true;

force_skip_crc = false;

Expand Down Expand Up @@ -1177,16 +1178,6 @@ bool Bsp::should_resize_lightmap(LIGHTMAP& oldLightmap, LIGHTMAP& newLightmap)
return false;
}

int Bsp::get_new_lightmaps_data_size()
{
int tmpLightDataSz = 0;
for (int i = 0; i < faceCount; i++) {
int size[2];
GetFaceLightmapSize(this, i, size);
tmpLightDataSz += size[0] * size[1] * sizeof(COLOR3) * lightmap_count(i);
}
return tmpLightDataSz;
}

void Bsp::resize_all_lightmaps(bool logged)
{
Expand Down Expand Up @@ -3599,79 +3590,57 @@ bool Bsp::load_lumps(std::string fpath)

update_lump_pointers();

int iFirstFace = -1;
int nOffseLight = 0;

for (int i = faceCount - 1; i >= 0; i--)
int lightmap3_bytes = 0;
for (int i = 0; i < faceCount; i++)
{
BSPFACE32& face = faces[i];
if (face.nLightmapOffset >= 0 && face.nLightmapOffset < bsp_header.lump[LUMP_LIGHTING].nLength)
if (faces[i].nLightmapOffset >= 0)
{
iFirstFace = i;
nOffseLight = face.nLightmapOffset;
break;
lightmap3_bytes += GetFaceLightmapSizeBytes(this, i);
}
}

int iNextFace = -1;
int nNextOffseLight = lightDataLength;
int lightmap1_bytes = lightmap3_bytes / 3;
is_colored_lightmap = lightdata == NULL || abs(lightmap1_bytes - lightDataLength) > abs(lightmap3_bytes - lightDataLength);

for (int i = iFirstFace - 1; i >= 0; i--)
{
BSPFACE32& face = faces[i];
if (face.nLightmapOffset >= 0 && face.nLightmapOffset < bsp_header.lump[LUMP_LIGHTING].nLength)
{
if (abs(face.nLightmapOffset - nOffseLight) < abs(nNextOffseLight - nOffseLight))
{
nNextOffseLight = face.nLightmapOffset;
iNextFace = i;
}
}
}
if (g_settings.verboseLogs)
print_log(get_localized_string(LANG_0102), !is_colored_lightmap ? "monochrome" : "colored");

float fLightSamples = is_bsp2 || is_bsp2_old || is_bsp29 ? 1.0f : 3.0f;
//print_log("{} {} {} -> {}\n", is_bsp2, is_bsp2_old, is_bsp29, fLightSamples);
if (fLightSamples < 3.0 && iNextFace >= 0 && iFirstFace >= 0 && iFirstFace != iNextFace)
int textures_bytes = 0;
int textures_no_pal_bytes = 0;

for (int t = 0; t < textureCount; t++)
{
float face1light = GetFaceLightmapSizeBytes(this, iFirstFace) / (float)sizeof(COLOR3);
float face2light = GetFaceLightmapSizeBytes(this, iNextFace) / (float)sizeof(COLOR3);
int iStartOffset = ((int*)textures)[t + 1];

int memsize = abs(nOffseLight - nNextOffseLight);
if (iStartOffset < 0 || iStartOffset + (int)sizeof(BSPMIPTEX) > textureDataLength)
continue;

if (iFirstFace > iNextFace)
{
fLightSamples = memsize / face1light;
}
else if (iNextFace > iFirstFace)
{
fLightSamples = memsize / face2light;
}
BSPMIPTEX* tex = ((BSPMIPTEX*)(textures + iStartOffset));
if (tex->nOffsets[0] > textureDataLength)
continue;

//print_log(get_localized_string(LANG_0101),fLightSamples,memsize,face1light,face2light);
textures_bytes += sizeof(BSPMIPTEX);
textures_no_pal_bytes += sizeof(BSPMIPTEX);

if (abs(fLightSamples - (int)fLightSamples) > 0.01f)
if (tex->nOffsets[0] > 0)
{
memsize = (memsize + 3) & ~3;
if (iFirstFace > iNextFace)
{
fLightSamples = memsize / face1light;
}
else if (iNextFace > iFirstFace)
textures_bytes += sizeof(short);
textures_no_pal_bytes += sizeof(short);

textures_bytes += sizeof(COLOR3) * 256;

for (int i = 0; i < MIPLEVELS; i++)
{
fLightSamples = memsize / face2light;
textures_bytes += (tex->nWidth >> i) * (tex->nHeight >> i);
textures_no_pal_bytes += (tex->nWidth >> i) * (tex->nHeight >> i);
}
//print_log(get_localized_string(LANG_1021),fLightSamples,memsize,face1light,face2light);
}
}

int tmp_is_colored_lightmap = (int)round(fLightSamples);
if (tmp_is_colored_lightmap < 1)
tmp_is_colored_lightmap = 1;

is_colored_lightmap = tmp_is_colored_lightmap > 1;

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(get_localized_string(LANG_0102), !is_colored_lightmap ? "monochrome" : "colored");
print_log("Embedded Textures: {}\n", !is_texture_pal ? "quake pal" : "has pal");

if (!is_colored_lightmap)
{
Expand Down Expand Up @@ -5331,7 +5300,7 @@ int Bsp::add_texture(const char* oldname, unsigned char* data, int width, int he
if (is_bsp2 || is_bsp29 || force_quake_pal)
{
if (!custompal)
memcpy(palette, quakeDefaultPalette, 256 * sizeof(COLOR3));
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);
Expand Down Expand Up @@ -7762,7 +7731,7 @@ void Bsp::ExportToObjWIP(const std::string& path, ExportObjOrder order, int isca
{
if (texOffset >= 0)
{
COLOR3* imageData = ConvertMipTexToRGB(((BSPMIPTEX*)(textures + texOffset)), is_texture_with_pal(texinfo.iMiptex) ? NULL : (COLOR3*)quakeDefaultPalette);
COLOR3* imageData = ConvertMipTexToRGB(((BSPMIPTEX*)(textures + texOffset)), is_texture_with_pal(texinfo.iMiptex) ? NULL : (COLOR3*)g_settings.palette_data);

for (int k = 0; k < tex.nHeight * tex.nWidth; k++)
{
Expand Down Expand Up @@ -8449,44 +8418,15 @@ bool Bsp::is_texture_with_pal(int textureid)
if (textureid < 0 || textureid >= textureCount)
return false;

if (bsp_header.nVersion == 30)
return true;

int iStartOffset = ((int*)textures)[textureid + 1];
unsigned char* pStartOffset = (unsigned char*)textures + iStartOffset;
unsigned char* pEndOffset = (unsigned char*)textures + textureDataLength;

if (iStartOffset >= 0)
{
for (int i = 0; i < textureCount; i++)
{
int iCurOffset = ((int*)textures)[i + 1];
if (iCurOffset < 0 || i == textureid || iCurOffset == iStartOffset)
{
continue;
}

unsigned char* pCurOffset = (unsigned char*)textures + iCurOffset;

if (pCurOffset < pEndOffset && pCurOffset > pStartOffset)
{
pEndOffset = pCurOffset;
}
}

BSPMIPTEX* tex = ((BSPMIPTEX*)(textures + iStartOffset));

if (tex->nOffsets[0] <= 0) // wad texture
return true;

int lastMipSize = (tex->nWidth / 8) * (tex->nHeight / 8);
unsigned char* palOffset = pStartOffset + tex->nOffsets[3] + lastMipSize;

if (abs(palOffset - pEndOffset) >= sizeof(COLOR3) * 256)
{
return true;
}
}

return false;
return is_texture_pal;
}
2 changes: 1 addition & 1 deletion src/bsp/Bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class Bsp
BSPMODEL* models;

bool is_colored_lightmap;
bool is_texture_pal;

std::string bsp_path;
std::string bsp_name;
Expand Down Expand Up @@ -333,7 +334,6 @@ class Bsp
void save_undo_lightmaps(bool logged = false);
void resize_all_lightmaps(bool logged = false);
bool should_resize_lightmap(LIGHTMAP& oldLightmap, LIGHTMAP& newLightmap);
int get_new_lightmaps_data_size();

private:
unsigned int remove_unused_lightmaps(bool* usedFaces);
Expand Down
53 changes: 0 additions & 53 deletions src/bsp/Wad.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,56 +136,3 @@ COLOR4* ConvertMipTexToRGBA(BSPMIPTEX* tex, COLOR3* palette = NULL);

COLOR3 GetMipTexAplhaColor(BSPMIPTEX* wadTex, COLOR3* palette = NULL);
COLOR3 GetWadTexAplhaColor(WADTEX* wadTex, COLOR3* palette = NULL);

const unsigned char quakeDefaultPalette[768] =
{
0,0,0,15,15,15,31,31,31,47,47,47,63,63,63,75,75,75,91,91,91,107,107,107,123,123,123,139,139,139,155,155,155,171,
171,171,187,187,187,203,203,203,219,219,219,235,235,235,15,11,7,23,15,11,31,23,11,39,27,15,47,35,19,55,43,23,63,
47,23,75,55,27,83,59,27,91,67,31,99,75,31,107,83,31,115,87,31,123,95,35,131,103,35,143,111,35,11,11,15,19,19,27,
27,27,39,39,39,51,47,47,63,55,55,75,63,63,87,71,71,103,79,79,115,91,91,127,99,99,139,107,107,151,115,115,163,123,
123,175,131,131,187,139,139,203,0,0,0,7,7,0,11,11,0,19,19,0,27,27,0,35,35,0,43,43,7,47,47,7,55,55,7,63,63,7,71,71,
7,75,75,11,83,83,11,91,91,11,99,99,11,107,107,15,7,0,0,15,0,0,23,0,0,31,0,0,39,0,0,47,0,0,55,0,0,63,0,0,71,0,0,79,
0,0,87,0,0,95,0,0,103,0,0,111,0,0,119,0,0,127,0,0,19,19,0,27,27,0,35,35,0,47,43,0,55,47,0,67,55,0,75,59,7,87,67,7,
95,71,7,107,75,11,119,83,15,131,87,19,139,91,19,151,95,27,163,99,31,175,103,35,35,19,7,47,23,11,59,31,15,75,35,19,
87,43,23,99,47,31,115,55,35,127,59,43,143,67,51,159,79,51,175,99,47,191,119,47,207,143,43,223,171,39,239,203,31,255,
243,27,11,7,0,27,19,0,43,35,15,55,43,19,71,51,27,83,55,35,99,63,43,111,71,51,127,83,63,139,95,71,155,107,83,167,123,
95,183,135,107,195,147,123,211,163,139,227,179,151,171,139,163,159,127,151,147,115,135,139,103,123,127,91,111,119,
83,99,107,75,87,95,63,75,87,55,67,75,47,55,67,39,47,55,31,35,43,23,27,35,19,19,23,11,11,15,7,7,187,115,159,175,107,
143,163,95,131,151,87,119,139,79,107,127,75,95,115,67,83,107,59,75,95,51,63,83,43,55,71,35,43,59,31,35,47,23,27,35,
19,19,23,11,11,15,7,7,219,195,187,203,179,167,191,163,155,175,151,139,163,135,123,151,123,111,135,111,95,123,99,83,
107,87,71,95,75,59,83,63,51,67,51,39,55,43,31,39,31,23,27,19,15,15,11,7,111,131,123,103,123,111,95,115,103,87,107,
95,79,99,87,71,91,79,63,83,71,55,75,63,47,67,55,43,59,47,35,51,39,31,43,31,23,35,23,15,27,19,11,19,11,7,11,7,255,
243,27,239,223,23,219,203,19,203,183,15,187,167,15,171,151,11,155,131,7,139,115,7,123,99,7,107,83,0,91,71,0,75,55,
0,59,43,0,43,31,0,27,15,0,11,7,0,0,0,255,11,11,239,19,19,223,27,27,207,35,35,191,43,43,175,47,47,159,47,47,143,47,
47,127,47,47,111,47,47,95,43,43,79,35,35,63,27,27,47,19,19,31,11,11,15,43,0,0,59,0,0,75,7,0,95,7,0,111,15,0,127,23,
7,147,31,7,163,39,11,183,51,15,195,75,27,207,99,43,219,127,59,227,151,79,231,171,95,239,191,119,247,211,139,167,123,
59,183,155,55,199,195,55,231,227,87,127,191,255,171,231,255,215,255,255,103,0,0,139,0,0,179,0,0,215,0,0,255,0,0,255,
243,147,255,247,199,255,255,255,159,91,83
};


const unsigned char halflifeDefaultPalette[768] =
{
0,0,0,15,15,15,31,31,31,47,47,47,63,63,63,75,75,75,91,91,91,107,107,107,123,123,123,139,139,139,155,155,155,171,
171,171,187,187,187,203,203,203,219,219,219,235,235,235,15,11,7,23,15,11,31,23,11,39,27,15,47,35,19,55,43,23,63,
47,23,75,55,27,83,59,27,91,67,31,99,75,31,107,83,31,115,87,31,123,95,35,131,103,35,143,111,35,11,11,15,19,19,27,
27,27,39,39,39,51,47,47,63,55,55,75,63,63,87,71,71,103,79,79,115,91,91,127,99,99,139,107,107,151,115,115,163,123,
123,175,131,131,187,139,139,203,0,0,0,7,7,0,11,11,0,19,19,0,27,27,0,35,35,0,43,43,7,47,47,7,55,55,7,63,63,7,71,71,
7,75,75,11,83,83,11,91,91,11,99,99,11,107,107,15,7,0,0,15,0,0,23,0,0,31,0,0,39,0,0,47,0,0,55,0,0,63,0,0,71,0,0,79,
0,0,87,0,0,95,0,0,103,0,0,111,0,0,119,0,0,127,0,0,19,19,0,27,27,0,35,35,0,47,43,0,55,47,0,67,55,0,75,59,7,87,67,7,
95,71,7,107,75,11,119,83,15,131,87,19,139,91,19,151,95,27,163,99,31,175,103,35,35,19,7,47,23,11,59,31,15,75,35,19,
87,43,23,99,47,31,115,55,35,127,59,43,143,67,51,159,79,51,175,99,47,191,119,47,207,143,43,223,171,39,239,203,31,255,
243,27,11,7,0,27,19,0,43,35,15,55,43,19,71,51,27,83,55,35,99,63,43,111,71,51,127,83,63,139,95,71,155,107,83,167,123,
95,183,135,107,195,147,123,211,163,139,227,179,151,171,139,163,159,127,151,147,115,135,139,103,123,127,91,111,119,
83,99,107,75,87,95,63,75,87,55,67,75,47,55,67,39,47,55,31,35,43,23,27,35,19,19,23,11,11,15,7,7,187,115,159,175,107,
143,163,95,131,151,87,119,139,79,107,127,75,95,115,67,83,107,59,75,95,51,63,83,43,55,71,35,43,59,31,35,47,23,27,35,
19,19,23,11,11,15,7,7,219,195,187,203,179,167,191,163,155,175,151,139,163,135,123,151,123,111,135,111,95,123,99,83,
107,87,71,95,75,59,83,63,51,67,51,39,55,43,31,39,31,23,27,19,15,15,11,7,111,131,123,103,123,111,95,115,103,87,107,
95,79,99,87,71,91,79,63,83,71,55,75,63,47,67,55,43,59,47,35,51,39,31,43,31,23,35,23,15,27,19,11,19,11,7,11,7,255,
243,27,239,223,23,219,203,19,203,183,15,187,167,15,171,151,11,155,131,7,139,115,7,123,99,7,107,83,0,91,71,0,75,55,
0,59,43,0,43,31,0,27,15,0,11,7,0,0,0,255,11,11,239,19,19,223,27,27,207,35,35,191,43,43,175,47,47,159,47,47,143,47,
47,127,47,47,111,47,47,95,43,43,79,35,35,63,27,27,47,19,19,31,11,11,15,43,0,0,59,0,0,75,7,0,95,7,0,111,15,0,127,23,
7,147,31,7,163,39,11,183,51,15,195,75,27,207,99,43,219,127,59,227,151,79,231,171,95,239,191,119,247,211,139,167,123,
59,183,155,55,199,195,55,231,227,87,0,255,0,171,231,255,215,255,255,103,0,0,139,0,0,179,0,0,215,0,0,255,0,0,255,243,
147,255,247,199,255,255,255,159,91,83
};
2 changes: 1 addition & 1 deletion src/editor/BspRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ void BspRenderer::loadTextures()
}
else
{
imageData = ConvertMipTexToRGB(tex, map->is_texture_with_pal(i) ? NULL : (COLOR3*)quakeDefaultPalette);
imageData = ConvertMipTexToRGB(tex, map->is_texture_with_pal(i) ? NULL : (COLOR3*)g_settings.palette_data);
embedCount++;
}

Expand Down
20 changes: 18 additions & 2 deletions src/editor/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2780,8 +2780,6 @@ void Gui::drawMenuBar()

if (ImGui::BeginMenu("Delete cull faces", map))
{
int leafIdx = 0;

if (ImGui::MenuItem("Delete from [SKY LEAFS]"))
{
map->remove_faces_by_content(CONTENTS_SKY);
Expand Down Expand Up @@ -5840,6 +5838,7 @@ void Gui::drawSettings()
bool oldShowSettings = showSettingsWidget;
bool apply_settings_pressed = false;
static std::string langForSelect = g_settings.selected_lang;
static std::string palForSelect = g_settings.palette_name;

if (ImGui::Begin(fmt::format("{}###SETTING_WIDGET", get_localized_string(LANG_1114)).c_str(), &showSettingsWidget))
{
Expand Down Expand Up @@ -6065,6 +6064,8 @@ void Gui::drawSettings()
ImGui::EndTooltip();
}
ImGui::Separator();
ImGui::TextUnformatted("Language:");
ImGui::SameLine();
if (ImGui::BeginCombo("##lang", langForSelect.c_str()))
{
for (const auto& s : g_settings.languages)
Expand All @@ -6077,6 +6078,20 @@ void Gui::drawSettings()
ImGui::EndCombo();
}
ImGui::Separator();
ImGui::TextUnformatted("Palette:");
ImGui::SameLine();
if (ImGui::BeginCombo("##pal", palForSelect.c_str()))
{
for (const auto& s : g_settings.palettes)
{
if (ImGui::Selectable(s.name.c_str(), s.name == palForSelect))
{
palForSelect = s.name;
}
}
ImGui::EndCombo();
}
ImGui::Separator();
if (ImGui::Button(get_localized_string(LANG_0739).c_str()))
{
g_settings.reset();
Expand Down Expand Up @@ -6592,6 +6607,7 @@ void Gui::drawSettings()
if (oldShowSettings && !showSettingsWidget || apply_settings_pressed)
{
g_settings.selected_lang = langForSelect;
g_settings.palette_name = palForSelect;
set_localize_lang(g_settings.selected_lang);
g_settings.save();
if (!app->reloading)
Expand Down
Loading

0 comments on commit 5eac869

Please sign in to comment.