PDA

View Full Version : Where to place texture generators, bindings etc. in C++ classes?



BigShooter
10-30-2003, 12:09 AM
Sorry about the lenght of this post...
I wrote a class that is supposed to display outline, texture mapped fonts (using standard procedures for doing so on Win platform).
I'm working on a simple game engine.
Let's say I have a simple class called CTestCube. In the constructor, I have this code: ( I have a class CTexture working ok)
cubeTexture[0].LoadTexture("Me.tga"); //.bmp file loading seems to be ****ed...!
glGenTextures(1, &cubeTexture[0].texID);
glBindTexture(GL_TEXTURE_2D, cubeTexture[0].texID);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

Then, in the OnDraw() of this cube, I have:
glBindTexture(GL_TEXTURE_2D, cubeTexture[0].texID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, cubeTexture[0].width, cubeTexture[0].height, 0, GL_RGB, GL_UNSIGNED_BYTE, cubeTexture[0].data);

before passing in to the OpenGl.
This all renders quite fast.
Now, for my outline fonts class, I 've placed everything in the OnDraw() function, which in part looks like this:


glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); // use bilinear filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // repeat texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, texture->width, texture->height, GL_RGB, GL_UNSIGNED_BYTE, texture->data);

// we want OpenGL to automatically generate the texture coordinates for the text
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
This is called every frame...
Ok, so my problems are:
1) This slows the display to crawl... obviously I'm doing something wrong here...
So, basically, where is the proper placement in terms of OnDraw() and constructor, of commands, such as :
gluBuild2DMipmaps
glTexParameteri
glBindTexture
glGenTextures
which can be called at the beginning and which need to be called everyframe (assuming that every object will have a difference texture (and every object has a pointer to CTexture).

2) When the fonts gets rendered, they change all the texturing of my other objects... what is the proper way in terms of good OO design to fix this? Call glPushAttrib(GL_TEXTURE_BIT) before foing the glBindTesture/glGenTextures/glTexParameteri?


Thank you for your help,
Luke

Relic
10-30-2003, 05:43 AM
1.) Ugh, gluBuild2DMipmaps does something like
for (lod)
{
gluScaleImage
glPackParameter //multiple
glGetError // multiple
glTexImage2D
}

The scaling is all in SW.
You don't want that per OnDraw. You want that per texture change only, which is hopefully only at init time. Texture initialization belongs into an init function (OnCreate, right after you built your rendering context?)

2.) Use texture objects (always a good idea)! glBindTexture is relatively inexpensive.
Remember glTexParameters are stored with the texture object, there's no need to call them again after glBindTexture later in the OnDraw.


[This message has been edited by Relic (edited 10-30-2003).]