mercredi 19 octobre 2016

How to pass pointer vector to function c++

Essentially I want to use a std::vector<> to hold all of the objects to draw to the screen (opengl). I want to read all of the new models from the .obj file and input these new objects into my std::vector<>. Right now the way I have it implemented is through passing the std::vector<> into my function for inserting each new model into the std::vector<>. I am pretty flexible as to the implementation except for the declaration of the std::vector<> holding all of the objects.

file/model loader:

void glc::ShapeLoader::load(const char* path, std::vector<CustomShape*>* shape) {
    file = fopen(path, "r");
    fileName = path;
    char line[1024];
    while (fscanf(file, "%s", line) != EOF) {
        std::cout << line << std::endl;
        CustomShape* nShape = new CustomShape;
        std::vector<GLfloat> newV;
        std::vector<GLfloat> newVN;
        std::vector<GLint> newF;
        GLfloat xf, yf, zf;
        if (strcmp(line, "o") == 0) {
            std::string name;
            fscanf(file, "%s\n", &name);
            nShape->typeName = name;
        }
        else if (strcmp(line, "v") == 0) {
            fscanf(file, "%f %f %f\n", &xf, &yf, &zf);
            std::vector<GLfloat> iv;
            iv.push_back(xf);
            iv.push_back(yf);
            iv.push_back(zf);
            nShape->vn.push_back(iv);
        }
        else if (strcmp(line, "vn") == 0) {
            fscanf(file, "%f %f %f\n", &xf, &yf, &zf);
            std::vector<GLfloat> iv;
            iv.push_back(xf);
            iv.push_back(yf);
            iv.push_back(zf);
            nShape->vn.push_back(iv);
        }
        else if (strcmp(line, "f") == 0) {
            GLint x, y, z;
            fscanf(file, "%i %i %i\n", &x, &y, &z);
            std::vector<GLint> iv;
            iv.push_back(x);
            iv.push_back(y);
            iv.push_back(z);
            nShape->f.push_back(iv);
        }
        shape->push_back(nShape);
    }
}

file/model loader call:

s1.load("../Trees.obj", &worldObjects);

std::vector<> holding all of the models:

std::vector<CustomShape*> worldObjects;

Aucun commentaire:

Enregistrer un commentaire