25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

528 lines
19 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. #ifdef __CELLOS_LV2__
  27. static GLint const INTERNAL_FORMAT = GL_ARGB_SCE;
  28. static GLenum const TEXTURE_FORMAT = GL_BGRA;
  29. static GLenum const TEXTURE_TYPE = GL_UNSIGNED_INT_8_8_8_8_REV;
  30. #else
  31. /* Seems efficient for little endian textures */
  32. static GLint const INTERNAL_FORMAT = GL_RGBA;
  33. static GLenum const TEXTURE_FORMAT = GL_BGRA;
  34. static GLenum const TEXTURE_TYPE = GL_UNSIGNED_INT_8_8_8_8_REV;
  35. #endif
  36. class Fractal : public WorldEntity
  37. {
  38. public:
  39. Fractal(ivec2 const &size)
  40. {
  41. /* Ensure size has even X and Y values */
  42. m_size = size;
  43. m_size.x = (m_size.x + 1) & ~1;
  44. m_size.y = (m_size.y + 1) & ~1;
  45. m_window_size = Video::GetSize();
  46. m_pixels = new u8vec4[m_size.x * m_size.y];
  47. m_tmppixels = new u8vec4[m_size.x / 2 * m_size.y / 2];
  48. m_frame = -1;
  49. for (int i = 0; i < 4; i++)
  50. {
  51. m_deltashift[i] = 0.0;
  52. m_deltascale[i] = 1.0;
  53. m_dirty[i] = 2;
  54. }
  55. m_center = -0.75;
  56. m_radius = 1.5;
  57. m_texture_radius = 0.5 * (m_size.x < m_size.y ? m_size.x : m_size.y);
  58. m_window_radius = 0.5 * (m_window_size.x < m_window_size.y ? m_window_size.x : m_window_size.y);
  59. m_pixel_delta = vec4(vec2(1.0, 1.0) / (vec2)m_size, m_size);
  60. m_ready = false;
  61. m_palette = new u8vec4[(MAX_ITERATIONS + 1) * PALETTE_STEP];
  62. for (int i = 0; i < (MAX_ITERATIONS + 1) * PALETTE_STEP; i++)
  63. {
  64. float f = i / (double)PALETTE_STEP;
  65. double r = 0.5 * sin(f * 0.27 - 2.5) + 0.5;
  66. double g = 0.5 * sin(f * 0.13 + 1.1) + 0.5;
  67. double b = 0.5 * sin(f * 0.21 + 0.4) + 0.5;
  68. uint8_t red = r * 255.0f;
  69. uint8_t green = g * 255.0f;
  70. uint8_t blue = b * 255.0f;
  71. m_palette[i] = u8vec4(blue, green, red, 0);
  72. }
  73. m_centertext = new Text(NULL, "gfx/font/ascii.png");
  74. m_centertext->SetPos(ivec3(5, m_window_size.y - 15, 1));
  75. Ticker::Ref(m_centertext);
  76. m_mousetext = new Text(NULL, "gfx/font/ascii.png");
  77. m_mousetext->SetPos(ivec3(5, m_window_size.y - 29, 1));
  78. Ticker::Ref(m_mousetext);
  79. m_zoomtext = new Text(NULL, "gfx/font/ascii.png");
  80. m_zoomtext->SetPos(ivec3(5, m_window_size.y - 43, 1));
  81. Ticker::Ref(m_zoomtext);
  82. position = ivec3(0, 0, 0);
  83. bbox[0] = position;
  84. bbox[1] = ivec3(m_window_size, 0);
  85. Input::TrackMouse(this);
  86. }
  87. ~Fractal()
  88. {
  89. Input::UntrackMouse(this);
  90. Ticker::Unref(m_centertext);
  91. Ticker::Unref(m_mousetext);
  92. Ticker::Unref(m_zoomtext);
  93. delete m_pixels;
  94. delete m_tmppixels;
  95. delete m_palette;
  96. }
  97. inline f64cmplx TexelToWorldOffset(ivec2 texel)
  98. {
  99. f64cmplx tmp = f64cmplx(0.5 + texel.x - m_size.x / 2,
  100. 0.5 + m_size.y / 2 - texel.y);
  101. return tmp * (m_radius / m_texture_radius);
  102. }
  103. inline f64cmplx ScreenToWorldOffset(ivec2 pixel)
  104. {
  105. /* No 0.5 offset here, because we want to be able to position the
  106. * mouse at (0,0) exactly. */
  107. f64cmplx tmp = f64cmplx(pixel.x - m_window_size.x / 2,
  108. m_window_size.y / 2 - pixel.y);
  109. return tmp * (m_radius / m_window_radius);
  110. }
  111. virtual void TickGame(float deltams)
  112. {
  113. WorldEntity::TickGame(deltams);
  114. m_frame = (m_frame + 1) % 4;
  115. f64cmplx worldmouse = m_center + ScreenToWorldOffset(mousepos);
  116. ivec3 buttons = Input::GetMouseButtons();
  117. #ifdef __CELLOS_LV2__
  118. if (true)
  119. #else
  120. if ((buttons[0] || buttons[2]) && mousepos.x != -1)
  121. #endif
  122. {
  123. f64cmplx oldcenter = m_center;
  124. double oldradius = m_radius;
  125. #ifdef __CELLOS_LV2__
  126. m_radius *= pow(2.0, -deltams * 0.00005);
  127. m_center = f64cmplx(0.001643721971153, 0.822467633298876);
  128. #else
  129. double zoom = pow(2.0, (buttons[0] ? -deltams : deltams) * 0.0025);
  130. if (m_radius * zoom > 8.0)
  131. zoom = 8.0 / m_radius;
  132. else if (m_radius * zoom < 1e-14)
  133. zoom = 1e-14 / m_radius;
  134. m_radius *= zoom;
  135. m_center = (m_center - worldmouse) * zoom + worldmouse;
  136. worldmouse = m_center + ScreenToWorldOffset(mousepos);
  137. #endif
  138. /* Store the transformation properties to go from m_frame-1
  139. * to m_frame. */
  140. m_deltashift[m_frame] = (oldcenter - m_center) / m_radius;
  141. m_deltascale[m_frame] = oldradius / m_radius;
  142. m_dirty[0] = m_dirty[1] = m_dirty[2] = m_dirty[3] = 2;
  143. }
  144. else
  145. {
  146. /* If settings didn't change, set transformation from previous
  147. * frame to identity. */
  148. m_deltashift[m_frame] = 0.0;
  149. m_deltascale[m_frame] = 1.0;
  150. }
  151. if (buttons[1])
  152. m_dirty[0] = m_dirty[1] = m_dirty[2] = m_dirty[3] = 2;
  153. char buf[128];
  154. sprintf(buf, "center: %+16.14f%+16.14fi", m_center.x, m_center.y);
  155. m_centertext->SetText(buf);
  156. sprintf(buf, " mouse: %+16.14f%+16.14fi", worldmouse.x, worldmouse.y);
  157. m_mousetext->SetText(buf);
  158. sprintf(buf, " zoom: %g", 1.0 / m_radius);
  159. m_zoomtext->SetText(buf);
  160. u8vec4 *m_pixelstart = m_pixels + m_size.x * m_size.y / 4 * m_frame;
  161. if (m_dirty[m_frame])
  162. {
  163. double const maxsqlen = 1024;
  164. double const k1 = 1.0 / (1 << 10) / log2(maxsqlen);
  165. m_dirty[m_frame]--;
  166. for (int j = ((m_frame + 1) % 4) / 2; j < m_size.y; j += 2)
  167. for (int i = m_frame % 2; i < m_size.x; i += 2)
  168. {
  169. f64cmplx z0 = m_center + TexelToWorldOffset(ivec2(i, j));
  170. f64cmplx r0 = z0;
  171. //f64cmplx r0(0.28693186889504513, 0.014286693904085048);
  172. //f64cmplx r0(0.001643721971153, 0.822467633298876);
  173. //f64cmplx r0(-1.207205434596, 0.315432814901);
  174. //f64cmplx r0(-0.79192956889854, -0.14632423080102);
  175. //f64cmplx r0(0.3245046418497685, 0.04855101129280834);
  176. f64cmplx z;
  177. int iter = MAX_ITERATIONS;
  178. for (z = z0; iter && z.sqlen() < maxsqlen; z = z * z + r0)
  179. --iter;
  180. if (iter)
  181. {
  182. double f = iter;
  183. double n = z.sqlen();
  184. if (n > maxsqlen * maxsqlen)
  185. n = maxsqlen * maxsqlen;
  186. /* Approximate log(sqrt(n))/log(sqrt(maxsqlen)) */
  187. union { double n; uint64_t x; } u = { n };
  188. double k = (u.x >> 42) - (((1 << 10) - 1) << 10);
  189. k *= k1;
  190. /* Approximate log2(k) in [1,2]. */
  191. f += (- 0.344847817623168308695977510213252644185 * k
  192. + 2.024664188044341212602376988171727038739) * k
  193. - 1.674876738008591047163498125918330313237;
  194. *m_pixelstart++ = m_palette[(int)(f * PALETTE_STEP)];
  195. }
  196. else
  197. {
  198. *m_pixelstart++ = u8vec4(0, 0, 0, 0);
  199. }
  200. }
  201. }
  202. }
  203. virtual void TickDraw(float deltams)
  204. {
  205. WorldEntity::TickDraw(deltams);
  206. static float const vertices[] =
  207. {
  208. 1.0f, 1.0f,
  209. -1.0f, 1.0f,
  210. -1.0f, -1.0f,
  211. -1.0f, -1.0f,
  212. 1.0f, -1.0f,
  213. 1.0f, 1.0f,
  214. };
  215. static float const texcoords[] =
  216. {
  217. 1.0f, 1.0f,
  218. 0.0f, 1.0f,
  219. 0.0f, 0.0f,
  220. 0.0f, 0.0f,
  221. 1.0f, 0.0f,
  222. 1.0f, 1.0f,
  223. };
  224. if (!m_ready)
  225. {
  226. /* Create a texture of half the width and twice the height
  227. * so that we can upload four different subimages each frame. */
  228. glGenTextures(1, &m_texid);
  229. glBindTexture(GL_TEXTURE_2D, m_texid);
  230. glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT,
  231. m_size.x / 2, m_size.y * 2, 0,
  232. TEXTURE_FORMAT, TEXTURE_TYPE, m_pixels);
  233. #if !defined __CELLOS_LV2__
  234. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_ALLOCATION_HINT_SCE,
  235. GL_TEXTURE_TILED_GPU_SCE);
  236. #endif
  237. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  238. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  239. m_shader = Shader::Create(
  240. #if !defined __CELLOS_LV2__
  241. "#version 120\n"
  242. "attribute vec2 in_TexCoord;\n"
  243. "attribute vec2 in_Vertex;"
  244. "void main(void) {"
  245. " gl_Position = vec4(in_Vertex, 0.0, 1.0);"
  246. " gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
  247. "}",
  248. "#version 120\n"
  249. ""
  250. "uniform vec4 in_PixelDelta;"
  251. "uniform sampler2D in_Texture;"
  252. ""
  253. /* Get the coordinate of the nearest point in slice 0 in xy,
  254. * and the squared distance to that point in z.
  255. * p is in normalised [0,1] texture coordinates.
  256. * return value has the 0.25 Y scaling. */
  257. "vec3 nearest0(vec2 p) {"
  258. " vec2 q = p + 0.5 * in_PixelDelta.xy;"
  259. " q -= mod(q, 2.0 * in_PixelDelta.xy);"
  260. " q += 0.5 * in_PixelDelta.xy;"
  261. " return vec3(q * vec2(1.0, 0.25),"
  262. " length(q - p));"
  263. "}"
  264. ""
  265. "vec3 nearest1(vec2 p) {"
  266. " vec2 q = p - 0.5 * in_PixelDelta.xy;"
  267. " q -= mod(q, 2.0 * in_PixelDelta.xy);"
  268. " q += 1.5 * in_PixelDelta.xy;"
  269. " return vec3(q * vec2(1.0, 0.25) + vec2(0.0, 0.25),"
  270. " length(q - p));"
  271. "}"
  272. ""
  273. "vec3 nearest2(vec2 p) {"
  274. " vec2 q = p + vec2(0.5, -0.5) * in_PixelDelta.xy;"
  275. " q -= mod(q, 2.0 * in_PixelDelta.xy);"
  276. " q += vec2(0.5, 1.5) * in_PixelDelta.xy;"
  277. " return vec3(q * vec2(1.0, 0.25) + vec2(0.0, 0.50),"
  278. " length(q - p));"
  279. "}"
  280. ""
  281. "vec3 nearest3(vec2 p) {"
  282. " vec2 q = p + vec2(-0.5, 0.5) * in_PixelDelta.xy;"
  283. " q -= mod(q, 2.0 * in_PixelDelta.xy);"
  284. " q += vec2(1.5, 0.5) * in_PixelDelta.xy;"
  285. " return vec3(q * vec2(1.0, 0.25) + vec2(0.0, 0.75),"
  286. " length(q - p));"
  287. "}"
  288. ""
  289. "void main(void) {"
  290. " vec2 coord = gl_TexCoord[0].xy;"
  291. /* Slightly shift our pixel so that it does not lie at
  292. * an exact texel boundary. This would lead to visual
  293. * artifacts. */
  294. " coord -= 0.1 * in_PixelDelta.xy;"
  295. /* Get a pixel from each slice */
  296. " vec4 p0 = texture2D(in_Texture, nearest0(coord).xy);"
  297. " vec4 p1 = texture2D(in_Texture, nearest1(coord).xy);"
  298. " vec4 p2 = texture2D(in_Texture, nearest2(coord).xy);"
  299. " vec4 p3 = texture2D(in_Texture, nearest3(coord).xy);"
  300. " gl_FragColor = 0.25 * (p0 + p1 + p2 + p3);"
  301. "}"
  302. #else
  303. "void main(float4 in_Position : POSITION,"
  304. " float2 in_TexCoord : TEXCOORD0,"
  305. " out float4 out_Position : POSITION,"
  306. " out float2 out_TexCoord : TEXCOORD0)"
  307. "{"
  308. " out_TexCoord = in_TexCoord;"
  309. " out_Position = in_Position;"
  310. "}",
  311. "float3 nearest0(float2 p, float4 in_PixelDelta) {"
  312. " float2 q = p + 0.5 * in_PixelDelta.xy;"
  313. " q -= fmod(q, 2.0 * in_PixelDelta.xy);"
  314. " q += 0.5 * in_PixelDelta.xy;"
  315. " return float3(q * float2(1.0, 0.25),"
  316. " length(q - p));"
  317. "}"
  318. ""
  319. "float3 nearest1(float2 p, float4 in_PixelDelta) {"
  320. " float2 q = p - 0.5 * in_PixelDelta.xy;"
  321. " q -= fmod(q, 2.0 * in_PixelDelta.xy);"
  322. " q += 1.5 * in_PixelDelta.xy;"
  323. " return float3(q * float2(1.0, 0.25) + float2(0.0, 0.25),"
  324. " length(q - p));"
  325. "}"
  326. ""
  327. "float3 nearest2(float2 p, float4 in_PixelDelta) {"
  328. " float2 q = p + float2(0.5, -0.5) * in_PixelDelta.xy;"
  329. " q -= fmod(q, 2.0 * in_PixelDelta.xy);"
  330. " q += float2(0.5, 1.5) * in_PixelDelta.xy;"
  331. " return float3(q * float2(1.0, 0.25) + float2(0.0, 0.50),"
  332. " length(q - p));"
  333. "}"
  334. ""
  335. "float3 nearest3(float2 p, float4 in_PixelDelta) {"
  336. " float2 q = p + float2(-0.5, 0.5) * in_PixelDelta.xy;"
  337. " q -= fmod(q, 2.0 * in_PixelDelta.xy);"
  338. " q += float2(1.5, 0.5) * in_PixelDelta.xy;"
  339. " return float3(q * float2(1.0, 0.25) + float2(0.0, 0.75),"
  340. " length(q - p));"
  341. "}"
  342. ""
  343. "void main(float2 in_TexCoord : TEXCOORD0,"
  344. " uniform float4 in_PixelDelta,"
  345. " uniform sampler2D in_Texture,"
  346. " out float4 out_FragColor : COLOR)"
  347. "{"
  348. " float2 coord = in_TexCoord.xy;"
  349. " coord -= 0.1 * in_PixelDelta.xy;"
  350. " float4 p0 = tex2D(in_Texture, nearest0(coord, in_PixelDelta).xy);"
  351. " float4 p1 = tex2D(in_Texture, nearest1(coord, in_PixelDelta).xy);"
  352. " float4 p2 = tex2D(in_Texture, nearest2(coord, in_PixelDelta).xy);"
  353. " float4 p3 = tex2D(in_Texture, nearest3(coord, in_PixelDelta).xy);"
  354. " out_FragColor = 0.25 * (p0 + p1 + p2 + p3);"
  355. "}"
  356. #endif
  357. );
  358. m_vertexattrib = m_shader->GetAttribLocation("in_Vertex");
  359. m_texattrib = m_shader->GetAttribLocation("in_TexCoord");
  360. m_pixeluni = m_shader->GetUniformLocation("in_PixelDelta");
  361. m_ready = true;
  362. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  363. /* Method 1: store vertex buffer on the GPU memory */
  364. glGenBuffers(1, &m_vbo);
  365. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  366. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
  367. GL_STATIC_DRAW);
  368. glGenBuffers(1, &m_tbo);
  369. glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
  370. glBufferData(GL_ARRAY_BUFFER, sizeof(texcoords), texcoords,
  371. GL_STATIC_DRAW);
  372. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  373. /* Method 2: upload vertex information at each frame */
  374. #else
  375. #endif
  376. /* FIXME: this object never cleans up */
  377. }
  378. glEnable(GL_TEXTURE_2D);
  379. glBindTexture(GL_TEXTURE_2D, m_texid);
  380. if (m_dirty[m_frame])
  381. {
  382. m_dirty[m_frame]--;
  383. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, m_frame * m_size.y / 2,
  384. m_size.x / 2, m_size.y / 2,
  385. TEXTURE_FORMAT, TEXTURE_TYPE,
  386. m_pixels + m_size.x * m_size.y / 4 * m_frame);
  387. }
  388. /* If other frames are dirty, upload fake data for now */
  389. if (0) for (int i = 0; i < 4; i++)
  390. {
  391. if (m_dirty[i])
  392. {
  393. glTexSubImage2D(GL_TEXTURE_2D, 0, 0, i * m_size.y / 2,
  394. m_size.x / 2, m_size.y / 2,
  395. TEXTURE_FORMAT, TEXTURE_TYPE,
  396. m_pixels + m_size.x * m_size.y / 4 * m_frame);
  397. }
  398. }
  399. m_shader->Bind();
  400. m_shader->SetUniform(m_pixeluni, m_pixel_delta);
  401. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  402. glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
  403. glEnableVertexAttribArray(m_vertexattrib);
  404. glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  405. glBindBuffer(GL_ARRAY_BUFFER, m_tbo);
  406. glEnableVertexAttribArray(m_texattrib);
  407. glVertexAttribPointer(m_texattrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
  408. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  409. /* Never used for now */
  410. //glEnableVertexAttribArray(m_vertexattrib);
  411. //glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, vertices);
  412. #else
  413. glEnableClientState(GL_VERTEX_ARRAY);
  414. glVertexPointer(2, GL_FLOAT, 0, vertices);
  415. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  416. glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
  417. #endif
  418. glDrawArrays(GL_TRIANGLES, 0, 6);
  419. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  420. glDisableVertexAttribArray(m_vertexattrib);
  421. glDisableVertexAttribArray(m_texattrib);
  422. glBindBuffer(GL_ARRAY_BUFFER, 0);
  423. #elif !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  424. /* Never used for now */
  425. //glDisableVertexAttribArray(m_vertexattrib);
  426. //glDisableVertexAttribArray(m_texattrib);
  427. #else
  428. glDisableClientState(GL_VERTEX_ARRAY);
  429. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  430. #endif
  431. }
  432. private:
  433. static int const MAX_ITERATIONS = 170;
  434. static int const PALETTE_STEP = 32;
  435. ivec2 m_size, m_window_size;
  436. u8vec4 *m_pixels, *m_tmppixels, *m_palette;
  437. Shader *m_shader;
  438. GLuint m_texid;
  439. #if !defined __CELLOS_LV2__ && !defined __ANDROID__ && !defined __APPLE__
  440. GLuint m_vbo, m_tbo;
  441. GLuint m_tco;
  442. #endif
  443. int m_vertexattrib, m_texattrib, m_pixeluni;
  444. int m_frame, m_dirty[4];
  445. bool m_ready;
  446. f64cmplx m_center;
  447. double m_radius, m_texture_radius, m_window_radius;
  448. vec4 m_pixel_delta;
  449. f64cmplx m_deltashift[4];
  450. double m_deltascale[4];
  451. /* Debug information */
  452. Text *m_centertext, *m_mousetext, *m_zoomtext;
  453. };
  454. int main()
  455. {
  456. #if defined _WIN32
  457. _chdir("../..");
  458. #endif
  459. Application app("Tutorial 3: Fractal", ivec2(640, 480), 60.0f);
  460. new DebugFps(5, 5);
  461. new Fractal(ivec2(640, 480));
  462. //new DebugRecord("fractalol.ogm", 60.0f);
  463. app.Run();
  464. return EXIT_SUCCESS;
  465. }