opengl_learn

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

commit ee58f2467a97b3bef00983089ac947e7420cfe38
parent ebec8290bd023f5cac868dbd5f0325e5b763d9de
Author: David DiPaola <DavidDiPaola@users.noreply.github.com>
Date:   Sun, 10 Jun 2018 11:49:48 -0400

ogl: perspective() now exists on error to make caller code simpler

Diffstat:
M01-perspective.c | 8+-------
M02-cube.c | 8+-------
Mogl/ogl.h | 2+-
Mogl/ogl_perspective.c | 29+++++++++++++++++++++++++----
4 files changed, 28 insertions(+), 19 deletions(-)

diff --git a/01-perspective.c b/01-perspective.c @@ -17,8 +17,6 @@ licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/ int main() { - int status; - GLFWwindow * window = ogl_init(400, 240, "01 - triangle with perspective"); const char * program_vertex_source = @@ -41,11 +39,7 @@ main() { GLint program_attribute_position_ID = glGetAttribLocation(program_ID, "attribute_position"); struct ogl_mat4f MVP_projection; - status = ogl_perspective(45.0f, 0.1f, 100.0f, &MVP_projection); - if (status < 0) { - fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__); - return status; - } + ogl_perspective(45.0f, 0.1f, 100.0f, &MVP_projection); struct ogl_mat4f MVP_view; struct ogl_vec3f MVP_view_eye = { .x= 4.0f, .y= 3.0f, .z= 3.0f }; diff --git a/02-cube.c b/02-cube.c @@ -17,8 +17,6 @@ licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/ int main() { - int status; - GLFWwindow * window = ogl_init(400, 240, "02 - cube"); const char * program_vertex_source = @@ -46,11 +44,7 @@ main() { GLint program_attribute_color_ID = glGetAttribLocation(program_ID, "attribute_color"); struct ogl_mat4f MVP_projection; - status = ogl_perspective(45.0f, 0.1f, 100.0f, &MVP_projection); - if (status < 0) { - fprintf(stderr, "\t" "at %s : %d" "\n", __FILE__, __LINE__); - return status; - } + ogl_perspective(45.0f, 0.1f, 100.0f, &MVP_projection); struct ogl_mat4f MVP_view; struct ogl_vec3f MVP_view_eye = { .x= 4.0f, .y= 3.0f, .z=-3.0f }; diff --git a/ogl/ogl.h b/ogl/ogl.h @@ -126,7 +126,7 @@ ogl_lookat( struct ogl_mat4f * out_matrix ); -int +void ogl_perspective( GLfloat fovy, GLfloat zNear, GLfloat zFar, struct ogl_mat4f * out_matrix diff --git a/ogl/ogl_perspective.c b/ogl/ogl_perspective.c @@ -3,6 +3,10 @@ licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/zero/1.0/) */ +#include <stdio.h> + +#include <stdlib.h> + #include <GL/glew.h> #include <math.h> @@ -10,16 +14,34 @@ licensed under CC0 (public domain, see https://creativecommons.org/publicdomain/ #include "ogl.h" #include "_ogl.h" -int +void ogl_perspective( GLfloat fovy, GLfloat zNear, GLfloat zFar, struct ogl_mat4f * out_matrix ) { /* from https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/gluPerspective.xml */ + GLfloat aspect = (GLfloat)_ogl_window_width / (GLfloat)_ogl_window_height; - if ((fovy < 0.0f) || (aspect < 0.0f) || (zNear < 0.0f) || (zFar < 0.0f) || (zNear > zFar)) { - return -1; + if (fovy < 0.0f) { + fprintf(stderr, "ogl:perspective() ERROR: fovy < 0.0f (is %g)" "\n", fovy); + exit(-1); + } + if (aspect < 0.0f) { + fprintf(stderr, "ogl:perspective() ERROR: aspect < 0.0f (is %g)" "\n", aspect); + exit(-1); + } + if (zNear < 0.0f) { + fprintf(stderr, "ogl:perspective() ERROR: zNear < 0.0f (is %g)" "\n", zNear); + exit(-1); + } + if (zFar < 0.0f) { + fprintf(stderr, "ogl:perspective() ERROR: zFar < 0.0f (is %g)" "\n", zFar); + exit(-1); + } + if (zNear > zFar) { + fprintf(stderr, "ogl:perspective() ERROR: zNear > zFar (%g > %g)" "\n", zNear, zFar); + exit(-1); } GLfloat f = 1.0f / tanf(fovy / 2.0f); @@ -29,6 +51,5 @@ ogl_perspective( values[ 4] = 0.0f; values[ 5] = f; values[ 6] = 0.0f; values[ 7] = 0.0f; values[ 8] = 0.0f; values[ 9] = 0.0f; values[10] = (zFar+zNear) / (zNear-zFar); values[11] = (2*zFar*zNear) / (zNear-zFar); values[12] = 0.0f; values[13] = 0.0f; values[14] = -1.0f; values[15] = 0.0f; - return 0; }