2 CDTestFramework http://codercorner.com
3 Copyright (c) 2007-2008 Pierre Terdiman, pierre@codercorner.com
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
19 static const float gCamSpeed = 1.0f;
20 //static Point gEye(3.0616338f, 1.1985892f, 2.5769043f);
21 //static Point gDir(-0.66853905,-0.14004262,-0.73037237);
22 static Point gEye(240, 205, 205);
23 static Point gDir(-1,-1,-1);
25 static float gFOV = 60.0f;
27 const Point& GetCameraPos()
32 const Point& GetCameraDir()
37 void MoveCameraForward()
39 gEye += gDir * gCamSpeed;
42 void MoveCameraBackward()
44 gEye -= gDir * gCamSpeed;
47 void MoveCameraRight()
49 gEye -= gN * gCamSpeed;
54 gEye += gN * gCamSpeed;
57 static const float NxPiF32 = 3.141592653589793f;
59 float degToRad(float a)
61 return (float)0.01745329251994329547 * a;
69 NxQuat(const float angle, const Point & axis)
75 const float i_length = 1.0f / sqrtf( x*x + y*y + z*z );
80 float Half = degToRad(angle * 0.5f);
83 const float sin_theta_over_two = sinf(Half );
84 x = x * sin_theta_over_two;
85 y = y * sin_theta_over_two;
86 z = z * sin_theta_over_two;
89 void NxQuat::multiply(const NxQuat& left, const Point& right)
93 a = - left.x*right.x - left.y*right.y - left.z *right.z;
94 b = left.w*right.x + left.y*right.z - right.y*left.z;
95 c = left.w*right.y + left.z*right.x - right.z*left.x;
96 d = left.w*right.z + left.x*right.y - right.x*left.y;
104 void NxQuat::rotate(Point & v) const
113 left.multiply(*this,v);
114 v.x = left.w*myInverse.x + myInverse.w*left.x + left.y*myInverse.z - myInverse.y*left.z;
115 v.y = left.w*myInverse.y + myInverse.w*left.y + left.z*myInverse.x - myInverse.z*left.x;
116 v.z = left.w*myInverse.z + myInverse.w*left.z + left.x*myInverse.y - myInverse.x*left.y;
122 void RotateCamera(int dx, int dy)
124 gDir = gDir.Normalize();
125 gN = gDir ^ Point(0,1,0);
127 NxQuat qx(NxPiF32 * dx * 20/ 180.0f, Point(0,1,0));
129 NxQuat qy(NxPiF32 * dy * 20/ 180.0f, gN);
133 void SetupCameraMatrix()
136 gluPerspective(gFOV, ((float)glutGet(GLUT_WINDOW_WIDTH))/((float)glutGet(GLUT_WINDOW_HEIGHT)), 1.0f, 10000.0f);
137 gluLookAt(gEye.x, gEye.y, gEye.z, gEye.x + gDir.x, gEye.y + gDir.y, gEye.z + gDir.z, 0.0f, 1.0f, 0.0f);
140 Point ComputeWorldRay(int xs, int ys)
143 GLdouble modelMatrix[16];
144 GLdouble projMatrix[16];
145 glGetIntegerv(GL_VIEWPORT, viewPort);
146 glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
147 glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
148 ys = viewPort[3] - ys - 1;
149 GLdouble wx0, wy0, wz0;
150 gluUnProject((GLdouble) xs, (GLdouble) ys, 0.0, modelMatrix, projMatrix, viewPort, &wx0, &wy0, &wz0);
151 GLdouble wx1, wy1, wz1;
152 gluUnProject((GLdouble) xs, (GLdouble) ys, 1.0, modelMatrix, projMatrix, viewPort, &wx1, &wy1, &wz1);
153 Point tmp(float(wx1-wx0), float(wy1-wy0), float(wz1-wz0));