用 GL_POLYGON 绘制圆,半径超出比例

Drawing circle with GL_POLYGON, radius out of scale

我正在尝试使用 GL_POLYGON 绘制一个圆,我尝试的代码正在运行,但圆的半径似乎与 window 大小不符,我不明白为什么。

代码如下:

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    if(start == OFF)
    {
        //Set Drawing Color - Will Remain this color until otherwise specified
        glColor3f(0.2, 0.3, 0.5);  //Some type of blue   

        //Draw Circle
        glBegin(GL_POLYGON);
        for(double i = 0; i < 2 * PI; i += PI / 24) 
            glVertex3f((cos(i) * radius) + gX,(sin(i) * radius) + gY, 0.0);
        glEnd();

        //Draw Circle
        center_x = gX;
        center_y = gY;
    }     

    glutSwapBuffers();
}

和初始化函数:

void init (void) 
{
  /* selecionar cor de fundo (preto) */
  glClearColor (0.0, 0.0, 0.0, 0.0);

  /* inicializar sistema de viz. */
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

我将 window 大小设置为 600,半径设置为 1,四分之一圆就是整个 window。我猜我必须对半径做某种变换,我只是不知道怎么做。

投影矩阵描述了从场景的 3D 点到视口的 2D 点的映射。它从眼睛 space 变换到剪辑 space,并且剪辑 space 中的坐标通过除以剪辑坐标。 NDC 的范围是 (-1,-1,-1) 到 (1,1,1)。

在正交投影中,眼睛中的坐标 space 线性映射到归一化设备坐标。

可以通过glOrtho设置正投影。您正在设置一个视口,其左下角为 (0, 0),右上角为 (1, 1),深度范围为 -1 到 1:

glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

如果你想设置一个允许你在 window 尺寸范围内绘制的投影,那么你必须这样做:

int wndWidth  = 800; // for example a window with the size 800*600  
int wndHeight = 600; 

glOrtho( 0.0, (float)wndWidth, 0.0, (float)wndHeight, -1.0, 1.0 );

如果你想让 (0, 0) 的原点在 window 的中心,那么你必须这样做:

float w = (float)wndWidth/2.0f;
float h = (float)wndHeight/2.0f;

glOrtho( -w, w, -h, h, -1.0, 1.0 );

进一步了解:

如果 Rabbid76 的回答(日期时间戳:2017-09-17T07:10)尚未解决您的问题,请同时使用以下计算机指令进行验证:

将 window 宽度和高度设置为您使用的值,例如600x600

示例:

int iWindowWidth=600;
int iWindowHeight=600;

glScalef(iWindowHeight/(iWindowWidth*1.0f), 1.0f, 1.0f);    

  //Draw Circle
  glBegin(GL_POLYGON);
    for(double i = 0; i < 2 * PI; i += PI / 24)  {
      glVertex3f((cos(i) * radius) + gX,(sin(i) * radius) + gY, 0.0);
    }
  glEnd();
        
//reset scaled shape
glScalef(1.0f, 1.0f, 1.0f);

温馨提示:我们需要将整数,即整数,转换为浮点数,即带小数的数字。

我们加上:(iWindowWidth*1.0f)

这是由于 iWindowHeight/iWindowWidth,例如1366/768 = 0.5622...,即不是整数

谢谢。