Skip to content

Commit

Permalink
Fix model import and duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
UnrealKaraulov committed Dec 18, 2023
1 parent 3ebcee0 commit 46edbc7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 29 deletions.
51 changes: 39 additions & 12 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6155,7 +6155,7 @@ int Bsp::create_texinfo()
void Bsp::copy_bsp_model(int modelIdx, Bsp* targetMap, STRUCTREMAP& remap, std::vector<BSPPLANE>& newPlanes, std::vector<vec3>& newVerts,
std::vector<BSPEDGE32>& newEdges, std::vector<int>& newSurfedges, std::vector<BSPTEXTUREINFO>& newTexinfo,
std::vector<BSPFACE32>& newFaces, std::vector<COLOR3>& newLightmaps, std::vector<BSPNODE32>& newNodes,
std::vector<BSPCLIPNODE32>& newClipnodes, std::vector<WADTEX> & newTextures)
std::vector<BSPCLIPNODE32>& newClipnodes, std::vector<WADTEX *> & newTextures)
{
STRUCTUSAGE usage(this);
mark_model_structures(modelIdx, &usage, true);
Expand All @@ -6165,7 +6165,7 @@ void Bsp::copy_bsp_model(int modelIdx, Bsp* targetMap, STRUCTREMAP& remap, std::
if (usage.planes[i])
{
remap.planes[i] = targetMap->planeCount + (int)newPlanes.size();
newPlanes.push_back(planes[i]);
newPlanes.push_back(this->planes[i]);
}
}

Expand All @@ -6174,7 +6174,7 @@ void Bsp::copy_bsp_model(int modelIdx, Bsp* targetMap, STRUCTREMAP& remap, std::
if (usage.verts[i])
{
remap.verts[i] = targetMap->vertCount + (int)newVerts.size();
newVerts.push_back(verts[i]);
newVerts.push_back(this->verts[i]);
}
}

Expand All @@ -6184,7 +6184,7 @@ void Bsp::copy_bsp_model(int modelIdx, Bsp* targetMap, STRUCTREMAP& remap, std::
{
remap.edges[i] = targetMap->edgeCount + (int)newEdges.size();

BSPEDGE32 edge = edges[i];
BSPEDGE32 edge = this->edges[i];
for (int k = 0; k < 2; k++)
edge.iVertex[k] = remap.verts[edge.iVertex[k]];
newEdges.push_back(edge);
Expand All @@ -6197,19 +6197,38 @@ void Bsp::copy_bsp_model(int modelIdx, Bsp* targetMap, STRUCTREMAP& remap, std::
{
remap.surfEdges[i] = targetMap->surfedgeCount + (int)newSurfedges.size();

int surfedge = remap.edges[abs(surfedges[i])];
int surfedge = remap.edges[abs(this->surfedges[i])];
if (surfedges[i] < 0)
surfedge = -surfedge;
newSurfedges.push_back(surfedge);
}
}

// copy src map textures for adding to new
std::set<int> usedmips;

for (unsigned int i = 0; i < this->texinfoCount; i++)
{
BSPTEXTUREINFO& texinfo = this->texinfos[i];
if (texinfo.iMiptex >= 0 && texinfo.iMiptex < this->textureCount && !usedmips.count(texinfo.iMiptex))
{
int texOffset = ((int*)this->textures)[texinfo.iMiptex + 1];
if (texOffset >= 0)
{
usedmips.insert(texinfo.iMiptex);
BSPMIPTEX * tex = ((BSPMIPTEX*)(this->textures + texOffset));
WADTEX* newTex = new WADTEX(tex);
newTextures.push_back(newTex);
}
}
}

for (unsigned int i = 0; i < usage.count.texInfos; i++)
{
if (usage.texInfo[i])
{
remap.texInfo[i] = targetMap->texinfoCount + (int)newTexinfo.size();
newTexinfo.push_back(texinfos[i]);
newTexinfo.push_back(this->texinfos[i]);
}
}

Expand All @@ -6235,7 +6254,7 @@ void Bsp::copy_bsp_model(int modelIdx, Bsp* targetMap, STRUCTREMAP& remap, std::

if (face.nLightmapOffset >= 0 && lightmapCount > 0)
{
COLOR3* lightmapSrc = (COLOR3*)(lightdata + face.nLightmapOffset);
COLOR3* lightmapSrc = (COLOR3*)(this->lightdata + face.nLightmapOffset);
for (int k = 0; k < lightmapSz; k++)
{
newLightmaps.push_back(lightmapSrc[k]);
Expand All @@ -6248,7 +6267,6 @@ void Bsp::copy_bsp_model(int modelIdx, Bsp* targetMap, STRUCTREMAP& remap, std::
}
}


newFaces.push_back(face);

lightmapAppendSz += lightmapSz * sizeof(COLOR3);
Expand All @@ -6260,7 +6278,7 @@ void Bsp::copy_bsp_model(int modelIdx, Bsp* targetMap, STRUCTREMAP& remap, std::
if (usage.nodes[i])
{
remap.nodes[i] = targetMap->nodeCount + (int)newNodes.size();
newNodes.push_back(nodes[i]);
newNodes.push_back(this->nodes[i]);
}
}

Expand All @@ -6284,7 +6302,7 @@ void Bsp::copy_bsp_model(int modelIdx, Bsp* targetMap, STRUCTREMAP& remap, std::
if (usage.clipnodes[i])
{
remap.clipnodes[i] = targetMap->clipnodeCount + (int)newClipnodes.size();
newClipnodes.push_back(clipnodes[i]);
newClipnodes.push_back(this->clipnodes[i]);
}
}

Expand Down Expand Up @@ -6314,10 +6332,14 @@ void Bsp::duplicate_model_structures(int modelIdx)
std::vector<COLOR3> newLightmaps;
std::vector<BSPNODE32> newNodes;
std::vector<BSPCLIPNODE32> newClipnodes;
std::vector<WADTEX> newTextures;
std::vector<WADTEX*> newTextures;

STRUCTREMAP remap(this);
copy_bsp_model(modelIdx, this, remap, newPlanes, newVerts, newEdges, newSurfedges, newTexinfo, newFaces, newLightmaps, newNodes, newClipnodes, newTextures);
for (auto& s : newTextures)
{
delete s;
}

if (newClipnodes.size())
append_lump(LUMP_CLIPNODES, &newClipnodes[0], sizeof(BSPCLIPNODE32) * newClipnodes.size());
Expand Down Expand Up @@ -6365,11 +6387,16 @@ int Bsp::duplicate_model(int modelIdx)
std::vector<COLOR3> newLightmaps;
std::vector<BSPNODE32> newNodes;
std::vector<BSPCLIPNODE32> newClipnodes;
std::vector<WADTEX> newTextures;
std::vector<WADTEX *> newTextures;

STRUCTREMAP remap(this);
copy_bsp_model(modelIdx, this, remap, newPlanes, newVerts, newEdges, newSurfedges, newTexinfo, newFaces, newLightmaps, newNodes, newClipnodes, newTextures);

for (auto & s : newTextures)
{
delete s;
}

if (newClipnodes.size())
append_lump(LUMP_CLIPNODES, &newClipnodes[0], sizeof(BSPCLIPNODE32) * newClipnodes.size());
if (newEdges.size())
Expand Down
2 changes: 1 addition & 1 deletion src/bsp/Bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ class Bsp
void copy_bsp_model(int modelIdx, Bsp* targetMap, STRUCTREMAP& remap, std::vector<BSPPLANE>& newPlanes, std::vector<vec3>& newVerts,
std::vector<BSPEDGE32>& newEdges, std::vector<int>& newSurfedges, std::vector<BSPTEXTUREINFO>& newTexinfo,
std::vector<BSPFACE32>& newFaces, std::vector<COLOR3>& newLightmaps, std::vector<BSPNODE32>& newNodes,
std::vector<BSPCLIPNODE32>& newClipnodes, std::vector<WADTEX>& newTextures);
std::vector<BSPCLIPNODE32>& newClipnodes, std::vector<WADTEX *>& newTextures);

int duplicate_model(int modelIdx);
void duplicate_model_structures(int modelIdx);
Expand Down
6 changes: 3 additions & 3 deletions src/bsp/Wad.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct WADTEX
int nWidth, nHeight;
int nOffsets[MIPLEVELS];
unsigned char* data; // all mip-maps and pallete
bool needclean = false;
bool needclean;
WADTEX()
{
needclean = false;
Expand All @@ -47,7 +47,6 @@ struct WADTEX
}
WADTEX(BSPMIPTEX* tex)
{
needclean = false;
memcpy(szName, tex->szName, MAXTEXTURENAME);

nWidth = tex->nWidth;
Expand All @@ -57,6 +56,7 @@ struct WADTEX

if (nOffsets[0] <= 0)
{
needclean = false;
data = NULL;
return;
}
Expand All @@ -83,7 +83,7 @@ struct WADTEX
}
~WADTEX()
{
if (needclean)
if (needclean && data)
delete[] data;
needclean = false;
szName[0] = '\0';
Expand Down
6 changes: 3 additions & 3 deletions src/editor/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ void DuplicateBspModelCommand::execute()

ent->setOrAddKeyvalue("model", "*" + std::to_string(newModelIdx));


map->remove_unused_model_structures(CLEAN_LEAVES);

renderer->loadLightmaps();
renderer->calcFaceMaths();
renderer->preRenderFaces();
Expand All @@ -294,9 +297,6 @@ void DuplicateBspModelCommand::execute()
pickCount++;
vertPickCount++;


map->remove_unused_model_structures(CLEAN_LEAVES);

g_app->gui->refresh();

/*
Expand Down
28 changes: 18 additions & 10 deletions src/editor/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6710,7 +6710,7 @@ void Gui::drawImportMapWidget()
std::vector<COLOR3> newLightmaps;
std::vector<BSPNODE32> newNodes;
std::vector<BSPCLIPNODE32> newClipnodes;
std::vector<WADTEX> newTextures;
std::vector<WADTEX*> newTextures;

STRUCTREMAP remap = STRUCTREMAP(map);
bspModel->copy_bsp_model(0, map, remap, newPlanes, newVerts, newEdges, newSurfedges, newTexinfo, newFaces, newLightmaps, newNodes, newClipnodes, newTextures);
Expand Down Expand Up @@ -6744,15 +6744,19 @@ void Gui::drawImportMapWidget()
{
while (newTextures.size())
{
auto tex = newTextures[newTextures.size() - 1];
map->add_texture(tex.szName, tex.data, tex.nWidth, tex.nHeight);
auto& tex = newTextures[newTextures.size() - 1];
auto data = ConvertWadTexToRGB(tex);
map->add_texture(tex->szName, (unsigned char*)data, tex->nWidth, tex->nHeight);
delete tex;
delete[]data;
newTextures.pop_back();
}
}

map->update_lump_pointers();

if (newTexinfo.size())
{
map->append_lump(LUMP_TEXINFO, &newTexinfo[0], sizeof(BSPTEXTUREINFO) * newTexinfo.size());
for (auto& texinfo : newTexinfo)
{
if (texinfo.iMiptex < 0 || texinfo.iMiptex >= map->textureCount)
Expand All @@ -6765,13 +6769,13 @@ void Gui::drawImportMapWidget()
if (texOffset < 0)
continue;
BSPMIPTEX& tex = *((BSPMIPTEX*)(bspModel->textures + texOffset));
for (int i = 0; i < map->textureCount; i++)
for (int i = map->textureCount - 1; i >= 0; i--)
{
int tex2Offset = ((int*)map->textures)[i + 1];
if (tex2Offset >= 0)
{
BSPMIPTEX& tex2 = *((BSPMIPTEX*)(map->textures + tex2Offset));
if (strcasecmp(tex.szName, tex2.szName) == 0)
BSPMIPTEX * tex2 = ((BSPMIPTEX*)(map->textures + tex2Offset));
if (strcasecmp(tex.szName, tex2->szName) == 0)
{
newMiptex = i;
break;
Expand All @@ -6792,14 +6796,15 @@ void Gui::drawImportMapWidget()
if (texinfo.iMiptex == -1)
texinfo.iMiptex = 0;

delete[] imageData;
delete wadTex;
delete[] imageData;
break;
}
}
}
texinfo.iMiptex = newMiptex;
}
map->append_lump(LUMP_TEXINFO, &newTexinfo[0], sizeof(BSPTEXTUREINFO) * newTexinfo.size());
}

if (newVerts.size())
Expand Down Expand Up @@ -8347,6 +8352,7 @@ void Gui::drawFaceEditorWidget()
static char textureName[MAXTEXTURENAME];
static char textureName2[MAXTEXTURENAME];
static int lastPickCount = -1;
static int miptex = 0;
static bool validTexture = true;
static bool scaledX = false;
static bool scaledY = false;
Expand Down Expand Up @@ -8415,7 +8421,7 @@ void Gui::drawFaceEditorWidget()
textureName[0] = '\0';
}

int miptex = texinfo.iMiptex;
miptex = texinfo.iMiptex;

vec3 xv, yv;
bestplane = TextureAxisFromPlane(plane, xv, yv);
Expand Down Expand Up @@ -8684,7 +8690,6 @@ void Gui::drawFaceEditorWidget()
"\nLightmaps may break in strange ways if this is used on a normal face.");
ImGui::EndTooltip();
}

ImGui::Dummy(ImVec2(0, 8));

ImGui::Text(get_localized_string(LANG_0891).c_str());
Expand All @@ -8695,6 +8700,9 @@ void Gui::drawFaceEditorWidget()
}

ImGui::InputText(get_localized_string(LANG_0892).c_str(), textureName, MAXTEXTURENAME);
ImGui::SameLine();
ImGui::Text(fmt::format("#{}", miptex).c_str());

ImGui::SameLine();

if (ImGui::Button("APPLY"))
Expand Down

0 comments on commit 46edbc7

Please sign in to comment.