다음은 내가 달성하고자하는 것입니다. 아래 코드에서 switch_2D_3D라는 플래그가 있는데, 사실이면 2D 모드로, 그렇지 않으면 3D로 전환합니다.OpenGL을 fron 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
}
모든 2D 모드에서만 그릴 때 완벽하게 작동하지만이 3D 모드로 전환 플래그를 변경할 때, 여기에 문제
I 창, 내가 그려 것들의 크기를 조정할 때마다 온다 3D 장면 (예 : 큐브)이 작아지고 결국 사라졌습니다. 왜 이런 일이 발생합니까?
2D 모드로 다시 전환하면 2 차원 모드의 모든 것이 여전히 정상적으로 작동하며 문제는 3D 모드와 관련이 있습니다.
또한, I sta 깃발이 false로 설정된 프로그램은 큐브를 볼 수 있으며 매번 창 크기를 조정할 때 여전히 작아집니다.
왜 이런 일이 발생합니까?
"2D 대 3D"라는 생각을하지 말아야한다고 생각합니다. 그 구별은 말이되지 않습니다. 여기서 전환하는 것은 투영이며 물론 3D 장면에도 직교 투영을 사용할 수 있습니다. – datenwolf