Jump to content
  • Advertisement
Sign in to follow this  
kc_0045

OpenGL .png files....

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

Hello, i am using opengl for a game i am currtly working on, but i have been using 2 .bmp files (one for color and one for alpha) but im getting tired of that, so how can i use .png files in opengl? would i load the RGB into one texture, then the alpha into the other? if so how? im rather new to opengl/c++. sooo....thanks in advanced.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
would i load the RGB into one texture, then the alpha into the other?


No, it'll all go in a RGBA texture [smile].

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
would i load the RGB into one texture, then the alpha into the other?


No, it'll all go in a RGBA texture [smile].
ok....how? :P

Share this post


Link to post
Share on other sites
Quote:
Original post by kc_0045
No, it'll all go in a RGBA texture [smile].
ok....how? :P[/quote]

Depends on what library you use to load the image (e.g. DevIL, libPNG).

Share this post


Link to post
Share on other sites
Quote:
Original post by sand_man
someone correct me if im wrong; however you load the .png file, the opengl textures have 4 values anyway. R,G,B,A.

You're wrong!

OpenGL textures can have at least these values:

format Specifies the of the pixel data. The following symbolic values are accepted:
GL_COLOR_INDEX, GL_RED, GL_GREEN, GL_BLUE,GL_ALPHA, GL_RGB, GL_BGR GL_RGBA, GL_BGRA, GL_LUMINANCE, and GL_LUMINANCE_ALPHA.

Share this post


Link to post
Share on other sites
ok thanks everone, does anyone know of any tutorials or samples using one of thoose librarys? as i am rather new to c++ and i tryed doing it my self, but with no luck :(

Share this post


Link to post
Share on other sites
Quote:
Original post by dotproduct
Quote:
Original post by sand_man
someone correct me if im wrong; however you load the .png file, the opengl textures have 4 values anyway. R,G,B,A.

You're wrong!

Not entirely. OpenGL (as well as D3D) will not allow 3 component internal formats, but will expand them automatically to 4 components. So basically, if you specify GL_RGB as internal format, then OpenGL will override this choice by using GL_RGBA instead. You, as an API user, will usually not notice this. But you should keep it in mind when considering memory consumption: a 24bit RGB texture will take up exactly the same amount of memory on the video card as a 32bit RGBA texture, because of the automatic padding. So, if you're dealing with uncompressed colour textures, then try to make good use of the alpha channel, because it's going to be allocated no matter what you do. If you don't use it, then that space is wasted.

Quote:
Original post by kc_0045
ok thanks everone, does anyone know of any tutorials or samples using one of thoose librarys? as i am rather new to c++ and i tryed doing it my self, but with no luck :(

libpng comes with a good example on how to load and save images. It's very simple.

Share this post


Link to post
Share on other sites
Quote:
Original post by Yann L
Quote:
Original post by dotproduct
Quote:
Original post by sand_man
someone correct me if im wrong; however you load the .png file, the opengl textures have 4 values anyway. R,G,B,A.

You're wrong!

Not entirely. OpenGL (as well as D3D) will not allow 3 component internal formats, but will expand them automatically to 4 components. So basically, if you specify GL_RGB as internal format, then OpenGL will override this choice by using GL_RGBA instead. You, as an API user, will usually not notice this. But you should keep it in mind when considering memory consumption: a 24bit RGB texture will take up exactly the same amount of memory on the video card as a 32bit RGBA texture, because of the automatic padding. So, if you're dealing with uncompressed colour textures, then try to make good use of the alpha channel, because it's going to be allocated no matter what you do. If you don't use it, then that space is wasted.


That sir... is true ;)

Share this post


Link to post
Share on other sites
If you're using SDL_image, the whole thing gets ridiculously easy:


/*
* --- Load a texture ---
*
* This is adapted from the texture mapping NeHe tutorial.
*/

bool load_bind_texture( char* file, GLuint* ta, uint idx ) {
/* Status indicator */
bool Status( false );

/* Create storage space for the texture */
SDL_Surface *TextureImage[1];

/* Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit */
if ( ( TextureImage[0] = IMG_Load( file ) ) ) {
Status = true;
// bind to the given texture ID
glBindTexture( GL_TEXTURE_2D, ta[idx] );

/* Generate The Texture */
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, TextureImage[0]->w,
TextureImage[0]->h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, TextureImage[0]->pixels );

/* Linear Filtering */
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
}

/* Free up any memory we may have used */
if ( TextureImage[0] ) SDL_FreeSurface( TextureImage[0] );

return Status;
}




Note that this code doesn't check for the correct image format and stuff, but it should work out of the box for any RGBA image with suitable dimensions. I also have a loader that only uses libpng, but that's a whole other story.

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!