Jump to content
  • Advertisement
Sign in to follow this  
traiger

loading textures with SDL

This topic is 4997 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

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
Advertisement
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
My stupid

I can show png's with transparent parts but the transparent part shows as black can anyone help with this?

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
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!