OpenGL нарисовать круг, странные ошибки - PullRequest
0 голосов
/ 28 ноября 2010

Я не математик, но мне нужно нарисовать закрашенный круг.

Мой подход состоял в том, чтобы использовать чужую математику, чтобы получить все точки на окружности круга и превратить их ввеерный треугольник.

Мне нужны вершины в массиве вершин, немедленного режима нет.

Круг действительно появляется.Однако, когда я пытаюсь наложить круги, происходят странные вещи.Они появляются только секунду, а затем исчезают.Когда я вывожу свою мышь из окна, из ниоткуда торчит треугольник.

Вот класс:

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);
    }
};

1 Ответ

2 голосов
/ 28 ноября 2010

Я бы сказал, что это некрасивый код - множество магических чисел и так далее.

Попробуйте что-то вроде:

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());

Вам решать переписать это как класс, как вы предпочитаете. Математика очень проста, не бойтесь попытаться понять это.

...