/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. #import <OpenGLES/ES1/gl.h>
  9. #import <math.h>
  10. #import "OpenGL_Internal.h"
  11. #include "glu.h"
  12. void gluPerspective(GLfloat fovy, GLfloat aspect, GLfloat zNear, GLfloat zFar)
  13. {
  14. GLfloat xmin, xmax, ymin, ymax;
  15. ymax = zNear * (GLfloat)tanf(fovy * (float)M_PI / 360);
  16. ymin = -ymax;
  17. xmin = ymin * aspect;
  18. xmax = ymax * aspect;
  19. glFrustumf(xmin, xmax,
  20. ymin, ymax,
  21. zNear, zFar);
  22. }
  23. void gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez,
  24. GLfloat centerx, GLfloat centery, GLfloat centerz,
  25. GLfloat upx, GLfloat upy, GLfloat upz)
  26. {
  27. GLfloat m[16];
  28. GLfloat x[3], y[3], z[3];
  29. GLfloat mag;
  30. /* Make rotation matrix */
  31. /* Z vector */
  32. z[0] = eyex - centerx;
  33. z[1] = eyey - centery;
  34. z[2] = eyez - centerz;
  35. mag = (float)sqrtf(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);
  36. if (mag) {
  37. z[0] /= mag;
  38. z[1] /= mag;
  39. z[2] /= mag;
  40. }
  41. /* Y vector */
  42. y[0] = upx;
  43. y[1] = upy;
  44. y[2] = upz;
  45. /* X vector = Y cross Z */
  46. x[0] = y[1] * z[2] - y[2] * z[1];
  47. x[1] = -y[0] * z[2] + y[2] * z[0];
  48. x[2] = y[0] * z[1] - y[1] * z[0];
  49. /* Recompute Y = Z cross X */
  50. y[0] = z[1] * x[2] - z[2] * x[1];
  51. y[1] = -z[0] * x[2] + z[2] * x[0];
  52. y[2] = z[0] * x[1] - z[1] * x[0];
  53. /* cross product gives area of parallelogram, which is < 1.0 for
  54. * non-perpendicular unit-length vectors; so normalize x, y here
  55. */
  56. mag = (float)sqrtf(x[0] * x[0] + x[1] * x[1] + x[2] * x[2]);
  57. if (mag) {
  58. x[0] /= mag;
  59. x[1] /= mag;
  60. x[2] /= mag;
  61. }
  62. mag = (float)sqrtf(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);
  63. if (mag) {
  64. y[0] /= mag;
  65. y[1] /= mag;
  66. y[2] /= mag;
  67. }
  68. #define M(row,col) m[col*4+row]
  69. M(0, 0) = x[0];
  70. M(0, 1) = x[1];
  71. M(0, 2) = x[2];
  72. M(0, 3) = 0.0f;
  73. M(1, 0) = y[0];
  74. M(1, 1) = y[1];
  75. M(1, 2) = y[2];
  76. M(1, 3) = 0.0f;
  77. M(2, 0) = z[0];
  78. M(2, 1) = z[1];
  79. M(2, 2) = z[2];
  80. M(2, 3) = 0.0f;
  81. M(3, 0) = 0.0f;
  82. M(3, 1) = 0.0f;
  83. M(3, 2) = 0.0f;
  84. M(3, 3) = 1.0f;
  85. #undef M
  86. glMultMatrixf(m);
  87. /* Translate Eye to Origin */
  88. glTranslatef(-eyex, -eyey, -eyez);
  89. }