I'm using OpenGL in C++ Visual Studio 2008 Forms application and I want a GLcontrol to switch between 3D and 2D when a bool is set to true/false.

The drawing in 3D works fine, the drawing in 2D works fine, the problem comes when switching from one to another. So if I start the application drawing in 2D it works perfectly and the same with 3D, but if I change the boolean while running it won't draw anything.

Here is the code where I change from one to another.

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
}

I don't know what am doing wrong, but I guess that I don't clear some matrix or something, because like I said before when the variable is draw3D==true at the beginning it draws perfectly and when the variable is draw3D==false at the beginning it draws perfectly in 2D, but the change during runtime makes it not work.

有帮助吗?

解决方案

You need to set the projection matrix in 3D mode for one thing. I'm guessing by default your framework is setting up a perspective projection for you. This is getting overwritten when you do GL::Ortho() in the 2d part.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top