Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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