TNM046: Datorgrafik Triangle and Normal
Sasan Gooran
VT 2014
Computer Graphics
What?
“Almost everything on computers that is not text or sound “ The representation and manipulation of image data by
computer
Techniques to create and manipulate images
Where?
Television, Newspapers (weather reports), Medical investigations and surgical procedures
3
Computer Graphics
Applications:
Entertainments (Movies, Video games) Computer Aided Design (CAD)
Medical Applications Scientific Visualization Virtual Reality
….
Computer Graphics
5
Computer Graphics, Objects
In order to define an object, you need to first specify the
coordinates of the vertices. Then you define the triangles the object consists of. Each triangle is defined by specifying its three vertices.
In order to perform different transformations (e.g. scaling, translation, rotation etc.) on an object you transform the
object’s vertices. This is done by multiplying the appropriate transformation matrix with the coordinates of the vertices.
Computer Graphics, TNM046
Triangle and Normal
2D and 3D Transformations Light Sources and Shading Perspective Projection
Culling and Clipping Texture Mapping
7
Vertex and Triangle
Vertex: ( x y z )
Triangle: array of Vertex (vertices)
3D object: array of Triangle (triangles)
Example: Triangle[1] : (10,10,10), (10,20,10), (10,20,20) Triangle[2] : (10,10,10), (10,20,10), (10,10,20)
Not a good approach
Every vertex is repeated
Vertex and Triangle
Vertex: ( x y z )
Vertex table: array of Vertex
Triangle table: array of integers
Example: Vertex table: (10,10,10) , (10,20,10) , (10,20,20), (10,10,20), …..
Triangle table: (1,2,3) , (1,2,4) , ….
3D object: Vertex table + Triangle table
Now all vertices in the object is in one list (table).
9
Vertex and Triangle
Example: Present the following object using vertex and
triangle table. The distance from P1, P2 and P3 to the origin (P0) is unity (1).
x
y z
p0 p1
p2 p3
Triangle 0: p0p1p2 Triangle 1:
p0p1p3
Triangle 2:
p0p2p3
Triangle 3:
p1p2p3
Vertex table:
p0: (0,0,0) p1: (1,0,0) p2: (0,1,0) p3: (0,0,1) Answer:
Triangle table:
Triangle 0: (0,1,2) Triangle 1: (0,1,3) Triangle 2: (0,2,3) Triangle 3: (1,2,3)
Vertex and Triangle
Assume triangle 1 (p0p1p3).
p0 = (0,0,0), p1 = (1,0,0), p3 = (0,0,1),
x z
p0 p1
p3
d
0d
1d
2Difference vectors:
d
0= (1, 0, 0) − (0, 0, 0) = (1, 0, 0)
d
1= (0, 0,1) − (1, 0, 0) = (−1, 0,1)
d
2= (0, 0, 0) − (0, 0,1) = (0, 0, −1)
11
Triangle and Normal
x z
p0 p1
p3
d
0d
1d
2x
y z
p0 p1
p2 p3
Triangle 1:
p0p1p3
d
0×
d
1= (1, 0, 0) × (−1, 0,1) =
ˆx ˆy ˆz
1 0 0
−1 0 1
= (0, −1, 0)
This normal vector points outwards (the object)d
1×
d
2= (−1, 0,1) × (0, 0, −1) = (0, −1, 0) d
2×
d
0= (0, 0, −1) × (1, 0, 0) = (0, −1, 0)
Triangle and Normal
x z
p0 p1
p3
d
0d
1d
2Triangle 1:
p0p1p3
N
1=
d
0×
d
1=
d
1×
d
2=
d
2× d
0Three possible starting points For this triangle three lists are possible:
(0,1,3) or (1,3,0) or (3,0,1)
In order to calculate the normal pointing outwards:
Find the vector from the first point to the second (call A1) Find the vector from the second point to the third (call A2)
13
Triangle and Normal
Triangle table: Let your thumb of the right hand stick out of the object. Pick the first point, then the sense of your fingers gives you the other two points in the right sequence.
x
y z
p0 p1
p2 p3
Triangle 0: p0p1p2 Triangle 1:
p0p1p3
Triangle 2:
p0p2p3
Triangle 3:
p1p2p3
Triangle table:
Triangle 0: (0,2,1) Triangle 1: (0,1,3) Triangle 2: (0,3,2) Triangle 3: (1,2,3)
Compare this triangle table with the one on page 4.
Triangle and Normal
Therefore the following 3D object can be defined as:
y z
p0 p1
p2 p3
Triangle table:
0: (0,2,1) 1: (0,1,3) 2: (0,3,2) 3: (1,2,3)
Vertex table:
0: (0,0,0) 1: (1,0,0) 2: (0,1,0) 3: (0,0,1)
15
Triangle and Normal
In OpenGL, together with the vertex coordinate you also specify the normal vector and the texture coordinates (for texture mapping). Texture mapping will be discussed later.
x
y z
p0 p1
p2 p3
Let us know look at vertex 0 (p0)
Vertex 0 is the junction oft hree triangles,
each of which has its own normal vector, i.e., (0,0,-1), (0, -1, 0) and (-1, 0, 0). Therefore vertex 0 is defined three times:
Vertex p0:
Nr coordinate normal 0: (0,0,0) (0, 0, -1) 1: (0,0,0) (0, -1, 0) 2: (0,0,0) (-1, 0, 0)
Triangle and Normal
y z
p0 p1
p2 p3
Vertex table:
Nr coordinate normal 0: (0,0,0) (0, 0, -1) 1: (0,0,0) (0, -1, 0) 2: (0,0,0) (-1, 0, 0) 3: (1,0,0) (0, 0, -1) 4: (1,0,0) (0, -1, 0)
5: (1,0,0) (0.577,0.577,0.577) 6: Fill the rest, will be discussed in the class (första lektionen)
17
Triangle and Normal
x
y z
p0 p1
p2
p3 Triangle table:
Nr
0: (0,?,3) 1: (?,?,?) 2: (?,?,?) 3: (?,?,?)
Fill the rest, will be discussed in the class (första lektionen)
Triangle 0, consists of (p0, p2, p1). For p0 we now have
three vertices, 0, 1 and 2. We choose the one that has the normal associated with Triangle 0, which will be vertex 0.
For p1 we should choose between vertex nr 3, 4 and 5.
The correct one in this case is vertex nr 3 because it has the normal vector associated with Triangle 0.