2011-01-15 18:05:35 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "ccxGL.h"
|
|
|
|
|
|
|
|
#if defined(CCX_PLATFORM_MOBILE)
|
|
|
|
|
|
|
|
#include "ccxStdC.h"
|
|
|
|
|
2011-02-17 14:31:52 +08:00
|
|
|
NS_CC_BEGIN
|
2010-07-19 11:13:54 +08:00
|
|
|
|
|
|
|
void gluPerspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar)
|
|
|
|
{
|
2011-01-15 18:05:35 +08:00
|
|
|
GLfloat xmin, xmax, ymin, ymax;
|
|
|
|
|
|
|
|
ymax = zNear * (GLfloat)tanf(fovy * (float)M_PI / 360);
|
|
|
|
ymin = -ymax;
|
|
|
|
xmin = ymin * aspect;
|
|
|
|
xmax = ymax * aspect;
|
|
|
|
|
|
|
|
glFrustumf(xmin, xmax,
|
|
|
|
ymin, ymax,
|
|
|
|
zNear, zFar);
|
2010-07-19 11:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void gluLookAt(float fEyeX, float fEyeY, float fEyeZ,
|
2011-01-15 18:05:35 +08:00
|
|
|
float fCenterX, float fCenterY, float fCenterZ,
|
|
|
|
float fUpX, float fUpY, float fUpZ)
|
2010-07-19 11:13:54 +08:00
|
|
|
{
|
|
|
|
GLfloat m[16];
|
|
|
|
GLfloat x[3], y[3], z[3];
|
|
|
|
GLfloat mag;
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2010-07-19 11:13:54 +08:00
|
|
|
/* Make rotation matrix */
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2010-07-19 11:13:54 +08:00
|
|
|
/* Z vector */
|
|
|
|
z[0] = fEyeX - fCenterX;
|
|
|
|
z[1] = fEyeY - fCenterY;
|
|
|
|
z[2] = fEyeZ - fCenterZ;
|
|
|
|
mag = (float)sqrtf(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
|
|
|
|
if (mag) {
|
|
|
|
z[0] /= mag;
|
|
|
|
z[1] /= mag;
|
|
|
|
z[2] /= mag;
|
|
|
|
}
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2010-07-19 11:13:54 +08:00
|
|
|
/* Y vector */
|
|
|
|
y[0] = fUpX;
|
|
|
|
y[1] = fUpY;
|
|
|
|
y[2] = fUpZ;
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2010-07-19 11:13:54 +08:00
|
|
|
/* X vector = Y cross Z */
|
|
|
|
x[0] = y[1] * z[2] - y[2] * z[1];
|
|
|
|
x[1] = -y[0] * z[2] + y[2] * z[0];
|
|
|
|
x[2] = y[0] * z[1] - y[1] * z[0];
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2010-07-19 11:13:54 +08:00
|
|
|
/* Recompute Y = Z cross X */
|
|
|
|
y[0] = z[1] * x[2] - z[2] * x[1];
|
|
|
|
y[1] = -z[0] * x[2] + z[2] * x[0];
|
|
|
|
y[2] = z[0] * x[1] - z[1] * x[0];
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2010-07-19 11:13:54 +08:00
|
|
|
/* cross product gives area of parallelogram, which is < 1.0 for
|
2011-01-15 18:05:35 +08:00
|
|
|
* non-perpendicular unit-length vectors; so normalize x, y here
|
|
|
|
*/
|
|
|
|
|
2010-07-19 11:13:54 +08:00
|
|
|
mag = (float)sqrtf(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
|
|
|
|
if (mag) {
|
|
|
|
x[0] /= mag;
|
|
|
|
x[1] /= mag;
|
|
|
|
x[2] /= mag;
|
|
|
|
}
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2010-07-19 11:13:54 +08:00
|
|
|
mag = (float)sqrtf(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
|
|
|
|
if (mag) {
|
|
|
|
y[0] /= mag;
|
|
|
|
y[1] /= mag;
|
|
|
|
y[2] /= mag;
|
|
|
|
}
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2010-07-19 11:13:54 +08:00
|
|
|
#define M(row,col) m[col*4+row]
|
|
|
|
M(0, 0) = x[0];
|
|
|
|
M(0, 1) = x[1];
|
|
|
|
M(0, 2) = x[2];
|
|
|
|
M(0, 3) = 0.0f;
|
|
|
|
M(1, 0) = y[0];
|
|
|
|
M(1, 1) = y[1];
|
|
|
|
M(1, 2) = y[2];
|
|
|
|
M(1, 3) = 0.0f;
|
|
|
|
M(2, 0) = z[0];
|
|
|
|
M(2, 1) = z[1];
|
|
|
|
M(2, 2) = z[2];
|
|
|
|
M(2, 3) = 0.0f;
|
|
|
|
M(3, 0) = 0.0f;
|
|
|
|
M(3, 1) = 0.0f;
|
|
|
|
M(3, 2) = 0.0f;
|
|
|
|
M(3, 3) = 1.0f;
|
|
|
|
#undef M
|
|
|
|
{
|
|
|
|
int a;
|
|
|
|
GLfloat fixedM[16];
|
|
|
|
for (a = 0; a < 16; ++a)
|
|
|
|
fixedM[a] = m[a];
|
|
|
|
glMultMatrixf(fixedM);
|
|
|
|
}
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2010-07-19 11:13:54 +08:00
|
|
|
/* Translate Eye to Origin */
|
|
|
|
glTranslatef(-fEyeX, -fEyeY, -fEyeZ);
|
2011-01-15 18:05:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END;
|
|
|
|
|
|
|
|
#endif // CCX_PLATFORM_MOBILE
|