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 3.7KB

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