Download as a PDF
Transcript
CHAPTER 4. DESIGN AND IMPLEMENTATION
15
y
f(x) = sin(x)
x
Figure 4.3: Surface of Triangles
To create the triangles the following algorithm is used:
p = 0
q = 0
for (j=0;j<(BodySections-1);j++){
for (i=0;i<CircleSections;i++){
p=i+1
if (p >(CircleSections-1)) {
p=0
}
triangle (pointsList[j+1][p],pointsList[j+1][i],pointsList[j][i])
q ++
triangle (pointsList[j][i],pointsList[j][p],pointsList[j+1][p])
}//i
}//j
Once the triangles have been created, a surface normal needs to be calculated for each point
to ensure correct lighting in OpenGL. For a particular point all the surrounding triangles need
to have their surface normals calculated and then these surface normals are averaged to get the
surface normal at that point. To calculate the normal for a triangle, two vectors are found tangent
to the surface, the dot product of these two vectors is perpendicular to both, and is therefore
perpendicular to the triangle. Two sides of the triangle are tangent to the the surface [9] and are
therefore used. It is a good idea to normalize the resulting normal vector. Normalizing a vector
produces a vector parallel to the original, but of unit length.
In Figure 4.4 to calculate the normal at point A, the normal vectors are found for the triangles
1 through to 6, and then averaged.