Hi all,
I have a really stupid problem !! it is so stupid I am a little embraced to ask about it, but it is driving me crazy so I will any how…(-:
my problem is:
I am drawing a triangle strip, and I am switching colors(using glMaterialfv) between triangles, BUT the intire strip is always painted by the material color of the last vertex in the strip !!!
The code is very basic, the init is :
// Perspective
::glMatrixMode(GL_PROJECTION);
::glLoadIdentity();
double aspect = (rect.Height() == 0) ? rect.Width() :
(double)rect.Width()/(double)rect.Height();
::gluPerspective(45, aspect, 0.1, 1000.0);
setSize(rect.Width(), rect.Height());
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Lights properties
float ambientProperties[] = { 0.2f, 0.2f, 0.2f, 1.0f};
float diffuseProperties[] = { 0.5f, 0.5f, 0.5f, 1.0f};
float specularProperties[] = { 0.1f, 0.1f, 0.1f, 1.0f};
//enable lighting.
::glEnable(GL_LIGHTING);
::glEnable(GL_LIGHT0);
::glEnable(GL_LIGHT1);
::glLightfv(GL_LIGHT0, GL_AMBIENT, ambientProperties);
::glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseProperties);
::glLightfv(GL_LIGHT0, GL_SPECULAR, specularProperties);
::glLightfv(GL_LIGHT1, GL_AMBIENT, ambientProperties);
::glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuseProperties);
::glLightfv(GL_LIGHT1, GL_SPECULAR, specularProperties);
//set the lighting position
setLightPosition();
::glShadeModel(GL_SMOOTH);
::glEnable(GL_NORMALIZE);
//set the black default clear color
float black[4] = {0.0f, 0.0f, 0.0f, 1.0f};
setBcgColor(black);
//enable the Z buffer
::glEnable(GL_DEPTH_TEST);
and the function I fill the display list is:
GLfloat SurfaceColor[4];
//make sure that there is content to draw
if(!isValid())
return false;
//create/replace the list located at listNumber
::glNewList(listNumber,GL_COMPILE);
glColor3f (1.0, 1.0, 1.0);
//check according to the transparency level of the object
//if to enable a blending function
if(getAlphaTransparencyLevel() < 1.0f)
{
::glBlendFunc(GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA);
::glEnable(GL_BLEND);
::glDepthMask (GL_FALSE);
}
//render the trinagle strip
::glBegin(GL_TRIANGLE_STRIP);
for(int i=0 ; i<getVerticesArraySize() ; i++)
{
CVertex& v = getVertex(i);
//set the vertex normal
::glNormal3f(v.getNormal().getX(), v.getNormal
().getY(), v.getNormal().getZ());
SurfaceColor[0] = v.getColor().getRedValue();
SurfaceColor[1] = v.getColor().getGreenValue();
SurfaceColor[2] = v.getColor().getBlueValue();
SurfaceColor[3] = v.getColor().getAlphaValue();
//set the vertex color (ambient diffuse and
//specular properties
::glMaterialfv(GL_FRONT_AND_BACK,
GL_AMBIENT_AND_DIFFUSE , SurfaceColor);
::glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,
SurfaceColor);
//set the vertex geometry.
::glVertex3f(v.getX(), v.getY(), v.getZ());
}
::glEnd();
//if we were drawing a transparent object turn off the
//blending function and turn back on the Z buffer
if(getAlphaTransparencyLevel() < 1.0f)
{
::glDepthMask (GL_TRUE);
::glDisable(GL_BLEND);
}
::glEndList();
return true;
I have tried all the trivial tests like switching off the lights and making sure that the glMaterialfv gets the proper material colors in SurfaceColor, I also tried changing the glMaterialfv calls to
::glColor4fv( SurfaceColor); calls, but for some reason then the strip becomes black and white……
what basic thing am I missing ? (just when I thought I was turning from an opengl beginner to an opengl intermediate….)-
thanks in advance.