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