2010-11-28 4 views
0

Je ne suis pas un mathématicien, mais j'ai besoin de dessiner un cercle rempli.Cercle de dessin OpenGL, bugs étranges

Mon approche consistait à utiliser les maths de quelqu'un d'autre pour obtenir tous les points sur la circonférence d'un cercle, et les transformer en un éventail en triangle.

J'ai besoin des vertex dans un tableau de vertex, pas de mode immédiat.

Le cercle apparaît. Cependant, quand j'essaie de superposer des cercles, des choses étranges se produisent. Ils n'apparaissent qu'une seconde et disparaissent ensuite. Quand je déplace ma souris hors de la fenêtre, un triangle sort de nulle part.

est ici la classe:

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

Où configurez-vous vos tests de profondeur? Pouvez-vous poster un programme SDL/GLUT minimal qui démontre le problème? – genpfault

+0

Le test de profondeur devrait être probablement désactivé s'il s'agit d'un 2D et l'OP s'attend à ce que les cercles apparaissent dans l'ordre d'affichage. – Kos

Répondre

2

C'est un code laid, je dirais - beaucoup de nombres magiques et cetera.

Essayez quelque chose comme:

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

Il est à vous de réécrire cela comme une classe, comme vous préférez. Le calcul derrière est vraiment simple, n'ayez pas peur d'essayer et de le comprendre.