Modify rasterizer interpolation

Hello everybody, and thanks for your valuable time :slight_smile:

I am currently learning cg vertex fragment shader (on cg official tuto, which is really great!) and I have a question about the rasterizer interpolations : I understand that rasterizer does a linear interpolation for COLOR and TEXCOORD# semantics. I am writing a light per-fragment shader, and I need fragment’s normal vector for both diffuse ans specular calculations. Problem : passing NORMAL from vertex to fragment via a TEXCOORD create linear interpolation of fragment’s normal, which is not good for “planar geometries” (normals should be the same for all fragments on a same plane, and rasterizer create a linear between the corner of these planes…)

So my question is : is there a way to modify the type of interpolation the rasterizer is doing ? Or a way to obtain more precise fragment’s normal on planes ?

Thanks for your help! :smile:

Why are your normals not all the same, for a plane? That’s a problem with your model data, not interpolation.

You can normalize the normal in the fragment shader to pull it back out to unit length.

On a flat plane, this shouldn’t matter anyway, though (because the normals at all the vertices will be the same). Not sure you mean by planar geometries other than that.

Thanks for your answers :slight_smile:

Well, maybe I have not understood how is defined a vertex’s normal…For me, it is an average off all the polygons normals which join at this vertex :

http://www.emeraldinsight.com/content_images/fig/1560100501023.png

So if I take one plane from a cube, the 4 corner vertex won’t have the same normal…and so the interpolation between these won’t give a plane…? :face_with_spiral_eyes:

Thanks for your help :slight_smile:

Depends on the smoothing of the cube.

A cube with flat shaded sides will actualy have 4 unique vertices for each face (i.e. it would have 24 vertices for the whole mesh) with the vertices for each face sharing the same normal. Some of the vertices may share the same position (the 3 vertices at each corner) but not share the same normal.

If it’s a smoothed cube, then it’d be 8 vertices for the whole mesh, with each vertex normal pointing outwards from the centre (as an average of all of the faces that use that vertex).

Same deal for any vertex attribute (UV, UV2, normal, tangent, colour, material) - anywhere that a vertex doesn’t share all of these with another vertex in the mesh, a second vertex has to be created. This tutorial covers that concept very nicely; Beautiful, Yet Friendly Part 2: Maximizing Efficiency - although it only mentions UV, material and shading splits, the same things applies to all per-vertex attributes.

Wow! Farfarer you solved my problem :stuck_out_tongue:
This is clear, and it also answers some other questions I had about passing from smooth shading to flat ones
Thanks a lot :smile:

PS : Do you have fun at Rockstar :wink: ?