-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsurface.c
More file actions
127 lines (106 loc) · 3.29 KB
/
Copy pathsurface.c
File metadata and controls
127 lines (106 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "types.h"
#include "maths.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
s16 ptInTriangle(f32 p[3], s16 p0[3], s16 p1[3], s16 p2[3]) {
if (( p0[2] - (s16) p[2]) * (p1[0] - p0[0]) - (p0[0] - (s16) p[0]) * (p1[2] - p0[2]) > 0) {
//printf("%iewhrje\n", p1[2]);//((p1[2] - p[2]) * (p2[0] - p1[0]) - (p1[0] - p[0]) * (p2[2] - p1[2])));
if (( p1[2] - (s16) p[2]) * ( p2[0] - p1[0]) - ( p1[0] - (s16) p[0]) * ( p2[2] - p1[2]) > 0) {
//printf("help");
if ((p2[2] - (s16) p[2]) * ( p0[0] - p2[0]) - ( p2[0] - (s16) p[0]) * ( p0[2] - p2[2]) > 0) {
return TRUE;
}
}
}
return FALSE;
}
struct Surface * read_surface_data(s16 vertexData[][3][3], s16 triNum) {
struct Surface *surface = malloc(sizeof(struct Surface));
s32 x1, y1, z1;
s32 x2, y2, z2;
s32 x3, y3, z3;
s32 maxY, minY;
f32 nx, ny, nz;
f32 mag;
x1 = vertexData[triNum][0][0];
y1 = vertexData[triNum][0][1];
z1 = vertexData[triNum][0][2];
x2 = vertexData[triNum][1][0];
y2 = vertexData[triNum][1][1];
z2 = vertexData[triNum][1][2];
x3 = vertexData[triNum][2][0];
y3 = vertexData[triNum][2][1];
z3 = vertexData[triNum][2][2];
// (v2 - v1) x (v3 - v2)
nx = (f32) (y2 - y1) * (f32) (z3 - z2) - (f32) (z2 - z1) * (f32) (y3 - y2);
ny = (f32) (z2 - z1) * (f32) (x3 - x2) - (f32) (x2 - x1) * (f32) (z3 - z2);
nz = (f32) (x2 - x1) * (f32) (y3 - y2) - (f32) (y2 - y1) * (f32) (x3 - x2);
mag = sqrtf(nx * nx + ny * ny + nz * nz);
// Could have used min_3 and max_3 for this...
minY = y1;
if (y2 < minY) {
minY = y2;
}
if (y3 < minY) {
minY = y3;
}
maxY = y1;
if (y2 > maxY) {
maxY = y2;
}
if (y3 > maxY) {
maxY = y3;
}
// Checking to make sure no DIV/0
if (mag < 0.0001) {
return NULL;
}
mag = (f32)(1.0 / mag);
nx *= mag;
ny *= mag;
nz *= mag;
//surface = alloc_surface();
surface->vertex1[0] = x1;
surface->vertex2[0] = x2;
surface->vertex3[0] = x3;
surface->vertex1[1] = y1;
surface->vertex2[1] = y2;
surface->vertex3[1] = y3;
surface->vertex1[2] = z1;
surface->vertex2[2] = z2;
surface->vertex3[2] = z3;
surface->normal.x = nx;
surface->normal.y = ny;
surface->normal.z = nz;
surface->originOffset = -(nx * x1 + ny * y1 + nz * z1);
surface->lowerY = minY - 5;
surface->upperY = maxY + 5;
return surface;
}
struct Surface * check_mario_surface(f32 mPos[3], struct Surface *triList[], s16 numTris) {
s16 i;
//printf("%i\n", numTris);
struct Surface *surface;
for (i = 0; i < numTris; i++) {
surface = triList[i];
//numSurfaces = sizeof vertexData / sizeof *vertexData;
if (ptInTriangle( mPos, surface->vertex1, surface->vertex2, surface->vertex3) == 1) {
//printf("%i\n", i);
return surface;
}
//surface->type = surfaceType;
}
return surface;
}
f32 find_floor_height(struct Surface *surf, s32 x, s32 y, s32 z) {
f32 nx, ny, nz;
f32 oo;
f32 height;
nx = surf->normal.x;
ny = surf->normal.y;
nz = surf->normal.z;
oo = surf->originOffset;
height = -((f32) x * nx + nz * (f32) z + oo) / ny;
return height;
}