OpenGothic
Open source reimplementation of Gothic I and II
Loading...
Searching...
No Matches
phoenix.cpp
Go to the documentation of this file.
1#include "phoenix.h"
2
3#include <limits>
4
5zenkit::AxisAlignedBoundingBox phoenix_compat::get_total_aabb(const zenkit::SoftSkinMesh& msh) {
6 zenkit::AxisAlignedBoundingBox bbox = {{std::numeric_limits<float>::max(), std::numeric_limits<float>::max(), std::numeric_limits<float>::max()},
7 {std::numeric_limits<float>::min(), std::numeric_limits<float>::min(), std::numeric_limits<float>::min()}};
8
9 {
10 auto meshBbox = msh.mesh.bbox;
11
12 bbox.min.x = std::min(bbox.min.x, meshBbox.min.x);
13 bbox.min.y = std::min(bbox.min.y, meshBbox.min.y);
14 bbox.min.z = std::min(bbox.min.z, meshBbox.min.z);
15
16 bbox.max.x = std::max(bbox.max.x, meshBbox.max.x);
17 bbox.max.y = std::max(bbox.max.y, meshBbox.max.y);
18 bbox.max.z = std::max(bbox.max.z, meshBbox.max.z);
19 }
20
21 for (const auto& bb : msh.bboxes) {
22 auto meshBbox = bb.as_bbox();
23
24 bbox.min.x = std::min(bbox.min.x, meshBbox.min.x);
25 bbox.min.y = std::min(bbox.min.y, meshBbox.min.y);
26 bbox.min.z = std::min(bbox.min.z, meshBbox.min.z);
27
28 bbox.max.x = std::max(bbox.max.x, meshBbox.max.x);
29 bbox.max.y = std::max(bbox.max.y, meshBbox.max.y);
30 bbox.max.z = std::max(bbox.max.z, meshBbox.max.z);
31 }
32
33 return bbox;
34}
35
38 std::vector<phoenix_compat::SkeletalVertex> vertices(self.mesh.positions.size());
39
40 auto bb = get_total_aabb(self);
41 mesh.bbox[0] = {bb.min.x, bb.min.y, bb.min.z};
42 mesh.bbox[1] = {bb.max.x, bb.max.y, bb.max.z};
43
44 // Extract weights and local positions
45 const auto& stream = self.weights;
46
47 for(size_t i=0; i<vertices.size(); ++i) {
48 for(size_t j=0; j<stream[i].size(); j++) {
49 auto& weight = stream[i][j];
50
51 vertices[i].BoneIndices[j] = weight.node_index;
52 vertices[i].LocalPositions[j] = {weight.position.x, weight.position.y, weight.position.z};
53 vertices[i].Weights[j] = weight.weight;
54 }
55 }
56
57 size_t vboSize = 0;
58 size_t iboSize = 0;
59 for(size_t s=0; s<self.mesh.sub_meshes.size(); s++) {
60 const auto& sm = self.mesh.sub_meshes[s];
61 vboSize += sm.wedges.size();
62 iboSize += sm.triangles.size()*3;
63 }
64
65 mesh.vertices.resize(vboSize);
66 mesh.indices .resize(iboSize);
67 mesh.subMeshes.resize(self.mesh.sub_meshes.size());
68 auto* vbo = mesh.vertices.data();
69 auto* ibo = mesh.indices.data();
70
71 uint32_t meshVxStart = 0, iboStart = 0;
72 for(size_t s=0; s<self.mesh.sub_meshes.size(); s++) {
73 const auto& sm = self.mesh.sub_meshes[s];
74 // Get data
75 for(size_t i=0; i<sm.wedges.size(); ++i) {
76 const auto& wedge = sm.wedges[i];
77 phoenix_compat::SkeletalVertex v = vertices[wedge.index];
78
79 v.Normal = {wedge.normal.x, wedge.normal.y, wedge.normal.z};
80 v.TexCoord = {wedge.texture.x, wedge.texture.y};
81 v.Color = 0xFFFFFFFF; // TODO: Apply color from material!
82 *vbo = v;
83 ++vbo;
84 }
85
86 // And get the indices
87 for(size_t i=0; i<sm.triangles.size(); ++i) {
88 for(int j=0; j<3; j++) {
89 *ibo = sm.triangles[i].wedges[j] + meshVxStart;
90 ++ibo;
91 }
92 }
93
94 auto& pack = mesh.subMeshes[s];
95 pack.indexOffset = iboStart;
96 pack.indexSize = sm.triangles.size()*3;
97 pack.material = sm.mat;
98 meshVxStart += uint32_t(sm.wedges.size());
99 iboStart += uint32_t(sm.triangles.size()*3);
100 }
101
102 return mesh;
103}
PackedSkeletalMesh pack_softskin_mesh(const zenkit::SoftSkinMesh &)
Definition phoenix.cpp:36
zenkit::AxisAlignedBoundingBox get_total_aabb(const zenkit::SoftSkinMesh &)
Definition phoenix.cpp:5