Changeset 701


Ignore:
Timestamp:
Feb 28, 2011, 3:26:18 AM (11 years ago)
Author:
sam
Message:

Add texturing support to DebugQuad. Works with OpenGL (fixed pipeline
and programmable pipeline versions) and OpenGL ES 1.x for now.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/debugquad.cpp

    r699 r701  
    2424
    2525/*
    26  * DebugTri implementation class
     26 * DebugQuad implementation class
    2727 */
    2828
    29 class DebugTriData
    30 {
    31     friend class DebugTri;
     29class DebugQuadData
     30{
     31    friend class DebugQuad;
    3232
    3333private:
    3434    int initialised;
    3535#if defined HAVE_GL_2X || defined HAVE_GLES_2X
    36     GLuint buflist[2];
     36    GLuint buflist[3];
    3737    Shader *shader;
    3838#elif defined HAVE_GL_1X || defined HAVE_GLES_1X
    39     GLuint buflist[2];
    40 #endif
     39    GLuint buflist[3];
     40#endif
     41    GLuint texlist[1];
     42    uint8_t image[1][32 * 32 * 4];
    4143};
    4244
    4345/*
    44  * Public DebugTri class
     46 * Public DebugQuad class
    4547 */
    4648
    47 DebugTri::DebugTri()
    48   : data(new DebugTriData())
     49DebugQuad::DebugQuad()
     50  : data(new DebugQuadData())
    4951{
    5052    data->initialised = 0;
    5153}
    5254
    53 void DebugTri::TickGame(float deltams)
     55void DebugQuad::TickGame(float deltams)
    5456{
    5557    Entity::TickGame(deltams);
    5658}
    5759
    58 void DebugTri::TickDraw(float deltams)
     60void DebugQuad::TickDraw(float deltams)
    5961{
    6062    Entity::TickDraw(deltams);
     
    6567        {
    6668#if defined HAVE_GL_2X || defined HAVE_GLES_2X
    67             glDeleteBuffers(2, data->buflist);
     69            glDeleteBuffers(3, data->buflist);
    6870            Shader::Destroy(data->shader);
    6971#elif defined HAVE_GL_1X || defined HAVE_GLES_1X
    70             glDeleteBuffers(2, data->buflist);
    71 #endif
     72            glDeleteBuffers(3, data->buflist);
     73#endif
     74            glDeleteTextures(1, data->texlist);
    7275            data->initialised = 0;
    7376        }
     
    7679    {
    7780#if defined HAVE_GL_2X || defined HAVE_GLES_2X
    78         glGenBuffers(2, data->buflist);
     81        glGenBuffers(3, data->buflist);
    7982
    8083        static char const *vertexshader =
    8184            "#version 130\n"
    8285            "in vec2 in_Position;\n"
    83             "in vec3 in_Color;\n"
    84             "out vec3 pass_Color;\n"
     86            "in vec4 in_Color;\n"
     87            "in vec2 in_TexCoord;\n"
     88            "out vec4 pass_Color;\n"
    8589            "void main()\n"
    8690            "{\n"
    8791            "    gl_Position = vec4(in_Position, 0.0f, 1.0f);\n"
     92            "    gl_TexCoord[0] = vec4(in_TexCoord, 0.0, 0.0);\n"
    8893            "    pass_Color = in_Color;\n"
    8994            "}\n";
    9095        static char const *fragmentshader =
    9196            "#version 130\n"
    92             "in vec3 pass_Color;\n"
     97            "in vec4 pass_Color;\n"
     98            "uniform sampler2D in_Texture;\n"
    9399            "void main()\n"
    94100            "{\n"
    95             "    gl_FragColor = vec4(pass_Color, 1.0);\n"
     101            "    vec4 col = pass_Color;\n"
     102            "    vec4 tex = texture2D(in_Texture, vec2(gl_TexCoord[0]));\n"
     103            "    gl_FragColor = col * tex;\n"
    96104            "}\n";
    97105        data->shader = Shader::Create(vertexshader, fragmentshader);
    98106#elif defined HAVE_GL_1X || defined HAVE_GLES_1X
    99         glGenBuffers(2, data->buflist);
    100 #endif
     107        glGenBuffers(3, data->buflist);
     108#endif
     109        glGenTextures(1, data->texlist);
     110
     111        glActiveTexture(GL_TEXTURE0);
     112        glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
     113        for (int j = 0; j < 32; j++)
     114            for (int i = 0; i < 32; i++)
     115            {
     116                uint8_t wb = (((i / 2) ^ (j / 2)) & 1) * 0xff;
     117                data->image[0][(j * 32 + i) * 4 + 0] = wb;
     118                data->image[0][(j * 32 + i) * 4 + 1] = wb;
     119                data->image[0][(j * 32 + i) * 4 + 2] = wb;
     120                data->image[0][(j * 32 + i) * 4 + 3] = 255;
     121            }
     122        /* Use GL_RGBA instead of 4 for the internal format (Android) */
     123        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 32, 32, 0,
     124                     GL_RGBA, GL_UNSIGNED_BYTE, data->image[0]);
     125        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     126        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    101127
    102128        data->initialised = 1;
     
    109135    };
    110136
     137    /* Using only 3 components breaks on Android for some reason. */
    111138    static GLfloat const cols[6][4] =
    112139    {
    113         { 0.8f, 0.2f, 0.2f, 1.0f }, { 0.2f, 0.2f, 0.8f, 1.0f }, { 0.8f, 0.8f, 0.2f, 1.0f },
    114         { 0.8f, 0.8f, 0.2f, 1.0f }, { 0.2f, 0.2f, 0.8f, 1.0f }, { 0.2f, 0.8f, 0.2f, 1.0f },
     140        { 0.8f, 0.2f, 0.2f, 1.0f },
     141        { 0.2f, 0.2f, 0.8f, 1.0f },
     142        { 0.8f, 0.8f, 0.2f, 1.0f },
     143
     144        { 0.8f, 0.8f, 0.2f, 1.0f },
     145        { 0.2f, 0.2f, 0.8f, 1.0f },
     146        { 0.2f, 0.8f, 0.2f, 1.0f },
    115147    };
    116148
     149    static GLfloat const tcs[6][2] =
     150    {
     151        { 0.0f, 1.0f }, { 1.0f, 1.0f }, { 0.0f, 0.0f },
     152        { 0.0f, 0.0f }, { 1.0f, 1.0f }, { 1.0f, 0.0f },
     153    };
     154
    117155#if defined HAVE_GL_2X || defined HAVE_GLES_2X
    118156    data->shader->Bind();
    119     GLuint attr_pos, attr_col;
     157    GLuint attr_pos, attr_col, attr_tex;
    120158    attr_pos = data->shader->GetAttribLocation("in_Position");
    121159    attr_col = data->shader->GetAttribLocation("in_Color");
     160    attr_tex = data->shader->GetAttribLocation("in_TexCoord");
    122161
    123162    glEnableVertexAttribArray(attr_pos);
    124163    glEnableVertexAttribArray(attr_col);
     164    glEnableVertexAttribArray(attr_tex);
    125165
    126166    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
     
    132172    glVertexAttribPointer(attr_col, 4, GL_FLOAT, GL_FALSE, 0, 0);
    133173
     174    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
     175    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), tcs, GL_STATIC_DRAW);
     176    glVertexAttribPointer(attr_tex, 2, GL_FLOAT, GL_FALSE, 0, 0);
     177
    134178    glDrawArrays(GL_TRIANGLES, 0, 6);
    135179
    136180    glDisableVertexAttribArray(attr_pos);
    137181    glDisableVertexAttribArray(attr_col);
    138 #elif defined HAVE_GL_1X || defined HAVE_GLES_1X
     182    glDisableVertexAttribArray(attr_tex);
     183#elif defined HAVE_GL_1X || defined HAVE_GLES_1X
     184    /* Reset all model-view-projection matrices */
    139185    glMatrixMode(GL_PROJECTION);
    140186    glPushMatrix();
     
    144190    glLoadIdentity();
    145191
    146     glDisableClientState(GL_NORMAL_ARRAY);
    147     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    148     glDisable(GL_TEXTURE_2D);
     192    /* Set up state machine */
     193    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
     194    glEnable(GL_TEXTURE_2D);
    149195    glEnableClientState(GL_VERTEX_ARRAY);
    150196    glEnableClientState(GL_COLOR_ARRAY);
    151197
     198    glActiveTexture(GL_TEXTURE0);
     199    glBindTexture(GL_TEXTURE_2D, data->texlist[0]);
     200
     201    /* Bind vertex, color and texture coordinate buffers */
    152202#if defined HAVE_GL_1X
    153203    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
     
    158208    glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(GLfloat), cols, GL_STATIC_DRAW);
    159209    glColorPointer(4, GL_FLOAT, 0, NULL);
     210
     211    glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
     212    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), tcs, GL_STATIC_DRAW);
     213    glTexCoordPointer(2, GL_FLOAT, 0, NULL);
    160214#else
    161215    glVertexPointer(2, GL_FLOAT, 0, verts);
    162216    glColorPointer(4, GL_FLOAT, 0, cols);
    163 #endif
    164 
     217    glTexCoordPointer(2, GL_FLOAT, 0, tcs);
     218#endif
     219
     220    /* Draw arrays */
    165221    glDrawArrays(GL_TRIANGLES, 0, 6);
    166222
     223    /* Disable state machine features */
    167224    glDisableClientState(GL_VERTEX_ARRAY);
    168225    glDisableClientState(GL_COLOR_ARRAY);
    169 
     226    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
     227
     228    /* Restore matrices */
    170229    glMatrixMode(GL_PROJECTION);
    171230    glPopMatrix();
     
    175234}
    176235
    177 DebugTri::~DebugTri()
     236DebugQuad::~DebugQuad()
    178237{
    179238    delete data;
  • trunk/src/debugquad.h

    r699 r701  
    1010
    1111//
    12 // The DebugTri class
    13 // ------------------
     12// The DebugQuad class
     13// -------------------
    1414//
    1515
     
    2222{
    2323
    24 class DebugTriData;
     24class DebugQuadData;
    2525
    26 class DebugTri : public Entity
     26class DebugQuad : public Entity
    2727{
    2828public:
    29     DebugTri();
    30     virtual ~DebugTri();
     29    DebugQuad();
     30    virtual ~DebugQuad();
    3131
    3232protected:
     
    3535
    3636private:
    37     DebugTriData *data;
     37    DebugQuadData *data;
    3838};
    3939
Note: See TracChangeset for help on using the changeset viewer.