summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2011-07-03 01:02:00 -0500
committerKamil Kaminski <kamilkss@gmail.com>2011-07-03 01:02:00 -0500
commit195b41fa399b437898dec7c92851c8d483c46072 (patch)
tree1a31f0468fc5527c7ce1e1e812f545f101027e53
parent01d6561d73d11a28fd339d46e73e12fd5658300a (diff)
downloadGLPyramid-195b41fa399b437898dec7c92851c8d483c46072.tar.gz
GLPyramid-195b41fa399b437898dec7c92851c8d483c46072.tar.bz2
GLPyramid-195b41fa399b437898dec7c92851c8d483c46072.zip
new glframe_rotate_local_x function
-rw-r--r--sdl/Makefile4
-rw-r--r--sdl/glframe.c51
-rw-r--r--sdl/glframe.h5
-rw-r--r--sdl/math3d.c21
-rw-r--r--sdl/math3d.h1
-rw-r--r--sdl/pyramid.c10
6 files changed, 78 insertions, 14 deletions
diff --git a/sdl/Makefile b/sdl/Makefile
index 7b878a9..74a9840 100644
--- a/sdl/Makefile
+++ b/sdl/Makefile
@@ -48,8 +48,8 @@ $(OBJ_DIR):
.PHONY: clean info
clean:
- rm -rf $(OBJ_DIR)
- rm -f $(BIN)
+ @rm -rf $(OBJ_DIR)
+ @rm -f $(BIN)
info:
@echo $(OBJ_REL)
diff --git a/sdl/glframe.c b/sdl/glframe.c
index e9c31e3..b3dc33e 100644
--- a/sdl/glframe.c
+++ b/sdl/glframe.c
@@ -67,7 +67,9 @@ void glframe_apply_camera_transform(GLFrame *frame)
int rot_only = 0;
M3DMatrix44f m;
- glframe_get_camera_orientation(frame, m);
+ glframe_get_camera_orientation(frame, m);
+ m3dPrintMatrix44f(m);
+
glMultMatrixf(m);
/* if rotation only, then do not do the translation */
@@ -91,20 +93,51 @@ void glframe_move_forward(GLFrame *frame, float delta)
frame->v_location[2] += frame->v_forward[2] * delta;
}
+/* rotate left or right */
void glframe_rotate_local_y(GLFrame *frame, float angle)
{
- M3DMatrix44f rotMat;
+ M3DMatrix44f rotmat;
+ M3DVector3f newvect;
/* just rotate around the up vector */
- /* create a rotation matrix around my Up (Y) vector */
- m3dRotationMatrix44f(rotMat, angle, frame->v_up[0], frame->v_up[1], frame->v_up[2]);
+ /* create a rotation matrix around up vector */
+ m3dRotationMatrix44f(rotmat, angle, frame->v_up[0], frame->v_up[1], frame->v_up[2]);
+
+ /* rotate forward pointing vector (inlined 3x3 transform) */
+ newvect[0] = rotmat[0] * frame->v_forward[0] + rotmat[4] * frame->v_forward[1] + rotmat[8] * frame->v_forward[2];
+ newvect[1] = rotmat[1] * frame->v_forward[0] + rotmat[5] * frame->v_forward[1] + rotmat[9] * frame->v_forward[2];
+ newvect[2] = rotmat[2] * frame->v_forward[0] + rotmat[6] * frame->v_forward[1] + rotmat[10] * frame->v_forward[2];
+ m3dCopyVector3f(frame->v_forward, newvect);
+}
- M3DVector3f newVect;
+/* rotate up or down */
+void glframe_rotate_local_x(GLFrame *frame, float angle)
+{
+ M3DMatrix44f rotmat;
+ M3DVector3f newfwdvec;
+ M3DVector3f newupvec;
+ M3DVector3f x, z;
+
+ /* z vector is reversed */
+ z[0] = -frame->v_forward[0];
+ z[1] = -frame->v_forward[1];
+ z[2] = -frame->v_forward[2];
+
+ /* x vector = y cross z */
+ m3dCrossProductf(x, frame->v_up, z);
+
+ /* create a rotation matrix around x axis */
+ m3dRotationMatrix44f(rotmat, angle, x[0], x[1], x[2]);
/* rotate forward pointing vector (inlined 3x3 transform) */
- newVect[0] = rotMat[0] * frame->v_forward[0] + rotMat[4] * frame->v_forward[1] + rotMat[8] * frame->v_forward[2];
- newVect[1] = rotMat[1] * frame->v_forward[0] + rotMat[5] * frame->v_forward[1] + rotMat[9] * frame->v_forward[2];
- newVect[2] = rotMat[2] * frame->v_forward[0] + rotMat[6] * frame->v_forward[1] + rotMat[10] * frame->v_forward[2];
- m3dCopyVector3f(frame->v_forward, newVect);
+ newfwdvec[0] = rotmat[0] * frame->v_forward[0] + rotmat[4] * frame->v_forward[1] + rotmat[8] * frame->v_forward[2];
+ newfwdvec[1] = rotmat[1] * frame->v_forward[0] + rotmat[5] * frame->v_forward[1] + rotmat[9] * frame->v_forward[2];
+ newfwdvec[2] = rotmat[2] * frame->v_forward[0] + rotmat[6] * frame->v_forward[1] + rotmat[10] * frame->v_forward[2];
+
+ /* calculate new up vector */
+ m3dCrossProductf(newupvec, x, newfwdvec);
+
+ m3dCopyVector3f(frame->v_forward, newfwdvec);
+ m3dCopyVector3f(frame->v_up, newupvec);
}
diff --git a/sdl/glframe.h b/sdl/glframe.h
index 3842d7a..0fb5e58 100644
--- a/sdl/glframe.h
+++ b/sdl/glframe.h
@@ -6,8 +6,8 @@
typedef struct
{
M3DVector3f v_location; /* location */
- M3DVector3f v_forward; /* where am I heading */
- M3DVector3f v_up; /* which way is up */
+ M3DVector3f v_forward; /* z, where am I heading */
+ M3DVector3f v_up; /* y, which way is up */
} GLFrame;
/* function prototypes */
@@ -16,6 +16,7 @@ void glframe_get_camera_orientation(GLFrame *, M3DMatrix44f);
void glframe_apply_camera_transform(GLFrame *);
void glframe_move_forward(GLFrame *, float);
void glframe_rotate_local_y(GLFrame *, float);
+void glframe_rotate_local_x(GLFrame *, float);
#endif
diff --git a/sdl/math3d.c b/sdl/math3d.c
index 775dabd..3277ede 100644
--- a/sdl/math3d.c
+++ b/sdl/math3d.c
@@ -7,6 +7,7 @@
*/
#include <math.h>
+#include <stdio.h>
#include <string.h>
#include "math3d.h"
@@ -202,3 +203,23 @@ void m3dMatrixMultiply44f(M3DMatrix44f product, const M3DMatrix44f a,
#undef P
}
+void m3dPrintMatrix44f(const M3DMatrix44f m)
+{
+ int i, j;
+
+ #define M(row,col) m[col*4+row]
+
+ for (i = 0; i < 4; i++)
+ {
+ for (j = 0; j < 4; j++)
+ {
+ printf("%5.2f ", M(i, j));
+ }
+ puts("");
+ }
+
+ puts("");
+
+ #undef M
+}
+
diff --git a/sdl/math3d.h b/sdl/math3d.h
index 5c04daa..670b4af 100644
--- a/sdl/math3d.h
+++ b/sdl/math3d.h
@@ -27,6 +27,7 @@ void m3dGetPlaneEquationf(M3DVector4f, const M3DVector3f,
void m3dRotationMatrix44f(M3DMatrix44f, float, float, float, float);
void m3dMakePlanarShadowMatrixf(M3DMatrix44f, const M3DVector4f, const M3DVector3f);
void m3dMatrixMultiply44f(M3DMatrix44f, const M3DMatrix44f, const M3DMatrix44f);
+void m3dPrintMatrix44f(const M3DMatrix44f);
static inline void m3dCrossProductf(M3DVector3f result, const M3DVector3f u,
const M3DVector3f v)
diff --git a/sdl/pyramid.c b/sdl/pyramid.c
index b3e1b2e..a3303b8 100644
--- a/sdl/pyramid.c
+++ b/sdl/pyramid.c
@@ -70,6 +70,8 @@ static void keys(SDL_keysym *keysym, const unsigned int *keys_held, const int fl
case SDLK_DOWN: glframe_move_forward(&p.camera, -0.5f); break;
case SDLK_LEFT: glframe_rotate_local_y(&p.camera, 0.1f); break;
case SDLK_RIGHT: glframe_rotate_local_y(&p.camera, -0.1f); break;
+ case SDLK_n: glframe_rotate_local_x(&p.camera, 0.1f); break;
+ case SDLK_m: glframe_rotate_local_x(&p.camera, -0.1f); break;
default: break;
}
}
@@ -92,6 +94,11 @@ static void keys(SDL_keysym *keysym, const unsigned int *keys_held, const int fl
glframe_rotate_local_y(&p.camera, 0.02f);
if (keys_held[SDLK_RIGHT])
glframe_rotate_local_y(&p.camera, -0.02f);
+
+ if (keys_held[SDLK_n])
+ glframe_rotate_local_x(&p.camera, 0.02f);
+ if (keys_held[SDLK_m])
+ glframe_rotate_local_x(&p.camera, -0.02f);
}
p.xrot = (GLfloat) ((const int) p.xrot % 360);
@@ -133,7 +140,8 @@ static void process_events(void)
/* below code has to be placed here, check for keys that are being constantly held */
if (keys_held[SDLK_w] || keys_held[SDLK_s] || keys_held[SDLK_a] || keys_held[SDLK_d] ||
- keys_held[SDLK_UP] || keys_held[SDLK_DOWN] || keys_held[SDLK_LEFT] || keys_held[SDLK_RIGHT])
+ keys_held[SDLK_UP] || keys_held[SDLK_DOWN] || keys_held[SDLK_LEFT] || keys_held[SDLK_RIGHT] ||
+ keys_held[SDLK_n] || keys_held[SDLK_m])
{
flag = !flag;
keys(NULL, keys_held, flag);