在openGL中调整圆形大小以保持纵横比

时间:2018-01-23 21:54:49

标签: c++ opengl reshape orthographic

我写了这个打印圆圈的代码。当我尝试调整窗口大小时出现问题。不保持纵横比,圆形成椭圆形。

#include<GL/glut.h>
#include<GL/glu.h>
#include<GL/gl.h>
#include<string.h>
#include<stdio.h> 
#include <math.h>
#define PI 3.1415

const float DEG2RAD = 3.14159 / 180;
// Keep track of windows changing width and height
GLfloat windowWidth;
GLfloat windowHeight;

void drawCircle(float radius)
{
    glBegin(GL_LINE_LOOP);
    for (int i = 0; i <= 300; i++) {
        double angle = 2 * PI * i / 300;
        double x = radius * cos(angle);
        double y = radius * sin(angle);
        glVertex2d(x, y);
    }
    glEnd();
}

///////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT );

    // Save the matrix state and do the rotations
    glMatrixMode(GL_MODELVIEW);
    //glPushMatrix();
    glColor3d(1, 0, 0);


    drawCircle(100);

    glutSwapBuffers();
}





///////////////////////////////////////////////////////////
// This function does any needed initialization on the 
// rendering context. 
void SetupRC()
{
    // Light values and coordinates
    //glEnable(GL_DEPTH_TEST);  // Hidden surface removal

    glClearColor(0,0,0,0);
}


void ChangeSize(int w, int h)
{
    GLfloat aspectRatio;
    GLfloat nRange = 200.0f;
    // Prevent a divide by zero
    if (h == 0)
        h = 1;

    // Set Viewport to window dimensions
    glViewport(0, 0, w, h);

    // Reset coordinate system
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Establish clipping volume (left, right, bottom, top, near, far)
    aspectRatio = (GLfloat)w / (GLfloat)h;
    if (w <= h)
    {
        glOrtho(-nRange, nRange, -nRange*aspectRatio, nRange*aspectRatio, -nRange*2, nRange * 2);
    }
    else
    {
        glOrtho(-nRange /aspectRatio, nRange /aspectRatio, -nRange, nRange, -nRange * 2, nRange * 2);
    }

    // Specify the orthographic (or perpendicular) projection, 
    // i.e., define the viewing box.

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}





///////////////////////////////////////////////////////////
// Entry point of the program
int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glClear(GL_COLOR_BUFFER_BIT);
    glutInitWindowSize(800, 800);
    glutCreateWindow("Circle");
    glutReshapeFunc(ChangeSize);

    glutDisplayFunc(RenderScene);
    SetupRC();
    glutMainLoop();

    return 0;
}

代码。我认为问题出在ChangeSize()函数中。有人能帮我吗?我尝试通过问题保持的宽度/高度定义的宽高比来划分和多播范围。

1 个答案:

答案 0 :(得分:1)

投影矩阵描述了从场景的3D点到视口的2D点的映射。投影矩阵从视图空间变换到剪辑空间。 通过除以剪辑的w分量,剪辑空间中的坐标转换为范围(-1,-1,-1)到(1,1,1)范围内的规范化设备坐标(NDC)坐标。

在Orthographic Projection中,眼睛空间中的坐标线性映射到标准化设备坐标,剪辑sapce坐标等于标准化设备坐标,因为w分量为1(对于一个carthesian坐标)。

enter image description here

正交投影矩阵:

r = right, l = left, b = bottom, t = top, n = near, f = far 

2/(r-l)         0               0               0
0               2/(t-b)         0               0
0               0               -2/(f-n)        0
-(r+l)/(r-l)    -(t+b)/(t-b)    -(f+n)/(f-n)    1


让我们假设你有一个完整的高清窗口:

w = 1920.0;
h = 1080.0;

窗口的aspcet比率为1.77778

aspectRatio = w / h = 1.77778

如果您设置这样的正交投影矩阵:

glOrtho(-nRange*aspectRatio, nRange*aspectRatio, -nRange, nRange, -nRange*2, nRange*2 );   

这将产生以下正交投影矩阵(1.0 / 1.77778 == 0.5625):

0.5625/nRange   0            0.0          0.0
0.0             1.0/nRange   0.0          0.0
0.0             0.0          0.5/nRange   0.0
0.0             0.0          0.0          1.0   

绘制几何体时,几何体的每个点都由投影矩阵变换。如果在视口的XY平面中绘制圆, 然后 X坐标 0.5625 / nRange 缩放:

X' = X * prjMat[0][0] = X * 0.5625/nRange

Y坐标 1.0 / nRange

缩放
Y' = Y * prjMat[1][1] = Y * 1.0/nRange

这意味着,当几何体从视图空间转换为规范化设备空间时,正交投影矩阵将视口的倒数纵横比应用于几何体。 这导致在标准化的设备空间中,正圆被扭曲为椭圆,如下所示:

enter image description here

如果您将此椭圆拉回到矩形视口,您可以在窗口或屏幕上看到完美的圆圈:

enter image description here

相关问题