This morning I received my copy of GPU Pro book. I am currently reading and trying to understand

The code is like this:

*NPR Effects Using the Geometry Shader*by Pedro Hermosilla and Pere-Pau Vazquez. In*Sillhouette Detection and Geometry Generation*section, there's a piece of code that describes how to check if a triangle is front facing. This is actually simple stuff but I just want to extract some tips from it.The code is like this:

// Calculate the triangle normal and view direction. float3 normalTrian = getNormal( input[0].Pos.xyz, input[2].Pos.xyz, input[4].Pos.xyz ); float3 viewDirect = normalize( -input[0].Pos.xyz - input[2].Pos.xyz - input[4].Pos.xyz ); // If the triangle is frontfacing if (dot(normalTrian, viewDirect) > 0.0f) { ... }Initially, I was wondering how did they get viewDirect? I finally realized, that it's just the view vector. In order to get the view vector from the triangle to the camera position, we need to subtract the

*camera position*with*triangle position*. Since the calculation is in view space, we can derive:// In view space, so the position is in the origin float3 cameraPos = float3(0,0,0); // We can assume the triangle position to be the average of the three vertices position float3 trianglePos = (input[0].Pos.xyz + input[2].Pos.xyz + input[4].Pos.xyz) / 3; // The view direct would be float4 viewDirect = cameraPos - trianglePos; viewDirect = normalize(viewDirect); // Now work simplify above calculation and you will get float3 viewDirect = normalize( -input[0].Pos.xyz - input[2].Pos.xyz - input[4].Pos.xyz );Another thing to note is that: to determine if a triangle is front facing, we just need to know the sign. So actually we don't really need to normalize the

*viewDirect*vector. For instance:float3 viewDirect = -input[0].Pos.xyz - input[2].Pos.xyz - input[4].Pos.xyz;I will share more tips as I'm reading this article.