generated from asmaloney/GDExtensionTemplate
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
415 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,8 @@ namespace SWBF2::Native | |
|
||
value = std::string_view(str, len); | ||
|
||
m_head += len + 1; | ||
|
||
return *this; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
#include <godot_cpp/variant/utility_functions.hpp> | ||
|
||
#include "Native/Chunks/StreamReader.hpp" | ||
#include "Native/Chunks/TerrainChunk.hpp" | ||
#include "Native/Terrain.hpp" | ||
#include "Native/SWBF2.hpp" | ||
|
||
namespace SWBF2::Native | ||
{ | ||
enum class VBufType : uint32_t { | ||
GEOMETRY = 290, | ||
TEXTURE = 20770, | ||
TEXTURE_EXTRA = 130 | ||
}; | ||
|
||
void TerrainChunk::ProcessChunk(StreamReader &streamReader) | ||
{ | ||
Terrain tern{}; | ||
|
||
auto nameReaderChild = streamReader.ReadChildWithHeader<"NAME"_m>(); | ||
{ | ||
*nameReaderChild >> tern.m_name; | ||
} | ||
|
||
if (streamReader.IsEof()) | ||
return; // No terrain | ||
|
||
auto infoReaderChild = streamReader.ReadChildWithHeader<"INFO"_m>(); | ||
{ | ||
*infoReaderChild >> tern.m_gridUnitSize; | ||
*infoReaderChild >> tern.m_heightScale; | ||
*infoReaderChild >> tern.m_heightFloor; | ||
*infoReaderChild >> tern.m_heightCeiling; | ||
*infoReaderChild >> tern.m_gridSize; | ||
*infoReaderChild >> tern.m_heightPatches; | ||
*infoReaderChild >> tern.m_texturePatches; | ||
*infoReaderChild >> tern.m_textureCount; | ||
*infoReaderChild >> tern.m_maxTextureLayers; | ||
*infoReaderChild >> tern.m_unknown; | ||
} | ||
|
||
auto ltexChild = streamReader.ReadChildWithHeader<"LTEX"_m>(); | ||
{ | ||
for (int i = 0; i < tern.m_textureCount; i++) | ||
{ | ||
std::string texName; | ||
*ltexChild >> texName; | ||
|
||
tern.m_textures.push_back(texName); | ||
} | ||
} | ||
|
||
auto dtexReaderChild = streamReader.ReadChildWithHeader<"DTEX"_m>(); | ||
{ | ||
// unused? | ||
dtexReaderChild->SkipBytes((uint32_t)dtexReaderChild->RemainingBytes()); | ||
} | ||
|
||
auto dtlxReaderChild = streamReader.ReadChildWithHeader<"DTLX"_m>(); | ||
{ | ||
*dtlxReaderChild >> tern.m_detailTexture; | ||
} | ||
|
||
auto scalChild = streamReader.ReadChildWithHeader<"SCAL"_m>(); | ||
{ | ||
for (int i = 0; i < tern.m_textureCount; i++) | ||
{ | ||
float scal; | ||
*scalChild >> scal; | ||
|
||
tern.m_textureScales.push_back(scal); | ||
} | ||
} | ||
|
||
auto axisChild = streamReader.ReadChildWithHeader<"AXIS"_m>(); | ||
{ | ||
for (int i = 0; i < tern.m_textureCount; i++) | ||
{ | ||
uint8_t axis; | ||
*axisChild >> axis; | ||
|
||
tern.m_textureAxis.push_back(axis); | ||
} | ||
} | ||
|
||
auto rotnChild = streamReader.ReadChildWithHeader<"ROTN"_m>(); | ||
{ | ||
for (int i = 0; i < tern.m_textureCount; i++) | ||
{ | ||
float rotn; | ||
*rotnChild >> rotn; | ||
|
||
tern.m_textureRotn.push_back(rotn); | ||
} | ||
} | ||
|
||
std::optional<StreamReader> readerChild; | ||
while ((readerChild = streamReader.ReadChild()).has_value()) | ||
{ | ||
switch (readerChild->GetHeader().m_Magic) | ||
{ | ||
case "PCHS"_m: | ||
{ | ||
StreamReader r{ *readerChild }; | ||
ReadPatches(r, tern); | ||
break; | ||
} | ||
case "WATR"_m: | ||
// todo | ||
break; | ||
} | ||
} | ||
|
||
SWBF2::m_tern = tern; | ||
} | ||
|
||
void TerrainChunk::ReadPatches(StreamReader &streamReader, Terrain &tern) | ||
{ | ||
auto comnReaderChild = streamReader.ReadChildWithHeader<"COMN"_m>(); | ||
{ | ||
// whats this | ||
comnReaderChild->SkipBytes((uint32_t)comnReaderChild->RemainingBytes()); | ||
} | ||
|
||
std::optional<StreamReader> ptchReaderChild; | ||
while ((ptchReaderChild = streamReader.ReadChildWithHeader<"PTCH"_m>()).has_value()) | ||
{ | ||
StreamReader r{ *ptchReaderChild }; | ||
ReadPatch(r, tern); | ||
} | ||
} | ||
|
||
void TerrainChunk::ReadPatch(StreamReader &streamReader, Terrain &tern) | ||
{ | ||
TerrainPatch ptch; | ||
|
||
auto infoReaderChild = streamReader.ReadChildWithHeader<"INFO"_m>(); | ||
{} | ||
|
||
std::optional<StreamReader> readerChild; | ||
while ((readerChild = streamReader.ReadChildWithHeader<"VBUF"_m>()).has_value()) | ||
{ | ||
uint32_t elementCount; | ||
*readerChild >> elementCount; | ||
|
||
uint32_t elementSize; | ||
*readerChild >> elementSize; | ||
|
||
VBufType elementType; | ||
*readerChild >> elementType; | ||
|
||
if (elementType == VBufType::TEXTURE_EXTRA) | ||
{ | ||
std::vector<TerrainTextureVBufExtraEntry> vbuf; | ||
vbuf.resize(elementCount); | ||
|
||
*readerChild >> vbuf; | ||
|
||
ptch.m_vertices.m_texturesExtra = vbuf; | ||
|
||
if (streamReader.IsNextHeader<"IBUF"_m>()) | ||
{ | ||
readerChild = streamReader.ReadChildWithHeader<"IBUF"_m>(); | ||
*readerChild >> elementCount; | ||
|
||
ptch.m_indices.m_textureExtra.resize(elementCount); | ||
|
||
*readerChild >> ptch.m_indices.m_textureExtra; | ||
} | ||
} | ||
else if (elementType == VBufType::TEXTURE) | ||
{ | ||
std::vector<TerrainTextureVBufEntry> vbuf; | ||
vbuf.resize(elementCount); | ||
|
||
*readerChild >> vbuf; | ||
|
||
ptch.m_vertices.m_textures = vbuf; | ||
|
||
if (streamReader.IsNextHeader<"IBUF"_m>()) | ||
{ | ||
throw std::runtime_error{ "not implemented" }; | ||
} | ||
} | ||
else if (elementType == VBufType::GEOMETRY) | ||
{ | ||
std::vector<TerrainGeometryVBufEntry> vbuf; | ||
vbuf.resize(elementCount); | ||
|
||
*readerChild >> vbuf; | ||
|
||
ptch.m_vertices.m_geometry = vbuf; | ||
|
||
if (streamReader.IsNextHeader<"IBUF"_m>()) | ||
{ | ||
readerChild = streamReader.ReadChildWithHeader<"IBUF"_m>(); | ||
*readerChild >> elementCount; | ||
|
||
ptch.m_indices.m_geometry.resize(elementCount); | ||
|
||
*readerChild >> ptch.m_indices.m_geometry; | ||
} | ||
} | ||
else | ||
{ | ||
throw std::runtime_error{ "Unknown VBUF type encountered in terrain." }; | ||
} | ||
} | ||
|
||
tern.m_patches.push_back(ptch); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
#include "StreamReader.hpp" | ||
|
||
namespace SWBF2::Native | ||
{ | ||
class Terrain; | ||
|
||
class TerrainChunk { | ||
public: | ||
static void ProcessChunk(StreamReader &streamReader); | ||
static void ReadPatches(StreamReader &streamReader, Terrain &tern); | ||
static void ReadPatch(StreamReader &streamReader, Terrain &tern); | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
#include "Native/SWBF2.hpp" | ||
|
||
#include "Terrain.hpp" | ||
#include "Terrain.hpp" | ||
|
||
namespace SWBF2::Native | ||
{ | ||
} |
Oops, something went wrong.