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.
 
 
 

199 lines
4.3 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. #if defined _WIN32 && !defined _XBOX
  15. # define WIN32_LEAN_AND_MEAN
  16. # include <windows.h>
  17. # undef near /* Fuck Microsoft */
  18. # undef far /* Fuck Microsoft again */
  19. #endif
  20. #include "core.h"
  21. #include "lolgl.h"
  22. using namespace std;
  23. namespace lol
  24. {
  25. class VideoData
  26. {
  27. friend class Video;
  28. private:
  29. static mat4 proj_matrix, view_matrix;
  30. static ivec2 saved_viewport;
  31. };
  32. mat4 VideoData::proj_matrix;
  33. mat4 VideoData::view_matrix;
  34. ivec2 VideoData::saved_viewport(0, 0);
  35. /*
  36. * Public Video class
  37. */
  38. void Video::Setup(ivec2 size)
  39. {
  40. #if defined USE_GLEW && !defined __APPLE__
  41. /* Initialise GLEW if necessary */
  42. GLenum glerr = glewInit();
  43. if (glerr != GLEW_OK)
  44. {
  45. Log::Error("cannot initialise GLEW: %s\n", glewGetErrorString(glerr));
  46. exit(EXIT_FAILURE);
  47. }
  48. #endif
  49. /* Initialise OpenGL */
  50. glViewport(0, 0, size.x, size.y);
  51. VideoData::saved_viewport = size;
  52. glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
  53. glClearDepth(1.0);
  54. #if defined HAVE_GL_2X && !defined __APPLE__
  55. glShadeModel(GL_SMOOTH);
  56. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  57. #endif
  58. }
  59. void Video::SetFov(float theta)
  60. {
  61. vec2 size = GetSize();
  62. float near = -size.x - size.y;
  63. float far = size.x + size.y;
  64. #if defined __ANDROID__
  65. size = vec2(640.0f, 480.0f);
  66. #endif
  67. /* Set the projection matrix */
  68. if (theta < 1e-4f)
  69. {
  70. /* The easy way: purely orthogonal projection. */
  71. VideoData::proj_matrix = mat4::ortho(0, size.x, 0, size.y, near, far);
  72. }
  73. else
  74. {
  75. /* Compute a view that approximates the glOrtho view when theta
  76. * approaches zero. This view ensures that the z=0 plane fills
  77. * the screen. */
  78. float t1 = tanf(theta / 2);
  79. float t2 = t1 * size.y / size.y;
  80. float dist = size.x / (2.0f * t1);
  81. near += dist;
  82. far += dist;
  83. if (near <= 0.0f)
  84. {
  85. far -= (near - 1.0f);
  86. near = 1.0f;
  87. }
  88. mat4 proj = mat4::frustum(-near * t1, near * t1,
  89. -near * t2, near * t2, near, far);
  90. mat4 trans = mat4::translate(-0.5f * size.x, -0.5f * size.y, -dist);
  91. VideoData::proj_matrix = proj * trans;
  92. }
  93. VideoData::view_matrix = mat4(1.0f);
  94. }
  95. void Video::SetDepth(bool set)
  96. {
  97. if (set)
  98. glEnable(GL_DEPTH_TEST);
  99. else
  100. glDisable(GL_DEPTH_TEST);
  101. }
  102. void Video::Clear()
  103. {
  104. ivec2 size = GetSize();
  105. glViewport(0, 0, size.x, size.y);
  106. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  107. SetFov(0.0f);
  108. }
  109. void Video::Destroy()
  110. {
  111. ;
  112. }
  113. void Video::Capture(uint32_t *buffer)
  114. {
  115. GLint v[4];
  116. #if defined __CELLOS_LV2__
  117. // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
  118. v[2] = 1920;
  119. v[3] = 1080;
  120. #else
  121. glGetIntegerv(GL_VIEWPORT, v);
  122. #endif
  123. int width = v[2], height = v[3];
  124. #if defined HAVE_GL_2X
  125. glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  126. #endif
  127. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  128. #if defined GL_BGRA
  129. glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
  130. #else
  131. glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  132. #endif
  133. for (int j = 0; j < height / 2; j++)
  134. for (int i = 0; i < width; i++)
  135. {
  136. uint32_t tmp = buffer[j * width + i];
  137. buffer[j * width + i] = buffer[(height - j - 1) * width + i];
  138. buffer[(height - j - 1) * width + i] = tmp;
  139. }
  140. }
  141. ivec2 Video::GetSize()
  142. {
  143. /* GetSize() is called too often on the game thread; we cannot rely on
  144. * the GL context at this point */
  145. #if 1
  146. return VideoData::saved_viewport;
  147. #elif defined __CELLOS_LV2__
  148. // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
  149. #else
  150. GLint v[4];
  151. glGetIntegerv(GL_VIEWPORT, v);
  152. return ivec2(v[2], v[3]);
  153. #endif
  154. }
  155. mat4 const & Video::GetProjMatrix()
  156. {
  157. return VideoData::proj_matrix;
  158. }
  159. mat4 const & Video::GetViewMatrix()
  160. {
  161. return VideoData::view_matrix;
  162. }
  163. } /* namespace lol */