persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
ModelComponent.cpp
Go to the documentation of this file.
1#include "ModelComponent.hpp"
2#include "core/logger.hpp"
3
4void ModelComponent::update(ae::q20_12_t)
5{
6 drawMesh();
7}
8
10{
11 active = false;
12
13 if (model)
14 {
15 for (MDL3Texture& tex : model->textures)
16 {
17 if (tex.textureID != -1)
18 {
19 render.deleteTexture(tex.textureID);
20 }
21 }
22
23 for (Node& node : model->nodes)
24 {
25 for (SubList_N& sl : node.subLists)
26 {
27 if (sl.displayList)
28 {
29 delete[] sl.displayList;
30 sl.displayList = nullptr;
31 }
32 }
33 }
34
35 model.reset();
36 }
37}
38
40{
42
43 if (fread(&raw, sizeof(RawTextureHeader), 1, f) != 1)
44 {
45 return false;
46 }
47
48 tex.width = raw.width;
49 tex.height = raw.height;
50 tex.isRGBA = raw.isRGBA;
51
52 memcpy(tex.name, raw.name, 64);
53 tex.name[64] = '\0'; // Ensure null-termination
54
55 // Trim trailing spaces and nulls
56 size_t len = strlen(tex.name);
57 while (len > 0 && (tex.name[len - 1] == '\0' || tex.name[len - 1] == ' ' || tex.name[len - 1] == '\r'))
58 {
59 tex.name[--len] = '\0';
60 }
61
62 return true;
63}
64
66{
67 char imgName[32];
68 uint32_t byteLength = 0;
69
70 if (fread(imgName, 1, 32, f) != 32 || fread(&byteLength, sizeof(uint32_t), 1, f) != 1)
71 {
72 return false;
73 }
74 if (byteLength == 0)
75 {
76 return false;
77 }
78
79 // Allocate 32-bit aligned buffer for DS DMA GPU uploads
80 size_t alignedWords = (static_cast<size_t>(byteLength) + 3) / 4;
81 uint32_t* alignedBuffer = static_cast<uint32_t*>(malloc(alignedWords * 4));
82 if (!alignedBuffer)
83 {
84 logMessage(LogSeverity::ERROR, "Failed to allocate aligned buffer for texture");
85 return false;
86 }
87
88 if (fread(alignedBuffer, 1, byteLength, f) != static_cast<size_t>(byteLength))
89 {
90 free(alignedBuffer);
91 return false;
92 }
93
94 // Explicitly delete old VRAM textures if previously allocated
95 if (tex.textureID != -1)
96 {
97 render.deleteTexture(tex.textureID);
98 }
99
100 GL_TEXTURE_TYPE_ENUM texType = tex.isRGBA ? GL_RGBA : GL_RGB16;
101 if (!render.uploadTexture(tex.textureID,
102 texType,
103 tex.width,
104 tex.height,
105 TEXGEN_TEXCOORD | GL_TEXTURE_WRAP_S | GL_TEXTURE_WRAP_T,
106 alignedBuffer))
107 {
108 free(alignedBuffer);
109 return false;
110 }
111 free(alignedBuffer);
112 return true;
113}
114
115bool ModelComponent::loadMesh(std::string* meshFilePath)
116{
117 if (meshFilePath == nullptr)
118 {
119 logMessage(LogSeverity::WARNING, "Called with null meshFilePath");
120 return false;
121 }
122
123 FILE* f = fopen(meshFilePath->c_str(), "rb");
124 if (!f)
125 {
126 logMessage(LogSeverity::ERROR, "Failed to open mesh file: " + *meshFilePath);
127 return false;
128 }
129
130 model = std::make_unique<MDL3Model>();
131 RawModelHeader rawHeader;
132
133 if (fread(&rawHeader, sizeof(RawModelHeader), 1, f) != 1)
134 {
135 logMessage(LogSeverity::ERROR, "Failed to read mesh header from: " + *meshFilePath);
136 model.reset();
137 fclose(f);
138 return false;
139 }
140
141 if (memcmp(rawHeader.magic, "MDL3", 4) != 0)
142 {
143 logMessage(LogSeverity::ERROR, "Invalid MDL3 magic header in mesh file: " + *meshFilePath);
144 fclose(f);
145 model.reset();
146 return false;
147 }
148
149 model->nodeCount = rawHeader.nodeCount;
150 model->texCount = rawHeader.texCount;
151
152 if (rawHeader.animCount > 0)
153 {
154 fclose(f);
155 model.reset();
156 return false; // Animations not supported for static meshes
157 }
158
159 // Read Texture Headers
160 model->textures.resize(model->texCount);
161 for (uint32_t i = 0; i < model->texCount; ++i)
162 {
163 if (!loadTextureHeader(f, model->textures[i]))
164 {
165 fclose(f);
166 model.reset();
167 return false;
168 }
169 }
170
171 // Read in Nodes
172 model->nodes.resize(model->nodeCount);
173 for (uint32_t i = 0; i < model->nodeCount; ++i)
174 {
175 RawNodeHeader rawNode;
176
177 if (fread(&rawNode, sizeof(RawNodeHeader), 1, f) != 1)
178 {
179 fclose(f);
180 model.reset();
181 return false;
182 }
183
184 model->nodes[i].pid = rawNode.pid;
185
186 for (uint32_t j = 0; j < rawNode.subListCount; ++j)
187 {
188 SubList_N sl;
189 if (fread(&sl.texSlot, sizeof(int32_t), 1, f) != 1 || fread(&sl.dlSize, sizeof(uint32_t), 1, f) != 1)
190 {
191 fclose(f);
192 model.reset();
193 return false;
194 }
195
196 if (sl.dlSize > 0)
197 {
198 // Allocate persistent memory for the display list
199 uint32_t* displayList = new uint32_t[sl.dlSize + 1];
200 displayList[0] = sl.dlSize;
201 if (fread(&displayList[1], sizeof(std::uint32_t), sl.dlSize, f) != static_cast<size_t>(sl.dlSize))
202 {
203 std::fclose(f);
204 model.reset();
205 return false;
206 }
207 sl.displayList = displayList;
208 }
209 model->nodes[i].subLists.push_back(std::move(sl));
210 }
211 }
212
213 for (Node& node : model->nodes)
214 {
215 std::sort(node.subLists.begin(),
216 node.subLists.end(),
217 [](const SubList_N& a, const SubList_N& b) { return a.texSlot < b.texSlot; });
218 }
219
220 // Skip animation data on static meshes
221
222 // Textures
223 for (uint32_t i = 0; i < model->texCount; ++i)
224 {
225 if (!loadTexture(f, model->textures[i]))
226 {
227 fclose(f);
228 model.reset();
229 return false;
230 }
231 }
232 fclose(f);
233 active = true;
234 return true;
235}
236
238{
239 if (!model)
240 {
241 return;
242 }
243 render.renderModelComponent(*model);
244}
Orchestrates rendering models from a 3D model file.
void drawMesh()
Draws the loaded mesh to the screen.
std::unique_ptr< MDL3Model > model
void update(ae::q20_12_t) override
Draws the mesh to the screen every frame.
void destroy() override
Sets active to false on component destruction.
bool loadMesh(std::string *meshFilePath)
Loads a mesh from a file into memory.
RenderManager & render
bool loadTextureHeader(FILE *f, MDL3Texture &tex)
Loads a texture header from the file buffer into memory.
bool loadTexture(FILE *f, MDL3Texture &tex)
Loads an embedded texture from the file buffer and uploads it to the GPU.
void logMessage(LogSeverity severity, std::string_view message, std::source_location location)
Logs a diagnostic message and halts only for fatal messages.
Definition logger.cpp:110
Util to handle error logging.
Represents a texture used in a 3D model.
uint16_t height
uint16_t width
char name[65]
Represents a node in the 3D model.
etl::vector< SubList_N, 64 > subLists
Represents the header of a raw model file.
uint32_t animCount
uint32_t nodeCount
uint32_t texCount
Represents the header of a raw node file.
uint32_t subListCount
Represents the header of a raw texture file.
Represents a sublist of a node in the 3D model.
const uint32_t * displayList
int32_t texSlot
int32_t dlSize