thecplusplusguy

Smooth OpenGL

Aug 11th, 2011
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1.     //before drawing after you read everything
  2.     std::vector<coordinate*> vertexnormals;
  3.     bool isvertexnormals=true;
  4.     //...
  5.     if(isvertexnormals) //a boolean, so you it's can be turned on/off
  6.     {
  7.         for(int i=0;i<vertex.size();i++)    //go through all vertex
  8.         {
  9.             float vecx=0,vecy=0,vecz=0;
  10.             int num=0;
  11.             for(int j=0;j<faces.size();j++) //than all faces
  12.             {
  13.                 if(faces[j]->faces[0]==i+1 || faces[j]->faces[1]==i+1 || faces[j]->faces[2]==i+1 || faces[j]->faces[3]==i+1)    //if the face is around the vertex
  14.                 {
  15.                     vecx+=normals[faces[j]->facenum-1]->x;  //get the avarage
  16.                     vecy+=normals[faces[j]->facenum-1]->y;
  17.                     vecz+=normals[faces[j]->facenum-1]->z;
  18.                     num++;
  19.                 }
  20.             }
  21.             if(num)
  22.             {
  23.                 vecx/=num;
  24.                 vecy/=num;
  25.                 vecz/=num;
  26.             }
  27.             //finally normalize the vector (set the length to one)
  28.             float d=sqrt(vecx*vecx+vecy*vecy+vecz*vecz);
  29.             if(d)
  30.             {
  31.                 vecx/=d;
  32.                 vecy/=d;
  33.                 vecz/=d;
  34.             }
  35.             vertexnormals.push_back(new coordinate(vecx,vecy,vecz));
  36.         }
  37.     }
  38.  
  39. //and when you draw the object:
  40. if(isvertexnormals)                 glNormal3f(vertexnormals[faces[i]->faces[0]-1]->x,vertexnormals[faces[i]->faces[0]-1]->y,vertexnormals[faces[i]->faces[0]-1]->z);
  41. //draw 1st vector
  42.  
  43. if(isvertexnormals)                 glNormal3f(vertexnormals[faces[i]->faces[1]-1]->x,vertexnormals[faces[i]->faces[1]-1]->y,vertexnormals[faces[i]->faces[1]-1]->z);
  44. //draw 2nd vector
  45. //...
Advertisement
Add Comment
Please, Sign In to add comment