Skip to main content
GameDev.net gamedev.net
🔒 Locked

Load PNG

Started by zurekx Aug 9, 2008 at 9:38 AM 6 replies 3.1k views
Original Post
zurekx
zurekx
Which is the easiest way to work with PNG textures (I want them for transparency) in OpenGL? I work in Visual Studio using C.
zurekx
zurekx
Thanks!

Exactly how do I use it with OpenGL, can it create a texture or just give me the raw data? (I dont have much experience with texture loading in OpenGL, just used the code from the Nehe tutorial)
Vaayu
Vaayu
Not to forget SOIL.

It is pretty easy to use as it will directly give you an OpenGL texture ID. So does DevIL, but i prefer SOIL.
Jaywalk
Jaywalk
There's little need for libraries to create OpenGL textures for you, because it's very simple to create an OpenGL texture from the raw image data. First, you load the image using one of the libraries mentioned.

Now, if that worked, you can create the OpenGL texture to store the image.
int texture_id;glGenTextures(1, &texture_id); // create 1 texture and store its ID in the variable texture_id


Next you write tell OpenGL that the texture must use the pixel data that you have loaded. Assuming that the image is RGBA format, that the variable named "pixels" is a pointer to the raw pixel data, and that "width" and "height" contain the width and height of the image, do this:

glBindTexture(GL_TEXTURE_2D, texture_id);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_CHAR, pixels);


Once this is done, you no longer need the raw image data, so you can delete it
delete pixels;
zurekx
zurekx
I tried to use LodePNG but it doesnt work, my triangles are white (i.e. no texture). I use the following code

unsigned error;unsigned char* image;size_t w, h, x, y;  	/*load the PNG in one function call*/error = LodePNG_decode32f(ℑ, &w, &h, "rwar.png");glGenTextures(1, &texture[0]);					glBindTexture(GL_TEXTURE_2D, texture[0]);glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);


It seems to load the file properly, I get no error and the size is correct. I looked through some of the data and it looks correct too. It's nothing wrong with my other code, it works if I load a bmp-image using
AUX_RGBImageRec *TextureImage[1];TextureImage[0]=LoadBMP("Data/NeHe.bmp");glBindTexture(GL_TEXTURE_2D, texture[0]);glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

so it has to be something when I create the texture from the raw data

[Edited by - zurekx on August 10, 2008 7:12:46 AM]
V-man
V-man
http://www.opengl.org/wiki/index.php/Common_Mistakes#Creating_a_Texture

Quote:
The texture won't work because it is incomplete. The default GL_TEXTURE_MIN_FILTER state is GL_LINEAR_MIPMAP_NEAREST so GL will consider the texture incomplete as long as you don't create the mipmaps.
zurekx
zurekx
Thanks. That wasn't the problem, however, but the texture width/height were no powers of 2, but appearantly gluBuild2DMipmaps doesn't require that

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.