From 53dd622263cb2b952a148463cfb046ba084d57ac Mon Sep 17 00:00:00 2001 From: Scott Anderson Date: Wed, 7 Jun 2017 16:43:57 +1200 Subject: Provide user with transformation matrix --- example/rotation.c | 56 ++++++++------------------------------------ include/wlr/types.h | 2 ++ types/wlr_output.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 77 insertions(+), 48 deletions(-) diff --git a/example/rotation.c b/example/rotation.c index 90b0ab9b..d19af224 100644 --- a/example/rotation.c +++ b/example/rotation.c @@ -16,13 +16,13 @@ static const GLchar vert_src[] = "#version 310 es\n" "precision mediump float;\n" -"layout(location = 0) uniform mat2 transform;\n" +"layout(location = 0) uniform mat4 transform;\n" "layout(location = 0) in vec2 pos;\n" "layout(location = 1) in float angle;\n" "out float v_angle;\n" "void main() {\n" " v_angle = angle;\n" -" gl_Position = vec4(transform * pos, 0.0, 1.0);\n" +" gl_Position = transform * vec4(pos, 0.0, 1.0);\n" "}\n"; static const GLchar frag_src[] = @@ -38,41 +38,6 @@ static const GLchar frag_src[] = " 1.0);\n" "}\n"; -static const GLfloat transforms[][4] = { - [WL_OUTPUT_TRANSFORM_NORMAL] = { - 1.0f, 0.0f, - 0.0f, 1.0f, - }, - [WL_OUTPUT_TRANSFORM_90] = { - 0.0f, -1.0f, - 1.0f, 0.0f, - }, - [WL_OUTPUT_TRANSFORM_180] = { - -1.0f, 0.0f, - 0.0f, -1.0f, - }, - [WL_OUTPUT_TRANSFORM_270] = { - 0.0f, 1.0f, - -1.0f, 0.0f, - }, - [WL_OUTPUT_TRANSFORM_FLIPPED] = { - -1.0f, 0.0f, - 0.0f, 1.0f, - }, - [WL_OUTPUT_TRANSFORM_FLIPPED_90] = { - 0.0f, 1.0f, - 1.0f, 0.0f, - }, - [WL_OUTPUT_TRANSFORM_FLIPPED_180] = { - 1.0f, 0.0f, - 0.0f, -1.0f, - }, - [WL_OUTPUT_TRANSFORM_FLIPPED_270] = { - 0.0f, -1.0f, - -1.0f, 0.0f, - }, -}; - struct state { float angle; struct timespec last_frame; @@ -172,8 +137,8 @@ static void output_frame(struct wl_listener *listener, void *data) { struct output_state *ostate = wl_container_of(listener, ostate, frame); struct state *s = ostate->state; - float width = ostate->output->width; - float height = ostate->output->height; + int32_t width = ostate->output->width; + int32_t height = ostate->output->height; glViewport(0, 0, width, height); // All of the odd numbered transformations involve a 90 or 270 degree rotation @@ -183,14 +148,13 @@ static void output_frame(struct wl_listener *listener, void *data) { height = tmp; } - // Single pixel in gl coordinates - float px_w = 2.0f / width; - float px_h = 2.0f / height; + int32_t mid_w = width / 2; + int32_t mid_h = height / 2; GLfloat vert_data[] = { - -250 * px_w, -250 * px_h, s->angle, - 0 * px_w, +250 * px_h, s->angle + 2.0944f, // 120 deg - +250 * px_w, -250 * px_h, s->angle + 4.18879f, // 240 deg + mid_w - 200, mid_h - 200, s->angle, + mid_w, mid_h + 200, s->angle + M_PI * 2.0f / 3.0f, // 120 deg + mid_w + 200, mid_h - 200, s->angle + M_PI * 4.0f / 3.0f, // 240 deg }; glUseProgram(s->gl.prog); @@ -199,7 +163,7 @@ static void output_frame(struct wl_listener *listener, void *data) { glBindBuffer(GL_ARRAY_BUFFER, s->gl.vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vert_data), vert_data, GL_STATIC_DRAW); - glUniformMatrix2fv(0, 1, GL_FALSE, transforms[ostate->output->transform]); + glUniformMatrix4fv(0, 1, GL_TRUE, ostate->output->transform_matrix); glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); diff --git a/include/wlr/types.h b/include/wlr/types.h index 5de80a60..27819f56 100644 --- a/include/wlr/types.h +++ b/include/wlr/types.h @@ -32,6 +32,8 @@ struct wlr_output { int32_t subpixel; // enum wl_output_subpixel int32_t transform; // enum wl_output_transform + float transform_matrix[16]; + list_t *modes; struct wlr_output_mode *current_mode; diff --git a/types/wlr_output.c b/types/wlr_output.c index 00975c3a..5cc8d0a4 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -1,9 +1,70 @@ #include +#include +#include #include #include #include #include "types.h" +static const float transforms[][4] = { + [WL_OUTPUT_TRANSFORM_NORMAL] = { + 1.0f, 0.0f, + 0.0f, 1.0f, + }, + [WL_OUTPUT_TRANSFORM_90] = { + 0.0f, -1.0f, + 1.0f, 0.0f, + }, + [WL_OUTPUT_TRANSFORM_180] = { + -1.0f, 0.0f, + 0.0f, -1.0f, + }, + [WL_OUTPUT_TRANSFORM_270] = { + 0.0f, 1.0f, + -1.0f, 0.0f, + }, + [WL_OUTPUT_TRANSFORM_FLIPPED] = { + -1.0f, 0.0f, + 0.0f, 1.0f, + }, + [WL_OUTPUT_TRANSFORM_FLIPPED_90] = { + 0.0f, 1.0f, + 1.0f, 0.0f, + }, + [WL_OUTPUT_TRANSFORM_FLIPPED_180] = { + 1.0f, 0.0f, + 0.0f, -1.0f, + }, + [WL_OUTPUT_TRANSFORM_FLIPPED_270] = { + 0.0f, -1.0f, + -1.0f, 0.0f, + }, +}; + +// Equivilent to glOrtho(0, width, 0, height, 1, -1) with the transform applied +static void set_matrix(float mat[static 16], int32_t width, int32_t height, + enum wl_output_transform transform) { + memset(mat, 0, sizeof(*mat) * 16); + + const float *t = transforms[transform]; + float x = 2.0f / width; + float y = 2.0f / height; + + // Rotation + relection + mat[0] = x * t[0]; + mat[1] = x * t[1]; + mat[4] = y * t[2]; + mat[5] = y * t[3]; + + // Translation + mat[3] = -copysign(1.0f, mat[0] + mat[1]); + mat[7] = -copysign(1.0f, mat[4] + mat[5]); + + // Identity + mat[10] = 1.0f; + mat[15] = 1.0f; +} + struct wlr_output *wlr_output_create(struct wlr_output_impl *impl, struct wlr_output_state *state) { struct wlr_output *output = calloc(1, sizeof(struct wlr_output)); @@ -20,19 +81,21 @@ void wlr_output_enable(struct wlr_output *output, bool enable) { } bool wlr_output_set_mode(struct wlr_output *output, struct wlr_output_mode *mode) { + set_matrix(output->transform_matrix, mode->width, mode->height, output->transform); return output->impl->set_mode(output->state, mode); } void wlr_output_transform(struct wlr_output *output, enum wl_output_transform transform) { + set_matrix(output->transform_matrix, output->width, output->height, transform); output->impl->transform(output->state, transform); } void wlr_output_destroy(struct wlr_output *output) { if (!output) return; output->impl->destroy(output->state); - if (output->make) free(output->make); - if (output->model) free(output->model); + free(output->make); + free(output->model); for (size_t i = 0; output->modes && i < output->modes->length; ++i) { free(output->modes->items[i]); } -- cgit v1.2.3