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.4KB

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