0
\$\begingroup\$

Some Tiles in my map are drawn wrong, I think it might be a indexing problem. Here's the output :

Map output

some tiles just look wrong!! I'm not sure where i'm going wrong with my code, so please take a look.

Here is where I build the map :

void SetData(GLfloat *data, unsigned int counter, GLfloat value)
{
    data[counter] = value;
}

void uSetData(unsigned int *data, unsigned int counter, unsigned int value)
{
    data[counter] = value;
}

void Layer_Build(Layer *layer)
{
    unsigned int size = layer->width * layer->height;

    unsigned int vDataCounter = size * 12,
                 uDataCounter = size * 8,
                 iDataCounter = size * 6,
                 cDataCounter = size * 16;

    GLfloat *vData = (GLfloat*)calloc(vDataCounter , sizeof(GLfloat));
    GLfloat *uData = (GLfloat*)calloc(uDataCounter, sizeof(GLfloat));
    unsigned int *iData = (unsigned int*)calloc(iDataCounter, sizeof(unsigned int));
    GLfloat *cData = (GLfloat*)calloc(cDataCounter, sizeof(GLfloat));

    unsigned int vCounter = 0,
                 uCounter = 0,
                 iCounter = 0,
                 cCounter = 0;

    for (unsigned int y = 0; y < layer->height; y++)
    {
        for (unsigned int x = 0; x < layer->width; x++)
        {
            struct Tile tile = Layer_GetTile(layer, x, y);

            if (tile.gid != 0)
            {
                Tileset *tileset = Tileset_Find(layer->terrain, tile.gid);

                Rect cell = Tileset_Cell(tileset, tile.gid);

                Vector2 auxPos;
                auxPos.x = x *  (float)tileset->width;
                auxPos.y = y * -(float)tileset->height;

                float cellWidth = (float)tileset->width,
                      cellHeight = (float)tileset->height;

                SetData(vData, vCounter,     -cellWidth + auxPos.x);
                SetData(vData, vCounter + 1, -cellHeight + auxPos.y);
                SetData(vData, vCounter + 2,  layer->depth);

                SetData(vData, vCounter + 3, cellWidth + auxPos.x);
                SetData(vData, vCounter + 4, -cellHeight + auxPos.y);
                SetData(vData, vCounter + 5,  layer->depth);

                SetData(vData, vCounter + 6, cellWidth + auxPos.x);
                SetData(vData, vCounter + 7, cellHeight + auxPos.y);
                SetData(vData, vCounter + 8, layer->depth);

                SetData(vData, vCounter + 9, -cellWidth + auxPos.x);
                SetData(vData, vCounter + 10, cellHeight + auxPos.y);
                SetData(vData, vCounter + 11, layer->depth);

                vCounter += 12;

                float textureWidth = tileset->texture->width,
                      textureHeight = tileset->texture->height;

                float rightCell = cell.right / textureWidth,
                      leftCell = cell.left / textureWidth,
                      topCell = cell.top / textureHeight,
                      bottomCell = cell.bottom / textureHeight;

                SetData(uData, uCounter, leftCell);
                SetData(uData, uCounter + 1, bottomCell);

                SetData(uData, uCounter + 2, rightCell);
                SetData(uData, uCounter + 3, bottomCell);

                SetData(uData, uCounter + 4, rightCell);
                SetData(uData, uCounter + 5, topCell);

                SetData(uData, uCounter + 6, leftCell);
                SetData(uData, uCounter + 7, topCell);

                uCounter += 8;

                uSetData(iData, iCounter,     0 + iCounter);
                uSetData(iData, iCounter + 1, 1 + iCounter);
                uSetData(iData, iCounter + 2, 2 + iCounter);
                uSetData(iData, iCounter + 3, 0 + iCounter);
                uSetData(iData, iCounter + 4, 2 + iCounter);
                uSetData(iData, iCounter + 5, 3 + iCounter);

                iCounter += 6;

                for (int i = 0; i < 16; i++)
                    SetData(cData, cCounter + i, 1.0f);

                cCounter += 16;
            }
        }
    }

    layer->mesh = Mesh_Create(vData, vDataCounter, iData, iDataCounter, uData, uDataCounter, cData, cDataCounter);

    free(vData);
    free(uData);
    free(iData);
    free(cData);
}

Here is how the mesh is created and drawn :

struct Mesh *Mesh_Create(GLfloat vertices[], unsigned int vertexElements, GLuint indices[], unsigned int indexElements, GLfloat uvs[], unsigned int uvElements, GLfloat colors[], unsigned int colorElements)
{
    struct Mesh *mesh = (struct Mesh*)malloc(sizeof(struct Mesh));

    mesh->vertices = (GLfloat*)malloc(sizeof(GLfloat) * vertexElements);
    memcpy(mesh->vertices, vertices, sizeof(GLfloat) * vertexElements);

    mesh->uvs = (GLfloat*)malloc(sizeof(GLfloat) * uvElements);
    memcpy(mesh->uvs, uvs, sizeof(GLfloat) * uvElements);

    mesh->indices = (GLuint*)malloc(sizeof(GLuint) * indexElements);
    memcpy(mesh->indices, indices, sizeof(GLuint) * indexElements);

    mesh->colors = (GLfloat*)malloc(sizeof(GLfloat) * colorElements);
    memcpy(mesh->colors, colors, sizeof(GLfloat) * colorElements);

    //mesh->elementCount = vertexElements * 3;
    mesh->elementCount = indexElements;

    glGenVertexArrays(1, &mesh->vao);
    glBindVertexArray(mesh->vao);

    glGenBuffers(1, &mesh->vbo[VERTEXBUFFER]);
    glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo[VERTEXBUFFER]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertexElements, &mesh->vertices[0], GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, FALSE, 0, NULL);
    glEnableVertexAttribArray(0);

    glGenBuffers(1, &mesh->vbo[TEXTUREBUFFER]);
    glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo[TEXTUREBUFFER]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * uvElements, &mesh->uvs[0], GL_STATIC_DRAW);
    glVertexAttribPointer(1, 2, GL_FLOAT, FALSE, 0, NULL);
    glEnableVertexAttribArray(1);

    glGenBuffers(1, &mesh->vbo[INDICESBUFFER]);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->vbo[INDICESBUFFER]);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * indexElements, &mesh->indices[0], GL_STATIC_DRAW);

    glGenBuffers(1, &mesh->vbo[COLORSBFFER]);
    glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo[COLORSBFFER]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * colorElements, &mesh->colors[0], GL_STATIC_DRAW);
    glVertexAttribPointer(2, 4, GL_FLOAT, FALSE, 0, NULL);
    glEnableVertexAttribArray(2);

    return mesh;
}

void Mesh_Render(struct Mesh *mesh)
{
    glBindVertexArray(mesh->vao);
    glDrawElements(GL_TRIANGLES, mesh->elementCount, GL_UNSIGNED_INT, NULL);
}

Can you spt anything wrong?

It seems like the odd tiles are getting deformed...Take a look:

X Spaced

\$\endgroup\$
0
\$\begingroup\$

OK I found na answer...

void Layer_Build(Layer *layer)
{
    unsigned int size = layer->width * layer->height;

    unsigned int vDataCounter = size * 12,
                 uDataCounter = size * 8,
                 iDataCounter = size * 6,
                 cDataCounter = size * 16;

    GLfloat *vData = (GLfloat*)calloc(vDataCounter, sizeof(GLfloat));
    GLfloat *uData = (GLfloat*)calloc(uDataCounter, sizeof(GLfloat));
    unsigned int *iData = (unsigned int*)calloc(iDataCounter, sizeof(unsigned int));
    GLfloat *cData = (GLfloat*)calloc(cDataCounter, sizeof(GLfloat));

    unsigned int vCounter = 0,
                 uCounter = 0,
                 iCounter = 0,
                 cCounter = 0,
                 iuCounter = 0;

    const float scale = 0.1f;
    Vector2 auxPos;
    for (unsigned int y = 0; y < layer->height; y++)
    {
        for (unsigned int x = 0; x < layer->width; x++)
        {
            struct Tile tile = Layer_GetTile(layer, x, y);

            auxPos.x = (float)x * (scale * 2);
            auxPos.y = (float)y * (-scale * 2);

            if (tile.gid != 0)
            {
                SetData(vData, vCounter,      auxPos.x - scale);
                SetData(vData, vCounter + 1,  auxPos.y - scale);
                SetData(vData, vCounter + 2,  layer->depth);

                SetData(vData, vCounter + 3,  auxPos.x + scale);
                SetData(vData, vCounter + 4,  auxPos.y - scale);
                SetData(vData, vCounter + 5,  layer->depth);

                SetData(vData, vCounter + 6,  auxPos.x + scale);
                SetData(vData, vCounter + 7,  auxPos.y + scale);
                SetData(vData, vCounter + 8,  layer->depth);

                SetData(vData, vCounter + 9,  auxPos.x - scale);
                SetData(vData, vCounter + 10, auxPos.y + scale);
                SetData(vData, vCounter + 11, layer->depth);

                Tileset *tileset = Tileset_Find(layer->terrain, tile.gid);
                Rect cell        = Tileset_Cell(tileset, tile.gid);

                float textureWidth  = (float)tileset->texture->width,
                      textureHeight = (float)tileset->texture->height,
                      rightCell     = cell.right  / textureWidth,
                      leftCell      = cell.left   / textureWidth,
                      topCell       = cell.top    / textureHeight,
                      bottomCell    = cell.bottom / textureHeight;

                SetData(uData, uCounter,     leftCell);
                SetData(uData, uCounter + 1, bottomCell);

                SetData(uData, uCounter + 2, rightCell);
                SetData(uData, uCounter + 3, bottomCell);

                SetData(uData, uCounter + 4, rightCell);
                SetData(uData, uCounter + 5, topCell);

                SetData(uData, uCounter + 6, leftCell);
                SetData(uData, uCounter + 7, topCell);

                uSetData(iData, iCounter,     0 + iuCounter);
                uSetData(iData, iCounter + 1, 1 + iuCounter);
                uSetData(iData, iCounter + 2, 2 + iuCounter);
                uSetData(iData, iCounter + 3, 0 + iuCounter);
                uSetData(iData, iCounter + 4, 2 + iuCounter);
                uSetData(iData, iCounter + 5, 3 + iuCounter);

                for (int i = 0; i < 16; i++)
                    SetData(cData, cCounter + i, 1.0f);

                vCounter += 12;
                uCounter += 8;
                iCounter += 6;
                iuCounter += 4;
                cCounter += 16;
            }
        }
    }

    layer->mesh = Mesh_Create(vData, vDataCounter, iData, iDataCounter, uData, uDataCounter, cData, cDataCounter);

    free(vData);
    free(uData);
    free(iData);
    free(cData);
}
\$\endgroup\$
  • \$\begingroup\$ This answer would be better if it explained what the problem was and how you solved it, so a reader doesn't have to do a line-by-line diff of the code to see what changed. \$\endgroup\$ – DMGregory Jan 13 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.