Need help - loading textures

Started by
3 comments, last by 3TATUK2 10 years, 6 months ago

Hi, I use opengl with SDL. this function works in my other programs. I have the function load a file in the same directory as the other files and it works.

Now my problem is that I use this in another program, and for some reason, it returns some absurdly large number. I coped over working files to see if it was the actual image files, and no it still doesn't work. I watch the value of id in this code. In my older program, it holds the value 1 before it returns. In my new program, it returns something like 3465094356 something stupid like that.


// Load a Texture and return the ID
unsigned int GRAPHICS::LoadTexture(const char *filename)
{
	SDL_Surface* img = IMG_Load(filename);

	if (img == NULL) 
	{
		OutputDebugString("\"");
		OutputDebugString(filename);
		OutputDebugString("\" failed to load.\n");
		return -1;
	}

	SDL_PixelFormat form = {NULL, 32, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff, 0, 255};
	SDL_Surface *img2 = SDL_ConvertSurface(img, &form, SDL_SWSURFACE);

	if (img2 == NULL)
	{
		OutputDebugString("\"");
		OutputDebugString(filename);
		OutputDebugString("\" failed to load. img2 == -1\n");
		return -1;
	}

	unsigned int id;
	glGenTextures(1, &id);
	glBindTexture(GL_TEXTURE_2D, id);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img->w, img->h, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, img2->pixels);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	SDL_FreeSurface(img);
	SDL_FreeSurface(img2);

	return id;
}

I don't understand at all why it would behave differently in each program.

Help is appreciated

Advertisement
Is it possible you are calling this function before you have created a valid OpenGL context?

Would the "absurdly large number" be 4294967295 by any chance? Your function returns -1 on failure, but it's an unsigned int return, meaning that it will translate to 4294967295, so one of the two conditions that causes a -1 return is being triggered. Which one? Well, your OutputDebugString calls should tell you that when you run it in a debugger, so you'll be able to trace through possible causes from there.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

id returns as 3435973836. God I wish I could fix this. Also, the function isn't returning -1, meaning that it is actually loading the file.

Like BitMaster said.

This topic is closed to new replies.

Advertisement