Open Source Tomb Raider Engine
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Window.cpp 4.7KB

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