Ignore:
Timestamp:
Jul 8, 2010, 2:00:13 AM (11 years ago)
Author:
sam
Message:

Rendering starts to work again. There is no Z-sorting yet.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/tileset.cpp

    r75 r79  
    4242
    4343    GLuint texture[1];
    44     GLuint buflist[3];
    4544};
    4645
     
    7877    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    7978    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    80 
    81     /* Three GPU buffers */
    82     glGenBuffers(3, data->buflist);
    8379}
    8480
     
    8682{
    8783    glDeleteTextures(1, data->texture);
    88     glDeleteBuffers(3, data->buflist);
    8984
    9085    free(data->tiles);
     
    108103}
    109104
    110 void TileSet::AddTile(int n, int x, int y, int z)
     105void TileSet::BlitTile(uint32_t id, int x, int y)
    111106{
    112     if ((data->ntiles % 1024) == 0)
    113     {
    114         data->tiles = (int *)realloc(data->tiles,
    115                                      (data->ntiles + 1024) * 4 * sizeof(int));
    116     }
     107    float tx = .0625f * (id & 0xf);
     108    float ty = .0625f * ((id >> 4) & 0xf);
    117109
    118     data->tiles[4 * data->ntiles] = n;
    119     data->tiles[4 * data->ntiles + 1] = x;
    120     data->tiles[4 * data->ntiles + 2] = y;
    121     data->tiles[4 * data->ntiles + 3] = z;
    122 
    123     data->ntiles++;
     110    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
     111    glBegin(GL_QUADS);
     112        glTexCoord2f(tx, ty);
     113        glVertex2f(x, y);
     114        glTexCoord2f(tx + .0625f, ty);
     115        glVertex2f(x + 32, y);
     116        glTexCoord2f(tx + .0625f, ty + .0625f);
     117        glVertex2f(x + 32, y + 32);
     118        glTexCoord2f(tx, ty + .0625f);
     119        glVertex2f(x, y + 32);
     120    glEnd();
    124121}
    125122
    126 void TileSet::Render()
    127 {
    128     /* Sort tiles */
    129     qsort(data->tiles, data->ntiles, 4 * sizeof(int), TileSetData::Compare);
    130 
    131     /* Texture coord buffer */
    132     float uvs[8 * data->ntiles];
    133     for (int n = 0; n < data->ntiles; n++)
    134     {
    135         int tile = data->tiles[4 * n];
    136         float ty = .0625f * (tile / 16);
    137         float tx = .0625f * (tile % 16);
    138         uvs[8 * n + 0] = tx;
    139         uvs[8 * n + 1] = ty;
    140         uvs[8 * n + 2] = tx + .0625f;
    141         uvs[8 * n + 3] = ty;
    142         uvs[8 * n + 4] = tx + .0625f;
    143         uvs[8 * n + 5] = ty + .0625f;
    144         uvs[8 * n + 6] = tx;
    145         uvs[8 * n + 7] = ty + .0625f;
    146     }
    147     glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
    148     glBufferData(GL_ARRAY_BUFFER,
    149                  8 * data->ntiles * sizeof(float), uvs, GL_STATIC_DRAW);
    150 
    151     /* Vertex buffer */
    152     float vertices[8 * data->ntiles];
    153     for (int n = 0; n < data->ntiles; n++)
    154     {
    155         int x = data->tiles[4 * n + 1];
    156         int y = data->tiles[4 * n + 2];
    157         vertices[8 * n + 0] = x;
    158         vertices[8 * n + 1] = y;
    159         vertices[8 * n + 2] = x + 32;
    160         vertices[8 * n + 3] = y;
    161         vertices[8 * n + 4] = x + 32;
    162         vertices[8 * n + 5] = y + 32;
    163         vertices[8 * n + 6] = x;
    164         vertices[8 * n + 7] = y + 32;
    165     }
    166     glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
    167     glBufferData(GL_ARRAY_BUFFER, 8 * data->ntiles * sizeof(float),
    168                  vertices, GL_STATIC_DRAW);
    169 
    170     /* Index buffer */
    171     int indices[4 * data->ntiles];
    172     for (int n = 0; n < 4 * data->ntiles; n++)
    173         indices[n] = n;
    174     glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
    175     glBufferData(GL_ARRAY_BUFFER, 4 * data->ntiles * sizeof(int),
    176                  indices, GL_STATIC_DRAW);
    177 
    178     /* Draw everything */
    179     glEnableClientState(GL_VERTEX_ARRAY);
    180     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    181     glEnableClientState(GL_INDEX_ARRAY);
    182 
    183     glBindTexture(GL_TEXTURE_2D, data->texture[0]);
    184 
    185     glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
    186     glVertexPointer(2, GL_FLOAT, 0, NULL);
    187     glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
    188     glTexCoordPointer(2, GL_FLOAT, 0, NULL);
    189     glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
    190     glIndexPointer(GL_INT, 0, NULL);
    191 
    192     glDrawArrays(GL_QUADS, 0, 4 * data->ntiles);
    193 
    194     glDisableClientState(GL_VERTEX_ARRAY);
    195     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    196     glDisableClientState(GL_INDEX_ARRAY);
    197 
    198     /* Empty our shit */
    199     free(data->tiles);
    200     data->tiles = NULL;
    201     data->ntiles = 0;
    202 }
    203 
Note: See TracChangeset for help on using the changeset viewer.