English 中文(简体)
调整屏幕大小时消失对象( OpenGl)
原标题:Disappearing object when resizeing screen (OpenGl)
  • 时间:2012-05-26 05:39:22
  •  标签:
  • c++
  • opengl

我试图从.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;
问题回答

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

我看得出你读你的粘贴纸仍然困惑不解,让我解释一下:

第一次绘制时可以看到对象的原因是因为您没有设置投影矩阵。所以您的对象直接被划入正常设备坐标( 1至 1 区域) 。

当您调整大小时,您将第一次重新设置投影矩阵,这改变了屏幕上所绘制的查看区域。您最初绘制的对象不在您的投影矩阵所定义的查看区域之内(它位于摄像头的顶部,我猜在近平面前面)。您必须将对象从相机上移开,使其在视图的鲁斯图内。这就是日期狼人所暗示的。

然而,与此同时,您在代码中引入了其他错误, 特别是您停止在控点Resize 中重新设置投影矩阵。 您必须总是在使用 gluPerspective 之前清除投影矩阵, 否则您将获得假结果 。

如果你从你的糊盘中取出准确的代码, 并添加一个 Gl Loadidentity 来处理Resize, 我认为这应该有用:

void handleResize(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);   //<--- add
    glLoadIdentity();              //<--- add
    gluPerspective(45.0,(double)w / (double)h,1.0,200.0);
} 

另外,您在绘图Scene 函数 中正在重新“ 坚固” 清除投影矩阵。当您清除矩阵时,您会再次丢弃您刚刚设置的视野设置 Resize 时, 您不想这样做 。

所以基本上说:

  1. Set the Projection matrix in handleResize and on initialization
  2. Don t touch the projection matrix in drawScene
  3. Translate the object so that it fits into the viewing frustum.




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签