Skip to content

Commit

Permalink
Initial object definition support
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyuu17 committed Jun 30, 2024
1 parent 1b169c9 commit 8612b1f
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ target_sources(${PROJECT_NAME}
"SWBF2/Native/Chunks/LoclChunk.cpp"
"SWBF2/Native/Chunks/ModelChunk.cpp"
"SWBF2/Native/Chunks/ModelSegmentChunk.cpp"
"SWBF2/Native/Chunks/ObjectDefinitionChunk.cpp"
"SWBF2/Native/Chunks/SkyChunk.cpp"
"SWBF2/Native/Chunks/StreamReader.cpp"
"SWBF2/Native/Chunks/TextureChunk.cpp"
Expand Down
43 changes: 27 additions & 16 deletions src/SWBF2/Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,23 @@ namespace SWBF2

godot::MeshInstance3D *Level::LoadModel(const std::string &id)
{
if (!Native::SWBF2::m_models.contains(id))
if (!Native::SWBF2::m_models.contains(id) && !Native::SWBF2::m_objectDefs.contains(id))
{
return nullptr;
}

const auto &model = Native::SWBF2::m_models[id];
std::string newId = id;
if (Native::SWBF2::m_objectDefs.contains(id))
{
newId = Native::SWBF2::m_objectDefs[id].m_geometryName;
if (newId.empty())
return nullptr;
}

const auto &model = Native::SWBF2::m_models[newId];

godot::MeshInstance3D *meshInstance = memnew(godot::MeshInstance3D);
meshInstance->set_name(id.c_str());
meshInstance->set_name(newId.c_str());

godot::ArrayMesh *arrMesh = memnew(godot::ArrayMesh);
for (auto const &segment : model.m_segments)
Expand All @@ -58,25 +66,28 @@ namespace SWBF2
const auto &mainTextureName = segment.m_textureNames[Native::ModelSegment::TEXTURE_DEFAULT];
const auto &bumpTextureName = segment.m_textureNames[Native::ModelSegment::TEXTURE_NORMAL];

auto &material = m_materialPool.getItem(mainTextureName);
if (material.is_valid())
if (!mainTextureName.empty())
{
if (!bumpTextureName.empty() && Native::SWBF2::m_tex.contains(bumpTextureName))
auto &material = m_materialPool.getItem(mainTextureName);
if (material.is_valid())
{
auto &bumpTexture = Native::SWBF2::m_tex[bumpTextureName].m_formats[0].m_faceLevels[0].m_gdTexture;
if (!bumpTextureName.empty() && Native::SWBF2::m_tex.contains(bumpTextureName))
{
auto &bumpTexture = Native::SWBF2::m_tex[bumpTextureName].m_formats[0].m_faceLevels[0].m_gdTexture;

material->set_feature(godot::BaseMaterial3D::FEATURE_NORMAL_MAPPING, true);
material->set_texture(godot::BaseMaterial3D::TEXTURE_NORMAL, bumpTexture);
}
material->set_feature(godot::BaseMaterial3D::FEATURE_NORMAL_MAPPING, true);
material->set_texture(godot::BaseMaterial3D::TEXTURE_NORMAL, bumpTexture);
}

// TODO: fix leaves transparency?
if (mainTextureName == "leaves")
material->set_transparency(godot::BaseMaterial3D::TRANSPARENCY_ALPHA_SCISSOR);
// TODO: fix leaves transparency?
if (mainTextureName == "leaves")
material->set_transparency(godot::BaseMaterial3D::TRANSPARENCY_ALPHA_SCISSOR);

if (segment.m_material.m_flags & Native::Material::MATERIAL_TRANSPARENT)
material->set_transparency(godot::BaseMaterial3D::TRANSPARENCY_ALPHA);
if (segment.m_material.m_flags & Native::Material::MATERIAL_TRANSPARENT)
material->set_transparency(godot::BaseMaterial3D::TRANSPARENCY_ALPHA);

arrMesh->surface_set_material(surfaceId, material);
arrMesh->surface_set_material(surfaceId, material);
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/SWBF2/Native/Chunks/ChunkProcessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "LevelChunk.hpp"
#include "SkyChunk.hpp"
#include "LightChunk.hpp"
#include "ObjectDefinitionChunk.hpp"

namespace SWBF2::Native
{
Expand All @@ -29,7 +30,8 @@ namespace SWBF2::Native
{ "tex_"_m, TextureChunk::ProcessChunk },
{ "lvl_"_m, LevelChunk::ProcessChunk },
{ "sky_"_m, SkyChunk::ProcessChunk },
{ "lght"_m, LightChunk::ProcessChunk }
{ "lght"_m, LightChunk::ProcessChunk },
{ "entc"_m, ObjectDefinitionChunk::ProcessChunk }
};

static void ProcessChunk(StreamReader &streamReader, StreamReader &parentReader);
Expand Down
52 changes: 52 additions & 0 deletions src/SWBF2/Native/Chunks/ObjectDefinitionChunk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <godot_cpp/variant/utility_functions.hpp>

#include "Native/Chunks/ConfigReader.hpp"
#include "Native/Chunks/StreamReader.hpp"
#include "Native/Chunks/ObjectDefinitionChunk.hpp"
#include "Native/Objects/ObjectDefinition.hpp"
#include "Native/SWBF2.hpp"

namespace SWBF2::Native
{
void ObjectDefinitionChunk::ProcessChunk(StreamReader &streamReader)
{
ObjectDefinition objDef{};

auto baseReaderChild = streamReader.ReadChildWithHeader<"BASE"_m>();
{
*baseReaderChild >> objDef.m_classLabel;
}

std::string type;
auto typeReaderChild = streamReader.ReadChildWithHeader<"TYPE"_m>();
{
*typeReaderChild >> type;
}

std::optional<StreamReader> propReaderChild;
while ((propReaderChild = streamReader.ReadChildWithHeader<"PROP"_m>()).has_value())
{
FNVHash prop;
*propReaderChild >> prop;

std::string value;
*propReaderChild >> value;

objDef.m_properties.insert_or_assign(prop, value);
}

if (objDef.m_properties.contains("GeometryName"_fnv))
{
objDef.m_geometryName = objDef.m_properties["GeometryName"_fnv];
}
else // assumed geometry name by classLabel
{
if (objDef.m_classLabel == "grasspatch")
{
objDef.m_geometryName = "editor_grasspatch";
}
}

SWBF2::m_objectDefs.insert_or_assign(type, objDef);
}
}
12 changes: 12 additions & 0 deletions src/SWBF2/Native/Chunks/ObjectDefinitionChunk.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "StreamReader.hpp"

namespace SWBF2::Native
{
class ObjectDefinitionChunk {
public:
static void ProcessChunk(StreamReader &streamReader);
};

}
2 changes: 1 addition & 1 deletion src/SWBF2/Native/Chunks/WorldChunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "Native/Chunks/StreamReader.hpp"
#include "Native/Chunks/WorldChunk.hpp"
#include "Native/GameObject.hpp"
#include "Native/Objects/GameObject.hpp"
#include "Native/SWBF2.hpp"

namespace SWBF2::Native
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ namespace SWBF2::Native
godot::Basis m_rotationMatrix;
godot::Vector3 m_position;

std::unordered_map<uint32_t, std::string> m_properties;
std::unordered_map<FNVHash, std::string> m_properties;
};
}
13 changes: 13 additions & 0 deletions src/SWBF2/Native/Objects/ObjectDefinition.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

#pragma once

namespace SWBF2::Native
{
class ObjectDefinition {
public:
std::string m_classLabel;
std::string m_geometryName;

std::unordered_map<FNVHash, std::string> m_properties;
};
}
1 change: 1 addition & 0 deletions src/SWBF2/Native/SWBF2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace SWBF2::Native
std::unordered_map<std::string, Light> SWBF2::m_lights;
std::unordered_map<std::string, Model> SWBF2::m_models;
std::unordered_map<std::string, Texture> SWBF2::m_tex;
std::unordered_map<std::string, ObjectDefinition> SWBF2::m_objectDefs;
std::unordered_map<std::string, SWBF2::LoclEntriesMap> SWBF2::m_locl;

void SWBF2::Init()
Expand Down
2 changes: 2 additions & 0 deletions src/SWBF2/Native/SWBF2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "Native/Models/Model.hpp"
#include "Native/Texture/Texture.hpp"
#include "Native/Objects/ObjectDefinition.hpp"
#include "Native/Level.hpp"
#include "Native/SkyDome.hpp"
#include "Native/Light.hpp"
Expand Down Expand Up @@ -38,6 +39,7 @@ namespace SWBF2::Native
static std::unordered_map<std::string, Light> m_lights;
static std::unordered_map<std::string, Model> m_models;
static std::unordered_map<std::string, Texture> m_tex;
static std::unordered_map<std::string, ObjectDefinition> m_objectDefs;

using LoclEntriesMap = std::unordered_map<FNVHash, std::u16string>;
static std::unordered_map<std::string, LoclEntriesMap> m_locl;
Expand Down
2 changes: 1 addition & 1 deletion src/SWBF2/Native/World.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#pragma once

#include "GameObject.hpp"
#include "Native/Objects/GameObject.hpp"

namespace SWBF2::Native
{
Expand Down

0 comments on commit 8612b1f

Please sign in to comment.