2 * kmscon - GL Math Helpers
4 * Copyright (c) 2011 David Herrmann <dh.herrmann@googlemail.com>
5 * Copyright (c) 2011 University of Tuebingen
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files
9 * (the "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Basic linear algebra and a matrix stack helper.
35 #include "static_gl.h"
38 struct gl_m4_entry *next;
43 struct gl_m4_entry stack;
44 struct gl_m4_entry *cache;
47 void gl_m4_identity(float *m)
73 void gl_m4_copy(float *dest, const float *src)
99 /* Matrix Multiplication
100 * This is the simplest algorithm to multiply two matrices. It computes:
102 * That is, n is left side and m is right side.
104 * Matrix-multiplication is heavy, avoid it if possible.
106 void gl_m4_mult_dest(float *dest, const float *n, const float *m)
108 unsigned int row, col, j;
110 if (!dest || !n || !m)
113 for (row = 0; row < 4; ++row) {
114 for (col = 0; col < 4; ++col) {
115 dest[row * 4 + col] = 0;
116 for (j = 0; j < 4; ++j)
117 dest[row * 4 + col] +=
118 n[row * 4 + j] * m[j * 4 + col];
123 /* this computes n = n * m */
124 void gl_m4_mult(float *n, const float *m)
131 gl_m4_mult_dest(tmp, n, m);
135 void gl_m4_translate(float *m, float x, float y, float z)
137 float trans[16] = { 1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1 };
142 gl_m4_mult(m, trans);
145 void gl_m4_scale(float *m, float x, float y, float z)
147 float scale[16] = { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 };
152 gl_m4_mult(m, scale);
155 void gl_m4_transpose_dest(float *dest, const float *src)
184 void gl_m4_transpose(float *m)
216 int gl_m4_stack_new(struct gl_m4_stack **out)
218 struct gl_m4_stack *stack;
223 stack = malloc(sizeof(*stack));
227 memset(stack, 0, sizeof(*stack));
228 gl_m4_identity(stack->stack.matrix);
234 void gl_m4_stack_free(struct gl_m4_stack *stack)
236 struct gl_m4_entry *tmp;
241 while (stack->stack.next) {
242 tmp = stack->stack.next;
243 stack->stack.next = tmp->next;
247 while (stack->cache) {
249 stack->cache = tmp->next;
256 float *gl_m4_stack_push(struct gl_m4_stack *stack)
258 struct gl_m4_entry *entry;
261 entry = stack->cache;
262 stack->cache = entry->next;
264 entry = malloc(sizeof(*entry));
269 gl_m4_copy(entry->matrix, stack->stack.matrix);
270 entry->next = stack->stack.next;
271 stack->stack.next = entry;
273 return stack->stack.matrix;
276 float *gl_m4_stack_pop(struct gl_m4_stack *stack)
278 struct gl_m4_entry *entry;
283 entry = stack->stack.next;
285 gl_m4_identity(stack->stack.matrix);
286 return stack->stack.matrix;
289 stack->stack.next = entry->next;
290 entry->next = stack->cache;
291 stack->cache = entry;
293 gl_m4_copy(stack->stack.matrix, entry->matrix);
295 return stack->stack.matrix;
298 float *gl_m4_stack_tip(struct gl_m4_stack *stack)
303 return stack->stack.matrix;