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.
 
 
 

266 line
6.3 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2012 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 _XBOX
  15. # include <xtl.h>
  16. # undef near /* Fuck Microsoft */
  17. # undef far /* Fuck Microsoft again */
  18. #elif defined _WIN32
  19. # define WIN32_LEAN_AND_MEAN
  20. # include <windows.h>
  21. # undef near /* Fuck Microsoft */
  22. # undef far /* Fuck Microsoft again */
  23. #endif
  24. #include "core.h"
  25. #include "lolgl.h"
  26. using namespace std;
  27. namespace lol
  28. {
  29. class VideoData
  30. {
  31. friend class Video;
  32. private:
  33. static mat4 proj_matrix, view_matrix;
  34. static ivec2 saved_viewport;
  35. #if defined _XBOX
  36. static Direct3D *d3d_ctx;
  37. static D3DDevice *d3d_dev;
  38. #endif
  39. };
  40. mat4 VideoData::proj_matrix;
  41. mat4 VideoData::view_matrix;
  42. ivec2 VideoData::saved_viewport(0, 0);
  43. #if defined _XBOX
  44. Direct3D *VideoData::d3d_ctx;
  45. D3DDevice *VideoData::d3d_dev;
  46. #endif
  47. /*
  48. * Public Video class
  49. */
  50. void Video::Setup(ivec2 size)
  51. {
  52. #if defined _XBOX
  53. VideoData::d3d_ctx = Direct3DCreate9(D3D_SDK_VERSION);
  54. if (!VideoData::d3d_ctx)
  55. {
  56. Log::Error("cannot initialise D3D\n");
  57. exit(EXIT_FAILURE);
  58. }
  59. D3DPRESENT_PARAMETERS d3dpp;
  60. memset(&d3dpp, 0, sizeof(d3dpp));
  61. XVIDEO_MODE VideoMode;
  62. XGetVideoMode( &VideoMode );
  63. if (size.x > VideoMode.dwDisplayWidth)
  64. size.x = VideoMode.dwDisplayWidth;
  65. if (size.y > VideoMode.dwDisplayHeight)
  66. size.y = VideoMode.dwDisplayHeight;
  67. VideoData::saved_viewport = size;
  68. d3dpp.BackBufferWidth = size.x;
  69. d3dpp.BackBufferHeight = size.y;
  70. d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  71. d3dpp.BackBufferCount = 1;
  72. d3dpp.EnableAutoDepthStencil = TRUE;
  73. d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
  74. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  75. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  76. if (!VideoData::d3d_ctx->CreateDevice(0, D3DDEVTYPE_HAL, NULL,
  77. D3DCREATE_HARDWARE_VERTEXPROCESSING,
  78. &d3dpp, &VideoData::d3d_dev))
  79. {
  80. Log::Error("cannot create D3D device\n");
  81. exit(EXIT_FAILURE);
  82. }
  83. #else
  84. # if defined USE_GLEW && !defined __APPLE__
  85. /* Initialise GLEW if necessary */
  86. GLenum glerr = glewInit();
  87. if (glerr != GLEW_OK)
  88. {
  89. Log::Error("cannot initialise GLEW: %s\n", glewGetErrorString(glerr));
  90. exit(EXIT_FAILURE);
  91. }
  92. # endif
  93. /* Initialise OpenGL */
  94. glViewport(0, 0, size.x, size.y);
  95. VideoData::saved_viewport = size;
  96. glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
  97. glClearDepth(1.0);
  98. # if defined HAVE_GL_2X && !defined __APPLE__
  99. glShadeModel(GL_SMOOTH);
  100. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  101. # endif
  102. #endif
  103. }
  104. void Video::SetFov(float theta)
  105. {
  106. vec2 size = GetSize();
  107. float near = -size.x - size.y;
  108. float far = size.x + size.y;
  109. #if defined __ANDROID__
  110. size = vec2(640.0f, 480.0f);
  111. #endif
  112. /* Set the projection matrix */
  113. if (theta < 1e-4f)
  114. {
  115. /* The easy way: purely orthogonal projection. */
  116. VideoData::proj_matrix = mat4::ortho(0, size.x, 0, size.y, near, far);
  117. }
  118. else
  119. {
  120. /* Compute a view that approximates the glOrtho view when theta
  121. * approaches zero. This view ensures that the z=0 plane fills
  122. * the screen. */
  123. float t1 = tanf(theta / 2);
  124. float t2 = t1 * size.y / size.y;
  125. float dist = size.x / (2.0f * t1);
  126. near += dist;
  127. far += dist;
  128. if (near <= 0.0f)
  129. {
  130. far -= (near - 1.0f);
  131. near = 1.0f;
  132. }
  133. mat4 proj = mat4::frustum(-near * t1, near * t1,
  134. -near * t2, near * t2, near, far);
  135. mat4 trans = mat4::translate(-0.5f * size.x, -0.5f * size.y, -dist);
  136. VideoData::proj_matrix = proj * trans;
  137. }
  138. VideoData::view_matrix = mat4(1.0f);
  139. }
  140. void Video::SetDepth(bool set)
  141. {
  142. #if defined _XBOX
  143. # define STR0(x) #x
  144. # define STR(x) STR0(x)
  145. # pragma message(__FILE__ "(" STR(__LINE__) "): warning: Video::SetDepth() not implemented")
  146. #else
  147. if (set)
  148. glEnable(GL_DEPTH_TEST);
  149. else
  150. glDisable(GL_DEPTH_TEST);
  151. #endif
  152. }
  153. void Video::Clear()
  154. {
  155. ivec2 size = GetSize();
  156. #if defined _XBOX
  157. VideoData::d3d_dev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
  158. | D3DCLEAR_STENCIL,
  159. D3DCOLOR_XRGB(26, 51, 77), 1.0f, 0);
  160. #else
  161. glViewport(0, 0, size.x, size.y);
  162. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  163. #endif
  164. SetFov(0.0f);
  165. }
  166. void Video::Destroy()
  167. {
  168. ;
  169. }
  170. void Video::Capture(uint32_t *buffer)
  171. {
  172. #if _XBOX
  173. /* TODO */
  174. #else
  175. GLint v[4];
  176. # if defined __CELLOS_LV2__
  177. // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
  178. v[2] = 1920;
  179. v[3] = 1080;
  180. # else
  181. glGetIntegerv(GL_VIEWPORT, v);
  182. # endif
  183. int width = v[2], height = v[3];
  184. # if defined HAVE_GL_2X
  185. glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  186. # endif
  187. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  188. # if defined GL_BGRA
  189. glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
  190. # else
  191. glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  192. # endif
  193. for (int j = 0; j < height / 2; j++)
  194. for (int i = 0; i < width; i++)
  195. {
  196. uint32_t tmp = buffer[j * width + i];
  197. buffer[j * width + i] = buffer[(height - j - 1) * width + i];
  198. buffer[(height - j - 1) * width + i] = tmp;
  199. }
  200. #endif
  201. }
  202. ivec2 Video::GetSize()
  203. {
  204. #if _XBOX
  205. return VideoData::saved_viewport;
  206. #elif 1
  207. /* GetSize() is called too often on the game thread; we cannot rely on
  208. * the GL context at this point */
  209. return VideoData::saved_viewport;
  210. #elif defined __CELLOS_LV2__
  211. // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
  212. #else
  213. GLint v[4];
  214. glGetIntegerv(GL_VIEWPORT, v);
  215. return ivec2(v[2], v[3]);
  216. #endif
  217. }
  218. mat4 const & Video::GetProjMatrix()
  219. {
  220. return VideoData::proj_matrix;
  221. }
  222. mat4 const & Video::GetViewMatrix()
  223. {
  224. return VideoData::view_matrix;
  225. }
  226. } /* namespace lol */