Nice Looking 3D Spheres

Hi,

I’m beginner in OpenGL and I was wondering how to create 3D spheres that actually look like that they are in 3D. I’m able to create spheres with gluSphere but these spheres look like 2D objects in 3D world. Using texture mapping I can create nice looking spheres that actually look like 3D objects, but is there any other way?

Light up your demo :stuck_out_tongue:

Ok, I did it (thanks) but now all the spheres have same colours.



private float lightAmb[] = {0.7f, 0.7f, 0.7f, 1.0f};      // Ambient Light
      private float lightPos[] = {4.0f, 4.0f, 6.0f, 1.0f};      // Light Position
      private float lightDif[] = {1.0f, 1.0f, 1.0f, 1.0f};      // Diffuse Light


public void init(GLDrawable drawable) {            
            gl.glShadeModel(GL.GL_SMOOTH);                         // Enable Smooth Shading
            gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);       // Black Background
            gl.glClearDepth(1.0f);                                     // Depth Buffer Setup
            gl.glEnable(GL.GL_DEPTH_TEST);                         // Enables Depth Testing
            gl.glDepthFunc(GL.GL_LEQUAL);                         // The Type Of Depth Testing To Do
            gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);

            gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, lightAmb);      // Set The Ambient Lighting 
            gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, lightDif);      // Set The Diffuse Lighting 
            gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, lightPos);      // Set The Position For Light0

            gl.glEnable(GL.GL_LIGHT0);                                          // Enable Light 0
            gl.glEnable(GL.GL_LIGHTING);                                    // Enable Lighting
...
}


public void display(GLDrawable arg0) {            
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
            gl.glLoadIdentity();                                     // Reset The View
RGB color = model.getRgb();
                  gl.glTranslatef(centerPoint.getX(), centerPoint.getY(), -5.0f);
                  gl.glColor3f(color.getRed(),color.getGreen(),color.getBlue());                              
                  glu.gluSphere(quadric, 1.3f, 32, 32);                  
}      

You can use the following code to enable per-vertex colors with lighting:

gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE);

This line should appear after the light initialization code.
Note: you might need to lower the alpha (opacity) value of your diffuse lighting (e.g. to 0.5) to enable the vertex coloring.

Thats funny, the alpha component of a light source shouldn’t affect the state of vertex colouring at all. Are you sure you didn’t have something else messing up your vertex colours?