Voici ce que je veux réaliser, j'ai un drapeau appelé switch_2D_3D dans le code ci-dessous, et quand c'est vrai je passe en mode 2D, sinon 3D.OpenGL lors du passage de 2D à 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
}
Tout fonctionne parfaitement lorsque le dessin uniquement en mode 2D, mais quand je change le drapeau pour passer en mode 3D, vient ici le problème
Chaque fois que je redimensionnez la fenêtre, les choses que je dessine en la scène 3D (par exemple un cube) serait plus petite et plus petite, finit par disparaître, pourquoi cela se produit
Et si je repasse en mode 2D, tout en mode 2d fonctionne toujours bien, le problème est avec le mode 3d
De plus, si je suis rt le programme avec le drapeau mis à false, je verrais un cube et il devient encore plus petit que je redimensionne la fenêtre à chaque fois
Pourquoi cela se produit-il?
Je pense que vous devriez arrêter de penser en termes de "2D vs. 3D". Cette distinction n'a aucun sens, la vérité à dire. Ce que vous changez ici, c'est la projection et bien sûr vous pouvez utiliser une projection orthographique aussi pour les scènes 3D. – datenwolf