OpenGL圆绘制得到一个椭圆

OpenGL circle drawing getting an ellipse

本文关键字:一个 绘制 OpenGL      更新时间:2023-10-16

我试图在opengl中画一个圆,但我似乎无法得到正确的坐标,所以无论我使用什么方法,我总是得到一个省略号。当前代码如下:

void Ball::Render() {
    float center_position_x = _body->GetWorldCenter().x;
    float center_position_y = _body->GetWorldCenter().y;
    float radius = static_cast<b2CircleShape*>(_body->GetFixtureList()->GetShape())->m_radius;
    glBegin(GL_LINE_STRIP);
        for(float angle = 0; angle <= 360; ++angle) {
            float angleInRadians = glm::radians(angle);
            float x = glm::cos(angleInRadians);
            float y = glm::sin(angleInRadians);
            glVertex3f(x,y,0);
        }
    glEnd();
}

(我知道代码应该在原点画圆,但即使这样它也不是一个完美的圆,如果我理解正确的话,应该在原点画一个半径=1的圆)

我使用的其他方法有:http://slabode.exofire.net/circle_draw.shtmlhttp://www.opengl.org/discussion_boards/showthread.php/167955-drawing-a-smooth-circle

这是我的OpenGL窗口设置代码(这是一个存根程序,所以我从硬编码值开始):gluOrtho2D(-10, 15, -10, 15);

使用gluOrtho2D(-WIDOWS_WIDTH, WIDOWS_WIDTH, -WINDOWS_HEIGHT, WINDOWS_HEIGHT);

顺便说一下,你们在2013年使用的是固定管道。有了顶点着色器,这就容易理解了。

由gluOrtho2d定义的2D投影矩阵的长宽比必须与你正在渲染的视口/窗口相同,否则你会注意到你正在渲染的数字失真。你可以使用上面的gluOrtho2d语句或者用其他方式写-

float = (float)WindowWidth/windowwheight;

gluOrtho2D(-1*ar, ar, -1, 1)