reset password
Author Message
GeorgeAria
Posts: 14
Posted 09:00 Oct 27, 2016 |

//###################### Mesh #######################
class Mesh {
private:
    int numVerts; // number of vertices in the mesh
    Point3* pt; // array of 3D vertices
    int numNormals; // number of normal vectors for the mesh
    Vector3 *norm; // array of normals
    int numFaces;  // number of faces in the mesh
    Face* face; // array of face data
                // ... others to be added later
public:
    Mesh();  // constructor
    ~Mesh(); // destructor
    int readmesh(char * fileName); // to read in a filed mesh
};

 

 

int Mesh::readmesh(char * fileName)
{
    fstream infile;
    infile.open(fileName, ios::in);
    if (infile.fail()) return -1; // error - can’t open file
    if (infile.eof()) return -1; // error - empty file
    infile >> numVerts >> numNormals >> numFaces;
    pt = new Point3[numVerts];
    norm = new Vector3[numNormals];
    face = new Face[numFaces];
    //check that enough memory was found:
    if (!pt || !norm || !face)return -1; // out of memory
    for (int p = 0; p < numVerts; p++) // read the vertices
        infile >> pt[p].x >> pt[p].y >> pt[p].z;
    for (int n = 0; n < numNormals; n++) // read the normals
        infile >> norm[n].x >> norm[n].y >> norm[n].z;
    for (int f = 0; f < numFaces; f++)// read the faces
    {
        infile >> face[f].nVerts;
        face[f].vert = new VertexID[face[f].nVerts];
        for (int i = 0; i < face[f].nVerts; i++)
            infile >> face[f].vert[i].vertIndex
            >> face[f].vert[i].normIndex;
    }
    return 0; // success
}

Last edited by GeorgeAria at 09:17 Oct 27, 2016.