如何从OBJ文件中绘制数据

时间:2015-04-21 18:49:34

标签: c++ macos opengl c++11

我正在使用Xcode。我能够从OBJ文件加载数据。每当我尝试使用这些数据绘制我的模型时。它给了我线程1:EXC_BAD_ACCESS(代码= EXC_I386_GPFLT)。有人可以告诉我我的代码有什么问题????

#include <iostream>
#include <fstream>
#include <string>
#include <GLUT/glut.h>

using namespace std;

// Model Structure
struct Model {
int vertices;
int positions;
int texels;
int normals;
int faces;
};

static float SpinX = 0.0f, SpinZ = 0.0f;
static int MousePosX = 0, MousePosY = 0;

string fullpath = "/Users/Desktop/cube.obj";

int p = 0;
int t = 0;
int n = 0;
int f = 0;


Model getOBJinfo (string fp){
Model model = {0};

ifstream inOBJ;
inOBJ.open(fp);
if(!inOBJ.good()){
    cerr << "ERROR OPENING OBJ FILE" << endl;
    exit(1);
}

while(!inOBJ.eof()){
    string line;
    getline(inOBJ, line);
    string type = line.substr(0, 2);

    if(type.compare("v ") == 0)
        model.positions++;
    else if (type.compare("vt") == 0)
        model.texels++;
    else if (type.compare("vn") == 0)
        model.normals++;
    else if (type.compare("f ") == 0)
        model.faces++;
}

model.vertices = model.faces*3;

inOBJ.close();

return model;
}

Model model = getOBJinfo(fullpath);

void extractOBJdata(string fp, float positions[][3], float texels[][2], float normals[][3], int faces[][9]){

ifstream inOBJ;
inOBJ.open(fp);
if(!inOBJ.good()){
    cerr << "ERROR OPENING OBJ FILE" << endl;
    exit(1);
}

while(!inOBJ.eof()){
    string line;
    getline(inOBJ, line);
    string type = line.substr(0, 2);

    if(type.compare("v ") == 0){
        char* l = new char[line.size()+1];
        memcpy(l, line.c_str(), line.size()+1);

        strtok(l, " ");
        for (int i = 0; i < 3 ; i++)
            positions[p][i] = atof(strtok(NULL, " "));

        delete [] l;
        p++;
    }

    else if (type.compare("vt") == 0){
        char* l = new char[line.size()+1];
        memcpy(l, line.c_str(), line.size()+1);

        strtok(l, " ");
        for (int i = 0; i < 2 ; i++)
            texels[t][i] = atof(strtok(NULL, " "));

        delete [] l;
        t++;
    }

    else if (type.compare("vn") == 0){
        char* l = new char[line.size()+1];
        memcpy(l, line.c_str(), line.size()+1);

        strtok(l, " ");
        for (int i = 0; i < 3 ; i++)
            normals[n][i] = atof(strtok(NULL, " "));

        delete [] l;
        n++;
    }

    /*else if (type.compare("f ") == 0){
        char* l = new char[line.size()+1];
        memcpy(l, line.c_str(), line.size()+1);

        strtok(l, " ");
        for (int i = 0; i < 9 ; i++)
            faces[f][i] = atof(strtok(NULL, " /"));

        delete [] l;
        f++;
    }*/
}

inOBJ.close();
}

void init(void){

glEnable(GL_DEPTH_TEST);

}

void mydraw(void){

float positions[model.positions][3];            // XYZ
float texels[model.texels][2];                  // UV
float normals[model.normals][3];                // XYZ
int faces[model.faces][9];                      // PTN PTN PTN

extractOBJdata(fullpath, positions, texels, normals, faces);

glBegin(GL_TRIANGLES);

glVertex3f(positions[0][0], positions[0][1], positions[0][2]);
glVertex3f(positions[1][0], positions[1][1], positions[1][2]);
glVertex3f(positions[2][0], positions[2][1], positions[2][2]);
glVertex3f(positions[3][0], positions[3][1], positions[3][2]);
glVertex3f(positions[4][0], positions[4][1], positions[4][2]);
glVertex3f(positions[5][0], positions[5][1], positions[5][2]);


/*for (int i = 0; i < t; i++){

}

for (int i = 0; i < n; i++){
    glNormal3f(normals[i][0], normals[i][1], normals[i][2]);
}

for (int i = 0; i < f; i++){

}*/

glEnd();

glFlush();
}

void display(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(0.0f, 0.0f, 10.f,         // Eye Position
          0.0f, 0.0f, 0.0f,         // Center of interest
          0.0f, 1.0f, 0.0f);        //Camera Up Vector

glRotatef(SpinZ, 1, 0, 0);
glRotatef(SpinX, 0, 0, 1);

mydraw();

glutSwapBuffers();
}

void reshape(int width, int height){
double aspect;

glViewport(0, 0, width, height);

aspect = (double)width / (double) height;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

if(aspect < 1.0) glOrtho(-4., 4., -4./aspect, 4./aspect, 1., 10);
else glOrtho(-4.*aspect, 4.*aspect, -4., 4., 1., 10);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(0., 0., 5., 0., 0., 0., 0., 1., 0.);
}

void mousepress(int button, int state, int x, int y){
switch (state){
    case GLUT_DOWN: {MousePosX = x; MousePosY = y; break;}
}
}

void mousemotion(int x, int y){
SpinX += (float)(x-MousePosX)*0.1f;
SpinZ += (float)(y-MousePosY)*0.1f;
MousePosX = x;
MousePosY = y;
}

void keypress(unsigned char key, int x, int y){

}

int main(int argc, char **argv) {
cout << "Model Info" << endl;
cout << "----------" << endl;
cout << "Positions: " << model.positions << endl;
cout << "Texels: " << model.texels << endl;
cout << "Normals: " << model.normals << endl;
cout << "Faces: " << model.faces << endl;
cout << "Vertices: " << model.vertices << endl;

/*float positions[model.positions][3];            // XYZ
float texels[model.texels][2];                  // UV
float normals[model.normals][3];                // XYZ
int faces[model.faces][9];                      // PTN PTN PTN

extractOBJdata(fullpath, positions, texels, normals, faces);
cout << endl << endl << endl;
cout << "Model Data" << endl;
cout << "----------" << endl;
cout << "P1: " << positions[0][0] << "x " << positions[0][1] << "y " << positions[0][2] << "z" << endl;
cout << "T1: " << texels[0][0] << "u " << texels[0][1] << "v" << endl;
cout << "N1: " << normals[0][0] << "x " << normals[0][1] << "y " << normals[0][2] << "z" << endl;
cout << "F1v1: " << faces[0][0] << "p " << faces[0][1] << "t " << faces[0][2] << "n" << endl;*/

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("OBJ File");

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutKeyboardFunc(keypress);

glutMouseFunc(mousepress);

init();

glutMainLoop();
return 0;
}

0 个答案:

没有答案