47 return TEXTURE_SIZE_8;
51 return TEXTURE_SIZE_16;
55 return TEXTURE_SIZE_32;
59 return TEXTURE_SIZE_64;
63 return TEXTURE_SIZE_128;
67 return TEXTURE_SIZE_256;
71 return TEXTURE_SIZE_512;
73 return TEXTURE_SIZE_1024;
81 fread(magic, 1, 4, file);
83 if (strncmp(magic,
"MDL1", 4) == 0)
88 if (strncmp(magic,
"MDL2", 4) == 0)
98 FILE* file = fopen(filepath,
"rb");
113 u32 numNodes, numAnims;
114 fread(&numNodes,
sizeof(u32), 1, file);
115 fread(&numAnims,
sizeof(u32), 1, file);
120 fread(&texCount,
sizeof(u32), 1, file);
122 fseek(file, (
long)(texCount * 72), SEEK_CUR);
128 for (u32 i = 0; i < numNodes; i++)
136 fread(&pid,
sizeof(s32), 1, file);
137 fread(&px,
sizeof(s32), 1, file);
138 fread(&py,
sizeof(s32), 1, file);
139 fread(&pz,
sizeof(s32), 1, file);
149 fread(&subListCount,
sizeof(u32), 1, file);
152 for (u32 s = 0; s < subListCount; s++)
157 fread(&texSlot,
sizeof(s32), 1, file);
158 fread(&dlSize,
sizeof(u32), 1, file);
166 fread(&sl.
displayList[1],
sizeof(u32), dlSize, file);
174 fread(&dlSize,
sizeof(u32), 1, file);
183 fread(&sl.
displayList[1],
sizeof(u32), dlSize, file);
192 else if (pid >= 0 && pid < (
int)numNodes)
199 for (u32 i = 0; i < numAnims; i++)
202 fseek(file, 32, SEEK_CUR);
203 fread(&anim.
duration,
sizeof(u32), 1, file);
206 for (u32 n = 0; n < numNodes; n++)
210 fread(&numFrames,
sizeof(u32), 1, file);
211 track.
frames.resize(numFrames);
226 int count,
const unsigned int** bitmaps,
const int* widths,
const int* heights,
const bool* isRGBA)
232 for (
int i = 0; i < count; i++)
234 if (!bitmaps || !bitmaps[i])
242 GL_TEXTURE_TYPE_ENUM fmt = isRGBA[i] ? GL_RGBA : GL_RGB;
244 glTexImage2D(GL_TEXTURE_2D,
250 TEXGEN_TEXCOORD | GL_TEXTURE_WRAP_S | GL_TEXTURE_WRAP_T,
270 if (animIndex < 0 || animIndex >= (
int)
animations.size())
350 return {0, 0, 0, 0, 0, 0, 0};
355 return {0, 0, 0, 0, 0, 0, 0};
357 if (track.
frames.size() == 1)
364 int timeInt = timeFP >> 8;
366 if (cacheIdx >= (
int)track.
frames.size() - 1 || timeInt < track.
frames[cacheIdx].time)
370 while (cacheIdx < (
int)track.
frames.size() - 1 && timeInt >= track.
frames[cacheIdx + 1].time)
376 if (cacheIdx >= (
int)track.
frames.size() - 1)
388 int progressFP = timeFP - (k1.
time << 8);
394 int t = (progressFP << 4) / range;
397 result.
time = timeInt;
413 glMatrixMode(GL_MODELVIEW);
426 if (nodeId < 0 || nodeId >= (
int)
modelNodes.size())
441 glRotateZi(current.
rotZ);
442 glRotateYi(current.
rotY);
443 glRotateXi(current.
rotX);
448 int currentTexSlot = -2;
457 if (sl.
texSlot != currentTexSlot)
static ModelVersion readModelVersion(FILE *file)
Loads, animates, and renders a model in the DS geometry engine.
void pause()
Finishes the current animation at its next boundary.
void renderNode(int nodeId)
void play()
Starts the selected animation.
std::vector< int > rootNodes
static void destroy()
Destroys the singleton instance and unloads model textures.
void render()
Renders the model hierarchy using the current animation frame.
static AnimationController * getInstance()
~AnimationController()
Releases texture objects owned by the controller.
Keyframe getInterpolatedFrame(const AnimTrack &track, int timeFP, int nodeId)
static void create()
Creates the singleton instance if it does not exist.
void update()
Advances the selected animation by one frame tick.
static int textureSizeToEnum(int px)
void stop()
Stops playback and resets the selected animation frame.
static AnimationController * instance
bool loadModel(const char *filepath)
Loads an MDL2 or legacy MDL1 model file.
std::vector< int > textureIDs
std::vector< int > trackIndices
bool loadTextures(int count, const unsigned int **bitmaps, const int *widths, const int *heights, const bool *isRGBA)
Uploads model textures to VRAM after loadModel().
std::vector< AnimNode > modelNodes
void set(int animIndex, bool loop=true)
Selects an animation.
void unloadTextures()
Deletes all model texture objects from VRAM.
std::vector< Animation > animations
void logMessage(LogSeverity severity, std::string_view message, std::source_location location)
Logs a diagnostic message and halts only for fatal messages.
Util to handle error logging.
std::vector< SubList > subLists
std::vector< int > children
std::vector< Keyframe > frames
std::vector< AnimTrack > nodeTracks
std::vector< u32 > displayList