Jump to content
  • Advertisement
Sign in to follow this  
bballmitch

OpenGL Loading png

This topic is 4520 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've loaded .ms3d files before. I've loaded .bmp files before, but now I'm in need of learning how to load a png file in OpenGL. Could someone explain this to me?

Share this post


Link to post
Share on other sites
Advertisement
You could use freeimage, or sdl-image to load almost all image formats, otherwise you could use libpng. Personally I prefer freeimage...

Share this post


Link to post
Share on other sites

FIBITMAP *reticle;
.
.
.
reticle = FreeImage_Load(FIF_PNG, "reticle.png", PNG_DEFAULT);
if (reticle)
{ // bitmap successfully loaded!
FreeImage_Unload(reticle);
}




I have that code among other code in my project now. It compiles, but I don't see that .png pic. Is there any other code I need to make it display (I want it to be in my hud, so it should be displayed after loading the IdentityMatrix )?

Share this post


Link to post
Share on other sites
I've just fought my way through this. The nicest one I found was libpng.

Nice and simple, I had a few moments of anger where I forgot to link libaries and such but its actually pretty easy to setup and use.

Share this post


Link to post
Share on other sites
But I already have ImageShack installed and working. I just wanted to know how to display my image; i already have it loaded....

Share this post


Link to post
Share on other sites
alright, but could you give me a general idea what that would look like? I know for a .bmp you do:


BITMAPINFOHEADER textureInfoHeader;
unsigned char* textureData;
unsigned int textures[5];

glGenTextures(5, textures); //All Textures
glBindTexture(GL_TEXTURE_2D, textures[1]);
textureData = LoadBitmapFile("asphalt.bmp", &textureInfoHeader);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureInfoHeader.biWidth, textureInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, textureData);



How would that look with a png in FreeImage?

Share this post


Link to post
Share on other sites
Quote:
Original post by bballmitch
alright, but could you give me a general idea what that would look like? I know for a .bmp you do:

*** Source Snippet Removed ***

How would that look with a png in FreeImage?
It looks exactly the same except you pass into the pixels parameter of glTexImage2D the array of pixels from the PNG image instead of from the BMP image. I've never used FreeImage before but documentation exists for a reason; I'm sure you can find how to get the data you need if you read it.

Share this post


Link to post
Share on other sites
well some funky sutff is happening...


unsigned int textures[5];
FIBITMAP *reticle;
.
.
.
glGenTextures(5, textures);
glBindTexture(GL_TEXTURE_2D, textures[1]);
reticle = FreeImage_Load(FIF_PNG, "reticle.png", PNG_DEFAULT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, reticle);
.
.
.
glPushMatrix(); //draws reticle
glBindTexture(GL_TEXTURE_2D, textures[1]);
glTranslatef(0.0,0.0,-5.0);
glRotatef( 90, 1,0,0);
glScalef(0.2,0.2,0.2);
glColor3f(1.0,1.0,1.0);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(-1,-1,-1);
glTexCoord2f(1,0); glVertex3f(-1,-1,1);
glTexCoord2f(1,1); glVertex3f(1,-1,1);
glTexCoord2f(0,1); glVertex3f(1,-1,-1);
glEnd();
glPopMatrix();
.
.
.



I used that code and I'm getting the following output:
http://img376.imageshack.us/my.php?image=mygame20060531170007993cw.png

I'm talking purely about the color/pixel problem of that square in the middle. Why is that happening? reticle.png is in the right location and it is 64x64.

Share this post


Link to post
Share on other sites
I think FBITMAP is a pointer to structure that holds the pixel data and some other stuff.

Try
unsigned char *data = FreeImage_GetBits(reticle);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);

My usage of FreeImage_GetBits() might be wrong but pretty sure you have to use that to get the actual data for the image.

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!