Search code examples
openglgraphics3dlwjgl

How to create a wiresphere in openGL without glutWireSphere()?


I'm trying to find a way to draw a wire sphere using LWJGL (Light Weight Java Game Library), a derived from openGL. I have seen that using GLUT I could use the function glutWireSphere(double, int, int), but what about LWJGL? is there any way to do that similarly? Not all people want to use GLUT. I was looking for that but I haven't found anything yet. Thanks in advance.


Solution

  • Well, as Vincent said, it seems that there's no way to do that as simple as with glut. But we can do that with more code. Spektre contributed a way in his comment. This is a way I found to do that using the sphere's parametric equation:

    public void myWireSphere(float r, int nParal, int nMerid){
        float x,y,z,i,j;
        for (j=0;j<Math.PI; j+=Math.PI/(nParal+1)){
            glBegin(GL_LINE_LOOP);
            y=(float) (r*Math.cos(j));
            for(i=0; i<2*Math.PI; i+=Math.PI/60){
                x=(float) (r*Math.cos(i)*Math.sin(j));
                z=(float) (r*Math.sin(i)*Math.sin(j));
                glVertex3f(x,y,z);
            }
            glEnd();
        }
    
        for(j=0; j<Math.PI; j+=Math.PI/nMerid){
            glBegin(GL_LINE_LOOP);
            for(i=0; i<2*Math.PI; i+=Math.PI/60){
                x=(float) (r*Math.sin(i)*Math.cos(j));
                y=(float) (r*Math.cos(i));
                z=(float) (r*Math.sin(j)*Math.sin(i));
                glVertex3f(x,y,z);
            }
            glEnd();
        }
    }
    

    Well, it's good enough. You can view better this 3D graphic if you add glRotatef(). For example, you can run the code (in the main loop) this way:

    float radius=50
    glRotatef(30,1f,0f,0f);
    myWireSphere(radius, 10, 10);
    

    Hope this will be helpful for those with the same problem.