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.
 
 
 

658 regels
22 KiB

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