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.
 
 
 

152 line
3.4 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cmath>
  14. #ifdef WIN32
  15. # define WIN32_LEAN_AND_MEAN
  16. # include <windows.h>
  17. #endif
  18. #if defined __APPLE__ && defined __MACH__
  19. # include <OpenGL/gl.h>
  20. #else
  21. # define GL_GLEXT_PROTOTYPES
  22. # include <GL/gl.h>
  23. #endif
  24. #include "core.h"
  25. /*
  26. * Public Video class
  27. */
  28. void Video::Setup(int width, int height)
  29. {
  30. /* Initialise OpenGL */
  31. glViewport(0, 0, width, height);
  32. glEnable(GL_TEXTURE_2D);
  33. glShadeModel(GL_SMOOTH);
  34. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  35. glClearDepth(1.0);
  36. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  37. }
  38. void Video::SetFov(float theta)
  39. {
  40. #undef near /* Fuck Microsoft */
  41. #undef far /* Fuck Microsoft again */
  42. glMatrixMode(GL_PROJECTION);
  43. glLoadIdentity();
  44. float width = GetWidth();
  45. float height = GetHeight();
  46. float near = -width - height;
  47. float far = width + height;
  48. /* Set the projection matrix */
  49. if (theta < 1e-4f)
  50. {
  51. /* The easy way: purely orthogonal projection. */
  52. glOrtho(0, width, 0, height, near, far);
  53. }
  54. else
  55. {
  56. /* Compute a view that approximates the glOrtho view when theta
  57. * approaches zero. This view ensures that the z=0 plane fills
  58. * the screen. */
  59. float t1 = tanf(theta / 2);
  60. float t2 = t1 * height / width;
  61. float dist = (float)width / (2.0f * t1);
  62. near += dist;
  63. far += dist;
  64. if (near <= 0.0f)
  65. {
  66. far -= (near - 1.0f);
  67. near = 1.0f;
  68. }
  69. glFrustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
  70. glTranslatef(-0.5f * width, -0.5f * height, -dist);
  71. }
  72. /* Reset the model view matrix, just in case */
  73. glMatrixMode(GL_MODELVIEW);
  74. glLoadIdentity();
  75. }
  76. void Video::SetDepth(bool set)
  77. {
  78. if (set)
  79. glEnable(GL_DEPTH_TEST);
  80. else
  81. glDisable(GL_DEPTH_TEST);
  82. }
  83. void Video::Clear()
  84. {
  85. glEnable(GL_DEPTH_TEST);
  86. glDepthFunc(GL_LEQUAL);
  87. glEnable(GL_ALPHA_TEST);
  88. glAlphaFunc(GL_GEQUAL, 0.01f);
  89. glEnable(GL_BLEND);
  90. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  91. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  92. SetFov(0.0f);
  93. }
  94. void Video::Capture(uint32_t *buffer)
  95. {
  96. GLint v[4];
  97. glGetIntegerv(GL_VIEWPORT, v);
  98. int width = v[2], height = v[3];
  99. glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  100. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  101. #if defined GL_BGRA
  102. glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
  103. #else
  104. glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  105. #endif
  106. for (int j = 0; j < height / 2; j++)
  107. for (int i = 0; i < width; i++)
  108. {
  109. uint32_t tmp = buffer[j * width + i];
  110. buffer[j * width + i] = buffer[(height - j - 1) * width + i];
  111. buffer[(height - j - 1) * width + i] = tmp;
  112. }
  113. }
  114. int Video::GetWidth()
  115. {
  116. GLint v[4];
  117. glGetIntegerv(GL_VIEWPORT, v);
  118. return v[2];
  119. }
  120. int Video::GetHeight()
  121. {
  122. GLint v[4];
  123. glGetIntegerv(GL_VIEWPORT, v);
  124. return v[3];
  125. }