Click here to Skip to main content
15,116,815 members
Articles / Programming Languages / C#
Posted 22 Aug 2007


70 bookmarked

Simple Ray Tracing in C# Part VI (Vertex Normal Interpolation)

Rate me:
Please Sign up or sign in to vote.
4.75/5 (26 votes)
15 Jul 2016GPL33 min read
An approach to interpolate vertex normals on triangles


Screenshot - teapota.png
fig. 1 - one normal per triangle

Screenshot - teapotb.png
fig. 2 - interpolated normals


Screenshot - mirrorteapot.png
fig. 3 - testing with reflective material


In this new article in our ray-tracing series we will start handling more complex objects. These objects are formed by points, vertices and polygons as we have seen before.

Polygons as flat surfaces have one normal vector, and this vector against the light vectors and viewer vector gives us its color. So, working with one normal per polygon face gives a non linear result as we see in fig. 1. To solve this I have implemented an algorithm to obtain normals at each vertex and them for each ray hitpoint provide a normal interpolation between the triangle vertices giving us different normals on each polygon intersection, and hence a linear colouring between the vertices.

Note: All the articles I am posting here are for learning purposes, so do not worry at any point about speed or optimization. Instead we will keep that for more advanced articles, so please do not focus on improvements at this point.


At first I recommend a previous reading in the last ray tracing articles.

Calculating normal vector of triangles

From linear algebra a normal vector of a plane can be obtained by proceeding a cross product between two vectors of this plane.

In our situation a triangle is ideal, by definition 3 points gives us a unique plane.

We have P1, P2 and P3, it is easy to get 2 vectors from this:

a = P1-P2

b = P3-P2 (remember we have 3 coordinates for each point x,y,z)

Screenshot - matrix.png
Fig 3 - cross product matrix

The cross product is given by solving the matrix given by fig. 3:

public static void Cross3(double ax, double ay, double az,
            double bx, double by, double bz,
            ref double outx, ref double outy, ref double outz)
        outx = ay * bz - az * by;
        outy = az * bx - ax * bz;
        outz = ax * by - ay * bx;

At this point our rendering has the fig. 1 result. Now lets think about how to calculate the interpolated normals.

At first we need to get normals for each triangle vertex, but how to achieve this?

Once we already have one normal per triangle, obtained by cross product, lets look to our points grid.

Each point belongs to more than one triangle, so it is easy to get a normal composition for each point by just adding each triangle normal on this. Aha, now we have normals at vertices!

But it is not enough... How to interpolate a hitpoint inside a triangle if we have just the vertices normals?

It is simple, we need to calculate in a similar way as we calculated the proportions for textures in our last article.

If we get u and v proportions for a given point of intersection P the interpolation is given by:

(1.0 - (u + v)) * P2 Normal + P1 Normal * u + P3 Normal * v

or better:

nx = -( (1.0 - (u + v)) * tData.normalVector(nb).x +
      tData.normalVector(na).x * u + tData.normalVector(nc).x * v);
ny = -( (1.0 - (u + v)) * tData.normalVector(nb).y +
      tData.normalVector(na).y * u + tData.normalVector(nc).y * v);
nz = -( (1.0 - (u + v)) * tData.normalVector(nb).z +
      tData.normalVector(na).z * u + tData.normalVector(nc).z * v);

The function

public override void getNormal(double x, double y, double z, 
                               ref double nx, ref double ny, 
                               ref double nz)
            /* code for non interpolated normals
                nx = -tnormal.x;
                ny = -tnormal.y;
                nz = -tnormal.z;

            tPoint U = new tPoint(tData.point(va).x - tData.point(vb).x, 
                           tData.point(va).y - tData.point(vb).y, 
                           tData.point(va).z - tData.point(vb).z);
            tPoint V = new tPoint(tData.point(vc).x - tData.point(vb).x, 
                           tData.point(vc).y - tData.point(vb).y, 
                           tData.point(vc).z - tData.point(vb).z);
            tPoint N = new tPoint(x - tData.point(vb).x,y - 
                           tData.point(vb).y,z - tData.point(vb).z);

            double dU = tAlgebra.modv(U.x, U.y, U.z);
            double dV = tAlgebra.modv(V.x, V.y, V.z);
            double dN = tAlgebra.modv(N.x, N.y, N.z);

            tAlgebra.Normalize(ref N.x, ref N.y, ref N.z);
            tAlgebra.Normalize(ref U.x, ref U.y, ref U.z);

            double cost = tAlgebra.Dot3(N.x, N.y, N.z, U.x, U.y, U.z);
            if (cost < 0) cost = 0;
            if (cost > 1) cost = 1;

            double t = Math.Acos(cost);

            double distY = 0, distX = 0;
            distX = dN * Math.Cos(t);
            distY = dN * Math.Sin(t);

            double u = distX/ dU;
            double v = distY/ dV;

            tAlgebra.Normalize(ref tData.normalVector(na).x, 
                          ref tData.normalVector(na).y, 
                          ref tData.normalVector(na).z);
            tAlgebra.Normalize(ref tData.normalVector(nb).x, 
                          ref tData.normalVector(nb).y, 
                          ref tData.normalVector(nb).z);
            tAlgebra.Normalize(ref tData.normalVector(nc).x, 
                          ref tData.normalVector(nc).y, 
                          ref tData.normalVector(nc).z);

            nx = -( (1.0 - (u + v)) * tData.normalVector(nb).x + 
                     tData.normalVector(na).x * u + 
                     tData.normalVector(nc).x * v);
            ny = -( (1.0 - (u + v)) * tData.normalVector(nb).y + 
                     tData.normalVector(na).y * u + 
                     tData.normalVector(nc).y * v);
            nz = -( (1.0 - (u + v)) * tData.normalVector(nb).z + 
                     tData.normalVector(na).z * u + 
                     tData.normalVector(nc).z * v);

Using the code

The samples includding classes and obj file are inside the zip file for download on the top of the article.

Screenshot - cube.png

Screenshot - cubeII.png

Screenshot - dragon.png

Screenshot - teapot_mirror2.png


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Comments and Discussions

PraiseVery good work. Thanks a lot. Pin
earthmoon1-Jan-18 17:24
Memberearthmoon1-Jan-18 17:24 
SuggestionI would say it's an excellent effort, but ............ Pin
Md. Marufuzzaman8-Apr-13 8:33
professionalMd. Marufuzzaman8-Apr-13 8:33 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 22:45
professionalManoj Kumar Choubey26-Feb-12 22:45 
QuestionLatest source code and library Pin
andalmeida6-Jan-12 6:13
Memberandalmeida6-Jan-12 6:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.