I have two functions, one takes a file name and a pointer to a GLuint. It appears the textures get loaded OK, but I get a segmentation fault in a call to glBindTexture();
Code:
#include "textureloader.h"
bool loadTexture(const char* fname, GLuint *texid, GLuint internalF, GLuint byteF)
{
SDL_Surface* imgFile;
if ((imgFile = IMG_Load(fname)) == NULL) //Load Image to an SDL surface
{
return false;
}
glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
glGenTextures(1,texid);
glBindTexture(GL_TEXTURE_2D,*texid); //Segementation Fault NOT here
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,0,internalF,imgFile->w, imgFile->h,0,byteF,GL_UNSIGNED_BYTE,imgFile->pixels);
SDL_FreeSurface(imgFile);
return true;
}
bool loadEmUp(char* fname, GLuint *texsToLoad)
{
std::string texpath;
int texnum;
GLuint internalformat, byteformat;
int texcount = 0;
std::ifstream texfile (fname);
if (texfile.is_open())
{
texfile >> texnum; //Now holds the number of textures
texsToLoad = new GLuint[texnum]; //Create room for the new textures
while (texcount < texnum)
{
texfile >> texpath;
texfile >> internalformat;
texfile >> byteformat;
if (!loadTexture(texpath.c_str(), &texsToLoad[texcount], internalformat, byteformat))
{
std::cout << "Failed to load image: " << texpath << std::endl;
return false;
}
else std::cout << "Loaded " << texpath << std::endl;
texcount++;
}
texfile.close();
}
else return false;
std::cout << texnum << " Textures Loaded" << std::endl;
return true;
}
The file with textures names in it is something like this:
Code:
2
media/img1.png 3 3
media/img2.png 3 3
Segmentation fault does not occur in the the loadTexture() function, it's in a different one. Also: GLuint *texsToLoad is declared as a global where the loadEmUp() functions is called from, but it's named myTextures.