Where'd My Colors Go?

Sage
Posts: 1,066
Joined: 2004.07
Post: #1
I finally got normals figured out and decided to try them out with lighting. The cube (without lighting) appears with each vertex being a different color. The lighting worked fine (as far as lighting the sides of my cube) but my colors disappeared. Any ideas on why my colors are gone?

Here's my light code (straight out of the red book):
Code:
GLfloat lightSpecular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat lightShininess[] = { 50.0 };
GLfloat lightPosition[] = { 0.0, 20.0, 5.0, 0.0 };
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, lightSpecular);
glMaterialfv(GL_FRONT, GL_SHININESS, lightShininess);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

And here's the code that draws the cube (or actually one face of the cube):
Code:
//v[#] are vertices; vn[#] are vertex normals; vc[#] are vertex colors
glNormal3f(vn[0].GetX(),vn[0].GetY(),vn[0].GetX());
glColor4f(vc[0].GetR(),vc[0].GetG(),vc[0].GetB(),vc[0].GetA());
glVertex3f(v[0].GetX(),v[0].GetY(),v[0].GetZ());
glNormal3f(vn[1].GetX(),vn[1].GetY(),vn[1].GetX());
glColor4f(vc[1].GetR(),vc[1].GetG(),vc[1].GetB(),vc[1].GetA());
glVertex3f(v[1].GetX(),v[1].GetY(),v[1].GetZ());
glNormal3f(vn[2].GetX(),vn[2].GetY(),vn[2].GetX());
glColor4f(vc[2].GetR(),vc[2].GetG(),vc[2].GetB(),vc[2].GetA());
glVertex3f(v[2].GetX(),v[2].GetY(),v[2].GetZ());
if (vertex[3]!=-1)
{
    glNormal3f(vn[3].GetX(),vn[3].GetY(),vn[3].GetX());
    glColor4f(vc[3].GetR(),vc[3].GetG(),vc[3].GetB(),vc[3].GetA());
    glVertex3f(v[3].GetX(),v[3].GetY(),v[3].GetZ());
}
Quote this message in a reply
Nibbie
Posts: 2
Joined: 2006.10
Post: #2
You need to throw in a glEnable(GL_COLOR_MATERIAL); somewhere if you want the glColor4f() calls to do anything. Otherwise you can mess around with glMaterialfv().
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #3
The glEnable(GL_COLOR_MATERIAL); didn't exactly do much (and by that I mean it did nothing). I'll play with the material thing later and see if that helps.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
"somewhere" meaning "before you render anything" Rasp

yes, glEnable(GL_COLOR_MATERIAL) should fix your problem.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #5
I call glEnable(GL_COLOR_MATERIAL) directly before any of my glColor4f() or glVertex3f() calls and it still doesn't help.

My lighting is also not working correctly because all 5 of my cubes are lighting identically in different spots and the lighting on them doesn't decrease as they move straight away from the camera.

EDIT:
I have it working now. Apparently if I put it in the if(TextureLeve==COLOR) area, it won't work (even though TextureLevel definitely equals color or else there would be a textured cube and not a colored one) but if I move it up where it shows now, it'll work just fine. Thanks for the help.
Code:
void Face::DrawTextured()
{
    glPolygonMode(GL_FRONT,GL_FILL);
    glPolygonMode(GL_BACK,GL_LINE);
    glEnable(GL_COLOR_MATERIAL);
    if (vertex[3]==-1) { glBegin(GL_TRIANGLES); }
    if (vertex[3]!=-1) { glBegin(GL_QUADS); }
    if(TextureLevel==TEXTURE)
    {
              //draw vertices
    }
    if(TextureLevel==COLOR)
    {
        //draw vertices
    }
    glEnd();
}
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
You can't change OpenGL state between Begin and End. If you were checking for OpenGL errors, you would have discovered this fact for yourself Rasp
Quote this message in a reply
Puzzler183
Unregistered
 
Post: #7
I'm pretty sure there is another function to call after the glEnable(GL_COLOR_MATERIAL); that indicates what face and what materials are selected (ambient, diffuse and or specular). I can't remember what it is but that would definitely be a problem...

I'll check on it when I get to a computer from which I can access my engine code.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #8
void glColorMaterial( GLenum face, GLenum mode )
Quote this message in a reply
Puzzler183
Unregistered
 
Post: #9
arekkusu Wrote:void glColorMaterial( GLenum face, GLenum mode )

Er yeah, good call. For arguments, you probably want GL_FRONT and then GL_SPECULAR for your case. Normally I'd just recommend using materials however with texturing.
Quote this message in a reply
Sage
Posts: 1,066
Joined: 2004.07
Post: #10
Puzzler183 Wrote:Normally I'd just recommend using materials however with texturing.

What do you mean by this? Do you mean using actual textures?
Quote this message in a reply
Puzzler183
Unregistered
 
Post: #11
Well, I meant use the materials for coloring a group of polygons and then for more detailed variations in color, just use the texture.
Quote this message in a reply
tigakub
Unregistered
 
Post: #12
There are three ways to color geometry.

1. Using glColor. In the absence of lighting or material, this tells OpenGL to use a specific color to rasterize lines or polygons.

2. Using glMaterial. With lighting enabled, glMaterial specifies how the light interacts with any polygons rasterized. glMaterial allows you to specify several colors: ambient, diffuse, specular. The ambient color is constant whatever the angle of incident light. The diffuse component darkens as the angle of incident light diverges from the surface normal. The specular component affects the highlights (or "shininess") of the surface. The ambient color is mixed with ambient light, the diffuse color is mixed with diffuse light, and the specular color affects specular light. glColorMaterial is simply a faster way to set these color components and really has NOTHING to do with the glColor command.

Edit: oops ... I was wrong: glColorMaterial causes you to be able to set one of the glMaterial component colors using glColor.

3. Using textures. Depending on the texture mode, you can have a texture mix with the underlying color of the geometry, or replace it. Other than that, materials and textures are really quite separate.
Quote this message in a reply
pkraft
Unregistered
 
Post: #13
I am just wondering if there is some kind of flag or blend mode or such you are supposed to set so that the color of the surface is combined with the color or the lighting?
Quote this message in a reply
tigakub
Unregistered
 
Post: #14
experiment with either

glTexEnvi(GL_TEXTURE_ENV_MODE, GL_BLEND);

or

glTexEnvi(GL_TEXTURE_ENV_MODE, GL_MODULATE);

The other possible parameter values are GL_REPLACE and GL_DECAL
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Lighting and changing texture colors in OpenGL agreendev 2 7,523 Aug 13, 2010 03:47 PM
Last Post: agreendev
  Reading texture colors values Leroy 6 4,580 Jul 24, 2007 10:06 PM
Last Post: Leroy
  Remapping of colors? pkraft 6 3,427 Mar 8, 2005 01:51 PM
Last Post: pkraft
  About transparent colors un textures valle 2 2,750 Oct 26, 2004 09:52 AM
Last Post: valle