299 行
9.7 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_center = 0;
  35. //m_target = f64cmplx(0.001643721971153, 0.822467633298876);
  36. m_target = f64cmplx(-1.207205434596, 0.315432814901);
  37. //m_target = f64cmplx(-0.79192956889854, -0.14632423080102);
  38. //m_target = f64cmplx(0.3245046418497685, 0.04855101129280834);
  39. //m_target = f64cmplx(0.28693186889504513, 0.014286693904085048);
  40. m_angle = 0.0;
  41. m_radius = 8.0;
  42. m_ready = false;
  43. }
  44. ~Fractal()
  45. {
  46. delete m_pixels;
  47. }
  48. virtual void TickGame(float deltams)
  49. {
  50. WorldEntity::TickGame(deltams);
  51. m_frame = (m_frame + 1) % 4;
  52. double zoom = pow(2.0, -deltams * 0.0005);
  53. m_radius *= zoom;
  54. m_center = (m_center - m_target) * zoom * zoom + m_target;
  55. double step = m_radius / (m_size.x > m_size.y ? m_size.x : m_size.y);
  56. // m_angle -= deltams * 0.00015;
  57. f64cmplx transform = step * f64cmplx(cos(m_angle), sin(m_angle));
  58. u8vec4 *m_pixelstart = m_pixels + m_size.x * m_size.y / 4 * m_frame;
  59. for (int j = ((m_frame + 1) % 4) / 2; j < m_size.y; j += 2)
  60. for (int i = m_frame % 2; i < m_size.x; i += 2)
  61. {
  62. double const maxlen = 32;
  63. int const maxiter = 170;
  64. f64cmplx delta(i - m_size.x / 2, j - m_size.y / 2);
  65. f64cmplx z0 = m_center + transform * delta;
  66. f64cmplx r0 = z0;
  67. //f64cmplx r0(0.28693186889504513, 0.014286693904085048);
  68. //f64cmplx r0(0.001643721971153, 0.822467633298876);
  69. f64cmplx z;
  70. int iter = maxiter;
  71. for (z = z0; iter && z.sqlen() < maxlen * maxlen; z = z * z + r0)
  72. --iter;
  73. double f = iter;
  74. double n = z.sqlen();
  75. double k = log(n) * 0.5f / log(maxlen);
  76. /* Approximate log2(k) in [1,2]. */
  77. f += (- 0.344847817623168308695977510213252644185 * k
  78. + 2.024664188044341212602376988171727038739) * k
  79. - 1.674876738008591047163498125918330313237;
  80. if (iter)
  81. {
  82. double r = 0.5 * sin(f * 0.27 - 2.0) + 0.5;
  83. double g = 0.5 * sin(f * 0.13 + 1.0) + 0.5;
  84. double b = 0.5 * sin(f * 0.21) + 0.5;
  85. uint8_t red = r * 255.0f;
  86. uint8_t green = g * 255.0f;
  87. uint8_t blue = b * 255.0f;
  88. *m_pixelstart++ = u8vec4(red, green, blue, 0);
  89. }
  90. else
  91. {
  92. *m_pixelstart++ = u8vec4(0, 0, 0, 0);
  93. }
  94. }
  95. }
  96. virtual void TickDraw(float deltams)
  97. {
  98. WorldEntity::TickDraw(deltams);
  99. static float const vertices[] =
  100. {
  101. 1.0f, 1.0f,
  102. -1.0f, 1.0f,
  103. -1.0f, -1.0f,
  104. -1.0f, -1.0f,
  105. 1.0f, -1.0f,
  106. 1.0f, 1.0f,
  107. };
  108. static float const texcoords[] =
  109. {
  110. 1.0f, 0.0f,
  111. 0.0f, 0.0f,
  112. 0.0f, 1.0f,
  113. 0.0f, 1.0f,
  114. 1.0f, 1.0f,
  115. 1.0f, 0.0f,
  116. };
  117. if (!m_ready)
  118. {
  119. /* Create a texture of half the width and twice the height
  120. * so that we can upload four different subimages each frame. */
  121. glGenTextures(1, &m_texid);
  122. glBindTexture(GL_TEXTURE_2D, m_texid);
  123. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_size.x / 2, m_size.y * 2,
  124. 0, GL_RGBA, GL_UNSIGNED_BYTE, m_pixels);
  125. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  126. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  127. m_shader = Shader::Create(
  128. #if !defined __CELLOS_LV2__
  129. "#version 120\n"
  130. "attribute vec2 in_TexCoord;\n"
  131. "attribute vec2 in_Vertex;"
  132. "void main(void) {"
  133. " gl_Position = vec4(in_Vertex, 0.0, 1.0);"
  134. " gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
  135. "}",
  136. "#version 120\n"
  137. "uniform sampler2D in_Texture;\n"
  138. "void main(void) {"
  139. " vec2 coord = gl_TexCoord[0].xy;"
  140. /* gl_FragCoord is centered inside the pixel, so we remove
  141. * 0.5 from gl_FragCoord.x. Also, (0,0) is at the bottom
  142. * left whereas our images have (0,0) at the top left, so we
  143. * _add_ 0.5 to gl_FragCoord.y. */
  144. " float i = mod(gl_FragCoord.x - 0.5, 2.0);"
  145. " float j = mod(gl_FragCoord.y + 0.5 + i, 2.0);"
  146. " coord.y += i + j * 2;"
  147. " coord.y *= 0.25;"
  148. " vec4 p = texture2D(in_Texture, coord);"
  149. " gl_FragColor = p;"
  150. "}"
  151. #else
  152. "void main(float4 in_Position : POSITION,"
  153. " float2 in_TexCoord : TEXCOORD0,"
  154. " out float4 out_Position : POSITION,"
  155. " out float2 out_TexCoord : TEXCOORD0)"
  156. "{"
  157. " out_TexCoord = in_TexCoord;"
  158. " out_Position = in_Position;"
  159. "}",
  160. "void main(float2 in_TexCoord : TEXCOORD0,"
  161. " uniform sampler2D tex,"
  162. " out float4 out_FragColor : COLOR)"
  163. "{"
  164. " out_FragColor = tex2D(tex, in_TexCoord);"
  165. "}"
  166. #endif
  167. );
  168. m_vertexattrib = m_shader->GetAttribLocation("in_Vertex");
  169. m_texattrib = m_shader->GetAttribLocation("in_TexCoord");
  170. m_ready = true;
  171. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  172. /* Method 1: store vertex buffer on the GPU memory */
  173. glGenBuffers(1, &m_vbo);
  174. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  175. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
  176. GL_STATIC_DRAW);
  177. glGenBuffers(1, &m_tbo);
  178. glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
  179. glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords,
  180. GL_STATIC_DRAW);
  181. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  182. /* Method 2: upload vertex information at each frame */
  183. #else
  184. #endif
  185. /* FIXME: this object never cleans up */
  186. }
  187. glEnable(GL_TEXTURE_2D);
  188. glBindTexture(GL_TEXTURE_2D, m_texid);
  189. glTexSubImage2D(GL_TEXTURE_2D, 0,
  190. 0, m_frame * m_size.y / 2, m_size.x / 2, m_size.y / 2,
  191. #if !defined __CELLOS_LV2__
  192. GL_RGBA, GL_UNSIGNED_BYTE,
  193. #else
  194. /* The PS3 is big-endian */
  195. GL_RGBA, GL_UNSIGNED_INT_8_8_8_8,
  196. #endif
  197. m_pixels + m_size.x * m_size.y / 4 * m_frame);
  198. m_shader->Bind();
  199. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  200. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  201. glEnableVertexAttribArray(m_vertexattrib);
  202. glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  203. glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
  204. glEnableVertexAttribArray(m_texattrib);
  205. glVertexAttribPointer(m_texattrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  206. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  207. /* Never used for now */
  208. //glEnableVertexAttribArray(m_vertexattrib);
  209. //glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  210. #else
  211. glEnableClientState(GL_VERTEX_ARRAY);
  212. glVertexPointer(2, GL_FLOAT, 0, vertices);
  213. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  214. glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
  215. #endif
  216. glDrawArrays(GL_TRIANGLES, 0, 6);
  217. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  218. glDisableVertexAttribArray(m_vertexattrib);
  219. glDisableVertexAttribArray(m_texattrib);
  220. glBindBuffer(GL_ARRAY_BUFFER, 0);
  221. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  222. /* Never used for now */
  223. //glDisableVertexAttribArray(m_vertexattrib);
  224. //glDisableVertexAttribArray(m_texattrib);
  225. #else
  226. glDisableClientState(GL_VERTEX_ARRAY);
  227. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  228. #endif
  229. }
  230. private:
  231. ivec2 m_size;
  232. u8vec4 *m_pixels;
  233. Shader *m_shader;
  234. GLuint m_texid;
  235. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  236. GLuint m_vbo, m_tbo;
  237. GLuint m_tco;
  238. #endif
  239. int m_vertexattrib, m_texattrib;
  240. int m_frame;
  241. bool m_ready;
  242. f64cmplx m_center, m_target;
  243. double m_radius, m_angle;
  244. };
  245. int main()
  246. {
  247. #if defined _WIN32
  248. _chdir("../..");
  249. #endif
  250. Application app("Tutorial 3: Fractal", ivec2(640, 480), 60.0f);
  251. new DebugFps(5, 5);
  252. new Fractal(ivec2(640, 480));
  253. //new DebugRecord("fractalol.ogm", 60.0f);
  254. app.Run();
  255. return EXIT_SUCCESS;
  256. }