blob: c71471f1047b5483b8939e0be69fb3bccad20875 [file] [log] [blame]
Pekka Paalanen668ca372012-01-12 14:30:47 +02001/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#include <string.h>
24#include <stdlib.h>
25#include <GLES2/gl2.h>
26#include <wayland-server.h>
27
28#include "matrix.h"
29
30/*
31 * Matrices are stored in column-major order, that is the array indices are:
32 * 0 4 8 12
33 * 1 5 9 13
34 * 2 6 10 14
35 * 3 7 11 15
36 */
37
38WL_EXPORT void
39weston_matrix_init(struct weston_matrix *matrix)
40{
41 static const struct weston_matrix identity = {
42 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
43 };
44
45 memcpy(matrix, &identity, sizeof identity);
46}
47
48/* m <- n * m, that is, m is multiplied on the LEFT. */
49WL_EXPORT void
50weston_matrix_multiply(struct weston_matrix *m, const struct weston_matrix *n)
51{
52 struct weston_matrix tmp;
53 const GLfloat *row, *column;
54 div_t d;
55 int i, j;
56
57 for (i = 0; i < 16; i++) {
58 tmp.d[i] = 0;
59 d = div(i, 4);
60 row = m->d + d.quot * 4;
61 column = n->d + d.rem;
62 for (j = 0; j < 4; j++)
63 tmp.d[i] += row[j] * column[j * 4];
64 }
65 memcpy(m, &tmp, sizeof tmp);
66}
67
68WL_EXPORT void
69weston_matrix_translate(struct weston_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
70{
71 struct weston_matrix translate = {
72 { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 }
73 };
74
75 weston_matrix_multiply(matrix, &translate);
76}
77
78WL_EXPORT void
79weston_matrix_scale(struct weston_matrix *matrix, GLfloat x, GLfloat y, GLfloat z)
80{
81 struct weston_matrix scale = {
82 { x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 }
83 };
84
85 weston_matrix_multiply(matrix, &scale);
86}
87
88/* v <- m * v */
89WL_EXPORT void
90weston_matrix_transform(struct weston_matrix *matrix, struct weston_vector *v)
91{
92 int i, j;
93 struct weston_vector t;
94
95 for (i = 0; i < 4; i++) {
96 t.f[i] = 0;
97 for (j = 0; j < 4; j++)
98 t.f[i] += v->f[j] * matrix->d[i + j * 4];
99 }
100
101 *v = t;
102}