简体   繁体   中英

OpenGL: Help to draw sphere/circle using gl function

In my project I have a requirement to draw a sphere (may be semi-sphere will be ok). For this purpose I used sin() and cos() methods to calculate the angle for (x,y) co-ordinates. But following this method I feel it is hitting performance of my project. Can anyone help me for drawing a sphere with simple gl functions.

Like below I defined a structure:

typedef struct
{
    float x;
    float y;
}Semi_Sphere;

Semi_Sphere Sky_SemiSphere[180000], Grnd_SemiSphere[180000];

In the below method I create an array to store the (x,y) co-ordinates. This method I call in my main() function.

void createSemi_Sphere_Table (void)
{
float angle_d = 1.1f, angle_r=0.0;
float const Gl_Pi = 3.14;
int i = 0;

while ( angle_d < 11.0 )
{
    angle_r = Gl_Pi/angle_d;
    Sky_SemiSphere[i].y = 1.0f + (((3.50)*sin(angle_r)));
    Sky_SemiSphere[i].x = ((3.7)*cos(angle_r)) - 0.52f;

    angle_d = angle_d + 0.001;
    i = i+1;
}

} 

Then, I use those (x,y) co-ordinates in the below method to draw my sphere. I call drawSemi_Sphere_Grnd() method in my drawScene() method.

void drawSemi_Sphere_Grnd (void)
{
int L_Index = 0;

glPushMatrix();
for (L_Index = 0; L_Index < 9750; L_Index++)
{
    glBegin(GL_LINES);
        glVertex2f(Grnd_SemiSphere[L_Index].x, Grnd_SemiSphere[L_Index].y);
        glVertex2f(-1.0f, -2.1f);
    glEnd();
}
glPopMatrix();
}

By the above procedure i get sphere. But the performance is slow.

There are many things you can do to speed this up, from using vertex arrays to buffer objects to display lists. But the simplest is this:

glBegin(GL_LINES);
for (L_Index = 0; L_Index < 9750; L_Index++)
{
    glVertex2f(Grnd_SemiSphere[L_Index].x, Grnd_SemiSphere[L_Index].y);
    glVertex2f(-1.0f, -2.1f);
}
glEnd();

Rather than issuing 9750 separate GL_LINES primitives, you draw one list of lines that is 9750 lines long.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM