Lighting, Normals, PS and VS ... Do I understand this correct?

Started by
3 comments, last by jkristia 10 years, 7 months ago

maybe a little cryptic topic, and maybe a silly question, but I'm looking at adding some light to my simple scene, and I would like to know if I understood this correct.

1: Values calculated in VS and send to PS are all (in case a struct is returned) interpolated from value of vertex1 to vertex2 (and3) ?

2: To calculate simple diffuse light (for a starter) I need to have a vertex normal for each vertex. This will be calculated at the time I generate the mesh.

3: For a 'smooth' surface the vertex normal should be calculated as an average of the neighboring surface normal.

4: For lighting I can either

a - calculate the color at the vertex, based on the vertex normal and lighting formula, and then the color will be interpolated from V1 to V2 (Lamberts shading)

b - pass the normal from VS to PS. The normal will now be interpolated from V1 to V2, and in the PS it will be normalized and can then be used for light calculation (Blinn-phong shading)

I think this makes sense, but I'm just double checking.

Thanks

Advertisement

1. Yes. They are interpolated across the primitive (for solid meshes the triangle)

2. For lighting you need a (surface) normal. Interpolated vertex normals are one way to achieve this, yes.

3. Yes, if you wanna do this yourself. 3D editors have such functionality already, of course. You could vary the calculation with weighting (e.g. based on face area, or distance from face center). Make sure you duplicate vertices where you want hard edges: Same position, but different normals. A standard cube is a good example: Though it has only 8 corners (positions) you will need 24 vertices (6 * 4) for proper lighting.

4. Yes, almost. You will have to re-normalize in the pixel shader manually (explicitly). Interpolated normals aren't - well - normal anymore :wink:

perfect, thank you very much

Yes to all of your points. To add some detail:

1: Values calculated in VS and send to PS are all (in case a struct is returned) interpolated from value of vertex1 to vertex2 (and3) ?

Not just when a struct is returned. Always when passing data through TEXCOORDs (also called interpolants) they will be interpolated.
As a side note, there is a way to pass the value from vertex1 to vertex2 and 3 without being interpolated, but it's hardly ever used as it is a bit difficult to manage.

3: For a 'smooth' surface the vertex normal should be calculated as an average of the neighboring surface normal.

Yes, though as unbird said, you need to renormalize after the average because it's not guaranteed to be unit-length anymore.

4: For lighting I can either
a - calculate the color at the vertex, based on the vertex normal and lighting formula, and then the color will be interpolated from V1 to V2 (Lamberts shading)

Yes, this technique is ancient and called gouraud shading (very used in the 90's and beginning of 2000) and tends to look very bad by today's standards unless there's a high vertex count. It's very fast though, and sometimes as a nice alternative for mobile games, or in combination with per-pixel lighting (what you listed as 4b) when the light count is very high (i.e. lit vertices with lights 8 to 16 using vertex shaders, lit pixels with lights 0 to 7 using pixel shaders).

These two options are called "forward rendering" techniques (because you lit while you draw).

Though, other alternatives quite different to your options 4a & 4b are deferred shading (called like that because you lit after you've drawn) and forward+ (also called clustered forward, forward deferred; it's more like the forward pass you're learning, but involves a preprocessing step similar to deferred shading); but since you're just getting into shaders & lighting, forward rendering techniques are the best starting point.

Btw. deferred shading & forward+ are just techniques whose only advantage is that they increase the number of lights that can be used in the scene at the same time without hurting performance, normally they don't produce higher quality results than forward rendering (though it's arguable that if you can use more lights with the same framerate, you can make it look better; but it's not that they're inherently higher quality)

Thank you very much for the additional information.

This topic is closed to new replies.

Advertisement