.png files....

Started by
15 comments, last by Morbo 19 years, 2 months ago
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.
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].
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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
Quote:Original post by kc_0045
No, it'll all go in a RGBA texture [smile].
ok....how? :P

Depends on what library you use to load the image (e.g. DevIL, libPNG).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
someone correct me if im wrong; however you load the .png file, the opengl textures have 4 values anyway. R,G,B,A.
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.
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 :(
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.
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 ;)
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.

This topic is closed to new replies.

Advertisement