Skip to content

Commit

Permalink
Add "generate nav mesh" button. And scale sprites!
Browse files Browse the repository at this point in the history
  • Loading branch information
UnrealKaraulov committed Nov 17, 2024
1 parent 8ee54f4 commit f0750a2
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/bsp/Bsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7002,7 +7002,7 @@ bool Bsp::recursiveHullCheck(int hull, int num, float p1f, float p2f, vec3 p1, v
{
trace->flFraction = midf;
trace->vecEndPos = mid;
print_log("backup past 0\n");
//print_log("backup past 0\n");
return false;
}

Expand Down
41 changes: 27 additions & 14 deletions src/editor/BspRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1498,15 +1498,17 @@ void BspRenderer::generateNavMeshBuffer() {

renderClip->faceMaths[hull] = std::move(navFaceMaths);

std::ofstream file(map->bsp_name + "_hull" + std::to_string(hull) + ".obj", std::ios::out | std::ios::trunc);
std::string navmesh_hull3_path = g_working_dir + map->bsp_name + "_hull" + std::to_string(hull) + ".obj";

std::ofstream file(navmesh_hull3_path, std::ios::out | std::ios::trunc);
for (int i = 0; i < allVerts.size(); i++) {
vec3 v = vec3(allVerts[i].pos.x, allVerts[i].pos.y, allVerts[i].pos.z);
file << "v " << std::fixed << std::setprecision(2) << v.x << " " << v.y << " " << v.z << std::endl;
}
for (int i = 0; i < allVerts.size(); i += 3) {
file << "f " << (i + 1) << " " << (i + 2) << " " << (i + 3) << std::endl;
}
print_log("Wrote {} verts\n", allVerts.size());
print_log("Wrote {} verts to {}\n", allVerts.size(), navmesh_hull3_path);
file.close();
}

Expand Down Expand Up @@ -1923,12 +1925,6 @@ void BspRenderer::generateClipnodeBuffer(int modelIdx)
{
generateClipnodeBufferForHull(modelIdx, i);
}

if (modelIdx == 0)
{
/*generateNavMeshBuffer();
generateLeafNavMeshBuffer();*/
}
}

void BspRenderer::updateClipnodeOpacity(unsigned char newValue)
Expand Down Expand Up @@ -2047,6 +2043,7 @@ void BspRenderer::refreshEnt(int entIdx)
int skin = -1;
int sequence = -1;
int body = -1;
float scale = 1.0f;

auto& rendEntity = renderEnts[entIdx];

Expand Down Expand Up @@ -2119,6 +2116,22 @@ void BspRenderer::refreshEnt(int entIdx)
}
}

if (ent->hasKey("scale") || g_app->fgd)
{
if (ent->hasKey("scale") && isFloating(ent->keyvalues["scale"]))
{
scale = str_to_float(ent->keyvalues["scale"]);
}
if (scale <= 0 && g_app->fgd)
{
FgdClass* fgdClass = g_app->fgd->getFgdClass(ent->classname);
if (fgdClass)
{
scale = fgdClass->scale;
}
}
}

if (ent->hasKey("sequence") || g_app->fgd)
{
if (ent->hasKey("sequence") && isNumeric(ent->keyvalues["sequence"]))
Expand Down Expand Up @@ -2214,13 +2227,13 @@ void BspRenderer::refreshEnt(int entIdx)
{
if (FindPathInAssets(map, modelpath, newModelPath))
{
if (rendEntity.pointEntCube)
if (rendEntity.pointEntCube && std::fabs(scale - 1.0f) < EPSILON)
{
rendEntity.spr = AddNewSpriteToRender(newModelPath, rendEntity.pointEntCube->mins, rendEntity.pointEntCube->maxs, 1.0f);
}
else
{
rendEntity.spr = AddNewSpriteToRender(newModelPath);
rendEntity.spr = AddNewSpriteToRender(newModelPath, scale);
}
}
else
Expand Down Expand Up @@ -2265,13 +2278,13 @@ void BspRenderer::refreshEnt(int entIdx)
{
if (FindPathInAssets(map, fgdClass->sprite, newModelPath))
{
if (rendEntity.pointEntCube)
if (rendEntity.pointEntCube && std::fabs(scale - 1.0f) < EPSILON)
{
rendEntity.spr = AddNewSpriteToRender(newModelPath, rendEntity.pointEntCube->mins, rendEntity.pointEntCube->maxs, 1.0f);
}
else
{
rendEntity.spr = AddNewSpriteToRender(newModelPath);
rendEntity.spr = AddNewSpriteToRender(newModelPath, scale);
}
}
else
Expand Down Expand Up @@ -2313,13 +2326,13 @@ void BspRenderer::refreshEnt(int entIdx)
{
if (FindPathInAssets(map, fgdClass->model, newModelPath))
{
if (rendEntity.pointEntCube)
if (rendEntity.pointEntCube && std::fabs(scale - 1.0f) < EPSILON)
{
rendEntity.spr = AddNewSpriteToRender(newModelPath, rendEntity.pointEntCube->mins, rendEntity.pointEntCube->maxs, 1.0f);
}
else
{
rendEntity.spr = AddNewSpriteToRender(newModelPath);
rendEntity.spr = AddNewSpriteToRender(newModelPath, scale);
}
}
else
Expand Down
18 changes: 15 additions & 3 deletions src/editor/Fgd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ void Fgd::processClassInheritance()
{
if (classes[i]->modelSequence <= 0)
{
if (isNumeric(classes[i]->keyvalues[c].defaultValue))
if (classes[i]->keyvalues[c].iType == FGD_KEY_TYPES::FGD_KEY_INTEGER)
{
classes[i]->modelSequence = str_to_int(classes[i]->keyvalues[c].defaultValue);
}
Expand All @@ -739,7 +739,7 @@ void Fgd::processClassInheritance()
{
if (classes[i]->modelBody <= 0)
{
if (isNumeric(classes[i]->keyvalues[c].defaultValue))
if (classes[i]->keyvalues[c].iType == FGD_KEY_TYPES::FGD_KEY_INTEGER)
{
classes[i]->modelBody = str_to_int(classes[i]->keyvalues[c].defaultValue);
}
Expand All @@ -749,12 +749,24 @@ void Fgd::processClassInheritance()
{
if (classes[i]->modelSkin <= 0)
{
if (isNumeric(classes[i]->keyvalues[c].defaultValue))
if (classes[i]->keyvalues[c].iType == FGD_KEY_TYPES::FGD_KEY_INTEGER)
{
classes[i]->modelSkin = str_to_int(classes[i]->keyvalues[c].defaultValue);
}
}
}
else if (classes[i]->keyvalues[c].name == "scale" ||
classes[i]->keyvalues[c].name == "sprite_scale")
{
if (classes[i]->keyvalues[c].iType == FGD_KEY_TYPES::FGD_KEY_INTEGER)
{
classes[i]->scale = str_to_int(classes[i]->keyvalues[c].defaultValue);
}
else if (classes[i]->keyvalues[c].iType == FGD_KEY_TYPES::FGD_KEY_STRING)
{
classes[i]->scale = str_to_float(classes[i]->keyvalues[c].defaultValue);
}
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/editor/Fgd.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct FgdClass
COLOR3 color;
vec3 offset;
hashmap otherTypes; // unrecognized types
float scale;

// if false, then need to get props from the base class
bool colorSet;
Expand All @@ -93,6 +94,7 @@ struct FgdClass
color = { 220, 0, 220 };
offset = vec3();
modelSkin = modelBody = modelSequence = 0;
scale = 1.0f;
}

// get parent classes from youngest to oldest, in right-to-left order
Expand Down
9 changes: 9 additions & 0 deletions src/editor/Gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4524,8 +4524,17 @@ void Gui::drawMenuBar()
rend->pushUndoCommand(createCommand);
}
IMGUI_TOOLTIP(g, "Create a point entity for use with the culling tool. 2 of these define the bounding box for structure culling operations.\n");

ImGui::EndMenu();
}

if (ImGui::MenuItem("Generate nav mesh", NULL, false, (!rend->debugNavMesh || !g_app->debugLeafNavMesh)))
{
rend->generateNavMeshBuffer();
rend->generateLeafNavMeshBuffer();
}
IMGUI_TOOLTIP(g, "I don't know for what it needs :) From original bspguy repository + crash fixes. \n");

if (ImGui::BeginMenu("MAP TRANSFORMATION [WIP]", map))
{
if (ImGui::MenuItem("Mirror map x/y", NULL, false, map))
Expand Down
13 changes: 6 additions & 7 deletions src/editor/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -922,8 +922,12 @@ void Renderer::renderLoop()
}
}

if (debugTraceStart != vec3())
{
glLineWidth(currentLineWidth);
}

if (debugLeafNavMesh) {
glLineWidth(1);
glDisable(GL_DEPTH_TEST);

Bsp* map = mapRenderers[0]->map;
Expand All @@ -937,7 +941,6 @@ void Renderer::renderLoop()
if (leafNavIdx >= 0 && leafNavIdx < debugLeafNavMesh->nodes.size()) {

if (pickInfo.selectedEnts.size()) {
glDisable(GL_DEPTH_TEST);

int endNode = debugLeafNavMesh->getNodeIdx(map, map->ents[pickInfo.selectedEnts[0]]);
//vector<int> route = debugLeafNavMesh->AStarRoute(leafNavIdx, endNode);
Expand Down Expand Up @@ -1069,12 +1072,8 @@ void Renderer::renderLoop()
colorShader->popMatrix(MAT_PROJECTION);
colorShader->popMatrix(MAT_VIEW);
*/
glEnable(GL_DEPTH_TEST);
}
if (debugTraceStart != vec3())
{
glLineWidth(currentLineWidth);
}

glEnable(GL_CULL_FACE);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// Solve:
// Create empty hull 0 box ?

std::string g_version_string = "NewBSPGuy v4.29";
std::string g_version_string = "NewBSPGuy v4.30";

bool g_verbose = false;

Expand Down
23 changes: 22 additions & 1 deletion src/res/Sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,19 @@ Sprite::Sprite(const std::string& filename, const vec3& mins , const vec3& maxs

tmpSpriteImage.spriteCube = new EntCube();

if (!useOwnSettigns)
if (!useOwnSettigns || (mins == maxs && mins == vec3()))
{
tmpSpriteImage.spriteCube->mins = { -5.0f, 0.0f, 0.0f };
tmpSpriteImage.spriteCube->maxs = { 5.0f, tmpSpriteImage.frameinfo.width * 1.0f, tmpSpriteImage.frameinfo.height * 1.0f };

tmpSpriteImage.spriteCube->mins += vec3(0.0f, tmpSpriteImage.frameinfo.origin[0] * 1.0f, tmpSpriteImage.frameinfo.origin[1] * -1.0f);
tmpSpriteImage.spriteCube->maxs += vec3(0.0f, tmpSpriteImage.frameinfo.origin[0] * 1.0f, tmpSpriteImage.frameinfo.origin[1] * -1.0f);

if (useOwnSettigns)
{
tmpSpriteImage.spriteCube->mins *= scale;
tmpSpriteImage.spriteCube->maxs *= scale;
}
}
else
{
Expand Down Expand Up @@ -269,6 +275,21 @@ Sprite* AddNewSpriteToRender(const std::string& path, unsigned int sum)
}
}

Sprite* AddNewSpriteToRender(const std::string& path, float scale)
{
unsigned int crc32 = GetCrc32InMemory((unsigned char*)path.data(), (unsigned int)path.size(), *(int*)&scale);

if (spr_models.find(crc32) != spr_models.end())
{
return spr_models[crc32];
}
else
{
Sprite* newModel = new Sprite(path, vec3(),vec3(), scale, true);
spr_models[crc32] = newModel;
return newModel;
}
}

Sprite* AddNewSpriteToRender(const std::string& path, vec3 mins, vec3 maxs, float scale)
{
Expand Down
1 change: 1 addition & 0 deletions src/res/Sprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ void TestSprite();

extern std::map<unsigned int, Sprite*> spr_models;
Sprite* AddNewSpriteToRender(const std::string & path, unsigned int sum = 0);
Sprite* AddNewSpriteToRender(const std::string& path, float scale);
Sprite* AddNewSpriteToRender(const std::string& path, vec3 mins, vec3 maxs, float scale);
17 changes: 17 additions & 0 deletions src/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,23 @@ bool isNumeric(const std::string& s)
return it == s.end();
}

bool isFloating(const std::string& s)
{
if (s.empty())
return false;
std::string::const_iterator it = s.begin();
int points = 0;

while (it != s.end() && (isdigit(*it) || *it == '.'))
{
++it;
if (*it == '.')
points++;
}

return it == s.end() && points <= 1;
}

std::string toLowerCase(const std::string& s)
{
std::string ret = s;
Expand Down
2 changes: 2 additions & 0 deletions src/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ std::string stripExt(const std::string& filename);
std::string stripFileName(const std::string& path);
std::wstring stripFileName(const std::wstring& path);

bool isFloating(const std::string& s);

bool isNumeric(const std::string& s);

bool dirExists(const std::string& dirName);
Expand Down

0 comments on commit f0750a2

Please sign in to comment.