View Full Version : glColor3f Question
I have added colored vertice to a few NeHe programs, and it worked fine, but in my gold program I want to color the flag and flagpole, but they won't work! It looks as if I just didn't add glColor3f in at all! Any ideas why this wouldn't work?
glPushMatrix();
glTranslatef( -holex, -holey, -holez );
glBegin( GL_TRIANGLES );
glColor3f(0.8f,1.0f,0.2f);
glVertex3f(0, -8, 0);
glColor3f(0.8f,1.0f,0.2f);
glVertex3f(0, -10, 0);
glColor3f(0.6f,0.8f,0.1f);
glVertex3f(-2.5, -9, 0);
glEnd();
glRotatef(90,1.0f,0.0f,0.0f);
glColor3f(1.0f,0.0f,0.0f);
gluCylinder(quadric2, 0.14, 0.14, 10, 16, 16);
glColor3f(1.0f,1.0f,1.0f);
glPopMatrix();
MattDiamond
2003.10.05, 07:48 PM
Maybe you turned on OpenGL lighting, or texture mapping? Those will cause the color to be ignored in favor of material properties or texels.
OneSadCookie
2003.10.05, 09:11 PM
Minor correction -- lighting will override vertex colors unless you've enabled COLOR_MATERIAL; texturing will only override vertex colors if you've set the texture environment to something like REPLACE (as opposed to the default MODULATE).
glEnable( GL_COLOR_MATERIAL ); seamed to fix the problem, but now my lighting is broke, how can I do both at the same time?
Look in the NeHe tutorial... there is a line where he makes the comment about something you have to set to fix that. I don't have the time right now to look it up, however. :(
Mars_999
2003.10.06, 01:38 AM
You might try disabling texturing before you call glColor3f() and enable texturing after you are done with glColor3f(). Ok, I am really tired right now, but I am going to ask this because for some reason I ran into this problem if I can remember correctly due to it has been awhile. Are you using mipmapping? If so is it setup correctly? I can't remember for sure but seems like I was missing my lighting because I had a texture parameter set to MIPMAP instead of GL_LINEAR? :???:
Hum, still no luck, here is my mipmap code, is it correct?
glBindTexture( GL_TEXTURE_2D, texture[ textureOn ] );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, size[textureOn], size[textureOn], GL_RGB,GL_UNSIGNED_BYTE, [imageRep[textureOn] bitmapData]);
I will start digging in NeHe later tonight if I have to :(, thanks for the tips.
Mars_999
2003.10.07, 07:37 PM
Originally posted by Jake
Hum, still no luck, here is my mipmap code, is it correct?
glBindTexture( GL_TEXTURE_2D, texture[ textureOn ] );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, size[textureOn], size[textureOn], GL_RGB,GL_UNSIGNED_BYTE, [imageRep[textureOn] bitmapData]);
I will start digging in NeHe later tonight if I have to :(, thanks for the tips.
Code looks ok, must be usign ObjC with Cocoa? Anyway I would use the GL_RGB constants for the 2nd parameter instead of 3 but that is just me. I would try commenting out the GL_COLOR_MATERIAL and see if lighting shows up again?
Originally posted by Mars_999
I would try commenting out the GL_COLOR_MATERIAL and see if lighting shows up again?
I did, and it works, except then the colors don't work on the flagpole.
Mars_999
2003.10.07, 11:48 PM
What are the parameters you are sending to glColorMaterial()?
GL_FRONT GL_BACK GL_FRONT_AND_BACK?? Which mode are you using?
After you call glColorMaterial(GL_FRONT_AND_BACK/*depends on if CCW or CW windings*/, GL_AMBIENT_AND_DIFFUSE)
you need to call
glEnable(GL_COLOR_MATERIAL);
You need to call glColorMaterial() with the correct parameters to change the different lighting modes.
Are you disabling after you are done?
glDisable(GL_COLOR_MATERIAL)
Do you have GL_DEPTH_TEST enabled?
glEnable(GL_LIGHTING) and glEnable(GL_LIGHT0)?
I personally use glMaterial() instead maybe you should give that a try?
Ok, I'm really confused, so here is my code
:wacko: :wacko: :wacko:
glPushMatrix();
glTranslatef( -holex, -holey, -holez );
glDisable( GL_TEXTURE_2D );
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable( GL_COLOR_MATERIAL );
glBegin( GL_TRIANGLES );
glColor3f(0.8f,1.0f,0.2f);
glVertex3f(0, -8, 0);
glColor3f(0.8f,1.0f,0.2f);
glVertex3f(0, -10, 0);
glColor3f(0.6f,0.8f,0.1f);
glVertex3f(-2.5, -9, 0);
glEnd();
glRotatef(90,1.0f,0.0f,0.0f);
glColor3f(1.0f,0.0f,0.0f);
gluCylinder(quadric2, 0.14, 0.14, 10, 16, 16);
glColor3f(1.0f,1.0f,1.0f);
glDisable( GL_COLOR_MATERIAL );
glEnable( GL_TEXTURE_2D );
glPopMatrix();
Thanks for helping me through this
Mars_999
2003.10.08, 11:57 PM
I would try using glMaterial() instead.
float material_Ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
float material_Diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
float light_Position[] = {0.0f, 0.0f, 0.0f, 1.0f};
float ambient_Light[] = {.3f, .5f, .8f, 1.0f};
float diffuse_Light[] = {.25f, .25f, .25f, 1.0f};
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_Light);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_Light);
glLightfv(GL_LIGHT0, GL_POSITION, light_Position);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialf(GL_FRONT, GL_AMBIENT, material_Ambient);
glMaterialf(GL_FRONT, GL_DIFFUSE, material_Diffuse);
Try this instead? Not sure if that is what your looking for? You will have to play around with the values in the arrays to change the effects of the lighting.
BTW have you done your normals calculations?? And have you enabled normals?
Thanks for the help, I just added a texture instead of using colors.
Yes about normals, they are implemented and looking great :)
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.