C ++ OpenGL从2D切换到3D

时间:2012-06-27 14:07:13

标签: c++ opengl perspective orthographic projection-matrix

我在C ++ Visual Studio 2008 Forms应用程序中使用OpenGL,当bool设置为true / false时,我希望GLcontrol在3D和2D之间切换。

3D绘图工作正常,2D绘图工作正常,从一个切换到另一个时出现问题。因此,如果我在2D中启动应用程序绘图,它可以完美地运行并且与3D相同,但是如果我在运行时更改布尔值则不会绘制任何内容。

以下是我从一个更改为另一个的代码。

if(opciones->draw3D){
    GL::MatrixMode(MatrixMode::Modelview);
    GL::LoadIdentity();
    GL::Viewport(0, 0, w, h);
    Matrix4 lookat = Matrix4::LookAt(100, 100, 100, 0, 0, 0, 0, 0, 1);
    GL::LoadMatrix(lookat);
    GL::Scale(this->zoom, this->zoom, this->zoom);

    GL::Rotate(xrot, 1.0f, 0.0f, 0.0f);
    GL::Rotate(yrot, 0.0f, 1.0f, 0.0f);
    GL::Clear(ClearBufferMask::ColorBufferBit | ClearBufferMask::DepthBufferBit);
    GL::ClearColor(Color::LightGray);
// Draw3D
}
else {
    GL::MatrixMode(MatrixMode::Projection);
    GL::LoadIdentity();
    GL::Ortho(5, w-5, 5, h-5, -1, 1); 
    GL::Viewport(5, 5, w-5, h-5); 

    GL::Clear(ClearBufferMask::ColorBufferBit|ClearBufferMask::DepthBufferBit);
    GL::ClearColor(Color::LightGray);
    GL::MatrixMode(MatrixMode::Modelview);
    GL::LoadIdentity();

// Draw 2D
}

我不知道出了什么问题,但我想我不清楚某些矩阵或其他东西,因为就像我之前所说的那样,变量在draw3D==true开始时它就完美地绘制了变量是draw3D==false一开始它在2D中完美绘制,但运行时的变化使它无效。

1 个答案:

答案 0 :(得分:6)

您需要在3D模式下设置投影矩阵。我猜测默认情况下你的框架正在为你设置一个透视投影。当你在2d部分执行GL :: Ortho()时,这会被覆盖。