opengl_learn

step-by-step introduction to OpenGL
git clone https://0xdd.org/code/opengl_learn.git
Log | Files | Refs | README | LICENSE

ogl_mat4f_rotate.c (1091B)


      1 /*
      2 2018 David DiPaola
      3 licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/zero/1.0/)
      4 */
      5 
      6 #define _ISOC99_SOURCE
      7 #include <math.h>
      8 
      9 #include "ogl.h"
     10 
     11 void
     12 ogl_mat4f_rotate(
     13 	struct ogl_mat4f matrix, struct ogl_quat rotation,
     14 	struct ogl_mat4f * out_result
     15 ) {
     16 	/* see OpenGL 2.1 glRotate(): https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glRotate.xml */
     17 	/* note: glRotate() was deprecated, then removed from later OpenGL versions. don't use it */
     18 
     19 	struct ogl_vec3f axis;
     20 	ogl_vec3f_normal(rotation.axis, &axis);
     21 	GLfloat x = rotation.axis.x;
     22 	GLfloat y = rotation.axis.y;
     23 	GLfloat z = rotation.axis.z;
     24 	GLfloat c = cosf(rotation.angle);
     25 	GLfloat s = sinf(rotation.angle);
     26 	struct ogl_mat4f rotation_matrix = { .values = {
     27 		(x*x*(1-c))+(  c), (x*y*(1-c))-(z*s), (x*z*(1-c))+(y*s), 0.0f,
     28 		(y*x*(1-c))+(z*s), (y*y*(1-c))+(  c), (y*z*(1-c))-(x*s), 0.0f,
     29 		(x*z*(1-c))-(y*s), (y*z*(1-c))+(x*s), (z*z*(1-c))+(  c), 0.0f,
     30 		             0.0f,              0.0f,              0.0f, 1.0f,
     31 	}};
     32 	ogl_mat4f_multiply(matrix, rotation_matrix, out_result);
     33 }
     34