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.
 
 
 

771 lines
29 KiB

  1. //
  2. // Lol Engine - Fractal tutorial
  3. //
  4. // Copyright: (c) 2011 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 std;
  18. using namespace lol;
  19. #if USE_SDL && defined __APPLE__
  20. # include <SDL_main.h>
  21. #endif
  22. #if defined _WIN32
  23. # undef main /* FIXME: still needed? */
  24. # include <direct.h>
  25. #endif
  26. #ifdef __CELLOS_LV2__
  27. static GLint const INTERNAL_FORMAT = GL_ARGB_SCE;
  28. static GLenum const TEXTURE_FORMAT = GL_BGRA;
  29. static GLenum const TEXTURE_TYPE = GL_UNSIGNED_INT_8_8_8_8_REV;
  30. #elif defined __native_client__
  31. static GLint const INTERNAL_FORMAT = GL_RGBA;
  32. static GLenum const TEXTURE_FORMAT = GL_RGBA;
  33. static GLenum const TEXTURE_TYPE = GL_UNSIGNED_BYTE;
  34. #else
  35. /* Seems efficient for little endian textures */
  36. static GLint const INTERNAL_FORMAT = GL_RGBA;
  37. static GLenum const TEXTURE_FORMAT = GL_BGRA;
  38. static GLenum const TEXTURE_TYPE = GL_UNSIGNED_INT_8_8_8_8_REV;
  39. #endif
  40. class Fractal : public WorldEntity
  41. {
  42. public:
  43. Fractal(ivec2 const &size)
  44. {
  45. /* Ensure texture size is a multiple of 16 for better aligned
  46. * data access. Store the dimensions of a texel for our shader,
  47. * as well as the half-size of the screen. */
  48. m_size = size;
  49. m_size.x = (m_size.x + 15) & ~15;
  50. m_size.y = (m_size.y + 15) & ~15;
  51. m_texel_settings = vec4(1.0, 1.0, 2.0, 2.0) / (vec4)m_size.xyxy();
  52. m_screen_settings = vec4(1.0, 1.0, 0.5, 0.5) * (vec4)m_size.xyxy();
  53. /* Window size decides the world aspect ratio. For instance, 640×480
  54. * will be mapped to (-0.66,-0.5) - (0.66,0.5). */
  55. #if !defined __native_client__
  56. m_window_size = Video::GetSize();
  57. #else
  58. /* FIXME: it's illegal to call this on the game thread! */
  59. m_window_size = ivec2(640, 480);
  60. #endif
  61. if (m_window_size.y < m_window_size.x)
  62. m_window2world = 0.5 / m_window_size.y;
  63. else
  64. m_window2world = 0.5 / m_window_size.x;
  65. m_texel2world = (vec2)m_window_size / (vec2)m_size * m_window2world;
  66. m_oldmouse = ivec2(0, 0);
  67. m_pixels = new u8vec4[m_size.x * m_size.y];
  68. m_tmppixels = new u8vec4[m_size.x / 2 * m_size.y / 2];
  69. m_frame = -1;
  70. m_slices = 4;
  71. for (int i = 0; i < 4; i++)
  72. {
  73. m_deltashift[i] = 0.0;
  74. m_deltascale[i] = 1.0;
  75. m_dirty[i] = 2;
  76. }
  77. #if defined __CELLOS_LV2__
  78. //m_center = f64cmplx(-.22815528839841, -1.11514249704382);
  79. //m_center = f64cmplx(0.001643721971153, 0.822467633298876);
  80. m_center = f64cmplx(-0.65823419062254, 0.50221777363480);
  81. m_zoom_speed = -0.000025;
  82. #else
  83. m_center = -0.75;
  84. m_zoom_speed = 0.0;
  85. #endif
  86. m_translate = 0;
  87. m_radius = 5.0;
  88. m_ready = false;
  89. m_drag = false;
  90. m_palette = new u8vec4[(MAX_ITERATIONS + 1) * PALETTE_STEP];
  91. for (int i = 0; i < (MAX_ITERATIONS + 1) * PALETTE_STEP; i++)
  92. {
  93. double f = (double)i / PALETTE_STEP;
  94. double r = 0.5 * sin(f * 0.27 - 2.5) + 0.5;
  95. double g = 0.5 * sin(f * 0.13 + 1.1) + 0.5;
  96. double b = 0.5 * sin(f * 0.21 + 0.4) + 0.5;
  97. if (f < 7.0)
  98. {
  99. f = f < 1.0 ? 0.0 : (f - 1.0) / 6.0;
  100. r *= f;
  101. g *= f;
  102. b *= f;
  103. }
  104. uint8_t red = r * 255.99f;
  105. uint8_t green = g * 255.99f;
  106. uint8_t blue = b * 255.99f;
  107. #if defined __CELLOS_LV2__
  108. m_palette[i] = u8vec4(255, red, green, blue);
  109. #elif defined __native_client__
  110. m_palette[i] = u8vec4(red, green, blue, 255);
  111. #else
  112. m_palette[i] = u8vec4(blue, green, red, 255);
  113. #endif
  114. }
  115. #if !defined __native_client__
  116. m_centertext = new Text(NULL, "gfx/font/ascii.png");
  117. m_centertext->SetPos(ivec3(5, m_window_size.y - 15, 1));
  118. Ticker::Ref(m_centertext);
  119. m_mousetext = new Text(NULL, "gfx/font/ascii.png");
  120. m_mousetext->SetPos(ivec3(5, m_window_size.y - 29, 1));
  121. Ticker::Ref(m_mousetext);
  122. m_zoomtext = new Text(NULL, "gfx/font/ascii.png");
  123. m_zoomtext->SetPos(ivec3(5, m_window_size.y - 43, 1));
  124. Ticker::Ref(m_zoomtext);
  125. #endif
  126. position = ivec3(0, 0, 0);
  127. bbox[0] = position;
  128. bbox[1] = ivec3(m_window_size, 0);
  129. Input::TrackMouse(this);
  130. }
  131. ~Fractal()
  132. {
  133. Input::UntrackMouse(this);
  134. #if !defined __native_client__
  135. Ticker::Unref(m_centertext);
  136. Ticker::Unref(m_mousetext);
  137. Ticker::Unref(m_zoomtext);
  138. #endif
  139. delete m_pixels;
  140. delete m_tmppixels;
  141. delete m_palette;
  142. }
  143. inline f64cmplx TexelToWorldOffset(vec2 texel)
  144. {
  145. double dx = (0.5 + texel.x - m_size.x / 2) * m_texel2world.x;
  146. double dy = (0.5 + m_size.y / 2 - texel.y) * m_texel2world.y;
  147. return m_radius * f64cmplx(dx, dy);
  148. }
  149. inline f64cmplx ScreenToWorldOffset(vec2 pixel)
  150. {
  151. /* No 0.5 offset here, because we want to be able to position the
  152. * mouse at (0,0) exactly. */
  153. double dx = pixel.x - m_window_size.x / 2;
  154. double dy = m_window_size.y / 2 - pixel.y;
  155. return m_radius * m_window2world * f64cmplx(dx, dy);
  156. }
  157. virtual void TickGame(float deltams)
  158. {
  159. WorldEntity::TickGame(deltams);
  160. int prev_frame = m_frame;
  161. m_frame = (m_frame + 1) % 4;
  162. f64cmplx worldmouse = m_center + ScreenToWorldOffset(mousepos);
  163. ivec3 buttons = Input::GetMouseButtons();
  164. #if !defined __CELLOS_LV2__
  165. if (buttons[1])
  166. {
  167. if (!m_drag)
  168. {
  169. m_oldmouse = mousepos;
  170. m_drag = true;
  171. }
  172. m_translate = ScreenToWorldOffset(m_oldmouse)
  173. - ScreenToWorldOffset(mousepos);
  174. /* XXX: the purpose of this hack is to avoid translating by
  175. * an exact number of pixels. If this were to happen, the step()
  176. * optimisation for i915 cards in our shader would behave
  177. * incorrectly because a quarter of the pixels in the image
  178. * would have tie rankings in the distance calculation. */
  179. m_translate *= 1023.0 / 1024.0;
  180. m_oldmouse = mousepos;
  181. }
  182. else
  183. {
  184. m_drag = false;
  185. if (m_translate != 0.0)
  186. {
  187. m_translate *= pow(2.0, -deltams * 0.005);
  188. if (m_translate.norm() / m_radius < 1e-4)
  189. m_translate = 0.0;
  190. }
  191. }
  192. if ((buttons[0] || buttons[2]) && mousepos.x != -1)
  193. {
  194. double zoom = buttons[0] ? -0.0005 : 0.0005;
  195. m_zoom_speed += deltams * zoom;
  196. if (m_zoom_speed / zoom > 5)
  197. m_zoom_speed = 5 * zoom;
  198. }
  199. else if (m_zoom_speed)
  200. {
  201. m_zoom_speed *= pow(2.0, -deltams * 0.005);
  202. if (abs(m_zoom_speed) < 1e-5 || m_drag)
  203. m_zoom_speed = 0.0;
  204. }
  205. #endif
  206. if (m_zoom_speed || m_translate != 0.0)
  207. {
  208. f64cmplx oldcenter = m_center;
  209. double oldradius = m_radius;
  210. double zoom = pow(2.0, deltams * m_zoom_speed);
  211. if (m_radius * zoom > 8.0)
  212. {
  213. m_zoom_speed *= -1.0;
  214. zoom = 8.0 / m_radius;
  215. }
  216. else if (m_radius * zoom < 1e-14)
  217. {
  218. m_zoom_speed *= -1.0;
  219. zoom = 1e-14 / m_radius;
  220. }
  221. m_radius *= zoom;
  222. #if !defined __CELLOS_LV2__
  223. m_center += m_translate;
  224. m_center = (m_center - worldmouse) * zoom + worldmouse;
  225. worldmouse = m_center + ScreenToWorldOffset(mousepos);
  226. #endif
  227. /* Store the transformation properties to go from m_frame - 1
  228. * to m_frame. */
  229. m_deltashift[prev_frame] = (m_center - oldcenter) / oldradius;
  230. m_deltashift[prev_frame].x /= m_size.x * m_texel2world.x;
  231. m_deltashift[prev_frame].y /= m_size.y * m_texel2world.y;
  232. m_deltascale[prev_frame] = m_radius / oldradius;
  233. m_dirty[0] = m_dirty[1] = m_dirty[2] = m_dirty[3] = 2;
  234. }
  235. else
  236. {
  237. /* If settings didn't change, set transformation from previous
  238. * frame to identity. */
  239. m_deltashift[prev_frame] = 0.0;
  240. m_deltascale[prev_frame] = 1.0;
  241. }
  242. /* Transformation from current frame to current frame is always
  243. * identity. */
  244. m_zoom_settings[m_frame][0] = 0.0f;
  245. m_zoom_settings[m_frame][1] = 0.0f;
  246. m_zoom_settings[m_frame][2] = 1.0f;
  247. /* Compute transformation from other frames to current frame */
  248. for (int i = 0; i < 3; i++)
  249. {
  250. int prev_index = (m_frame + 4 - i) % 4;
  251. int cur_index = (m_frame + 3 - i) % 4;
  252. m_zoom_settings[cur_index][0] = m_zoom_settings[prev_index][0] * m_deltascale[cur_index] + m_deltashift[cur_index].x;
  253. m_zoom_settings[cur_index][1] = m_zoom_settings[prev_index][1] * m_deltascale[cur_index] + m_deltashift[cur_index].y;
  254. m_zoom_settings[cur_index][2] = m_zoom_settings[prev_index][2] * m_deltascale[cur_index];
  255. }
  256. /* Precompute texture offset change instead of doing it in GLSL */
  257. for (int i = 0; i < 4; i++)
  258. {
  259. m_zoom_settings[i][0] += 0.5 * (1.0 - m_zoom_settings[i][2]);
  260. m_zoom_settings[i][1] -= 0.5 * (1.0 - m_zoom_settings[i][2]);
  261. }
  262. #if !defined __native_client__
  263. char buf[128];
  264. sprintf(buf, "center: %+16.14f%+16.14fi", m_center.x, m_center.y);
  265. m_centertext->SetText(buf);
  266. sprintf(buf, " mouse: %+16.14f%+16.14fi", worldmouse.x, worldmouse.y);
  267. m_mousetext->SetText(buf);
  268. sprintf(buf, " zoom: %g", 1.0 / m_radius);
  269. m_zoomtext->SetText(buf);
  270. #endif
  271. if (m_dirty[m_frame])
  272. {
  273. m_dirty[m_frame]--;
  274. /* FIXME: this is the ugliest, most pathetic excuse for a
  275. * threading system that I have seen in a while. */
  276. DoWorkHelper helpers[m_slices];
  277. for (int slice = 0; slice < m_slices; slice++)
  278. {
  279. helpers[slice].fractal = this;
  280. helpers[slice].slice = slice;
  281. // helpers[slice].thread = new Thread(DoWorkHelper::Help,
  282. // &helpers[slice]);
  283. DoWork(slice);
  284. }
  285. for (int slice = 0; slice < m_slices; slice++)
  286. {
  287. // delete helpers[slice].thread;
  288. }
  289. }
  290. }
  291. struct DoWorkHelper
  292. {
  293. Fractal *fractal;
  294. Thread *thread;
  295. int slice;
  296. static void *Help(void *data)
  297. {
  298. DoWorkHelper *helper = (DoWorkHelper *)data;
  299. helper->fractal->DoWork(helper->slice);
  300. return NULL;
  301. }
  302. };
  303. void DoWork(int slice)
  304. {
  305. double const maxsqlen = 1024;
  306. double const k1 = 1.0 / (1 << 10) / log2(maxsqlen);
  307. int jmin = m_size.y * slice / m_slices;
  308. int jmax = m_size.y * (slice + 1) / m_slices;
  309. u8vec4 *m_pixelstart = m_pixels
  310. + m_size.x * (m_size.y / 4 * m_frame + jmin / 4);
  311. for (int j = ((m_frame + 1) % 4) / 2 + jmin; j < jmax; j += 2)
  312. for (int i = m_frame % 2; i < m_size.x; i += 2)
  313. {
  314. f64cmplx z0 = m_center + TexelToWorldOffset(ivec2(i, j));
  315. f64cmplx r0 = z0;
  316. //f64cmplx r0(0.28693186889504513, 0.014286693904085048);
  317. //f64cmplx r0(0.001643721971153, 0.822467633298876);
  318. //f64cmplx r0(-1.207205434596, 0.315432814901);
  319. //f64cmplx r0(-0.79192956889854, -0.14632423080102);
  320. //f64cmplx r0(0.3245046418497685, 0.04855101129280834);
  321. f64cmplx z;
  322. int iter = MAX_ITERATIONS;
  323. for (z = z0; iter && z.sqlen() < maxsqlen; z = z * z + r0)
  324. --iter;
  325. if (iter)
  326. {
  327. double f = iter;
  328. double n = z.sqlen();
  329. if (n > maxsqlen * maxsqlen)
  330. n = maxsqlen * maxsqlen;
  331. /* Approximate log(sqrt(n))/log(sqrt(maxsqlen)) */
  332. union { double n; uint64_t x; } u = { n };
  333. double k = (u.x >> 42) - (((1 << 10) - 1) << 10);
  334. k *= k1;
  335. /* Approximate log2(k) in [1,2]. */
  336. f += (- 0.344847817623168308695977510213252644185 * k
  337. + 2.024664188044341212602376988171727038739) * k
  338. - 1.674876738008591047163498125918330313237;
  339. *m_pixelstart++ = m_palette[(int)(f * PALETTE_STEP)];
  340. }
  341. else
  342. {
  343. #if defined __CELLOS_LV2__
  344. *m_pixelstart++ = u8vec4(255, 0, 0, 0);
  345. #else
  346. *m_pixelstart++ = u8vec4(0, 0, 0, 255);
  347. #endif
  348. }
  349. }
  350. }
  351. virtual void TickDraw(float deltams)
  352. {
  353. WorldEntity::TickDraw(deltams);
  354. static float const vertices[] =
  355. {
  356. 1.0f, 1.0f,
  357. -1.0f, 1.0f,
  358. -1.0f, -1.0f,
  359. -1.0f, -1.0f,
  360. 1.0f, -1.0f,
  361. 1.0f, 1.0f,
  362. };
  363. static float const texcoords[] =
  364. {
  365. 1.0f, 1.0f,
  366. 0.0f, 1.0f,
  367. 0.0f, 0.0f,
  368. 0.0f, 0.0f,
  369. 1.0f, 0.0f,
  370. 1.0f, 1.0f,
  371. };
  372. if (!m_ready)
  373. {
  374. /* Create a texture of half the width and twice the height
  375. * so that we can upload four different subimages each frame. */
  376. glGenTextures(1, &m_texid);
  377. glBindTexture(GL_TEXTURE_2D, m_texid);
  378. glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT,
  379. m_size.x / 2, m_size.y * 2, 0,
  380. TEXTURE_FORMAT, TEXTURE_TYPE, m_pixels);
  381. #if defined __CELLOS_LV2__
  382. /* We need this hint because by default the storage type is
  383. * GL_TEXTURE_SWIZZLED_GPU_SCE. */
  384. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_ALLOCATION_HINT_SCE,
  385. GL_TEXTURE_TILED_GPU_SCE);
  386. #endif
  387. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  388. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  389. m_shader = Shader::Create(
  390. #if !defined __CELLOS_LV2__
  391. #if !defined HAVE_GLES_2X
  392. "#version 120\n"
  393. #else
  394. "precision highp float;"
  395. #endif
  396. ""
  397. "uniform mat4 u_ZoomSettings;"
  398. "uniform vec4 u_TexelSize;"
  399. "uniform vec4 u_ScreenSize;"
  400. ""
  401. "attribute vec2 a_TexCoord;"
  402. "attribute vec2 a_Vertex;"
  403. ""
  404. "varying vec4 v_CenterX, v_CenterY, v_IndexX, v_IndexY;"
  405. ""
  406. "void main(void)"
  407. "{"
  408. " gl_Position = vec4(a_Vertex, 0.0, 1.0);"
  409. /* Center point in [-.5,.5], apply zoom and translation
  410. * transformation, and go back to texture coordinates
  411. * in [0,1]. That's the ideal point we would like to
  412. * compute the value for. Then add or remove half the
  413. * size of a texel: the distance from this new point to
  414. * the final point will be our error. */
  415. " vec4 offsets = vec4(0.5, -0.5, 0.015625, -0.015625);"
  416. " vec4 zoomscale = vec4(u_ZoomSettings[0][2],"
  417. " u_ZoomSettings[1][2],"
  418. " u_ZoomSettings[2][2],"
  419. " u_ZoomSettings[3][2]);"
  420. " vec4 zoomtx = vec4(u_ZoomSettings[0][0],"
  421. " u_ZoomSettings[1][0],"
  422. " u_ZoomSettings[2][0],"
  423. " u_ZoomSettings[3][0]);"
  424. " vec4 zoomty = vec4(u_ZoomSettings[0][1],"
  425. " u_ZoomSettings[1][1],"
  426. " u_ZoomSettings[2][1],"
  427. " u_ZoomSettings[3][1]);"
  428. " v_CenterX = zoomscale * a_TexCoord.x + zoomtx"
  429. " + offsets.xyxy * u_TexelSize.x;"
  430. " v_CenterY = zoomscale * a_TexCoord.y - zoomty"
  431. " + offsets.xyyx * u_TexelSize.y;"
  432. /* Precompute the multiple of one texel where our ideal
  433. * point lies. The fragment shader will call floor() on
  434. * this value. We add or remove a slight offset to avoid
  435. * rounding issues at the image's edges. */
  436. " v_IndexX = v_CenterX * u_ScreenSize.z - offsets.zwzw;"
  437. " v_IndexY = v_CenterY * u_ScreenSize.w - offsets.zwwz;"
  438. "}",
  439. #if !defined HAVE_GLES_2X
  440. "#version 120\n"
  441. #else
  442. "precision highp float;"
  443. #endif
  444. ""
  445. "uniform vec4 u_TexelSize;"
  446. "uniform sampler2D u_Texture;"
  447. ""
  448. "varying vec4 v_CenterX, v_CenterY, v_IndexX, v_IndexY;"
  449. ""
  450. "void main(void)"
  451. "{"
  452. " vec4 v05 = vec4(0.5, 0.5, 0.5, 0.5);"
  453. " vec4 rx, ry, t0, dx, dy, dd;"
  454. /* Get a pixel coordinate from each slice into rx & ry */
  455. " rx = u_TexelSize.x + u_TexelSize.z * floor(v_IndexX);"
  456. " ry = u_TexelSize.y + u_TexelSize.w * floor(v_IndexY);"
  457. /* Compute inverse distance to expected pixel in dd,
  458. * and put zero if we fall outside the texture. */
  459. " t0 = step(abs(rx - v05), v05) * step(abs(ry - v05), v05);"
  460. " dx = rx - v_CenterX;"
  461. " dy = ry - v_CenterY;"
  462. //" vec4 dd = t0 * (abs(dx) + abs(dy));"
  463. //" vec4 dd = t0 / (0.001 + sqrt((dx * dx) + (dy * dy)));"
  464. " dd = t0 / (0.000001 + (dx * dx) + (dy * dy));"
  465. /* Modify Y coordinate to select proper quarter. */
  466. " ry = ry * 0.25 + vec4(0.0, 0.25, 0.5, 0.75);"
  467. ""
  468. #if 1
  469. "\n#if 0\n" /* XXX: disabled until we can autodetect i915 */
  470. /* t1.x <-- dd.x > dd.y */
  471. /* t1.y <-- dd.z > dd.w */
  472. " vec2 t1 = step(dd.xz, dd.yw);"
  473. /* ret.x <-- max(rx.x, rx.y) wrt. t1.x */
  474. /* ret.y <-- max(rx.z, rx.w) wrt. t1.y */
  475. /* ret.z <-- max(ry.x, ry.y) wrt. t1.x */
  476. /* ret.w <-- max(ry.z, ry.w) wrt. t1.y */
  477. " vec4 ret = mix(vec4(rx.xz, ry.xz),"
  478. " vec4(rx.yw, ry.yw), t1.xyxy);"
  479. /* dd.x <-- max(dd.x, dd.y) */
  480. /* dd.z <-- max(dd.z, dd.w) */
  481. " dd.xy = mix(dd.xz, dd.yw, t1);"
  482. /* t2 <-- dd.x > dd.z */
  483. " float t2 = step(dd.x, dd.y);"
  484. /* ret.x <-- max(ret.x, ret.y); */
  485. /* ret.y <-- max(ret.z, ret.yw; */
  486. " ret.xy = mix(ret.xz, ret.yw, t2);"
  487. "\n#else\n"
  488. /* Fallback for i915 cards -- the trick to reduce the
  489. * number of operations is to compute both step(a,b)
  490. * and step(b,a) and hope that their sum is 1. This is
  491. * almost always the case, and when it isn't we can
  492. * afford to have a few wrong pixels. However, a real
  493. * problem is when panning the image, because half the
  494. * screen is likely to flicker. To avoid this problem,
  495. * we cheat a little (see m_translate comment above). */
  496. " vec4 t1 = step(dd.xzyw, dd.ywxz);"
  497. " vec4 ret = vec4(rx.xz, ry.xz) * t1.zwzw"
  498. " + vec4(rx.yw, ry.yw) * t1.xyxy;"
  499. " dd.xy = dd.xz * t1.zw + dd.yw * t1.xy;"
  500. " vec2 t2 = step(dd.xy, dd.yx);"
  501. " ret.xy = ret.xz * t2.yy + ret.yw * t2.xx;"
  502. "\n#endif\n"
  503. /* Nearest neighbour */
  504. " gl_FragColor = texture2D(u_Texture, ret.xy);"
  505. #else
  506. /* Alternate version: some kind of linear interpolation */
  507. " vec4 p0 = texture2D(u_Texture, vec2(rx.x, ry.x));"
  508. " vec4 p1 = texture2D(u_Texture, vec2(rx.y, ry.y));"
  509. " vec4 p2 = texture2D(u_Texture, vec2(rx.z, ry.z));"
  510. " vec4 p3 = texture2D(u_Texture, vec2(rx.w, ry.w));"
  511. " gl_FragColor = 1.0 / (dd.x + dd.y + dd.z + dd.w)"
  512. " * (dd.x * p0 + dd.y * p1 + dd.z * p2 + dd.w * p3);"
  513. #endif
  514. "}"
  515. #else
  516. "void main(float4 a_Vertex : POSITION,"
  517. " float2 a_TexCoord : TEXCOORD0,"
  518. " uniform float4x4 u_ZoomSettings,"
  519. " uniform float4 u_TexelSize,"
  520. " uniform float4 u_ScreenSize,"
  521. " out float4 out_Position : POSITION,"
  522. " out float4 v_CenterX,"
  523. " out float4 v_CenterY,"
  524. " out float4 v_IndexX,"
  525. " out float4 v_IndexY)"
  526. "{"
  527. " out_Position = a_Vertex;"
  528. " float4 offsets = float4(0.5, -0.5, 0.015625, -0.015625);"
  529. " float4 zoomscale = float4(u_ZoomSettings[2][0],"
  530. " u_ZoomSettings[2][1],"
  531. " u_ZoomSettings[2][2],"
  532. " u_ZoomSettings[2][3]);"
  533. " float4 zoomtx = float4(u_ZoomSettings[0][0],"
  534. " u_ZoomSettings[0][1],"
  535. " u_ZoomSettings[0][2],"
  536. " u_ZoomSettings[0][3]);"
  537. " float4 zoomty = float4(u_ZoomSettings[1][0],"
  538. " u_ZoomSettings[1][1],"
  539. " u_ZoomSettings[1][2],"
  540. " u_ZoomSettings[1][3]);"
  541. " v_CenterX = zoomscale * a_TexCoord.x + zoomtx"
  542. " + offsets.xyxy * u_TexelSize.x;"
  543. " v_CenterY = zoomscale * a_TexCoord.y - zoomty"
  544. " + offsets.xyyx * u_TexelSize.y;"
  545. " v_IndexX = v_CenterX * u_ScreenSize.z - offsets.zwzw;"
  546. " v_IndexY = v_CenterY * u_ScreenSize.w - offsets.zwwz;"
  547. "}",
  548. "void main(in float4 v_CenterX,"
  549. " in float4 v_CenterY,"
  550. " in float4 v_IndexX,"
  551. " in float4 v_IndexY,"
  552. " uniform float4 u_TexelSize2,"
  553. " uniform sampler2D u_Texture,"
  554. " out float4 out_FragColor : COLOR)"
  555. "{"
  556. " float4 v05 = float4(0.5, 0.5, 0.5, 0.5);"
  557. " float4 rx, ry, t0, dx, dy, dd;"
  558. " rx = u_TexelSize2.x + u_TexelSize2.z * floor(v_IndexX);"
  559. " ry = u_TexelSize2.y + u_TexelSize2.w * floor(v_IndexY);"
  560. " t0 = step(abs(rx - v05), v05) * step(abs(ry - v05), v05);"
  561. " dx = rx - v_CenterX;"
  562. " dy = ry - v_CenterY;"
  563. " dd = t0 / (0.000001 + (dx * dx) + (dy * dy));"
  564. " ry = ry * 0.25 + float4(0.0, 0.25, 0.5, 0.75);"
  565. " float2 t1 = step(dd.xz, dd.yw);"
  566. " float4 ret = lerp(float4(rx.xz, ry.xz),"
  567. " float4(rx.yw, ry.yw), t1.xyxy);"
  568. " dd.xy = lerp(dd.xz, dd.yw, t1);"
  569. " float t2 = step(dd.x, dd.y);"
  570. " ret.xy = lerp(ret.xz, ret.yw, t2);"
  571. " out_FragColor = tex2D(u_Texture, ret.xy);"
  572. "}"
  573. #endif
  574. );
  575. m_vertexattrib = m_shader->GetAttribLocation("a_Vertex");
  576. m_texattrib = m_shader->GetAttribLocation("a_TexCoord");
  577. m_texeluni = m_shader->GetUniformLocation("u_TexelSize");
  578. #if defined __CELLOS_LV2__
  579. m_texeluni2 = m_shader->GetUniformLocation("u_TexelSize2");
  580. #endif
  581. m_screenuni = m_shader->GetUniformLocation("u_ScreenSize");
  582. m_zoomuni = m_shader->GetUniformLocation("u_ZoomSettings");
  583. m_ready = true;
  584. #if !defined __CELLOS_LV2__ && !defined __ANDROID__
  585. /* Method 1: store vertex buffer on the GPU memory */
  586. glGenBuffers(1, &m_vbo);
  587. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  588. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
  589. GL_STATIC_DRAW);
  590. glGenBuffers(1, &m_tbo);
  591. glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
  592. glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords,
  593. GL_STATIC_DRAW);
  594. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  595. /* Method 2: upload vertex information at each frame */
  596. #else
  597. #endif
  598. /* FIXME: this object never cleans up */
  599. }
  600. #if !defined HAVE_GLES_2X
  601. glEnable(GL_TEXTURE_2D);
  602. #endif
  603. glBindTexture(GL_TEXTURE_2D, m_texid);
  604. if (m_dirty[m_frame])
  605. {
  606. m_dirty[m_frame]--;
  607. #ifdef __CELLOS_LV2__
  608. /* glTexSubImage2D is extremely slow on the PS3, to the point
  609. * that uploading the whole texture is 40 times faster. */
  610. glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT,
  611. m_size.x / 2, m_size.y * 2, 0,
  612. TEXTURE_FORMAT, TEXTURE_TYPE, m_pixels);
  613. #else
  614. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, m_frame * m_size.y / 2,
  615. m_size.x / 2, m_size.y / 2,
  616. TEXTURE_FORMAT, TEXTURE_TYPE,
  617. m_pixels + m_size.x * m_size.y / 4 * m_frame);
  618. #endif
  619. }
  620. m_shader->Bind();
  621. m_shader->SetUniform(m_texeluni, m_texel_settings);
  622. #if defined __CELLOS_LV2__
  623. m_shader->SetUniform(m_texeluni2, m_texel_settings);
  624. #endif
  625. m_shader->SetUniform(m_screenuni, m_screen_settings);
  626. m_shader->SetUniform(m_zoomuni, m_zoom_settings);
  627. #if !defined __CELLOS_LV2__ && !defined __ANDROID__
  628. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  629. glEnableVertexAttribArray(m_vertexattrib);
  630. glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  631. glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
  632. glEnableVertexAttribArray(m_texattrib);
  633. glVertexAttribPointer(m_texattrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  634. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  635. /* Never used for now */
  636. //glEnableVertexAttribArray(m_vertexattrib);
  637. //glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  638. #else
  639. glEnableClientState(GL_VERTEX_ARRAY);
  640. glVertexPointer(2, GL_FLOAT, 0, vertices);
  641. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  642. glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
  643. #endif
  644. glDrawArrays(GL_TRIANGLES, 0, 6);
  645. #if !defined __CELLOS_LV2__ && !defined __ANDROID__
  646. glDisableVertexAttribArray(m_vertexattrib);
  647. glDisableVertexAttribArray(m_texattrib);
  648. glBindBuffer(GL_ARRAY_BUFFER, 0);
  649. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  650. /* Never used for now */
  651. //glDisableVertexAttribArray(m_vertexattrib);
  652. //glDisableVertexAttribArray(m_texattrib);
  653. #else
  654. glDisableClientState(GL_VERTEX_ARRAY);
  655. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  656. #endif
  657. }
  658. private:
  659. static int const MAX_ITERATIONS = 170;
  660. static int const PALETTE_STEP = 32;
  661. ivec2 m_size, m_window_size, m_oldmouse;
  662. double m_window2world;
  663. f64vec2 m_texel2world;
  664. u8vec4 *m_pixels, *m_tmppixels, *m_palette;
  665. Shader *m_shader;
  666. GLuint m_texid;
  667. #if !defined __CELLOS_LV2__ && !defined __ANDROID__
  668. GLuint m_vbo, m_tbo;
  669. GLuint m_tco;
  670. #endif
  671. int m_vertexattrib, m_texattrib, m_texeluni, m_screenuni, m_zoomuni;
  672. #if defined __CELLOS_LV2__
  673. int m_texeluni2;
  674. #endif
  675. int m_frame, m_slices, m_dirty[4];
  676. bool m_ready, m_drag;
  677. f64cmplx m_center, m_translate;
  678. double m_zoom_speed, m_radius;
  679. vec4 m_texel_settings, m_screen_settings;
  680. mat4 m_zoom_settings;
  681. f64cmplx m_deltashift[4];
  682. double m_deltascale[4];
  683. /* Debug information */
  684. #if !defined __native_client__
  685. Text *m_centertext, *m_mousetext, *m_zoomtext;
  686. #endif
  687. };
  688. int main(int argc, char **argv)
  689. {
  690. #if defined _WIN32
  691. _chdir("../..");
  692. #endif
  693. Application app("Tutorial 3: Fractal", ivec2(640, 480), 60.0f);
  694. new DebugFps(5, 5);
  695. new Fractal(ivec2(640, 480));
  696. //new DebugRecord("fractalol.ogm", 60.0f);
  697. app.Run();
  698. return EXIT_SUCCESS;
  699. }