Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

271 wiersze
8.4 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_time = 0.0f;
  34. m_ready = false;
  35. }
  36. ~Fractal()
  37. {
  38. delete m_pixels;
  39. }
  40. virtual void TickGame(float deltams)
  41. {
  42. WorldEntity::TickGame(deltams);
  43. m_time += deltams * 0.0005f;
  44. f64cmplx center(0.001643721971153, 0.822467633298876);
  45. //f64cmplx center(0.28693186889504513, 0.014286693904085048);
  46. double radius = 8.0 * pow(2.0, -m_time);
  47. double step = radius / (m_size.x > m_size.y ? m_size.x : m_size.y);
  48. for (int j = 0; j < m_size.y; j++)
  49. for (int i = 0; i < m_size.x; i++)
  50. {
  51. double const maxlen = 32;
  52. int const colors = 16;
  53. int const maxiter = 200;
  54. f64cmplx delta(i - m_size.x / 2, j - m_size.y / 2);
  55. f64cmplx z0 = center + step * delta;
  56. f64cmplx r0 = z0;
  57. //f64cmplx r0(0.28693186889504513, 0.014286693904085048);
  58. //f64cmplx r0(-0.824,0.1711);
  59. //f64cmplx r0(0.001643721971153, 0.822467633298876);
  60. f64cmplx z;
  61. int iter = maxiter;
  62. for (z = z0; iter && z.sqlen() < maxlen * maxlen; z = z * z + r0)
  63. --iter;
  64. double f = iter;
  65. double n = z.sqlen();
  66. /* Approximate log2(x) with x-1 because x is in [1,2]. */
  67. f += (log(n) * 0.5f / log(maxlen)) - 1.0f;
  68. if (iter)
  69. {
  70. double r = fmod(f, (double)colors);
  71. if (r > (double)colors / 2) r = (double)colors - r;
  72. double g = fmod(f * 1.3 + 4.0f, (double)colors);
  73. if (g > (double)colors / 2) g = (double)colors - g;
  74. double b = fmod(f * 1.7 - 8.0f, (double)colors);
  75. if (b > (double)colors / 2) b = (double)colors - b;
  76. uint8_t red = 255 - r * (255.0f / (colors + 1));
  77. uint8_t green = 255 - g * (255.0f / (colors + 1));
  78. uint8_t blue = 255 - b * (255.0f / (colors + 1));
  79. m_pixels[j * m_size.x + i] = u8vec4(red, green, blue, 0);
  80. }
  81. else
  82. {
  83. m_pixels[j * m_size.x + i] = u8vec4(0, 0, 0, 0);
  84. }
  85. }
  86. }
  87. virtual void TickDraw(float deltams)
  88. {
  89. WorldEntity::TickDraw(deltams);
  90. static float const vertices[] =
  91. {
  92. 1.0f, 1.0f,
  93. -1.0f, 1.0f,
  94. -1.0f, -1.0f,
  95. -1.0f, -1.0f,
  96. 1.0f, -1.0f,
  97. 1.0f, 1.0f,
  98. };
  99. static float const texcoords[] =
  100. {
  101. 0.0f, 0.0f,
  102. 1.0f, 0.0f,
  103. 1.0f, 1.0f,
  104. 1.0f, 1.0f,
  105. 0.0f, 1.0f,
  106. 0.0f, 0.0f,
  107. };
  108. if (!m_ready)
  109. {
  110. glGenTextures(1, &m_texid);
  111. glBindTexture(GL_TEXTURE_2D, m_texid);
  112. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_size.x, m_size.y, 0,
  113. GL_RGBA, GL_UNSIGNED_BYTE, m_pixels);
  114. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  115. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  116. m_shader = Shader::Create(
  117. #if !defined __CELLOS_LV2__
  118. "#version 120\n"
  119. "attribute vec2 in_TexCoord;\n"
  120. "attribute vec2 in_Vertex;"
  121. "void main(void) {"
  122. " gl_Position = vec4(in_Vertex, 0.0, 1.0);"
  123. " gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
  124. "}",
  125. "#version 120\n"
  126. "uniform sampler2D in_Texture;\n"
  127. "void main(void) {"
  128. " gl_FragColor = texture2D(in_Texture, gl_TexCoord[0].xy);"
  129. "}"
  130. #else
  131. "void main(float4 in_Position : POSITION,"
  132. " float2 in_TexCoord : TEXCOORD0,"
  133. " out float4 out_Position : POSITION,"
  134. " out float2 out_TexCoord : TEXCOORD0)"
  135. "{"
  136. " out_TexCoord = in_TexCoord;"
  137. " out_Position = in_Position;"
  138. "}",
  139. "void main(float2 in_TexCoord : TEXCOORD0,"
  140. " uniform sampler2D tex,"
  141. " out float4 out_FragColor : COLOR)"
  142. "{"
  143. " out_FragColor = tex2D(tex, in_TexCoord);"
  144. "}"
  145. #endif
  146. );
  147. m_vertexattrib = m_shader->GetAttribLocation("in_Vertex");
  148. m_texattrib = m_shader->GetAttribLocation("in_TexCoord");
  149. m_ready = true;
  150. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  151. /* Method 1: store vertex buffer on the GPU memory */
  152. glGenBuffers(1, &m_vbo);
  153. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  154. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
  155. GL_STATIC_DRAW);
  156. glGenBuffers(1, &m_tbo);
  157. glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
  158. glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords,
  159. GL_STATIC_DRAW);
  160. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  161. /* Method 2: upload vertex information at each frame */
  162. #else
  163. #endif
  164. /* FIXME: this object never cleans up */
  165. }
  166. glEnable(GL_TEXTURE_2D);
  167. glBindTexture(GL_TEXTURE_2D, m_texid);
  168. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_size.x, m_size.y,
  169. #if !defined __CELLOS_LV2__
  170. GL_RGBA, GL_UNSIGNED_BYTE,
  171. #else
  172. /* The PS3 is big-endian */
  173. GL_RGBA, GL_UNSIGNED_INT_8_8_8_8,
  174. #endif
  175. m_pixels);
  176. m_shader->Bind();
  177. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  178. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  179. glEnableVertexAttribArray(m_vertexattrib);
  180. glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  181. glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
  182. glEnableVertexAttribArray(m_texattrib);
  183. glVertexAttribPointer(m_texattrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  184. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  185. /* Never used for now */
  186. //glEnableVertexAttribArray(m_vertexattrib);
  187. //glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  188. #else
  189. glEnableClientState(GL_VERTEX_ARRAY);
  190. glVertexPointer(2, GL_FLOAT, 0, vertices);
  191. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  192. glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
  193. #endif
  194. glDrawArrays(GL_TRIANGLES, 0, 6);
  195. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  196. glDisableVertexAttribArray(m_vertexattrib);
  197. glDisableVertexAttribArray(m_texattrib);
  198. glBindBuffer(GL_ARRAY_BUFFER, 0);
  199. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  200. /* Never used for now */
  201. //glDisableVertexAttribArray(m_vertexattrib);
  202. //glDisableVertexAttribArray(m_texattrib);
  203. #else
  204. glDisableClientState(GL_VERTEX_ARRAY);
  205. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  206. #endif
  207. }
  208. private:
  209. ivec2 m_size;
  210. u8vec4 *m_pixels;
  211. Shader *m_shader;
  212. GLuint m_texid;
  213. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  214. GLuint m_vbo, m_tbo;
  215. GLuint m_tco;
  216. #endif
  217. int m_vertexattrib, m_texattrib;
  218. float m_time;
  219. bool m_ready;
  220. };
  221. int main()
  222. {
  223. #if defined _WIN32
  224. _chdir("../..");
  225. #endif
  226. Application app("Tutorial 3: Fractal", ivec2(640, 480), 60.0f);
  227. new DebugFps(5, 5);
  228. new Fractal(ivec2(1280, 960));
  229. app.Run();
  230. return EXIT_SUCCESS;
  231. }