Computer Graphics Final (short)

Ace your homework & exams now with Quizwiz!

Dot Product

a=(a1,a2,a3),b=(b1,b2,b3) • a·b=a1b1 +a2b2 +a3b3 • a·b = |a| |b| cos(theta) • So, cos(theta) = (a·b) / (|a||b|) • Dot product is a scalar number not a vector. • If they are unit vectors, the dot product will give you the cosine value of the angle. • For two unit vectors, if the dot product is 0, what is the relationship of the two vectors in space? What if the dot product is 1? • Dot product being 0 means the two vectors are perpendicular. • Dot product being 1 means the two vectors are parallel.

Projection Fundamentals

• Goal: projecting a 3D point to a 2D point on a plane • For example: • Point P = (Px, Py, Pz), defined in the camera's CS, projects onto the near plane of the camera at P' = (P'x, P'y). We know the distance between the origin of the near plane and the eye position, denoted as N. The x and z coordinates of P denote as Px and Pz. Note: they are the coordinates in the camera CS. Use similar triangles, we have: P'x/Px = N/-Pz P'y/Py = N/-Pz So, P' = (N*(Px/-Pz), N*(Py/-Pz)) • For a farther point, -Pz is larger, which reduces the values of P'x and P'y. This make a farther object appears smaller than a nearer object. • If P lies in front of the near plane, we remove it by Clipping. • Also, a far plane is introduced. If P lies behind the far plane, we remove it by Clipping. • The value of N can be used to scale the picture. • A straight line is still straight after the projection.

3D Scale

• glScalef(float x, float y, float z) // produce a scaling along x, y and z axes

3D Translation

• glTranslatef (float x, float y, float z) // produce a translation by x, y and z

Retained Mode Graphics - Display List

• glVertex...() functions etc. that specify vertices are stored in a display list - in "compiled" form. • Display list is on the GPU as long as the system doesn't quit. Thus, recreating an image (e.g., redraw) is more efficient than Immediate Mode. Note: Immediate and Retained modes assume vertex and texture data are already specified in GL codes (e.g., glVertex...()), rather than by the data files. • If a new list has the same id as an existing list, the old list is replaced with the new one. No error occurs. • Not all OpenGL routines can be stored in display lists. If there is an attempt to store a non-supported routine, it is executed in immediate mode. No error occurs. • Display lists can call other display lists. • Display lists are not editable, but you can "fake" it. For example, a list A calls other lists B, C, and D, then delete/replace B, C, and D. Creating a display list GLuint id; void init( void ) { id = glGenLists( 1 ); glNewList( id, GL_COMPILE ); /* other OpenGL routines */ glEndList(); } Call a created list void display( void ) { glCallList( id ); } void display( void ) { glCallList( wheel_id ); gltranslatef (......); glCallList( wheel_id ); gltranslatef (......); glCallList( wheel_id ); } Again, a display list is a cache of commands, which means once the display list is created, it cannot be modified. What does Cache Mean? glRotatef(35.0, 1.0, 0.0, 0.0): This glRotatef(35.0, 1.0, 0.0, 0.0) command (which is already on the GPU) can only produce this matrix; thus, the matrix is on the GPU cannot be modified.

Vector

• represents a direction and magnitude in 2D/3D space. • V (x, y, z) You can only rotate and scale a vector. A vector defines a direction and magnitude, it does not have a position in space. So translation does not take effects on a vector. Length (Magnitude) of a vector V (x, y, z, 0) |V|= x2+y2+z2 • A unit vector: • A vector can be normalized so that it retains its direction, its length is scaled to be 1. V = V/|V| = ( x/|V| , y/|V| , z/|V|, 0)

Gift-Warping Algorithm for Convex Hull in 2D

1. Start at lowest point (this is necessarily on the convex hull) 2. Rotate the line until we hit another point (ditto) • All other points will lie on one side of this line • Look for the point that gives you the largest angle with the current line 3. Repeat 4. You're done when you get back to the starting point

Consistently Oriented Triangulation

A Triangulation is said to be consistently oriented if any two adjacent triangles order the shared edge oppositely; otherwise, the triangulation is inconsistently oriented.

2D Scaling

Change the size of an object. - Change the size of the object in width and height with respect to the origin. - sx and sy is the amount of scaling for each vertex along x and y axis. sx =W ' / W ; sy = H '/ H ; Matrix/Vector representation of the scaling: [ x' ] = [ sx 0 ] [ x ] = [ sxX ] [ y' ] [ 0 sy ] [ y ] [ syY ]

glViewport()

Defining a raster window: Assuming a 600x600 raster window, so we need to define glViewport (0, 0, 600, 600). The origin of the window is at the bottom-left corner, the width of the window is 600, and the height of the window is 600. When displaying the content in the canvas, four corners of the canvas will map to the four corners defined in glViewport(). The rasterized image is shown in the figure on the right. The blue dot is the origin of the canvas.

Immediate Mode Graphics

Directly execute this code pieces will trigger Immediate mode of drawing: glBegin(( GL_XXXX ); glVertex ...; ... ... glEnd(); glVertex...() functions etc. that specify vertices are sent to pipeline and display right away. Vertices flow through the system and produce images; then, these data are lost in the system. New images are created by re-executing the functions and regenerating the geometry primitives.

Triangulation Properties

Given a triangulated polygon P with nv vertices, ne edges and nf faces: nv - ne+ nf = 1 (Euler Characteristic) 2ne - nv = 3nf (D.S.) Conditions for these two properties: Any two triangles share only one edge, if at all. Two edges meet only in one common end-point (vertex), if at all. Thus, triangulation of polygon P gives exactly nv-2 faces. Can you verify the three equations using this triangulated polygon in the attached image?

2D Translation

Move an object from one position to another. x' =x+tx y' =y+ty • Define an origin as reference for translation. • Translate the object by translating its vertices. • Each vertex is translated along X and Y directions. • A vertex P=(x,y)=(3,4). • After moving the vertex 3 units to the right and 1 unit up, what are the new coordinates of the vertex? • Answer: Q = (x', y') = (6, 5). • How do we get this answer? • x'=x+3; • y'=y+1; • Matrix/Vector representation of the translation: Q=P+t =[ x ] + [tx] = [ x + tx ] [ y ] [ty] [ y + ty ]

2D Rotation

Rotate an object about an axis.

Triangulation Process by OpenGL

See attached image first. When using OpenGL to render a polygon, the rendering is unpredictable. Thus, we need to ensure: (1) the polygon is convex, or (2) we set the start vertex that can produce a valid triangulation. Suggestion: avoid creating polygons, create triangles instead! Newer versions of OpenGL have discarded polygon altogether (like v4.3). Let's see an example using our Proj.1 - the 2D drawing tool. Triangulation using GL_TRIANGLE_STRIP: glBegin(GL_TRIANGLE_STRIP); glVertex3f( 0.0f, 2.0f, 0.0f ); glVertex3f( 0.0f, 0.0f, 0.0f ); glVertex3f( 1.0f, 2.0f, 0.0f ); glVertex3f( 1.5f, 0.0f, 0.0f ); glEnd();

Graphics pipeline

Vertices & Polygons --> Modelling & Transformation --> Camera/View Transformation --> Projection Transformation --> Rasterizer --> Display

Define the Camera Space

We represent the camera using a coordinate system, where the position of eye is the origin, and three vectors, n, u, v, define the axes. In general, the camera is oriented down the direction -n. The direction u points off the right of the camera. The direction v points upward. • Position of the camera is easy to describe, but orientation is a bit difficult. • Orientations of the camera can be explained using flying terms. Pitch: rotate about the u-axis Roll: rotate about the n-axis Yaw: rotate about the v-axis • Now, how to determine the vectors n, u and v? • n=eye-lookAt • Then, we determine an up vector to calculate u and v. By default, we say up is parallel to the Y-axis. • So, u should be perpendicular to the plane of n and up. X • u = up x n Z • Then, v must be perpendicular to the plane of n and u. • v=nxu • Finally, we normalize n, u and v.

gluOrtho2D()

defines the canvas. Mathematically, it defines a 2D orthographic projection matrix. The size of the canvas indicates the range of values that you can use to specify coordinates. Any portion of an object that is out of the range cannot be displayed in the raster window. For example, we define a canvas with size 10x10, using gluOrtho2D(0, 0, 10, 10), so the origin of the canvas is defined at the bottom-left corner. And we define a triangle with three vertices at (0.0, 0.0), (5.0, 0.0), (2.5, 3.0).

Using Motion Callback to Draw the Cursor

void drawCursor() { glColor3f(1.0f, 0.0f, 1.0f); glBegin(GL_POLYGON); glVertex2f(mousePos[0]+5.0f, mousePos[1]+5.0f); glVertex2f(mousePos[0]-5.0f, mousePos[1]+5.0f); glVertex2f(mousePos[0]-5.0f, mousePos[1]-5.0f); glVertex2f(mousePos[0]+5.0f, mousePos[1]-5.0f); glEnd(); } void motion(int x, int y) { mousePos[0] = x; mousePos[1] = height-y; glutPostRedisplay(); }

Matrix in OpenGL

• A 4x4 matrix in OpenGL is stored as a 1D array in OpenGL. • OpenGL uses the column-major matrix order. • Column-major order traverses the matrix by columns, then enumerates rows within the columns. m0 m4 m8 m12 m1 m5 m9 m13 m2 m6 m10 m14 m3 m7 m11 m15 • Conversion between column-major and row-major. M column _ major =MT row _ major M row _ major = MT column _ major The first four elements in the array are the first column of the matrix. Transpose of a column- major matrix is its row- major representation.

What are Callback Functions?

• A callback function is a function passed as an argument to other codes of the program, and it can be called at a later time. • Callback functions usually need to be registered to the code prior to runtime. • Callback functions are commonly used for responding to events, e.g., mouse and keyboard events. • GLUT is an eventing/callback architecture. • When an event happens somewhere in the program, the corresponding callback functions will be executed. • You just need to define the operations that should be executed when the event occurs.

What is a Camera?

• A camera is an object to view scenes. • The way that a camera works is similar to our eyes. • Objects are visible only if they are in the front of the camera. • A camera is developed using mathematical tools to handle perspective projections. View Volume of Camera: • In human vision perceptions, objects are visible only when they appear in the area that can be captured by eyes. • E.g., objects behind/above the eye cannot be seen. • In computer graphics, we simulate the system of human vision perceptions by defining a view volume. • The shape of the view volume is a pyramid, whose apex is at the eye position of the camera.

Matrix Multiplication <=> Transforming Coordinate System

• A matrix actually defines a Coordinate System (CS). • A matrix multiplication will transform one CS to another. • Let's review the previous example: P' = T(1,1)R(−90)T(−1,−1)P • Initially, we assume the default matrix defines the world space, which is an identity matrix. • We then obtain CS1 by applying T(1,1) (means to translate the identity matrix by T(1,1) to obtain CS1). • We then obtain CS2 by rotating CS1 by R(-90) degrees. • We then obtain CS3 by translating CS2 by T(-1,-1). • Finally, the vertex P is drawn in CS3. Rule: If CS1 is transformed by matrix M to create CS2, the coordinates of the vertex (P') in CS1 can be represented as P' = MP.

Color Channels

• A pixel can have varied color channels • 1 channel -gray scale / black and white • 3 channels - RGB • 4 channels - RGBA

Equations of Planes

• A plane, in the vector form, is specified by a based point P and its normal vector n. • For an arbitrary point on the plane, the direction vector from the based point to this arbitrary point must be perpendicular to the normal vector.

homogeneous points and vectors

• A point in 3D space can be represented with a homogenous 4D point. • Homogenous Points • Add 1D, but constrain that to be 1. • P(x,y,z,1) • Homogenous Vectors • • • Add 1D, but constrain that to be 0. V(x,y,z,0) The 0 will make sure that the translation column of the matrix is discarded when doing matrix-vector multiplication ***Remember, you must not translate vectors. • Homogenous Vectors • Add 1D, but constrain it to be 0. • V(x,y,z,0) • The 0 will make sure that the translation column of the matrix is discarded when doing matrix-vector multiplication. Points are not equivalent to vectors! • Although points and vectors have the same form of containing three elements, they represent different things, and serve for different graphical purposes. • Point - Point = Vector; • Vector + Vector = Vector; • Point + Vector = Point; (the point is translated by the vector) • Point + Point = ? (does not make sense!)

Convexity

• A set may not be convex. How to tell? • A non-empty set s of points in R2 is said to be convex if, for any two points , it is true that the line segment PQ ⊂ S. P,Q ∈ S • In computer graphics, a polygon, or, more generally, an area or region is convex if whenever two points are within the region, and the connecting line between these two points lies completely inside the region as well.

How to determine which side of a triangle to render?

• A triangle has two sides. • One side is front; the other side is back. • OpenGL must determine for each triangle if the viewer currently sees one side or the other. • Why does OpenGL need to distinguish sides? • Front and back may have different color. • OpenGL should be able to display accordingly.

Perspective Transformation

• Actually, projection matrix(v.1) + perspective division alter a 3D point into another 3D point. • In other words, we can think of it as warping the shape of camera space (or the view volume) into another shape. • Our interest is how it transforms the camera's view volume.

Clipping (in its own slides)

• After applying the projection matrix to geometric primitives, clipping process is applied to determine the visible part of a primitive within the view frustum. • The view frustum becomes an axis-aligned 3D viewing box. • After the projection, the range of coordinates on axis n, u, v is [-1,1]. • Recall what we learned from Projection matrix: • The view frustum defined by a camera is not a box. • Clipping line segments to this view frustum is not easy. • The view frustum is "straightened" into a box by the projection transformation with either glFrustum()or gluPerspective(). • So after the projection, we only need to clip line segments to a box.

Transform Object V.S. Transform Coordinate System

• All transformations we just learned are about transforming vertices in a fixed 2D coordinate system. • The order of transformation (matrix multiplications) for the vertex is right to left. For example: P'''=TA * Rθ * TA * P • Another way to think of transforming is to transform the coordinate system. • The order of transformation (matrix multiplications) for the local coordinate system is left to right.

Using Global Variables

• As you can see, the interface of callback functions are fixed. • You cannot pass custom parameters to callbacks. • You must use global variables to pass information to callbacks.

Camera Space (A specific type of local space)

• Before rendering objects, they are first transformed into the camera space. • The origin of the camera space is placed at the eye position with three axes n, u, and v.

Display List vs. Vertex Array

• Both are to enhance performance. • In some systems, Vertex Array may provide better performance than Display List. • Reduced function call overhead • Data format for better Memory access • In distributed system or multi-context applications (e.g., multiple windows), Display List may be better. • Display lists can be shared between multiple GL contexts • reduce memory usage in multi-context applications

Clipping

• Clipping is performed after vertices pass through the projection matrix. • Clipping algorithm is easy to perform. • After the transformation of perspective division, the space is warped into an axis-aligned box space. • The clipping algorithm is to perform a line-box intersection. • For example, we have a line segment AB given in homogeneous coordinates. • A=(Ax,Ay,Az,Aw) • B=(Bx,By,Bz,Bw) Note: before performing the perspective division, Aw and Bw are not equal to 1. • We can represent the line as A+(B-A)t • If t=0, it lies at A. • If t=1, it lies at B. • Now, we need to find out if the line segment intersects with the planes of the box space. • For example, take the plane x = -1, point A lies to the right B (on the inside) of this plane if Ax >−1 ⇒ Ax+Aw>0 Aw • For other planes, we can do similar tests.

Shading

• Considering the interaction between light and the material of surfaces. • Light-material interactions cause each point on the surfaces to have a different color or shade. • To calculate shades, we need: • Light sources (e.g., the position of a light source, color of the light, light intensity, etc.) • Transformation • Material properties of the surface • Location of Viewer Physical Process: • How light is reflected from a surface? • Light strikes A • Some scattered • Some absorbed • Light strikes B • Some scattered • Some absorbed • Some of scattered light from A strikes B • Some scattered • Some absorbed • Some of this scattered light strikes A ... and so on Challenging: • The infinite scattering and absorption of light can be described by the rendering equation, but cannot be solved in general. • Rendering equation is global and includes: • Shadows • Multiple scattering from object to object

Display Callback

• Display callback is executed when GLUT determines the window needs to be refreshed. • The window is first opened. • The program has been instructed to redraw the window. • Every GLUT program must have a display callback. • Many events may invoke the display callback function. • E.g., call the display function in reshape, idle, mouse callbacks, etc. • However, this will lead to multiple executions of the display function on a single pass of the main loop.

Gray Scale Image

• Each pixel uses a single color channel to represent intensity. • Measure amount of light of that pixel. • 0 = absent. • 1 = maximum intensity. • However, real numbers take up a lot of memory space • We use compact representation for storage [0, 255]. • As good as human eye can distinguish. Note: [0-255] is used to display color on the monitor. Real numbers in [0.0, 1.0] are needed by graphics algorithms. Conversion: Intensity = bits/255.0 Bits = floor (intensity*255)

GLUT Callbacks

• GLUT recognizes a subset of events of window systems(e.g., Windows, X-Window on Unix-like system, Macintosh) • GLUT callback registration: • void glutDisplayFunc(void (*func)(void)); • void glutReshapeFunc(void (*func)(int width, int height)); • The width and height parameters specify the new size of the window in pixels. • void glutKeyboardFunc(void (*func)(unsigned char key, int x, int y)); • The key parameter is the generated ASCII character. (each key press generates an ASCII character.) • The x and y parameters are the mouse location in the space defined by the window system when the key is pressed. (Note: glviewport() set the origin at the bottom- left, but the window system sets the origin at the top-left. ) • GLUT callback registration: • void glutMouseFunc(void (*func)(int button, int state, int x, int y)); • The button parameter tells which mouse button is active. It must be one of the GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, or GLUT_RIGHT_BUTTON. • The state parameters tells whether the button is a release or press. It must be either GLUT_UP or GLUT_DOWN, respectively. • The x and y parameters are the mouse location in the space defined by the window system when the button is activated. • void glutMotionFunc(void (*func)(int x, int y)); • It is called when the mouse moves within the window while one or more mouse buttons are pressed. • void glutPassiveMotionFunc(void (*func)(int x, int y)); • It is called when the mouse moves within the window while no mouse button is pressed. • GLUT callback registration: • void glutIdleFunc(void (*func)(void)); • It is usually used to perform background tasks, or continuous animations when no window event is received. • For a game application development, idle callback is the place to implement the game logics. • Be careful with the amount of the computations done in the idle callback. The many computations may affect the program's performance.

GLUT Menu - A Simple Menu

• GLUT support pop-up menus • GLUT menu can have submenus • Three steps to create a menu: • Add entries or submenus to the menu • Define action for each menu item • Action will be performed if the menu item is selected • Attach the menu to a mouse button • The menu can be attached to either left, right or middle button int m_id = glutCreateMenu(menu); glutAddMenuEntry("Clear", 0); glutAddMenuEntry("Exit", 1); glutAttachMenu(GLUT_RIGHT_BUTTON); -The names of entries that appear when the menu is popped-up. ("Clear" and "Exit" used above). -The identifier used for creating menu actions ("0" and "1" used above by gludAddMenuEntry) Define a myMenu() function to perform the menu actions. void myMenu(int value) { } switch(value) { case 0: // clear // codes for "Clear" menu item // ... ... ... break; case 1: // exit // codes for "Exit" menu item exit(0); break; }

Ray-Plane Intersection

• Given vector notations of rays and planes, it is very easy to compute the ray-plane intersection point. • Let the ray be A+td. • Let the plane be defined with the base point B and normal vector n. • The intersection point can be computed by finding the value of t from: (A + td - B) n = 0

Convex Combination on Line Segment

• Here is the formal statement: If P and Q are two points, then a point V lies on the segment PQ if and only if it can be expressed as V = c1 × P + c2 × Q where 0 ≤ci≤1 for both i=1 and i=2, and where c1+c2=1. Then, V is said to be a convex combination (or barycentric combination) of P and Q. c1 and c2 are called barycentric coordinates of V. Note: the point V on the segment PQ can be usefully thought of as a weighted sum of P and Q, where c1 and c2 are the weights.

Convex Combination on Triangle

• Here is the formal statement: If P, Q and R are three points in R3, then a point V lies on the triangle PQR if and only if it can be expressed as V = c1 × P + c2 × Q + c3 × R where 0 ≤ci≤1 for both i=1, i=2 and i=3, and where c1+c2+c3=1. Then, V is said to be a convex combination (or barycentric combination) of P, Q and R. c1, c2 and c3 are called the barycentric coordinates of V. Practice: where is V when (a) one of the ci's is 1 and the other two are 0? --> V is at one of the P, Q, R. (b) one of the ci's is 0 and the other two equal to 1⁄2? --> V is at the midpoint one of three line segments. (c) all the ci's are equal to 1/3? -->V is at the center of the triangle. Notes on Color interpolations for Rendering • The screen space is not actually a 2D continuum; instead, it is a rectangular array - raster. • Each pixel on the raster window is not a point but a square of non-zero size. • OpenGL must pick a reprehensive location inside the pixel for color interpolation. • And then, OpenGL sets the entire pixel RGB with this color value. • The valid choice of the reprehensive location is the pixel's center. • Boundary pixels are more complicated to handle since both foreground and background colors need to be taken into account. • Blending weights are determined by the area of the pixel occupied by the primitive and the background. • The desired level of antialiasing is also taken into account.

Convex combinations for Arbitrary Numbers

• If F ={P1,P2,...,Pk} is a set of k points in R2, then a point V of the form V=c1P1+c2P2+...+ckPk where 0≤ci≤1 for 1≤i≤k, and for all i=1 to k, ∑ ci =1 is said to be a convex combination of F.

Cross Product

• If we have two vectors a and b: • a=(a1,a2,a3) • b=(b1,b2,b3) • Then, the cross product of a and b is the vector: • axb=(a2b3-a3b2,a3b1-a1b3,a1b2-a2b1) • The result of cross product is not a scalar but a vector, which is perpendicular to the plane of the two vectors. • Cross product of a vector with itself is null. • The magnitude of the cross product: • |axb|=|a||b|sin(theta) axb = i(a2b3 −a3b2)− j(a1b3 −a3b1)+k(a1b2 −a2b1) • Let's see an example: • a=(5,1,4),b=(-1,0,2) • axb=(2,-14,1)

Equation of a Sphere

• In 2D, we know that a circle is defined as: x2+y2=r2 • For a sphere, you need Pythagoras' theorem twice: • Let's say P (x, y, z) is on the sphere with center O and radius r, which means the distance from O to P is r. • The right triangle OAB results in x2+y2=s2. • The right triangle OBP results in s2+z2=r2. • Hence, the distance |OP| satisfies: • x2+y2+z2=r2=|OP|2 • In general, the sphere with the center (i, j, k) and radius r is defined as: (x-i)2 +(y-j)2 +(z-k)2 =r2

Positioning a Camera and Its Viewing Direction

• In 3D space, a camera, same as other objects, is set at a point. We call this point as Eye. • To know the direction that the camera faces to, we define a look point, called LookAt. • Now, the viewing direction is determined by the vector n = eye-lookAt. • Usually, the vector n is normalized to be a unit vector. • Note that the direction of n points away from the lookAt, which is the way used by Graphics APIs.

OpenGL Matrix Mode

• In OpenGL, the matrices are classified in two modes. • ModelView Mode • Projection Mode • ModelView matrix stores the product of Model matrix and View matrix. • Model matrix: the product of transformation matrices, such as translation, rotation, and scaling. • View matrix: the matrix defined by camera, which is used to convert the coordinates of an object in a CS to the camera's CS. • Projection matrix is used to project 3D objects to the screen (3D to 2D conversion). Other Matrix Functions in OpenGL • Load Identity Matrix: • glLoadIdentity(); // replace current matrix with identity matrix • Matrix Multiplication: • glMultMatrixf(const GLfloat *m); // multiple current matrix with the specific matrix • Load Arbitrary Matrix: • glLoadMatrixf(const GLfloat *m); // replace current matrix with the specific matrix • Push Matrix : • glPushMatrix(); // push the current matrix into the stack • Pop Matrix: • glPopMatrix(); // pop the current matrix from the stack

ModelView Matrix in OpenGL

• In OpenGL, the view matrix is stored in the ModelView Matrix. • The model and view matrices are two separate matrices. • Model matrix represents the transformation of an object. (Local spaceàworld space) • View matrix is used to convert the coordinates of the object from the world space to the camera space. • In the OpenGL graphics pipeline, the product of model and view matrices is maintained in ModelView matrix stack. • They are composed to represent how an object is placed in the camera space. • Model and view matrices are applied to a point as PV = MV*MM*PW.

glFrustrum( ) and gluPerspective( )

• In OpenGL, there are two function for specifying projection matrix: • glFrustrum(left, right, bottom, top, N, F) • gluPerspective(viewAngle, aspect, N, F) • glFrustrum() • left left, right, bottom, and top, specify the size of the virtual image plane - that's the "window" you look through from the camera's position that is mapped to fill the viewport. • near (N) defines how far the image plane is from the camera's position. • Far (F) defines the maximum render distance. • Anything you can do with gluPerspective() you can do with glFrustrum(), but not vice versa.

Why Transform Coordinate System?

• In OpenGL, two concepts to draw a transformed object: • We can transform the object first, then draw. • Or, we can draw the original object in a transformed coordinate system. (Usually, this is preferred.) • OpenGL first executes all matrix multiplications, then draw the object only once in the display callback function. • We only deal with matrices / transformed coordinate systems. • Let's see a simple OpenGL example!

4x4 Matrix for 3D Transformation

• In a 2D coordinate system, we use 3x3 matrices for translation, rotation, and scaling. • In a 3D coordinate system, we need 4x4 matrices for those transformations. • Right- and Left-handed 3D coordinate systems: Right-handed system (default in OpenGL, XNA, Maya): The positive x and y axes point right and up. The positive z axis points out of the screen. Positive rotation is counterclockwise about the rotation axis. Left-handed system (default in 3D Max, Unity and Blender): The positive x and y axes point right and up. The positive z axis points into the screen. Positive rotation is clockwise about the rotation axis.

Post Redisplay

• In a program, if you decide to redraw the window at some point, you use glutPostRedisplay(). • The post redisplay command sets a flag, instead of actually executing the display function. • Then, at the end of the main loop, GLUT checks whether the flag is set. • If it is set, the display function is executed, and only once.

View Volume of Camera

• In human vision perceptions, objects are visible only when they appear in the area that can be captured by eyes. • E.g., objects behind/above the eye cannot be seen. • In computer graphics, we simulate the system of human vision perceptions by defining a view volume. • The shape of the view volume is a pyramid, whose apex is at the eye position of the camera.

World-to-Camera Conversion

• In order to know the coordinates of an object in the camera space, we convert its world coordinates into the camera space. • We call the matrix of world-to-camera transformation as View matrix MV. • Similar to general world-to-local transformation, if the coordinates of a point are known in the world space, named PW, its coordinates in the camera space is calculated by MVPW. • Let's see how can we form the matrix MV. • We have already known the position of the camera, named eye. • We have also known the orientation of the camera, defined by three vectors n, u, v. • There are several ways to derive what MV must be, but the following matrix is the easiest way: | ux uy uz dx | dx =−eye⋅u MV = | vx vy vz dy | dy =−eye⋅v | nx ny nz dz | dz =−eye⋅n | 0 0 0 1 | Note: technically, it's illegal to dot a point and a vector. So, the eye here should be the vector of eye - [0 0 0 1]T.

Clearing the Window

• In the display function, the codes usually start by clearing the window. • void glClear(GLbitfield mask) • The mask indicates the buffers you want to clear. Usually they are GL_COLOR_BIFFER_BIT and GL_DEPTH_BUFFER_BIT. • Why? • Rasterizing data into the frame buffer within the display function is decoupled from the display of its contents. • Dual ported memory So, the contents for displaying must be cleared before rasterizing data into the frame buffer.

Vanishing Point in Projection

• Let's examine how parallel lines project. • If two lines are parallel in 3D, the 2D lines they project to are not necessary parallel. Instead, those 2D lines meet at a vanishing point. • Suppose a line in 3D (in camera's CS) passes through point A = (Ax, Ay, Az) and the direction is a vector d = (dx, dy, dz). • So the line can be represented as → A+dt • We have the project line as: see attached image. • Now, what if parallel lines in 3D are parallel to the near plane? • Parallel to the near plane means dz = 0. • Thus, those lines project to parallel lines. Draw a line passing through the eye position and parallel to AB, the vanishing point is at the intersection between the line and the near plane. Would use the A(infinity) equation in attached image.

Vertex Array

• No matter using Immediate Mode or Retained Mode, data (e.g., vertices, colors) are generated one call at a time. • Vertex Array is a faster drawing method. • Allow vertices and their attributes to be specified in chunks. • Another Advantages: • Remove redundant processing of vertices that are shared between adjacent polygons. Vertex Array method stores unique vertices in a vertex array and indices (expressing how vertices connect each other) in a index array. Both arrays can be sent in chunk in one call. Basic Idea of Vertex Array Vertex array: v0 = .2.0 4.0 4.0 8.0 6.0 6.0 7.0 3.0 5.0 3.0 Index array: How-to: t0 = 0 1 2 0 2 4 4 2 3 - Active (enable) arrays. - Put data into the arrays. - Draw geometry with the data. Note: Arrays are created on CPU, they are sent to GPU when they are called to draw. Example: float vertices[] = {2.0f, 4.0f, 0.0f, 4.0f, 8.0f, 0.0f, 6.0f, 6.0f, 0.0f, 7.0f, 3.0f, 0.0f, 5.0f, 3.0f, 0.0f}; unsigned short indices[] = {0, 1, 2, 0, 2, 4, 4, 2, 3}; float colors [] = {1.0f, 0.0f, 0.0f, // per vertex color 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f}; // enble and specify pointers to vertex arrays // draw triangles using glDrawElements Display Callback: glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); glColorPointer(3, GL_FLOAT, 0, colors); glVertexPointer(3, GL_FLOAT, 0, vertices); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glDrawElements(GL_TRIANGLES, 9, GL_UNSIGNED_SHORT, indices); glDisableClientState(GL_VERTEX_ARRAY) glDisableClientState(GL_COLOR_ARRAY);

World-to-Local Conversion

• Now, if we know the coordinates of the point in the world space, named PW, what are the coordinates in the coordinate system A? • The PA = M(inverse)OA * PW

Projection Matrix V.1 and Perspective Division

• Now, it's time to find out the matrix of the projection! • For a point P = (Px, Py, Pz) in camera's CS, we want to know its projected coordinates and its pseudodepth, which is P' = (P'x, P'y, pseudodepth).

Mapping the transformed view volume into Canonical View Volume

• Now, we know that the pseudodepth values are mapped to the range (-1, 1) in n (or z) axis after the transformation of perspective division. • How about the ranges for u (or x) and v (or y) axes? • We need to map P'x and P'y to(-1,1)

Pixel

• Pixels are all we can see on a display device. • Pixel is a word invented from "Picture Element". • The basic programmable color. • Pixelsarearrangedinpreciserowsandcolumns. • The number of rows and columns refers to the resolution of an image. • For example, 800 x 600, 1024 x 768, 1152 x 864, these resolutions are sized at 4:3 ratio. • Most old monitors/TV are manufactured with 4:3 ratio. • Current TV standard ratio 16: 9, i.e., 1080p, 720p • Next-generation TV is 4K.

Vector Graphics

• Pixels are really good at on colors - this is what we need eventually. • Vectors are scalable, so an image is designed only once and can be resized to any size. • Vector graphics uses geometrical primitives, such as points, lines ,curves, and shapes (triangles, quads, polygons, etc.) • Based on math expression • Vector graphics is based on vectors - a path • Lead through one location to another • E.g., draw a line in 2D • A(x,y),B(x',y') • Color • Thickness

Pseudodepth

• Projection discards depth information. • We need a way that somehow preserves the depth information, otherwise we won't be able to remove hidden surfaces later. • The actual depth is the distance of a point P from the eye of the camera, which is sqrt(Px^2 + Py^2 + Pz^2) . But, this is slow in computation. • All we really need is the measure of distance that tells, when two points project to the same point on the near plane, which one is closer? P'=(P'x,P'y,P'z)=(N*(Px/-Pz), N*(Py/-Pz), ((a*Pz+b)/-Pz)) There are many choices for a and b. • We choose them so that the pseudodepth can vary between -1 and 1. (we will see why this is the good choice later.) • We decide the pseudodepth is -1 when -Pz = N. • We decide the pseudodepth is 1 when -Pz = F.

Equation of a circle

• Pythagoras theorem: a^2 + b^2 = c^2 • Given a circle through the origin with radius r, for any point on the circle we have • x*2+y^2=r^2 • If the center of the circle is not at the origin: P (0, 0) • (x-xc)^2 + (y-yc)^2 = r^2

RGB Image

• RGB Image is consistent to human vision system. • A pixel has 3 color channels: red, green and blue. • Human see various colors, but they are just a blend of RGB. • Intensity for three color channels: • Each channel has an intensity ranging in [0, 255]. • 3 bytes or 24 bits.

When should we use a raster or a vector?

• Raster images are usually used for photos. • Your phone or camera directly produce raster images for you. • You can use Photoshop to process. • Research area: computer vision, video processing, surveillance. • Vector images are usually used for CAD, gaming, etc. • Math expression/definitions creates vector images. • Cannot be displayed until you rasterize it. • Commonly used in 3D graphics, where you can rasterize a image/frame at any position and with any size. • Research area: scientific visualization, CG movies, 3D games, etc.

Find the position of vertex P

• Rotate a vertex P about the vertex A (3, 2) by 60 degrees. • Find the matrix M used to transform P. M =T(3,2)R(60)T(−3,−2) glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(3.0, 2.0, 0.0); glRotatef(60.0, 0.0, 0.0, 1.0); glTranslatef(-3.0, -2.0, 0.0);

Convex Combination

• The building blocks of OpenGL are vertices/points, line segments/edges and triangles. • Note: any polygonal shapes in OpenGL are converted to triangles before coloring them. • Thus, to describe a property in a polygon using the properties specified for the points, we only need to know the interpolation process for line segments and triangles.

GLUT Event Loop

• The last line in a OpenGL program is glutMainLoop(); • It puts the program in an infinite loop. • In each iteration of the loop, • GLUT looks for the events in the queue. • Then, GLUT executes the corresponding callback function of each event. • If the callback function for an event is not defined, GLUT will ignore this event.

Mouse Position returned by Window Systems

• The operating system returns the mouse coordinates with the screen origin at the top-left corner of the window. • But, as we introduced in previous classes, OpenGL's projection uses the 2D coordinates with the origin at the bottom-left corner. • So, in your program, you must invert the y-coordinate passed into callbacks by the height of the window. y' = height - y;

CW or CCW

• The orientation of the primitive (order of vertices) with respect to the viewer. • Clock wise (CW) or counter clock wise (CCW) • By default, the triangle's CCW orientation is presumed to be front-facing. • CW orientation is presumed to be back- facing. • glFrontFace(GL_CCW) or glFrontFace(GL_CW)

Equation of Rays

• The sum of a point and a vector leads to a ray. • P = P0 + td • P0 is an arbitrary point which is the starting position of the ray. • d is an arbitrary vector which is usually normalized. • t is a scalar determine the distance that P travels on the direction d from the position of P0. For example, P = [ 2 ] [ 0.577 ] [ 1 ] + [ 0.577 ] t [ 2 ] [ 0.577 ] [ 1 ] [ 0 ]

Brute Force Algorithm for Convex Hull in 2D

• There are many algorithms for finding the convex hull. • It is a part of research in geometric processing. • We just see a brute force algorithm: • Considering each ordered pair of points (P, Q). • Then determining whether all the remaining points of the set lie within half-plane to the right of the directed line from P to Q.

Mouse, Motion and Keyboard Callbacks

• These callbacks are used to build interactive applications. • Mouse: • void mouse(GLint button, GLint state, GLint x, GLint y) • glutMouseFunc(mouse) • Motion: • motion(int x, int y) • glutMotionFunc(motion), or • glutPassiveMotionFunc(motion) • Keyboard: • void keyboard(unsigned char key, int x, int y) • glutKeyboardFunc(mykey)

Local-to-World Conversion

• This is the way we use for transforming objects. • A matrix defines a coordinate system, we transform objects from one coordinate system to another by a series of matrix multiplications. • E.g., a point PA is placed in a coordinate system A, then the coordinates of this point in the world space O can be calculated by MOAPA.

Special and Modifier keys

• To access the keys, such as F1 - F12, left/right/up/down arrow keys, Page up/page down/Home keys, you need • void glutSpecialFunc(void (*func)(int key, int x, int y)); • Function key 1: GLUT_KEY_F1 • Up arrow key: GLUT_KEY_UP • E.g., if (key == GLUT_KEY_UP) ... • To check the modifiers, you need glutGetModifiers() • GLUT_ACTIVE_SHIFT • GLUT_ACTIVE_CTRL • CLUT_ACTIVE_ALT

Geometric concepts in 3D modeling

• To draw things using computer graphics technologies, we only specify: • vertices • edges/line segments (the order of vertices) • polygons (connected line segments), e.g., triangle primitives Then, we see colors spreading throughout the primitive's interior. How does the graphics engine know the colors of the interior? This is done by means of interpolation. The exact mechanics of the interpolation process is convex combination. If you have done a good job in an earlier math class, the knowledge of linear interpolation and convexity may be easy for you.

Window Size

• To invert y, we need the height of window • Whenever the window is resized, the reshape callback is called. • The widow size is usually updated in the Reshape callback function. • Other ways to get the current size of the window: • Using GLUT query functions: • glutGet(GLUT_WINDOW_WIDTH) • glutGet(GLUT_WINDOW_HEIGHT)

Idle Callback

• Useful for animation void idle() { g_moveDis += g_moveStep; glutPostRedisplay(); } void display(void) { glClearColor(1.0, 1.0, 1.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 0.0, 0.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(g_moveDis, 0.0f, 0.0f); glBegin(GL_QUADS); glVertex2f(quadCenter[0]-quadOffset,quadCenter[1]+quadOffset); glVertex2f(quadCenter[0]+quadOffset,quadCenter[1]+quadOffset); glVertex2f(quadCenter[0]+quadOffset,quadCenter[1]-quadOffset); glVertex2f(quadCenter[0]-quadOffset,quadCenter[1]-quadOffset); glEnd(); glutSwapBuffers(); }

Rasterization

• Vector graphics cannot be displayed. • To put it on the screen, a vector image (math definitions) must be converted to a raster image (pixels). • The conversion process is called rasterization. • We'll learn the rasterizaiton algorithms later. • Procedure of rasterization (in a 3D case): • Find an angle for projection. • Determine which primitives are able to be projected to the screen. • Calculate the colors. • Fill colors to pixels of the shape. • Display.

Double Buffering

• We can use two color buffers for drawing • Front buffer: it is used for displaying the content but not written to • Back buffer: new colors are written to it but not displayed. • To use two color buffers, you need glutInitDisplayMode(GL_RGB | GL_DOULBE) • At the end of the display callback, the buffers are swapped. Void display() { //specify what color to use for clearing the buffer glClearColor(1.0, 1.0, 1.0, 0.0); glClear(GL_COLOR_BUFFER_BIT ); glBegin(GL_QUADS); // vertex data goes here ... glEnd(); glutSwapBuffers(); }

World Space

• When constructing the 3D world, a world space is defined as a common reference for all the objects. • The origin of the world space acts as a common reference to unify the coordinates of all objects.

Local Space

• When creating an object, the points that make up this object are defined in the particular object's local space. • Usually, the origin of the local space is placed in a position convenient for the creation of the object. (e.g., at the center of the object or at the bottom of the object.)

Point

• gives us a position in relation to the origin of a 2D/3D coordinate system. • P (x, y, z) • you can translate, rotate and scale a point.

3D Rotation

• glRotatef(float angle, float x, float y, float z) // produce a rotation of the angle degrees about the vector <x, y, z>

Vertex Buffer Object (VBO)

• the same idea as the Vertex Array, and the use is pretty much the same as well. • Data handled by VBO will be stored directly on the graphics hardware. • Motivation: combining the benefits of Vertex Array and Display List. • Vertex Array is CPU side implementation (client), data can be updated. • Display List works on GPU side (GPU), but data cannot be updated. Creating a VBO • Generate a new buffer object with glGenBuffers() • creates buffer objects and returns the identifiers of the buffer objects. • Syntax: void glGenBuffers(GLsizei n, GLuint* ids) • Bind the buffer object with glBindBuffer() • hook the buffer object with the corresponding ID • Syntax: void glBindBuffer(GLenum target, GLuint id) • Copy vertex data to the buffer object with glBufferData() • copy the data into the buffer object • Syntax: void glBufferData(GLenum target, GLsizei size, const void* data,GLenum usage)


Related study sets

The Life Span Human Development for Helping Professionals Ch 4

View Set

AP European History Chapter 17 Quiz

View Set

WH: 4.13.1 Lesson: The Roman Empire Review

View Set

Chapter 7:2 Study Guide Foundation of Health Science

View Set

Chapter 17: Government and Legal Issues in Compensation

View Set

Principles of Economics 2e Chapter 17 Test Bank

View Set

Billing and coding exam style questions

View Set