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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 "global.h"
  11. #include "math/math.h"
  12. #include "utils/strings.h"
  13. #include "Window.h"
  14. Window::~Window() {
  15. }
  16. unsigned int Window::getWidth() {
  17. return mWidth;
  18. }
  19. unsigned int Window::getHeight() {
  20. return mHeight;
  21. }
  22. int Window::initializeGL() {
  23. // Print driver support information
  24. //printf("GL Vendor : %s\n", glGetString(GL_VENDOR));
  25. //printf("GL Renderer: %s\n", glGetString(GL_RENDERER));
  26. //printf("GL Version : %s\n", glGetString(GL_VERSION));
  27. // Testing for goodies
  28. const char *s = (const char *)glGetString(GL_EXTENSIONS);
  29. if ((s != NULL) && (s[0] != '\0')) {
  30. //! \todo MultiTexture flag
  31. //if (strstr(s, "GL_ARB_multitexture"))
  32. //mFlags |= Render::fMultiTexture;
  33. }
  34. // Set up Z buffer
  35. glEnable(GL_DEPTH_TEST);
  36. glDepthFunc(GL_LESS);
  37. // Set up culling
  38. glEnable(GL_CULL_FACE);
  39. glFrontFace(GL_CW);
  40. //glFrontFace(GL_CCW);
  41. //glCullFace(GL_FRONT);
  42. // Set background to black
  43. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  44. // Disable lighting
  45. glDisable(GL_LIGHTING);
  46. // Set up alpha blending
  47. glEnable(GL_BLEND);
  48. glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  49. //glEnable(GL_ALPHA_TEST); // Disable per pixel alpha blending
  50. glAlphaFunc(GL_GREATER, 0);
  51. glPointSize(5.0);
  52. // Setup shading
  53. glShadeModel(GL_SMOOTH);
  54. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  55. glHint(GL_FOG_HINT, GL_NICEST);
  56. glEnable(GL_COLOR_MATERIAL);
  57. glEnable(GL_DITHER);
  58. // AA polygon edges
  59. glEnable(GL_POLYGON_SMOOTH);
  60. glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
  61. glDisable(GL_LINE_SMOOTH);
  62. glDisable(GL_POINT_SMOOTH);
  63. glDisable(GL_AUTO_NORMAL);
  64. glDisable(GL_LOGIC_OP);
  65. glDisable(GL_TEXTURE_1D);
  66. glDisable(GL_STENCIL_TEST);
  67. glDisable(GL_FOG);
  68. glDisable(GL_NORMALIZE);
  69. glEnableClientState(GL_VERTEX_ARRAY);
  70. glDisableClientState(GL_EDGE_FLAG_ARRAY);
  71. glDisableClientState(GL_COLOR_ARRAY);
  72. glDisableClientState(GL_NORMAL_ARRAY);
  73. glPolygonMode(GL_FRONT, GL_FILL);
  74. glMatrixMode(GL_MODELVIEW);
  75. return 0;
  76. }
  77. void Window::resizeGL() {
  78. float fovY = 40.0f; // 45.0f
  79. float clipNear = 10.0f; // 4.0f
  80. float clipFar = 600000.0f; // 4000.0f
  81. glViewport(0, 0, mWidth, mHeight);
  82. glMatrixMode(GL_PROJECTION);
  83. glLoadIdentity();
  84. // Adjust clipping
  85. GLfloat fH = tanf(fovY * OR_PI / 360.0f) * clipNear;
  86. GLfloat fW = fH * ((GLfloat)mWidth) / ((GLfloat)mHeight);
  87. glFrustum(-fW, fW, -fH, fH, clipNear, clipFar);
  88. glMatrixMode(GL_MODELVIEW);
  89. glLoadIdentity();
  90. }
  91. void Window::glEnter2D() {
  92. glPushAttrib(GL_ENABLE_BIT);
  93. glDisable(GL_DEPTH_TEST);
  94. glDisable(GL_CULL_FACE);
  95. glEnable(GL_TEXTURE_2D);
  96. /* This allows alpha blending of 2D textures with the scene */
  97. glEnable(GL_BLEND);
  98. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  99. glViewport(0, 0, mWidth, mHeight);
  100. glMatrixMode(GL_PROJECTION);
  101. glPushMatrix();
  102. glLoadIdentity();
  103. glOrtho(0.0, (GLdouble)mWidth, (GLdouble)mHeight, 0.0, 0.0, 1.0);
  104. glMatrixMode(GL_MODELVIEW);
  105. glPushMatrix();
  106. glLoadIdentity();
  107. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  108. }
  109. void Window::glExit2D() {
  110. glMatrixMode(GL_MODELVIEW);
  111. glPopMatrix();
  112. glMatrixMode(GL_PROJECTION);
  113. glPopMatrix();
  114. glPopAttrib();
  115. glMatrixMode(GL_MODELVIEW);
  116. }