410 lines
14 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. class Fractal : public WorldEntity
  27. {
  28. public:
  29. Fractal(ivec2 const &size)
  30. {
  31. m_size = size;
  32. m_pixels = new u8vec4[size.x * size.y];
  33. m_tmppixels = new u8vec4[size.x / 2 * size.y / 2];
  34. m_frame = -1;
  35. for (int i = 0; i < 4; i++)
  36. {
  37. m_deltashift[i] = 0.0;
  38. m_deltascale[i] = 1.0;
  39. m_dirty[i] = 2;
  40. }
  41. m_center = -0.75;
  42. m_radius = 1.5;
  43. m_screenradius = 0.5 * (m_size.x < m_size.y ? m_size.x : m_size.y);
  44. m_ready = false;
  45. m_palette = new u8vec4[MAX_ITERATIONS * PALETTE_STEP];
  46. for (int i = 0; i < MAX_ITERATIONS * PALETTE_STEP; i++)
  47. {
  48. float f = i / (double)PALETTE_STEP;
  49. double r = 0.5 * sin(f * 0.27 - 1.5) + 0.5;
  50. double g = 0.5 * sin(f * 0.13 + 1.3) + 0.5;
  51. double b = 0.5 * sin(f * 0.21 + 0.4) + 0.5;
  52. uint8_t red = r * 255.0f;
  53. uint8_t green = g * 255.0f;
  54. uint8_t blue = b * 255.0f;
  55. m_palette[i] = u8vec4(red, green, blue, 0);
  56. }
  57. m_centertext = new Text(NULL, "gfx/font/ascii.png");
  58. m_centertext->SetPos(ivec3(5, m_size.y - 15, 1));
  59. Ticker::Ref(m_centertext);
  60. m_mousetext = new Text(NULL, "gfx/font/ascii.png");
  61. m_mousetext->SetPos(ivec3(5, m_size.y - 29, 1));
  62. Ticker::Ref(m_mousetext);
  63. m_zoomtext = new Text(NULL, "gfx/font/ascii.png");
  64. m_zoomtext->SetPos(ivec3(5, m_size.y - 43, 1));
  65. Ticker::Ref(m_zoomtext);
  66. position = ivec3(0, 0, 0);
  67. bbox[0] = position;
  68. bbox[1] = ivec3(size, 0);
  69. Input::TrackMouse(this);
  70. }
  71. ~Fractal()
  72. {
  73. Input::UntrackMouse(this);
  74. Ticker::Unref(m_centertext);
  75. Ticker::Unref(m_mousetext);
  76. Ticker::Unref(m_zoomtext);
  77. delete m_pixels;
  78. delete m_tmppixels;
  79. delete m_palette;
  80. }
  81. inline f64cmplx ScreenToWorldOffset(ivec2 pixel)
  82. {
  83. f64cmplx tmp = f64cmplx(0.5 + pixel.x - m_size.x / 2,
  84. 0.5 + m_size.y / 2 - pixel.y);
  85. return tmp * (m_radius / m_screenradius);
  86. }
  87. virtual void TickGame(float deltams)
  88. {
  89. WorldEntity::TickGame(deltams);
  90. m_frame = (m_frame + 1) % 4;
  91. f64cmplx worldmouse = m_center + ScreenToWorldOffset(mousepos);
  92. ivec3 buttons = Input::GetMouseButtons();
  93. if ((buttons[0] || buttons[2]) && mousepos.x != -1)
  94. {
  95. f64cmplx oldcenter = m_center;
  96. double oldradius = m_radius;
  97. double zoom = pow(2.0, (buttons[0] ? -deltams : deltams) * 0.0015);
  98. if (m_radius * zoom > 8.0)
  99. zoom = 8.0 / m_radius;
  100. else if (m_radius * zoom < 1e-14)
  101. zoom = 1e-14 / m_radius;
  102. m_radius *= zoom;
  103. m_center = (m_center - worldmouse) * zoom + worldmouse;
  104. worldmouse = m_center + ScreenToWorldOffset(mousepos);
  105. /* Store the transformation properties to go from m_frame-1
  106. * to m_frame. */
  107. m_deltashift[m_frame] = (oldcenter - m_center) / m_radius;
  108. m_deltascale[m_frame] = oldradius / m_radius;
  109. m_dirty[0] = m_dirty[1] = m_dirty[2] = m_dirty[3] = 2;
  110. }
  111. else
  112. {
  113. /* If settings didn't change, set transformation from previous
  114. * frame to identity. */
  115. m_deltashift[m_frame] = 0.0;
  116. m_deltascale[m_frame] = 1.0;
  117. }
  118. if (buttons[1])
  119. m_dirty[0] = m_dirty[1] = m_dirty[2] = m_dirty[3] = 2;
  120. char buf[128];
  121. sprintf(buf, "center: %+13.11f%+13.11fi", m_center.x, m_center.y);
  122. m_centertext->SetText(buf);
  123. sprintf(buf, " mouse: %+13.11f%+13.11fi", worldmouse.x, worldmouse.y);
  124. m_mousetext->SetText(buf);
  125. sprintf(buf, " zoom: %g", 1.0 / m_radius);
  126. m_zoomtext->SetText(buf);
  127. u8vec4 *m_pixelstart = m_pixels + m_size.x * m_size.y / 4 * m_frame;
  128. if (m_dirty[m_frame])
  129. {
  130. m_dirty[m_frame]--;
  131. for (int j = ((m_frame + 1) % 4) / 2; j < m_size.y; j += 2)
  132. for (int i = m_frame % 2; i < m_size.x; i += 2)
  133. {
  134. double const maxlen = 32;
  135. f64cmplx z0 = m_center + ScreenToWorldOffset(ivec2(i, j));
  136. f64cmplx r0 = z0;
  137. //f64cmplx r0(0.28693186889504513, 0.014286693904085048);
  138. //f64cmplx r0(0.001643721971153, 0.822467633298876);
  139. //f64cmplx r0(-1.207205434596, 0.315432814901);
  140. //f64cmplx r0(-0.79192956889854, -0.14632423080102);
  141. //f64cmplx r0(0.3245046418497685, 0.04855101129280834);
  142. f64cmplx z;
  143. int iter = MAX_ITERATIONS;
  144. for (z = z0; iter && z.sqlen() < maxlen * maxlen; z = z * z + r0)
  145. --iter;
  146. if (iter)
  147. {
  148. double f = iter;
  149. double n = z.sqlen();
  150. double k = log(n) * 0.5f / log(maxlen);
  151. /* Approximate log2(k) in [1,2]. */
  152. f += (- 0.344847817623168308695977510213252644185 * k
  153. + 2.024664188044341212602376988171727038739) * k
  154. - 1.674876738008591047163498125918330313237;
  155. *m_pixelstart++ = m_palette[(int)(f * PALETTE_STEP + 0.25 * m_frame)];
  156. }
  157. else
  158. {
  159. *m_pixelstart++ = u8vec4(0, 0, 0, 0);
  160. }
  161. }
  162. }
  163. }
  164. virtual void TickDraw(float deltams)
  165. {
  166. WorldEntity::TickDraw(deltams);
  167. static float const vertices[] =
  168. {
  169. 1.0f, 1.0f,
  170. -1.0f, 1.0f,
  171. -1.0f, -1.0f,
  172. -1.0f, -1.0f,
  173. 1.0f, -1.0f,
  174. 1.0f, 1.0f,
  175. };
  176. static float const texcoords[] =
  177. {
  178. 1.0f, 1.0f,
  179. 0.0f, 1.0f,
  180. 0.0f, 0.0f,
  181. 0.0f, 0.0f,
  182. 1.0f, 0.0f,
  183. 1.0f, 1.0f,
  184. };
  185. if (!m_ready)
  186. {
  187. /* Create a texture of half the width and twice the height
  188. * so that we can upload four different subimages each frame. */
  189. glGenTextures(1, &m_texid);
  190. glBindTexture(GL_TEXTURE_2D, m_texid);
  191. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_size.x / 2, m_size.y * 2,
  192. 0, GL_RGBA, GL_UNSIGNED_BYTE, m_pixels);
  193. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  194. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  195. m_shader = Shader::Create(
  196. #if !defined __CELLOS_LV2__
  197. "#version 120\n"
  198. "attribute vec2 in_TexCoord;\n"
  199. "attribute vec2 in_Vertex;"
  200. "void main(void) {"
  201. " gl_Position = vec4(in_Vertex, 0.0, 1.0);"
  202. " gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
  203. "}",
  204. "#version 120\n"
  205. "uniform sampler2D in_Texture;\n"
  206. "void main(void) {"
  207. " vec2 coord = gl_TexCoord[0].xy;"
  208. /* gl_FragCoord is centered inside the pixel, so we remove
  209. * 0.5 from gl_FragCoord.x. Also, (0,0) is at the bottom
  210. * left whereas our images have (0,0) at the top left, so we
  211. * _add_ 0.5 to gl_FragCoord.y. (XXX: this is no longer true
  212. * but will be again when mouse coordinates are back to
  213. * being top-left again). */
  214. " float i = mod(gl_FragCoord.x - 0.5, 2.0);"
  215. " float j = mod(gl_FragCoord.y - 0.5 + i, 2.0);"
  216. " coord.y += i + j * 2;"
  217. " coord.y *= 0.25;"
  218. " vec4 p = texture2D(in_Texture, coord);"
  219. " gl_FragColor = p;"
  220. "}"
  221. #else
  222. "void main(float4 in_Position : POSITION,"
  223. " float2 in_TexCoord : TEXCOORD0,"
  224. " out float4 out_Position : POSITION,"
  225. " out float2 out_TexCoord : TEXCOORD0)"
  226. "{"
  227. " out_TexCoord = in_TexCoord;"
  228. " out_Position = in_Position;"
  229. "}",
  230. "void main(float2 in_TexCoord : TEXCOORD0,"
  231. " uniform sampler2D tex,"
  232. " out float4 out_FragColor : COLOR)"
  233. "{"
  234. " out_FragColor = tex2D(tex, in_TexCoord);"
  235. "}"
  236. #endif
  237. );
  238. m_vertexattrib = m_shader->GetAttribLocation("in_Vertex");
  239. m_texattrib = m_shader->GetAttribLocation("in_TexCoord");
  240. m_ready = true;
  241. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  242. /* Method 1: store vertex buffer on the GPU memory */
  243. glGenBuffers(1, &m_vbo);
  244. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  245. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
  246. GL_STATIC_DRAW);
  247. glGenBuffers(1, &m_tbo);
  248. glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
  249. glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords,
  250. GL_STATIC_DRAW);
  251. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  252. /* Method 2: upload vertex information at each frame */
  253. #else
  254. #endif
  255. /* FIXME: this object never cleans up */
  256. }
  257. glEnable(GL_TEXTURE_2D);
  258. glBindTexture(GL_TEXTURE_2D, m_texid);
  259. if (m_dirty[m_frame])
  260. {
  261. m_dirty[m_frame]--;
  262. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, m_frame * m_size.y / 2,
  263. m_size.x / 2, m_size.y / 2,
  264. #if !defined __CELLOS_LV2__
  265. GL_RGBA, GL_UNSIGNED_BYTE,
  266. #else
  267. /* The PS3 is big-endian */
  268. GL_RGBA, GL_UNSIGNED_INT_8_8_8_8,
  269. #endif
  270. m_pixels + m_size.x * m_size.y / 4 * m_frame);
  271. }
  272. /* If other frames are dirty, upload fake data for now */
  273. if (0) for (int i = 0; i < 4; i++)
  274. {
  275. if (m_dirty[i])
  276. {
  277. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, i * m_size.y / 2,
  278. m_size.x / 2, m_size.y / 2,
  279. #if !defined __CELLOS_LV2__
  280. GL_RGBA, GL_UNSIGNED_BYTE,
  281. #else
  282. /* The PS3 is big-endian */
  283. GL_RGBA, GL_UNSIGNED_INT_8_8_8_8,
  284. #endif
  285. m_pixels + m_size.x * m_size.y / 4 * m_frame);
  286. }
  287. }
  288. m_shader->Bind();
  289. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  290. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  291. glEnableVertexAttribArray(m_vertexattrib);
  292. glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  293. glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
  294. glEnableVertexAttribArray(m_texattrib);
  295. glVertexAttribPointer(m_texattrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  296. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  297. /* Never used for now */
  298. //glEnableVertexAttribArray(m_vertexattrib);
  299. //glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  300. #else
  301. glEnableClientState(GL_VERTEX_ARRAY);
  302. glVertexPointer(2, GL_FLOAT, 0, vertices);
  303. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  304. glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
  305. #endif
  306. glDrawArrays(GL_TRIANGLES, 0, 6);
  307. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  308. glDisableVertexAttribArray(m_vertexattrib);
  309. glDisableVertexAttribArray(m_texattrib);
  310. glBindBuffer(GL_ARRAY_BUFFER, 0);
  311. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  312. /* Never used for now */
  313. //glDisableVertexAttribArray(m_vertexattrib);
  314. //glDisableVertexAttribArray(m_texattrib);
  315. #else
  316. glDisableClientState(GL_VERTEX_ARRAY);
  317. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  318. #endif
  319. }
  320. private:
  321. static int const MAX_ITERATIONS = 170;
  322. static int const PALETTE_STEP = 32;
  323. ivec2 m_size;
  324. u8vec4 *m_pixels, *m_tmppixels, *m_palette;
  325. Shader *m_shader;
  326. GLuint m_texid;
  327. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  328. GLuint m_vbo, m_tbo;
  329. GLuint m_tco;
  330. #endif
  331. int m_vertexattrib, m_texattrib;
  332. int m_frame, m_dirty[4];
  333. bool m_ready;
  334. f64cmplx m_center;
  335. double m_radius, m_screenradius;
  336. f64cmplx m_deltashift[4];
  337. double m_deltascale[4];
  338. /* Debug information */
  339. Text *m_centertext, *m_mousetext, *m_zoomtext;
  340. };
  341. int main()
  342. {
  343. #if defined _WIN32
  344. _chdir("../..");
  345. #endif
  346. Application app("Tutorial 3: Fractal", ivec2(640, 480), 60.0f);
  347. new DebugFps(5, 5);
  348. new Fractal(ivec2(640, 480));
  349. //new DebugRecord("fractalol.ogm", 60.0f);
  350. app.Run();
  351. return EXIT_SUCCESS;
  352. }