• Advertisement
Sign in to follow this  

Point Light and Normals

This topic is 798 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a room (actually it's a box), I'm trying to lit the inside of the room, so I added point light but it seems that the point light is only working correctly if it's outside of the box (inside the box it doesn't lit all the box faces correctly).

 

I think the reason why is because of the box normals facing directions to outside of the box instead of inside of the box

 

How do I make the point light work correctly no matter if it's inside the box or outside the box?

 

I have something similar to this in my point light pixel shader:

...
...
float nDotL = saturate(dot(normal, normalize(lightDir)));
return lightColor * nDotL * atten;
Edited by Medo3337

Share this post


Link to post
Share on other sites
Advertisement

@L. Spiro:

 

Here is what happens

 

Here you can see 2 point lights (the white one is large enough to cover the 6 box faces)

 

If I reverse the normals in Pixel Shader:

[attachment=30671:screenshot 1.png]

 

If I don't reverse the normals:

[attachment=30672:screenshot 2.png]

Share this post


Link to post
Share on other sites
Conditionally flip them depending on whether it's a backface or not - e.g.
float3 V = cameraPosition - pixelPosition;
N *= dot(N, V) >= 0 ? 1 : -1;
or
float4 ps_main( bool ff : SV_IsFrontFace ) { 
N *= ff ? 1 : -1;

Share this post


Link to post
Share on other sites

In the equation you souldnt use "lightDir" but inverse light dir, (aka point to light, not light to point). Its a bit stange in the 1st moment, but when you draw the vectors on a paper and do the dot' products you will undestand why.

Edited by imoogiBG

Share this post


Link to post
Share on other sites

@vinterberg: If the I set the normals to face the inside of the box and I can one light inside the box and another one outside of the box, The light inside will work while the light outside will not work correctly.

 

@Hodgman: Will this work correctly if I have a character walking and he intersect the point light source?

Share this post


Link to post
Share on other sites

You should try adding some hint as to where the normals are facing, either lines on the vertex or just a debug pass to render the normals as colors per pixel.  It looks like the boxes and the room are all outward facing cubes, you can't flip them all or else half will still be wrong.  The rooms normals need to face in while the boxes need to face out, having a debug screen to show the normal directions can help identify those issues.

 

To correct them depends on how you setup your data, is it an imported mesh or are you creating it on the fly?  If you're manually creating boxes on the fly, then you just need a creation parameter to say if it's a box or a room (so flip the normals/winding with the room and keep the boxes as is).  If you're exporting data from an editor, then check what your data is in the editor and how you're exporting it (to make sure things aren't accidentally getting flipped).

Edited by xycsoscyx

Share this post


Link to post
Share on other sites


@vinterberg: If the I set the normals to face the inside of the box and I can one light inside the box and another one outside of the box, The light inside will work while the light outside will not work correctly.

Why do you not index/create inside triangles as well as outside triangles? You do not need extra verticies for that.

Share this post


Link to post
Share on other sites

Why do you not index/create inside triangles as well as outside triangles? You do not need extra verticies for that.

 

This would be the simplest and probably best solution. Just add reversed triangles to your index buffer, eg. 0-1-2 also adds a 2-1-0 and so on smile.png

Edited by vinterberg

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement