Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Light problem  (Read 1593 times)
0 Members and 1 Guest are viewing this topic.
Offline davicente

Senior Newbie





« Posted 2010-07-07 10:59:31 »

Hi.
I have a problem with lights, and I can´t solve it.
I´m drawing several shapes which are composed by polygons. I apply the same colors and lights to all shapes. All polygons have one face lighted and the other shaded. The problem is that in some shapes the faces lighted are just the opposite that in the others, and this causes an annoyed scene.
I have made some tests, and I can change the face lighted inserting the points just in the opposite order, but I can´t control this with a lot of shapes.

Is there any way to light the two faces of the polygons (   I have tried with glMaterialfv(GL.GL_FRONT_AND_BACK,   doesn´t work to me  ), or how can I get to light always the face in the same orientation??

Thanks.
Offline lhkbob

JGO Knight


Medals: 32



« Reply #1 - Posted 2010-07-07 14:04:00 »

Within OpenGL, the orientation of a polygon is determined by the 'winding' or ordering of the vertices as they're given to OpenGL.  If the points are given in counter-clockwise order when projected onto the screen, the face is considered to be front-facing, if the ordering is clockwise it's a backfacing polygon.

This can be switched by using the command, glFrontFace() with GL_CW or GL_CCW to change which ordering is considered as being front-facing.  However, IMO it is much better to specify the ordering that your shapes must abide by.  Most modeling tools use CCW ordering already, and if you're writing your own shapes that have inconsistent ordering, then it's a bug within your model specification.

You can enable two-sided lighting, though, by using glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE)

Offline davicente

Senior Newbie





« Reply #2 - Posted 2010-07-08 06:58:14 »

Thanks for the answer lhkbob.
I have no control to create the shapes, so I can´t choose the order of vertices. I have tried with glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE), but it change the face which is lighted, but it doesn´t light the two faces of shapes.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline lhkbob

JGO Knight


Medals: 32



« Reply #3 - Posted 2010-07-08 13:51:13 »

If you don't have control over creating the shapes, you need to specify the ordering policy otherwise there's not much you can do.  It is a lot more work for the graphics card if it can't cull back faces or has to light the back faces, too.  The winding of triangles is not a bad requirement, as said before, most modeling tools already do this.

Could post a screenshot though, the two-sided lighting should do two-sided lighting. I haven't used it too much though, so I'm wondering what it looks like, or I might be able to notice some other problem.

Offline davicente

Senior Newbie





« Reply #4 - Posted 2010-07-08 14:17:19 »

I´m trying to get access to the process of creating shapes, perhaps I can transform some code. But I can´t solve one problem. How to know when a polygon is given in counter-clockwise order, or if the ordering is clockwise.
Thanks for your answers Wink
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2010-07-08 15:29:49 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
Triangle3D tr3 = ...;
Triangle2D tr2 = matrix.transform(tr3);
Point2D p0 = tr2.getPoint(0);
Point2D p1 = tr2.getPoint(1);
Point2D p2 = tr2.getPoint(2);

if(crossProduct2D(subtract(p1,p0), subtract(p2,p0)) > 0)
  // CW
else
  // CCW

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline davicente

Senior Newbie





« Reply #6 - Posted 2010-07-09 07:49:34 »

Hi again.
Thanks for the answer.

Now I have all my polygons directed to the same side, and the most of the polygons show correctly. But I continue with a problem. Some of the polygons appear black, they doesn´t apply the color that I have put to gl. I have made some tests and it seems to be related with concave and convex polygons, because when I delete some point it works. I delete the points which go toward the inside of the polygon.
I´m going to try to show an example of two polygons (with extrude) that execute the same code, the only difference is one point. Do you have some idea how to solve it??
Regards




Offline Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2010-07-09 07:52:28 »

The normal is probably pointing in the wrong direction.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline davicente

Senior Newbie





« Reply #8 - Posted 2010-07-09 08:49:35 »

Your are right. It is a problem of the normal. If I change the points to calculate the normal, sometimes it draws correctly, but others it doesn´t, but I don´t know how to know if the normal is correctly calculated. I use always the same code to calculate it. Some idea?
Offline lhkbob

JGO Knight


Medals: 32



« Reply #9 - Posted 2010-07-09 16:43:15 »

I'm assuming you're triangulating the concave polygons, so it's likely that there's a bug in your normal generation during the triangulation.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 793
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2010-07-09 19:27:09 »

Your are right. It is a problem of the normal. If I change the points to calculate the normal, sometimes it draws correctly, but others it doesn´t

That'd odd. If you swap two points (or in general, change the winding order), you must invert the normal, always.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (22 views)
2014-09-12 09:08:26

Norakomi (55 views)
2014-09-10 13:57:51

TehJavaDev (66 views)
2014-09-10 06:39:09

Tekkerue (33 views)
2014-09-09 02:24:56

mitcheeb (54 views)
2014-09-08 06:06:29

BurntPizza (38 views)
2014-09-07 01:13:42

Longarmx (24 views)
2014-09-07 01:12:14

Longarmx (30 views)
2014-09-07 01:11:22

Longarmx (28 views)
2014-09-07 01:10:19

mitcheeb (37 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!