我打算加載模型並使用OpenGL進行顯示。代碼運行良好,沒有錯誤,但沒有顯示任何內容。有人可以幫我解決這個問題嗎?加載模型並使用OpenGL顯示它?
的源代碼:
所有我猜你是混合的東西了的int cube;
int number_faces;
int number_vertices;
struct coordinate {
float x, y, z;
coordinate(float a, float b, float c): x(a), y(b), z(c) {};
};
struct face {
int faces[3];
face (int f1, int f2, int f3){
faces[0] = f1;
faces[1] = f2;
faces[2] = f3;
};
};
// function prototype
bool loadObject(const char * file_name);
int main(int argc, char ** argv)
{
//initialize Glut
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(w_x_position, w_y_position);
glutInitWindowSize(w_width, w_height);
glutCreateWindow("Assignment 1");
//function call
init();
glutDisplayFunc(display);
//loop main
glutMainLoop();
return 0;
}
bool loadObject(const char * file_name){
//initialize stuff
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//create the vectors that will hold the file coordinates
std::vector<std::string*> points;
std::vector<coordinate*> vertex;
std::vector<face*> faces;
//check if the file opens
if (!file_name) {
return false;
}
// open file
FILE *fp = fopen(file_name, "r");
if (!fp) {
return false;
}
// num of vertices and indices
fscanf(fp, "data%d%d", &number_vertices, &number_faces);
//push the content into the vertexs
for(int i=0;i<number_vertices;i++) {
float x_temp, y_temp, z_temp;
fscanf(fp, "%f%f%f", &x_temp, &y_temp,&z_temp);
vertex.push_back(new coordinate(x_temp, y_temp, z_temp));
}
for (int y=0; y<number_faces;y++){
int a,b,c;
fscanf(fp, "%d%d%d", &a, &b, &c);
faces.push_back(new face(a, b, c));
}
//draw the modle
int number;
number = glGenLists(1);
glNewList(number, GL_COMPILE);
for(int i =0; i <faces.size();i++){
glBegin(GL_TRIANGLES);
glVertex3f(vertex[faces[i]->faces[0]-1]->x, vertex[faces[i]->faces[0]-1]->y,vertex[faces[i]->faces[0]-1]->z);
glVertex3f(vertex[faces[i]->faces[1]-1]->x, vertex[faces[i]->faces[1]-1]->y,vertex[faces[i]->faces[1]-1]->z);
glVertex3f(vertex[faces[i]->faces[2]-1]->x, vertex[faces[i]->faces[2]-1]->y,vertex[faces[i]->faces[2]-1]->z);
glEnd();
}
glEndList();
//delete the poiters for memory space
for(int i=0;i<points.size();i++){
delete points[i];
}
for(int i=0;i<faces.size();i++){
delete faces[i];
}
for(int i=0;i<vertex.size();i++){
delete vertex[i];
}
return number;
}
請[提高您的問題](http://stackoverflow.com/posts/25699679/edit)。減少到給你一個_issue_問題的代碼部分! – 2014-09-06 11:20:14
我的問題是爲什麼不顯示立方體。代碼中沒有錯誤? – toogz 2014-09-06 11:28:00
調用'loadObject'在哪裏? 'glutDisplayFunc(display)'所需的函數'display'在哪裏?除此之外,顯示列表,即時模式等已經過時了,但我必須承認,現代OpenGL的入門級是很高的。 – Marius 2014-09-06 12:32:05