696 rivejä
26 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. m_size = size;
  48. m_size.x = (m_size.x + 15) & ~15;
  49. m_size.y = (m_size.y + 15) & ~15;
  50. m_texel_settings = vec4(vec2(1.0, 1.0) / (vec2)m_size, m_size);
  51. /* Window size decides the world aspect ratio. For instance, 640×480
  52. * will be mapped to (-0.66,-0.5) - (0.66,0.5). */
  53. #if !defined __native_client__
  54. m_window_size = Video::GetSize();
  55. #else
  56. /* FIXME: it's illegal to call this on the game thread! */
  57. m_window_size = ivec2(640, 480);
  58. #endif
  59. if (m_window_size.y < m_window_size.x)
  60. m_window2world = 0.5 / m_window_size.y;
  61. else
  62. m_window2world = 0.5 / m_window_size.x;
  63. m_texel2world = (vec2)m_window_size / (vec2)m_size * m_window2world;
  64. m_pixels = new u8vec4[m_size.x * m_size.y];
  65. m_tmppixels = new u8vec4[m_size.x / 2 * m_size.y / 2];
  66. m_frame = -1;
  67. for (int i = 0; i < 4; i++)
  68. {
  69. m_deltashift[i] = 0.0;
  70. m_deltascale[i] = 1.0;
  71. m_dirty[i] = 2;
  72. }
  73. #if defined __CELLOS_LV2__ || defined __native_client__
  74. //m_center = f64cmplx(-.22815528839841, -1.11514249704382);
  75. //m_center = f64cmplx(0.001643721971153, 0.822467633298876);
  76. m_center = f64cmplx(-0.65823419062254, .50221777363480);
  77. m_zoom_speed = 0;//-0.0025;
  78. #else
  79. m_center = -0.75;
  80. m_zoom_speed = 0.0;
  81. #endif
  82. m_radius = 5.0;
  83. m_ready = false;
  84. m_palette = new u8vec4[(MAX_ITERATIONS + 1) * PALETTE_STEP];
  85. for (int i = 0; i < (MAX_ITERATIONS + 1) * PALETTE_STEP; i++)
  86. {
  87. double f = (double)i / PALETTE_STEP;
  88. double r = 0.5 * sin(f * 0.27 - 2.5) + 0.5;
  89. double g = 0.5 * sin(f * 0.13 + 1.1) + 0.5;
  90. double b = 0.5 * sin(f * 0.21 + 0.4) + 0.5;
  91. if (f < 7.0)
  92. {
  93. f = f < 1.0 ? 0.0 : (f - 1.0) / 6.0;
  94. r *= f;
  95. g *= f;
  96. b *= f;
  97. }
  98. uint8_t red = r * 255.99f;
  99. uint8_t green = g * 255.99f;
  100. uint8_t blue = b * 255.99f;
  101. #if defined __native_client__
  102. m_palette[i] = u8vec4(red, green, blue, 255);
  103. #else
  104. m_palette[i] = u8vec4(blue, green, red, 255);
  105. #endif
  106. }
  107. #if !defined __native_client__
  108. m_centertext = new Text(NULL, "gfx/font/ascii.png");
  109. m_centertext->SetPos(ivec3(5, m_window_size.y - 15, 1));
  110. Ticker::Ref(m_centertext);
  111. m_mousetext = new Text(NULL, "gfx/font/ascii.png");
  112. m_mousetext->SetPos(ivec3(5, m_window_size.y - 29, 1));
  113. Ticker::Ref(m_mousetext);
  114. m_zoomtext = new Text(NULL, "gfx/font/ascii.png");
  115. m_zoomtext->SetPos(ivec3(5, m_window_size.y - 43, 1));
  116. Ticker::Ref(m_zoomtext);
  117. #endif
  118. position = ivec3(0, 0, 0);
  119. bbox[0] = position;
  120. bbox[1] = ivec3(m_window_size, 0);
  121. Input::TrackMouse(this);
  122. }
  123. ~Fractal()
  124. {
  125. Input::UntrackMouse(this);
  126. #if !defined __native_client__
  127. Ticker::Unref(m_centertext);
  128. Ticker::Unref(m_mousetext);
  129. Ticker::Unref(m_zoomtext);
  130. #endif
  131. delete m_pixels;
  132. delete m_tmppixels;
  133. delete m_palette;
  134. }
  135. inline f64cmplx TexelToWorldOffset(ivec2 texel)
  136. {
  137. double dx = (0.5 + texel.x - m_size.x / 2) * m_texel2world.x;
  138. double dy = (0.5 + m_size.y / 2 - texel.y) * m_texel2world.y;
  139. return m_radius * f64cmplx(dx, dy);
  140. }
  141. inline f64cmplx ScreenToWorldOffset(ivec2 pixel)
  142. {
  143. /* No 0.5 offset here, because we want to be able to position the
  144. * mouse at (0,0) exactly. */
  145. double dx = pixel.x - m_window_size.x / 2;
  146. double dy = m_window_size.y / 2 - pixel.y;
  147. return m_radius * m_window2world * f64cmplx(dx, dy);
  148. }
  149. virtual void TickGame(float deltams)
  150. {
  151. WorldEntity::TickGame(deltams);
  152. int prev_frame = m_frame;
  153. m_frame = (m_frame + 1) % 4;
  154. f64cmplx worldmouse = m_center + ScreenToWorldOffset(mousepos);
  155. ivec3 buttons = Input::GetMouseButtons();
  156. #if !defined __CELLOS_LV2__ && !defined __native_client__
  157. if ((buttons[0] || buttons[2]) && mousepos.x != -1)
  158. {
  159. double zoom = buttons[0] ? -0.0005 : 0.0005;
  160. m_zoom_speed += deltams * zoom;
  161. if (m_zoom_speed / zoom > 5)
  162. m_zoom_speed = 5 * zoom;
  163. }
  164. else if (m_zoom_speed)
  165. {
  166. m_zoom_speed *= pow(2.0, -deltams * 0.005);
  167. if (abs(m_zoom_speed) < 1e-5)
  168. m_zoom_speed = 0.0;
  169. }
  170. #endif
  171. if (m_zoom_speed)
  172. {
  173. f64cmplx oldcenter = m_center;
  174. double oldradius = m_radius;
  175. double zoom = pow(2.0, deltams * m_zoom_speed);
  176. if (m_radius * zoom > 8.0)
  177. {
  178. m_zoom_speed *= -1.0;
  179. zoom = 8.0 / m_radius;
  180. }
  181. else if (m_radius * zoom < 1e-14)
  182. {
  183. m_zoom_speed *= -1.0;
  184. zoom = 1e-14 / m_radius;
  185. }
  186. m_radius *= zoom;
  187. #if !defined __CELLOS_LV2__ && !defined __native_client__
  188. m_center = (m_center - worldmouse) * zoom + worldmouse;
  189. worldmouse = m_center + ScreenToWorldOffset(mousepos);
  190. #endif
  191. /* Store the transformation properties to go from m_frame - 1
  192. * to m_frame. */
  193. m_deltashift[prev_frame] = (m_center - oldcenter) / oldradius;
  194. m_deltashift[prev_frame].x /= m_size.x * m_texel2world.x;
  195. m_deltashift[prev_frame].y /= m_size.y * m_texel2world.y;
  196. m_deltascale[prev_frame] = m_radius / oldradius;
  197. m_dirty[0] = m_dirty[1] = m_dirty[2] = m_dirty[3] = 2;
  198. }
  199. else
  200. {
  201. /* If settings didn't change, set transformation from previous
  202. * frame to identity. */
  203. m_deltashift[prev_frame] = 0.0;
  204. m_deltascale[prev_frame] = 1.0;
  205. }
  206. if (buttons[1])
  207. m_dirty[0] = m_dirty[1] = m_dirty[2] = m_dirty[3] = 2;
  208. /* Transformation from current frame to current frame is always
  209. * identity. */
  210. m_zoom_settings[m_frame][0] = 0.0f;
  211. m_zoom_settings[m_frame][1] = 0.0f;
  212. m_zoom_settings[m_frame][2] = 1.0f;
  213. /* Compute transformation from other frames to current frame */
  214. for (int i = 0; i < 3; i++)
  215. {
  216. int prev_index = (m_frame + 4 - i) % 4;
  217. int cur_index = (m_frame + 3 - i) % 4;
  218. m_zoom_settings[cur_index][0] = m_zoom_settings[prev_index][0] * m_deltascale[cur_index] + m_deltashift[cur_index].x;
  219. m_zoom_settings[cur_index][1] = m_zoom_settings[prev_index][1] * m_deltascale[cur_index] + m_deltashift[cur_index].y;
  220. m_zoom_settings[cur_index][2] = m_zoom_settings[prev_index][2] * m_deltascale[cur_index];
  221. }
  222. #if !defined __native_client__
  223. char buf[128];
  224. sprintf(buf, "center: %+16.14f%+16.14fi", m_center.x, m_center.y);
  225. m_centertext->SetText(buf);
  226. sprintf(buf, " mouse: %+16.14f%+16.14fi", worldmouse.x, worldmouse.y);
  227. m_mousetext->SetText(buf);
  228. sprintf(buf, " zoom: %g", 1.0 / m_radius);
  229. m_zoomtext->SetText(buf);
  230. #endif
  231. u8vec4 *m_pixelstart = m_pixels + m_size.x * m_size.y / 4 * m_frame;
  232. if (m_dirty[m_frame])
  233. {
  234. double const maxsqlen = 1024;
  235. double const k1 = 1.0 / (1 << 10) / log2(maxsqlen);
  236. m_dirty[m_frame]--;
  237. for (int j = ((m_frame + 1) % 4) / 2; j < m_size.y; j += 2)
  238. for (int i = m_frame % 2; i < m_size.x; i += 2)
  239. {
  240. f64cmplx z0 = m_center + TexelToWorldOffset(ivec2(i, j));
  241. f64cmplx r0 = z0;
  242. //f64cmplx r0(0.28693186889504513, 0.014286693904085048);
  243. //f64cmplx r0(0.001643721971153, 0.822467633298876);
  244. //f64cmplx r0(-1.207205434596, 0.315432814901);
  245. //f64cmplx r0(-0.79192956889854, -0.14632423080102);
  246. //f64cmplx r0(0.3245046418497685, 0.04855101129280834);
  247. f64cmplx z;
  248. int iter = MAX_ITERATIONS;
  249. for (z = z0; iter && z.sqlen() < maxsqlen; z = z * z + r0)
  250. --iter;
  251. if (iter)
  252. {
  253. double f = iter;
  254. double n = z.sqlen();
  255. if (n > maxsqlen * maxsqlen)
  256. n = maxsqlen * maxsqlen;
  257. /* Approximate log(sqrt(n))/log(sqrt(maxsqlen)) */
  258. union { double n; uint64_t x; } u = { n };
  259. double k = (u.x >> 42) - (((1 << 10) - 1) << 10);
  260. k *= k1;
  261. /* Approximate log2(k) in [1,2]. */
  262. f += (- 0.344847817623168308695977510213252644185 * k
  263. + 2.024664188044341212602376988171727038739) * k
  264. - 1.674876738008591047163498125918330313237;
  265. *m_pixelstart++ = m_palette[(int)(f * PALETTE_STEP)];
  266. }
  267. else
  268. {
  269. *m_pixelstart++ = u8vec4(0, 0, 0, 255);
  270. }
  271. }
  272. }
  273. }
  274. virtual void TickDraw(float deltams)
  275. {
  276. WorldEntity::TickDraw(deltams);
  277. static float const vertices[] =
  278. {
  279. 1.0f, 1.0f,
  280. -1.0f, 1.0f,
  281. -1.0f, -1.0f,
  282. -1.0f, -1.0f,
  283. 1.0f, -1.0f,
  284. 1.0f, 1.0f,
  285. };
  286. static float const texcoords[] =
  287. {
  288. 1.0f, 1.0f,
  289. 0.0f, 1.0f,
  290. 0.0f, 0.0f,
  291. 0.0f, 0.0f,
  292. 1.0f, 0.0f,
  293. 1.0f, 1.0f,
  294. };
  295. if (!m_ready)
  296. {
  297. /* Create a texture of half the width and twice the height
  298. * so that we can upload four different subimages each frame. */
  299. glGenTextures(1, &m_texid);
  300. glBindTexture(GL_TEXTURE_2D, m_texid);
  301. glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT,
  302. m_size.x / 2, m_size.y * 2, 0,
  303. TEXTURE_FORMAT, TEXTURE_TYPE, m_pixels);
  304. #if defined __CELLOS_LV2__
  305. /* We need this hint because by default the storage type is
  306. * GL_TEXTURE_SWIZZLED_GPU_SCE. */
  307. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_ALLOCATION_HINT_SCE,
  308. GL_TEXTURE_TILED_GPU_SCE);
  309. #endif
  310. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  311. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  312. m_shader = Shader::Create(
  313. #if !defined __CELLOS_LV2__
  314. #if !defined HAVE_GLES_2X
  315. "#version 120\n"
  316. #else
  317. "precision highp float;"
  318. #endif
  319. ""
  320. #if defined HAVE_GLES_2X
  321. "varying vec2 pass_TexCoord;"
  322. #endif
  323. "attribute vec2 in_TexCoord;"
  324. "attribute vec2 in_Vertex;"
  325. "void main(void)"
  326. "{"
  327. " gl_Position = vec4(in_Vertex, 0.0, 1.0);"
  328. #if defined HAVE_GLES_2X
  329. " pass_TexCoord = in_TexCoord;"
  330. #else
  331. " gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);"
  332. #endif
  333. "}",
  334. #if !defined HAVE_GLES_2X
  335. "#version 120\n"
  336. #else
  337. "precision highp float;"
  338. #endif
  339. ""
  340. #if defined HAVE_GLES_2X
  341. "varying vec2 pass_TexCoord;"
  342. #endif
  343. "uniform vec4 in_TexelSize;"
  344. "uniform mat4 in_ZoomSettings;"
  345. "uniform sampler2D in_Texture;"
  346. ""
  347. "float mylen(vec2 p)"
  348. "{"
  349. " p *= in_TexelSize.zw;" /* Correct for aspect ratio */
  350. //" return 0.001 + abs(p.x) + abs(p.y);"
  351. //" return 0.1 + length(p);"
  352. " vec2 q = p * p;"
  353. " return 0.01 + q.x + q.y;"
  354. "}"
  355. ""
  356. /* Get the coordinate of the nearest point in slice 0 in xy,
  357. * and the squared distance to that point in z.
  358. * p is in normalised [0,1] texture coordinates.
  359. * return value has the 0.25 Y scaling. */
  360. "vec3 nearest0(vec2 p)"
  361. "{"
  362. /* Step 1: center point in [-.5,.5], apply zoom and
  363. * translation transformation, and go back to texture
  364. * coordinates in [0,1]. That's the ideal point we
  365. * would like to compute the value for. */
  366. " p -= vec2(0.5, 0.5);"
  367. " p *= in_ZoomSettings[0][2];"
  368. " p += vec2(in_ZoomSettings[0][0], -in_ZoomSettings[0][1]);"
  369. " p += vec2(0.5, 0.5);"
  370. /* Step 2: find out the (rounded) texel coordinates in
  371. * the half-resolution slice. We use an offset of 15/32
  372. * instead of 1/2 to avoid rounding issues at edges. */
  373. " vec2 q = p + 0.46875 * in_TexelSize.xy;"
  374. " q = in_TexelSize.xy"
  375. " * (1.0 + 2.0 * floor(q * 0.5 * in_TexelSize.zw));"
  376. /* Step 3: go back to full-resolution texel coordinates
  377. * so that we know the error between the pixel we get
  378. * and the pixel we wanted. */
  379. " vec2 r = q - 0.5 * in_TexelSize.xy;"
  380. " float l = (abs(q.x - .5) < .5 && abs(q.y - .5) < .5)"
  381. " ? 1.0 / mylen(r - p) : 0.0;"
  382. /* Step 4: return final texel coordinates and
  383. * corresponding error value. */
  384. " return vec3(q * vec2(1.0, 0.25), l);"
  385. "}"
  386. ""
  387. "vec3 nearest1(vec2 p)"
  388. "{"
  389. " p -= vec2(0.5, 0.5);"
  390. " p *= in_ZoomSettings[1][2];"
  391. " p += vec2(in_ZoomSettings[1][0], -in_ZoomSettings[1][1]);"
  392. " p += vec2(0.5, 0.5);"
  393. " vec2 q = p + -0.46875 * in_TexelSize.xy;"
  394. " q = in_TexelSize.xy"
  395. " * (1.0 + 2.0 * floor(q * 0.5 * in_TexelSize.zw));"
  396. " vec2 r = q - -0.5 * in_TexelSize.xy;"
  397. " float l = (abs(q.x - .5) < .5 && abs(q.y - .5) < .5)"
  398. " ? 1.0 / mylen(r - p) : 0.0;"
  399. " return vec3(q * vec2(1.0, 0.25) + vec2(0.0, 0.25), l);"
  400. "}"
  401. ""
  402. "vec3 nearest2(vec2 p)"
  403. "{"
  404. " p -= vec2(0.5, 0.5);"
  405. " p *= in_ZoomSettings[2][2];"
  406. " p += vec2(in_ZoomSettings[2][0], -in_ZoomSettings[2][1]);"
  407. " p += vec2(0.5, 0.5);"
  408. " vec2 q = p + vec2(0.46875, -0.46875) * in_TexelSize.xy;"
  409. " q = in_TexelSize.xy"
  410. " * (1.0 + 2.0 * floor(q * 0.5 * in_TexelSize.zw));"
  411. " vec2 r = q - vec2(0.5, -0.5) * in_TexelSize.xy;"
  412. " float l = (abs(q.x - .5) < .5 && abs(q.y - .5) < .5)"
  413. " ? 1.0 / mylen(r - p) : 0.0;"
  414. " return vec3(q * vec2(1.0, 0.25) + vec2(0.0, 0.50), l);"
  415. "}"
  416. ""
  417. "vec3 nearest3(vec2 p)"
  418. "{"
  419. " p -= vec2(0.5, 0.5);"
  420. " p *= in_ZoomSettings[3][2];"
  421. " p += vec2(in_ZoomSettings[3][0], -in_ZoomSettings[3][1]);"
  422. " p += vec2(0.5, 0.5);"
  423. " vec2 q = p + vec2(-0.46875, 0.46875) * in_TexelSize.xy;"
  424. " q = in_TexelSize.xy"
  425. " * (1.0 + 2.0 * floor(q * 0.5 * in_TexelSize.zw));"
  426. " vec2 r = q - vec2(-0.5, 0.5) * in_TexelSize.xy;"
  427. " float l = (abs(q.x - .5) < .5 && abs(q.y - .5) < .5)"
  428. " ? 1.0 / mylen(r - p) : 0.0;"
  429. " return vec3(q * vec2(1.0, 0.25) + vec2(0.0, 0.75), l);"
  430. "}"
  431. ""
  432. "void main(void)"
  433. "{"
  434. #if defined HAVE_GLES_2X
  435. " vec2 coord = pass_TexCoord;"
  436. #else
  437. " vec2 coord = gl_TexCoord[0].xy;"
  438. #endif
  439. /* Get a pixel from each slice */
  440. " vec3 k0 = nearest0(coord);"
  441. " vec3 k1 = nearest1(coord);"
  442. " vec3 k2 = nearest2(coord);"
  443. " vec3 k3 = nearest3(coord);"
  444. " vec4 p0 = texture2D(in_Texture, k0.xy);"
  445. " vec4 p1 = texture2D(in_Texture, k1.xy);"
  446. " vec4 p2 = texture2D(in_Texture, k2.xy);"
  447. " vec4 p3 = texture2D(in_Texture, k3.xy);"
  448. "if (k0.z >= k1.z && k0.z >= k2.z && k0.z >= k3.z) gl_FragColor = p0;"
  449. "if (k1.z >= k0.z && k1.z >= k2.z && k1.z >= k3.z) gl_FragColor = p1;"
  450. "if (k2.z >= k0.z && k2.z >= k1.z && k2.z >= k3.z) gl_FragColor = p2;"
  451. "if (k3.z >= k0.z && k3.z >= k1.z && k3.z >= k2.z) gl_FragColor = p3;"
  452. // " gl_FragColor = 1.0 / (k0.z + k1.z + k2.z + k3.z)"
  453. // " * (k0.z * p0 + k1.z * p1 + k2.z * p2 + k3.z * p3);"
  454. "}"
  455. #else
  456. "void main(float4 in_Position : POSITION,"
  457. " float2 in_TexCoord : TEXCOORD0,"
  458. " out float4 out_Position : POSITION,"
  459. " out float2 out_TexCoord : TEXCOORD0)"
  460. "{"
  461. " out_TexCoord = in_TexCoord;"
  462. " out_Position = in_Position;"
  463. "}",
  464. "float3 nearest0(float2 p, float4 in_TexelSize)"
  465. "{"
  466. " float2 q = p + 0.5 * in_TexelSize.xy;"
  467. " q -= fmod(q, 2.0 * in_TexelSize.xy);"
  468. " q += 0.5 * in_TexelSize.xy;"
  469. " return float3(q * float2(1.0, 0.25),"
  470. " length(q - p));"
  471. "}"
  472. ""
  473. "float3 nearest1(float2 p, float4 in_TexelSize)"
  474. "{"
  475. " float2 q = p - 0.5 * in_TexelSize.xy;"
  476. " q -= fmod(q, 2.0 * in_TexelSize.xy);"
  477. " q += 1.5 * in_TexelSize.xy;"
  478. " return float3(q * float2(1.0, 0.25) + float2(0.0, 0.25),"
  479. " length(q - p));"
  480. "}"
  481. ""
  482. "float3 nearest2(float2 p, float4 in_TexelSize)"
  483. "{"
  484. " float2 q = p + float2(0.5, -0.5) * in_TexelSize.xy;"
  485. " q -= fmod(q, 2.0 * in_TexelSize.xy);"
  486. " q += float2(0.5, 1.5) * in_TexelSize.xy;"
  487. " return float3(q * float2(1.0, 0.25) + float2(0.0, 0.50),"
  488. " length(q - p));"
  489. "}"
  490. ""
  491. "float3 nearest3(float2 p, float4 in_TexelSize)"
  492. "{"
  493. " float2 q = p + float2(-0.5, 0.5) * in_TexelSize.xy;"
  494. " q -= fmod(q, 2.0 * in_TexelSize.xy);"
  495. " q += float2(1.5, 0.5) * in_TexelSize.xy;"
  496. " return float3(q * float2(1.0, 0.25) + float2(0.0, 0.75),"
  497. " length(q - p));"
  498. "}"
  499. ""
  500. "void main(float2 in_TexCoord : TEXCOORD0,"
  501. " uniform float4 in_TexelSize,"
  502. " uniform sampler2D in_Texture,"
  503. " out float4 out_FragColor : COLOR)"
  504. "{"
  505. " float2 coord = in_TexCoord.xy;"
  506. " coord -= 0.1 * in_TexelSize.xy;"
  507. " float4 p0 = tex2D(in_Texture, nearest0(coord, in_TexelSize).xy);"
  508. " float4 p1 = tex2D(in_Texture, nearest1(coord, in_TexelSize).xy);"
  509. " float4 p2 = tex2D(in_Texture, nearest2(coord, in_TexelSize).xy);"
  510. " float4 p3 = tex2D(in_Texture, nearest3(coord, in_TexelSize).xy);"
  511. " out_FragColor = 0.25 * (p0 + p1 + p2 + p3);"
  512. "}"
  513. #endif
  514. );
  515. m_vertexattrib = m_shader->GetAttribLocation("in_Vertex");
  516. m_texattrib = m_shader->GetAttribLocation("in_TexCoord");
  517. m_texeluni = m_shader->GetUniformLocation("in_TexelSize");
  518. m_zoomuni = m_shader->GetUniformLocation("in_ZoomSettings");
  519. m_ready = true;
  520. #if !defined __CELLOS_LV2__ && !defined __ANDROID__
  521. /* Method 1: store vertex buffer on the GPU memory */
  522. glGenBuffers(1, &m_vbo);
  523. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  524. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
  525. GL_STATIC_DRAW);
  526. glGenBuffers(1, &m_tbo);
  527. glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
  528. glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords,
  529. GL_STATIC_DRAW);
  530. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  531. /* Method 2: upload vertex information at each frame */
  532. #else
  533. #endif
  534. /* FIXME: this object never cleans up */
  535. }
  536. #if !defined HAVE_GLES_2X
  537. glEnable(GL_TEXTURE_2D);
  538. #endif
  539. glBindTexture(GL_TEXTURE_2D, m_texid);
  540. if (m_dirty[m_frame])
  541. {
  542. m_dirty[m_frame]--;
  543. #ifdef __CELLOS_LV2__
  544. /* glTexSubImage2D is extremely slow on the PS3, to the point
  545. * that uploading the whole texture is 40 times faster. */
  546. glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT,
  547. m_size.x / 2, m_size.y * 2, 0,
  548. TEXTURE_FORMAT, TEXTURE_TYPE, m_pixels);
  549. #else
  550. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, m_frame * m_size.y / 2,
  551. m_size.x / 2, m_size.y / 2,
  552. TEXTURE_FORMAT, TEXTURE_TYPE,
  553. m_pixels + m_size.x * m_size.y / 4 * m_frame);
  554. #endif
  555. }
  556. m_shader->Bind();
  557. m_shader->SetUniform(m_texeluni, m_texel_settings);
  558. m_shader->SetUniform(m_zoomuni, m_zoom_settings);
  559. #if !defined __CELLOS_LV2__ && !defined __ANDROID__
  560. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  561. glEnableVertexAttribArray(m_vertexattrib);
  562. glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  563. glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
  564. glEnableVertexAttribArray(m_texattrib);
  565. glVertexAttribPointer(m_texattrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  566. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  567. /* Never used for now */
  568. //glEnableVertexAttribArray(m_vertexattrib);
  569. //glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  570. #else
  571. glEnableClientState(GL_VERTEX_ARRAY);
  572. glVertexPointer(2, GL_FLOAT, 0, vertices);
  573. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  574. glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
  575. #endif
  576. glDrawArrays(GL_TRIANGLES, 0, 6);
  577. #if !defined __CELLOS_LV2__ && !defined __ANDROID__
  578. glDisableVertexAttribArray(m_vertexattrib);
  579. glDisableVertexAttribArray(m_texattrib);
  580. glBindBuffer(GL_ARRAY_BUFFER, 0);
  581. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__
  582. /* Never used for now */
  583. //glDisableVertexAttribArray(m_vertexattrib);
  584. //glDisableVertexAttribArray(m_texattrib);
  585. #else
  586. glDisableClientState(GL_VERTEX_ARRAY);
  587. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  588. #endif
  589. }
  590. private:
  591. static int const MAX_ITERATIONS = 170;
  592. static int const PALETTE_STEP = 32;
  593. ivec2 m_size, m_window_size;
  594. double m_window2world;
  595. f64vec2 m_texel2world;
  596. u8vec4 *m_pixels, *m_tmppixels, *m_palette;
  597. Shader *m_shader;
  598. GLuint m_texid;
  599. #if !defined __CELLOS_LV2__ && !defined __ANDROID__
  600. GLuint m_vbo, m_tbo;
  601. GLuint m_tco;
  602. #endif
  603. int m_vertexattrib, m_texattrib, m_texeluni, m_zoomuni;
  604. int m_frame, m_dirty[4];
  605. bool m_ready;
  606. f64cmplx m_center;
  607. double m_zoom_speed, m_radius;
  608. vec4 m_texel_settings;
  609. mat4 m_zoom_settings;
  610. f64cmplx m_deltashift[4];
  611. double m_deltascale[4];
  612. /* Debug information */
  613. #if !defined __native_client__
  614. Text *m_centertext, *m_mousetext, *m_zoomtext;
  615. #endif
  616. };
  617. int main(int argc, char **argv)
  618. {
  619. #if defined _WIN32
  620. _chdir("../..");
  621. #endif
  622. Application app("Tutorial 3: Fractal", ivec2(640, 480), 60.0f);
  623. new DebugFps(5, 5);
  624. new Fractal(ivec2(640, 480));
  625. //new DebugRecord("fractalol.ogm", 60.0f);
  626. app.Run();
  627. return EXIT_SUCCESS;
  628. }