Sign in to follow this  
MichaelBarth

OpenGL Having trouble getting an image on screen

Recommended Posts

Alright, I've sort of touched on this question before, and also I wasn't sure whether or not to put this in the OpenGL or SDL section since I'm using SDL_OpenGL.

Anyway, here's my problem, I have code that loads an SDL_Surface and converts it to a GLuint. It worked fine... with the particular image I was testing it with.

It works just fine with this image: [IMG]http://i610.photobucket.com/albums/tt190/Spirrwell/nukeico.png[/IMG]

However I've tried a few other images (that were 1920 x 1200) with different depths, with transparent pixels, and different image formats. Only this particular image works.

Visual Studio comes up with an error: "Unhandled exception at 0x00f81841 in NAMEOFGAME.exe: 0xC0000005: Access violation reading location 0x0614b000."

Here's my code:

[CODE]
GLuint loadTexture ( char *filename )
{
GLuint retval;
SDL_Surface *sdlimage;
void *raw;
int w, h, i, j, bpp;
Uint8 *srcPixel, *dstPixel;
Uint32 truePixel;
GLenum errorCode;

sdlimage = IMG_Load(filename);

if ( !sdlimage ) {
printf("SDL_Image load error: %s\n", IMG_GetError());
return 0;
}
if ( sdlimage->format->BytesPerPixel < 2 ) {
printf("Bad image -- not true color!\n");
return 0;
}
w = sdlimage->w;
h = sdlimage->h;
raw = (void *)malloc( w * h * 4 );
dstPixel = (Uint8 *)raw;
SDL_LockSurface( sdlimage );
bpp = sdlimage->format->BytesPerPixel;

//It seems to crash within this for loop:
for ( i = h ; i > 0 ; i-- ) {

for ( j = 0 ; j < w ; j++ ) {
srcPixel = (Uint8 *)sdlimage->pixels + i * sdlimage->pitch + j * bpp;
switch (bpp) {
case 1:
truePixel = *srcPixel;
break;
case 2:
truePixel = *(Uint16 *)srcPixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
truePixel = srcPixel[0] << 16 | srcPixel[1] << 8 | srcPixel[2];
} else {
truePixel = srcPixel[0] | srcPixel[1] << 8 | srcPixel[2] << 16;
}
break;
case 4:
truePixel = *(Uint32 *)srcPixel;
break;
default:
printf("Image bpp of %d unusable\n", bpp);
return 0;
break;
}
SDL_GetRGBA( truePixel, sdlimage->format, &(dstPixel[0]), &(dstPixel[1]), &(dstPixel[2]), &(dstPixel[3]));
dstPixel++;
dstPixel++;
dstPixel++;
dstPixel++;
}
}
SDL_UnlockSurface( sdlimage );
SDL_FreeSurface( sdlimage );
while ( glGetError() ) { ; }
glGenTextures( 1, &retval );
glBindTexture( GL_TEXTURE_2D, retval );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
errorCode = glGetError();
if ( errorCode != 0 ) {
if ( errorCode == GL_OUT_OF_MEMORY ) {
printf("Out of texture memory!\n");
}
return 0;
}
gluBuild2DMipmaps( GL_TEXTURE_2D, 4, w, h, GL_RGBA, GL_UNSIGNED_BYTE, (Uint8 *)raw);
errorCode = glGetError();
if ( errorCode != 0 ) {
if ( errorCode == GL_OUT_OF_MEMORY ) {
printf("Out of texture memory!\n");
}
return 0;
}
return retval;
}
[/CODE]

I wrote a comment at the for loop at which it crashes, it seems to crash determining the BPP. Any help is appreciated.

Share this post


Link to post
Share on other sites
Have you tried running the debugger to pinpoint the crash location and the variable values at the time?

Suspiciously it works on a what looks like square image, while failing on other non-square images, so my guess is that you've got something broken with your loop that you've commented on. Pull up the debugger and see what's going on.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this