Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Does lighting work?  (Read 1398 times)
0 Members and 2 Guests are viewing this topic.
Offline dolachin

JGO n00b
*

Posts: 25


Java games rock!


« on: 2003-09-05 14:44:55 »

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.


2. 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
Offline Caoimhin

Full Member
**

Posts: 136



« Reply #1 on: 2003-09-05 17:46:59 »

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);
Offline cfmdobbie

JGO Wizard
****

Posts: 1257


Who, me?


« Reply #2 on: 2003-09-06 17:17:44 »

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

Hellomynameis Charlie Dobbie.
Games published by our own members! Go get 'em!
Offline dolachin

JGO n00b
*

Posts: 25


Java games rock!


« Reply #3 on: 2003-09-06 21:41:51 »

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
Offline GKW

Sr. Member
**

Posts: 453


Revenge is mine!


« Reply #4 on: 2003-09-07 00:07:57 »

You need to put your glNormal call before glVertex.
Offline cfmdobbie

JGO Wizard
****

Posts: 1257


Who, me?


« Reply #5 on: 2003-09-07 04:42:08 »

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.


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!

Hellomynameis Charlie Dobbie.
Offline blahblahblahh

JGO Kernel
*****

Posts: 4575


http://t-machine.org


« Reply #6 on: 2003-09-07 06:04:20 »

Quote


"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 Smiley).

...wonders if a page of "typical OpenGL mistakes, and what they look like" exists anywhere on the web? Smiley

malloc will be first against the wall when the revolution comes...
Offline Java Cool Dude

JGO Ninja
***

Posts: 680


Java forever


« Reply #7 on: 2003-09-07 10:29:26 »

Quote


"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 Smiley).

...wonders if a page of "typical OpenGL mistakes, and what they look like" exists anywhere on the web? Smiley


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

JGO n00b
*

Posts: 25


Java games rock!


« Reply #8 on: 2003-09-07 12:06:12 »

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.
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.092 seconds with 20 queries.