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