opengl-learn

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

commit 3b05886e88115096fb40a21deebc099c7e255b97
parent 2a9099b38c262d371fa9bba3db2de94f9b61dbf1
Author: David DiPaola <DavidDiPaola@users.noreply.github.com>
Date:   Fri,  5 Oct 2018 20:22:40 -0400

05: added translation transform
05: fixed window title text

Diffstat:
05-geometrytransforms.c | 12+++++++-----
ogl/Makefile | 2+-
ogl/ogl.h | 6++++++
ogl/ogl_mat4f_translate.c | 23+++++++++++++++++++++++
4 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/05-geometrytransforms.c b/05-geometrytransforms.c @@ -23,7 +23,7 @@ licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/ int main() { - GLFWwindow * window = ogl_init(400, 240, "03 - texture"); + GLFWwindow * window = ogl_init(400, 240, "05 - geometry transforms"); /* vu: vertex shader uniform, va: vertex shader attribute, fu: fragment shader uniform, fv: fragment shader varying */ const char * program_vertex_source = @@ -87,11 +87,13 @@ main() { ogl_lookat(MVP_view_eye, MVP_view_center, MVP_view_up, &MVP_view); struct ogl_mat4f MVP_model; - struct ogl_vec3f MVP_model_scale = { .x= 2.0f, .y= 2.0f, .z= 2.0f }; - ogl_mat4f_identity(&MVP_model); - /* TODO model translation */ + struct ogl_vec3f MVP_model_translation = { .x= 0.0f, .y= 0.0f, .z=-1.0f }; /* TODO model rotation */ - ogl_mat4f_scale(MVP_model, MVP_model_scale, &MVP_model); + 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_scale( MVP_model, MVP_model_scale, &MVP_model); struct ogl_mat4f MVP; ogl_mat4f_identity(&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_identity.c ogl_mat4f_isapproxequal.c ogl_mat4f_multiply.c ogl_mat4f_print.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 @@ -89,6 +89,12 @@ ogl_mat4f_scale( struct ogl_mat4f * out_result ); +void +ogl_mat4f_translate( + struct ogl_mat4f matrix, struct ogl_vec3f translation, + struct ogl_mat4f * out_result +); + void diff --git a/ogl/ogl_mat4f_translate.c b/ogl/ogl_mat4f_translate.c @@ -0,0 +1,23 @@ +/* +2018 David DiPaola +licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/zero/1.0/) +*/ + +#include "ogl.h" + +void +ogl_mat4f_translate( + struct ogl_mat4f matrix, struct ogl_vec3f translation, + struct ogl_mat4f * out_result +) { + /* see OpenGL 2.1 glTranslate(): https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glTranslate.xml */ + + struct ogl_mat4f translation_matrix = { .values = { + 1.0f, 0.0f, 0.0f, translation.x, + 0.0f, 1.0f, 0.0f, translation.y, + 0.0f, 0.0f, 1.0f, translation.z, + 0.0f, 0.0f, 0.0f, 1.0f, + }}; + ogl_mat4f_multiply(matrix, translation_matrix, out_result); +} +