GrooveStomp's 3D Software Renderer  0.1.0
math.h
Go to the documentation of this file.
1 /******************************************************************************
2  GrooveStomp's 3D Software Renderer
3  Copyright (c) 2019 Aaron Oman (GrooveStomp)
4 
5  File: math.h
6  Created: 2019-08-13
7  Updated: 2019-08-27
8  Author: Aaron Oman
9  Notice: GNU GPLv3 License
10 
11  Based off of: One Lone Coder Console Game Engine Copyright (C) 2018 Javidx9
12  This program comes with ABSOLUTELY NO WARRANTY.
13  This is free software, and you are welcome to redistribute it under certain
14  conditions; See LICENSE for details.
15  ******************************************************************************/
16 
19 
20 #ifndef MATH_VERSION
21 #define MATH_VERSION "0.1.0"
22 
23 struct vec2 {
28  union {
29  struct {
30  float u;
31  float v;
32  float w;
33  };
34  float p[3];
35  };
36 };
37 
42 struct vec3 {
43  union {
44  struct {
45  float x;
46  float y;
47  float z;
48  float w;
49  };
50  float p[4];
51  };
52 };
53 
61 struct triangle {
62  union {
63  struct {
64  float x1;
65  float y1;
66  float z1;
67  float w1;
68  float x2;
69  float y2;
70  float z2;
71  float w2;
72  float x3;
73  float y3;
74  float z3;
75  float w3;
76  };
77  struct vec3 v[3];
78  };
79  union {
80  struct {
81  float u1;
82  float v1;
83  float tw1;
84  float u2;
85  float v2;
86  float tw2;
87  float u3;
88  float v3;
89  float tw3;
90  };
91  struct vec2 t[3];
92  };
93  unsigned int color;
94 };
95 
97 struct mesh {
98  struct triangle *tris;
99  int count;
100 };
101 
103 struct mat4x4 {
104  float m[4][4];
105 };
106 
108 void
109 Vec3Debug(struct vec3 vec3, char *name);
110 
111 struct vec3
112 Vec3Init(float x, float y, float z);
113 
114 float
115 Vec3DotProduct(struct vec3 left, struct vec3 right);
116 
117 struct vec3
118 Vec3CrossProduct(struct vec3 left, struct vec3 right);
119 
120 struct vec3
121 Vec3Normalize(struct vec3 vec3);
122 
123 struct vec3
124 Vec3Add(struct vec3 left, struct vec3 right);
125 
126 struct vec3
127 Vec3Subtract(struct vec3 minuend, struct vec3 subtrahend);
128 
129 struct vec3
130 Vec3Multiply(struct vec3 vec3, float f);
131 
132 struct vec3
133 Vec3Divide(struct vec3 vec3, float f);
134 
142 struct vec3
143 Vec3IntersectPlane(struct vec3 plane, struct vec3 normal, struct vec3 lineStart, struct vec3 lineEnd, float *t);
144 
146 struct triangle TriangleInit(
147  float x1, float y1, float z1,
148  float u1, float v1,
149  float x2, float y2, float z2,
150  float u2, float v2,
151  float x3, float y3, float z3,
152  float u3, float v3);
153 
167 int
168 TriangleClipAgainstPlane(struct vec3 plane, struct vec3 normal, struct triangle in, struct triangle *out1, struct triangle *out2);
169 
171 void
172 TriangleDebug(struct triangle triangle, char *name);
173 
181 struct mesh *
182 MeshInit(int numTris);
183 
194 struct mesh *
195 MeshInitFromObj(char *objFile);
196 
202 void
203 MeshDeinit(struct mesh *mesh);
204 
205 struct vec3
206 Mat4x4MultiplyVec3(struct mat4x4 mat, struct vec3 vec);
207 
208 struct mat4x4
209 Mat4x4Identity();
210 
211 struct mat4x4
212 Mat4x4RotateX(float rad);
213 
214 struct mat4x4
215 Mat4x4RotateY(float rad);
216 
217 struct mat4x4
218 Mat4x4RotateZ(float rad);
219 
220 struct mat4x4
221 Mat4x4Translate(float x, float y, float z);
222 
223 struct mat4x4
224 Mat4x4Project(float fovDegrees, float aspect, float near, float far);
225 
226 struct mat4x4
227 Mat4x4Multiply(struct mat4x4 left, struct mat4x4 right);
228 
230 struct mat4x4
231 Mat4x4InvertFast(struct mat4x4 matrix);
232 
233 struct mat4x4
234 Mat4x4PointAt(struct vec3 pos, struct vec3 target, struct vec3 up);
235 
237 void
238 Mat4x4Debug(struct mat4x4 mat, char *name);
239 
240 #endif // MATH_VERSION
void Vec3Debug(struct vec3 vec3, char *name)
Prints debug information about the homogenous 3D vector.
Triangular mesh face.
Definition: math.h:61
struct mesh * MeshInitFromObj(char *objFile)
Initialize a new mesh object from an obj file.
Definition: math.c:326
Homogenous 3D coordinates.
Definition: math.h:42
RGBA color quad.
Definition: color.h:30
void Mat4x4Debug(struct mat4x4 mat, char *name)
Prints debug information about the 4x4 matrix.
Definition: math.c:578
void TriangleDebug(struct triangle triangle, char *name)
Prints debug information about the triangle face.
Homogenous 2D coordinates.
Definition: math.h:27
A collection of triangles representing some kind of 3D model.
Definition: math.h:97
A 3D matrix using homogenous coordinates.
Definition: math.h:103
struct mesh * MeshInit(int numTris)
Initialize a new mesh object.
Definition: math.c:314
int TriangleClipAgainstPlane(struct vec3 plane, struct vec3 normal, struct triangle in, struct triangle *out1, struct triangle *out2)
Clip a triangle against a plane.
Definition: math.c:191
void MeshDeinit(struct mesh *mesh)
De-initializes the mesh object.
Definition: math.c:440