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