Ejemplo n.º 1
0
Chain VertexStream::readObj(const Chain & fname) {
    InputBuffer in(fname.c_str());
    Chain src = in.readString(in.get_Size()).c_str();
    src.setDelimiters("\n\r");
    int numValues;
    Chain token;

    if (!src.length()) {
        return "The file could not be opened";

    }
    vector<GLfloat> OBJvertexList;
    vector<GLfloat> OBJtexCoords;
    vector<GLfloat> OBJnormals;
    vector<GLfloat> OBJspaceCoords;


    vector<GLfloat> OBJfaceVertex;



    src.nextLink();
    while(src.linksLeft()) {
        Chain line;
        line.setDelimiters(" ");
        line = src.getLink().c_str();
        //cout << "Currentline: {" << line << "}" << endl;

        if (line.getLink()[0] == '#') {
            // read comment
            cout << "Reading a comment..." << endl;
            src++;

        } else if (line.getLink() == "v") {
            // read vertices
            Chain vertexLine;
            vertexLine.setDelimiters(" \t");
            vertexLine  = line.c_str();


            numValues = 0;
            cout << "Read vertex {";
            vertexLine++; // skip v
            while(vertexLine.linksLeft()) {


                numValues++;
                OBJvertexList.push_back(atof(vertexLine.getLink().c_str()));
                cout << vertexLine.getLink().c_str() << ", ";
                vertexLine++;
            }


            if (numValues == 3) {
                OBJvertexList.push_back(1.f);
                cout << "1.f";
            }

            cout << "}" << endl;

            src++;

        } else if (line.getLink() == "vt") {
            // texture coords
            Chain textureLine;
            textureLine.setDelimiters(" \t");
            textureLine = line.c_str();

            int numCoords = 0;
            cout << "Read texCoords {";
            textureLine++; // skip vt
            while(textureLine.linksLeft() && numCoords < 2) {

                cout << textureLine.getLink().c_str() << ", ";
                OBJtexCoords.push_back(atof(textureLine.getLink().c_str()));
                numCoords++;

                textureLine++;
                // discard optional 3rd coordinate
            }

            cout << "}\n";
            src++;
        } else if (line.getLink() == "vn") {
            // normals
            Chain normalLine;
            normalLine.setDelimiters(" \t");
            normalLine = line.c_str();
            cout << "read normal {";
            normalLine++; // skip vn
            while(normalLine.linksLeft()){
                cout << normalLine.getLink() << ", ";
                OBJnormals.push_back(atof(normalLine.getLink().c_str()));
                normalLine++;

            }

            cout << "}" << endl;

            src++;

        } else if (line.getLink() == "f") {

            // the face definitions
            DrawBuffer::DBufferEntry face;
            int link = 0,
                iter = 0,
                vertices = 0;
            Chain faceDef;
            Chain faceLine = line.c_str();
            faceLine.setDelimiters(" \t");

            GLfloat * current;
            faceLine++; // exhaust 'f'
            face.useTex = -1.f;
            face.colorR = rand() / (float) (rand() + 1);
            face.colorG = rand() / (float) (rand() + 1);
            face.colorB = rand() / (float) (rand() + 1);
            face.colorA = 1.f;
            face.texX = -99;
            face.texY = -98;

            cout << "read face |src = " << faceLine << endl;

            while(faceLine.linksLeft()) {
                vertices++;


                faceDef = faceLine.getLink();
                faceDef.setDelimiters("/");
                link = 0;
                while(faceDef.linksLeft()) {
                    token = faceDef.getLink().c_str();
                    iter = atoi(token.c_str()) -1;

                    if (link == 0) {// vertices
                        current = &OBJvertexList[iter*4];
                        face.x = current[0];
                        face.y = current[1];
                        face.z = current[2];
                        face.w = current[3];
                        cout << "added vertex {" <<
                            face.x << ", " <<
                            face.y << ", " <<
                            face.z << ", " <<
                            face.w << " } src = " << token << endl;
                    }

                    if (link == 1) { //texCoords
                        current = &OBJtexCoords[iter*2];
                        face.texX = current[0];
                        face.texY = current[1];
                        //face.useTex = 1.f;
                        cout << "added texture coord {" <<
                            face.texX << ", " <<
                            face.texY << ", " << " } src = " << token << endl;

                    }

                    if (link == 2) { // normals
                        cout << "Would have read normal (src = " << token << ")" << endl;
                    }

                    link++;
                    faceDef++;
                }
                faceLine++;
                data.push_back(face);

            }
            if (vertices == 3) {
                drawMode = GL_TRIANGLES;
            } else {
                drawMode = GL_QUADS;
            }
            src++;

        } else if (line.getLink() == "mtllib"){
            // material library external file
            // loadMaterial(filename);
            // add material properties to vertexData
            ///  so texture names, light reflection vals, etc.
            ///  when Graphics gets a well-formed vertexStream,
            ///  it will store the textures
            src++;
        } else if (line.getLink() == "usemtl") {
            // material external file

            src++;
        } else if (line.getLink() == "s") {
            src++;
        } else if (line.getLink() == "o") {
            src++;
        } else if (line.getLink() == "vp") {
            src++;

        } else {

                Chain out;
                out << "Unexpected string " << src.getLink() << ". Failed to load OBJ\n";
                cout << endl << "Unexpected string " << src.getLink().c_str() << ". Failed to load OBJ" << endl;
                return out;
        }
    }

    return "";
}
Ejemplo n.º 2
0
Chain VertexStream::readPly(const Chain & fname) {
    InputBuffer in(fname.c_str());
    Chain src = in.readString(in.get_Size()).c_str();
    int stage = 0;


    Chain format   = "ascii";
    Chain version  = "0.f";
    Chain comments = "";
    vector<vector<GLfloat>> properties;
    vector<Chain> propertyNames;
    vector<vector<GLfloat>> elements;
    vector<Chain> elementNames;
    int curElement = 0;

    // header

    if (src.getLink() != "ply") {
        return ("Header incorrect. Missing \" ply \".");
    }
    src++;

    while (stage == 0 && src.linksLeft()) {
        Chain curToken = src.getLink(); src++;

        // reading format
        if (curToken == "format") {
            if (src.getLink() != "ascii" ||
                src.getLink() != "binary_big_endian" ||
                src.getLink() != "binary_little_endian") {
                    Chain out = "Header error: incorrect or unsupported format listed";
                    out += src.getLink();
            } else  {
                format = src.getLink();
            }
            src++;
            version = src.getLink(); src++;

        } else if (curToken == "comment") {
        // comments appended to one text buffer

            comments += src.getLink(); src++;
        } else if (curToken == "element") {
        // element <element name> <num entries>
        // can use the num entries to spot check the element list later

            curElement = elementNames.size();
            elementNames.push_back(src.getLink()); src++;
            int size = atoi(src.getLink());
            elements.push_back(vector<GLfloat>(size));




        } else if (curToken == "property") {
        // a propoerty is always of the currently defined element

        }











    }

    return "";


}