使用以下在opengl中绘制实心圆的函数,如何使其显示在窗口的不同坐标处?

发布于 2024-10-02 08:52:29 字数 692 浏览 4 评论 0原文

我有以下代码在 opengl 中绘制一个实心圆。问题是它绘制在屏幕的中心。我如何让它绘制在它的另一个位置?

这是代码:

#define CIRCLE_RADIUS = 0.15f

int circle_points = 100;

void draw()
{
    glClear(GL_COLOR_BUFFER_BIT);

    double angle = 2*  PI/circle_points ;
    glPolygonMode( GL_FRONT, GL_FILL );
    glColor3f(0.2, 0.5, 0.5 );
    
    glBegin(GL_POLYGON);
    
    double angle1 = 0.0;        
    glVertex2d( CIRCLE_RADIUS * cos(0.0) , CIRCLE_RADIUS * sin(0.0));

    int i;
    for (i = 0; i < circle_points; i++)
    {
        glVertex2d(CIRCLE_RADIUS * cos(angle1), CIRCLE_RADIUS *sin(angle1));
        angle1 += angle ;
    }

    glEnd();
    glFlush();
}

I have got the following code to draw a filled circle in opengl. The problem is that it draws at the center of the screen. How do I make it draw in another position of it?

Here is the code:

#define CIRCLE_RADIUS = 0.15f

int circle_points = 100;

void draw()
{
    glClear(GL_COLOR_BUFFER_BIT);

    double angle = 2*  PI/circle_points ;
    glPolygonMode( GL_FRONT, GL_FILL );
    glColor3f(0.2, 0.5, 0.5 );
    
    glBegin(GL_POLYGON);
    
    double angle1 = 0.0;        
    glVertex2d( CIRCLE_RADIUS * cos(0.0) , CIRCLE_RADIUS * sin(0.0));

    int i;
    for (i = 0; i < circle_points; i++)
    {
        glVertex2d(CIRCLE_RADIUS * cos(angle1), CIRCLE_RADIUS *sin(angle1));
        angle1 += angle ;
    }

    glEnd();
    glFlush();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿港小镇 2024-10-09 08:52:29

最明显的方法是首先调用 glTranslate。但请注意,通过结合使用 glPointSizeglPoint,您已经可以更轻松地完成相同的任务:

glPointSize(CIRCLE_RADIUS/2.0f);
glPoint(center_x, center_y, center_z);

在开始绘制圆圈之前,您需要类似的内容:

glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);

否则,你的“圆圈”最终可能会变成正方形。

编辑:在不知道如何设置坐标的情况下,不可能知道“左上角”位置是什么,但您可以这样做:

void draw_circle(float x, float y, float radius) { 
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glTranslatef(x, y, 0.0f);
    static const int circle_points = 100;
    static const float angle = 2.0f * 3.1416f / circle_points;

    // this code (mostly) copied from question:
    glBegin(GL_POLYGON);
    double angle1=0.0;
    glVertex2d(radius * cos(0.0) , radius * sin(0.0));
    int i;
    for (i=0; i<circle_points; i++)
    {       
        glVertex2d(radius * cos(angle1), radius *sin(angle1));
        angle1 += angle;
    }
    glEnd();
    glPopMatrix();
}

然后您可以调用(例如):

draw_circle(0.0f, 0.0f, 0.2f); // centered
draw_circle(1.0f, 0.0f, 0.2f); // right of center
draw_circle(1.0f, 1.0f, 0.2f); // right and up from center

当然,我的方向假设没有(例如)旋转您的视图,因此 x 向右增加,y 向上增加。

The obvious way would be to call glTranslate first. Note, however, that you can already accomplish the same a bit more easily with a combination of glPointSize and glPoint:

glPointSize(CIRCLE_RADIUS/2.0f);
glPoint(center_x, center_y, center_z);

Before you start drawing the circles, you'll want something like:

glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);

Otherwise, your "circles" could end up as squares.

Edit: Without knowing how you've set up your coordinates, it's impossible to know what the "top-left" position is, but you could do something like this:

void draw_circle(float x, float y, float radius) { 
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();
    glTranslatef(x, y, 0.0f);
    static const int circle_points = 100;
    static const float angle = 2.0f * 3.1416f / circle_points;

    // this code (mostly) copied from question:
    glBegin(GL_POLYGON);
    double angle1=0.0;
    glVertex2d(radius * cos(0.0) , radius * sin(0.0));
    int i;
    for (i=0; i<circle_points; i++)
    {       
        glVertex2d(radius * cos(angle1), radius *sin(angle1));
        angle1 += angle;
    }
    glEnd();
    glPopMatrix();
}

You could then call (for example):

draw_circle(0.0f, 0.0f, 0.2f); // centered
draw_circle(1.0f, 0.0f, 0.2f); // right of center
draw_circle(1.0f, 1.0f, 0.2f); // right and up from center

Of course, the directions I've given assume haven't (for example) rotated your view, so x increases to the right and y increases upward.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文