No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

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