【发布时间】:2014-08-26 14:03:55
【问题描述】:
我的 wavefront .obj 加载程序有问题,它没有显示我希望它显示的内容。我希望它显示一个立方体,但我不断得到这个:
这是我的 obj 文件
# cube.obj
#
g cube
v 0.0 0.0 0.0
v 0.0 0.0 1.0
v 0.0 1.0 0.0
v 0.0 1.0 1.0
v 1.0 0.0 0.0
v 1.0 0.0 1.0
v 1.0 1.0 0.0
v 1.0 1.0 1.0
vn 0.0 0.0 1.0
vn 0.0 0.0 -1.0
vn 0.0 1.0 0.0
vn 0.0 -1.0 0.0
vn 1.0 0.0 0.0
vn -1.0 0.0 0.0
f 1//2 7//2 5//2
f 1//2 3//2 7//2
f 1//6 4//6 3//6
f 1//6 2//6 4//6
f 3//3 8//3 7//3
f 3//3 4//3 8//3
f 5//5 7//5 8//5
f 5//5 8//5 6//5
f 1//4 5//4 6//4
f 1//4 6//4 2//4
f 2//1 6//1 8//1
f 2//1 8//1 4//1
我在我的代码中添加了部分以查看是否所有内容都正确存储,顶点和法线是否存储为 GLfloats
显示:
输入:
这是我的绘图功能: 我不太确定 glDrawArrays 中的最后一个参数,它是硬编码的,因为我不太确定索引的数量。
void Draw(void)
{
glPushMatrix();
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef( rotate_x, 1.0, 0.0, 0.0 );
glRotatef( rotate_y, 0.0, 1.0, 0.0 );
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, normals);
glVertexPointer(3, GL_FLOAT,0, vertices);
glDrawArrays(GL_TRIANGLES, 0, (14));
glTranslatef(0, 0, -3);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glFlush();
glutSwapBuffers();
glPopMatrix();
}
这也是我的主要内容:
int main(int argc,char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Object Loader!");
glEnable(GL_DEPTH_TEST);
//glEnable(GL_LIGHTING);
//glEnable(GL_LIGHT0);
GLfloat posLight0[] = {0.0f, 0.0f, -1.0f, 0.0f};
GLfloat lightColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, posLight0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
setUpArrays();
glutDisplayFunc(Draw);
glutSpecialFunc(specialKeys);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
最后一件事是我不允许使用 glDrawElements 或 glBegin() 直接模式。
如果有帮助,这里是我的程序的更多内容: 一开始就设置全局变量
GLfloat* vertices;
GLfloat* normals;
GLint* faces;
int amountOfFaces;
double rotate_y=0;
double rotate_x=0;
Loader* loader = new Loader();
设置顶点、法线和索引的数组:
void setUpArrays()
{
loader->loadObject("cube.obj");
vector<GLfloat> temp = loader->getVerticies();
vertices = new GLfloat[temp.size()];
int tempSize = 0;
cout << "vectors: ";
for(vector<GLfloat>::const_iterator i = temp.begin(); i != temp.end(); ++i)
{
vertices[tempSize] = *i;
cout << vertices[tempSize] << ", ";
tempSize++;
}
cout << endl;
vector<GLfloat> tempNormal = loader->getNormals();
normals = new GLfloat[tempNormal.size()];
tempSize = 0;
cout << "normals: ";
for(vector<GLfloat>::const_iterator i = tempNormal.begin(); i != tempNormal.end(); ++i)
{
normals[tempSize] = *i;
cout << normals[tempSize] << ", ";
tempSize++;
}
amountOfFaces = tempSize;
cout << endl;
vector<GLint> tempIndices = loader->getFaces();
faces = new GLint[tempIndices.size()];
tempSize = 0;
cout << "Indices: ";
for(vector<GLint>::const_iterator i = tempIndices.begin(); i != tempIndices.end(); ++i)
{
faces[tempSize] = *i;
cout << faces[tempSize] << ", ";
tempSize++;
}
cout << endl;
amountOfFaces += tempSize;
}
【问题讨论】:
-
我认为您没有正确附上问题的输出。