A model I’m loading might or might not use multiple textures (in tga format) for different segments. I don’t know in advance how many it will use. So what I do is I count the number of textures at loadtime, then create an std::vector<GLuint> of size numTextures to hold all the texture ids. Then I cycle through the textures and load them one at a time, using the SOIL library.
The problem comes when I try to use them. If I do glBindTexture(GL_TEXTURE_2D, texIds[0]) everything works fine, but if I try to use a texture other than the first, glBindTexture(GL_TEXTURE_2D, texIds[1]) I get a solid black shape, as if the texture isn’t loading properly. I know it’s not a problem with the texture file itself, because if I switch the order of them so it’s the first one loaded, it works fine, and still the others in the list are broken.
So is there some fundamental problem with storing texture ids in an std::vector that I, as a newbie, don’t realize? Or is it some peculiarity of SOIL? Or is it something else?
Here’s the code for generating the texture list (texList) is a list of just the texture filenames, for example “texture.tga”):
vector<GLuint> modelTools::loadTextures(vector<string> texList, string filePath){
string directory;
const size_t last_slash_idx = filePath.rfind('/');
if (string::npos != last_slash_idx){
directory = filePath.substr(0, last_slash_idx);
}
int numTextures = texList.size();
vector<GLuint> texIdList;
texIdList.reserve(numTextures);
texIdList.resize(numTextures, 0);
int curIter = 0;
int maxIter = numTextures;
while (curIter < maxIter){
string curDir = directory + '/' + texList[curIter];
texIdList[curIter] = SOIL_load_OGL_texture(curDir.c_str(), SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS);
curIter++;
}
return texIdList;
}