Sign in to follow this  
mello

Textured square

Recommended Posts

Hello community, I've a really simple function to draw a square with a white pavement texture:
void draw_square(float size[], float translation[])
{    
     glPushMatrix();
     glTranslatef(translation[0], translation[1], translation[2]);

     glColor3ub(255,0,0); //just to force OpenGL to draw in RED 
     glDisable(GL_COLOR_MATERIAL); //supposely, the RED color should be disabled

     glEnable(GL_TEXTURE_2D);
     glBindTexture(GL_TEXTURE_2D, 1]); //bind a WHITE pavement texture
     int rep = 5; 
     glBegin(GL_POLYGON);
        glNormal3d(0.0, 1.0, 0.0);
	glTexCoord2f(0.0,0.0); glVertex3d(-size[0]/2, size[1]/2, size[2]/2);
	glTexCoord2f(rep,0.0); glVertex3d(size[0]/2, size[1]/2, size[2]/2);
	glTexCoord2f(rep,rep); glVertex3d(size[0]/2, size[1]/2, -size[2]/2);
	glTexCoord2f(0.0,rep); glVertex3d(-size[0]/2, size[1]/2, -size[2]/2);
     glEnd();
     
     glDisable(GL_TEXTURE_2D);
     glEnable(GL_COLOR_MATERIAL);  //OpenGL should draw in RED again

     glPopMatrix;
}
The problem is that I get a RED textured square and not a WHITE. It seems that the glDisable(GL_COLOR_MATERIAL); is not working at all! Actually, if I comment that line I get the same results! Any ideas on how to solve this color problem? Thanks a lot, mello

Share this post


Link to post
Share on other sites
The GL_COLOR_MATERIAL state controls whether or not a glColor call updates the current material state, not whether the current object is rendered with the current material colour. In pseudocode:
void glColor(colour)
{
current_colour = colour;
if (GL_COLOR_MATERIAL is enabled)
{
current_material.colour = colour;
}
}

So disabling GL_COLOR_MATERIAL after you set the colour is too late. The material colour has already been set to red. You probably want to either ignore GL_COLOR_MATERIAL and just set the colour to white before rending your textured quad, explicitly set the material colour to white separately using glMaterial, or disable GL_COLOR_MATERIAL before any colour changes.

Σnigma

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this