SDL Forum Index
SDL
Simple DirectMedia Layer Forums
Reply to topic
SDL + OpenGL Texture Loading
Benjamin Li
Guest

Reply with quote
Okay, I am trying to create code that will let me load OpenGL textures using
SDL. However, following some tutorial code that I found, I get a segfault that I
cannot figure out. I am running Ubuntu Linux and have gotten my SDL and GL code
to work fine for everything else.

I am able to reproduce the error with a short program

/////////////////////////////////
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>


int main(int argc, char *argv[])
{

GLuint texture;
SDL_Surface* surface;
SDL_Surface* screen;

if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}

atexit(SDL_Quit);

int videoMode = 0;
videoMode |= SDL_OPENGL;

screen = SDL_SetVideoMode( 640, 480, 16, videoMode );

//Load Bitmap
if(!(surface = SDL_LoadBMP("image.bmp"))) {
return -1;
}

// Have OpenGL generate a texture object handle for us
glGenTextures( 1, &texture );

// Bind the texture object
glBindTexture( GL_TEXTURE_2D, texture );

// Set the texture's stretching properties
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, 3, surface->w, surface->h, 0,
GL_BGR, GL_UNSIGNED_BYTE, surface->pixels );

//Free surface after using it
if(surface)
SDL_FreeSurface(surface);
}
///////////////////////////

Debugging with GDB doesn't give me the specific line of the problem, but tracing
through, I have isolated the problem at glTexImage2D. It seems that some other
people may have had similar problems, but I can't find a resolution anywhere.

In my actual program I originally tried loading the (pcx) image through
SDL_image, and then converted it into a BMP when that failed for the same
reason.

Any help at all would be welcome! If there is any other information that would
be useful, please let me know.

-Ben
SDL + OpenGL Texture Loading
Daniel K. O.
Guest

Reply with quote
Benjamin Li escreveu:
Quote:
Okay, I am trying to create code that will let me load OpenGL textures using
SDL. However, following some tutorial code that I found, I get a segfault that I
cannot figure out.

Unfortunatelly it doesn't crash here. Could you provide your image.bmp,
just to check if it isn't the culprit?


Quote:
In my actual program I originally tried loading the (pcx) image through
SDL_image, and then converted it into a BMP when that failed for the same
reason.


Last time I had to use SDL + raw OpenGL, I did something like this for
textures:

1) img = IMG_Load().

2) tex = SDL_CreateRGBSurface(..., 32, 0x000000FF, 0x0000FF00,
0x00FF0000, 0xFF000000) to obtain a 32-bit GL_RGBA-compatible surface. I
think you would need to revert the mask for "SDL_BYTEORDER ==
SDL_BIG_ENDIAN" systems.

3) SDL_SetAlpha(img, 0, 0), then SDL_BlitSurface(img, 0, tex, 0), to
make SDL convert "img" to "tex" format.

4) glPixelStorei() to properly set GL_UNPACK_ROW_LENGTH to tex->pitch (I
didn't need this for my textures, I'm not sure it's necessary).

5) glTexImage2D(GL_TEXTURE_2D, 0, 4, tex->w, tex->h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, tex->pixels).




--
Daniel K. O.
SDL + OpenGL Texture Loading
Vardar Sahin
Guest

Reply with quote
Does you image have a dimension of a power of 2 like 16x16 or 64x64?

OpenGl texture must/should have a dimesnion ofa ower of 2


2007/3/16, Daniel K. O. <danielko.listas at gmail.com>:
Quote:

Benjamin Li escreveu:
Quote:
Okay, I am trying to create code that will let me load OpenGL textures
using
Quote:
SDL. However, following some tutorial code that I found, I get a
segfault that I
Quote:
cannot figure out.

Unfortunatelly it doesn't crash here. Could you provide your image.bmp,
just to check if it isn't the culprit?


Quote:
In my actual program I originally tried loading the (pcx) image through
SDL_image, and then converted it into a BMP when that failed for the
same
Quote:
reason.


Last time I had to use SDL + raw OpenGL, I did something like this for
textures:

1) img = IMG_Load().

2) tex = SDL_CreateRGBSurface(..., 32, 0x000000FF, 0x0000FF00,
0x00FF0000, 0xFF000000) to obtain a 32-bit GL_RGBA-compatible surface. I
think you would need to revert the mask for "SDL_BYTEORDER ==
SDL_BIG_ENDIAN" systems.

3) SDL_SetAlpha(img, 0, 0), then SDL_BlitSurface(img, 0, tex, 0), to
make SDL convert "img" to "tex" format.

4) glPixelStorei() to properly set GL_UNPACK_ROW_LENGTH to tex->pitch (I
didn't need this for my textures, I'm not sure it's necessary).

5) glTexImage2D(GL_TEXTURE_2D, 0, 4, tex->w, tex->h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, tex->pixels).




--
Daniel K. O.
_______________________________________________
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20070317/2d731075/attachment.htm
SDL + OpenGL Texture Loading
Benjamin Li
Guest

Reply with quote
Daniel K. O. <danielko.listas <at> gmail.com> writes:

Quote:

Benjamin Li escreveu:
Okay, I am trying to create code that will let me load OpenGL textures using
SDL. However, following some tutorial code that I found, I get a segfault that
I
cannot figure out.

Unfortunatelly it doesn't crash here. Could you provide your image.bmp,
just to check if it isn't the culprit?

Well, thanks for the response. It turns out that the image I was trying to
usewas indexed, which caused the problem. After switching the image type,
it works perfectly.


Quote:
--
Daniel K. O.



-Ben
SDL + OpenGL Texture Loading
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic