Skip to content

Commit

Permalink
XashNT .csm VIEWER [not textures]
Browse files Browse the repository at this point in the history
  • Loading branch information
UnrealKaraulov committed Mar 25, 2024
1 parent 72b6784 commit 2e2e06b
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 18 deletions.
1 change: 1 addition & 0 deletions resources/languages/language.ini
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ LANG_0524 = Map (.bsp)
LANG_0525 = Open map in new Window
LANG_0526 = Model (.mdl)
OPEN_SPR_VIEW = Sprite (.spr)
OPEN_XASHNT_CSM_VIEW = XashNT (.csm)
LANG_0527 = Open model in new Window
LANG_0528 = Wad
LANG_0529 = Add wad file path to current map
Expand Down
1 change: 1 addition & 0 deletions resources/languages/language_ru.ini
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ LANG_0524 = Карту (BSP)
LANG_0525 = Открыть карту в новом окне.
LANG_0526 = Модель (MDL)
OPEN_SPR_VIEW = Спрайт (SPR)
OPEN_XASHNT_CSM_VIEW = XashNT (.csm)
LANG_0527 = Открыть модель в новом окне.
LANG_0528 = Файл текстур (WAD)
LANG_0529 = Указать путь к WAD-файлу для текущей карты.
Expand Down
14 changes: 14 additions & 0 deletions src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,10 @@ Bsp::Bsp()
is_protected = false;
is_bsp_model = false;
is_mdl_model = false;

map_mdl = NULL;
map_spr = NULL;
map_csm = NULL;

undo_lightmaps.clear();

Expand Down Expand Up @@ -150,8 +152,10 @@ Bsp::Bsp(std::string fpath)
is_protected = false;
is_bsp_model = false;
is_mdl_model = false;

map_mdl = NULL;
map_spr = NULL;
map_csm = NULL;

undo_lightmaps.clear();

Expand Down Expand Up @@ -206,6 +210,16 @@ Bsp::Bsp(std::string fpath)
init_empty_bsp();
return;
}
if (lowerpath.ends_with(".csm"))
{
is_mdl_model = true;
if (fileExists(fpath))
{
map_csm = AddNewXashCsmToRender(fpath);
}
init_empty_bsp();
return;
}
}
if (!fileExists(fpath))
{
Expand Down
2 changes: 2 additions & 0 deletions src/bsp/Bsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "bsptypes.h"
#include "mdl_studio.h"
#include "Sprite.h"
#include "XASH_csm.h"

class BspRenderer;

Expand Down Expand Up @@ -111,6 +112,7 @@ class Bsp

StudioModel* map_mdl;
Sprite* map_spr;
CSMFile* map_csm;

Bsp* parentMap;
void selectModelEnt();
Expand Down
21 changes: 20 additions & 1 deletion src/editor/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,13 @@ void Gui::drawMenuBar()
app->addMap(tmpMap);
app->selectMap(tmpMap);
}
else if (pathlowercase.ends_with(".csm"))
{
Bsp* tmpMap = new Bsp(res.string());
tmpMap->is_mdl_model = true;
app->addMap(tmpMap);
app->selectMap(tmpMap);
}
else
{
Bsp* tmpMap = new Bsp(res.string());
Expand Down Expand Up @@ -2690,7 +2697,6 @@ void Gui::drawMenuBar()
ImGui::EndTooltip();
}


if (ImGui::MenuItem(get_localized_string("OPEN_SPR_VIEW").c_str()))
{
filterNeeded = true;
Expand All @@ -2704,6 +2710,19 @@ void Gui::drawMenuBar()
ImGui::EndTooltip();
}

if (ImGui::MenuItem(get_localized_string("OPEN_XASHNT_CSM_VIEW").c_str()))
{
filterNeeded = true;
ifd::FileDialog::Instance().Open("MapOpenDialog", "Select XashNT CSM path", "XashNT CSM model (*.csm){.csm}", false, g_settings.lastdir);
}

if (ImGui::IsItemHovered() && g.HoveredIdTimer > g_tooltip_delay)
{
ImGui::BeginTooltip();
ImGui::TextUnformatted(get_localized_string(LANG_0527).c_str());
ImGui::EndTooltip();
}

if (ImGui::MenuItem(get_localized_string(LANG_0528).c_str()))
{
filterNeeded = true;
Expand Down
14 changes: 14 additions & 0 deletions src/editor/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ void drop_callback(GLFWwindow* window, int count, const char** paths)
print_log(get_localized_string(LANG_0897), tmpPath.string());
g_app->addMap(new Bsp(tmpPath.string()));
}
else if (lowerPath.ends_with(".csm"))
{
print_log(get_localized_string(LANG_0897), tmpPath.string());
g_app->addMap(new Bsp(tmpPath.string()));
}
else
{
print_log(get_localized_string(LANG_0898), tmpPath.string());
Expand Down Expand Up @@ -662,6 +667,15 @@ void Renderer::renderLoop()
continue;
}

if (SelectedMap->is_mdl_model && SelectedMap->map_csm)
{
matmodel.loadIdentity();
modelShader->bind();
modelShader->updateMatrixes();
SelectedMap->map_csm->draw();
continue;
}

std::set<int> modelidskip;

if (curMap->ents.size() && !isLoading)
Expand Down
2 changes: 1 addition & 1 deletion src/mdl/mdl_studio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,7 @@ int StudioModel::SetSkin(int iValue)
return iValue;
}

std::map<int, StudioModel*> mdl_models;
std::map<unsigned int, StudioModel*> mdl_models;

StudioModel* AddNewModelToRender(const std::string& path, unsigned int sum)
{
Expand Down
16 changes: 8 additions & 8 deletions src/mdl/mdl_studio.h
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,9 @@ class StudioModel
{
delete tex;
}

mdl_textures.clear();

for (int i = 0; i < 32; i++)
{
if (m_panimhdr[i])
Expand All @@ -496,18 +499,15 @@ class StudioModel
}
for (auto& body : mdl_mesh_groups)
{
//for (auto& subbody : body)
for (auto& submesh : body)
{
//for (auto& submesh : subbody)
for (auto& submesh : body)
if (submesh.buffer)
{
if (submesh.buffer)
{
delete submesh.buffer;
}
delete submesh.buffer;
}
}
}
mdl_mesh_groups.clear();
}

void DrawMDL(int mesh = -1);
Expand Down Expand Up @@ -561,5 +561,5 @@ class StudioModel
//float colorData[MAX_VERTS_PER_CALL * 4];
};

extern std::map<int, StudioModel *> mdl_models;
extern std::map<unsigned int, StudioModel *> mdl_models;
StudioModel* AddNewModelToRender(const std::string& path, unsigned int sum = 0);
2 changes: 1 addition & 1 deletion src/res/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ Sprite::Sprite(const std::string& filename)
}


std::map<int, Sprite*> spr_models;
std::map<unsigned int, Sprite*> spr_models;


Sprite* AddNewSpriteToRender(const std::string& path, unsigned int sum)
Expand Down
2 changes: 1 addition & 1 deletion src/res/Sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ class Sprite {

void TestSprite();

extern std::map<int, Sprite*> spr_models;
extern std::map<unsigned int, Sprite*> spr_models;
Sprite* AddNewSpriteToRender(const std::string & path, unsigned int sum = 0);
120 changes: 118 additions & 2 deletions src/res/XASH_csm.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "XASH_csm.h"
#include "util.h"
#include "log.h"
#include "forcecrc32.h"
#include "Settings.h"
#include "Renderer.h"

void CSMFile::parseMaterialsFromString(const std::string& materialsStr)
{
Expand All @@ -27,11 +30,29 @@ std::string CSMFile::getStringFromMaterials()
CSMFile::CSMFile()
{
readed = false;
for (auto& m : model)
delete m;
model.clear();
}

CSMFile::CSMFile(std::string path)
{
readed = read(path);
for (auto& m : model)
delete m;
model.clear();
}

CSMFile::~CSMFile()
{
for (auto& tex : mat_textures)
{
delete tex;
}
mat_textures.clear();
for (auto& m : model)
delete m;
model.clear();
}

bool CSMFile::validate()
Expand Down Expand Up @@ -170,13 +191,13 @@ bool CSMFile::write(const std::string& filePath) {
header.version = IDCSM_VERSION;

std::string matstr = getStringFromMaterials();

header.mat_size = (unsigned int)matstr.size() + 1;
header.vertex_size = sizeof(csm_vertex);
header.face_size = sizeof(csm_face);

header.mat_ofs = sizeof(csm_header);
header.vertex_ofs = header.mat_ofs + header.mat_size ;
header.vertex_ofs = header.mat_ofs + header.mat_size;
header.faces_ofs = header.vertex_ofs + (header.vertex_size * header.vertex_count);

file.write(reinterpret_cast<char*>(&header), sizeof(header));
Expand All @@ -192,3 +213,98 @@ bool CSMFile::write(const std::string& filePath) {
file.close();
return true;
}

void CSMFile::upload()
{
for (auto& m : model)
delete m;
model.clear();

if (readed)
{
for (auto& f : faces)
{
bool added = false;

for (auto& m : model)
{
if (m->matid == f.matIdx)
{
added = true;
for (int i = 0; i < 3; i++)
{
modelVert tmpVert;
tmpVert.pos = vertices[f.vertIdx[i]].point.flip();
tmpVert.u = f.uvs->uv[i].x;
tmpVert.v = f.uvs->uv[i].y;
m->verts.push_back(tmpVert);
}
break;
}
}

if (!added)
{
CSM_MDL_MESH* tmpModel = new CSM_MDL_MESH();
tmpModel->matid = f.matIdx;
tmpModel->buffer = new VertexBuffer(g_app->modelShader, NULL, 0, GL_TRIANGLES);

for (int i = 0; i < 3; i++)
{
modelVert tmpVert;
tmpVert.pos = vertices[f.vertIdx[i]].point.flip();
tmpVert.u = f.uvs->uv[i].x;
tmpVert.v = f.uvs->uv[i].y;
tmpModel->verts.push_back(tmpVert);
}

model.push_back(tmpModel);
}
}
}

if (model.size())
{
for (auto& m : model)
{
m->buffer->setData(&m->verts[0], m->verts.size());
m->buffer->uploaded = false;
}
}
}

void CSMFile::draw()
{
if (!model.size())
{
upload();
}

if (model.size())
{
missingTex->bind(0);
for (auto& m : model)
{
if (m && m->buffer)
m->buffer->drawFull();
}
}
}


std::map<unsigned int, CSMFile*> csm_models;
CSMFile* AddNewXashCsmToRender(const std::string& path, unsigned int sum)
{
unsigned int crc32 = GetCrc32InMemory((unsigned char*)path.data(), (unsigned int)path.size(), sum);

if (csm_models.find(crc32) != csm_models.end())
{
return csm_models[crc32];
}
else
{
CSMFile* newModel = new CSMFile(path);
csm_models[crc32] = newModel;
return newModel;
}
}
Loading

0 comments on commit 2e2e06b

Please sign in to comment.