Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #219 from storm-devs/feature/msg-model-substitute-…
Browse files Browse the repository at this point in the history
…geometry-node

model, ship: add MSG_MODEL_SUBSTITUTE_GEOMETRY_NODE message
  • Loading branch information
espkk authored Sep 14, 2021
2 parents 410e4d0 + 70cec78 commit 645e18d
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 66 deletions.
3 changes: 2 additions & 1 deletion src/libs/Common/include/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "Matrix.h"
#include "geos.h"
#include "object.h"
#include "vmodule_api.h"

class NODER;

Expand Down Expand Up @@ -53,6 +52,8 @@ class NODE
const CMatrix &globm, NODER *par, const char *lmPath) = 0;

virtual float Trace(const CVECTOR &src, const CVECTOR &dst) = 0;

virtual void SubstituteGeometry(const std::string& new_model) = 0;
};

class VDX9RENDER;
Expand Down
13 changes: 13 additions & 0 deletions src/libs/model/src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,19 @@ uint64_t MODELR::ProcessMessage(MESSAGE &message)
if (root)
root->SetMaxViewDist(message.Float());
break;
case MSG_MODEL_SUBSTITUTE_GEOMETRY_NODE: {
auto &&geometry_node = message.String();
auto &&new_model_name = message.String();

if (auto *node = FindNode(geometry_node.c_str()))
{
node->SubstituteGeometry(new_model_name);
}
else
{
spdlog::trace("MODELR: Cannot substitute geometry node {}", geometry_node);
}
}
}
return 1;
}
Expand Down
17 changes: 12 additions & 5 deletions src/libs/model/src/modelr.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#pragma once

#include <string>

#include "dx9render.h"
#include "geometry.h"
#include "model.h"

class NODER : public NODE
{
char *sys_modelName;
char *sys_LightPath;
char *sys_TexPath;
char *sys_lmPath;
bool isReleaed;
std::string sys_modelName_base;
std::string sys_modelName_full;
std::string sys_LightPath;
std::string sys_TexPath;
std::string sys_lmPath;
bool isReleased;

static long depth, node;
uintptr_t idGeoGroup; // id of "geometry" string
Expand Down Expand Up @@ -54,11 +57,15 @@ class NODER : public NODE

void SetTechnique(const char *name) override;
const char *GetTechnique() override;

// replace only this node model without touching anything else
void SubstituteGeometry(const std::string &new_model) override;

void ReleaseGeometry();
void RestoreGeometry();

void SetMaxViewDist(float fDist);

};

#define MODEL_ANI_MAXBUFFERS 16
Expand Down
86 changes: 34 additions & 52 deletions src/libs/model/src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

VGEOMETRY *NODER::gs = nullptr;
VDX9RENDER *NODER::rs = nullptr;
static long nlab = 0;
long NODER::depth = -1;
long NODER::node;
extern long clip_nps;
Expand All @@ -15,7 +14,6 @@ extern const CVECTOR *clip_c;
extern float clip_r;
extern GEOS::ADD_POLYGON_FUNC clip_geosap;
extern ADD_POLYGON_FUNC clip_ap;
char nm[256];
GEOS::PLANE clip_gp[256];
NODE *bestTraceNode = nullptr;
GEOS *clipGeo;
Expand Down Expand Up @@ -43,7 +41,7 @@ bool AddPolygon(const GEOS::VERTEX *vr, long nv)
//-------------------------------------------------------------------
bool NODER::Clip()
{
if (isReleaed)
if (isReleased)
return false;

// check for bounding spheres intersection
Expand Down Expand Up @@ -122,7 +120,7 @@ float NODER::Update(CMatrix &mtx, CVECTOR &cnt)
//----------------------------------------------------------
float NODER::Trace(const CVECTOR &src, const CVECTOR &dst)
{
if (isReleaed)
if (isReleased)
return 2.0f;
// check for bounding spheres intersection
const auto lmn = dst - src;
Expand Down Expand Up @@ -174,17 +172,14 @@ NODER::NODER()
nnext = 0;
next = nullptr;
parent = nullptr;
sys_modelName = nullptr;
sys_TexPath = nullptr;
sys_LightPath = nullptr;
sys_lmPath = nullptr;

max_view_dist = 0.f;
}

bool NODER::Init(const char *lightPath, const char *pname, const char *oname, const CMatrix &m, const CMatrix &globm,
NODER *par, const char *lmPath)
{
isReleased = false;
name[0] = 0;
technique[0] = 0;
geoMaterialFunc = nullptr;
Expand All @@ -193,44 +188,28 @@ bool NODER::Init(const char *lightPath, const char *pname, const char *oname, co
loc_mtx = m;
glob_mtx.EqMultiply(loc_mtx, globm);

nlab++;
if (oname[0] == 0)
sprintf_s(nm, "%s", pname);
else
sprintf_s(nm, "%s_%s", pname, oname);

char lp[256];
// sprintf_s(lp, "%s\\%s", lightPath, nm);
sprintf_s(lp, "%s", lightPath);

auto len = strlen(nm) + 1;
sys_modelName = new char[len];
memcpy(sys_modelName, nm, len);

len = strlen(lp) + 1;
sys_LightPath = new char[len];
memcpy(sys_LightPath, lp, len);
if (pname == nullptr)
{
throw std::runtime_error(fmt::format("NODER::Init: got nullptr model name"));
}
sys_modelName_base = pname;

len = strlen(lmPath) + 1;
sys_lmPath = new char[len];
memcpy(sys_lmPath, lmPath, len);
if (oname && oname[0])
sys_modelName_full = fmt::format("{}_{}", sys_modelName_base, oname);
else
sys_modelName_full = sys_modelName_base;

if (lightPath)
sys_LightPath = lightPath;

const auto *const tPath = gs->GetTexturePath();
len = strlen(tPath) + 1;
sys_TexPath = new char[len];
memcpy(sys_TexPath, tPath, len);
if (lmPath)
sys_lmPath = lmPath;

isReleaed = false;
sys_TexPath = gs->GetTexturePath();

geo = gs->CreateGeometry(nm, lp, 0, lmPath);
geo = gs->CreateGeometry(sys_modelName_full.c_str(), sys_LightPath.c_str(), 0, lmPath);
if (!geo)
{
delete sys_modelName;
sys_modelName = nullptr;
delete sys_LightPath;
sys_LightPath = nullptr;
delete sys_lmPath;
sys_lmPath = nullptr;
return false;
}

Expand Down Expand Up @@ -305,19 +284,15 @@ NODER::~NODER()
delete next[l];
if (nnext > 0)
free(next);
delete sys_modelName;
delete sys_LightPath;
delete sys_lmPath;
delete sys_TexPath;
}

void NODER::ReleaseGeometry()
{
if (isReleaed)
if (isReleased)
return;
delete geo;
geo = nullptr;
isReleaed = true;
isReleased = true;
for (long i = 0; i < nnext; i++)
{
if (!next[i])
Expand All @@ -328,21 +303,21 @@ void NODER::ReleaseGeometry()

void NODER::RestoreGeometry()
{
if (!isReleaed)
if (!isReleased)
return;

const auto *const tPath = gs->GetTexturePath();
const auto len = strlen(tPath) + 1;
auto *const ttPath = new char[len];
memcpy(ttPath, tPath, len);
gs->SetTexturePath(sys_TexPath);
geo = gs->CreateGeometry(sys_modelName, sys_LightPath, 0, sys_lmPath);
gs->SetTexturePath(sys_TexPath.c_str());
geo = gs->CreateGeometry(sys_modelName_full.c_str(), sys_LightPath.c_str(), 0, sys_lmPath.c_str());
gs->SetTexturePath(ttPath);
delete[] ttPath;
if (!geo)
throw std::runtime_error("No geometry");
throw std::runtime_error(fmt::format("Cannot restore geometry {}", sys_modelName_full));

isReleaed = false;
isReleased = false;
for (long i = 0; i < nnext; i++)
{
if (!next[i])
Expand All @@ -359,7 +334,7 @@ GEOS::PLANE TViewPlane[4];

void NODER::Draw()
{
if (isReleaed)
if (isReleased)
return;

const auto cnt = glob_mtx * center;
Expand Down Expand Up @@ -617,3 +592,10 @@ void NODER::SetMaxViewDist(float fDist)
if (next[n])
static_cast<NODER *>(next[n])->SetMaxViewDist(fDist);
}

void NODER::SubstituteGeometry(const std::string &new_model)
{
sys_modelName_full = fmt::format("{}_{}", sys_modelName_base, new_model);
ReleaseGeometry();
RestoreGeometry();
}
9 changes: 2 additions & 7 deletions src/libs/rigging/src/Flag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,22 @@ uint64_t FLAG::ProcessMessage(MESSAGE &message)
const auto eidModel = message.EntityID();
const auto nNation = message.Long();

MODEL *host_mdl;
host_mdl = static_cast<MODEL *>(EntityManager::GetEntityPointer(eidModel));
MODEL *host_mdl = static_cast<MODEL *>(EntityManager::GetEntityPointer(eidModel));
if (host_mdl == nullptr)
{
core.Trace("Missing INIT message to FLAG: bad MODEL");
return 0;
}

if (groupQuantity == 0)
{
gdata = new GROUPDATA[1];
if (gdata == nullptr)
throw std::runtime_error("Not memory allocation");

groupQuantity = 1;
}
else
{
auto *const oldgdata = gdata;
gdata = new GROUPDATA[groupQuantity + 1];
if (gdata == nullptr)
throw std::runtime_error("Not memory allocation");
memcpy(gdata, oldgdata, sizeof(GROUPDATA) * groupQuantity);
delete oldgdata;
groupQuantity++;
Expand Down
2 changes: 2 additions & 0 deletions src/libs/shared_headers/include/shared/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#define MSG_MODEL_SET_FOG 20511
#define MSG_MODEL_SET_MAX_VIEW_DIST 20512

#define MSG_MODEL_SUBSTITUTE_GEOMETRY_NODE 20600 // "ss", geometry, new model name

//============================================================================================
// blade messages
//============================================================================================
Expand Down
9 changes: 8 additions & 1 deletion src/libs/ship/src/ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1301,8 +1301,15 @@ uint64_t SHIP::ProcessMessage(MESSAGE &message)
core.Send_Message(GetModelEID(), "ls", MSG_MODEL_SET_TECHNIQUE, sTech.c_str());
// MODEL * pModel = GetModel();
// NODE* pNode = pModel->GetNode(0);
break;
}
case MSG_MODEL_SUBSTITUTE_GEOMETRY_NODE: {
auto &&geometry_node = message.String();
auto &&new_model_name = message.String();
core.Send_Message(GetModelEID(), "lss", MSG_MODEL_SUBSTITUTE_GEOMETRY_NODE, geometry_node.c_str(),
new_model_name.c_str());
break;
}
break;
}
return 0;
}
Expand Down

0 comments on commit 645e18d

Please sign in to comment.