我正在制作一个反弹其窗口边缘的球的程序,但我遇到了边界倾斜的问题。
如果我将初始分辨率设置为方形窗口,
int windowWidth = 600;
int windowHeight = 600;
它工作正常。一旦我重塑窗口,窗口上的边界就会倾斜。
当它是正方形时,它看起来像这样:
当我按其宽度拉伸它时,它看起来像这样:
当我按它的高度拉伸它时,它看起来像这样:
基本上我无法在不倾斜窗口边界的情况下调整窗口大小。
这是我的reshape
功能的代码:
void reshape(GLsizei weight, GLsizei height)
{
if (height == 0) height = 1; // To prevent divide by 0
GLfloat aspect = (GLfloat)weight / height; // Get aspect ratio
// Set the viewport to cover the entire window
glViewport(0, 0, weight, height);
// Adjust the aspect ratio of clipping area to match the viewport
glMatrixMode(GL_PROJECTION); // Select the Projection matrix
glLoadIdentity(); // Reset
for (int i = 0; i < numOfBalls; i++)
{
if (weight <= height)
{
balls[i].xLeft = -1.0;
balls[i].xRight = 1.0;
balls[i].yBottom = -1.0 / aspect;
balls[i].yTop = 1.0 / aspect;
}
else
{
balls[i].xLeft = -1.0 * aspect;
balls[i].xRight = 1.0 * aspect;
balls[i]. yBottom = -1.0;
balls[i]. yTop = 1.0;
}
gluOrtho2D(balls[i].xLeft, balls[i].xRight, balls[i].yBottom, balls[i].yTop);
balls[i].xPosMin = balls[i].xLeft + balls[i].ballRadius;
balls[i].xPosMax = balls[i].xRight - balls[i].ballRadius;
balls[i].yPosMin = balls[i].yBottom + balls[i].ballRadius;
balls[i].yPosMax = balls[i].yTop - balls[i].ballRadius;
}
glMatrixMode(GL_MODELVIEW); // Select the model-view matrix
glLoadIdentity(); // Reset
}
*注意:如果需要,我可以发布更多代码...