Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Triangles And Planes-Computer Graphics-Lecture Notes, Study notes of Computer Graphics

Computer Graphics involves technology to accept, process, transform and present information in a visual form that also concerns with producing images and animations using a computer. This course teach how to make your own design in computer using OpenGl. This lecture includes: Triangles, Planes, Calculation, Flesh, Clockwise, Polygon, Accelerator, Ordering, Boundary, Generally

Typology: Study notes

2011/2012

Uploaded on 08/04/2012

parnashi
parnashi 🇮🇳

4.4

(48)

77 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture No.21 Triangles and Planes
Triangles
Triangles are to 3D graphics what pixels are to 2D graphics. Every PC hardware
accelerator under the sun uses triangles as the fundamental drawing primitive (well …
scan line aligned trapezoids actually, but that's a hardware implementation issue). When
you draw a polygon, hardware devices really draw a fan of triangles. Triangles "flesh out"
a 3D object, connecting them together to form a skin or mesh that defines the boundary
surface of an object. Triangles, like polygons, generally have an orientation associated
with them, to help in normal calculations. The ordering of the vertices goes clockwise
around the triangle. Figure below shows what a clockwise ordered triangle would look
like.
Figure: Three points in space, and the triangle connecting them
When defining a mesh of triangles that define the boundary of a solid, you set it up so that
all of the triangles along the skin are ordered clockwise when viewed from the outside.
It is impossible to see triangles that face away from you. (You can find this out by
computing the triangle's plane normal and performing a dot product with a vector from
the camera location to a location on the plane.)
Now let's move on to the code. To help facilitate using the multiple types, I'll implement
triangles structure. I only define constructors and keep the access public.
struct tri
{
type v[3]; // Array access useful for loops
tri()
{
// nothing
}
tri( type v0, type v1, type v2 )
docsity.com
pf3
pf4
pf5
pf8

Partial preview of the text

Download Triangles And Planes-Computer Graphics-Lecture Notes and more Study notes Computer Graphics in PDF only on Docsity!

Lecture No.21 Triangles and Planes

Triangles

Triangles are to 3D graphics what pixels are to 2D graphics. Every PC hardware accelerator under the sun uses triangles as the fundamental drawing primitive (well … scan line aligned trapezoids actually, but that's a hardware implementation issue). When you draw a polygon, hardware devices really draw a fan of triangles. Triangles "flesh out" a 3D object, connecting them together to form a skin or mesh that defines the boundary surface of an object. Triangles, like polygons, generally have an orientation associated with them, to help in normal calculations. The ordering of the vertices goes clockwise around the triangle. Figure below shows what a clockwise ordered triangle would look like.

Figure: Three points in space, and the triangle connecting them

When defining a mesh of triangles that define the boundary of a solid, you set it up so that all of the triangles along the skin are ordered clockwise when viewed from the outside.

It is impossible to see triangles that face away from you. (You can find this out by computing the triangle's plane normal and performing a dot product with a vector from the camera location to a location on the plane.)

Now let's move on to the code. To help facilitate using the multiple types, I'll implement triangles structure. I only define constructors and keep the access public.

struct tri {

type v[3]; // Array access useful for loops

tri() { // nothing }

tri( type v0, type v1, type v2 )

docsity.com

v[0] = v0; v[1] = v1; v[2] = v2; } };

Strips and Fans

Lists of triangles are generally represented in one of three ways. The first is an explicit list or array of triangles, where every three elements represent a new triangle. However, there are two additional representations, designed to save bandwidth while sending triangles to dedicated hardware to draw them. They are called triangle strips and triangle fans.

Triangle fans, conceptually, look like the folding fans you see in Asian souvenir shops. They are a list of triangles that all share a common point. The first three elements indicate the first triangle. Then each new element is combined with the first element and the current last element to form a new triangle. Note that an N-sided polygon can be represented efficiently using a triangle fan

Figure below illustrates what I'm talking about.

Figure: A list of points composing a triangle fan

Triangles in a triangle strip, instead of sharing a common element with all other triangles like a fan, only share elements with the triangle immediately preceding them. The first three elements define the first triangle. Then each subsequent element is combined with the two elements before it, in clockwise order, to create a new triangle. See Figure below for an explanation of strips.

docsity.com

Figure 1: d is negative when the normal faces away from the origin

Figure 2: d is positive when it faces towards the origin

It's important to notice that technically the normal < a , b , c > does not have to be unit-length for it to have a valid plane equation. But since things end up nicer if the normal is unit- length.

Constructing a plane given three points that lie in the plane is a simple task. You just perform a cross product between the two vectors made up by the three points

<point 2  point 0 >

<point 1  point 0 >

and find a normal for the plane. After generating the normal and making it unit length, finding the d value for the plane is just a matter of storing the negative dot product of the normal with any of the points. This holds because it essentially solves the plane equation above for d. Of course plugging a point in the plane equation will make it equal 0, and this constructor has three of them. Following has the code to construct a plane from three points.

To calculate a plane from 3 given points we first calculate the normal. If we imagine the 3 points form three edges in the plane then we can take two of the edges and calculate the

docsity.com

cross-product between them. The resulting directional vector will be the normal, and then we can plug any of the 3 known points into the plane equation to solve for k. For points p1,p2 and p3 we get:

normal = (p1-p2) x (p3-p2) k = normal * p

Note that it is extremely important to keep track of which direction your points are stored in. Let's take 3 points stored in clockwise direction in the x/y plane:

The normal to the plane these 3 points define is:

normal = (p1-p2) x (p3-p2) = (0,-1,0) x (1,-1,0) = <(-1)0 - 0(-1), 01 - 00, 0(-1) - (-1)1> = <0,0,1>**

ie the z axis. If we were to store the points counter-clockwise the normal calculated would be <0,0,-1>, which is still the z axis but in the "opposite" direction. It's important to keep track of these things since we often need plane equations to be correct in order to determine which side of a polygon an object (such as the view point) is on.

Constructing a plane from three points on the plane:

Normal vector = n n = cross product ( (b-a),(c-a) ) Normalize(n); find a unit vector

d = - dot product(n,a)

If you already have a normal and also have a point on the plane, the first step can be skipped.

docsity.com

partially in front and partially in back. I'll refer to this state as splitting the plane. It's just a term; the element isn't actually splitting anything. Back-face Culling Now that you know how to define a point with respect to a plane, you can perform back- face culling, one of the most fundamental optimization techniques of 3D graphics. Let's suppose you have a triangle whose elements are ordered in such a fashion that when viewing the triangle from the front, the elements appear in clockwise order. Back-face culling allows you to take triangles defined with this method and use the plane equation to discard triangles that are facing away. Conceptually, any closed mesh, a cube for example, will have some triangles facing you and some facing away. You know for a fact that you'll never be able to see a polygon that faces away from you; they are always hidden by triangles facing towards you. This, of course, doesn't hold if you're allowed to view the cube from its inside, but this shouldn't be allowed to happen if you want to really optimize your engine. Rather than perform the work necessary to draw all of the triangles on the screen, you can use the plane equation to find out if a triangle is facing towards the camera, and discard it if it is not. How is this achieved? Given the three points of the triangle, you can define a plane that the triangle sits in. Since you know the elements of the triangle are listed in clockwise order, you also know that if you pass the elements in order to the plane constructor, the normal to the plane will be on the front side of the triangle. If you then think of the location of the camera as a point, all you need to do is perform a point-plane test. If the point of the camera is in front of the plane, then the triangle is visible and should be drawn. There's an optimization to be had. Since you know three points that lie in the plane (the three points of the triangle) you only need to hold onto the normal of the plane, not the entire plane equation. To perform the back-face cull, just subtract one of the triangle's points from the camera location and perform a dot product with the resultant vector and the normal. If the result of the dot product is greater than zero, then the view point was in front of the triangle. Figure below can help explain the point.

Figure: A visual example of back-face culling In practice, 3D accelerators can actually perform back-face culling by themselves, so as the triangle rates of cards increase, the amount of manual back-face culling that is performed has steadily decreased. However, the information is useful for custom 3D engines that don't plan on using the facilities of direct hardware acceleration.

docsity.com

Intersection between a Line and a Plane This occurs at the point which satisfies both the line and the plane equations. Line equation: p = org + u * dir (1) Plane equation: p * normal - k = 0. (2) Substituting (1) into (2) and rearranging we get: (org + u * dir) * normal - k = 0 ie u * dir * normal = k - org * normal ie u = (k - org * normal) / (dir * normal) If (d * normal) = 0 then the line runs parrallel to the plane and no intersection occurs. The exact point at which intersection does occur can be found by plugging u back into the line equation in (1).

Clipping Lines One thing that you'll need is the ability to take two points ( a and b ) that are on different sides of a plane defining a line segment, and find the point making the intersection of the line with the plane. This is easy enough to do. Think of this parametrically. Point a can be thought of as the point at time 0 and point b as the point at time 1, and the point of intersection you want to find is somewhere between those two. Take the dot product of a and b. Using them and the inverse of the plane's d parameter, you can find the scale value (which is a value between 0 and 1 that defines the parametric location of the particle when it intersects the plane). Armed with that, you just use the scale value, plugging it into the linear parametric equation to find the intersection location. Figure 5.17 shows this happening visually, and Listing 5.20 has the code.

Figure 5.17: Finding the intersection of a plane and a line Listing 5.20: plane3::Split

inline const point3 plane3::Split( const point3 &a, const point3 &b ) const { float aDot = (a * n); float bDot = (b * n);

float scale = ( -d - aDot) / ( bDot - aDot ); return a + (scale * (b - a)); }

docsity.com