# Point Light and Normals

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

## 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 on other sites
If the normals are back-facing then reverse them in the pixel shader.

L. Spiro Edited by L. Spiro

##### Share on other sites

@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 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 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 on other sites

Best solution would probably be to correct your model normals to face the correct way :)

##### 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 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 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 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

Edited by vinterberg

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 13
• 19
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
632934
• Total Posts
3009301
• ### Who's Online (See full list)

There are no registered users currently online

×