Skip to content

Commit

Permalink
Use godot Vector and Color where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyuu17 committed Jun 28, 2024
1 parent c3a11d7 commit a9368e6
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 192 deletions.
32 changes: 6 additions & 26 deletions src/SWBF2/Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,15 @@ namespace SWBF2
godot::ArrayMesh *arrMesh = memnew(godot::ArrayMesh);
for (auto const &segment : model.m_segments)
{
godot::PackedVector3Array vertices;
vertices.resize(segment.m_verticesBuf.m_positions.size());
std::copy(segment.m_verticesBuf.m_positions.begin(), segment.m_verticesBuf.m_positions.end(), reinterpret_cast<Vector3<float>*>(vertices.ptrw()));

godot::PackedVector3Array normals;
normals.resize(segment.m_verticesBuf.m_normals.size());
std::copy(segment.m_verticesBuf.m_normals.begin(), segment.m_verticesBuf.m_normals.end(), reinterpret_cast<Vector3<float>*>(normals.ptrw()));

godot::PackedColorArray colors;
colors.resize(segment.m_verticesBuf.m_colors.size());
std::copy(segment.m_verticesBuf.m_colors.begin(), segment.m_verticesBuf.m_colors.end(), colors.ptrw());

godot::PackedVector2Array uvs;
uvs.resize(segment.m_verticesBuf.m_texCoords.size());
std::copy(segment.m_verticesBuf.m_texCoords.begin(), segment.m_verticesBuf.m_texCoords.end(), reinterpret_cast<Vector2<float>*>(uvs.ptrw()));

godot::PackedInt32Array indices;
indices.resize(segment.m_indicesBuf.m_indices.size());
std::copy(segment.m_indicesBuf.m_indices.begin(), segment.m_indicesBuf.m_indices.end(), indices.ptrw());

godot::Array arrays;
arrays.resize(godot::ArrayMesh::ARRAY_MAX);
arrays[godot::ArrayMesh::ARRAY_VERTEX] = vertices;
arrays[godot::ArrayMesh::ARRAY_NORMAL] = normals;
arrays[godot::ArrayMesh::ARRAY_VERTEX] = segment.m_verticesBuf.m_positions;
arrays[godot::ArrayMesh::ARRAY_NORMAL] = segment.m_verticesBuf.m_normals;
//arrays[godot::ArrayMesh::ARRAY_TANGENT] = tangents;
if (colors.size() > 0)
arrays[godot::ArrayMesh::ARRAY_COLOR] = colors;
arrays[godot::ArrayMesh::ARRAY_TEX_UV] = uvs;
arrays[godot::ArrayMesh::ARRAY_INDEX] = indices;
if (segment.m_verticesBuf.m_colors.size() > 0)
arrays[godot::ArrayMesh::ARRAY_COLOR] = segment.m_verticesBuf.m_colors;
arrays[godot::ArrayMesh::ARRAY_TEX_UV] = segment.m_verticesBuf.m_texCoords;
arrays[godot::ArrayMesh::ARRAY_INDEX] = segment.m_indicesBuf.m_indices;

arrMesh->add_surface_from_arrays(Native::ModelUtils::DXtoGLPrimitiveType(segment.m_primitiveType), arrays);

Expand Down
51 changes: 31 additions & 20 deletions src/SWBF2/Native/Chunks/ModelSegmentChunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ namespace SWBF2::Native
{
Material mat;
*readerChild >> mat.m_flags;
*readerChild >> mat.m_diffuseColor.color32;
*readerChild >> mat.m_specularColor.color32;

uint8_t r, g, b, a;
*readerChild >> r >> g >> b >> a;
mat.m_diffuseColor = godot::Color(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);

*readerChild >> r >> g >> b >> a;
mat.m_specularColor = godot::Color(r / 255.0f, g / 255.0f, b / 255.0f, a / 255.0f);

*readerChild >> mat.m_specularExponent;
*readerChild >> mat.m_parameters;
*readerChild >> mat.m_attachedLight;
Expand All @@ -49,7 +55,12 @@ namespace SWBF2::Native

segment.m_indicesBuf.m_indices.resize(segment.m_indicesBuf.m_indicesCount);

*readerChild >> segment.m_indicesBuf.m_indices;
std::vector<uint16_t> indices;
indices.resize(segment.m_indicesBuf.m_indicesCount);

*readerChild >> indices;

std::copy(indices.begin(), indices.end(), segment.m_indicesBuf.m_indices.ptrw());
break;
}
case "VBUF"_m:
Expand Down Expand Up @@ -97,21 +108,21 @@ namespace SWBF2::Native
{
if ((segment.m_verticesBuf.m_flags & VBUFFlags::PositionCompressed) != 0)
{
Vector3<float> low = model.m_info.m_vertexBox[0];
Vector3<float> mul = model.m_info.m_vertexBox[1] - model.m_info.m_vertexBox[0];
godot::Vector3 low = model.m_info.m_vertexBox[0];
godot::Vector3 mul = model.m_info.m_vertexBox[1] - model.m_info.m_vertexBox[0];

int16_t data[4];
streamReader >> data;
Vector3<float> c(data[0], data[1], data[2]);
godot::Vector3 c(data[0], data[1], data[2]);

constexpr float i16min = std::numeric_limits<int16_t>::min();
constexpr float i16max = std::numeric_limits<int16_t>::max();

segment.m_verticesBuf.m_positions.push_back((low + (c - i16min) * mul / (i16max - i16min)));
segment.m_verticesBuf.m_positions.push_back((low + (c - godot::Vector3{ i16min, i16min, i16min }) * mul / (i16max - i16min)));
}
else
{
Vector3<float> vec3;
godot::Vector3 vec3;
streamReader >> vec3;

segment.m_verticesBuf.m_positions.push_back(vec3);
Expand All @@ -130,7 +141,7 @@ namespace SWBF2::Native
}
else
{
Vector2<float> vec2;
godot::Vector2 vec2;
streamReader >> vec2;

segment.m_verticesBuf.m_weights.push_back({ vec2.x, vec2.y, 1.0f - vec2.x - vec2.y });
Expand All @@ -147,7 +158,7 @@ namespace SWBF2::Native
uint8_t y = (uint8_t)((inds >> 8u) & 0xffu);
uint8_t z = (uint8_t)((inds >> 16u) & 0xffu);

segment.m_verticesBuf.m_boneIndices.push_back({ x, y, z });
segment.m_verticesBuf.m_boneIndices.push_back(godot::Vector3i{ x, y, z });
}

if ((segment.m_verticesBuf.m_flags & VBUFFlags::Normal) != 0)
Expand All @@ -156,13 +167,13 @@ namespace SWBF2::Native
{
int8_t data[4];
streamReader >> data;
Vector3<float> normal((float_t)data[0], (float_t)data[1], (float_t)data[2]);
normal = (normal * 2.0f) - 1.0f;
godot::Vector3 normal((float_t)data[0], (float_t)data[1], (float_t)data[2]);
normal = (normal * 2.0f) - godot::Vector3{ 1.0f, 1.0f, 1.0f };
segment.m_verticesBuf.m_normals.push_back(normal);
}
else
{
Vector3<float> vec3;
godot::Vector3 vec3;
streamReader >> vec3;

segment.m_verticesBuf.m_normals.push_back(vec3);
Expand All @@ -175,18 +186,18 @@ namespace SWBF2::Native
{
int8_t data[4];
streamReader >> data;
Vector3<float> tangent((float_t)data[0], (float_t)data[1], (float_t)data[2]);
tangent = (tangent * 2.0f) - 1.0f;
godot::Vector3 tangent((float_t)data[0], (float_t)data[1], (float_t)data[2]);
tangent = (tangent * 2.0f) - godot::Vector3{ 1.0f, 1.0f, 1.0f };
segment.m_verticesBuf.m_tangents.push_back(tangent);

streamReader >> data;
Vector3<float> biTangent((float_t)data[0], (float_t)data[1], (float_t)data[2]);
biTangent = (biTangent * 2.0f) - 1.0f;
godot::Vector3 biTangent((float_t)data[0], (float_t)data[1], (float_t)data[2]);
biTangent = (biTangent * 2.0f) - godot::Vector3{ 1.0f, 1.0f, 1.0f };
segment.m_verticesBuf.m_biTangents.push_back(biTangent);
}
else
{
Vector3<float> vec3Tangent, vec3BiTangent;
godot::Vector3 vec3Tangent, vec3BiTangent;
streamReader >> vec3Tangent >> vec3BiTangent;

segment.m_verticesBuf.m_tangents.push_back(vec3Tangent);
Expand Down Expand Up @@ -218,13 +229,13 @@ namespace SWBF2::Native
{
uint16_t data[2];
streamReader >> data;
Vector2<float> uv(data[0], data[1]);
godot::Vector2 uv(data[0], data[1]);
uv = uv / 2048.0f;
segment.m_verticesBuf.m_texCoords.push_back(uv);
}
else
{
Vector2<float> vec2;
godot::Vector2 vec2;
streamReader >> vec2;

segment.m_verticesBuf.m_texCoords.push_back(vec2);
Expand Down
4 changes: 2 additions & 2 deletions src/SWBF2/Native/Models/Material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ namespace SWBF2::Native
};

MaterialFlags m_flags;
RGBA m_diffuseColor;
RGBA m_specularColor;
godot::Color m_diffuseColor;
godot::Color m_specularColor;
uint32_t m_specularExponent;
uint32_t m_parameters[2];
std::string m_attachedLight;
Expand Down
4 changes: 2 additions & 2 deletions src/SWBF2/Native/Models/Model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace SWBF2::Native
typedef struct _MODEL_INFO
{
uint32_t m_unknown[4];
Vector3<float> m_vertexBox[2];
Vector3<float> m_visibilityBox[2];
godot::Vector3 m_vertexBox[2];
godot::Vector3 m_visibilityBox[2];
uint32_t m_faceCount;
} ModelInfo;

Expand Down
18 changes: 9 additions & 9 deletions src/SWBF2/Native/Models/ModelSegment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace SWBF2::Native
typedef struct _INDICES_BUF
{
uint32_t m_indicesCount;
std::vector<uint16_t> m_indices;
godot::PackedInt32Array m_indices;
} IndicesBuf;

typedef struct _VERTICES_BUF
Expand All @@ -50,16 +50,16 @@ namespace SWBF2::Native
uint32_t m_stride; // bytes per vertex
VBUFFlags m_flags;

std::vector<Vector3<float>> m_positions;
std::vector<Vector3<float>> m_normals;
std::vector<Vector3<float>> m_tangents;
std::vector<Vector3<float>> m_biTangents;
godot::PackedVector3Array m_positions;
godot::PackedVector3Array m_normals;
godot::PackedVector3Array m_tangents;
godot::PackedVector3Array m_biTangents;

std::vector<godot::Color> m_colors;
std::vector<Vector2<float>> m_texCoords;
godot::PackedColorArray m_colors;
godot::PackedVector2Array m_texCoords;

std::vector<Vector3<uint8_t>> m_boneIndices;
std::vector<Vector3<float>> m_weights;
godot::PackedVector3Array m_boneIndices;
godot::PackedVector3Array m_weights;
} VerticesBuf;

class ModelSegment {
Expand Down
43 changes: 0 additions & 43 deletions src/SWBF2/RGBA.hpp

This file was deleted.

3 changes: 0 additions & 3 deletions src/SWBF2/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
#include <map>
#include <format>

#include "Vector2.hpp"
#include "Vector3.hpp"
#include "RGBA.hpp"
#include "FNVHash.hpp"
#include "Utils.hpp"

Expand Down
35 changes: 0 additions & 35 deletions src/SWBF2/Vector2.hpp

This file was deleted.

52 changes: 0 additions & 52 deletions src/SWBF2/Vector3.hpp

This file was deleted.

0 comments on commit a9368e6

Please sign in to comment.