• Home
  • Textbooks
  • Mathematics for Game Programming and Computer Graphics: Explore the essential mathematics for creating, rendering, and manipulating 3D virtual environments
  • Reviewing Our Knowledge 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 8

Reviewing Our Knowledge of Triangles - all with Video Answers

Educators


Section 1

Comparing similar triangles

Problem 1

Create a new folder in PyCharm called Chapter Eight and make copies for it of the final versions of the files from Chapter Seven. Then, include Button. py, Cube.py, Mesh3D. py, object.py, Settings . py, Transform.py, and Utils.py. Also, make a copy of AddingButtons. py from Chapter Seven for Chapter Eight but rename it DisplayTeapot.py.

Check back soon!

Problem 2

In the Chapter Eight folder, create a folder called models and copy the teapot.obj file from GitHub into it.

Check back soon!

Problem 3

Create a new Python script called LoadMesh.py and add the following code:
from Mesh3D import *
class LoadMesh(Mesh3D):
def __init__(self, draw_type, model_filename):
self.vertices, self.triangles =
self.load_drawing(model_filename)
self.draw_type = draw_type
def draw(self):
for t in range(0, len(self.triangles), 3):
glBegin(self.draw_type)
glVertex3fv(self.vertices[
self.triangles[t]])
glVertex3fv(self.vertices[
self.triangles[t + 1]])
glVertex3fv(self.vertices[
self.triangles[t + 2]])
glEnd()
glDisable(GL_TEXTURE_2D)

Check back soon!

Problem 4

To load this model into the project, open DisplayTeapot.py and make the following changes:
import math
import pygame.mouse
from Object import *
from Cube import *
from LoadMesh import *
from pygame.locals import *
...
objects_2d = []
cube = Object("Cube")
cube.add_component(Transform((0, 0, -5)))
cube.add_component(LoadMesh(GL_LINE_LOOP,
"models/teapot.obj"))
objects_3d.append(cube)

Check back soon!