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.
 
 
 

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