Vectors & MatricesCSE167: Computer GraphicsInstructor: Steve RotenbergUCSD, Fall 2006Project 1 Make a program that renders a simple 3D object (like a cube). It should render several copies of the same object with different positions/rotations. Create a ‘Model’ class that stores an array of triangles and has a ‘Draw()’ function. The Model should have a ‘CreateBox(float,float,float)’ function that initializes it to a box. You can also make other shapes if you’d like. Use an object oriented approach that will allow you to re-use the Model for other projects and add new features easily as the course goes on The goal of project 1 is to get familiar with the C++ compiler and OpenGL (or Java, Direct3D…) Due Thursday, October 5, 5:00 pm More details will be on the web pageProject 1class Vertex {Vector3 Position;Vector3 Color;public:void Draw();};class Triangle {Vertex Vert[3];public:void Draw();};class Model {int NumTris;Triangle *Tri;void Init(int num) {delete Tri; Tri=new Triangle[num]; NumTris=num;}public:Model() {NumTris=0; Tri=0;}~Model() {delete Tri;}void CreateBox(float x,float y,float z);void CreateTeapot();void Draw();};Software Architecture Object oriented Make objects for things that should be objects Avoid global data & functions Encapsulate information Provide useful interfaces Put different objects in different files Keep lower level classes as generic as possibleVectorsCoordinate Systemsxyz Right handed coordinate systemVector Arithmetic[][][][][][]zyxzyxzzyyxxzzyyxxzyxzyxsasasasaaabababababababbbaaa=−−−=−−−−=−+++=+==aabababaVector AlgebraAssociativityCommutativityZero identityAdditive inverseDistributivityDistributivityMultiplicative identity()()()()()aababaaaa0aaaa0abbacbacba=+=++=+=−+=++=+++=++1ssststsVector Magnitude The magnitude (length) of a vector is: A vector with length=1.0 is called a unit vector We can also normalize a vector to make it a unit vector:222zyxvvv ++=vvvVector Magnitude PropertiesTriangle inequalitybabaaa+≤+= ssDot Productθcosbabababa=⋅++=⋅=⋅∑zzyyxxiibabababaDot ProductbabababababaTzzyyxxiibabababa=⋅=⋅++=⋅=⋅∑θcos[]⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=⋅zyxzyxbbbaaa baDot Product PropertiesCommutativityCauchy-Schwartz inequality()() ( )babaabbababacabacba≤⋅⋅=⋅⋅=⋅⋅+⋅=⋅+ssExample: Angle Between Vectors How do you find the angle θ between vectors a and b?abθExample: Angle Between Vectors⎟⎟⎠⎞⎜⎜⎝⎛⋅=⎟⎟⎠⎞⎜⎜⎝⎛⋅==⋅−babababababa1coscoscosθθθabθDot Products with General Vectors The dot product is a scalar value that tells us something about the relationship between two vectors If a·b > 0 then θ < 90º If a·b < 0 then θ > 90º If a·b = 0 then θ = 90º (or one or more of the vectors is degenerate (0,0,0))Dot Products with One Unit Vectoraua·u If |u|=1.0 then a·u is the length of the projectionof a onto uExample: Distance to Plane A plane is described by a point p on the plane and a unit normal n. Find the distance from point x to the plane•pn•xExample: Distance to Plane The distance is the length of the projection of x-p onto n:•pn•xx-p()npx⋅−=distDot Products with Unit Vectorsbθaa·b = 00 < a·b < 1a·b = -1a·b = 1-1 < a·b < 0()θcos0.1=⋅==babaa·bCross Product[]xyyxzxxzyzzyzyxzyxbababababababbbaaakji−−−=×=×babaProperties of the Cross Product0sin=×=×=××bababababaθarea of parallelogram abis a vector perpendicular to both a and b, in the direction defined by the right hand ruleif a and b are parallelExample: Normal of a Triangle Find the unit length normal of the triangle defined by 3D points a, b, and cabcExample: Normal of a Triangle()()∗∗∗=−×−=nnnacabnb-ac-aabcExample: Area of a Triangle Find the area of the triangle defined by 3D points a, b, and cabcExample: Area of a Triangle()()acab −×−=21areab-ac-aabcExample: Alignment to Target An object is at position p with a unit length heading of h. We want to rotate it so that the heading is facing some target t. Find a unit axis a and an angle θ to rotate around.••phtExample: Alignment to Target••phtt-pθa()()()()⎟⎟⎠⎞⎜⎜⎝⎛−−⋅=−×−×=−ptpthpthptha1cosθVector Classclass Vector3 {public:Vector3() {x=0.0f; y=0.0f; z=0.0f;}Vector3(float x0,float y0,float z0) {x=x0; y=y0; z=z0;}void Set(float x0,float y0,float z0) {x=x0; y=y0; z=z0;}void Add(Vector3 &a) {x+=a.x; y+=a.y; z+=a.z;}void Add(Vector3 &a,Vector3 &b) {x=a.x+b.x; y=a.y+b.y; z=a.z+b.z;}void Subtract(Vector3 &a) {x-=a.x; y-=a.y; z-=a.z;}void Subtract(Vector3 &a,Vector3 &b) {x=a.x-b.x; y=a.y-b.y; z=a.z-b.z;}void Negate() {x=-x; y=-y; z=-z;}void Negate(Vector3 &a) {x=-a.x; y=-a.y; z=-a.z;}void Scale(float s) {x*=s; y*=s; z*=s;}void Scale(float s,Vector3 &a) {x=s*a.x; y=s*a.y; z=s*a.z;}float Dot(Vector3 &a) {return x*a.x+y*a.y+z*a.z;}void Cross(Vector3 &a,Vector3 &b){x=a.y*b.z-a.z*b.y; y=a.z*b.x-a.x*b.z; z=a.x*b.y-a.y*b.x;}float Magnitude() {return sqrtf(x*x+y*y+z*z);}void Normalize() {Scale(1.0f/Magnitude());}float x,y,z;};Matrices & Transformations3D Models Let’s say we have a 3D model that has an array of position vectors describing its shape We will group all of the position vectors used to store the data in the model into a single array: vnwhere 0 ≤ n ≤ NumVerts-1 Each vector vnhas components vnxvnyvnzTranslation Let’s say that we want to move our 3D model from it’s current location to somewhere else… In technical jargon, we call this a translation We want to compute a new array of positions v′nrepresenting the new location Let’s say that vector d represents the relative offset that we want to move our object by We can simply use: v′n= vn+ dto get the new array of positionsTransformationsv′n= vn+ d This translation represents a very simple example of an object transformation The result is that the entire object gets moved or translated by d From now on, we will drop the nsubscript, and just writev′ = v + dremembering that in practice, this is actually a loop over several different vnvectors applying the same vector devery timeTransformations Always remember that this compact equation can be expanded out into Or into a system of linear equations:dvv
View Full Document