Front and back face with diferent colors using index arrays?

Hello,

I can’t manage to render front and back face with diferent colors using index arrays.

I have tried to use glColorMaterial but it seems to have only an effect on one side. Can anyone help me? Maybe post some code?

Thanks,

Billy.

glEnable(GL_CULL_FACE);

// Draw Front faces
glCullFace(GL_BACK);
glColor4fv(colFrontFace);
/* Draw here */

// Draw Back faces;
glCullFace(GL_FRONT);
glColor4fv(colBackFace);
/* Draw here */

You could also use a vertex program, which gives you complete freedom in how you interpret the different vertex attributes.

You can implement a two-sided lighting model with separate front-facing and back-facing color arrays easily, e.g. by using the secondary vertex color instead of the backfacing material color. Your vertex program would have to output to “result.color.front” and “result.color.back” instead of just “result.color”.

– Tom

For constant materials try the core OpenGL functionality with glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1) and use the standard material properties to set a different material for front and back faces.
It’s also allowed to call glMaterial per vertex, but that’s going to be slow, trust me.
If you have prelit-geometry with vertex colors for front and back in arrays then the two previous answers apply.

Originally posted by Relic:
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1)

I believe that in certain situations that will bump you back to software (or degrade your performance). I think from memory that VAR doesn’t like two sided lighting? (Perhaps that has changed?)

Originally posted by rgpc:
I believe that in certain situations that will bump you back to software (or degrade your performance). I think from memory that VAR doesn’t like two sided lighting? (Perhaps that has changed?)

The original nVidia paper on VAR said not to use two sided lighting because that would result in software rendering, which would take forever and a day when the data was in video or AGP memory. This was true at the time. However, at least from GeForce 3 on, the hardware T&L supports two-sided lighting, and it runs as fast as you would expect (taking into account that two-sided lighting will be slower than using twice as many lights with one-sided lighting).

If you’re using a directional light without specular, you can simply insert a second directional light pointing in the opposite direction and get the same effect as two-sided lighting, but with better performance (plus you don’t have to require GeForce 3 or higher).

In any event, as long as you want one face (the back face, I would imagine) to be a constant color, you should be able to use glMaterial to set the color of that face. Other than the vertex program suggestions already posted, I doubt there is a way to have each vertex have a unique front and back color that changes.

Thanks rgpc. It works now.