• Home
  • Textbooks
  • Mathematics for Game Programming and Computer Graphics: Explore the essential mathematics for creating, rendering, and manipulating 3D virtual environments
  • Manipulating the Light and Texture of Triangles

Mathematics for Game Programming and Computer Graphics: Explore the essential mathematics for creating, rendering, and manipulating 3D virtual environments

Penny de Byl

Chapter 11

Manipulating the Light and Texture of Triangles - all with Video Answers

Educators


Section 1

Displaying Mesh Triangle Normals

01:25

Problem 1

Begin by making an exact copy of your project folder as it was at the very end of Chapter 10, Getting Acquainted with Lines, Rays, and Normals. Name the copied folder Chapter 11.

Mayukh Banik
Mayukh Banik
Numerade Educator

Problem 2

Modify the code in DisplayNormals. py to calculate and use the median of each triangle like this:
...
for $t$ in range $(0$, len(self.triangles), 3$)$ :
vertex1 $=$ self.vertices [self.triangles [t]]
vertex2 $=$ self.vertices [self.triangles $[t+1]]$
vertex3 = self.vertices [self.triangles [t + 2]]
$\mathrm{p}=$ pygame.Vector3(vertex1[0] - vertex2[0],
vertex1[1] - vertex2[1],
vertex1[2] - vertex2[2])
$q=$ pygame. $\operatorname{vector3}($ vertex $2[0]$ - vertex3 [0],
vertex2[1] - vertex3[1],
vertex2[2] - vertex3[2])
norm = cross_product $(p, q)$
\# find median
midpoint $=$ vertex $3+q * 0.5$
$\mathrm{v}=($ midpoint - vertex 1$) * 2 / 3$
centroid $=$ vertex $1+v$
self.normals.append ((centroid, centroid + norm))

Check back soon!

Problem 3

Run the Animate.py file that has been copied to the Chapter 11 folder to see the result. Note the new starting position for the normals, as shown in Figure 11.2 (with a little embellishment to make it obvious):
(Figure can't copy)
Having made these changes, you will now be able to draw any mesh and display the normals.

Check back soon!

Problem 4

To have a look at the normals on a mesh loaded from a file, make a copy of Animate.py and rename it ExploreNormals.py.

Check back soon!

Problem 5

Download a copy of planesm. obj from the Chapter 11/models folder on GitHub and place this file in PyCharm in the Chapter 11/models folder.

Check back soon!

Problem 6

From GitHub, download a copy of ExploreNormals.py from the Chapter11/ startercode folder and replace your code with the code in this new file. This code will replace all the cube and grid drawings, as well as the animations with a wireframe view of planesm.obj. But don't run it yet as we need to make a few more adjustments to get the code to execute correctly.

Check back soon!

Problem 7

The plane you are about to draw is facing the camera. This will make it difficult to see the normals. Therefore, we can temporarily rotate and recolor it by changing the code in object. py like this:
def update(self, events $=$ None):
glPushMatrix()
for $c$ in self.components:
if isinstance(c, Transform) :
pos $=$ c.get_position()
glTranslatef(pos.x, pos.y, pos.z)
glRotate $(45,0,1,0)$
elif isinstance (c, Mesh3D) :
glColor (1, 1, 1)
c. draw ()
elif isinstance (c, Grid) :
c. draw ()

These new lines will rotate the plane by 45 degrees around the $y$-axis and recolor the polygon wireframe to draw in white.

Check back soon!

Problem 8

One last change to make before running ExploreNormals.py is to increase the length of the normals to make them visible. We can do this in DisplayNormals . py by adding a multiplier to the end position of the normals, as follows:
middle $=($ vertex $3+q * 0.5)$
$\mathrm{v}=($ middle - vertexl) $* 2 / 3$
median $=$ vertex $1+\mathrm{v}$
nstart $=$ median
self.normals.append ((nstart, nstart + norm * 10))
By multiplying the normal (which has a length of 1) by 10 , it will make them go in the same direction but display a longer-drawn line.

Check back soon!
02:10

Problem 9

Now it's time to play ExploreNormals.py. When you do, you will see the mesh in white and normals in green, as shown in Figure 11.3:
(Figure can't copy)

LM
Louis M.
Numerade Educator