Changeset 1061


Ignore:
Timestamp:
Nov 12, 2011, 1:15:38 PM (12 years ago)
Author:
sam
Message:

tutorial: speed up Mandelbrot zoomer by only updating one pixel out of 4
each frame.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/tutorial/tut03.cpp

    r1060 r1061  
    3838        m_size = size;
    3939        m_pixels = new u8vec4[size.x * size.y];
    40         m_time = 0.0f;
     40        m_frame = 0;
     41        m_center = 0;
     42        m_target = f64cmplx(0.001643721971153, 0.822467633298876);
     43        //m_target = f64cmplx(0.28693186889504513, 0.014286693904085048);
     44        m_radius = 8.0;
    4145        m_ready = false;
    4246    }
     
    5155        WorldEntity::TickGame(deltams);
    5256
    53         m_time += deltams * 0.0005f;
    54 
    55         f64cmplx center(0.001643721971153, 0.822467633298876);
    56         //f64cmplx center(0.28693186889504513, 0.014286693904085048);
    57         double radius = 8.0 * pow(2.0, -m_time);
    58         double step = radius / (m_size.x > m_size.y ? m_size.x : m_size.y);
    59 
    60         for (int j = 0; j < m_size.y; j++)
    61         for (int i = 0; i < m_size.x; i++)
     57        m_frame = (m_frame + 1) % 4;
     58
     59        double zoom = pow(2.0, -deltams * 0.00015);
     60        m_radius *= zoom;
     61        m_center = (m_center - m_target) * zoom * zoom + m_target;
     62
     63        double step = m_radius / (m_size.x > m_size.y ? m_size.x : m_size.y);
     64
     65        for (int j = m_frame / 2; j < m_size.y; j += 2)
     66        //for (int j = ((m_frame + 1) % 4) / 2; j < m_size.y; j += 2)
     67        for (int i = m_frame % 2; i < m_size.x; i += 2)
    6268        {
    6369            double const maxlen = 32;
    64             int const colors = 16;
    65             int const maxiter = 200;
     70            int const maxiter = 170;
    6671
    6772            f64cmplx delta(i - m_size.x / 2, j - m_size.y / 2);
    6873
    69             f64cmplx z0 = center + step * delta;
     74            f64cmplx z0 = m_center + step * delta;
    7075            f64cmplx r0 = z0;
    7176            //f64cmplx r0(0.28693186889504513, 0.014286693904085048);
    72             //f64cmplx r0(-0.824,0.1711);
    7377            //f64cmplx r0(0.001643721971153, 0.822467633298876);
    7478            f64cmplx z;
     
    8084            double n = z.sqlen();
    8185
    82             /* Approximate log2(x) with x-1 because x is in [1,2]. */
    83             f += (log(n) * 0.5f / log(maxlen)) - 1.0f;
     86            double k = log(n) * 0.5f / log(maxlen);
     87            /* Approximate log2(k) in [1,2]. */
     88            f += (- 0.344847817623168308695977510213252644185 * k
     89                  + 2.024664188044341212602376988171727038739) * k
     90                  - 1.674876738008591047163498125918330313237;
    8491
    8592            if (iter)
    8693            {
    87                 double r = fmod(f, (double)colors);
    88                 if (r > (double)colors / 2) r = (double)colors - r;
    89                 double g = fmod(f * 1.3 + 4.0f, (double)colors);
    90                 if (g > (double)colors / 2) g = (double)colors - g;
    91                 double b = fmod(f * 1.7 - 8.0f, (double)colors);
    92                 if (b > (double)colors / 2) b = (double)colors - b;
    93 
    94                 uint8_t red = 255 - r * (255.0f / (colors + 1));
    95                 uint8_t green = 255 - g * (255.0f / (colors + 1));
    96                 uint8_t blue = 255 - b * (255.0f / (colors + 1));
     94                double r = 0.5 * sin(f * 0.27 - 2.0) + 0.5;
     95                double g = 0.5 * sin(f * 0.13 + 1.0) + 0.5;
     96                double b = 0.5 * sin(f * 0.21) + 0.5;
     97
     98                uint8_t red = r * 255.0f;
     99                uint8_t green = g * 255.0f;
     100                uint8_t blue = b * 255.0f;
    97101                m_pixels[j * m_size.x + i] = u8vec4(red, green, blue, 0);
    98102            }
     
    120124        static float const texcoords[] =
    121125        {
     126             1.0f,  0.0f,
    122127             0.0f,  0.0f,
     128             0.0f,  1.0f,
     129             0.0f,  1.0f,
     130             1.0f,  1.0f,
    123131             1.0f,  0.0f,
    124              1.0f,  1.0f,
    125              1.0f,  1.0f,
    126              0.0f,  1.0f,
    127              0.0f,  0.0f,
    128132        };
    129133
     
    134138            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_size.x, m_size.y, 0,
    135139                         GL_RGBA, GL_UNSIGNED_BYTE, m_pixels);
    136             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    137             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     140            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     141            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    138142
    139143            m_shader = Shader::Create(
     
    150154                "uniform sampler2D in_Texture;\n"
    151155                "void main(void) {"
    152                 "    gl_FragColor = texture2D(in_Texture, gl_TexCoord[0].xy);"
     156                "    vec2 coord = gl_TexCoord[0].xy;"
     157                "    gl_FragColor = texture2D(in_Texture, coord);"
    153158                "}"
    154159#else
     
    249254#endif
    250255    int m_vertexattrib, m_texattrib;
    251     float m_time;
     256    int m_frame;
    252257    bool m_ready;
     258
     259    f64cmplx m_center, m_target;
     260    double m_radius;
    253261};
    254262
     
    262270
    263271    new DebugFps(5, 5);
    264     new Fractal(ivec2(1280, 960));
     272    new Fractal(ivec2(640, 480));
    265273
    266274    app.Run();
Note: See TracChangeset for help on using the changeset viewer.