Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

387 рядки
9.5 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 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://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #if defined _XBOX
  14. # include <xtl.h>
  15. # undef near /* Fuck Microsoft */
  16. # undef far /* Fuck Microsoft again */
  17. #elif defined _WIN32
  18. # if defined USE_D3D9
  19. # include <d3d9.h>
  20. # endif
  21. # define WIN32_LEAN_AND_MEAN
  22. # include <windows.h>
  23. # undef near /* Fuck Microsoft */
  24. # undef far /* Fuck Microsoft again */
  25. #endif
  26. #include "core.h"
  27. #include "lolgl.h"
  28. using namespace std;
  29. /* FIXME: g_d3ddevice should never be exported */
  30. #if defined USE_D3D9
  31. IDirect3DDevice9 *g_d3ddevice;
  32. # if defined USE_SDL
  33. extern HWND g_hwnd;
  34. # endif
  35. #elif defined _XBOX
  36. D3DDevice *g_d3ddevice;
  37. #endif
  38. namespace lol
  39. {
  40. class VideoData
  41. {
  42. friend class Video;
  43. private:
  44. static mat4 proj_matrix;
  45. static ivec2 saved_viewport;
  46. static DebugRenderMode render_mode;
  47. #if defined USE_D3D9 || defined _XBOX
  48. # if defined USE_D3D9
  49. static IDirect3D9 *d3d_ctx;
  50. static IDirect3DDevice9 *d3d_dev;
  51. # elif defined _XBOX
  52. static Direct3D *d3d_ctx;
  53. static D3DDevice *d3d_dev;
  54. # endif
  55. static D3DCOLOR clear_color;
  56. static float clear_depth;
  57. #endif
  58. };
  59. mat4 VideoData::proj_matrix;
  60. ivec2 VideoData::saved_viewport(0, 0);
  61. DebugRenderMode VideoData::render_mode = DebugRenderMode::Default;
  62. #if defined USE_D3D9 || defined _XBOX
  63. # if defined USE_D3D9
  64. IDirect3D9 *VideoData::d3d_ctx;
  65. IDirect3DDevice9 *VideoData::d3d_dev;
  66. # elif defined _XBOX
  67. Direct3D *VideoData::d3d_ctx;
  68. D3DDevice *VideoData::d3d_dev;
  69. # endif
  70. D3DCOLOR VideoData::clear_color;
  71. float VideoData::clear_depth;
  72. #endif
  73. /*
  74. * Public Video class
  75. */
  76. void Video::Setup(ivec2 size)
  77. {
  78. #if defined USE_D3D9 || defined _XBOX
  79. VideoData::d3d_ctx = Direct3DCreate9(D3D_SDK_VERSION);
  80. if (!VideoData::d3d_ctx)
  81. {
  82. Log::Error("cannot initialise D3D\n");
  83. exit(EXIT_FAILURE);
  84. }
  85. HWND window = 0;
  86. D3DPRESENT_PARAMETERS d3dpp;
  87. memset(&d3dpp, 0, sizeof(d3dpp));
  88. # if defined USE_SDL
  89. window = g_hwnd;
  90. d3dpp.hDeviceWindow = g_hwnd;
  91. d3dpp.Windowed = TRUE;
  92. # elif defined _XBOX
  93. XVIDEO_MODE VideoMode;
  94. XGetVideoMode( &VideoMode );
  95. if (size.x > VideoMode.dwDisplayWidth)
  96. size.x = VideoMode.dwDisplayWidth;
  97. if (size.y > VideoMode.dwDisplayHeight)
  98. size.y = VideoMode.dwDisplayHeight;
  99. # endif
  100. VideoData::saved_viewport = size;
  101. d3dpp.BackBufferWidth = size.x;
  102. d3dpp.BackBufferHeight = size.y;
  103. d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  104. d3dpp.BackBufferCount = 1;
  105. d3dpp.EnableAutoDepthStencil = TRUE;
  106. d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
  107. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  108. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  109. HRESULT hr = VideoData::d3d_ctx->CreateDevice(0, D3DDEVTYPE_HAL, window,
  110. D3DCREATE_HARDWARE_VERTEXPROCESSING,
  111. &d3dpp, &VideoData::d3d_dev);
  112. if (FAILED(hr))
  113. {
  114. Log::Error("cannot create D3D device\n");
  115. exit(EXIT_FAILURE);
  116. }
  117. g_d3ddevice = VideoData::d3d_dev;
  118. #else
  119. # if defined USE_GLEW && !defined __APPLE__
  120. /* Initialise GLEW if necessary */
  121. GLenum glerr = glewInit();
  122. if (glerr != GLEW_OK)
  123. {
  124. Log::Error("cannot initialise GLEW: %s\n", glewGetErrorString(glerr));
  125. exit(EXIT_FAILURE);
  126. }
  127. # endif
  128. /* Initialise OpenGL */
  129. glViewport(0, 0, size.x, size.y);
  130. VideoData::saved_viewport = size;
  131. # if defined HAVE_GL_2X && !defined __APPLE__
  132. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  133. # endif
  134. #endif
  135. /* Initialise reasonable scene default properties */
  136. SetClearColor(vec4(0.1f, 0.2f, 0.3f, 1.0f));
  137. SetClearDepth(1.f);
  138. SetDebugRenderMode(DebugRenderMode::Default);
  139. }
  140. void Video::SetFov(float theta)
  141. {
  142. vec2 size = GetSize();
  143. float near = -size.x - size.y;
  144. float far = size.x + size.y;
  145. #if defined __ANDROID__
  146. size = vec2(640.0f, 480.0f);
  147. #endif
  148. /* Set the projection matrix */
  149. if (theta < 1e-4f)
  150. {
  151. /* The easy way: purely orthogonal projection. */
  152. VideoData::proj_matrix = mat4::ortho(0, size.x, 0, size.y, near, far);
  153. }
  154. else
  155. {
  156. /* Compute a view that approximates the glOrtho view when theta
  157. * approaches zero. This view ensures that the z=0 plane fills
  158. * the screen. */
  159. float t1 = tanf(theta / 2);
  160. float t2 = t1 * size.y / size.y;
  161. float dist = size.x / (2.0f * t1);
  162. near += dist;
  163. far += dist;
  164. if (near <= 0.0f)
  165. {
  166. far -= (near - 1.0f);
  167. near = 1.0f;
  168. }
  169. mat4 proj = mat4::frustum(-near * t1, near * t1,
  170. -near * t2, near * t2, near, far);
  171. mat4 trans = mat4::translate(-0.5f * size.x, -0.5f * size.y, -dist);
  172. VideoData::proj_matrix = proj * trans;
  173. }
  174. }
  175. void Video::SetDepth(bool set)
  176. {
  177. #if defined USE_D3D9 || defined _XBOX
  178. # define STR0(x) #x
  179. # define STR(x) STR0(x)
  180. # pragma message(__FILE__ "(" STR(__LINE__) "): warning: Video::SetDepth() not implemented")
  181. #else
  182. if (set)
  183. glEnable(GL_DEPTH_TEST);
  184. else
  185. glDisable(GL_DEPTH_TEST);
  186. #endif
  187. }
  188. void Video::SetClearColor(vec4 color)
  189. {
  190. #if defined USE_D3D9 || defined _XBOX
  191. VideoData::clear_color = D3DCOLOR_XRGB((int)(color.r * 255.999f),
  192. (int)(color.g * 255.999f),
  193. (int)(color.b * 255.999f));
  194. #else
  195. glClearColor(color.r, color.g, color.b, color.a);
  196. #endif
  197. }
  198. void Video::SetClearDepth(float f)
  199. {
  200. #if defined USE_D3D9 || defined _XBOX
  201. VideoData::clear_depth = f;
  202. #elif defined HAVE_GLES_2X
  203. glClearDepthf(f);
  204. #else
  205. glClearDepth(f);
  206. #endif
  207. }
  208. void Video::SetDebugRenderMode(DebugRenderMode d)
  209. {
  210. if (d == DebugRenderMode::Max)
  211. return;
  212. switch(d)
  213. {
  214. //All these modes are handled in the shaders.
  215. case DebugRenderMode::Default:
  216. case DebugRenderMode::Lighting:
  217. case DebugRenderMode::Normal:
  218. case DebugRenderMode::UV:
  219. {
  220. #if defined USE_D3D9 || defined _XBOX
  221. #elif defined HAVE_GLES_2X
  222. glEnable(GL_CULL_FACE);
  223. #else
  224. glEnable(GL_CULL_FACE);
  225. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  226. #endif
  227. break;
  228. }
  229. case DebugRenderMode::Wireframe:
  230. {
  231. if (VideoData::render_mode == DebugRenderMode::Wireframe)
  232. {
  233. #if defined USE_D3D9 || defined _XBOX
  234. #else
  235. if (glIsEnabled(GL_CULL_FACE) == GL_TRUE)
  236. glDisable(GL_CULL_FACE);
  237. else
  238. glEnable(GL_CULL_FACE);
  239. #endif
  240. }
  241. else
  242. {
  243. #if defined USE_D3D9 || defined _XBOX
  244. #else
  245. glDisable(GL_CULL_FACE);
  246. #endif
  247. }
  248. #if defined USE_D3D9 || defined _XBOX
  249. #elif defined HAVE_GLES_2X
  250. #else
  251. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  252. #endif
  253. break;
  254. }
  255. }
  256. VideoData::render_mode = d;
  257. }
  258. DebugRenderMode Video::GetDebugRenderMode()
  259. {
  260. return VideoData::render_mode;
  261. }
  262. void Video::Clear(ClearMask m)
  263. {
  264. #if defined USE_D3D9 || defined _XBOX
  265. int mask = 0;
  266. if (m & ClearMask::Color)
  267. mask |= D3DCLEAR_TARGET;
  268. if (m & ClearMask::Depth)
  269. mask |= D3DCLEAR_ZBUFFER;
  270. if (m & ClearMask::Stencil)
  271. mask |= D3DCLEAR_STENCIL;
  272. if (FAILED(VideoData::d3d_dev->Clear(0, NULL, mask,
  273. VideoData::clear_color,
  274. VideoData::clear_depth, 0)))
  275. Abort();
  276. #else
  277. /* FIXME: is this necessary here? */
  278. ivec2 size = GetSize();
  279. glViewport(0, 0, size.x, size.y);
  280. GLbitfield mask = 0;
  281. if (m & ClearMask::Color)
  282. mask |= GL_COLOR_BUFFER_BIT;
  283. if (m & ClearMask::Depth)
  284. mask |= GL_DEPTH_BUFFER_BIT;
  285. if (m & ClearMask::Stencil)
  286. mask |= GL_STENCIL_BUFFER_BIT;
  287. glClear(mask);
  288. #endif
  289. SetFov(0.0f);
  290. }
  291. void Video::Destroy()
  292. {
  293. ;
  294. }
  295. void Video::Capture(uint32_t *buffer)
  296. {
  297. #if defined USE_D3D9 || defined _XBOX
  298. /* TODO */
  299. #else
  300. GLint v[4];
  301. # if defined __CELLOS_LV2__
  302. // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
  303. v[2] = 1920;
  304. v[3] = 1080;
  305. # else
  306. glGetIntegerv(GL_VIEWPORT, v);
  307. # endif
  308. int width = v[2], height = v[3];
  309. # if defined HAVE_GL_2X
  310. glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  311. # endif
  312. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  313. # if defined GL_BGRA
  314. glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
  315. # else
  316. glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  317. # endif
  318. for (int j = 0; j < height / 2; j++)
  319. for (int i = 0; i < width; i++)
  320. {
  321. uint32_t tmp = buffer[j * width + i];
  322. buffer[j * width + i] = buffer[(height - j - 1) * width + i];
  323. buffer[(height - j - 1) * width + i] = tmp;
  324. }
  325. #endif
  326. }
  327. ivec2 Video::GetSize()
  328. {
  329. #if defined USE_D3D9 || defined _XBOX
  330. return VideoData::saved_viewport;
  331. #elif 1
  332. /* GetSize() is called too often on the game thread; we cannot rely on
  333. * the GL context at this point */
  334. return VideoData::saved_viewport;
  335. #elif defined __CELLOS_LV2__
  336. // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
  337. #else
  338. GLint v[4];
  339. glGetIntegerv(GL_VIEWPORT, v);
  340. return ivec2(v[2], v[3]);
  341. #endif
  342. }
  343. } /* namespace lol */