PDA

View Full Version : About Phong Shading



arrowyang
11-03-2003, 07:38 PM
I have need to do phong shading...
In my former homework, I have finished phong shading (vertex rendering with interpolation normals) by myself. This time, I have to do similar work in OpenGL environment. Now, I got a idea from the notes of OpenGL website. That is to do two-times rendering: The first time only draws AMBIENT and DIFFUSE reflectance, and the second time draws the SPECULAR and SHININESS with BLEND function (glBlendFunc(GL_ONE, GL_ONE), like draw a mask and paste on the former picture.

But I have a question that I don't know how to draw two times in display function(). I list my code here and hope some kind guys could give me a hint. Your help is greatly appreciated!!


GLfloat vArray[4];

void display(CPolygon objPolygon, CMaterial objMaterial)
{
glBegin(GL_POLYGON);
// Vertexes
for(int k=0; k < objPolygon.Vertexes.size(); k++) {
// Normal
CVector3D& objVector=objPolygon.Normals.getItem(objFacet.Vert exes[k] );
vArray[0]=objVector.x;
vArray[1]=objVector.y;
vArray[2]=objVector.z;
glNormal3fv(vArray);

CPoint3D& objVertex=objPolygon.Vertexes.getItem(objFacet.Ver texes[k] );
vArray[0]=objVertex.x;
vArray[1]=objVertex.y;
vArray[2]=objVertex.z;
glVertex3fv(vArray);
} // for k

// first time rendering
glPushMatrix();
vArray[0]=objMaterial.Ambient.R; vArray[1]=objMaterial.Ambient.G; vArray[2]=objMaterial.Ambient.B; vArray[3]=1.0;
glMaterialfv(GL_FRONT, GL_AMBIENT, vArray);

vArray[0]=objMaterial.Diffuse.R; vArray[1]=objMaterial.Diffuse.G; vArray[2]=objMaterial.Diffuse.B; vArray[3]=1.0;
glMaterialfv(GL_FRONT, GL_DIFFUSE, vArray);
glPopMatrix();

// second time rendering - but doesn't work
glPushMatrix();
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);

vArray[0]=objMaterial.Specular.R; vArray[1]=objMaterial.Specular.G; vArray[2]=objMaterial.Specular.B; vArray[3]=1.0;
glMaterialfv(GL_FRONT, GL_SPECULAR, vArray);

vArray[0]=1/objMaterial.Shininess;
glMaterialfv(GL_FRONT, GL_SHININESS, vArray);

glDisable(GL_BLEND);
glPopMatrix();
glFlush();
}

V-man
11-04-2003, 08:26 AM
Could be a simple problem like depthFunc
Try setting it to GL_LEQUAL

Anyways, what you are doing there doesnt sound like phong shading.