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.
 
 
 

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