Advertisement

Creating textures

Started by September 24, 2003 11:29 AM
3 comments, last by Ruudje 21 years, 5 months ago
In the tutorials, a texture is stored in a GLuint. This is a pointer? Why I am asking is because Im planning something like this: I wanna create a texture-loader which has a GLuint as a local variable, and return that variable at the end. Should I return GLuint, or &GLuint, and how exactly do I release the texture, simply by using delete GLuint-variable? Additional q: which texture formats are most popular, and worth adding support for? Ive got bmp, jpg and tga on my list
quote:
Original post by Ruudje
In the tutorials, a texture is stored in a GLuint. This is a pointer?



Yes, in a round about way. If you display it, you''ll see that the value returned will have an inreasing value, starting with 1. If it''s 0, the texture wasn''t created properly. So it''s more of an index in a memory address lookup table.

"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared
Advertisement
first, let say your texture-loader function is defined as follow:
GLuint LoadTex(char *filename);
note that the parameters may be different, it's not important.

your function should look like this:
GLuint LoadTex(char *filename)
{
GLuint texId;
unsigned char *texData; // the texture pixels

// load your file here
// and store the pixels in texData
// don't forget to allocate memory for texData
// texData = (unsigned char *)malloc(texSize)
// where texSize is the size (in bytes) of the image data
// if you have width and height you need to calculate the size
// texSize = width * height * bits_per_pixel

glEnable(GL_TEXTURE_2D); // enable 2D texturing
glGenTextures(1, &texId); // generate the texture Id
glBindTexture(GL_TEXTURE_2D, texId); // this is a 2D texture

// do texture modifications here
// hint: glTexParameteri() or gluBuild2DMipmaps()

// load the texture image
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, texData);

return texId;
}

[EDIT]
for releasing the texture, AFAIK OpenGL takes care of doing it for you.
[/EDIT]

Matt
---------
"Without C, we would only have Basi, Pasal and obol."

[edited by - lemurion on September 24, 2003 1:24:28 PM]
Matt
tnx!
Ive tried to implement the tga-loader from the tutorials today, but failed The only real difference from the tutor that I had was that I stored the Texture-structure in a class and not globally, but this is what happened:

First of all, it went fine untill AFTER loading the texture using the LoadTGA function. But then you have to create the actual texture using the opengl code. That wouldnt work. the texID (the GLuint) was still 0 afterwards, and the really strange thing was when I looked at the list of members of my class, the structure was displayed as: var int file_3ds::Texture. It shouldve been like this though: var Texture file_3ds::texture, since I named my instance texture. I couldnt get this fixed, so ended up removing all the tga code from my files.

I still cant explain why... spent 5 hours working on this

This topic is closed to new replies.

Advertisement