commit 0e6c9099c9414a8108e85d9228aac2d96907ba79
parent 580f490d75b1d4cd9ce697e03b851a5030010f0c
Author: David DiPaola <DavidDiPaola@users.noreply.github.com>
Date: Sat, 6 Oct 2018 09:09:44 -0400
05: added rotation transformation
Diffstat:
4 files changed, 48 insertions(+), 3 deletions(-)
diff --git a/05-geometrytransforms.c b/05-geometrytransforms.c
@@ -88,11 +88,11 @@ main() {
struct ogl_mat4f MVP_model;
struct ogl_vec3f MVP_model_translation = { .x= 0.0f, .y= 0.0f, .z=-1.0f };
- /* TODO model rotation */
+ struct ogl_quat MVP_model_rotation = { .angle= 3.14f/3.0f, .axis={ .x= 0.0f, .y= 1.0f, .z= 0.0f } };
struct ogl_vec3f MVP_model_scale = { .x= 2.0f, .y= 2.0f, .z= 2.0f };
ogl_mat4f_identity( &MVP_model);
ogl_mat4f_translate(MVP_model, MVP_model_translation, &MVP_model);
- /* TODO model rotation */
+ ogl_mat4f_rotate( MVP_model, MVP_model_rotation, &MVP_model);
ogl_mat4f_scale( MVP_model, MVP_model_scale, &MVP_model);
struct ogl_mat4f MVP;
diff --git a/ogl/Makefile b/ogl/Makefile
@@ -1,7 +1,7 @@
SRC = \
_ogl.c ogl_init.c \
ogl_GLfloat_isapproxequal.c ogl_GLfloat_print.c \
- ogl_mat4f_identity.c ogl_mat4f_isapproxequal.c ogl_mat4f_multiply.c ogl_mat4f_print.c ogl_mat4f_scale.c ogl_mat4f_translate.c \
+ ogl_mat4f_identity.c ogl_mat4f_isapproxequal.c ogl_mat4f_multiply.c ogl_mat4f_print.c ogl_mat4f_rotate.c ogl_mat4f_scale.c ogl_mat4f_translate.c \
ogl_program_build.c ogl_program_uniform_get_ID.c ogl_program_uniform_set_mat4f.c \
ogl_vec3f_cross.c ogl_vec3f_dot.c ogl_vec3f_isapproxequal.c ogl_vec3f_magnitude.c ogl_vec3f_normal.c ogl_vec3f_print.c \
ogl_arraybuffer_load.c \
diff --git a/ogl/ogl.h b/ogl/ogl.h
@@ -22,6 +22,11 @@ struct ogl_vec3f {
GLfloat z;
};
+struct ogl_quat {
+ GLfloat angle;
+ struct ogl_vec3f axis;
+};
+
GLFWwindow *
@@ -84,6 +89,12 @@ ogl_mat4f_print(
);
void
+ogl_mat4f_rotate(
+ struct ogl_mat4f matrix, struct ogl_quat rotation,
+ struct ogl_mat4f * out_result
+);
+
+void
ogl_mat4f_scale(
struct ogl_mat4f matrix, struct ogl_vec3f scale,
struct ogl_mat4f * out_result
diff --git a/ogl/ogl_mat4f_rotate.c b/ogl/ogl_mat4f_rotate.c
@@ -0,0 +1,34 @@
+/*
+2018 David DiPaola
+licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/zero/1.0/)
+*/
+
+#define _ISOC99_SOURCE
+#include <math.h>
+
+#include "ogl.h"
+
+void
+ogl_mat4f_rotate(
+ struct ogl_mat4f matrix, struct ogl_quat rotation,
+ struct ogl_mat4f * out_result
+) {
+ /* see OpenGL 2.1 glRotate(): https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml */
+ /* note: glRotate() was deprecated, then removed from later OpenGL versions. don't use it */
+
+ struct ogl_vec3f axis;
+ ogl_vec3f_normal(rotation.axis, &axis);
+ GLfloat x = rotation.axis.x;
+ GLfloat y = rotation.axis.y;
+ GLfloat z = rotation.axis.z;
+ GLfloat c = cosf(rotation.angle);
+ GLfloat s = sinf(rotation.angle);
+ struct ogl_mat4f rotation_matrix = { .values = {
+ (x*x*(1-c))+( c), (x*y*(1-c))-(z*s), (x*z*(1-c))+(y*s), 0.0f,
+ (y*x*(1-c))+(z*s), (y*y*(1-c))+( c), (y*z*(1-c))-(x*s), 0.0f,
+ (x*z*(1-c))-(y*s), (y*z*(1-c))+(x*s), (z*z*(1-c))+( c), 0.0f,
+ 0.0f, 0.0f, 0.0f, 1.0f,
+ }};
+ ogl_mat4f_multiply(matrix, rotation_matrix, out_result);
+}
+