获得椭圆的OpenGL圆绘图

时间:2013-04-24 17:14:30

标签: c++ math opengl trigonometry geometry

我正在尝试在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.shtml http://www.opengl.org/discussion_boards/showthread.php/167955-drawing-a-smooth-circle

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

2 个答案:

答案 0 :(得分:1)

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

顺便说一下,你在2013年使用固定管道。使用顶点着色器这很容易理解。

答案 1 :(得分:1)

由gluOrtho2d定义的2D投影矩阵的纵横比必须与要渲染的视口/窗口相同,否则您会注意到正在渲染的图形中的扭曲。你可以使用上面的gluOrtho2d语句或其他写作方式 -

float ar =(float)WindowWidth / WindowHeight;

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

相关问题