How to Render a Sphere With Glusphere

Save

Open Graphics Library, or OpenGL, is a computer graphics application programming interface (API) used to render two-dimensional and three-dimensional computer graphic images. It can be implemented on a variety of computer platforms and programming languages. OpenGL is used for video game development and scientific visualization. OpenGL does not offer a direct method of sphere rendering, but it does offer the "Glusphere" function, which draws a sphere of the given radius centered around the origin. To create a sphere, you will code a series of lines, calculate the sin and cosine values necessary to create the sphere, and then implement glusphere in the code.

  • Launch the OpenGL Viewport window.

  • Type the following code to provide the sine and cosine functions needed to calculate the circle:

    include <math.h>
  • Type the following code to render a sphere with glusphere:

    void renderSphere(float x, float y, float z, float radius,int subdivisions,GLUquadricObj *quadric)

    {

    glPushMatrix();

    glTranslatef( x,y,z );

    gluSphere(quadric, radius, subdivisions,subdivisions);

    glPopMatrix();

    }

    void renderSphere_convenient(float x, float y, float z, float radius,int subdivisions)

    {

    //the same quadric can be re-used for drawing many spheres

    GLUquadricObj *quadric=gluNewQuadric();

    gluQuadricNormals(quadric, GLU_SMOOTH);

    renderSphere(x,y,z,radius,subdivisions,quadric);

    gluDeleteQuadric(quadric);

    }

References

Promoted By Zergnet

Comments

Related Searches

Read Article

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!