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