Sign in to follow this  
traiger

loading textures with SDL

Recommended Posts

I load textures using SDL and now want to use the alpha channel in PNGs. Does any one know how to do this? Currently if I try to load a PNG with an Alpha channel the entire texture is transparent. Thanks

Share this post


Link to post
Share on other sites
I use SDL to load a surface then convert the surface into a openGL texture which is then applied to a plain, again in opengl.

Opengl does all the rendering and SDL does the file and windows handling.

Share this post


Link to post
Share on other sites
GLuint g_texture[MAX_TEXTURES];

SDL_Surface *image;
SDL_RWops *rwop;
rwop=SDL_RWFromFile("sample.png", "rb");
image=IMG_LoadPNG_RW(rwop);

glGenTextures(1, &texture);
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, g_texture[next_number_goes_here]);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, image->w,image->h,GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

This should correctly create your OpenGL texture from a PNG file using SDL_Image.lib

Then, to draw with alpha blending enabled,

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_texture[the one you loaded]);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

glBegin(whatever you need);

// repeat following as necessary
glTexCoord2f(input vertex u,v coords here);
glVertex3f(input vertex x,y,z coords here);
//

glEnd();
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);


hope that helps and I dinnae have any typos...

Share this post


Link to post
Share on other sites
That looks almost identical to my code only I dont know what glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
and
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); are about

I perhaps should state that I currently have no problems loading pngs if they have no alpha transparent parts.

I will try your suggestion.

Thanks

Share this post


Link to post
Share on other sites
traiger,

post your "render scene" code for the section where you are trying to draw the .png

Mebbe that will help me understand better, thanks

Share this post


Link to post
Share on other sites

This part of the engine makes a texture from a pre loaded surface called BM->Bitmap


void stDB_Images::Get_Image(class stDBBitmaps *BM, GLuint x, GLuint y, GLuint w, GLuint h)
{
GLuint old_texture=texture;
SDL_Surface *image;
SDL_Rect Dest_Rect;

/* Use the surface width and height expanded to powers of 2 */
o_width = w;
o_height = h;
Dest_Rect.w = o_width;
Dest_Rect.h = o_height;

width = power_of_two(w);
height = power_of_two(h);
Dest_Rect.x = (width/2)-o_width/2;
Dest_Rect.y = (height/2)-o_height/2;
x_offset = Dest_Rect.x;
y_offset = Dest_Rect.y;

image = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height,32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000
#else
0xFF000000,
0x00FF0000,
0x0000FF00,
0x000000FF
#endif
);

/* Copy the surface GLuinto the GL texture image */
area.x = x;
area.y = y;
area.w = w;
area.h = h;

SDL_BlitSurface(BM->Bitmap, &area, image, &Dest_Rect);

/* Create an OpenGL texture for the image */
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,0, GL_RGBA, GL_UNSIGNED_BYTE,image->pixels);

SDL_FreeSurface(image); /* No longer needed */
}





This part renders the sprite only showing the first part here which renders sprites smaller than 256x256

void stDB_Sprite::Draw(void)
{
glTranslated(this->x_translate,this->y_translate,0);
glRotated((GLdouble)this->rotatez,0,0,1);
glTranslated((-(this->x_translate)+this->x),(-(this->y_translate))+this->y,this->z);

glBlendFunc(sf, df) ;

glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslated(0.5f,0.5f,0);
glRotated(this->texture_rotatez,0,0,1);
glTranslated(-0.5f,-0.5f,0);

glMatrixMode(GL_MODELVIEW);

glBindTexture(GL_TEXTURE_2D, this->texture);

if(this->img_w<=256 && this->img_h<=256)
{
glBegin(GL_QUADS);
//bl
glColor4d(bl.r, bl.g, bl.b,this->alpha);
glTexCoord2d(0,1);
glVertex2d( 0, this->img_h*y_scale);
//br
glColor4d(br.r,br.g,br.b,this->alpha);
glTexCoord2d(1,1);
glVertex2d(this->img_w*x_scale, this->img_h*y_scale);
//tr
glColor4d(tr.r,tr.g,tr.b,this->alpha);
glTexCoord2d(1,0);
glVertex2d( this->img_w*x_scale,0);
//tl
glColor4d(tl.r,tl.g,tl.b,this->alpha);
glTexCoord2d(0,0);
glVertex2d(0,0);
glEnd();
}

Share this post


Link to post
Share on other sites
traiger,

first:

in the first section:
Quote:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,0, GL_RGBA, GL_UNSIGNED_BYTE,image->pixels);


I would suggest changing the first GL_RGB to GL_RGBA since you are wanting to preserve the format from the image to the internal openGL texture structure.

Second, in your drawing section, I dont see that you've enabled blending anywhere. Is it globally enabled? I dont usually do that cause you have to be real careful which order you draw things in. I generally enclose the draw code for any transparent images I have with:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);

<draw code goes here>

glDisable(GL_BLEND);

Also, I notice that you are assigning colors to the vertices. No problem here but the color you assign will combine with the texture image to "shade" it with that color. I "generally" do not use color on the vertices if I am going to assign a texture to it.

Anyways, just a couple of things I noticed. Mebbe they'll help...

Share this post


Link to post
Share on other sites
Bullfrog

Yup that GL_RGB is usually GL_RGBA, although that makes the png texture invisible.

Because this is only a sprite engine it makes sorting a lot easier so blending is global.


I'll look into the vetex colouring issue, thanks for your help.

There must be some one who use png's for textures out there how do you do it?

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