wlroots-hyprland/types/wlr_matrix.c

170 lines
4 KiB
C
Raw Normal View History

#include <math.h>
2018-02-12 21:29:23 +01:00
#include <string.h>
2017-08-06 11:38:40 +02:00
#include <wayland-server-protocol.h>
2018-03-15 09:11:03 +01:00
#include <wlr/types/wlr_matrix.h>
2018-01-21 21:53:29 +01:00
#include <wlr/types/wlr_box.h>
#include <wlr/types/wlr_output.h>
2018-03-15 15:33:58 +01:00
void wlr_matrix_identity(float mat[static 9]) {
static const float identity[9] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
};
memcpy(mat, identity, sizeof(identity));
}
2018-03-15 15:33:58 +01:00
void wlr_matrix_multiply(float mat[static 9], const float a[static 9],
const float b[static 9]) {
float product[9];
product[0] = a[0]*b[0] + a[1]*b[3] + a[2]*b[6];
product[1] = a[0]*b[1] + a[1]*b[4] + a[2]*b[7];
product[2] = a[0]*b[2] + a[1]*b[5] + a[2]*b[8];
product[3] = a[3]*b[0] + a[4]*b[3] + a[5]*b[6];
product[4] = a[3]*b[1] + a[4]*b[4] + a[5]*b[7];
product[5] = a[3]*b[2] + a[4]*b[5] + a[5]*b[8];
product[6] = a[6]*b[0] + a[7]*b[3] + a[8]*b[6];
product[7] = a[6]*b[1] + a[7]*b[4] + a[8]*b[7];
product[8] = a[6]*b[2] + a[7]*b[5] + a[8]*b[8];
memcpy(mat, product, sizeof(product));
}
void wlr_matrix_transpose(float mat[static 9], const float a[static 9]) {
float transposition[9] = {
a[0], a[3], a[6],
a[1], a[4], a[7],
a[2], a[5], a[8],
};
memcpy(mat, transposition, sizeof(transposition));
}
2018-03-15 15:33:58 +01:00
void wlr_matrix_translate(float mat[static 9], float x, float y) {
float translate[9] = {
1.0f, 0.0f, x,
0.0f, 1.0f, y,
0.0f, 0.0f, 1.0f,
};
wlr_matrix_multiply(mat, mat, translate);
}
2018-03-15 15:33:58 +01:00
void wlr_matrix_scale(float mat[static 9], float x, float y) {
float scale[9] = {
x, 0.0f, 0.0f,
0.0f, y, 0.0f,
0.0f, 0.0f, 1.0f,
};
wlr_matrix_multiply(mat, mat, scale);
}
2018-03-15 15:33:58 +01:00
void wlr_matrix_rotate(float mat[static 9], float rad) {
float rotate[9] = {
cos(rad), -sin(rad), 0.0f,
sin(rad), cos(rad), 0.0f,
0.0f, 0.0f, 1.0f,
};
2018-03-15 15:33:58 +01:00
wlr_matrix_multiply(mat, mat, rotate);
}
2017-08-06 11:38:40 +02:00
2018-03-15 15:33:58 +01:00
static const float transforms[][9] = {
2017-08-06 11:38:40 +02:00
[WL_OUTPUT_TRANSFORM_NORMAL] = {
2018-03-15 15:33:58 +01:00
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
2017-08-06 11:38:40 +02:00
},
[WL_OUTPUT_TRANSFORM_90] = {
0.0f, 1.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
2018-03-15 15:33:58 +01:00
0.0f, 0.0f, 1.0f,
2017-08-06 11:38:40 +02:00
},
[WL_OUTPUT_TRANSFORM_180] = {
2018-03-15 15:33:58 +01:00
-1.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
2017-08-06 11:38:40 +02:00
},
[WL_OUTPUT_TRANSFORM_270] = {
0.0f, -1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
2018-03-15 15:33:58 +01:00
0.0f, 0.0f, 1.0f,
2017-08-06 11:38:40 +02:00
},
[WL_OUTPUT_TRANSFORM_FLIPPED] = {
2018-03-15 15:33:58 +01:00
-1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
2017-08-06 11:38:40 +02:00
},
[WL_OUTPUT_TRANSFORM_FLIPPED_90] = {
0.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
2018-03-15 15:33:58 +01:00
0.0f, 0.0f, 1.0f,
2017-08-06 11:38:40 +02:00
},
[WL_OUTPUT_TRANSFORM_FLIPPED_180] = {
2018-03-15 15:33:58 +01:00
1.0f, 0.0f, 0.0f,
0.0f, -1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
2017-08-06 11:38:40 +02:00
},
[WL_OUTPUT_TRANSFORM_FLIPPED_270] = {
0.0f, -1.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
2018-03-15 15:33:58 +01:00
0.0f, 0.0f, 1.0f,
2017-08-06 11:38:40 +02:00
},
};
2018-03-15 15:33:58 +01:00
void wlr_matrix_transform(float mat[static 9],
2017-11-30 23:58:12 +01:00
enum wl_output_transform transform) {
2018-03-15 15:33:58 +01:00
wlr_matrix_multiply(mat, mat, transforms[transform]);
2017-11-30 23:58:12 +01:00
}
2018-05-03 22:59:43 +02:00
// Equivalent to glOrtho(0, width, 0, height, 1, -1) with the transform applied
void wlr_matrix_projection(float mat[static 9], int width, int height,
2017-08-06 11:38:40 +02:00
enum wl_output_transform transform) {
2018-03-15 15:33:58 +01:00
memset(mat, 0, sizeof(*mat) * 9);
2017-08-06 11:38:40 +02:00
const float *t = transforms[transform];
float x = 2.0f / width;
float y = 2.0f / height;
// Rotation + reflection
2017-08-06 11:38:40 +02:00
mat[0] = x * t[0];
mat[1] = x * t[1];
2018-03-15 15:33:58 +01:00
mat[3] = y * -t[3];
mat[4] = y * -t[4];
2017-08-06 11:38:40 +02:00
// Translation
2018-03-15 15:33:58 +01:00
mat[2] = -copysign(1.0f, mat[0] + mat[1]);
mat[5] = -copysign(1.0f, mat[3] + mat[4]);
2017-08-06 11:38:40 +02:00
// Identity
2018-03-15 15:33:58 +01:00
mat[8] = 1.0f;
2017-08-06 11:38:40 +02:00
}
2018-01-21 21:53:29 +01:00
2018-03-15 15:33:58 +01:00
void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box,
2018-01-22 01:03:10 +01:00
enum wl_output_transform transform, float rotation,
2018-03-15 15:33:58 +01:00
const float projection[static 9]) {
2018-01-21 21:53:29 +01:00
int x = box->x;
int y = box->y;
int width = box->width;
int height = box->height;
2018-03-15 15:33:58 +01:00
wlr_matrix_identity(mat);
wlr_matrix_translate(mat, x, y);
2018-01-21 21:53:29 +01:00
if (rotation != 0) {
2018-03-15 15:33:58 +01:00
wlr_matrix_translate(mat, width/2, height/2);
wlr_matrix_rotate(mat, rotation);
wlr_matrix_translate(mat, -width/2, -height/2);
}
2018-01-21 21:53:29 +01:00
2018-03-15 15:33:58 +01:00
wlr_matrix_scale(mat, width, height);
2018-01-21 21:53:29 +01:00
if (transform != WL_OUTPUT_TRANSFORM_NORMAL) {
2018-03-15 15:33:58 +01:00
wlr_matrix_translate(mat, 0.5, 0.5);
wlr_matrix_transform(mat, transform);
wlr_matrix_translate(mat, -0.5, -0.5);
2018-01-21 21:53:29 +01:00
}
2018-01-22 01:03:10 +01:00
2018-03-15 15:33:58 +01:00
wlr_matrix_multiply(mat, projection, mat);
2018-01-21 21:53:29 +01:00
}