Paper Mario DX
Paper Mario (N64) modding
 
Loading...
Searching...
No Matches
f8f60_len_1560.c File Reference

Go to the source code of this file.

Functions

void load_path_data (s32 num, f32 *normalizedLengths, Vec3f *pathPositions, Vec3f *outVectors)
 
void get_path_position (f32 alpha, Vec3f *outPos, s32 numVectors, f32 *normalizedLengths, Vec3f *pathPoints, Vec3f *vectors)
 

Function Documentation

◆ load_path_data()

void load_path_data ( s32 num,
f32 * normalizedLengths,
Vec3f * pathPositions,
Vec3f * outVectors )

Definition at line 279 of file f8f60_len_1560.c.

279 {
280 f32* lenBuf = heap_malloc(num * sizeof(f32));
281 Vec3f* vecBuf = heap_malloc(num * sizeof(Vec3f));
282 s32 i;
283
284 // compute the distance of each vector along the path and map to the range [0,1]
285 normalizedLengths[0] = 0.0f;
286 for (i = 1; i < num; i++) {
287 f32 dx = pathPositions[i].x - pathPositions[i-1].x;
288 f32 dy = pathPositions[i].y - pathPositions[i-1].y;
289 f32 dz = pathPositions[i].z - pathPositions[i-1].z;
290 f32 length = sqrtf(SQ(dx) + SQ(dy) + SQ(dz));
291 normalizedLengths[i] = normalizedLengths[i-1] + length;
292 }
293 for (i = 1; i < num; i++) {
294 normalizedLengths[i] /= normalizedLengths[num-1];
295 }
296
297 // end points
298 outVectors[0].x = 0.0f;
299 outVectors[0].y = 0.0f;
300 outVectors[0].z = 0.0f;
301 outVectors[num-1].x = 0.0f;
302 outVectors[num-1].y = 0.0f;
303 outVectors[num-1].z = 0.0f;
304
305 for (i = 0; i < num - 1; i++) {
306 lenBuf[i] = normalizedLengths[i+1] - normalizedLengths[i];
307 vecBuf[i+1].x = (pathPositions[i+1].x - pathPositions[i].x) / lenBuf[i];
308 vecBuf[i+1].y = (pathPositions[i+1].y - pathPositions[i].y) / lenBuf[i];
309 vecBuf[i+1].z = (pathPositions[i+1].z - pathPositions[i].z) / lenBuf[i];
310 }
311
312 // n = 1
313 outVectors[1].x = vecBuf[2].x - vecBuf[1].x;
314 outVectors[1].y = vecBuf[2].y - vecBuf[1].y;
315 outVectors[1].z = vecBuf[2].z - vecBuf[1].z;
316 vecBuf[1].x = 2.0f * (normalizedLengths[2] - normalizedLengths[0]);
317 vecBuf[1].y = 2.0f * (normalizedLengths[2] - normalizedLengths[0]);
318 vecBuf[1].z = 2.0f * (normalizedLengths[2] - normalizedLengths[0]);
319
320 // 1 < n < N - 2
321 for (i = 1; i < num - 2; i++) {
322 f32 sx = lenBuf[i] / vecBuf[i].x;
323 f32 sy = lenBuf[i] / vecBuf[i].y;
324 f32 sz = lenBuf[i] / vecBuf[i].z;
325 outVectors[i+1].x = (vecBuf[i+2].x - vecBuf[i+1].x) - outVectors[i].x * sx;
326 outVectors[i+1].y = (vecBuf[i+2].y - vecBuf[i+1].y) - outVectors[i].y * sy;
327 outVectors[i+1].z = (vecBuf[i+2].z - vecBuf[i+1].z) - outVectors[i].z * sz;
328 vecBuf[i+1].x = 2.0f * (normalizedLengths[i+2] - normalizedLengths[i]) - lenBuf[i] * sx;
329 vecBuf[i+1].y = 2.0f * (normalizedLengths[i+2] - normalizedLengths[i]) - lenBuf[i] * sy;
330 vecBuf[i+1].z = 2.0f * (normalizedLengths[i+2] - normalizedLengths[i]) - lenBuf[i] * sz;
331 }
332
333 // n = N - 2
334 outVectors[num-2].x -= (lenBuf[num-2] * outVectors[num-1].x);
335 outVectors[num-2].y -= (lenBuf[num-2] * outVectors[num-1].y);
336 outVectors[num-2].z -= (lenBuf[num-2] * outVectors[num-1].z);
337
338 for (i = num - 2; i > 0; i--) {
339 outVectors[i].x = (outVectors[i].x - (lenBuf[i] * outVectors[i+1].x)) / vecBuf[i].x;
340 outVectors[i].y = (outVectors[i].y - (lenBuf[i] * outVectors[i+1].y)) / vecBuf[i].y;
341 outVectors[i].z = (outVectors[i].z - (lenBuf[i] * outVectors[i+1].z)) / vecBuf[i].z;
342 }
343
344 heap_free(lenBuf);
345 heap_free(vecBuf);
346}
#define sqrtf
s32 heap_free(void *ptr)
Definition heap.c:42
void * heap_malloc(s32 size)
Definition heap.c:34
#define SQ(x)
Definition macros.h:166

◆ get_path_position()

void get_path_position ( f32 alpha,
Vec3f * outPos,
s32 numVectors,
f32 * normalizedLengths,
Vec3f * pathPoints,
Vec3f * vectors )

Definition at line 348 of file f8f60_len_1560.c.

348 {
349 s32 limit = numVectors - 1;
350 f32 curLength;
351 f32 curProgress;
352 f32 ax, ay, az, bx, by, bz, dx, dy, dz;
353 s32 i;
354
355 for (i = 0; i < limit;) {
356 s32 temp_v1 = (i + limit) / 2;
357
358 if (normalizedLengths[temp_v1] < alpha) {
359 i = temp_v1 + 1;
360 } else {
361 limit = temp_v1;
362 }
363 }
364
365 if (i > 0) {
366 i--;
367 }
368
369 curLength = normalizedLengths[i+1] - normalizedLengths[i];
370 curProgress = alpha - normalizedLengths[i];
371
372 dx = (pathPoints[i+1].x - pathPoints[i].x) / curLength;
373 ax = (((vectors[i+1].x - vectors[i].x) * curProgress / curLength) + (3.0f * vectors[i].x)) * curProgress;
374 bx = dx - (((2.0f * vectors[i].x) + vectors[i+1].x) * curLength);
375 outPos->x = ((ax + bx) * curProgress) + pathPoints[i].x;
376
377 dy = (pathPoints[i+1].y - pathPoints[i].y) / curLength;
378 ay = (((vectors[i+1].y - vectors[i].y) * curProgress / curLength) + (3.0f * vectors[i].y)) * curProgress;
379 by = dy - (((2.0f * vectors[i].y) + vectors[i+1].y) * curLength);
380 outPos->y = ((ay + by) * curProgress) + pathPoints[i].y;
381
382 dz = (pathPoints[i+1].z - pathPoints[i].z) / curLength;
383 az = (((vectors[i+1].z - vectors[i].z) * curProgress / curLength) + (3.0f * vectors[i].z)) * curProgress;
384 bz = dz - (((2.0f * vectors[i].z) + vectors[i+1].z) * curLength);
385 outPos->z = ((az + bz) * curProgress) + pathPoints[i].z;
386}