source: trunk/tutorial/11_fractal.cpp @ 2056

Last change on this file since 2056 was 2056, checked in by sam, 7 years ago

math: reimplement min(), max(), abs() and fmod() in the lol:: namespace to
avoid conflicts with the C++ stdlib.

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