Advertisement

Generating textures in SDL/OpenGL causes crash

Started by January 25, 2006 07:28 PM
8 comments, last by NeilRoy 13 years, 8 months ago
Hi, I'm running Mandriva Linux 2006 with an nVidia GeForce4 420 Go card. I am experiencing some problems trying to get OpenGL textures to work. The problem is that when I try to generate the texture, be it via glTexImage2D or gluBuild2DMipmaps, my application crashes. I've deduced that it is a problem with the images I'm loading, even though they are perfectly fine (a 256x256 bitmap will fail). I enable textures in my custom init() function, and everything seems ok to me. The strange thing is that this code worked perfectly before I upgraded my system (from 10.1 to 2006), even though I doubt that the upgrade had anything to do with it. As of now, I tried out a variety of different image programs, but any image I create will not work. I changed the permissions of the images and everything, but something just won't budge. I'm not sure what I'm doing wrong, so if anyone has any ideas, I'd be glad to learn. :) Thanks in advance! Here is the loading code in question:
void loadGLTextures() {
	SDL_Surface *tex=SDL_LoadBMP("particle.bmp");
	if (!tex) {
		printf("Unable to load texture 'particle.bmp'\n");
		exit(0);
	}
	
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
	
	gluBuild2DMipmaps(GL_TEXTURE_2D, 3, tex->w, tex->h, GL_RGB, GL_UNSIGNED_BYTE, tex->pixels);
	
	if (tex)
		SDL_FreeSurface(tex);
};


And here is my init code:
void initGL() {
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClearDepth(1.0f);
	
	glEnable(GL_DEPTH_TEST);
	
	glEnable(GL_TEXTURE_2D);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	gluPerspective(45.0, screen->w/screen->h, 0.1, 100.0);
	
	glMatrixMode(GL_MODELVIEW);
	
	// load textures
	loadGLTextures();
};

[Edited by - acrimon on January 25, 2006 7:50:18 PM]
Are you sure the image is ok?

In your error, you have particle.tga... SDL_LoadBMP well, loads bitmaps. Make sure that you didn't just rename the file, as maybe an imaging program can figure it out, while SDL_LoadBMP cannot read the renamed tga file. I can't think of anything else...
Advertisement
I changed the file name to point to the correct image, which was a bitmap, but that still caused a crash. I also tried using SDL_image to load the aforementioned TGA, and that didn't yield results either.

Another thing I discovered is that when I take an image off the internet, for example, and try loading it, then it's fine. Perhaps it's my image that crashes, but I honestly can't figure out why since I made sure that everything was accounted for (size, etc).

Is there any way to figure out specifically why my image is crashing? I'm thinking of some kind of function call, but none come to mind.
Quote: Original post by acrimon
I changed the file name to point to the correct image, which was a bitmap, but that still caused a crash. I also tried using SDL_image to load the aforementioned TGA, and that didn't yield results either.

Another thing I discovered is that when I take an image off the internet, for example, and try loading it, then it's fine. Perhaps it's my image that crashes, but I honestly can't figure out why since I made sure that everything was accounted for (size, etc).

Is there any way to figure out specifically why my image is crashing? I'm thinking of some kind of function call, but none come to mind.


I can't think of anything either...

So, you're saying that any image already done on the web works?

What image program are you using? What if you were to open it up, create something simple (a .bmp), make it 128x128, and fill it with red. Save it as whatever .bmp, and then load it.

I don't really know, especially since I'm not a linux person, sorry :(.
Sorry to double-post, but this should stand-out:
Try using SDL_SaveBMP, and then save your loaded bitmap as something. Then, see if you can view it with some imaging program. If you can, then the image itself is fine.

Also, what if you were to create an RGBA surface or RGB surface, that is blitted onto by your image? And then, use that as your texture. This is what I do, since I have to use colorkeys (with colorkeys it's extremely useful this way).
Thanks for all the suggestions, but still I'm getting segfaults. Just to make sure that my program's code wasn't the problem, I downloaded and compiled a simple demo app (the one from NeHe's tutorials) that loads a texture. And just like before, it crashes. :( I used the GIMP (v 2.2) and XPaint to make my images.

Perhaps this is a problem with my implementation of OpenGL?
Advertisement
Quote: Original post by acrimon
Thanks for all the suggestions, but still I'm getting segfaults. Just to make sure that my program's code wasn't the problem, I downloaded and compiled a simple demo app (the one from NeHe's tutorials) that loads a texture. And just like before, it crashes. :( I used the GIMP (v 2.2) and XPaint to make my images.

Perhaps this is a problem with my implementation of OpenGL?


Haven't ever heard of OpenGL actually crashing :(.

Hmm... But I can't really think of anything else. Since NeHe uses windows and sdl, then it's mostly OpenGL, I guess... I'm sorry I can't help any further, I'm not that much of a graphics/computer hardware person :(.
Anyway, thanks for all the help, I really appreciated it. :)

For now I guess I'll just stick to my Windows box for SDL/OpenGL until later when I have more time to figure this out.
The only thing i can think of is that the loaded image isnt in the pixel format you expect.

Try loading the image with the code from "testgl.c" which is included with the SDL source code. it converts the loaded bitmap into a Surface of known format, then makes a texture out of it.
I'm curious if you ever solved this? I realize this was quite a while ago but I am having similar problems. It's in the section of code that frees up the memory alocated to hold the textures. The code is slightly different than yours but I think it's probably the same cause whatever that may be.

I commented out the section that frees up the memory allocated and it works just fine (all textures loaded and displayed fine). This used to work on Windows XP, but crashes if you try and free memory now on Windows 7. The code never changed, just the operating system. (windows 7 64bit).


int LoadGLTextures() // Load Bitmaps And Convert To Textures
{
int Status=FALSE; // Status Indicator
int i;

AUX_RGBImageRec *TextureImage[7]; // Create Storage Space For The Texture

memset(TextureImage,0,sizeof(void *)*7); // Set The Pointer To NULL

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
TextureImage[0]=LoadBMP("Data/grass.bmp");
TextureImage[1]=LoadBMP("Data/FlatRoadNS.bmp");
TextureImage[2]=LoadBMP("Data/FlatRoadEW.bmp");
TextureImage[3]=LoadBMP("Data/FlatRoadSE.bmp");
TextureImage[4]=LoadBMP("Data/FlatRoadSW.bmp");
TextureImage[5]=LoadBMP("Data/FlatRoadNW.bmp");
TextureImage[6]=LoadBMP("Data/FlatRoadNE.bmp");

Status=TRUE;

glGenTextures(7, &texture[0]); // Create The Texture

for (i=0; i<7; i++) {
glBindTexture(GL_TEXTURE_2D, texture);
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, TextureImage->sizeX, TextureImage->sizeY,
0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->data);
}

/*
for (i=0; i<7; i++) {
if (TextureImage) {
if (TextureImage->data) free(TextureImage->data);
free(TextureImage);
}
}
*/
return Status; // Return The Status
}

This topic is closed to new replies.

Advertisement