Changeset 1058


Ignore:
Timestamp:
Nov 9, 2011, 9:42:53 AM (11 years ago)
Author:
gary
Message:

tutorial: port Mandelbrot tutorial to the PS3.

File:
1 edited

Legend:

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

    r1056 r1058  
    5353        m_angle += deltams * 0.0005f;
    5454
    55         cmplx r0(cosf(m_angle), sinf(m_angle));
     55        cmplx r0(cosf(m_angle), 0.8f * sinf(m_angle));
    5656        for (int j = 0; j < m_size.y; j++)
    5757        for (int i = 0; i < m_size.x; i++)
    5858        {
    59             float const maxlen = 128.0f;
    60             int const maxiter = 20;
    61 
    62             cmplx x0(4.0f / m_size.x * i - 2.5f, 3.0f / m_size.y * j - 1.5f);
    63             cmplx r = x0;
     59            float const maxlen = 32.0f;
     60            int const colors = 16;
     61            int const maxiter = 30;
     62
     63            cmplx x0(4.0f / m_size.x * i - 2.0f, 3.0f / m_size.y * j - 1.5f);
     64            cmplx r = x0 * r0;
    6465            cmplx z;
    6566            int iter = maxiter;
     
    6768                --iter;
    6869
    69             float f = iter;
     70            float f = iter % colors;
    7071            float n = z.len();
    7172            f += logf(logf(n) / logf(maxlen)) / logf(2.0f);
     
    7374            if (iter)
    7475            {
    75                 uint8_t red = 255 - f * 11;
    76                 uint8_t green = 255 - f * 11;
    77                 uint8_t blue = (f * 23 > 255) ? 511 - f * 23 : 255;
     76                uint8_t red = 255 - f * (255.0f / (colors + 1));
     77                uint8_t green = 255 - f * (255.0f / (colors + 1));
     78                uint8_t blue = (f * 512.0f / (colors + 1) > 255)
     79                             ? 511 - (f * 512.0f / (colors + 1)) : 255;
    7880                m_pixels[j * m_size.x + i] = u8vec4(red, green, blue, 0);
    7981            }
     
    115117            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_size.x, m_size.y, 0,
    116118                         GL_RGBA, GL_UNSIGNED_BYTE, m_pixels);
    117             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    118             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     119            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     120            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    119121
    120122            m_shader = Shader::Create(
     123#if !defined __CELLOS_LV2__
    121124                "#version 120\n"
    122125                "attribute vec2 in_TexCoord;\n"
     
    131134                "void main(void) {"
    132135                "    gl_FragColor = texture2D(in_Texture, gl_TexCoord[0].xy);"
    133                 "}");
     136                "}"
     137#else
     138                "void main(float4 in_Position : POSITION,"
     139                "          float2 in_TexCoord : TEXCOORD0,"
     140                "          out float4 out_Position : POSITION,"
     141                "          out float2 out_TexCoord : TEXCOORD0)"
     142                "{"
     143                "    out_TexCoord = in_TexCoord;"
     144                "    out_Position = in_Position;"
     145                "}",
     146
     147                "void main(float2 in_TexCoord : TEXCOORD0,"
     148                "          uniform sampler2D tex,"
     149                "          out float4 out_FragColor : COLOR)"
     150                "{"
     151                "    out_FragColor = tex2D(tex, in_TexCoord);"
     152                "}"
     153#endif
     154            );
    134155            m_vertexattrib = m_shader->GetAttribLocation("in_Vertex");
    135156            m_texattrib = m_shader->GetAttribLocation("in_TexCoord");
     
    157178        glBindTexture(GL_TEXTURE_2D, m_texid);
    158179        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_size.x, m_size.y,
    159                         GL_RGBA, GL_UNSIGNED_BYTE, m_pixels);
     180#if !defined __CELLOS_LV2__
     181                        GL_RGBA, GL_UNSIGNED_BYTE,
     182#else
     183                        /* The PS3 is big-endian */
     184                        GL_RGBA, GL_UNSIGNED_INT_8_8_8_8,
     185#endif
     186                        m_pixels);
    160187
    161188        m_shader->Bind();
     
    173200        //glVertexAttribPointer(m_vertexattrib, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    174201#else
    175         //glEnableClientState(GL_VERTEX_ARRAY);
    176         //glVertexPointer(2, GL_FLOAT, 0, vertices);
    177         //glEnableClientState(GL_VERTEX_ARRAY);
    178         //glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
     202        glEnableClientState(GL_VERTEX_ARRAY);
     203        glVertexPointer(2, GL_FLOAT, 0, vertices);
     204        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
     205        glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
    179206#endif
    180207
     
    190217        //glDisableVertexAttribArray(m_texattrib);
    191218#else
    192         //glDisableClientState(GL_VERTEX_ARRAY);
    193         //glDisableClientState(GL_TEXTURE_COORD_ARRAY);
     219        glDisableClientState(GL_VERTEX_ARRAY);
     220        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    194221#endif
    195222    }
Note: See TracChangeset for help on using the changeset viewer.