source: trunk/test/tutorial/03_fractal.cpp @ 1426

Last change on this file since 1426 was 1426, checked in by sam, 10 years ago

core: rename f64vec4 to dvec4 etc. for consistency with GLSL.

  • Property svn:keywords set to Id
File size: 21.6 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 <cmath>
17
18#include "core.h"
19#include "lolgl.h"
20#include "loldebug.h"
21
22using namespace std;
23using namespace lol;
24
25#if defined _WIN32 && defined USE_D3D9
26#   define FAR
27#   define NEAR
28#   include <d3d9.h>
29#endif
30
31#if USE_SDL && defined __APPLE__
32#   include <SDL_main.h>
33#endif
34
35#if defined _WIN32
36#   undef main /* FIXME: still needed? */
37#   include <direct.h>
38#endif
39
40extern char const *lolfx_03_fractal;
41
42#if defined USE_D3D9
43extern IDirect3DDevice9 *g_d3ddevice;
44#elif defined _XBOX
45extern D3DDevice *g_d3ddevice;
46#elif __CELLOS_LV2__
47static GLint const INTERNAL_FORMAT = GL_ARGB_SCE;
48static GLenum const TEXTURE_FORMAT = GL_BGRA;
49static GLenum const TEXTURE_TYPE = GL_UNSIGNED_INT_8_8_8_8_REV;
50#elif defined __native_client__
51static GLint const INTERNAL_FORMAT = GL_RGBA;
52static GLenum const TEXTURE_FORMAT = GL_RGBA;
53static GLenum const TEXTURE_TYPE = GL_UNSIGNED_BYTE;
54#else
55/* Seems efficient for little endian textures */
56static GLint const INTERNAL_FORMAT = GL_RGBA;
57static GLenum const TEXTURE_FORMAT = GL_BGRA;
58static GLenum const TEXTURE_TYPE = GL_UNSIGNED_INT_8_8_8_8_REV;
59#endif
60
61class Fractal : public WorldEntity
62{
63public:
64    Fractal(ivec2 const &size)
65    {
66        /* Ensure texture size is a multiple of 16 for better aligned
67         * data access. Store the dimensions of a texel for our shader,
68         * as well as the half-size of the screen. */
69        m_size = size;
70        m_size.x = (m_size.x + 15) & ~15;
71        m_size.y = (m_size.y + 15) & ~15;
72        m_texel_settings = vec4(1.0, 1.0, 2.0, 2.0) / m_size.xyxy;
73        m_screen_settings = vec4(1.0, 1.0, 0.5, 0.5) * m_size.xyxy;
74
75        /* Window size decides the world aspect ratio. For instance, 640×480
76         * will be mapped to (-0.66,-0.5) - (0.66,0.5). */
77#if !defined __native_client__
78        m_window_size = Video::GetSize();
79#else
80        /* FIXME: it's illegal to call this on the game thread! */
81        m_window_size = ivec2(640, 480);
82#endif
83        if (m_window_size.y < m_window_size.x)
84            m_window2world = 0.5 / m_window_size.y;
85        else
86            m_window2world = 0.5 / m_window_size.x;
87        m_texel2world = (vec2)m_window_size / m_size * m_window2world;
88
89        m_oldmouse = ivec2(0, 0);
90
91        m_pixels = new u8vec4[m_size.x * m_size.y];
92        m_tmppixels = new u8vec4[m_size.x / 2 * m_size.y / 2];
93        m_frame = -1;
94        m_slices = 4;
95        for (int i = 0; i < 4; i++)
96        {
97            m_deltashift[i] = 0.0;
98            m_deltascale[i] = 1.0;
99            m_dirty[i] = 2;
100        }
101#if defined __CELLOS_LV2__ || defined _XBOX
102        //m_center = dcmplx(-.22815528839841, -1.11514249704382);
103        //m_center = dcmplx(0.001643721971153, 0.822467633298876);
104        m_center = dcmplx(-0.65823419062254, 0.50221777363480);
105        m_zoom_speed = -0.025;
106#else
107        m_center = -0.75;
108        m_zoom_speed = 0.0;
109#endif
110        m_translate = 0;
111        m_radius = 5.0;
112        m_ready = false;
113        m_drag = false;
114
115        m_palette = new u8vec4[(MAX_ITERATIONS + 1) * PALETTE_STEP];
116        for (int i = 0; i < (MAX_ITERATIONS + 1) * PALETTE_STEP; i++)
117        {
118            double f = (double)i / PALETTE_STEP;
119
120            double r = 0.5 * sin(f * 0.27 + 2.0) + 0.5;
121            double g = 0.5 * sin(f * 0.17 - 1.8) + 0.5;
122            double b = 0.5 * sin(f * 0.21 - 2.6) + 0.5;
123
124            if (f < 7.0)
125            {
126                f = f < 1.0 ? 0.0 : (f - 1.0) / 6.0;
127                r *= f;
128                g *= f;
129                b *= f;
130            }
131
132            uint8_t red = r * 255.99f;
133            uint8_t green = g * 255.99f;
134            uint8_t blue = b * 255.99f;
135#if defined __CELLOS_LV2__ || defined _XBOX
136            m_palette[i] = u8vec4(255, red, green, blue);
137#elif defined __native_client__
138            m_palette[i] = u8vec4(red, green, blue, 255);
139#else
140            m_palette[i] = u8vec4(blue, green, red, 255);
141#endif
142        }
143
144#if !defined __native_client__
145        m_centertext = new Text(NULL, "gfx/font/ascii.png");
146        m_centertext->SetPos(ivec3(5, m_window_size.y - 15, 1));
147        Ticker::Ref(m_centertext);
148
149        m_mousetext = new Text(NULL, "gfx/font/ascii.png");
150        m_mousetext->SetPos(ivec3(5, m_window_size.y - 29, 1));
151        Ticker::Ref(m_mousetext);
152
153        m_zoomtext = new Text(NULL, "gfx/font/ascii.png");
154        m_zoomtext->SetPos(ivec3(5, m_window_size.y - 43, 1));
155        Ticker::Ref(m_zoomtext);
156#endif
157
158        m_position = ivec3(0, 0, 0);
159        m_bbox[0] = m_position;
160        m_bbox[1] = ivec3(m_window_size, 0);
161        Input::TrackMouse(this);
162
163        /* Spawn worker threads and wait for their readiness. */
164        for (int i = 0; i < MAX_THREADS; i++)
165            m_threads[i] = new Thread(DoWorkHelper, this);
166        for (int i = 0; i < MAX_THREADS; i++)
167            m_spawnqueue.Pop();
168    }
169
170    ~Fractal()
171    {
172        /* Signal worker threads for completion and wait for
173         * them to quit. */
174        for (int i = 0; i < MAX_THREADS; i++)
175            m_jobqueue.Push(-1);
176        for (int i = 0; i < MAX_THREADS; i++)
177            m_donequeue.Pop();
178
179        Input::UntrackMouse(this);
180#if !defined __native_client__
181        Ticker::Unref(m_centertext);
182        Ticker::Unref(m_mousetext);
183        Ticker::Unref(m_zoomtext);
184#endif
185        delete m_pixels;
186        delete m_tmppixels;
187        delete m_palette;
188    }
189
190    inline dcmplx TexelToWorldOffset(vec2 texel)
191    {
192        double dx = (0.5 + texel.x - m_size.x / 2) * m_texel2world.x;
193        double dy = (0.5 + m_size.y / 2 - texel.y) * m_texel2world.y;
194        return m_radius * dcmplx(dx, dy);
195    }
196
197    inline dcmplx ScreenToWorldOffset(vec2 pixel)
198    {
199        /* No 0.5 offset here, because we want to be able to position the
200         * mouse at (0,0) exactly. */
201        double dx = pixel.x - m_window_size.x / 2;
202        double dy = m_window_size.y / 2 - pixel.y;
203        return m_radius * m_window2world * dcmplx(dx, dy);
204    }
205
206    virtual void TickGame(float seconds)
207    {
208        WorldEntity::TickGame(seconds);
209
210        int prev_frame = m_frame;
211        m_frame = (m_frame + 1) % 4;
212
213        dcmplx worldmouse = m_center + ScreenToWorldOffset(m_mousepos);
214
215        ivec3 buttons = Input::GetMouseButtons();
216#if !defined __CELLOS_LV2__ && !defined _XBOX
217        if (buttons[1])
218        {
219            if (!m_drag)
220            {
221                m_oldmouse = m_mousepos;
222                m_drag = true;
223            }
224            m_translate = ScreenToWorldOffset(m_oldmouse)
225                        - ScreenToWorldOffset(m_mousepos);
226            /* XXX: the purpose of this hack is to avoid translating by
227             * an exact number of pixels. If this were to happen, the step()
228             * optimisation for i915 cards in our shader would behave
229             * incorrectly because a quarter of the pixels in the image
230             * would have tie rankings in the distance calculation. */
231            m_translate *= 1023.0 / 1024.0;
232            m_oldmouse = m_mousepos;
233        }
234        else
235        {
236            m_drag = false;
237            if (m_translate != 0.0)
238            {
239                m_translate *= pow(2.0, -seconds * 5.0);
240                if (m_translate.norm() / m_radius < 1e-4)
241                    m_translate = 0.0;
242            }
243        }
244
245        if ((buttons[0] || buttons[2]) && m_mousepos.x != -1)
246        {
247            double zoom = buttons[0] ? -0.5 : 0.5;
248            m_zoom_speed += seconds * zoom;
249            if (m_zoom_speed / zoom > 5e-3f)
250                m_zoom_speed = 5e-3f * zoom;
251        }
252        else if (m_zoom_speed)
253        {
254            m_zoom_speed *= pow(2.0, -seconds * 5.0);
255            if (abs(m_zoom_speed) < 1e-5 || m_drag)
256                m_zoom_speed = 0.0;
257        }
258#endif
259
260        if (m_zoom_speed || m_translate != 0.0)
261        {
262            dcmplx oldcenter = m_center;
263            double oldradius = m_radius;
264            double zoom = pow(2.0, seconds * 1e3f * m_zoom_speed);
265            if (m_radius * zoom > 8.0)
266            {
267                m_zoom_speed *= -1.0;
268                zoom = 8.0 / m_radius;
269            }
270            else if (m_radius * zoom < 1e-14)
271            {
272                m_zoom_speed *= -1.0;
273                zoom = 1e-14 / m_radius;
274            }
275            m_radius *= zoom;
276#if !defined __CELLOS_LV2__ && !defined _XBOX
277            m_center += m_translate;
278            m_center = (m_center - worldmouse) * zoom + worldmouse;
279            worldmouse = m_center + ScreenToWorldOffset(m_mousepos);
280#endif
281
282            /* Store the transformation properties to go from m_frame - 1
283             * to m_frame. */
284            m_deltashift[prev_frame] = (m_center - oldcenter) / oldradius;
285            m_deltashift[prev_frame].x /= m_size.x * m_texel2world.x;
286            m_deltashift[prev_frame].y /= m_size.y * m_texel2world.y;
287            m_deltascale[prev_frame] = m_radius / oldradius;
288            m_dirty[0] = m_dirty[1] = m_dirty[2] = m_dirty[3] = 2;
289        }
290        else
291        {
292            /* If settings didn't change, set transformation from previous
293             * frame to identity. */
294            m_deltashift[prev_frame] = 0.0;
295            m_deltascale[prev_frame] = 1.0;
296        }
297
298        /* Transformation from current frame to current frame is always
299         * identity. */
300        m_zoom_settings[m_frame][0] = 0.0f;
301        m_zoom_settings[m_frame][1] = 0.0f;
302        m_zoom_settings[m_frame][2] = 1.0f;
303
304        /* Compute transformation from other frames to current frame */
305        for (int i = 0; i < 3; i++)
306        {
307            int prev_index = (m_frame + 4 - i) % 4;
308            int cur_index = (m_frame + 3 - i) % 4;
309
310            m_zoom_settings[cur_index][0] = m_zoom_settings[prev_index][0] * m_deltascale[cur_index] + m_deltashift[cur_index].x;
311            m_zoom_settings[cur_index][1] = m_zoom_settings[prev_index][1] * m_deltascale[cur_index] + m_deltashift[cur_index].y;
312            m_zoom_settings[cur_index][2] = m_zoom_settings[prev_index][2] * m_deltascale[cur_index];
313        }
314
315        /* Precompute texture offset change instead of doing it in GLSL */
316        for (int i = 0; i < 4; i++)
317        {
318            m_zoom_settings[i][0] += 0.5 * (1.0 - m_zoom_settings[i][2]);
319            m_zoom_settings[i][1] -= 0.5 * (1.0 - m_zoom_settings[i][2]);
320        }
321
322#if !defined __native_client__
323        char buf[128];
324        sprintf(buf, "center: %+16.14f%+16.14fi", m_center.x, m_center.y);
325        m_centertext->SetText(buf);
326        sprintf(buf, " mouse: %+16.14f%+16.14fi", worldmouse.x, worldmouse.y);
327        m_mousetext->SetText(buf);
328        sprintf(buf, "  zoom: %g", 1.0 / m_radius);
329        m_zoomtext->SetText(buf);
330#endif
331
332        if (m_dirty[m_frame])
333        {
334            m_dirty[m_frame]--;
335
336            for (int i = 0; i < m_size.y; i += MAX_LINES * 2)
337                m_jobqueue.Push(i);
338        }
339    }
340
341    static void *DoWorkHelper(void *data)
342    {
343        Fractal *that = (Fractal *)data;
344        that->m_spawnqueue.Push(0);
345        for ( ; ; )
346        {
347            int line = that->m_jobqueue.Pop();
348            if (line == -1)
349                break;
350            that->DoWork(line);
351            that->m_donequeue.Push(0);
352        }
353        that->m_donequeue.Push(0);
354        return NULL;
355    };
356
357    void DoWork(int line)
358    {
359        double const maxsqlen = 1024;
360        double const k1 = 1.0 / (1 << 10) / (std::log(maxsqlen) / std::log(2.0));
361
362        int jmin = ((m_frame + 1) % 4) / 2 + line;
363        int jmax = jmin + MAX_LINES * 2;
364        if (jmax > m_size.y)
365            jmax = m_size.y;
366        u8vec4 *m_pixelstart = m_pixels
367                             + m_size.x * (m_size.y / 4 * m_frame + line / 4);
368
369        for (int j = jmin; j < jmax; j += 2)
370        for (int i = m_frame % 2; i < m_size.x; i += 2)
371        {
372            dcmplx z0 = m_center + TexelToWorldOffset(ivec2(i, j));
373            dcmplx z1, z2, z3, r0 = z0;
374            //dcmplx r0(0.28693186889504513, 0.014286693904085048);
375            //dcmplx r0(0.001643721971153, 0.822467633298876);
376            //dcmplx r0(-1.207205434596, 0.315432814901);
377            //dcmplx r0(-0.79192956889854, -0.14632423080102);
378            //dcmplx r0(0.3245046418497685, 0.04855101129280834);
379            int iter = MAX_ITERATIONS - 4;
380            for (;;)
381            {
382                /* Unroll the loop: tests are more expensive to do at each
383                 * iteration than the few extra multiplications. */
384                z1 = z0 * z0 + r0;
385                z2 = z1 * z1 + r0;
386                z3 = z2 * z2 + r0;
387                z0 = z3 * z3 + r0;
388                if (sqlength(z0) >= maxsqlen)
389                    break;
390                iter -= 4;
391                if (iter < 4)
392                    break;
393            }
394
395            if (iter)
396            {
397                double n = sqlength(z0);
398
399                if (sqlength(z1) >= maxsqlen) { iter += 3; n = sqlength(z1); }
400                else if (sqlength(z2) >= maxsqlen) { iter += 2; n = sqlength(z2); }
401                else if (sqlength(z3) >= maxsqlen) { iter += 1; n = sqlength(z3); }
402
403                if (n > maxsqlen * maxsqlen)
404                    n = maxsqlen * maxsqlen;
405
406                /* Approximate log(sqrt(n))/log(sqrt(maxsqlen)) */
407                double f = iter;
408                union { double n; uint64_t x; } u = { n };
409                double k = (u.x >> 42) - (((1 << 10) - 1) << 10);
410                k *= k1;
411
412                /* Approximate log2(k) in [1,2]. */
413                f += (- 0.344847817623168308695977510213252644185 * k
414                      + 2.024664188044341212602376988171727038739) * k
415                      - 1.674876738008591047163498125918330313237;
416
417                *m_pixelstart++ = m_palette[(int)(f * PALETTE_STEP)];
418            }
419            else
420            {
421#if defined __CELLOS_LV2__ || defined _XBOX
422                *m_pixelstart++ = u8vec4(255, 0, 0, 0);
423#else
424                *m_pixelstart++ = u8vec4(0, 0, 0, 255);
425#endif
426            }
427        }
428    }
429
430    virtual void TickDraw(float seconds)
431    {
432        WorldEntity::TickDraw(seconds);
433
434        static float const vertices[] =
435        {
436             1.0f,  1.0f,
437            -1.0f,  1.0f,
438            -1.0f, -1.0f,
439            -1.0f, -1.0f,
440             1.0f, -1.0f,
441             1.0f,  1.0f,
442        };
443
444        static float const texcoords[] =
445        {
446             1.0f,  1.0f,
447             0.0f,  1.0f,
448             0.0f,  0.0f,
449             0.0f,  0.0f,
450             1.0f,  0.0f,
451             1.0f,  1.0f,
452        };
453
454        if (!m_ready)
455        {
456#if !defined _XBOX && !defined USE_D3D9
457            /* Create a texture of half the width and twice the height
458             * so that we can upload four different subimages each frame. */
459            glGenTextures(1, &m_texid);
460            glBindTexture(GL_TEXTURE_2D, m_texid);
461            glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT,
462                         m_size.x / 2, m_size.y * 2, 0,
463                         TEXTURE_FORMAT, TEXTURE_TYPE, m_pixels);
464#   if defined __CELLOS_LV2__
465            /* We need this hint because by default the storage type is
466             * GL_TEXTURE_SWIZZLED_GPU_SCE. */
467            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_ALLOCATION_HINT_SCE,
468                            GL_TEXTURE_TILED_GPU_SCE);
469#   endif
470            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
471            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
472#elif defined _XBOX
473            /* By default the X360 will swizzle the texture. Ask for linear. */
474            g_d3ddevice->CreateTexture(m_size.x / 2, m_size.y * 2, 1,
475                                       D3DUSAGE_WRITEONLY, D3DFMT_LIN_A8R8G8B8,
476                                       D3DPOOL_DEFAULT, &m_tex, NULL);
477#else
478            g_d3ddevice->CreateTexture(m_size.x / 2, m_size.y * 2, 1,
479                                       D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8,
480                                       D3DPOOL_SYSTEMMEM, &m_tex, NULL);
481#endif
482
483            m_shader = Shader::Create(lolfx_03_fractal);
484
485            m_vertexattrib = m_shader->GetAttribLocation("a_Vertex", VertexUsage::Position, 0);
486            m_texattrib = m_shader->GetAttribLocation("a_TexCoord", VertexUsage::TexCoord, 0);
487            m_texeluni = m_shader->GetUniformLocation("u_TexelSize");
488            m_screenuni = m_shader->GetUniformLocation("u_ScreenSize");
489            m_zoomuni = m_shader->GetUniformLocation("u_ZoomSettings");
490
491            m_vdecl =
492              new VertexDeclaration(VertexStream<vec2>(VertexUsage::Position),
493                                    VertexStream<vec2>(VertexUsage::TexCoord));
494            m_vbo = new VertexBuffer(sizeof(vertices));
495            m_tbo = new VertexBuffer(sizeof(texcoords));
496
497            void *tmp = m_vbo->Lock(0, 0);
498            memcpy(tmp, vertices, sizeof(vertices));
499            m_vbo->Unlock();
500
501            tmp = m_tbo->Lock(0, 0);
502            memcpy(tmp, texcoords, sizeof(texcoords));
503            m_tbo->Unlock();
504
505            /* FIXME: this object never cleans up */
506            m_ready = true;
507        }
508
509#if defined _XBOX || defined USE_D3D9
510
511#else
512#   if !defined HAVE_GLES_2X
513        glEnable(GL_TEXTURE_2D);
514#   endif
515        glBindTexture(GL_TEXTURE_2D, m_texid);
516#endif
517
518        if (m_dirty[m_frame])
519        {
520            for (int i = 0; i < m_size.y; i += MAX_LINES * 2)
521                m_donequeue.Pop();
522
523            m_dirty[m_frame]--;
524
525#if defined _XBOX || defined USE_D3D9
526            D3DLOCKED_RECT rect;
527#   if defined _XBOX
528            m_tex->LockRect(0, &rect, NULL, D3DLOCK_NOOVERWRITE);
529#   else
530            m_tex->LockRect(0, &rect, NULL,
531                            D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE);
532#   endif
533            for (int j = 0; j < m_size.y * 2; j++)
534            {
535                u8vec4 *line = (u8vec4 *)rect.pBits + j * rect.Pitch / 4;
536                for (int i = 0; i < m_size.x / 2; i++)
537                    line[i] = m_pixels[m_size.x / 2 * j + i];
538            }
539            m_tex->UnlockRect(0);
540#elif defined __CELLOS_LV2__
541            /* glTexSubImage2D is extremely slow on the PS3, to the point
542             * that uploading the whole texture is 40 times faster. */
543            glTexImage2D(GL_TEXTURE_2D, 0, INTERNAL_FORMAT,
544                         m_size.x / 2, m_size.y * 2, 0,
545                         TEXTURE_FORMAT, TEXTURE_TYPE, m_pixels);
546#else
547            glTexSubImage2D(GL_TEXTURE_2D, 0, 0, m_frame * m_size.y / 2,
548                            m_size.x / 2, m_size.y / 2,
549                            TEXTURE_FORMAT, TEXTURE_TYPE,
550                            m_pixels + m_size.x * m_size.y / 4 * m_frame);
551#endif
552        }
553
554        m_shader->Bind();
555        m_shader->SetUniform(m_texeluni, m_texel_settings);
556        m_shader->SetUniform(m_screenuni, m_screen_settings);
557        m_shader->SetUniform(m_zoomuni, m_zoom_settings);
558        m_vdecl->Bind();
559        m_vdecl->SetStream(m_vbo, m_vertexattrib);
560        m_vdecl->SetStream(m_tbo, m_texattrib);
561#if defined _XBOX || defined USE_D3D9
562        g_d3ddevice->SetTexture(0, m_tex);
563        g_d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
564#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
565#else
566        //glEnableClientState(GL_VERTEX_ARRAY);
567        //glVertexPointer(2, GL_FLOAT, 0, vertices);
568        //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
569        //glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
570#endif
571
572#if defined _XBOX || defined USE_D3D9
573        g_d3ddevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
574#else
575        glDrawArrays(GL_TRIANGLES, 0, 6);
576#endif
577
578#if defined _XBOX || defined USE_D3D9
579
580        m_vdecl->Unbind();
581#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
582        //glDisableVertexAttribArray(m_vertexattrib);
583        //glDisableVertexAttribArray(m_texattrib);
584        //glBindBuffer(GL_ARRAY_BUFFER, 0);
585#elif !defined __CELLOS_LV2__ && !defined __ANDROID__
586        /* Never used for now */
587        //glDisableVertexAttribArray(m_vertexattrib);
588        //glDisableVertexAttribArray(m_texattrib);
589#else
590        //glDisableClientState(GL_VERTEX_ARRAY);
591        //glDisableClientState(GL_TEXTURE_COORD_ARRAY);
592#endif
593    }
594
595private:
596    static int const MAX_ITERATIONS = 340;
597    static int const PALETTE_STEP = 32;
598    static int const MAX_THREADS = 8;
599    static int const MAX_LINES = 8;
600
601    ivec2 m_size, m_window_size, m_oldmouse;
602    double m_window2world;
603    dvec2 m_texel2world;
604    u8vec4 *m_pixels, *m_tmppixels, *m_palette;
605
606    Shader *m_shader;
607    ShaderAttrib m_vertexattrib, m_texattrib;
608    ShaderUniform m_texeluni, m_screenuni, m_zoomuni;
609
610    VertexDeclaration *m_vdecl;
611    VertexBuffer *m_vbo, *m_tbo;
612#if defined USE_D3D9
613    IDirect3DTexture9 *m_tex;
614#elif defined _XBOX
615    D3DTexture *m_tex;
616#else
617    GLuint m_texid;
618#endif
619    int m_frame, m_slices, m_dirty[4];
620    bool m_ready, m_drag;
621
622    dcmplx m_center, m_translate;
623    double m_zoom_speed, m_radius;
624    vec4 m_texel_settings, m_screen_settings;
625    mat4 m_zoom_settings;
626    dcmplx m_deltashift[4];
627    double m_deltascale[4];
628
629    /* Worker threads */
630    Thread *m_threads[MAX_THREADS];
631    Queue<int> m_spawnqueue, m_jobqueue, m_donequeue;
632
633    /* Debug information */
634#if !defined __native_client__
635    Text *m_centertext, *m_mousetext, *m_zoomtext;
636#endif
637};
638
639int main(int argc, char **argv)
640{
641    Application app("Tutorial 3: Fractal", ivec2(640, 480), 60.0f);
642
643#if defined _MSC_VER && !defined _XBOX
644    _chdir("..");
645#elif defined _WIN32 && !defined _XBOX
646    _chdir("../..");
647#endif
648
649    new DebugFps(5, 5);
650    new Fractal(ivec2(640, 480));
651    //new DebugRecord("fractalol.ogm", 60.0f);
652
653    app.Run();
654
655    return EXIT_SUCCESS;
656}
657
Note: See TracBrowser for help on using the repository browser.