PDA

View Full Version : Vertex color with lighting enabled



02-04-2006, 09:05 AM
Vertex colors dont seem to have an affect on my lighting. All my polygons just look grey (or whatever color I make my light) even though I assign specific colors to them. I must have something I need disabled, but I couldnt find any answers in the man pages.


#include <GLUT/glut.h>

double aspect;
double anglex, angley, manglex, mangley;

GLfloat light_position[] = {4, 0, 0, 0};
GLfloat light_specularity[] = {1, 1, 1, 1};
GLfloat mat_ambient[] = {.2, .2, .2, 1};

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/*
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex2f(0, 1);
glColor3f(0, 1, 0);
glVertex2f(1, -1);
glColor3f(0, 0, 1);
glVertex2f(-1, -1);
glEnd();
*/
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
glPushMatrix();
glRotated(manglex, 0, 1, 0);
glRotated(mangley, 0, 0, -1);
glColor3f(1, 0, 0);
glutSolidTeapot(1);
glPopMatrix();
glRotated(anglex, 0, 1, 0);
glRotated(angley, 0, 0, -1);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (aspect > 1) {
glFrustum(-aspect, aspect, -1, 1, 1, 20);
} else {
glFrustum(-1, 1, -1/aspect, 1/aspect, 1, 20);
}
glTranslated(0, 0, -4);
glFlush();
glutSwapBuffers();
}

void reshape(int x, int y)
{
glViewport(0, 0, x, y);
aspect = (double)x / (double)y;
}

void init(void)
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specularity);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
}

void idle(void)
{
display();
}

void motion(int x, int y)
{
anglex = x / 2;
angley = y / 2;
display();
}

void activemotion(int x, int y)
{
manglex = x / 2;
mangley = y / 2;
display();
}

int main(int argc, char *argv[])
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("dick");
init();
glutPassiveMotionFunc(motion);
glutMotionFunc(activemotion);
glutIdleFunc(idle);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

songho
02-04-2006, 11:22 PM
When lighting is enabled, glColor*() will be ignored.
Instead, the material properties of a vertex (ambient, diffuse, specular) will be used to compute the colour of the vertex.

I see the ambient material, but I cannot see diffuse and specular material in your code. The default value of diffuse material is (0.8, 0.8, 0.8) and default specular material is (0,0,0), which means no specular reflection. That is why you see only gray gradient.

For example, to make the object red with white highlight:

GLfloat redColor[4] = {1,0,0,1};
GLfloat whiteColor[4] = {1,1,1,1};
...
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, redColor);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, whiteColor);
glutSolidTeapot(1);
...Or, you can still use glColor*() to change color of surface if you enable GL_COLOR_MATERIAL. This method is less expensive and simpler than glMaterial*().

// put this in your init()
// track material ambient and diffuse from surface color
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
...

// same red surface
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specularColor); // for white highlight
glColor4fv(redColor); // for ambient and diffuse
glutSolidTeapot(1);
...I have notice that you set the projection matrix every frame. It is okay to set it only once in your reshape() callback function.

des
02-05-2006, 05:22 PM
Sorry for my English.
I'm not sure, but try that:

glEnable(GL_BLEND);