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.
 
 
 

581 wiersze
19 KiB

  1. //
  2. // Lol Engine - Fractal tutorial
  3. //
  4. // Copyright: (c) 2011-2013 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://www.wtfpl.net/ for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cstring>
  14. #include <cstdio>
  15. #include "core.h"
  16. #include "loldebug.h"
  17. using namespace lol;
  18. LOLFX_RESOURCE_DECLARE(11_fractal);
  19. class Fractal : public WorldEntity
  20. {
  21. public:
  22. Fractal(ivec2 const &size)
  23. {
  24. /* Ensure texture size is a multiple of 16 for better aligned
  25. * data access. Store the dimensions of a texel for our shader,
  26. * as well as the half-size of the screen. */
  27. m_size = size;
  28. m_size.x = (m_size.x + 15) & ~15;
  29. m_size.y = (m_size.y + 15) & ~15;
  30. m_texel_settings = vec4(1.0, 1.0, 2.0, 2.0) / m_size.xyxy;
  31. m_screen_settings = vec4(1.0, 1.0, 0.5, 0.5) * m_size.xyxy;
  32. /* Window size decides the world aspect ratio. For instance, 640×480
  33. * will be mapped to (-0.66,-0.5) - (0.66,0.5). */
  34. #if !defined __native_client__
  35. m_window_size = Video::GetSize();
  36. #else
  37. /* FIXME: it's illegal to call this on the game thread! */
  38. m_window_size = ivec2(640, 480);
  39. #endif
  40. if (m_window_size.y < m_window_size.x)
  41. m_window2world = 0.5 / m_window_size.y;
  42. else
  43. m_window2world = 0.5 / m_window_size.x;
  44. m_texel2world = (dvec2)m_window_size / (dvec2)m_size * m_window2world;
  45. m_oldmouse = ivec2(0, 0);
  46. m_pixels.Resize(m_size.x * m_size.y);
  47. m_frame = -1;
  48. m_slices = 4;
  49. for (int i = 0; i < 4; i++)
  50. {
  51. m_deltashift[i] = real("0");
  52. m_deltascale[i] = real("1");
  53. m_dirty[i] = 2;
  54. }
  55. #if defined __CELLOS_LV2__ || defined _XBOX
  56. //m_center = rcmplx(-.22815528839841, -1.11514249704382);
  57. //m_center = rcmplx(0.001643721971153, 0.822467633298876);
  58. m_center = rcmplx("-0.65823419062254", "0.50221777363480");
  59. m_zoom_speed = -0.025;
  60. #else
  61. m_center = rcmplx(-0.75, 0.0);
  62. m_zoom_speed = 0.0;
  63. #endif
  64. m_translate = rcmplx(0.0, 0.0);
  65. m_radius = 5.0;
  66. m_ready = false;
  67. m_drag = false;
  68. for (int i = 0; i < (MAX_ITERATIONS + 1) * PALETTE_STEP; i++)
  69. {
  70. double f = (double)i / PALETTE_STEP;
  71. double r = 0.5 * lol::sin(f * 0.27 + 2.0) + 0.5;
  72. double g = 0.5 * lol::sin(f * 0.17 - 1.8) + 0.5;
  73. double b = 0.5 * lol::sin(f * 0.21 - 2.6) + 0.5;
  74. if (f < 7.0)
  75. {
  76. f = f < 1.0 ? 0.0 : (f - 1.0) / 6.0;
  77. r *= f;
  78. g *= f;
  79. b *= f;
  80. }
  81. uint8_t red = r * 255.99f;
  82. uint8_t green = g * 255.99f;
  83. uint8_t blue = b * 255.99f;
  84. #if defined __CELLOS_LV2__ || defined _XBOX
  85. m_palette.Push(u8vec4(255, red, green, blue));
  86. #elif defined __native_client__
  87. m_palette.Push(u8vec4(red, green, blue, 255));
  88. #else
  89. m_palette.Push(u8vec4(blue, green, red, 255));
  90. #endif
  91. }
  92. #if !defined __native_client__
  93. m_centertext = new Text(NULL, "data/font/ascii.png");
  94. m_centertext->SetPos(ivec3(5, m_window_size.y - 15, 1));
  95. Ticker::Ref(m_centertext);
  96. m_mousetext = new Text(NULL, "data/font/ascii.png");
  97. m_mousetext->SetPos(ivec3(5, m_window_size.y - 29, 1));
  98. Ticker::Ref(m_mousetext);
  99. m_zoomtext = new Text(NULL, "data/font/ascii.png");
  100. m_zoomtext->SetPos(ivec3(5, m_window_size.y - 43, 1));
  101. Ticker::Ref(m_zoomtext);
  102. #endif
  103. m_position = ivec3(0, 0, 0);
  104. m_bbox[0] = m_position;
  105. m_bbox[1] = ivec3(m_window_size, 0);
  106. //Input::TrackMouse(this);
  107. #if LOL_FEATURE_THREADS
  108. /* Spawn worker threads and wait for their readiness. */
  109. for (int i = 0; i < MAX_THREADS; i++)
  110. m_threads[i] = new Thread(DoWorkHelper, this);
  111. for (int i = 0; i < MAX_THREADS; i++)
  112. m_spawnqueue.Pop();
  113. #endif
  114. }
  115. ~Fractal()
  116. {
  117. #if LOL_FEATURE_THREADS
  118. /* Signal worker threads for completion and wait for
  119. * them to quit. */
  120. for (int i = 0; i < MAX_THREADS; i++)
  121. m_jobqueue.Push(-1);
  122. for (int i = 0; i < MAX_THREADS; i++)
  123. m_donequeue.Pop();
  124. #endif
  125. //Input::UntrackMouse(this);
  126. #if !defined __native_client__
  127. Ticker::Unref(m_centertext);
  128. Ticker::Unref(m_mousetext);
  129. Ticker::Unref(m_zoomtext);
  130. #endif
  131. }
  132. inline dcmplx TexelToWorldOffset(vec2 texel)
  133. {
  134. double dx = (0.5 + texel.x - m_size.x / 2) * m_texel2world.x;
  135. double dy = (0.5 + m_size.y / 2 - texel.y) * m_texel2world.y;
  136. return m_radius * dcmplx(dx, dy);
  137. }
  138. inline dcmplx ScreenToWorldOffset(vec2 pixel)
  139. {
  140. /* No 0.5 offset here, because we want to be able to position the
  141. * mouse at (0,0) exactly. */
  142. double dx = pixel.x - m_window_size.x / 2;
  143. double dy = m_window_size.y / 2 - pixel.y;
  144. return m_radius * m_window2world * dcmplx(dx, dy);
  145. }
  146. virtual void TickGame(float seconds)
  147. {
  148. WorldEntity::TickGame(seconds);
  149. int prev_frame = (m_frame + 4) % 4;
  150. m_frame = (m_frame + 1) % 4;
  151. rcmplx worldmouse = m_center + rcmplx(ScreenToWorldOffset(m_mousepos));
  152. uint32_t buttons = 0;
  153. //uint32_t buttons = Input::GetMouseButtons();
  154. #if !defined __CELLOS_LV2__ && !defined _XBOX
  155. if (buttons & 0x2)
  156. {
  157. if (!m_drag)
  158. {
  159. m_oldmouse = m_mousepos;
  160. m_drag = true;
  161. }
  162. m_translate = ScreenToWorldOffset(m_oldmouse)
  163. - ScreenToWorldOffset(m_mousepos);
  164. /* XXX: the purpose of this hack is to avoid translating by
  165. * an exact number of pixels. If this were to happen, the step()
  166. * optimisation for i915 cards in our shader would behave
  167. * incorrectly because a quarter of the pixels in the image
  168. * would have tie rankings in the distance calculation. */
  169. m_translate *= real(1023.0 / 1024.0);
  170. m_oldmouse = m_mousepos;
  171. }
  172. else
  173. {
  174. m_drag = false;
  175. if (m_translate != rcmplx(0.0, 0.0))
  176. {
  177. m_translate *= real(std::pow(2.0, -seconds * 5.0));
  178. if ((double)m_translate.norm() < m_radius * 1e-4)
  179. m_translate = rcmplx(0.0, 0.0);
  180. }
  181. }
  182. if (buttons & 0x5 && m_mousepos.x != -1)
  183. {
  184. double zoom = (buttons & 0x1) ? -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. #endif
  196. if (m_zoom_speed || m_translate != rcmplx(0.0, 0.0))
  197. {
  198. rcmplx oldcenter = m_center;
  199. double oldradius = m_radius;
  200. double zoom = std::pow(2.0, seconds * 1e3f * m_zoom_speed);
  201. if (m_radius * zoom > 8.0)
  202. {
  203. m_zoom_speed *= -1.0;
  204. zoom = 8.0 / m_radius;
  205. }
  206. else if (m_radius * zoom < 1e-14)
  207. {
  208. m_zoom_speed *= -1.0;
  209. zoom = 1e-14 / m_radius;
  210. }
  211. m_radius *= zoom;
  212. #if !defined __CELLOS_LV2__ && !defined _XBOX
  213. m_center += m_translate;
  214. m_center = (m_center - worldmouse) * real(zoom) + worldmouse;
  215. worldmouse = m_center + rcmplx(ScreenToWorldOffset(m_mousepos));
  216. #endif
  217. /* Store the transformation properties to go from m_frame - 1
  218. * to m_frame. */
  219. m_deltashift[prev_frame] = (m_center - oldcenter) / real(oldradius);
  220. m_deltashift[prev_frame].x /= m_size.x * m_texel2world.x;
  221. m_deltashift[prev_frame].y /= m_size.y * m_texel2world.y;
  222. m_deltascale[prev_frame] = m_radius / oldradius;
  223. m_dirty[0] = m_dirty[1] = m_dirty[2] = m_dirty[3] = 2;
  224. }
  225. else
  226. {
  227. /* If settings didn't change, set transformation from previous
  228. * frame to identity. */
  229. m_deltashift[prev_frame] = real::R_0();
  230. m_deltascale[prev_frame] = real::R_1();
  231. }
  232. /* Transformation from current frame to current frame is always
  233. * identity. */
  234. m_zoom_settings[m_frame][0] = 0.0f;
  235. m_zoom_settings[m_frame][1] = 0.0f;
  236. m_zoom_settings[m_frame][2] = 1.0f;
  237. /* Compute transformation from other frames to current frame */
  238. for (int i = 0; i < 3; i++)
  239. {
  240. int prev_index = (m_frame + 4 - i) % 4;
  241. int cur_index = (m_frame + 3 - i) % 4;
  242. m_zoom_settings[cur_index][0] = (real)m_zoom_settings[prev_index][0] * m_deltascale[cur_index] + m_deltashift[cur_index].x;
  243. m_zoom_settings[cur_index][1] = (real)m_zoom_settings[prev_index][1] * m_deltascale[cur_index] + m_deltashift[cur_index].y;
  244. m_zoom_settings[cur_index][2] = (real)m_zoom_settings[prev_index][2] * m_deltascale[cur_index];
  245. }
  246. /* Precompute texture offset change instead of doing it in GLSL */
  247. for (int i = 0; i < 4; i++)
  248. {
  249. m_zoom_settings[i][0] += 0.5 * (1.0 - m_zoom_settings[i][2]);
  250. m_zoom_settings[i][1] -= 0.5 * (1.0 - m_zoom_settings[i][2]);
  251. }
  252. #if !defined __native_client__
  253. char buf[256];
  254. std::sprintf(buf, "center: ");
  255. m_center.x.sprintf(buf + strlen(buf), 30);
  256. std::sprintf(buf + strlen(buf), " ");
  257. m_center.y.sprintf(buf + strlen(buf), 30);
  258. m_centertext->SetText(buf);
  259. std::sprintf(buf, " mouse: ");
  260. worldmouse.x.sprintf(buf + strlen(buf), 30);
  261. std::sprintf(buf + strlen(buf), " ");
  262. worldmouse.y.sprintf(buf + strlen(buf), 30);
  263. m_mousetext->SetText(buf);
  264. std::sprintf(buf, " zoom: %g", 1.0 / m_radius);
  265. m_zoomtext->SetText(buf);
  266. #endif
  267. if (m_dirty[m_frame])
  268. {
  269. m_dirty[m_frame]--;
  270. for (int i = 0; i < m_size.y; i += MAX_LINES * 2)
  271. {
  272. #if LOL_FEATURE_THREADS
  273. m_jobqueue.Push(i);
  274. #else
  275. DoWork(i);
  276. #endif
  277. }
  278. }
  279. }
  280. #if LOL_FEATURE_THREADS
  281. static void *DoWorkHelper(void *data)
  282. {
  283. Fractal *that = (Fractal *)data;
  284. that->m_spawnqueue.Push(0);
  285. for ( ; ; )
  286. {
  287. int line = that->m_jobqueue.Pop();
  288. if (line == -1)
  289. break;
  290. that->DoWork(line);
  291. that->m_donequeue.Push(0);
  292. }
  293. that->m_donequeue.Push(0);
  294. return NULL;
  295. };
  296. #endif
  297. void DoWork(int line)
  298. {
  299. double const maxsqlen = 1024;
  300. double const k1 = 1.0 / (1 << 10) / (std::log(maxsqlen) / std::log(2.0));
  301. int jmin = ((m_frame + 1) % 4) / 2 + line;
  302. int jmax = jmin + MAX_LINES * 2;
  303. if (jmax > m_size.y)
  304. jmax = m_size.y;
  305. u8vec4 *m_pixelstart = &m_pixels[0]
  306. + m_size.x * (m_size.y / 4 * m_frame + line / 4);
  307. dcmplx c = (dcmplx)m_center;
  308. for (int j = jmin; j < jmax; j += 2)
  309. for (int i = m_frame % 2; i < m_size.x; i += 2)
  310. {
  311. double xr, yr, x0, y0, x1, y1, x2, y2, x3, y3;
  312. dcmplx z0 = c + TexelToWorldOffset(ivec2(i, j));
  313. //dcmplx r0(0.28693186889504513, 0.014286693904085048);
  314. //dcmplx r0(0.001643721971153, 0.822467633298876);
  315. //dcmplx r0(-1.207205434596, 0.315432814901);
  316. //dcmplx r0(-0.79192956889854, -0.14632423080102);
  317. //dcmplx r0(0.3245046418497685, 0.04855101129280834);
  318. dcmplx r0 = z0;
  319. x0 = z0.x; y0 = z0.y;
  320. xr = r0.x; yr = r0.y;
  321. int iter = MAX_ITERATIONS - 4;
  322. for (;;)
  323. {
  324. /* Unroll the loop: tests are more expensive to do at each
  325. * iteration than the few extra multiplications. */
  326. x1 = x0 * x0 - y0 * y0 + xr;
  327. y1 = x0 * y0 + x0 * y0 + yr;
  328. x2 = x1 * x1 - y1 * y1 + xr;
  329. y2 = x1 * y1 + x1 * y1 + yr;
  330. x3 = x2 * x2 - y2 * y2 + xr;
  331. y3 = x2 * y2 + x2 * y2 + yr;
  332. x0 = x3 * x3 - y3 * y3 + xr;
  333. y0 = x3 * y3 + x3 * y3 + yr;
  334. if (x0 * x0 + y0 * y0 >= maxsqlen)
  335. break;
  336. iter -= 4;
  337. if (iter < 4)
  338. break;
  339. }
  340. if (iter)
  341. {
  342. double n = x0 * x0 + y0 * y0;
  343. if (x1 * x1 + y1 * y1 >= maxsqlen)
  344. {
  345. iter += 3; n = x1 * x1 + y1 * y1;
  346. }
  347. else if (x2 * x2 + y2 * y2 >= maxsqlen)
  348. {
  349. iter += 2; n = x2 * x2 + y2 * y2;
  350. }
  351. else if (x3 * x3 + y3 * y3 >= maxsqlen)
  352. {
  353. iter += 1; n = x3 * x3 + y3 * y3;
  354. }
  355. if (n > maxsqlen * maxsqlen)
  356. n = maxsqlen * maxsqlen;
  357. /* Approximate log(sqrt(n))/log(sqrt(maxsqlen)) */
  358. double f = iter;
  359. union { double n; uint64_t x; } u = { n };
  360. double k = (u.x >> 42) - (((1 << 10) - 1) << 10);
  361. k *= k1;
  362. /* Approximate log2(k) in [1,2]. */
  363. f += (- 0.344847817623168308695977510213252644185 * k
  364. + 2.024664188044341212602376988171727038739) * k
  365. - 1.674876738008591047163498125918330313237;
  366. *m_pixelstart++ = m_palette[(int)(f * PALETTE_STEP)];
  367. }
  368. else
  369. {
  370. #if defined __CELLOS_LV2__ || defined _XBOX
  371. *m_pixelstart++ = u8vec4(255, 0, 0, 0);
  372. #else
  373. *m_pixelstart++ = u8vec4(0, 0, 0, 255);
  374. #endif
  375. }
  376. }
  377. }
  378. virtual void TickDraw(float seconds)
  379. {
  380. WorldEntity::TickDraw(seconds);
  381. static float const vertices[] =
  382. {
  383. 1.0f, 1.0f,
  384. -1.0f, 1.0f,
  385. -1.0f, -1.0f,
  386. -1.0f, -1.0f,
  387. 1.0f, -1.0f,
  388. 1.0f, 1.0f,
  389. };
  390. static float const texcoords[] =
  391. {
  392. 1.0f, 1.0f,
  393. 0.0f, 1.0f,
  394. 0.0f, 0.0f,
  395. 0.0f, 0.0f,
  396. 1.0f, 0.0f,
  397. 1.0f, 1.0f,
  398. };
  399. if (!m_ready)
  400. {
  401. /* Create a texture of half the width and twice the height
  402. * so that we can upload four different subimages each frame. */
  403. m_texture = new Texture(ivec2(m_size.x / 2, m_size.y * 2),
  404. PixelFormat::ABGR_8);
  405. /* Ensure the texture data is complete at least once, otherwise
  406. * uploading subimages will not work. */
  407. m_texture->SetData(&m_pixels[0]);
  408. m_shader = Shader::Create(LOLFX_RESOURCE_NAME(11_fractal));
  409. m_vertexattrib = m_shader->GetAttribLocation(VertexUsage::Position, 0);
  410. m_texattrib = m_shader->GetAttribLocation(VertexUsage::TexCoord, 0);
  411. m_texeluni = m_shader->GetUniformLocation("u_TexelSize");
  412. m_screenuni = m_shader->GetUniformLocation("u_ScreenSize");
  413. m_zoomuni = m_shader->GetUniformLocation("u_ZoomSettings");
  414. m_vdecl =
  415. new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position),
  416. VertexStream<vec2>(VertexUsage::TexCoord));
  417. m_vbo = new VertexBuffer(sizeof(vertices));
  418. m_tbo = new VertexBuffer(sizeof(texcoords));
  419. void *tmp = m_vbo->Lock(0, 0);
  420. memcpy(tmp, vertices, sizeof(vertices));
  421. m_vbo->Unlock();
  422. tmp = m_tbo->Lock(0, 0);
  423. memcpy(tmp, texcoords, sizeof(texcoords));
  424. m_tbo->Unlock();
  425. /* FIXME: this object never cleans up */
  426. m_ready = true;
  427. }
  428. m_texture->Bind();
  429. if (m_dirty[m_frame])
  430. {
  431. #if LOL_FEATURE_THREADS
  432. for (int i = 0; i < m_size.y; i += MAX_LINES * 2)
  433. m_donequeue.Pop();
  434. #endif
  435. m_dirty[m_frame]--;
  436. #if defined __CELLOS_LV2__
  437. /* glTexSubImage2D is extremely slow on the PS3, to the point
  438. * that uploading the whole texture is 40 times faster. */
  439. m_texture->SetData(&m_pixels[0]);
  440. #else
  441. m_texture->SetSubData(ivec2(0, m_frame * m_size.y / 2),
  442. m_size / 2,
  443. &m_pixels[m_size.x * m_size.y / 4 * m_frame]);
  444. #endif
  445. }
  446. m_shader->Bind();
  447. m_shader->SetUniform(m_texeluni, m_texel_settings);
  448. m_shader->SetUniform(m_screenuni, m_screen_settings);
  449. m_shader->SetUniform(m_zoomuni, m_zoom_settings);
  450. m_vdecl->Bind();
  451. m_vdecl->SetStream(m_vbo, m_vertexattrib);
  452. m_vdecl->SetStream(m_tbo, m_texattrib);
  453. m_texture->Bind();
  454. m_vdecl->DrawElements(MeshPrimitive::Triangles, 0, 6);
  455. m_vdecl->Unbind();
  456. }
  457. private:
  458. static int const MAX_ITERATIONS = 340;
  459. static int const PALETTE_STEP = 32;
  460. static int const MAX_THREADS = 8;
  461. static int const MAX_LINES = 8;
  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_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. rcmplx m_deltashift[4], m_center, m_translate;
  475. real m_deltascale[4];
  476. double m_zoom_speed, m_radius;
  477. vec4 m_texel_settings, m_screen_settings;
  478. mat4 m_zoom_settings;
  479. #if LOL_FEATURE_THREADS
  480. /* Worker threads */
  481. Thread *m_threads[MAX_THREADS];
  482. Queue<int> m_spawnqueue, m_jobqueue, m_donequeue;
  483. #endif
  484. #if !defined __native_client__
  485. /* Debug information */
  486. Text *m_centertext, *m_mousetext, *m_zoomtext;
  487. #endif
  488. };
  489. int main(int argc, char **argv)
  490. {
  491. ivec2 window_size(640, 480);
  492. System::Init(argc, argv);
  493. Application app("Tutorial 3: Fractal", window_size, 60.0f);
  494. new DebugFps(5, 5);
  495. new Fractal(window_size);
  496. //new DebugRecord("fractalol.ogm", 60.0f);
  497. app.Run();
  498. return EXIT_SUCCESS;
  499. }