-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.cpp
267 lines (247 loc) · 7.66 KB
/
matrix.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <stdio.h> //deal with texture loading at linux.c
#include "softrendr.hpp"
#include "include/linux.hpp"
#include "include/types.hpp"
#include "include/asmmath.hpp"
#include "include/keyboard.hpp"
#include "include/camera.hpp"
#include "include/constants.hpp"
#include "include/matrix.hpp"
#include <stddef.h>
#include <algorithm>
#include <limits.h>
#include <SDL2/SDL.h>
#include <iostream>
#include <cassert>
//https://theailearner.com/2020/11/04/affine-transformation/
vec2 mulm3andv2(matrix3x3 m, vec2 v)
{
/* x y 1
[x] [a][b][c] [ax+by+c] [ret.x/z]
[y] [d][e][f] = [dx+ey+f] = [ret.y/z]
[1] [g][h][i] [gx+hy+i]
*/
vec2 ret(0, 0);
scalar z;
ret.x = m[0][0] * v.x + m[0][1] * v.y + m[0][2];
ret.y = m[1][0] * v.x + m[1][1] * v.y + m[1][2];
z = m[2][0] * v.x + m[2][1] * v.y + m[2][2];
// normalize (convert from homogeneous to Cartesian coordinates)
ret.x = ret.x / z;
ret.y = ret.y / z;
return ret;
}
vec3 mulm4x4andv3(matrix4x4 M, vec3 in) //SHITTT understood! pointer magic!!!!!!!
{
//out = in * M;
vec3 out = vec3(1, 1, 1);
out.x = in.x * M[0][0] + in.y * M[1][0] + in.z * M[2][0] + M[3][0];
out.y = in.x * M[0][1] + in.y * M[1][1] + in.z * M[2][1] + M[3][1];
out.z = in.x * M[0][2] + in.y * M[1][2] + in.z * M[2][2] + M[3][2];
scalar w = in.x * M[0][3] + in.y * M[1][3] + in.z * M[2][3] + M[3][3];
// normalize if w is different than 1 (convert from homogeneous to Cartesian coordinates)
if (w != 1 && w != 0)
{
out.x /= w;
out.y /= w;
out.z /= w;
}
return out;
}
void mulm4x4(matrix4x4 *a, matrix4x4 *b, matrix4x4 *result)
{
for (int x = 0; x < 4; x++)
{
for (int y = 0; y < 4; y++)
{
(*result)[y][x] =
(*a)[y][0] * (*b)[0][x] +
(*a)[y][1] * (*b)[1][x] +
(*a)[y][2] * (*b)[2][x] +
(*a)[y][3] * (*b)[3][x];
} //just accept that how it is. it works 99.9%
}
}
void Identitym4x4(matrix4x4 *in)
{
matrix4x4 id = {
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}};
memcpy(in[0][0], &id[0][0], 4 * 4 * sizeof(id[0][0]));
}
//camera shit goes here
//FOV IS IN RADIANS!
void setProjectionMatrix(const float &FOV, const float &near, const float &far, matrix4x4 *M)
{
Identitym4x4(M); //clears all shit before
float scale = 1 / tan(FOV);
(*M)[0][0] = scale;
(*M)[1][1] = scale;
(*M)[2][2] = -far / (far - near);
(*M)[2][3] = -(far + near) / (far - near);
//this works. TESTED!
}
void rotatev3(vec3 *v, vec3 angle)
{
/*
[cos,-sin, 0, 0] rotateZ
[sin, cos, 0, 0]
[ 0 , 0 , 1, 0]
[ 0 , 0 , 0, 1]
[ cos, 0 ,sin, 0] rotateY
[ 0 , 1 , 0 , 0]
[-sin, 0 ,cos, 0]
[ 0 , 0 , 0 , 1 ]
[ 1 , 0 , 0 , 0] rotateX
[ 0,cos,-sin, 0]
[ 0,sin, cos, 0]
[ 0 , 0 , 0 , 1]
*/
matrix4x4 temp;
matrix4x4 temp2;
matrix4x4 temp3;
matrix4x4 rotx =
{
{1, 0, 0, 0},
{0, asmmath_cos(angle.x), -asmmath_sin(angle.x), 0},
{0, asmmath_sin(angle.x), asmmath_cos(angle.x), 0},
{0, 0, 0, 1}};
matrix4x4 roty =
{
{asmmath_cos(angle.y), 0, asmmath_sin(angle.y), 0},
{0, 1, 0, 0},
{-asmmath_sin(angle.y), 0, asmmath_cos(angle.y), 0},
{0, 0, 0, 1}};
matrix4x4 rotz =
{
{asmmath_cos(angle.z), -asmmath_sin(angle.z), 0, 0},
{asmmath_sin(angle.z), asmmath_cos(angle.z), 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}};
matrix4x4 M;
Identitym4x4(&M);
mulm4x4(&M, &rotx, &temp);
mulm4x4(&temp, &roty, &temp2);
mulm4x4(&temp2, &rotz, &temp3);
vec3 a = mulm4x4andv3(temp3, *v);
memcpy(v, &a, sizeof(vec3));
}
void rotate4x4(matrix4x4 *M, vec3 angle)
{
//in progress
/*
[cos,-sin, 0, 0] rotateZ
[sin, cos, 0, 0]
[ 0 , 0 , 1, 0]
[ 0 , 0 , 0, 1]
[ cos, 0 ,sin, 0] rotateY
[ 0 , 1 , 0 , 0]
[-sin, 0 ,cos, 0]
[ 0 , 0 , 0 , 1 ]
[ 1 , 0 , 0 , 0] rotateX
[ 0,cos,-sin, 0]
[ 0,sin, cos, 0]
[ 0 , 0 , 0 , 1]
*/
matrix4x4 temp;
matrix4x4 temp2;
matrix4x4 temp3;
matrix4x4 rotx =
{
{1, 0, 0, 0},
{0, asmmath_cos(angle.x), -asmmath_sin(angle.x), 0},
{0, asmmath_sin(angle.x), asmmath_cos(angle.x), 0},
{0, 0, 0, 1}};
matrix4x4 roty =
{
{asmmath_cos(angle.y), 0, asmmath_sin(angle.y), 0},
{0, 1, 0, 0},
{-asmmath_sin(angle.y), 0, asmmath_cos(angle.y), 0},
{0, 0, 0, 1}};
matrix4x4 rotz =
{
{asmmath_cos(angle.z), -asmmath_sin(angle.z), 0, 0},
{asmmath_sin(angle.z), asmmath_cos(angle.z), 0, 0},
{0, 0, 1, 0},
{0, 0, 0, 1}};
mulm4x4(M, &rotx, &temp); mulm4x4(&temp, &roty, &temp2);
mulm4x4(&temp2, &rotz, &temp3);
memcpy(M[0][0], &temp3[0][0], 4 * 4 * sizeof(temp[0][0]));
}
//move for GLOBAL coords. for LOCAL (CAMERA for example) use translate4x4!
void move4x4(matrix4x4 *M, vec3 pos)
{
/*
[ 1 , 0 , 0 , X ]
[ 0 , 1 , 0 , Y ]
[ 0 , 0 , 1 , Z ]
[ 0 , 0 , 0 , 1 ]
*/
matrix4x4 temp;
matrix4x4 trns =
{
{1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, 1, 0},
{pos.x, pos.y, pos.z, 1}};
mulm4x4(&trns, M, &temp);
memcpy(M, &temp, 4 * 4 * sizeof(scalar));
}
void translate4x4(matrix4x4 *M,vec3 pos){
scalar xDot = pos.x * (*M)[0][0] +
pos.y * (*M)[1][0] +
pos.z * (*M)[2][0];
scalar yDot = pos.x * (*M)[0][1] +
pos.y * (*M)[1][1] +
pos.z * (*M)[2][1];
scalar zDot = pos.x * (*M)[0][2] +
pos.y * (*M)[1][2] +
pos.z * (*M)[2][2];
(*M)[3][0] = xDot;
(*M)[3][1] = -yDot;
(*M)[3][2] = zDot;
}
//this shit also wants to be free(texture.raw). be democratic to malloc.
texturexywh matrix3x3Img(texturewh image, matrix3x3 m)
{
scalar minx = __DBL_MAX__, miny = __DBL_MAX__, maxx = __DBL_MIN__, maxy = __DBL_MIN__;
vec2 corners[] = {
mulm3andv2(m, vec2(0, 0)), //corners of image.
mulm3andv2(m, vec2(image.width, 0)), //calculate it at first
mulm3andv2(m, vec2(0, image.height)), //to get min and max
mulm3andv2(m, vec2(image.width, image.height)) //of result image
};
for (int i = 0; i < 4; i++)
{
minx = asmmath_min(corners[i].x, minx);
miny = asmmath_min(corners[i].y, miny);
maxx = asmmath_max(corners[i].x, maxx);
maxy = asmmath_max(corners[i].y, maxy);
}
unsigned int outwidth = asmmath_floor(maxx - minx + 0.5); //+0.5 makes round from floor
unsigned int outheight = asmmath_floor(maxy - miny + 0.5);
texturexywh texture;
texture.raw = new unsigned int[outwidth * outheight]; //(unsigned int *)malloc(4 * (outwidth*outheight));
texture.width = outwidth;
texture.height = outheight;
texture.x = minx;
texture.y = miny;
memset(texture.raw, 0x0, outwidth * outheight * 4);
for (unsigned int x = 0; x < image.width; x++)
{
for (unsigned int y = 0; y < image.height; y++)
{
vec2 pos = mulm3andv2(m, vec2(x, y));
pos = pos + vec2(-minx, -miny);
unsigned int px = (unsigned int)asmmath_floor(pos.x + 0.5);
unsigned int py = (unsigned int)asmmath_floor(pos.y + 0.5);
if ((x >= 0 && y >= 0) && (x < image.width && y < image.height))
{
texture.raw[px + py * outwidth] = image.raw[x + y * image.width];
}
}
}
return texture;
}