# Coding Gouraud or Phong shading on a polygon

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

## Recommended Posts

I'm implementing smooth groups for our lightmapper.  This means each face's normal needs to be interpolated across a face for the lighting equation, instead of just using the flat polygon normal.

Our lightmapper works on BSP-style, where each side is coplanar and can have any number of sides.

So my question is, is there a simple way to do Gouraud or Phong shading on a coplanar polygon?  Is there any other routine that can calculate this?  I tried messing around with a vertex distance method, but I don't think that will work.

##### Share on other sites

So my question is, is there a simple way to do Gouraud or Phong shading on a coplanar polygon?

Coplanar polygons are flat by definition.  Any automatic processing will produce flat shading ... because it is flat.

Artists can include vertex normals for you to use at every corner. Maya and other 3D tools can generate vertex normals for you.

If you go that route you might as well use SH lighting shader and modern lighting techniques, rather than those two techniques pioneered in the 1970's.  I mean, they are great when you don't have 3D hardware available, but the reason for programmable shaders was so we could advance past those techniques.

Edited by frob

##### Share on other sites

I have to calculate this on the CPU because it's for calculating lightmapping on a curved surface.

To make matters worse, the pixels are not guaranteed to be inside the polygon.  There's a bit of a bleed border where lighting is calculated beyond the bounds of the polygon.

Edited by Josh Klint

##### Share on other sites

In that case you are breaking up the polygon in code. You will need to calculate the new normal at each vertex and store that for use with your shader.

But what do you calculate for that normal?

There is no easy solution to that problem.  Something may be broken up along a smooth curve, or along a hard edge. That's why artists usually specify vertex normals, rather than code.

##### Share on other sites

I'm generating the lightmaps, not rendering geometry.

##### Share on other sites

Well, I did have to write a full Gouraud implementation.  It took about 100 lines of code.

I still have problems with luxels that aren't within any single triangle.  To fix that, I think I will have to find the closest triangle, and use the closest point on that triangle for the normals calculation.

##### Share on other sites

I have been waiting years to use smoothing groups with CSG. :D

Will post a full blog about what I did.

[attachment=15724:lightmap7.png]

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633709
• Total Posts
3013481
×