Loading png

Started by
11 comments, last by Lode 17 years, 10 months ago
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?
Mitchen Games
Advertisement
You could use freeimage, or sdl-image to load almost all image formats, otherwise you could use libpng. Personally I prefer freeimage...
-[Anudhyan][Website]
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 )?
Mitchen Games
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.
But I already have ImageShack installed and working. I just wanted to know how to display my image; i already have it loaded....
Mitchen Games
You need to make a texture with the image data, just as you had to do with the bitmap.

Check out my new game Smash and Dash at:

http://www.smashanddashgame.com/

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?
Mitchen Games
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.
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.
Mitchen Games
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.

This topic is closed to new replies.

Advertisement