Does lighting work?

Hi,

  1. I may be making an OpenGL mistake or it might be a JOGL bug. I am lighting a sphere, composed exclusively of triangles. I compute the normals on my own and I am very confident that my normals are OK.

http://www.geocities.com/bura3no/lighting.png

The figure on the left is GL_FLAT. The figure on the right is GL_SMOOTH.

  1. Also, I can’t get the back side of my polygons to be a different color from the front side. They both have the same color as GL_FRONT.

Are these known issues or should I post some code?

Many thanks in advance!

Dola

Yes, lighting works. Remeber you have to enable both your light and the lighting system.

gl.glEnable(GL.GL_LIGHT1);
gl.glEnable(GL.GL_LIGHTING);

Yeah, post code. Something like the lighting errors on that second sphere are unlikely to be introduced by JoGL.

Hi,

I’ll start in little specs. I have a concept of a mesh which consists of triangles. The mesh knows how to calculate the normals and I am assured that the normals are correct.

Actually, there only difference between the two spheres on the image is GL_FLAT vs. GL_SMOOTH and I would argue that even on the left GL_FLAT image the shading is wrong.

  gl.glMaterialfv(GL.GL_BACK, GL.GL_AMBIENT_AND_DIFFUSE, myAmbientColorBack.getComponents(null) );
  gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, myAmbientColorFront.getComponents(null) );

  gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_EMISSION, ZERO );

  gl.glMaterialfv(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, Color.WHITE.getComponents(null) );
  gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_FILL);

  for (int t = 0; t < myMesh.pGetNumberOfElements(); t++) {
    Element elem = myMesh.pGetElement(t);
    gl.glBegin(GL.GL_TRIANGLES);
    for (int v = 0; v < elem.pGetNumberOfNodes(); v++) {
      VDouble x = myMesh.pGetX(elem.pGetNode(v));
      VDouble n = normals[elem.pGetNode(v)];
      gl.glVertex3d(x.pGet(0), x.pGet(1), x.pGet(2));
      gl.glNormal3d(n.pGet(0), n.pGet(1), n.pGet(2));
    }
    gl.glEnd();
  }
}

Then elsewhere in my init() I have:

  GL gl = inDrawable.getGL();

  gl.glClearColor(1f, 1f, 1f, 1f);

  gl.glEnable(GL.GL_DEPTH_TEST);
  gl.glEnable(GL.GL_POLYGON_OFFSET_FILL);
  gl.glEnable(GL.GL_POLYGON_OFFSET_LINE);

  // Lights
  gl.glEnable(GL.GL_LIGHTING);
  gl.glLightModelfv(GL.GL_LIGHT_MODEL_AMBIENT, new float[] { .6f, .6f, .6f, 1f });

  myLight0 = new GLUtils.Light(myCanvas3D,  // Self-explanatory, really

GL.GL_LIGHT0,
0f, 0f, 3f,
Color.GRAY,
Color.BLACK);

// Uncomment for FLAT.
// gl.glShadeModel(GL.GL_FLAT);

Thanks for taking a look at this.

Dola

You need to put your glNormal call before glVertex.

Yep, GKW’s got it. Swap those and give it another try. Considering the cases where the lighting is wrong, usually two vertices are correct and only one is off - given that normal n is being applied to vertex n+1 in each “element”, this kind of thing is what you’d expect.

I presume in each triangle the normal is the same for all vertices, and that your sphere is made up of a number of “elements”? The bad normals are probably a default normal being applied to the first vertex in each “element”, or something similar to that.

[quote]Actually, there only difference between the two spheres on the image is GL_FLAT vs. GL_SMOOTH and I would argue that even on the left GL_FLAT image the shading is wrong.
[/quote]
Yep, undoubtedly. The problem is that when in GL_FLAT mode, in each triangle only one vertex’s normal will be used. So the errors are that much harder to spot!

"We’re sorry, but this page is currently unavailable for viewing.
If this site belongs to you, please read this help page for more information and assistance.

For general questions see our main help area, or search for other member pages."

Could you put the image back? It would be helpful to people following in your mistakes to be able to see what that mistake looks like (increases the chances of recognising it when it happens to each of us :)).

…wonders if a page of “typical OpenGL mistakes, and what they look like” exists anywhere on the web? :slight_smile:

Copy the link into a new IE window; Geocities doesn’t allow straight linking to images

Thank you very much! You guys nailed it, of course. I’m now getting beautiful spheres and other shapes.

I’ve put the image with the solution on my index page:

http://www.geocities.com/bura3no/index.htm

to obey the geocities policies so everyone can see the answer. Feel free to use my images if anyone is going to put together that “common OpenGL mistakes and what they look like” page.