PageRenderTime 27ms CodeModel.GetById 9ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/cocos2d/Support/glu.c

http://github.com/kstenerud/ObjectAL-for-iPhone
C | 108 lines | 73 code | 19 blank | 16 comment | 3 complexity | 897c8e3b6bb41a48490fe0251dff6262 MD5 | raw file
  1//
  2// cocos2d (incomplete) GLU implementation
  3//
  4// gluLookAt and gluPerspective from:
  5// http://jet.ro/creations (San Angeles Observation)
  6// 
  7// 
  8
  9#import <OpenGLES/ES1/gl.h>
 10#import <math.h>
 11#import "OpenGL_Internal.h"
 12#include "glu.h"
 13
 14void gluPerspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar)
 15{	
 16	GLfloat xmin, xmax, ymin, ymax;
 17		
 18	ymax = zNear * (GLfloat)tanf(fovy * (float)M_PI / 360);
 19	ymin = -ymax;
 20	xmin = ymin * aspect;
 21	xmax = ymax * aspect;
 22		
 23	glFrustumf(xmin, xmax,
 24				ymin, ymax,
 25				zNear, zFar);	
 26}
 27
 28void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez,
 29					  GLfloat centerx, GLfloat centery, GLfloat centerz,
 30					  GLfloat upx, GLfloat upy, GLfloat upz)
 31{
 32    GLfloat m[16];
 33    GLfloat x[3], y[3], z[3];
 34    GLfloat mag;
 35	
 36    /* Make rotation matrix */
 37	
 38    /* Z vector */
 39    z[0] = eyex - centerx;
 40    z[1] = eyey - centery;
 41    z[2] = eyez - centerz;
 42    mag = (float)sqrtf(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
 43    if (mag) {
 44        z[0] /= mag;
 45        z[1] /= mag;
 46        z[2] /= mag;
 47    }
 48	
 49    /* Y vector */
 50    y[0] = upx;
 51    y[1] = upy;
 52    y[2] = upz;
 53	
 54    /* X vector = Y cross Z */
 55    x[0] = y[1] * z[2] - y[2] * z[1];
 56    x[1] = -y[0] * z[2] + y[2] * z[0];
 57    x[2] = y[0] * z[1] - y[1] * z[0];
 58	
 59    /* Recompute Y = Z cross X */
 60    y[0] = z[1] * x[2] - z[2] * x[1];
 61    y[1] = -z[0] * x[2] + z[2] * x[0];
 62    y[2] = z[0] * x[1] - z[1] * x[0];
 63	
 64    /* cross product gives area of parallelogram, which is < 1.0 for
 65     * non-perpendicular unit-length vectors; so normalize x, y here
 66     */
 67	
 68    mag = (float)sqrtf(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
 69    if (mag) {
 70        x[0] /= mag;
 71        x[1] /= mag;
 72        x[2] /= mag;
 73    }
 74	
 75    mag = (float)sqrtf(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
 76    if (mag) {
 77        y[0] /= mag;
 78        y[1] /= mag;
 79        y[2] /= mag;
 80    }
 81	
 82#define M(row,col)  m[col*4+row]
 83    M(0, 0) = x[0];
 84    M(0, 1) = x[1];
 85    M(0, 2) = x[2];
 86    M(0, 3) = 0.0f;
 87    M(1, 0) = y[0];
 88    M(1, 1) = y[1];
 89    M(1, 2) = y[2];
 90    M(1, 3) = 0.0f;
 91    M(2, 0) = z[0];
 92    M(2, 1) = z[1];
 93    M(2, 2) = z[2];
 94    M(2, 3) = 0.0f;
 95    M(3, 0) = 0.0f;
 96    M(3, 1) = 0.0f;
 97    M(3, 2) = 0.0f;
 98    M(3, 3) = 1.0f;
 99#undef M
100	
101	glMultMatrixf(m);
102
103	
104    /* Translate Eye to Origin */
105    glTranslatef(-eyex, -eyey, -eyez);
106}
107
108