Jump to content
  • Advertisement
Sign in to follow this  
Jossos

OpenGL Need help - loading textures

This topic is 2118 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!