I m a new member here.I am now writing a program that render Vertex Normals.I m now trying to implement a data structure that will best suit computing vertex normals.For that computation , my data structure should have accessed to the faces adjacent to each vertices.
first i read all vertices ,and faces from .m file into a structure.And stored in vectors.
vector<Vertex> vertices;
vector<Face> faces;
Then i render that model.No error and problem till now.
After that i created "poly" vector to store the faces adjacent to each vertices.
vector<Polygon> poly;
For all vertices,I find the faces that share a vertex and stored in poly.Following is the code i wrote for this.
Expand|Select|Wrap|Line Numbers
- //to search faces that share a vertex
- for (int i =0; i <(int)vertices.size(); i++)//loop1
- {
- int s=0;
- Polygon polygon;
- Face face;
- Vertex vertex;
- vertex=vertices.at(i);
- polygon.incident=0;
- vertex.Incident=0;
- for (int j = 0; j < (int)faces.size(); j ++)//loop2
- {
- if(faces[j].v1==vertices[i].vertexid )
- {
- face=faces.at(j);
- //vertex=vertices.at(i);
- polygon.faceid=face.faceid;
- polygon.v2=face.v1;
- polygon.v1=face.v2;
- polygon.v3=face.v3;
- polygon.v2x=vertex.x;
- polygon.v2y=vertex.y;
- polygon.v2z=vertex.z;
- for (int k = 0; k < (int)vertices.size(); k ++)//loop 3
- { vertex=vertices.at(k);
- if(faces[j].v2==vertices[k].vertexid )
- {
- polygon.v1x=vertex.x;
- polygon.v1y=vertex.y;
- polygon.v1z=vertex.z;
- }
- if(faces[j].v3==vertices[k].vertexid )
- {
- polygon.v3x=vertex.x;
- polygon.v3y=vertex.y;
- polygon.v3z=vertex.z;
- }
- }//loop 3
- s++;
- }
- else if(faces[j].v2==vertices[i].vertexid )
- {
- face=faces.at(j);
- //vertex=vertices.at(i);
- polygon.faceid=faces[j].faceid;
- polygon.v2=faces[j].v2;
- polygon.v1=faces[j].v1;
- polygon.v3=faces[j].v3;
- polygon.v2x=vertices[i].x;
- polygon.v2y=vertices[i].y;
- polygon.v2z=vertices[i].z;
- for (int k = 0; k < (int)vertices.size(); k ++)//loop 3
- {
- vertex=vertices.at(k);
- if(faces[j].v1==vertices[k].vertexid )
- {
- polygon.v1x=vertex.x;
- polygon.v1y=vertex.y;
- polygon.v1z=vertex.z;
- }
- if(faces[j].v3==vertices[k].vertexid )
- {
- polygon.v3x=vertex.x;
- polygon.v3y=vertex.y;
- polygon.v3z=vertex.z;
- }
- }//loop 3
- s++;
- }
- else if(faces[j].v3==vertices[i].vertexid )
- {
- face=faces.at(j);
- //vertex=vertices.at(i);
- polygon.faceid=faces[j].faceid;
- polygon.v2=faces[j].v3;
- polygon.v1=faces[j].v1;
- polygon.v3=faces[j].v2;
- polygon.v2x=vertices[i].x;
- polygon.v2y=vertices[i].y;
- polygon.v2z=vertices[i].z;
- for (int k = 0; k < (int)vertices.size(); k ++)//loop 3
- {
- vertex=vertices.at(k);
- if(faces[j].v1==vertices[k].vertexid )
- {
- polygon.v1x=vertex.x;
- polygon.v1y=vertex.y;
- polygon.v1z=vertex.z;
- }
- if(faces[j].v2==vertices[k].vertexid )
- {
- polygon.v3x=vertex.x;
- polygon.v3y=vertex.y;
- polygon.v3z=vertex.z;
- }
- }//loop 3
- s++;
- }//else if
- }//loop2
- polygon.incident=s;//no of adjacent faces per vertex
- //cout<<"before vertex pointer"<<vertex.vp<<"\n";
- vertex.vp=&poly;
- vertices[i]=vertex;
- }//loop1
- //
- }
Thanks in advance.