X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=progs%2Fdemos%2Fshapes%2Fglmatrix.c;h=485e324d8a2f4841b0a90762676063e28fee7a93;hb=b304f6f9002f17cc263e06641a92f19ffbfaf062;hp=3d1b77e65d4442797335b3741a45cfd2d3b5788a;hpb=8a58cc57b97a1aa188018d987671ed565031874d;p=freeglut diff --git a/progs/demos/shapes/glmatrix.c b/progs/demos/shapes/glmatrix.c index 3d1b77e..485e324 100644 --- a/progs/demos/shapes/glmatrix.c +++ b/progs/demos/shapes/glmatrix.c @@ -3,6 +3,10 @@ #include #include "glmatrix.h" +#ifndef M_PI +#define M_PI 3.141592653589793 +#endif + #define MMODE_IDX(x) ((x) - GL_MODELVIEW) #define MAT_STACK_SIZE 32 #define MAT_IDENT {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1} @@ -114,17 +118,17 @@ void gl_scalef(float x, float y, float z) gl_mult_matrixf(mat); } -void gl_ortho(float left, float right, float bottom, float top, float near, float far) +void gl_ortho(float left, float right, float bottom, float top, float znear, float zfar) { float mat[] = MAT_IDENT; float dx = right - left; float dy = top - bottom; - float dz = far - near; + float dz = zfar - znear; float tx = -(right + left) / dx; float ty = -(top + bottom) / dy; - float tz = -(far + near) / dz; + float tz = -(zfar + znear) / dz; float sx = 2.f / dx; float sy = 2.f / dy; @@ -140,37 +144,39 @@ void gl_ortho(float left, float right, float bottom, float top, float near, floa gl_mult_matrixf(mat); } -void gl_frustum(float left, float right, float bottom, float top, float near, float far) +void gl_frustum(float left, float right, float bottom, float top, float znear, float zfar) { float mat[] = MAT_IDENT; float dx = right - left; float dy = top - bottom; - float dz = far - near; + float dz = zfar - znear; float a = (right + left) / dx; float b = (top + bottom) / dy; - float c = -(far + near) / dz; - float d = -2.f * far * near / dz; + float c = -(zfar + znear) / dz; + float d = -2.f * zfar * znear / dz; - mat[0] = 2.f * near / dx; - mat[5] = 2.f * near / dy; + mat[0] = 2.f * znear / dx; + mat[5] = 2.f * znear / dy; mat[8] = a; mat[9] = b; mat[10] = c; mat[11] = -1.f; mat[14] = d; + mat[15] = 0; gl_mult_matrixf(mat); } -void glu_perspective(float vfov, float aspect, float near, float far) +void glu_perspective(float vfov, float aspect, float znear, float zfar) { float vfov_rad = (float)M_PI * vfov / 180.f; - float x = near * (float)tan(vfov_rad / 2.f); - gl_frustum(-aspect * x, aspect * x, -x, x, near, far); + float x = znear * (float)tan(vfov_rad / 2.f); + gl_frustum(-aspect * x, aspect * x, -x, x, znear, zfar); } +/* return the matrix (16 elements, 4x4 matrix, row-major order */ float* get_matrix(int mm) { int idx = MMODE_IDX(mm); @@ -182,6 +188,10 @@ float* get_matrix(int mm) #define M3(i, j) ((i * 3) + j) static float inv_transpose_result[9]; +/* return the inverse transpose of the left-upper 3x3 of a matrix + The returned pointer is only valid until the next time this function is + called, so make a deep copy when you want to keep it around. + */ float* get_inv_transpose_3x3(int mm) { int idx = MMODE_IDX(mm); @@ -207,48 +217,3 @@ float* get_inv_transpose_3x3(int mm) return inv_transpose_result; } - - -#if 0 -void gl_apply_xform(unsigned int prog) -{ - int loc, mvidx, pidx, tidx, mvtop, ptop, ttop; - - mvidx = MMODE_IDX(GL_MODELVIEW); - pidx = MMODE_IDX(GL_PROJECTION); - tidx = MMODE_IDX(GL_TEXTURE); - - mvtop = stack_top[mvidx]; - ptop = stack_top[pidx]; - ttop = stack_top[tidx]; - - assert(prog); - - if((loc = glGetUniformLocation(prog, "matrix_modelview")) != -1) { - glUniformMatrix4fv(loc, 1, 0, mat_stack[mvidx][mvtop]); - } - - if((loc = glGetUniformLocation(prog, "matrix_projection")) != -1) { - glUniformMatrix4fv(loc, 1, 0, mat_stack[pidx][ptop]); - } - - if((loc = glGetUniformLocation(prog, "matrix_texture")) != -1) { - glUniformMatrix4fv(loc, 1, 0, mat_stack[tidx][ttop]); - } - - if((loc = glGetUniformLocation(prog, "matrix_normal")) != -1) { - float nmat[9]; - - nmat[0] = mat_stack[mvidx][mvtop][0]; - nmat[1] = mat_stack[mvidx][mvtop][1]; - nmat[2] = mat_stack[mvidx][mvtop][2]; - nmat[3] = mat_stack[mvidx][mvtop][4]; - nmat[4] = mat_stack[mvidx][mvtop][5]; - nmat[5] = mat_stack[mvidx][mvtop][6]; - nmat[6] = mat_stack[mvidx][mvtop][8]; - nmat[7] = mat_stack[mvidx][mvtop][9]; - nmat[8] = mat_stack[mvidx][mvtop][10]; - glUniformMatrix3fv(loc, 1, 0, nmat); - } -} -#endif