badcodenotreat
badcodenotreat

Reputation: 336

Lighting does not work with gluSphere

This is a simple issue that I'm somewhat ashamed to ask for help on.

I'm making a simple call to gluSphere to render a sphere, however, it does not light properly even though I'm pretty sure I added the normals and lighting correctly. If, however, I add a texture, the model lights normally, except it seems to be always SMOOTH, and I cannot change it to flat.

This is the lighting code in my init() function:

gl.glLightfv( GL.GL_LIGHT0, GL.GL_AMBIENT , AMBIENT_LIGHT, 0 );
gl.glLightfv( GL.GL_LIGHT0, GL.GL_DIFFUSE , DIFFUSE_LIGHT, 0 );
gl.glLightfv( GL.GL_LIGHT0, GL.GL_POSITION, light_pos    , 0 );
gl.glEnable ( GL.GL_LIGHT0 );
gl.glEnable ( GL.GL_LIGHTING );

this is my sphere code in my display() function:

gl.glColor3d(1.0, 1.0, 1.0);
glu.gluQuadricDrawStyle  (quad, GLU.GLU_FILL);
glu.gluQuadricNormals    (quad, GLU.GLU_FLAT);
glu.gluQuadricOrientation(quad, GLU.GLU_OUTSIDE);

glu.gluSphere(quad, 1.0, lat, lon);

Please advise.

EDIT: light values:

public final static float[] DIFFUSE_LIGHT  = {  1.0f, 1.0f,  1.0f, 1.0f };
public final static float[] AMBIENT_LIGHT  = {  0.3f, 0.3f,  0.3f, 1.0f };
public              float[] light_pos      = { -2.0f, 2.0f, 10.0f, 0.0f };

added materials, no change:

gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT , new float[]{0.5f, 0.5f, 0.5f, 1.0f}, 0);
gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE , new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 0);
gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, new float[]{0.7f, 0.7f, 0.7f, 1.0f}, 0);
gl.glMaterialf (GL.GL_FRONT, GL.GL_SHININESS, 0.5f);
gl.glMaterialfv(GL.GL_FRONT, GL.GL_EMISSION, new float[]{0.3f, 0.3f, 0.3f, 0.0f}, 0);

EDIT2:

Blah, I figured i had a:

gl.glEnable(GL.GL_TEXTURE_2D);

active somewhere and it was causing my model not to have shading if there was no texture associated with it. -_- carry on good people, carry on.

Upvotes: 2

Views: 4423

Answers (3)

Li Xiaosheng
Li Xiaosheng

Reputation: 41

I encountered lighting problem too and figured out that i need to add glEnable(GL_NORMALIZE).

Upvotes: 2

Andrei Krotkov
Andrei Krotkov

Reputation: 5684

  1. Are there any translations/rotations you do that may affect the position of the light?
  2. Is GL_COLOR_MATERIAL enabled or disabled? It may overwrite your material settings if it is.
  3. Even if your code is not enabling GL_TEXTURE_2D, you should try disabling it manually right before the line is reached, just in case.
  4. If you wish to ignore gluSphere completely, there's some code in this thread that you can use.

Upvotes: 0

Bill Prin
Bill Prin

Reputation: 2518

What kind of lighting are you expecting? The third parameter to glLightfv is supposed to be the values of the light you are setting.

Upvotes: 0

Related Questions