選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

598 行
20 KiB

  1. //
  2. // Lol Engine — Fractal tutorial
  3. //
  4. // Copyright © 2011—2016 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <cstring>
  16. #include <cstdio>
  17. #include <lol/engine.h>
  18. #include "loldebug.h"
  19. #define USE_REAL 0
  20. using namespace lol;
  21. LOLFX_RESOURCE_DECLARE(11_fractal);
  22. class Fractal : public WorldEntity
  23. {
  24. public:
  25. Fractal(ivec2 const &size)
  26. : m_julia(false)
  27. {
  28. /* Ensure texture size is a multiple of 16 for better aligned
  29. * data access. Store the dimensions of a texel for our shader,
  30. * as well as the half-size of the screen. */
  31. m_size = size;
  32. m_size.x = (m_size.x + 15) & ~15;
  33. m_size.y = (m_size.y + 15) & ~15;
  34. m_texel_settings = vec4(1.0, 1.0, 2.0, 2.0) / (vec4)m_size.xyxy;
  35. m_screen_settings = vec4(1.0, 1.0, 0.5, 0.5) * (vec4)m_size.xyxy;
  36. m_controller = new Controller("Default");
  37. m_profile << InputProfile::MouseKey(0, "Left")
  38. << InputProfile::MouseKey(1, "Right")
  39. << InputProfile::MouseKey(2, "Middle")
  40. << InputProfile::Keyboard(3, "Space");
  41. m_controller->Init(m_profile);
  42. m_mouse = InputDevice::GetMouse();
  43. /* Window size decides the world aspect ratio. For instance, 640×480
  44. * will be mapped to (-0.66,-0.5) - (0.66,0.5). */
  45. m_window_size = Video::GetSize();
  46. if (m_window_size.y < m_window_size.x)
  47. m_window2world = 0.5 / m_window_size.y;
  48. else
  49. m_window2world = 0.5 / m_window_size.x;
  50. m_texel2world = (dvec2)m_window_size / (dvec2)m_size * m_window2world;
  51. m_oldmouse = ivec2(0, 0);
  52. m_pixels.resize(m_size.x * m_size.y);
  53. m_frame = -1;
  54. m_slices = 4;
  55. for (int i = 0; i < 4; i++)
  56. {
  57. m_deltashift[i] = real("0");
  58. m_deltascale[i] = real("1");
  59. m_dirty[i] = 2;
  60. }
  61. m_view.center = rcmplx(-0.75, 0.0);
  62. m_zoom_speed = 0.0;
  63. m_view.translate = rcmplx(0.0, 0.0);
  64. m_view.radius = 5.0;
  65. m_ready = false;
  66. m_drag = false;
  67. for (int i = 0; i < (MAX_ITERATIONS + 1) * PALETTE_STEP; i++)
  68. {
  69. double f = (double)i / PALETTE_STEP;
  70. vec3 hsv(lol::fmod(i * 0.001f, 1.f),
  71. 0.3 * lol::sin(f * 0.27 + 2.0) + 0.3,
  72. 0.3 * lol::sin(f * 0.21 - 2.6) + 0.6);
  73. vec3 rgb = Color::HSVToRGB(hsv);
  74. if (f < 7.0)
  75. {
  76. rgb *= f < 1.0 ? 0.0 : (f - 1.0) / 6.0;
  77. }
  78. uint8_t red = (uint8_t)(rgb.r * 256);
  79. uint8_t green = (uint8_t)(rgb.g * 256);
  80. uint8_t blue = (uint8_t)(rgb.b * 256);
  81. m_palette.push(u8vec4(blue, green, red, 255));
  82. }
  83. m_zoomtext = new Text("", "data/font/ascii.png");
  84. m_zoomtext->SetPos(vec3(5, (float)m_window_size.y - 15, 1));
  85. Ticker::Ref(m_zoomtext);
  86. m_centertext = new Text("", "data/font/ascii.png");
  87. m_centertext->SetPos(vec3(5, (float)m_window_size.y - 29, 1));
  88. Ticker::Ref(m_centertext);
  89. m_mousetext = new Text("", "data/font/ascii.png");
  90. m_mousetext->SetPos(vec3(5, (float)m_window_size.y - 43, 1));
  91. Ticker::Ref(m_mousetext);
  92. m_position = vec3::zero;
  93. m_aabb.aa = m_position;
  94. m_aabb.bb = vec3((vec2)m_window_size, 0);
  95. #if LOL_FEATURE_THREADS
  96. /* Spawn worker threads and wait for their readiness. */
  97. for (int i = 0; i < MAX_THREADS; i++)
  98. m_threads[i] = new thread(std::bind(&Fractal::DoWorkHelper, this, std::placeholders::_1));
  99. for (int i = 0; i < MAX_THREADS; i++)
  100. m_spawnqueue.pop();
  101. #endif
  102. }
  103. ~Fractal()
  104. {
  105. #if LOL_FEATURE_THREADS
  106. /* Signal worker threads for completion and wait for
  107. * them to quit. */
  108. for (int i = 0; i < MAX_THREADS; i++)
  109. m_jobqueue.push(-1);
  110. for (int i = 0; i < MAX_THREADS; i++)
  111. m_donequeue.pop();
  112. #endif
  113. Ticker::Unref(m_centertext);
  114. Ticker::Unref(m_mousetext);
  115. Ticker::Unref(m_zoomtext);
  116. }
  117. inline f128cmplx TexelToWorldOffset(vec2 texel)
  118. {
  119. double dx = (0.5 + texel.x - m_size.x / 2) * m_texel2world.x;
  120. double dy = (0.5 + m_size.y / 2 - texel.y) * m_texel2world.y;
  121. return m_view.radius * f128cmplx(dx, dy);
  122. }
  123. inline f128cmplx ScreenToWorldOffset(vec2 pixel)
  124. {
  125. /* No 0.5 offset here, because we want to be able to position the
  126. * mouse at (0,0) exactly. */
  127. double dx = pixel.x - m_window_size.x / 2;
  128. double dy = m_window_size.y / 2 - pixel.y;
  129. return m_view.radius * m_window2world * f128cmplx(dx, dy);
  130. }
  131. virtual void tick_game(float seconds)
  132. {
  133. WorldEntity::tick_game(seconds);
  134. ivec2 mousepos = m_mouse->GetCursorPixel(0);
  135. int prev_frame = (m_frame + 4) % 4;
  136. m_frame = (m_frame + 1) % 4;
  137. if (m_controller->WasKeyPressedThisFrame(3))
  138. {
  139. m_julia = !m_julia;
  140. if (m_julia)
  141. {
  142. m_saved_view = m_view;
  143. m_view.r0 = m_view.center + rcmplx(ScreenToWorldOffset((vec2)mousepos));
  144. }
  145. else
  146. {
  147. m_view = m_saved_view;
  148. }
  149. for (auto & flag : m_dirty)
  150. flag = 2;
  151. }
  152. rcmplx worldmouse = m_view.center + rcmplx(ScreenToWorldOffset((vec2)mousepos));
  153. if (m_controller->IsKeyPressed(2))
  154. {
  155. if (!m_drag)
  156. {
  157. m_oldmouse = mousepos;
  158. m_drag = true;
  159. }
  160. m_view.translate = rcmplx(ScreenToWorldOffset((vec2)m_oldmouse)
  161. - ScreenToWorldOffset((vec2)mousepos));
  162. /* XXX: the purpose of this hack is to avoid translating by
  163. * an exact number of pixels. If this were to happen, the step()
  164. * optimisation for i915 cards in our shader would behave
  165. * incorrectly because a quarter of the pixels in the image
  166. * would have tied rankings in the distance calculation. */
  167. m_view.translate *= real(1023.0 / 1024.0);
  168. m_oldmouse = mousepos;
  169. }
  170. else
  171. {
  172. m_drag = false;
  173. if (m_view.translate != rcmplx(0.0, 0.0))
  174. {
  175. m_view.translate *= real(std::pow(2.0, -seconds * 5.0));
  176. if ((double)norm(m_view.translate) < m_view.radius * 1e-4)
  177. m_view.translate = rcmplx(0.0, 0.0);
  178. }
  179. }
  180. bool hold_right = m_controller->IsKeyPressed(0);
  181. bool hold_left = m_controller->IsKeyPressed(1);
  182. if ((hold_right || hold_left) && mousepos.x != -1)
  183. {
  184. double zoom = hold_right ? -0.5 : 0.5;
  185. m_zoom_speed += zoom * seconds;
  186. if (m_zoom_speed / zoom > 5e-3f)
  187. m_zoom_speed = zoom * 5e-3f;
  188. }
  189. else if (m_zoom_speed)
  190. {
  191. m_zoom_speed *= std::pow(2.0, -seconds * 5.0);
  192. if (lol::abs(m_zoom_speed) < 1e-5 || m_drag)
  193. m_zoom_speed = 0.0;
  194. }
  195. if (m_zoom_speed || m_view.translate != rcmplx(0.0, 0.0))
  196. {
  197. rcmplx oldcenter = m_view.center;
  198. double oldradius = m_view.radius;
  199. double zoom = std::pow(2.0, seconds * 1e3f * m_zoom_speed);
  200. if (m_view.radius * zoom > 8.0)
  201. {
  202. m_zoom_speed *= -1.0;
  203. zoom = 8.0 / m_view.radius;
  204. }
  205. else if (m_view.radius * zoom < MAX_ZOOM)
  206. {
  207. m_zoom_speed *= -1.0;
  208. zoom = MAX_ZOOM / m_view.radius;
  209. }
  210. m_view.radius *= zoom;
  211. m_view.center += m_view.translate;
  212. m_view.center = (m_view.center - worldmouse) * real(zoom) + worldmouse;
  213. worldmouse = m_view.center
  214. + rcmplx(ScreenToWorldOffset((vec2)mousepos));
  215. /* Store the transformation properties to go from m_frame - 1
  216. * to m_frame. */
  217. m_deltashift[prev_frame] = (m_view.center - oldcenter) / real(oldradius);
  218. m_deltashift[prev_frame].x /= m_size.x * m_texel2world.x;
  219. m_deltashift[prev_frame].y /= m_size.y * m_texel2world.y;
  220. m_deltascale[prev_frame] = m_view.radius / oldradius;
  221. for (auto & flag : m_dirty)
  222. flag = 2;
  223. }
  224. else
  225. {
  226. /* If settings didn't change, set transformation from previous
  227. * frame to identity. */
  228. m_deltashift[prev_frame] = real::R_0();
  229. m_deltascale[prev_frame] = real::R_1();
  230. }
  231. /* Transformation from current frame to current frame is always
  232. * identity. */
  233. m_zoom_settings[m_frame][0] = 0.0f;
  234. m_zoom_settings[m_frame][1] = 0.0f;
  235. m_zoom_settings[m_frame][2] = 1.0f;
  236. /* Compute transformation from other frames to current frame */
  237. for (int i = 0; i < 3; i++)
  238. {
  239. int prev_index = (m_frame + 4 - i) % 4;
  240. int cur_index = (m_frame + 3 - i) % 4;
  241. m_zoom_settings[cur_index][0] = (real)m_zoom_settings[prev_index][0] * m_deltascale[cur_index] + m_deltashift[cur_index].x;
  242. m_zoom_settings[cur_index][1] = (real)m_zoom_settings[prev_index][1] * m_deltascale[cur_index] + m_deltashift[cur_index].y;
  243. m_zoom_settings[cur_index][2] = (real)m_zoom_settings[prev_index][2] * m_deltascale[cur_index];
  244. }
  245. /* Precompute texture offset change instead of doing it in GLSL */
  246. for (int i = 0; i < 4; i++)
  247. {
  248. m_zoom_settings[i][0] += 0.5f * (1.0f - m_zoom_settings[i][2]);
  249. m_zoom_settings[i][1] -= 0.5f * (1.0f - m_zoom_settings[i][2]);
  250. }
  251. char buf[256];
  252. std::sprintf(buf, "center: ");
  253. m_view.center.x.sprintf(buf + strlen(buf), 30);
  254. std::sprintf(buf + strlen(buf), " ");
  255. m_view.center.y.sprintf(buf + strlen(buf), 30);
  256. m_centertext->SetText(buf);
  257. std::sprintf(buf, " mouse: ");
  258. worldmouse.x.sprintf(buf + strlen(buf), 30);
  259. std::sprintf(buf + strlen(buf), " ");
  260. worldmouse.y.sprintf(buf + strlen(buf), 30);
  261. m_mousetext->SetText(buf);
  262. std::sprintf(buf, "[%s] zoom: %g", m_julia ? "Julia" : "Mandelbrot", 1.0 / m_view.radius);
  263. m_zoomtext->SetText(buf);
  264. if (m_dirty[m_frame])
  265. {
  266. m_dirty[m_frame]--;
  267. for (int i = 0; i < m_size.y; i += MAX_LINES * 2)
  268. {
  269. #if LOL_FEATURE_THREADS
  270. m_jobqueue.push(i);
  271. #else
  272. DoWork(i);
  273. #endif
  274. }
  275. }
  276. }
  277. #if LOL_FEATURE_THREADS
  278. void DoWorkHelper(thread *)
  279. {
  280. m_spawnqueue.push(0);
  281. for ( ; ; )
  282. {
  283. int line = m_jobqueue.pop();
  284. if (line == -1)
  285. break;
  286. DoWork(line);
  287. m_donequeue.push(0);
  288. }
  289. m_donequeue.push(0);
  290. };
  291. #endif
  292. void DoWork(int line)
  293. {
  294. double const maxsqlen = 1024;
  295. double const k1 = 1.0 / (1 << 10) / (std::log(maxsqlen) / std::log(2.0));
  296. int jmin = ((m_frame + 1) % 4) / 2 + line;
  297. int jmax = jmin + MAX_LINES * 2;
  298. if (jmax > m_size.y)
  299. jmax = m_size.y;
  300. u8vec4 *pixelstart = &m_pixels[0]
  301. + m_size.x * (m_size.y / 4 * m_frame + line / 4);
  302. #if USE_REAL
  303. rcmplx c = (rcmplx)m_view.center;
  304. rcmplx jr0 = (rcmplx)m_view.r0;
  305. #else
  306. f128cmplx c = (f128cmplx)m_view.center;
  307. f128cmplx jr0 = (f128cmplx)m_view.r0;
  308. #endif
  309. for (int j = jmin; j < jmax; j += 2)
  310. for (int i = m_frame % 2; i < m_size.x; i += 2)
  311. {
  312. #if USE_REAL
  313. real xr, yr, x0, y0, x1, y1, x2, y2, x3, y3;
  314. real sqx0, sqy0, sqx1, sqy1, sqx2, sqy2, sqx3, sqy3;
  315. rcmplx z0 = c + rcmplx(TexelToWorldOffset(vec2(ivec2(i, j))));
  316. rcmplx r0 = m_julia ? jr0 : z0;
  317. #else
  318. ldouble xr, yr, x0, y0, x1, y1, x2, y2, x3, y3;
  319. ldouble sqx0, sqy0, sqx1, sqy1, sqx2, sqy2, sqx3, sqy3;
  320. f128cmplx z0 = c + TexelToWorldOffset(vec2(ivec2(i, j)));
  321. f128cmplx r0 = m_julia ? jr0 : z0;
  322. #endif
  323. x0 = z0.x; y0 = z0.y;
  324. xr = r0.x; yr = r0.y;
  325. sqx0 = x0 * x0; sqy0 = y0 * y0;
  326. int iter = MAX_ITERATIONS - 4;
  327. for (;;)
  328. {
  329. /* Unroll the loop: tests are more expensive to do at each
  330. * iteration than the few extra multiplications, at least
  331. * with floats/doubles. */
  332. x1 = sqx0 - sqy0 + xr; y1 = x0 * y0 + x0 * y0 + yr;
  333. sqx1 = x1 * x1; sqy1 = y1 * y1;
  334. x2 = sqx1 - sqy1 + xr; y2 = x1 * y1 + x1 * y1 + yr;
  335. sqx2 = x2 * x2; sqy2 = y2 * y2;
  336. x3 = sqx2 - sqy2 + xr; y3 = x2 * y2 + x2 * y2 + yr;
  337. sqx3 = x3 * x3; sqy3 = y3 * y3;
  338. x0 = sqx3 - sqy3 + xr; y0 = x3 * y3 + x3 * y3 + yr;
  339. sqx0 = x0 * x0; sqy0 = y0 * y0;
  340. if ((double)sqx0 + (double)sqy0 >= maxsqlen)
  341. break;
  342. iter -= 4;
  343. if (iter < 4)
  344. break;
  345. }
  346. if (iter)
  347. {
  348. double n = (double)sqx0 + (double)sqy0;
  349. if ((double)sqx1 + (double)sqy1 >= maxsqlen)
  350. {
  351. iter += 3; n = (double)sqx1 + (double)sqy1;
  352. }
  353. else if ((double)sqx2 + (double)sqy2 >= maxsqlen)
  354. {
  355. iter += 2; n = (double)sqx2 + (double)sqy2;
  356. }
  357. else if ((double)sqx3 + (double)sqy3 >= maxsqlen)
  358. {
  359. iter += 1; n = (double)sqx3 + (double)sqy3;
  360. }
  361. if (n > maxsqlen * maxsqlen)
  362. n = maxsqlen * maxsqlen;
  363. /* Approximate log(sqrt(n))/log(sqrt(maxsqlen)) */
  364. double f = iter;
  365. union { double n; uint64_t x; } u = { (double)n };
  366. double k = (double)(u.x >> 42) - (((1 << 10) - 1) << 10);
  367. k *= k1;
  368. /* Approximate log2(k) in [1,2]. */
  369. f += (- 0.344847817623168308695977510213252644185 * k
  370. + 2.024664188044341212602376988171727038739) * k
  371. - 1.674876738008591047163498125918330313237;
  372. *pixelstart++ = m_palette[(int)(f * PALETTE_STEP)];
  373. }
  374. else
  375. {
  376. *pixelstart++ = u8vec4(0, 0, 0, 255);
  377. }
  378. }
  379. }
  380. virtual void tick_draw(float seconds, Scene &scene)
  381. {
  382. WorldEntity::tick_draw(seconds, scene);
  383. static float const vertices[] =
  384. {
  385. 1.0f, 1.0f,
  386. -1.0f, 1.0f,
  387. -1.0f, -1.0f,
  388. -1.0f, -1.0f,
  389. 1.0f, -1.0f,
  390. 1.0f, 1.0f,
  391. };
  392. static float const texcoords[] =
  393. {
  394. 1.0f, 1.0f,
  395. 0.0f, 1.0f,
  396. 0.0f, 0.0f,
  397. 0.0f, 0.0f,
  398. 1.0f, 0.0f,
  399. 1.0f, 1.0f,
  400. };
  401. if (!m_ready)
  402. {
  403. /* Create a texture of half the width and twice the height
  404. * so that we can upload four different subimages each frame. */
  405. m_texture = new Texture(ivec2(m_size.x / 2, m_size.y * 2),
  406. PixelFormat::RGBA_8);
  407. /* Ensure the texture data is complete at least once, otherwise
  408. * uploading subimages will not work. */
  409. m_texture->SetData(&m_pixels[0]);
  410. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(11_fractal));
  411. m_vertexattrib = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  412. m_texattrib = m_shader->GetAttribLocation(VertexUsage::TexCoord, 0);
  413. m_texuni = m_shader->GetUniformLocation("u_texture");
  414. m_texeluni = m_shader->GetUniformLocation("u_texel_size");
  415. m_screenuni = m_shader->GetUniformLocation("u_screen_size");
  416. m_zoomuni = m_shader->GetUniformLocation("u_zoom_settings");
  417. m_vdecl =
  418. new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position),
  419. VertexStream<vec2>(VertexUsage::TexCoord));
  420. m_vbo = new VertexBuffer(sizeof(vertices));
  421. m_tbo = new VertexBuffer(sizeof(texcoords));
  422. void *tmp = m_vbo->Lock(0, 0);
  423. memcpy(tmp, vertices, sizeof(vertices));
  424. m_vbo->Unlock();
  425. tmp = m_tbo->Lock(0, 0);
  426. memcpy(tmp, texcoords, sizeof(texcoords));
  427. m_tbo->Unlock();
  428. /* FIXME: this object never cleans up */
  429. m_ready = true;
  430. }
  431. m_texture->Bind();
  432. if (m_dirty[m_frame])
  433. {
  434. #if LOL_FEATURE_THREADS
  435. for (int i = 0; i < m_size.y; i += MAX_LINES * 2)
  436. m_donequeue.pop();
  437. #endif
  438. m_dirty[m_frame]--;
  439. m_texture->SetSubData(ivec2(0, m_frame * m_size.y / 2),
  440. m_size / 2,
  441. &m_pixels[m_size.x * m_size.y / 4 * m_frame]);
  442. }
  443. m_shader->Bind();
  444. m_shader->SetUniform(m_texuni, m_texture->GetTextureUniform(), 0);
  445. m_shader->SetUniform(m_texeluni, m_texel_settings);
  446. m_shader->SetUniform(m_screenuni, m_screen_settings);
  447. m_shader->SetUniform(m_zoomuni, m_zoom_settings);
  448. m_vdecl->Bind();
  449. m_vdecl->SetStream(m_vbo, m_vertexattrib);
  450. m_vdecl->SetStream(m_tbo, m_texattrib);
  451. m_texture->Bind();
  452. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  453. m_vdecl->Unbind();
  454. }
  455. private:
  456. static int const MAX_ITERATIONS = 400;
  457. static int const PALETTE_STEP = 32;
  458. static int const MAX_THREADS = 8;
  459. static int const MAX_LINES = 8;
  460. // 1e-14 for doubles, 1e-17 for long doubles
  461. static double constexpr MAX_ZOOM = 1e-17;
  462. ivec2 m_size, m_window_size, m_oldmouse;
  463. double m_window2world;
  464. dvec2 m_texel2world;
  465. array<u8vec4> m_pixels, m_palette;
  466. Shader *m_shader;
  467. ShaderAttrib m_vertexattrib, m_texattrib;
  468. ShaderUniform m_texuni, m_texeluni, m_screenuni, m_zoomuni;
  469. VertexDeclaration *m_vdecl;
  470. VertexBuffer *m_vbo, *m_tbo;
  471. Texture *m_texture;
  472. int m_frame, m_slices, m_dirty[4];
  473. bool m_ready, m_drag;
  474. struct view_settings
  475. {
  476. rcmplx center, translate, r0;
  477. double radius;
  478. };
  479. view_settings m_view, m_saved_view;
  480. rcmplx m_deltashift[4];
  481. real m_deltascale[4];
  482. double m_zoom_speed;
  483. bool m_julia;
  484. vec4 m_texel_settings, m_screen_settings;
  485. mat4 m_zoom_settings;
  486. // Input support
  487. InputDevice *m_mouse;
  488. Controller *m_controller;
  489. InputProfile m_profile;
  490. #if LOL_FEATURE_THREADS
  491. /* Worker threads */
  492. thread *m_threads[MAX_THREADS];
  493. queue<int> m_spawnqueue, m_jobqueue, m_donequeue;
  494. #endif
  495. /* Debug information */
  496. Text *m_centertext, *m_mousetext, *m_zoomtext;
  497. };
  498. int main(int argc, char **argv)
  499. {
  500. ivec2 window_size(640, 480);
  501. sys::init(argc, argv);
  502. Application app("Tutorial 11: Fractal", window_size, 60.0f);
  503. new DebugFps(5, 5);
  504. new Fractal(window_size);
  505. //new DebugRecord("fractalol.ogm", 60.0f);
  506. app.Run();
  507. return EXIT_SUCCESS;
  508. }