OpenGL画圆,奇怪的错误

发布于 2024-10-04 23:48:10 字数 1776 浏览 5 评论 0原文

我不是数学家,但我需要画一个实心圆。

我的方法是利用别人的数学方法得到圆圆周上的所有点,并将它们变成三角形扇形。

我需要顶点数组中的顶点,没有立即模式。

圆圈确实出现了。然而,当我尝试叠加圆圈时,奇怪的事情发生了。它们只出现一秒钟,然后就消失了。当我将鼠标移出窗口时,一个三角形不知从哪里伸出来。

这是课程:

class circle
{
    //every coordinate with have an X and Y
    private:
    GLfloat *_vertices;
    static const float DEG2RAD = 3.14159/180;

    GLfloat _scalex, _scaley, _scalez;
    int _cachearraysize;

    public:

    circle(float scalex, float scaley, float scalez, float radius, int numdegrees)
    {
       //360 degrees, 2 per coordinate, 2 coordinates for center and end of triangle fan
        _cachearraysize = (numdegrees * 2) + 4;

        _vertices = new GLfloat[_cachearraysize];
        for(int x= 2; x < (_cachearraysize-2); x = x + 2)
        {
            float degreeinRadians = x*DEG2RAD;
            _vertices[x] = cos(degreeinRadians)*radius;
            _vertices[x + 1] = sin(degreeinRadians)*radius;
        }


       //get the X as X of 0 and X of 180 degrees, subtract to get diameter.  divide
       //by 2 for radius and add back to X of 180
        _vertices[0]= ((_vertices[2] - _vertices[362])/2) + _vertices[362];

        //same idea for Y
        _vertices[1]= ((_vertices[183] - _vertices[543])/2) + _vertices[543];

        //close off the triangle fan at the same point as start
        _vertices[_cachearraysize -1] = _vertices[0];
        _vertices[_cachearraysize] = _vertices[1];

        _scalex = scalex;
        _scaley = scaley;
        _scalez = scalez;

    }
    ~circle()
    {
        delete[] _vertices;
    }

    void draw()
    {
        glScalef(_scalex, _scaley, _scalez);
        glVertexPointer(2,GL_FLOAT, 0, _vertices);
        glDrawArrays(GL_TRIANGLE_FAN, 0, _cachearraysize);
    }
};

I'm no mathematician, but I need to draw a filled in circle.

My approach was to use someone else's math to get all the points on the circumference of a circle, and turn them into a triangle fan.

I need the vertices in a vertex array, no immediate mode.

The circle does appear. However, when I try and overlay circles strange things happen. They appear only a second and then disappear. When I move my mouse out of the window a triangle sticks out from nowhere.

Here's the class:

class circle
{
    //every coordinate with have an X and Y
    private:
    GLfloat *_vertices;
    static const float DEG2RAD = 3.14159/180;

    GLfloat _scalex, _scaley, _scalez;
    int _cachearraysize;

    public:

    circle(float scalex, float scaley, float scalez, float radius, int numdegrees)
    {
       //360 degrees, 2 per coordinate, 2 coordinates for center and end of triangle fan
        _cachearraysize = (numdegrees * 2) + 4;

        _vertices = new GLfloat[_cachearraysize];
        for(int x= 2; x < (_cachearraysize-2); x = x + 2)
        {
            float degreeinRadians = x*DEG2RAD;
            _vertices[x] = cos(degreeinRadians)*radius;
            _vertices[x + 1] = sin(degreeinRadians)*radius;
        }


       //get the X as X of 0 and X of 180 degrees, subtract to get diameter.  divide
       //by 2 for radius and add back to X of 180
        _vertices[0]= ((_vertices[2] - _vertices[362])/2) + _vertices[362];

        //same idea for Y
        _vertices[1]= ((_vertices[183] - _vertices[543])/2) + _vertices[543];

        //close off the triangle fan at the same point as start
        _vertices[_cachearraysize -1] = _vertices[0];
        _vertices[_cachearraysize] = _vertices[1];

        _scalex = scalex;
        _scaley = scaley;
        _scalez = scalez;

    }
    ~circle()
    {
        delete[] _vertices;
    }

    void draw()
    {
        glScalef(_scalex, _scaley, _scalez);
        glVertexPointer(2,GL_FLOAT, 0, _vertices);
        glDrawArrays(GL_TRIANGLE_FAN, 0, _cachearraysize);
    }
};

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

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

发布评论

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

评论(1

望喜 2024-10-11 23:48:10

我想说,这是一些丑陋的代码——很多神奇的数字等等。

尝试这样的事情:

struct Point {
   Point(float x, float y) : x(x), y(y) {}
   float x, y;
};
std::vector<Point> points;
const float step = 0.1;
const float radius = 2;

points.push_back(Point(0,0));
// iterate over the angle array
for (float a=0; a<2*M_PI; a+=step) {
   points.push_back(cos(a)*radius,sin(a)*radius);
}
// duplicate the first vertex after the centre
points.push_back(points.at(1));

// rendering:

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2,GL_FLOAT,0, &points[0]);
glDrawArrays(GL_TRIANGLE_FAN,0,points.size());

您可以根据自己的喜好将其重写为一个类。背后的数学非常简单,不要害怕尝试并理解它。

That's some ugly code, I'd say - lots of magic numbers et cetera.

Try something like:

struct Point {
   Point(float x, float y) : x(x), y(y) {}
   float x, y;
};
std::vector<Point> points;
const float step = 0.1;
const float radius = 2;

points.push_back(Point(0,0));
// iterate over the angle array
for (float a=0; a<2*M_PI; a+=step) {
   points.push_back(cos(a)*radius,sin(a)*radius);
}
// duplicate the first vertex after the centre
points.push_back(points.at(1));

// rendering:

glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2,GL_FLOAT,0, &points[0]);
glDrawArrays(GL_TRIANGLE_FAN,0,points.size());

It's up to you to rewrite this as a class, as you prefer. The math behind is really simple, don't fear to try and understand it.

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