Skip to main content
gamedev.net
🔒 Locked

question about glEnable( )

Started by Himilou Sep 26, 2002 at 8:16 PM 6 replies 600+ views
Original Post
Himilou
Himilou
Hi Everyone, I''ve been playing around with the tutorials at nehe.gamedev.net and ran into a question about using mapped textures and the color palette. In the 6th tutorial http://nehe.gamedev.net/tutorials/lesson.asp?l=06 you basically map a texture onto a cube drawn with GLQUADS after loading it with glGenTextures( ) and glBindTexture( ) and glTexImage2D ( ) I was also trying to draw a simple triagle filled with color on the same screen. However when I set the color for the triangle the color of the texture changes or depening on what I change the trangle draws in one of the colors that is in the texture. The workaround I''ve found is to call glDisable((GL_TEXTURE_2D) draw the triangle and then call glEnable(GL_TEXTURE_2D) and draw the cube with the texture. Is it normal to have to do this to switch between drawing a simple color filled shape and a texture filled shape? GL environment is setup through int InitGL(GLvoid) // All Setup For OpenGL Goes Here { if (!LoadGLTextures())// Jump To Texture Loading Routine { return FALSE; } glEnable(GL_TEXTURE_2D); glShadeModel(GL_SMOOTH); glClearColor(0.0f, 0.0f, 0.0f, 0.5f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); return TRUE; } int DrawGLScene(GLfloat xaxis,GLfloat yaxis,GLfloat rot) // Here''s Where We Do All The Drawing { GLfloat colorarray[4]; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer glGetFloatv(GL_CURRENT_COLOR,colorarray); glDisable(GL_TEXTURE_2D); glLoadIdentity(); // Reset The Current Modelview Matrix glTranslatef(-1.5f,0.0f,-12.0f); // Left 1.5 Then Into Screen Six Units glBegin(GL_TRIANGLES); glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red glVertex3f( 0.0f, 1.0f, 0.0f); // Move Up One Unit From Center (Top Point) glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green glVertex3f(-1.0f,-1.0f, 0.0f); // Left And Down One Unit (Bottom Left) glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue glVertex3f( 1.0f,-1.0f, 0.0f); // Right And Down One Unit (Bottom Right) glEnd(); // Done Drawing A Triangle glEnable(GL_TEXTURE_2D); glLoadIdentity(); //reset our draw points so the square is static glTranslatef(-0.0f,0.0f,-10.0f); //Move the square back in the z buffer glRotatef(xrot,1.0f,0.0f,0.0f); // Rotate On The X Axis glRotatef(yrot,0.0f,1.0f,0.0f); // Rotate On The Y Axis glRotatef(zrot,0.0f,0.0f,1.0f); // Rotate On The Z Axis glBindTexture(GL_TEXTURE_2D, texture[0]); // Select Our Texture glBegin(GL_QUADS); // Front Face glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad // Back Face glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad // Top Face glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad // Bottom Face glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad // Right face glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad // Left Face glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad glEnd(); xrot+=0.7f; // X Axis Rotation yrot+=0.8f; // Y Axis Rotation zrot+=0.9f; // Z Axis Rotation rtri+=rot; rquad += rot; return TRUE; // Everything Went OK }
tuita
tuita
The colour of the quad is tinting because of the lines:

glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
glVertex3f( 1.0f,-1.0f, 0.0f); // Right And Down One Unit (Bottom Right)
glEnd(); // Done Drawing A Triangle

Because you have not changed the colour back to white, the quad will be drawn with full blue colouring (should cause blueish colours in the texture to stand out more).

Try calling glColor3f(1.0f, 1.0f, 1.0f) before you draw the Cube.

Also, if you store the textures in an array, you can cheat and reference ''textures[-1]'' in a bind texture call to save enabling, disabling textures. However, I''m not sure if this is actually faster or slower than calling glEnable/glDisable.
Himilou
Himilou
Hi,
Thanks for the reply. If I take out the glEnable and glDisable calls around the triangle code and then just call glColor3f( ) before rendering the quad the triangle becomes very blueish rather than a smoothed bright red, green, and blue as it is with glEnable glDisable.

I guess I''m really curious to know why using the texture upsets the color palette so much.

Cheers,

James
DC_Tsunami
DC_Tsunami
You can''t cheat and reference textures[-1] because that memory doesn''t have anything to do with textures. You might reference some other variable in that function (assuming static array allocation) or cause a GPF. Instead, you can just bind to texture 0 (glBindTexture( GL_TEXTURE_2D, 0 ) which is the default texture. It is just plain white. Any color you send will be blended with the white, just like there is no texture there.

Another trick you can try is changing texture environment (to prevent the blue from showing through your textures). The default mode is GL_MODULATE I think, which multiplies the texel color with the triangle color. Change this to GL_DECAL with the glTexEnvi() function, so that texel colors replace existing pixel colors.
Himilou
Himilou
Hi,

Thanks for the idea. It didnt really work though.
Using glTexEnvf( ) certainly affects how the texture is displayed once lighting is added but for the simple shape( a GL_TRIANGLES ) the color is still wrong. Additionally now that I have added diffuse lighting to the project calling
glDisable(GL_TEXTURE_2D) no longer lets the simple shape color correctly.Now the shape is either white ( the color of the ambient light I have ? ) or the color of the diffuse light when the shapes glNormal( ) is facing the light. Any ideas?

zedzeek
zedzeek
>>The workaround I''ve found is to call glDisable((GL_TEXTURE_2D) draw the triangle and then call glEnable(GL_TEXTURE_2D) and draw the cube with the texture.

Is it normal to have to do this to switch between drawing a simple color filled shape and a texture filled shape?<<

i think others have answered so i dont see what the problem is
r u trying to do 2 things
A/u want a triangle drawn in a color eg red glColor3f(1,0,0)
B/draw a triangle with a texture on it (unaffected by the colour)

is this what u want to do?
a sidenote remember u cant enable lighting + colors (unless u have colormaterial enabled)

http://uk.geocities.com/sloppyturds/kea/kea.html
http://uk.geocities.com/sloppyturds/gotterdammerung.html
Himilou
Himilou
Hi,
Thanks for the reply, I was unaware that I cant use lighting and colors at the same time. As far as what I was trying to do you are correct I''m trying to draw a triangle using colorfill through glColor3f( ) and then draw an ojbect with a texture on it whose texture is unaffected by the colors used to fill the triangle. Or vs versa as the results change with the order that you draw the objects.
Cheers,

Himilou
garo
garo
I have this simple color stack funktion which I use for storing colors:


    

#define PUSH true

#define POP false

void G_ColorStack(bool store)
{
static float colors[10][4];
static Sint8 i = 0;

if (store) {
if (i > 9) {
G_Error("G_ColorStack: overflow on push!\n");
} else {
glGetFloatv(GL_CURRENT_COLOR, colors[++i]);

}
} else {
if (i < 0) {
G_Error("G_ColorStack: overflow on pop!\n");
} else {
glColor4fv(colors[i--]);
}
}



}



You can use this to store the current color G_ColorStack(PUSH),
then change the color to whatever you wan't, and when done, call G_ColorStack(POP) to restore the original color.

- jtm

[edited by - garo on October 1, 2002 7:18:13 AM]

Topic Locked

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

Sign in to reply to this topic.