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.

преди 13 години
преди 12 години
преди 12 години
преди 12 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. //
  2. // Lol Engine - Fractal tutorial
  3. //
  4. // Copyright: (c) 2011-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 <cstring>
  14. #include "core.h"
  15. #include "lolgl.h"
  16. #include "loldebug.h"
  17. using namespace lol;
  18. #if defined _WIN32
  19. # include <direct.h>
  20. # if defined USE_D3D9
  21. # define FAR
  22. # define NEAR
  23. # include <d3d9.h>
  24. # endif
  25. #endif
  26. extern char const *lolfx_11_fractal;
  27. #if defined USE_D3D9
  28. extern IDirect3DDevice9 *g_d3ddevice;
  29. #elif defined _XBOX
  30. extern D3DDevice *g_d3ddevice;
  31. #elif __CELLOS_LV2__
  32. static GLint const INTERNAL_FORMAT = GL_ARGB_SCE;
  33. static GLenum const TEXTURE_FORMAT = GL_BGRA;
  34. static GLenum const TEXTURE_TYPE = GL_UNSIGNED_INT_8_8_8_8_REV;
  35. #elif defined __native_client__ || defined HAVE_GLES_2X
  36. static GLint const INTERNAL_FORMAT = GL_RGBA;
  37. static GLenum const TEXTURE_FORMAT = GL_RGBA;
  38. static GLenum const TEXTURE_TYPE = GL_UNSIGNED_BYTE;
  39. #else
  40. /* Seems efficient for little endian textures */
  41. static GLint const INTERNAL_FORMAT = GL_RGBA;
  42. static GLenum const TEXTURE_FORMAT = GL_BGRA;
  43. static GLenum const TEXTURE_TYPE = GL_UNSIGNED_INT_8_8_8_8_REV;
  44. #endif
  45. class Fractal : public WorldEntity
  46. {
  47. public:
  48. Fractal(ivec2 const &size)
  49. {
  50. /* Ensure texture size is a multiple of 16 for better aligned
  51. * data access. Store the dimensions of a texel for our shader,
  52. * as well as the half-size of the screen. */
  53. m_size = size;
  54. m_size.x = (m_size.x + 15) & ~15;
  55. m_size.y = (m_size.y + 15) & ~15;
  56. m_texel_settings = vec4(1.0, 1.0, 2.0, 2.0) / m_size.xyxy;
  57. m_screen_settings = vec4(1.0, 1.0, 0.5, 0.5) * m_size.xyxy;
  58. /* Window size decides the world aspect ratio. For instance, 640×480
  59. * will be mapped to (-0.66,-0.5) - (0.66,0.5). */
  60. #if !defined __native_client__
  61. m_window_size = Video::GetSize();
  62. #else
  63. /* FIXME: it's illegal to call this on the game thread! */
  64. m_window_size = ivec2(640, 480);
  65. #endif
  66. if (m_window_size.y < m_window_size.x)
  67. m_window2world = 0.5 / m_window_size.y;
  68. else
  69. m_window2world = 0.5 / m_window_size.x;
  70. m_texel2world = (vec2)m_window_size / m_size * m_window2world;
  71. m_oldmouse = ivec2(0, 0);
  72. m_pixels.Resize(m_size.x * m_size.y);
  73. m_frame = -1;
  74. m_slices = 4;
  75. for (int i = 0; i < 4; i++)
  76. {
  77. m_deltashift[i] = real("0");
  78. m_deltascale[i] = real("1");
  79. m_dirty[i] = 2;
  80. }
  81. #if defined __CELLOS_LV2__ || defined _XBOX
  82. //m_center = rcmplx(-.22815528839841, -1.11514249704382);
  83. //m_center = rcmplx(0.001643721971153, 0.822467633298876);
  84. m_center = rcmplx("-0.65823419062254", "0.50221777363480");
  85. m_zoom_speed = -0.025;
  86. #else
  87. m_center = rcmplx(-0.75, 0.0);
  88. m_zoom_speed = 0.0;
  89. #endif
  90. m_translate = rcmplx(0.0, 0.0);
  91. m_radius = 5.0;
  92. m_ready = false;
  93. m_drag = false;
  94. for (int i = 0; i < (MAX_ITERATIONS + 1) * PALETTE_STEP; i++)
  95. {
  96. double f = (double)i / PALETTE_STEP;
  97. double r = 0.5 * lol::sin(f * 0.27 + 2.0) + 0.5;
  98. double g = 0.5 * lol::sin(f * 0.17 - 1.8) + 0.5;
  99. double b = 0.5 * lol::sin(f * 0.21 - 2.6) + 0.5;
  100. if (f < 7.0)
  101. {
  102. f = f < 1.0 ? 0.0 : (f - 1.0) / 6.0;
  103. r *= f;
  104. g *= f;
  105. b *= f;
  106. }
  107. uint8_t red = r * 255.99f;
  108. uint8_t green = g * 255.99f;
  109. uint8_t blue = b * 255.99f;
  110. #if defined __CELLOS_LV2__ || defined _XBOX
  111. m_palette.Push(u8vec4(255, red, green, blue));
  112. #elif defined __native_client__
  113. m_palette.Push(u8vec4(red, green, blue, 255));
  114. #else
  115. m_palette.Push(u8vec4(blue, green, red, 255));
  116. #endif
  117. }
  118. #if !defined __native_client__
  119. m_centertext = new Text(NULL, "src/data/font/ascii.png");
  120. m_centertext->SetPos(ivec3(5, m_window_size.y - 15, 1));
  121. Ticker::Ref(m_centertext);
  122. m_mousetext = new Text(NULL, "src/data/font/ascii.png");
  123. m_mousetext->SetPos(ivec3(5, m_window_size.y - 29, 1));
  124. Ticker::Ref(m_mousetext);
  125. m_zoomtext = new Text(NULL, "src/data/font/ascii.png");
  126. m_zoomtext->SetPos(ivec3(5, m_window_size.y - 43, 1));
  127. Ticker::Ref(m_zoomtext);
  128. #endif
  129. m_position = ivec3(0, 0, 0);
  130. m_bbox[0] = m_position;
  131. m_bbox[1] = ivec3(m_window_size, 0);
  132. Input::TrackMouse(this);
  133. /* Spawn worker threads and wait for their readiness. */
  134. for (int i = 0; i < MAX_THREADS; i++)
  135. m_threads[i] = new Thread(DoWorkHelper, this);
  136. for (int i = 0; i < MAX_THREADS; i++)
  137. m_spawnqueue.Pop();
  138. }
  139. ~Fractal()
  140. {
  141. /* Signal worker threads for completion and wait for
  142. * them to quit. */
  143. for (int i = 0; i < MAX_THREADS; i++)
  144. m_jobqueue.Push(-1);
  145. for (int i = 0; i < MAX_THREADS; i++)
  146. m_donequeue.Pop();
  147. Input::UntrackMouse(this);
  148. #if !defined __native_client__
  149. Ticker::Unref(m_centertext);
  150. Ticker::Unref(m_mousetext);
  151. Ticker::Unref(m_zoomtext);
  152. #endif
  153. }
  154. inline dcmplx TexelToWorldOffset(vec2 texel)
  155. {
  156. double dx = (0.5 + texel.x - m_size.x / 2) * m_texel2world.x;
  157. double dy = (0.5 + m_size.y / 2 - texel.y) * m_texel2world.y;
  158. return m_radius * dcmplx(dx, dy);
  159. }
  160. inline dcmplx ScreenToWorldOffset(vec2 pixel)
  161. {
  162. /* No 0.5 offset here, because we want to be able to position the
  163. * mouse at (0,0) exactly. */
  164. double dx = pixel.x - m_window_size.x / 2;
  165. double dy = m_window_size.y / 2 - pixel.y;
  166. return m_radius * m_window2world * dcmplx(dx, dy);
  167. }
  168. virtual void TickGame(float seconds)
  169. {
  170. WorldEntity::TickGame(seconds);
  171. int prev_frame = (m_frame + 4) % 4;
  172. m_frame = (m_frame + 1) % 4;
  173. rcmplx worldmouse = m_center + rcmplx(ScreenToWorldOffset(m_mousepos));
  174. ivec3 buttons = Input::GetMouseButtons();
  175. #if !defined __CELLOS_LV2__ && !defined _XBOX
  176. if (buttons[1])
  177. {
  178. if (!m_drag)
  179. {
  180. m_oldmouse = m_mousepos;
  181. m_drag = true;
  182. }
  183. m_translate = ScreenToWorldOffset(m_oldmouse)
  184. - ScreenToWorldOffset(m_mousepos);
  185. /* XXX: the purpose of this hack is to avoid translating by
  186. * an exact number of pixels. If this were to happen, the step()
  187. * optimisation for i915 cards in our shader would behave
  188. * incorrectly because a quarter of the pixels in the image
  189. * would have tie rankings in the distance calculation. */
  190. m_translate *= real(1023.0 / 1024.0);
  191. m_oldmouse = m_mousepos;
  192. }
  193. else
  194. {
  195. m_drag = false;
  196. if (m_translate != rcmplx(0.0, 0.0))
  197. {
  198. m_translate *= real(std::pow(2.0, -seconds * 5.0));
  199. if ((double)m_translate.norm() < m_radius * 1e-4)
  200. m_translate = rcmplx(0.0, 0.0);
  201. }
  202. }
  203. if ((buttons[0] || buttons[2]) && m_mousepos.x != -1)
  204. {
  205. double zoom = buttons[0] ? -0.5 : 0.5;
  206. m_zoom_speed += zoom * seconds;
  207. if (m_zoom_speed / zoom > 5e-3f)
  208. m_zoom_speed = zoom * 5e-3f;
  209. }
  210. else if (m_zoom_speed)
  211. {
  212. m_zoom_speed *= std::pow(2.0, -seconds * 5.0);
  213. if (abs(m_zoom_speed) < 1e-5 || m_drag)
  214. m_zoom_speed = 0.0;
  215. }
  216. #endif
  217. if (m_zoom_speed || m_translate != rcmplx(0.0, 0.0))
  218. {
  219. rcmplx oldcenter = m_center;
  220. double oldradius = m_radius;
  221. double zoom = std::pow(2.0, seconds * 1e3f * m_zoom_speed);
  222. if (m_radius * zoom > 8.0)
  223. {
  224. m_zoom_speed *= -1.0;
  225. zoom = 8.0 / m_radius;
  226. }
  227. else if (m_radius * zoom < 1e-14)
  228. {
  229. m_zoom_speed *= -1.0;
  230. zoom = 1e-14 / m_radius;
  231. }
  232. m_radius *= zoom;
  233. #if !defined __CELLOS_LV2__ && !defined _XBOX
  234. m_center += m_translate;
  235. m_center = (m_center - worldmouse) * real(zoom) + worldmouse;
  236. worldmouse = m_center + rcmplx(ScreenToWorldOffset(m_mousepos));
  237. #endif
  238. /* Store the transformation properties to go from m_frame - 1
  239. * to m_frame. */
  240. m_deltashift[prev_frame] = (m_center - oldcenter) / real(oldradius);
  241. m_deltashift[prev_frame].x /= m_size.x * m_texel2world.x;
  242. m_deltashift[prev_frame].y /= m_size.y * m_texel2world.y;
  243. m_deltascale[prev_frame] = m_radius / oldradius;
  244. m_dirty[0] = m_dirty[1] = m_dirty[2] = m_dirty[3] = 2;
  245. }
  246. else
  247. {
  248. /* If settings didn't change, set transformation from previous
  249. * frame to identity. */
  250. m_deltashift[prev_frame] = real::R_0();
  251. m_deltascale[prev_frame] = real::R_1();
  252. }
  253. /* Transformation from current frame to current frame is always
  254. * identity. */
  255. m_zoom_settings[m_frame][0] = 0.0f;
  256. m_zoom_settings[m_frame][1] = 0.0f;
  257. m_zoom_settings[m_frame][2] = 1.0f;
  258. /* Compute transformation from other frames to current frame */
  259. for (int i = 0; i < 3; i++)
  260. {
  261. int prev_index = (m_frame + 4 - i) % 4;
  262. int cur_index = (m_frame + 3 - i) % 4;
  263. m_zoom_settings[cur_index][0] = (real)m_zoom_settings[prev_index][0] * m_deltascale[cur_index] + m_deltashift[cur_index].x;
  264. m_zoom_settings[cur_index][1] = (real)m_zoom_settings[prev_index][1] * m_deltascale[cur_index] + m_deltashift[cur_index].y;
  265. m_zoom_settings[cur_index][2] = (real)m_zoom_settings[prev_index][2] * m_deltascale[cur_index];
  266. }
  267. /* Precompute texture offset change instead of doing it in GLSL */
  268. for (int i = 0; i < 4; i++)
  269. {
  270. m_zoom_settings[i][0] += 0.5 * (1.0 - m_zoom_settings[i][2]);
  271. m_zoom_settings[i][1] -= 0.5 * (1.0 - m_zoom_settings[i][2]);
  272. }
  273. #if !defined __native_client__
  274. char buf[256];
  275. sprintf(buf, "center: ");
  276. m_center.x.sprintf(buf + strlen(buf), 30);
  277. sprintf(buf + strlen(buf), " ");
  278. m_center.y.sprintf(buf + strlen(buf), 30);
  279. m_centertext->SetText(buf);
  280. sprintf(buf, " mouse: ");
  281. worldmouse.x.sprintf(buf + strlen(buf), 30);
  282. sprintf(buf + strlen(buf), " ");
  283. worldmouse.y.sprintf(buf + strlen(buf), 30);
  284. m_mousetext->SetText(buf);
  285. sprintf(buf, " zoom: %g", 1.0 / m_radius);
  286. m_zoomtext->SetText(buf);
  287. #endif
  288. if (m_dirty[m_frame])
  289. {
  290. m_dirty[m_frame]--;
  291. for (int i = 0; i < m_size.y; i += MAX_LINES * 2)
  292. m_jobqueue.Push(i);
  293. }
  294. }
  295. static void *DoWorkHelper(void *data)
  296. {
  297. Fractal *that = (Fractal *)data;
  298. that->m_spawnqueue.Push(0);
  299. for ( ; ; )
  300. {
  301. int line = that->m_jobqueue.Pop();
  302. if (line == -1)
  303. break;
  304. that->DoWork(line);
  305. that->m_donequeue.Push(0);
  306. }
  307. that->m_donequeue.Push(0);
  308. return NULL;
  309. };
  310. void DoWork(int line)
  311. {
  312. double const maxsqlen = 1024;
  313. double const k1 = 1.0 / (1 << 10) / (std::log(maxsqlen) / std::log(2.0));
  314. int jmin = ((m_frame + 1) % 4) / 2 + line;
  315. int jmax = jmin + MAX_LINES * 2;
  316. if (jmax > m_size.y)
  317. jmax = m_size.y;
  318. u8vec4 *m_pixelstart = &m_pixels[0]
  319. + m_size.x * (m_size.y / 4 * m_frame + line / 4);
  320. dcmplx c = (dcmplx)m_center;
  321. for (int j = jmin; j < jmax; j += 2)
  322. for (int i = m_frame % 2; i < m_size.x; i += 2)
  323. {
  324. double xr, yr, x0, y0, x1, y1, x2, y2, x3, y3;
  325. dcmplx z0 = c + TexelToWorldOffset(ivec2(i, j));
  326. //dcmplx r0(0.28693186889504513, 0.014286693904085048);
  327. //dcmplx r0(0.001643721971153, 0.822467633298876);
  328. //dcmplx r0(-1.207205434596, 0.315432814901);
  329. //dcmplx r0(-0.79192956889854, -0.14632423080102);
  330. //dcmplx r0(0.3245046418497685, 0.04855101129280834);
  331. dcmplx r0 = z0;
  332. x0 = z0.x; y0 = z0.y;
  333. xr = r0.x; yr = r0.y;
  334. int iter = MAX_ITERATIONS - 4;
  335. for (;;)
  336. {
  337. /* Unroll the loop: tests are more expensive to do at each
  338. * iteration than the few extra multiplications. */
  339. x1 = x0 * x0 - y0 * y0 + xr;
  340. y1 = x0 * y0 + x0 * y0 + yr;
  341. x2 = x1 * x1 - y1 * y1 + xr;
  342. y2 = x1 * y1 + x1 * y1 + yr;
  343. x3 = x2 * x2 - y2 * y2 + xr;
  344. y3 = x2 * y2 + x2 * y2 + yr;
  345. x0 = x3 * x3 - y3 * y3 + xr;
  346. y0 = x3 * y3 + x3 * y3 + yr;
  347. if (x0 * x0 + y0 * y0 >= maxsqlen)
  348. break;
  349. iter -= 4;
  350. if (iter < 4)
  351. break;
  352. }
  353. if (iter)
  354. {
  355. double n = x0 * x0 + y0 * y0;
  356. if (x1 * x1 + y1 * y1 >= maxsqlen)
  357. {
  358. iter += 3; n = x1 * x1 + y1 * y1;
  359. }
  360. else if (x2 * x2 + y2 * y2 >= maxsqlen)
  361. {
  362. iter += 2; n = x2 * x2 + y2 * y2;
  363. }
  364. else if (x3 * x3 + y3 * y3 >= maxsqlen)
  365. {
  366. iter += 1; n = x3 * x3 + y3 * y3;
  367. }
  368. if (n > maxsqlen * maxsqlen)
  369. n = maxsqlen * maxsqlen;
  370. /* Approximate log(sqrt(n))/log(sqrt(maxsqlen)) */
  371. double f = iter;
  372. union { double n; uint64_t x; } u = { n };
  373. double k = (u.x >> 42) - (((1 << 10) - 1) << 10);
  374. k *= k1;
  375. /* Approximate log2(k) in [1,2]. */
  376. f += (- 0.344847817623168308695977510213252644185 * k
  377. + 2.024664188044341212602376988171727038739) * k
  378. - 1.674876738008591047163498125918330313237;
  379. *m_pixelstart++ = m_palette[(int)(f * PALETTE_STEP)];
  380. }
  381. else
  382. {
  383. #if defined __CELLOS_LV2__ || defined _XBOX
  384. *m_pixelstart++ = u8vec4(255, 0, 0, 0);
  385. #else
  386. *m_pixelstart++ = u8vec4(0, 0, 0, 255);
  387. #endif
  388. }
  389. }
  390. }
  391. virtual void TickDraw(float seconds)
  392. {
  393. WorldEntity::TickDraw(seconds);
  394. static float const vertices[] =
  395. {
  396. 1.0f, 1.0f,
  397. -1.0f, 1.0f,
  398. -1.0f, -1.0f,
  399. -1.0f, -1.0f,
  400. 1.0f, -1.0f,
  401. 1.0f, 1.0f,
  402. };
  403. static float const texcoords[] =
  404. {
  405. 1.0f, 1.0f,
  406. 0.0f, 1.0f,
  407. 0.0f, 0.0f,
  408. 0.0f, 0.0f,
  409. 1.0f, 0.0f,
  410. 1.0f, 1.0f,
  411. };
  412. if (!m_ready)
  413. {
  414. #if !defined _XBOX && !defined USE_D3D9
  415. /* Create a texture of half the width and twice the height
  416. * so that we can upload four different subimages each frame. */
  417. glGenTextures(1, &m_texid);
  418. glBindTexture(GL_TEXTURE_2D, m_texid);
  419. glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT,
  420. m_size.x / 2, m_size.y * 2, 0,
  421. TEXTURE_FORMAT, TEXTURE_TYPE, &m_pixels[0]);
  422. # if defined __CELLOS_LV2__
  423. /* We need this hint because by default the storage type is
  424. * GL_TEXTURE_SWIZZLED_GPU_SCE. */
  425. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_ALLOCATION_HINT_SCE,
  426. GL_TEXTURE_TILED_GPU_SCE);
  427. # endif
  428. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  429. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  430. #elif defined _XBOX
  431. /* By default the X360 will swizzle the texture. Ask for linear. */
  432. g_d3ddevice->CreateTexture(m_size.x / 2, m_size.y * 2, 1,
  433. D3DUSAGE_WRITEONLY, D3DFMT_LIN_A8R8G8B8,
  434. D3DPOOL_DEFAULT, &m_tex, NULL);
  435. #else
  436. g_d3ddevice->CreateTexture(m_size.x / 2, m_size.y * 2, 1,
  437. D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8,
  438. D3DPOOL_SYSTEMMEM, &m_tex, NULL);
  439. #endif
  440. m_shader = Shader::Create(lolfx_11_fractal);
  441. m_vertexattrib = m_shader->GetAttribLocation("a_Vertex", VertexUsage::Position, 0);
  442. m_texattrib = m_shader->GetAttribLocation("a_TexCoord", VertexUsage::TexCoord, 0);
  443. m_texeluni = m_shader->GetUniformLocation("u_TexelSize");
  444. m_screenuni = m_shader->GetUniformLocation("u_ScreenSize");
  445. m_zoomuni = m_shader->GetUniformLocation("u_ZoomSettings");
  446. m_vdecl =
  447. new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position),
  448. VertexStream<vec2>(VertexUsage::TexCoord));
  449. m_vbo = new VertexBuffer(sizeof(vertices));
  450. m_tbo = new VertexBuffer(sizeof(texcoords));
  451. void *tmp = m_vbo->Lock(0, 0);
  452. memcpy(tmp, vertices, sizeof(vertices));
  453. m_vbo->Unlock();
  454. tmp = m_tbo->Lock(0, 0);
  455. memcpy(tmp, texcoords, sizeof(texcoords));
  456. m_tbo->Unlock();
  457. /* FIXME: this object never cleans up */
  458. m_ready = true;
  459. }
  460. #if defined _XBOX || defined USE_D3D9
  461. #else
  462. # if !defined HAVE_GLES_2X
  463. glEnable(GL_TEXTURE_2D);
  464. # endif
  465. glBindTexture(GL_TEXTURE_2D, m_texid);
  466. #endif
  467. if (m_dirty[m_frame])
  468. {
  469. for (int i = 0; i < m_size.y; i += MAX_LINES * 2)
  470. m_donequeue.Pop();
  471. m_dirty[m_frame]--;
  472. #if defined _XBOX || defined USE_D3D9
  473. D3DLOCKED_RECT rect;
  474. # if defined _XBOX
  475. m_tex->LockRect(0, &rect, NULL, D3DLOCK_NOOVERWRITE);
  476. # else
  477. m_tex->LockRect(0, &rect, NULL,
  478. D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE);
  479. # endif
  480. for (int j = 0; j < m_size.y * 2; j++)
  481. {
  482. u8vec4 *line = (u8vec4 *)rect.pBits + j * rect.Pitch / 4;
  483. for (int i = 0; i < m_size.x / 2; i++)
  484. line[i] = m_pixels[m_size.x / 2 * j + i];
  485. }
  486. m_tex->UnlockRect(0);
  487. #elif defined __CELLOS_LV2__
  488. /* glTexSubImage2D is extremely slow on the PS3, to the point
  489. * that uploading the whole texture is 40 times faster. */
  490. glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT,
  491. m_size.x / 2, m_size.y * 2, 0,
  492. TEXTURE_FORMAT, TEXTURE_TYPE, &m_pixels[0]);
  493. #else
  494. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, m_frame * m_size.y / 2,
  495. m_size.x / 2, m_size.y / 2,
  496. TEXTURE_FORMAT, TEXTURE_TYPE,
  497. &m_pixels[m_size.x * m_size.y / 4 * m_frame]);
  498. #endif
  499. }
  500. m_shader->Bind();
  501. m_shader->SetUniform(m_texeluni, m_texel_settings);
  502. m_shader->SetUniform(m_screenuni, m_screen_settings);
  503. m_shader->SetUniform(m_zoomuni, m_zoom_settings);
  504. m_vdecl->Bind();
  505. m_vdecl->SetStream(m_vbo, m_vertexattrib);
  506. m_vdecl->SetStream(m_tbo, m_texattrib);
  507. #if defined _XBOX || defined USE_D3D9
  508. g_d3ddevice->SetTexture(0, m_tex);
  509. g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
  510. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  511. #else
  512. #endif
  513. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  514. m_vdecl->Unbind();
  515. }
  516. private:
  517. static int const MAX_ITERATIONS = 340;
  518. static int const PALETTE_STEP = 32;
  519. static int const MAX_THREADS = 8;
  520. static int const MAX_LINES = 8;
  521. ivec2 m_size, m_window_size, m_oldmouse;
  522. double m_window2world;
  523. dvec2 m_texel2world;
  524. Array<u8vec4> m_pixels, m_palette;
  525. Shader *m_shader;
  526. ShaderAttrib m_vertexattrib, m_texattrib;
  527. ShaderUniform m_texeluni, m_screenuni, m_zoomuni;
  528. VertexDeclaration *m_vdecl;
  529. VertexBuffer *m_vbo, *m_tbo;
  530. #if defined USE_D3D9
  531. IDirect3DTexture9 *m_tex;
  532. #elif defined _XBOX
  533. D3DTexture *m_tex;
  534. #else
  535. GLuint m_texid;
  536. #endif
  537. int m_frame, m_slices, m_dirty[4];
  538. bool m_ready, m_drag;
  539. rcmplx m_deltashift[4], m_center, m_translate;
  540. real m_deltascale[4];
  541. double m_zoom_speed, m_radius;
  542. vec4 m_texel_settings, m_screen_settings;
  543. mat4 m_zoom_settings;
  544. /* Worker threads */
  545. Thread *m_threads[MAX_THREADS];
  546. Queue<int> m_spawnqueue, m_jobqueue, m_donequeue;
  547. /* Debug information */
  548. #if !defined __native_client__
  549. Text *m_centertext, *m_mousetext, *m_zoomtext;
  550. #endif
  551. };
  552. int main(int argc, char **argv)
  553. {
  554. ivec2 window_size(640, 480);
  555. Application app("Tutorial 3: Fractal", window_size, 60.0f);
  556. #if defined _MSC_VER && !defined _XBOX
  557. _chdir("..");
  558. #elif defined _WIN32 && !defined _XBOX
  559. _chdir("../..");
  560. #endif
  561. new DebugFps(5, 5);
  562. new Fractal(window_size);
  563. //new DebugRecord("fractalol.ogm", 60.0f);
  564. app.Run();
  565. return EXIT_SUCCESS;
  566. }