Ok so what I am trying to do is read tiles from a tileset, and then load them each into individual textures. The problem is that when I render these tiles, everything is just grey. This even happens when my tileset consists of only one tile. The code I am using to build the individual tiles from a larger tileset is this:
void WorldMap::loadTileset(const std::string & path)
{
AUX_RGBImageRec *TextureImage[1];
if(TextureImage[0]=loadBMP(path.c_str()))
{
// get tileset information
m_setWidth = TextureImage[0]->sizeX;
m_setHeight= TextureImage[0]->sizeY;
m_numTileW = m_setWidth/m_tileWidth;
m_numTileH = m_setHeight/m_tileHeight;
m_totTiles = m_numTileW * m_numTileH;
// allocate space for textures
m_texture = new GLuint[m_totTiles];
// generate textures
glGenTextures(m_totTiles, &m_texture[0]);
// pixel offset, assuming tile w and h are same
int offset = m_tileWidth / m_setWidth;
int curText = 0;
for(int h=0; h<m_numTileH; h++)
{
for(int w=0; w<m_numTileW; w++)
{
glPixelStoref(GL_UNPACK_ROW_LENGTH, m_setWidth);
glPixelStoref(GL_UNPACK_SKIP_PIXELS, w*offset);
glPixelStoref(GL_UNPACK_SKIP_ROWS, h*offset);
// create our textures
glBindTexture(GL_TEXTURE_2D, m_texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, m_tileWidth, m_tileHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
}
}
}
if (TextureImage[0]) // If Texture Exists
{
if (TextureImage[0]->data) // If Texture Image Exists
free(TextureImage[0]->data); // Free The Texture Image Memory
free(TextureImage[0]); // Free The Image Structure
}
}
The loadBMP is taken straight out of NeHe. I am parsing an XML file to get map info and tile size. My rendering code is:
void WorldMap::render()
{
for(int w=0; w<m_width; w++)
{
for(int h=0; h<m_height; h++)
{
int tile = m_mapData.at(w).at(h);
glBindTexture(GL_TEXTURE_2D, m_texture[0]);
glBegin(GL_QUADS);
glTexCoord3f(0.0f, 0.0f, 0.0f); glVertex3f(float(w), float(h), 0.0f);
glTexCoord3f(1.0f, 0.0f, 0.0f); glVertex3f(float(w + 1), float(h), 0.0f);
glTexCoord3f(1.0f, 1.0f, 0.0f); glVertex3f(float(w + 1), float(h + 1), 0.0f);
glTexCoord3f(0.0f, 1.0f, 0.0f); glVertex3f(float(w), float(h + 1), 0.0f);
glEnd();
}
}
}
In my init method I do enable openGL textures. Any ideas on what I am doing wrong? I was able to draw single textures fine, but I really want to be able to just load a tileset. Thanks in advance.