calculating normal for smooth shading

ummm so what i understand is for the shade model for lighting to be smooth i need to give a normal vector for each vertex…

below is my normal function and then the drawing function with the normal for each vertex…my mesh is made up of QUADS and when i use the following code to draw the light is in triangle shape…

i think the problem is the vertices im passing to normal function…

my each quad is structured as …

mesh[i][0] - lower left
mesh[i][1] - uper left
mesh[i][2] - upper right
mesh[i][3] - lower right

help plz

normal function


void normal (point p1,point p2,point p3,point &normal)
{
    point a, b;
 
    // calculate the vectors A and B
    // note that v[3] is defined with counterclockwise winding in mind
    // a
    a.x = p3.x - p2.x;
    a.y = p3.y - p2.y;
    a.z = p3.z - p2.z;
    // b
    b.x = p1.x - p2.x;
    b.y = p1.y - p2.y;
    b.z = p1.z - p2.z;
 
    // calculate the cross product and place the resulting vector
    // into the address specified by vertex_t *normal
    normal.x = ((a.y * b.z) - (a.z * b.y));
    normal.y = ((a.z * b.x) - (a.x * b.z));
    normal.z = ((a.x * b.y) - (a.y * b.x));
 
    // normalize
    float len = (float)(sqrt((double)((normal.x * normal.x) + (normal.y * normal.y) + (normal.z * normal.z))));
 
    // avoid division by 0
    if (len == 0.0f)
        len = 1.0f;
 
    // reduce to unit size
    normal.x /= len;
    normal.y /= len;
    normal.z /= len;
}

drawing function


void draw()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);	

point norm;
   //sphere2(20,20);
   //glPolygonMode(GL_FRONT_AND_BACK ,GL_LINE);
   
	for(int i=0;i<stack;i++){ // base mesh
		if(mesh[i][3].y==0){		
			if(mesh[i][3].y >=0 && mesh[i][3].y <=30)
				glColor4f(0,0,1,0.6);
		glBegin(GL_QUADS);
      
      
      normal(mesh[i][0],mesh[i][1],mesh[i][3],norm);
      glNormal3f(norm.x,norm.y,norm.z);
      glVertex3i(mesh[i][0].x,mesh[i][0].y,mesh[i][0].z);
		
      normal(mesh[i][1],mesh[i][0],mesh[i][2],norm);
      glNormal3f(norm.x,norm.y,norm.z);
      glVertex3i(mesh[i][1].x,mesh[i][1].y,mesh[i][1].z);
		
      normal(mesh[i][2],mesh[i][1],mesh[i][3],norm);
      glNormal3f(norm.x,norm.y,norm.z);
      glVertex3i(mesh[i][2].x,mesh[i][2].y,mesh[i][2].z);
		
      normal(mesh[i][3],mesh[i][0],mesh[i][2],norm);
      glNormal3f(norm.x,norm.y,norm.z);      
      glVertex3i(mesh[i][3].x,mesh[i][3].y,mesh[i][3].z);
	glEnd();
		}
	}
  
   for(int i=0;i<=mesh2count;i++) {  	      // print elevated mesh
			            
      glColor3f(0.0,0.6,0.0); 
      int j=0;
      if(i>=41*21 && i<=41*28) {
         glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mbdiffuse_ambient);         
         glMaterialfv(GL_FRONT, GL_SPECULAR, mbspecular);
      }
		glBegin(GL_QUADS);
            normal(mesh[i][0],mesh[i][1],mesh[i][3],norm);
            glNormal3f(norm.x,norm.y,norm.z);
				glVertex3i(mesh2[i][0].x,mesh2[i][0].y,mesh2[i][0].z);
				
            normal(mesh[i][0],mesh[i][1],mesh[i][2],norm);
            glNormal3f(norm.x,norm.y,norm.z);
            glVertex3i(mesh2[i][1].x,mesh2[i][1].y,mesh2[i][1].z);
				
            normal(mesh[i][2],mesh[i][1],mesh[i][3],norm);
            glNormal3f(norm.x,norm.y,norm.z);            
            glVertex3i(mesh2[i][2].x,mesh2[i][2].y,mesh2[i][2].z);
				
            normal(mesh[i][0],mesh[i][3],mesh[i][2],norm);
            glNormal3f(norm.x,norm.y,norm.z);                  
            glVertex3i(mesh2[i][3].x,mesh2[i][3].y,mesh2[i][3].z);
			glEnd();
         glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,mdiffuse_ambient);
         glMaterialfv(GL_FRONT, GL_SPECULAR, mspecular);
		}        
}

thanks
Raza

Hi,

is your question “how to calculate the smooth normal”?

If so, this has a good explanation:
http://www.lighthouse3d.com/opengl/terrain/index.php3?normals

As you can see in the last picture, you basically sum up the face normals for the faces around the vertex, and normalize the result.