kelechi96 0 Light Poster

Basically I have created a sprite sheet/tileset with dimentions of 320 x 2048, the tiles are each 32 x 32. I've been able to load individual files with textures in them but I don't seem to be able to load textures from a sprite sheet. Any help ?

Here is the code I'm struggling with:

This is my tiling function

float VertexX[4], VertexY[4];
int TexcoordX, TexcoordY;
for (int y = 0; y < WorldSizey; ++y)
{
    for (int x = 0; x < WorldSizex; ++x)
    {
        CalculateTextureSheetCoords(TileData[0][y][x],
            TilesetX,32,TexcoordX,TexcoordY);

        CalculateTextureVertexCoords(TilesetX,TilesetY,TexcoordX,
            TexcoordY,32,VertexX[0],VertexY[0],VERTEX_LOWER_LEFT);
        CalculateTextureVertexCoords(TilesetX,TilesetY,TexcoordX,
            TexcoordY,32,VertexX[1],VertexY[1],VERTEX_UPPER_LEFT);
        CalculateTextureVertexCoords(TilesetX,TilesetY,TexcoordX,
            TexcoordY,32,VertexX[2],VertexY[2],VERTEX_UPPER_RIGHT);
        CalculateTextureVertexCoords(TilesetX,TilesetY,TexcoordX,
            TexcoordY,32,VertexX[3],VertexY[3],VERTEX_LOWER_RIGHT);

        glEnable(GL_TEXTURE_2D);
        glColor4fv(TextureColour);
        glBindTexture(GL_TEXTURE_2D,TILESET);
        glBegin(GL_QUADS);
        glTexCoord2f(VertexX[0],VertexY[0]);
        glVertex2f(x/8,(WorldSizey-(y+1))/8); //Lower left
        glTexCoord2f(VertexX[1],VertexY[1]);
        glVertex2f(x/8,(WorldSizey-y)/8); //Upper left
        glTexCoord2f(VertexX[2],VertexY[2]);
        glVertex2f((x+1)/8,(WorldSizey-y)/8); //Upper Right
        glTexCoord2f(VertexX[3],VertexY[3]);
        glVertex2f((x+1)/8,(WorldSizey-(y+1))/8); //Lower Right
        glEnd();
        glDisable(GL_TEXTURE_2D);
    }
}

And Here's my functions which calculate the coordinates that should work to find the textures but all I'm getting is blank white squares.

enum VertexMode {VERTEX_LOWER_LEFT, VERTEX_UPPER_LEFT, VERTEX_UPPER_RIGHT, VERTEX_LOWER_RIGHT};
void CalculateTextureVertexCoords(float in_ImageSizeX, float in_ImageSizeY,
                                  float in_TextureCoordX, float in_TextureCoordY,
                                  float in_TextureSize,
                                  float &out_VertexCoordX, float &out_VertexCoordY,
                                  int in_VertexMode)
{
    switch (in_VertexMode)
    {
    case VERTEX_LOWER_LEFT:
        out_VertexCoordX = (in_TextureSize * in_TextureCoordX)/in_ImageSizeX;
        out_VertexCoordY = ( 1 - ((in_TextureSize * (in_TextureCoordY + 1))/in_ImageSizeY));
        break;
    case VERTEX_UPPER_LEFT:
        out_VertexCoordX = (in_TextureSize * in_TextureCoordX)/in_ImageSizeX;
        out_VertexCoordY = ( 1 - ((in_TextureSize * in_TextureCoordY)/in_ImageSizeY));
        break;
    case VERTEX_UPPER_RIGHT:
        out_VertexCoordX = ((in_TextureSize * (in_TextureCoordX + 1))/in_ImageSizeX);
        out_VertexCoordY = ( 1 - ((in_TextureSize * in_TextureCoordY)/in_ImageSizeY));
        break;
    case VERTEX_LOWER_RIGHT:
        out_VertexCoordX = (in_TextureSize * (in_TextureCoordX + 1))/in_ImageSizeX;
        out_VertexCoordY = ( 1 - ((in_TextureSize * (in_TextureCoordY + 1))/in_ImageSizeY));
        break;
    }
}

void CalculateTextureVertexCoordsABS(int in_ImageSizeX, int in_ImageSizeY,
                                     int in_TextureCoordX, int in_TextureCoordY,
                                     int in_TextureSize,
                                     int &out_VertexCoordX, int &out_VertexCoordY,
                                     int in_VertexMode)
{
    switch (in_VertexMode)
    {
    case VERTEX_LOWER_LEFT:
        out_VertexCoordX = (in_ImageSizeX - (in_ImageSizeX - in_TextureSize * in_TextureCoordX));
        out_VertexCoordY = (in_ImageSizeY - in_TextureSize * (in_TextureCoordY + 1));
        break;
    case VERTEX_UPPER_LEFT:
        out_VertexCoordX = (in_ImageSizeX - (in_ImageSizeX - in_TextureSize * in_TextureCoordX));
        out_VertexCoordY = (in_ImageSizeY - in_TextureSize * in_TextureCoordY);
        break;
    case VERTEX_UPPER_RIGHT:
        out_VertexCoordX = (in_ImageSizeX - (in_ImageSizeX - in_TextureSize * (in_TextureCoordX + 1)));
        out_VertexCoordY = (in_ImageSizeY - in_TextureSize * in_TextureCoordY);
        break;
    case VERTEX_LOWER_RIGHT:
        out_VertexCoordX = (in_ImageSizeX - (in_ImageSizeX - in_TextureSize * (in_TextureCoordX + 1)));
        out_VertexCoordY = (in_ImageSizeY - in_TextureSize * (in_TextureCoordY + 1));
        break;
    }
}

void CalculateTextureSheetCoords(int in_TextureNumber, int in_ImageSizeX,
                                 int in_TextureSize, int &out_TextureCoordX,
                                 int &out_TextureCoordY)
{
    if (in_TextureNumber != 0)
    {
        int TexturesPerRow = in_ImageSizeX/in_TextureSize;
        out_TextureCoordY = floor(((double)in_TextureNumber - 1) / (double)TexturesPerRow);
        out_TextureCoordX = (((in_TextureNumber - 1) - TexturesPerRow * out_TextureCoordY)  % TexturesPerRow);

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.