Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Window.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*!
  2. * \file src/Window.cpp
  3. * \brief windowing implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <cstdarg>
  10. #include <cmath>
  11. #include "global.h"
  12. #include "math/math.h"
  13. #include "utils/strings.h"
  14. #include "Window.h"
  15. unsigned int Window::getWidth() {
  16. return mWidth;
  17. }
  18. unsigned int Window::getHeight() {
  19. return mHeight;
  20. }
  21. int Window::initializeGL() {
  22. // Print driver support information
  23. //printf("GL Vendor : %s\n", glGetString(GL_VENDOR));
  24. //printf("GL Renderer: %s\n", glGetString(GL_RENDERER));
  25. //printf("GL Version : %s\n", glGetString(GL_VERSION));
  26. // Testing for goodies
  27. const char *s = (const char *)glGetString(GL_EXTENSIONS);
  28. if ((s != NULL) && (s[0] != '\0')) {
  29. //! \todo MultiTexture flag
  30. //if (strstr(s, "GL_ARB_multitexture"))
  31. //mFlags |= Render::fMultiTexture;
  32. }
  33. // Set up Z buffer
  34. glEnable(GL_DEPTH_TEST);
  35. glDepthFunc(GL_LESS);
  36. // Set up culling
  37. glEnable(GL_CULL_FACE);
  38. glFrontFace(GL_CW);
  39. //glFrontFace(GL_CCW);
  40. //glCullFace(GL_FRONT);
  41. // Set background to black
  42. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  43. // Disable lighting
  44. glDisable(GL_LIGHTING);
  45. // Set up alpha blending
  46. glEnable(GL_BLEND);
  47. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  48. //glEnable(GL_ALPHA_TEST); // Disable per pixel alpha blending
  49. glAlphaFunc(GL_GREATER, 0);
  50. glPointSize(5.0);
  51. // Setup shading
  52. glShadeModel(GL_SMOOTH);
  53. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  54. glHint(GL_FOG_HINT, GL_NICEST);
  55. glEnable(GL_COLOR_MATERIAL);
  56. glEnable(GL_DITHER);
  57. // AA polygon edges
  58. glEnable(GL_POLYGON_SMOOTH);
  59. glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
  60. glDisable(GL_LINE_SMOOTH);
  61. glDisable(GL_POINT_SMOOTH);
  62. glDisable(GL_AUTO_NORMAL);
  63. glDisable(GL_LOGIC_OP);
  64. glDisable(GL_TEXTURE_1D);
  65. glDisable(GL_STENCIL_TEST);
  66. glDisable(GL_FOG);
  67. glDisable(GL_NORMALIZE);
  68. glEnableClientState(GL_VERTEX_ARRAY);
  69. glDisableClientState(GL_EDGE_FLAG_ARRAY);
  70. glDisableClientState(GL_COLOR_ARRAY);
  71. glDisableClientState(GL_NORMAL_ARRAY);
  72. glPolygonMode(GL_FRONT, GL_FILL);
  73. glMatrixMode(GL_MODELVIEW);
  74. return 0;
  75. }
  76. void Window::resizeGL() {
  77. float fovY = 40.0f; // 45.0f
  78. float clipNear = 10.0f; // 4.0f
  79. float clipFar = 600000.0f; // 4000.0f
  80. glViewport(0, 0, mWidth, mHeight);
  81. glMatrixMode(GL_PROJECTION);
  82. glLoadIdentity();
  83. // Adjust clipping
  84. GLfloat fH = tanf(fovY * OR_PI / 360.0f) * clipNear;
  85. GLfloat fW = fH * ((GLfloat)mWidth) / ((GLfloat)mHeight);
  86. glFrustum(-fW, fW, -fH, fH, clipNear, clipFar);
  87. glMatrixMode(GL_MODELVIEW);
  88. glLoadIdentity();
  89. }
  90. void Window::glEnter2D() {
  91. glPushAttrib(GL_ENABLE_BIT);
  92. glDisable(GL_DEPTH_TEST);
  93. glDisable(GL_CULL_FACE);
  94. glEnable(GL_TEXTURE_2D);
  95. /* This allows alpha blending of 2D textures with the scene */
  96. glEnable(GL_BLEND);
  97. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  98. glViewport(0, 0, mWidth, mHeight);
  99. glMatrixMode(GL_PROJECTION);
  100. glPushMatrix();
  101. glLoadIdentity();
  102. glOrtho(0.0, (GLdouble)mWidth, (GLdouble)mHeight, 0.0, 0.0, 1.0);
  103. glMatrixMode(GL_MODELVIEW);
  104. glPushMatrix();
  105. glLoadIdentity();
  106. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  107. }
  108. void Window::glExit2D() {
  109. glMatrixMode(GL_MODELVIEW);
  110. glPopMatrix();
  111. glMatrixMode(GL_PROJECTION);
  112. glPopMatrix();
  113. glPopAttrib();
  114. glMatrixMode(GL_MODELVIEW);
  115. }
  116. // Replaced the deprecated gluLookAt with slightly modified code from here:
  117. // http://www.khronos.org/message_boards/showthread.php/4991
  118. void Window::lookAt(float eyeX, float eyeY, float eyeZ,
  119. float lookAtX, float lookAtY, float lookAtZ,
  120. float upX, float upY, float upZ) {
  121. // calculating the viewing vector
  122. float f[3] = {
  123. lookAtX - eyeX,
  124. lookAtY - eyeY,
  125. lookAtZ - eyeZ
  126. };
  127. // normalizing the viewing vector
  128. float fMag = sqrtf(f[0] * f[0] + f[1] * f[1] + f[2] * f[2]);
  129. f[0] /= fMag;
  130. f[1] /= fMag;
  131. f[2] /= fMag;
  132. float s[3] = {
  133. (f[1] * upZ) - (upY * f[2]),
  134. (upX * f[2]) - (f[0] * upZ),
  135. (f[0] * upY) - (upX * f[1])
  136. };
  137. float u[3] = {
  138. (s[1] * f[2]) - (f[1] * s[2]),
  139. (f[0] * s[2]) - (s[0] * f[2]),
  140. (s[0] * f[1]) - (f[0] * s[1])
  141. };
  142. float m[16] = {
  143. s[0], u[0], -f[0], 0,
  144. s[1], u[1], -f[1], 0,
  145. s[2], u[2], -f[2], 0,
  146. 0, 0, 0, 1
  147. };
  148. glMultMatrixf(m);
  149. glTranslatef(-eyeX, -eyeY, -eyeZ);
  150. }