调整窗口偏斜显示的大小(OpenGL)

时间:2013-10-17 17:02:31

标签: c++ opengl

我正在制作一个程序来反弹其窗口边缘的球,但我遇到的问题是边界变得偏斜。

如果我将初始分辨率设置为方形窗口,

int windowWidth = 600;

int windowHeight = 600;

它工作正常。一旦我重塑窗口,窗口上的边界就会倾斜。

当它是正方形时,它看起来像这样: enter image description here

当我按宽度拉伸它时,它看起来像这样: enter image description here

当我按高度伸展它时,它看起来像这样: enter image description here

基本上我无法在不偏离窗口范围的情况下调整窗口大小。

这是我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 
} 

*注意:如果需要,我可以发布更多代码......

1 个答案:

答案 0 :(得分:2)

尝试从你的循环中删除它:

gluOrtho2D(balls[i].xLeft, balls[i].xRight, balls[i].yBottom, balls[i].yTop);

并定义一次拼写矩阵。

我认为无论何时执行循环,都要将新矩阵与GL中先前插入的矩阵相乘。

GL生产的正交矩阵是:

enter image description here

现在,当您设置宽度:800和高度:600时,您的宽高比将为1.33,第一个循环的矩阵将为:

enter image description here

现在通过每个循环,GL将新矩阵与前一个矩阵相乘,坐标将逐渐增加0.75。

(我也不确定)