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

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