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