gpt4 book ai didi

c++ - 调整屏幕大小时消失的对象(OpenGl)

转载 作者:行者123 更新时间:2023-11-28 03:36:20 25 4
gpt4 key购买 nike

我正在尝试从 .obj 格式加载 3D 模型,它在屏幕上绘制对象没有任何问题,但是当我调整屏幕大小时,一切都消失了。这是代码:

Obj* object = new Obj();
GLuint texture[1];

void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(double)w / (double)h,1.0,200.0);
}
void initRendering() {
object->GetObj("cube.obj");
glShadeModel(GL_LINEAR);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glEnable(GL_DEPTH_TEST);
}
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 27:
{
exit(0);
break;
}
}
}

void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glRotatef(45.0,0.0,1.0,0.0);
object->DrawObj();
glPopMatrix();
glutSwapBuffers();
glFlush();

}
int _tmain(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);


glutCreateWindow("3D");
initRendering();

glutReshapeFunc(handleResize);
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutMainLoop();
return 0;
}

这是 Obj.DrawObj() 的代码:

glBegin(GL_TRIANGLES);
for(int i = 0;i < faces.capacity()-1;i++)
{
glVertex3f(vertices[faces[i].vertex1].cordinate1,vertices[faces[i].vertex1].cordinate2,vertices[faces[i].vertex1].cordinate3);
glVertex3f(vertices[faces[i].vertex2].cordinate1,vertices[faces[i].vertex2].cordinate2,vertices[faces[i].vertex2].cordinate3);
glVertex3f(vertices[faces[i].vertex3].cordinate1,vertices[faces[i].vertex3].cordinate2,vertices[faces[i].vertex3].cordinate3);
}
glEnd;

最佳答案

在您的绘图代码中,您设置了投影矩阵,这很好。但是,您将其设置为身份。在调整大小处理程序中,您也设置了投影矩阵,但您不应该在那里设置;是的,我知道教程里都有,但这是非常糟糕的风格。您应该将当前在 reshape 处理程序中的所有代码移动到绘图处理程序中,替换投影矩阵的当前设置。

关于c++ - 调整屏幕大小时消失的对象(OpenGl),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10763896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com