Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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