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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/*
* kmscon - GL Math Helpers
*
* Copyright (c) 2011 David Herrmann <dh.herrmann@googlemail.com>
* Copyright (c) 2011 University of Tuebingen
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* Math Helper
* Basic linear algebra and a matrix stack helper.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "static_gl.h"
struct gl_m4_entry {
struct gl_m4_entry *next;
float matrix[16];
};
struct gl_m4_stack {
struct gl_m4_entry stack;
struct gl_m4_entry *cache;
};
void gl_m4_identity(float *m)
{
if (!m)
return;
m[0] = 1;
m[1] = 0;
m[2] = 0;
m[3] = 0;
m[4] = 0;
m[5] = 1;
m[6] = 0;
m[7] = 0;
m[8] = 0;
m[9] = 0;
m[10] = 1;
m[11] = 0;
m[12] = 0;
m[13] = 0;
m[14] = 0;
m[15] = 1;
}
void gl_m4_copy(float *dest, const float *src)
{
if (!dest || !src)
return;
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest[3] = src[3];
dest[4] = src[4];
dest[5] = src[5];
dest[6] = src[6];
dest[7] = src[7];
dest[8] = src[8];
dest[9] = src[9];
dest[10] = src[10];
dest[11] = src[11];
dest[12] = src[12];
dest[13] = src[13];
dest[14] = src[14];
dest[15] = src[15];
}
/* Matrix Multiplication
* This is the simplest algorithm to multiply two matrices. It computes:
* dest = n * m
* That is, n is left side and m is right side.
*
* Matrix-multiplication is heavy, avoid it if possible.
*/
void gl_m4_mult_dest(float *dest, const float *n, const float *m)
{
unsigned int row, col, j;
if (!dest || !n || !m)
return;
for (row = 0; row < 4; ++row) {
for (col = 0; col < 4; ++col) {
dest[row * 4 + col] = 0;
for (j = 0; j < 4; ++j)
dest[row * 4 + col] +=
n[row * 4 + j] * m[j * 4 + col];
}
}
}
/* this computes n = n * m */
void gl_m4_mult(float *n, const float *m)
{
float tmp[16];
if (!n || !m)
return;
gl_m4_mult_dest(tmp, n, m);
gl_m4_copy(n, tmp);
}
void gl_m4_translate(float *m, float x, float y, float z)
{
float trans[16] = { 1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1 };
if (!m)
return;
gl_m4_mult(m, trans);
}
void gl_m4_scale(float *m, float x, float y, float z)
{
float scale[16] = { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 };
if (!m)
return;
gl_m4_mult(m, scale);
}
void gl_m4_transpose_dest(float *dest, const float *src)
{
if (!dest || !src)
return;
dest[0] = src[0];
dest[5] = src[5];
dest[10] = src[10];
dest[15] = src[15];
dest[1] = src[4];
dest[4] = src[1];
dest[8] = src[2];
dest[2] = src[8];
dest[3] = src[12];
dest[12] = src[3];
dest[7] = src[13];
dest[13] = src[7];
dest[11] = src[14];
dest[14] = src[11];
dest[6] = src[9];
dest[9] = src[6];
}
void gl_m4_transpose(float *m)
{
float tmp;
if (!m)
return;
tmp = m[1];
m[1] = m[4];
m[4] = tmp;
tmp = m[8];
m[8] = m[2];
m[2] = tmp;
tmp = m[3];
m[3] = m[12];
m[12] = tmp;
tmp = m[7];
m[7] = m[13];
m[13] = tmp;
tmp = m[11];
m[11] = m[14];
m[14] = tmp;
tmp = m[6];
m[6] = m[9];
m[9] = tmp;
}
int gl_m4_stack_new(struct gl_m4_stack **out)
{
struct gl_m4_stack *stack;
if (!out)
return -EINVAL;
stack = malloc(sizeof(*stack));
if (!stack)
return -ENOMEM;
memset(stack, 0, sizeof(*stack));
gl_m4_identity(stack->stack.matrix);
*out = stack;
return 0;
}
void gl_m4_stack_free(struct gl_m4_stack *stack)
{
struct gl_m4_entry *tmp;
if (!stack)
return;
while (stack->stack.next) {
tmp = stack->stack.next;
stack->stack.next = tmp->next;
free(tmp);
}
while (stack->cache) {
tmp = stack->cache;
stack->cache = tmp->next;
free(tmp);
}
free(stack);
}
float *gl_m4_stack_push(struct gl_m4_stack *stack)
{
struct gl_m4_entry *entry;
if (stack->cache) {
entry = stack->cache;
stack->cache = entry->next;
} else {
entry = malloc(sizeof(*entry));
if (!entry)
return NULL;
}
gl_m4_copy(entry->matrix, stack->stack.matrix);
entry->next = stack->stack.next;
stack->stack.next = entry;
return stack->stack.matrix;
}
float *gl_m4_stack_pop(struct gl_m4_stack *stack)
{
struct gl_m4_entry *entry;
if (!stack)
return NULL;
entry = stack->stack.next;
if (!entry) {
gl_m4_identity(stack->stack.matrix);
return stack->stack.matrix;
}
stack->stack.next = entry->next;
entry->next = stack->cache;
stack->cache = entry;
gl_m4_copy(stack->stack.matrix, entry->matrix);
return stack->stack.matrix;
}
float *gl_m4_stack_tip(struct gl_m4_stack *stack)
{
if (!stack)
return NULL;
return stack->stack.matrix;
}
|