用OpenGL绘图

时间:2013-11-30 13:44:18

标签: opengl geometry

如何在OpenGl中绘制半个圆圈? 我试过这个:

float step=5.0;
glBegin(GL_LINE_STRIP);
 for(float angle=0.0f; angle <= 360; angle+=step)
 {
   float rad  = 2*angle/180;
   x  = radius*sin(rad);
   y  = radius*cos(rad);
   glVertex3f(x,y,0.0f);

}

glEnd();

但是我获得了一个不在直线上的半圈。这是倾向的。我怎么能解决这个问题呢?

3 个答案:

答案 0 :(得分:3)

您的问题很难理解,尤其是最后一部分。

如果我理解正确,你要说的是你正在画这样的东西:

http://www.mathopenref.com/images/circles/semiarc.png

但你想要的是:

http://abyss.uoregon.edu/~js/images/half_circle.gif

这样做的原因是您使用了一行 strip ,它连接了每个线段,但没有创建一个循环回到第一个点的线段。改为使用GL_LINE_LOOP,你将有一个“在一条直线上的半圆”。

答案 1 :(得分:1)

您将度数转换为弧度是错误的,您必须将度数值乘以PI / 180才能获得正确的值。为了获得一个“面朝上”的圈子,交换你对sin和cos函数的使用。

答案 2 :(得分:0)

这两个函数绘制了2个半圆,1个是完整的,另一个是空心的(在c ++中)

void drawLeftCircle()   // the filled one
{
    float radius = 70;
    float twoPI = 2 * PI;

    glBegin(GL_TRIANGLE_FAN);

    for (float i = PI; i <= twoPI; i += 0.001)
        glVertex2f((sin(i)*radius), (cos(i)*radius));

    glEnd();
    glFlush();
}


void drawRightHalfCircle()  // the empty one
{
    float radius = 70;
    float twoPI = 2 * PI;

    glBegin(GL_POINTS);

    for (float i = 0.0; i <= twoPI / 2; i += 0.001)
        glVertex2f((sin(i)*radius), (cos(i)*radius));

    glEnd();
    glFlush();
}
相关问题