从 2D 切换到 3D 时的 OpenGL
OpenGL when switch fron 2D to 3D
这是我想要实现的,我在下面的代码中有一个名为 switch_2D_3D 的标志,当它为真时,我切换到 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 模式时,问题就来了
每次我调整window时,我在3d场景中绘制的东西(例如立方体)都会越来越小,最终消失,为什么会这样
如果我切换回 2D 模式,2d 模式下的一切仍然正常,问题出在 3d 模式上
此外,如果我在将标志设置为 false 的情况下启动程序,我会看到一个立方体,并且随着我每次调整 window 的大小,它仍然会变小
为什么会这样?
您应该查看您的 glLoadIdentity()
/ glMatrixMode()
互动。
现在,您有两种不同的行为:
在 2D 中:当您输入函数时,您正在为任何活动的矩阵重置矩阵,大概是 GL_MODELVIEW
,这会导致 gluOrtho2D
调用 "stack up".
在 3D 中:您总是重置投影矩阵,这似乎更正确。
尝试仅在第一个路径 (2D) 中交换 glLoadIdentity
和 glMatrixMode
调用的顺序。
明智的做法是始终在实际修改之前显式设置要修改的矩阵。
这是我想要实现的,我在下面的代码中有一个名为 switch_2D_3D 的标志,当它为真时,我切换到 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 模式时,问题就来了
每次我调整window时,我在3d场景中绘制的东西(例如立方体)都会越来越小,最终消失,为什么会这样
如果我切换回 2D 模式,2d 模式下的一切仍然正常,问题出在 3d 模式上
此外,如果我在将标志设置为 false 的情况下启动程序,我会看到一个立方体,并且随着我每次调整 window 的大小,它仍然会变小
为什么会这样?
您应该查看您的 glLoadIdentity()
/ glMatrixMode()
互动。
现在,您有两种不同的行为:
在 2D 中:当您输入函数时,您正在为任何活动的矩阵重置矩阵,大概是 GL_MODELVIEW
,这会导致 gluOrtho2D
调用 "stack up".
在 3D 中:您总是重置投影矩阵,这似乎更正确。
尝试仅在第一个路径 (2D) 中交换 glLoadIdentity
和 glMatrixMode
调用的顺序。
明智的做法是始终在实际修改之前显式设置要修改的矩阵。