Hier ist, was ich erreichen möchte, ich habe eine Flagge namens switch_2D_3D in dem Code unten, und wenn es wahr ist, wechsle ich in 2D-Modus, sonst 3D.OpenGL beim Umschalten von 2D nach 3D
void reshape(GLsizei width, GLsizei height)
{
if (switch_2D_3D)
{
// GLsizei for non-negative integer
// Compute aspect ratio of the new window
if (height == 0)
height = 1; // To prevent divide by 0
GLfloat aspect = (GLfloat)width/(GLfloat)height;
// Reset transformations
glLoadIdentity();
// Set the aspect ratio of the clipping area to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
if (width >= height)
{
// aspect >= 1, set the height from -1 to 1, with larger width
gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
}
else
{
// aspect < 1, set the width to -1 to 1, with larger height
gluOrtho2D(-1.0, 1.0, -1.0/aspect, 1.0/aspect);
}
winWidth = width;
winHeight = height;
} // 2D mode
else
{
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if (height == 0)
height = 1;
float ratio = width * 1.0/height;
// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);
// Reset Matrix
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, width, height);
// Set the correct perspective.
gluPerspective(45.0f, ratio, 0.1f, 100.0f);
// Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
winWidth = width;
winHeight = height;
}// 3D mode
}
Alles funktioniert perfekt, wenn nur im 2D-Modus zeichnen, aber wenn ich die Flagge wechseln Sie in den 3D-Modus zu wechseln, hier kommt das Problem
Jedes Mal, wenn ich die Größe des Fensters, die Dinge, die ich in zeichnen die 3D-Szene (zum Beispiel ein Würfel) würde kommen smallerand kleiner, schließlich verschwand, warum ist das passiert
Und wenn ich zurück in 2D-Modus, alles im 2D-Modus funktioniert immer noch gut, das Problem ist mit dem 3D-Modus
Auch, wenn ich sta rt das Programm mit dem Flag auf false gesetzt, würde ich einen Würfel sehen und es wird immer kleiner, wie ich das Fenster jedes Mal
Warum ändert sich das?
Ich denke, Sie sollten aufhören zu denken in Bezug auf "2D vs. 3D". Diese Unterscheidung ergibt keinen Sinn, Wahrheit zu erzählen. Was Sie dort schalten, ist die Projektion und natürlich können Sie eine orthographische Projektion auch für 3D-Szenen verwenden. – datenwolf