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.
 
 
 

133 lines
2.8 KiB

  1. //
  2. // Deus Hax (working title)
  3. // Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
  4. //
  5. #if defined HAVE_CONFIG_H
  6. # include "config.h"
  7. #endif
  8. #include <math.h>
  9. #ifdef WIN32
  10. # define WIN32_LEAN_AND_MEAN
  11. # include <windows.h>
  12. #endif
  13. #if defined __APPLE__ && defined __MACH__
  14. # include <OpenGL/gl.h>
  15. #else
  16. # define GL_GLEXT_PROTOTYPES
  17. # include <GL/gl.h>
  18. #endif
  19. #include "core.h"
  20. /*
  21. * Public Video class
  22. */
  23. void Video::Setup(int width, int height)
  24. {
  25. /* Initialise OpenGL */
  26. glViewport(0, 0, width, height);
  27. glEnable(GL_TEXTURE_2D);
  28. glShadeModel(GL_SMOOTH);
  29. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  30. glClearDepth(1.0);
  31. glEnable(GL_DEPTH_TEST);
  32. glDepthFunc(GL_LEQUAL);
  33. glEnable(GL_ALPHA_TEST);
  34. glAlphaFunc(GL_GEQUAL, 0.01f);
  35. glEnable(GL_BLEND);
  36. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  37. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  38. }
  39. void Video::SetFov(float theta)
  40. {
  41. glMatrixMode(GL_PROJECTION);
  42. glLoadIdentity();
  43. float width = GetWidth();
  44. float height = GetHeight();
  45. float near = -width - height;
  46. float far = width + height;
  47. /* Set the projection matrix */
  48. if (theta < 1e-4f)
  49. {
  50. /* The easy way: purely orthogonal projection. */
  51. glOrtho(0, width, 0, height, near, far);
  52. }
  53. else
  54. {
  55. /* Compute a view that approximates the glOrtho view when theta
  56. * approaches zero. This view ensures that the z=0 plane fills
  57. * the screen. */
  58. float t1 = tanf(theta / 2);
  59. float t2 = t1 * height / width;
  60. float dist = (float)width / (2.0f * t1);
  61. near += dist;
  62. far += dist;
  63. if (near <= 0.0f)
  64. {
  65. far -= (near - 1.0f);
  66. near = 1.0f;
  67. }
  68. glFrustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
  69. glTranslatef(-0.5f * width, -0.5f * height, -dist);
  70. }
  71. /* Reset the model view matrix, just in case */
  72. glMatrixMode(GL_MODELVIEW);
  73. glLoadIdentity();
  74. }
  75. void Video::Clear()
  76. {
  77. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  78. SetFov(0.0f);
  79. }
  80. void Video::Capture(uint32_t *buffer)
  81. {
  82. GLint v[4];
  83. glGetIntegerv(GL_VIEWPORT, v);
  84. int width = v[2], height = v[3];
  85. glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  86. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  87. glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
  88. for (int j = 0; j < height / 2; j++)
  89. for (int i = 0; i < width; i++)
  90. {
  91. uint32_t tmp = buffer[j * width + i];
  92. buffer[j * width + i] = buffer[(height - j - 1) * width + i];
  93. buffer[(height - j - 1) * width + i] = tmp;
  94. }
  95. }
  96. int Video::GetWidth()
  97. {
  98. GLint v[4];
  99. glGetIntegerv(GL_VIEWPORT, v);
  100. return v[2];
  101. }
  102. int Video::GetHeight()
  103. {
  104. GLint v[4];
  105. glGetIntegerv(GL_VIEWPORT, v);
  106. return v[3];
  107. }