Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //before drawing after you read everything
- std::vector<coordinate*> vertexnormals;
- bool isvertexnormals=true;
- //...
- if(isvertexnormals) //a boolean, so you it's can be turned on/off
- {
- for(int i=0;i<vertex.size();i++) //go through all vertex
- {
- float vecx=0,vecy=0,vecz=0;
- int num=0;
- for(int j=0;j<faces.size();j++) //than all faces
- {
- 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
- {
- vecx+=normals[faces[j]->facenum-1]->x; //get the avarage
- vecy+=normals[faces[j]->facenum-1]->y;
- vecz+=normals[faces[j]->facenum-1]->z;
- num++;
- }
- }
- if(num)
- {
- vecx/=num;
- vecy/=num;
- vecz/=num;
- }
- //finally normalize the vector (set the length to one)
- float d=sqrt(vecx*vecx+vecy*vecy+vecz*vecz);
- if(d)
- {
- vecx/=d;
- vecy/=d;
- vecz/=d;
- }
- vertexnormals.push_back(new coordinate(vecx,vecy,vecz));
- }
- }
- //and when you draw the object:
- if(isvertexnormals) glNormal3f(vertexnormals[faces[i]->faces[0]-1]->x,vertexnormals[faces[i]->faces[0]-1]->y,vertexnormals[faces[i]->faces[0]-1]->z);
- //draw 1st vector
- if(isvertexnormals) glNormal3f(vertexnormals[faces[i]->faces[1]-1]->x,vertexnormals[faces[i]->faces[1]-1]->y,vertexnormals[faces[i]->faces[1]-1]->z);
- //draw 2nd vector
- //...
Advertisement
Add Comment
Please, Sign In to add comment