Download Shapes - Introduction to Computer Graphics - Lecture Slides and more Slides Computer Graphics in PDF only on Docsity!
Lecture 07:
Shapes
Quick Review of Linear Algebra
• Points vs. Vectors
• What are basis vectors?
- In 2D, what 2 vectors cannot be
basis vectors?
• What is:
- a unit vector
- a normal vector
- a null vector?
• Vector math:
- Add, subtract, multiple (by scalar)
- Normalize a vector
• What is a cross product?
- How do you find the cross
product?
- Different ways to think about the
cross product?
• What is a dot product?
- How do you find the dot product?
- Different ways to think about the
dot product?
• Given two vectors A, B, show that
the cross product N is orthogonal
to A and B.
Example OpenGL Application
int main( int argc, char** argv ) {
glutInit( &argc, argv ); // Boilerplate initialization glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH );
glutInitWindowPosition( 50, 50 ); //upper left glutInitWindowSize( 640, 480 ); // width, height, in pixels glutCreateWindow( "OpenGL Example" ); // window title
glViewport( /* lower left corner of the viewport / 0, 0, / width, height of the viewport */ 640, 480 ); //lower left
glShadeModel( GL_SMOOTH );
glPolygonMode( GL_FRONT, GL_FILL );
setupLighting();
setupCamera(640, 480);
registerCallBacks();
glutMainLoop();
}
void registerCallbacks() {
glutReshapeFunc (myReshape);
glutKeyboardFunc (myKeyboard);
glutMouseFunc (myMouseClick);
glutMotionFunc (myMouseMove);
glutDisplayFunc (myDisplay);
glutIdleFunc (myIdle);
Example OpenGL Application
void myReshape(GLint width, GLint height) {
//called when the window is resized
glViewport(0, 0, width, height);
setupCamera(width, height);
}
void myKeyboard (unsigned car key, int x, int y) {
//called on keyboard event
switch (key) {
case ‘a’: someGlobalVariable = 1;
: :
}
}
void registerCallbacks() {
glutReshapeFunc (myReshape);
glutKeyboardFunc (myKeyboard);
glutMouseFunc (myMouseClick);
glutMotionFunc (myMouseMove);
glutDisplayFunc (myDisplay);
glutIdleFunc (myIdle);
Example OpenGL Application
void display(void) {
//called when the window needs to paint itself
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawMyStuff();
glutSwapBuffers();
numberOfFrames++; }
void myIdle (void) { //called ALL THE TIME!!
DWORD time_now = GetTickCount(); float diff = (float)(time_now-last_check_time)/1000.0; if (diff > 1.0) { float frameRate = numberOfFrames / (float)diff; numberOfFrames = 0; last_check_time = time_now; } glutPostRedisplay(); }
void registerCallbacks() {
glutReshapeFunc (myReshape);
glutKeyboardFunc (myKeyboard);
glutMouseFunc (myMouseClick);
glutMotionFunc (myMouseMove);
glutDisplayFunc (myDisplay);
glutIdleFunc (myIdle);
} Docsity.com
Example OpenGL Application
int main( int argc, char** argv ) {
glutInit( &argc, argv ); // Boilerplate initialization glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH );
glutInitWindowPosition( 50, 50 ); //upper left glutInitWindowSize( 640, 480 ); // width, height, in pixels glutCreateWindow( "OpenGL Example" ); // window title
glViewport( /* lower left corner of the viewport / 0, 0, / width, height of the viewport */ 640, 480 ); //lower left
glShadeModel( GL_SMOOTH );
glPolygonMode( GL_FRONT, GL_FILL );
setupLighting();
setupCamera(640, 480);
registerCallBacks();
glutMainLoop();
}
No turning back at
this point, we enter
the infinite loop!
2D Object Definition
• Lines and Polyline
– Polyline: lines drawn between
ordered points
• Polygon
– If the first and last points of a
polyline are the same point, then
the polyline is said to be “closed”,
and that the closed polyline forms
a polygon
• Self-intersected polyline
simple polygon,
closed polyline
not closed,
simple polyline
not simple polygon,
closed polyline
2D Object Definition
• Convex and Concave polygons
– Convex: for every pair of points inside the
polygon, the line between them is entirely inside
the polygon
– Concave: for some pair of points inside the
polygon, the line between them is not entirely
inside the polygon. Not convex.
convex
concave
2D Object Definition
• Why is a convex polygon so awesome?
– Center of mass
– Tessellation
– Subdivision
– Fixed boundary
– Exactly two intersections
– All diagonals are within the boundary
– Tessellation -> computing the area
– Every interior angle is less than 180
– Etc.
Special Polygons
• Circle
– Set of all points equidistant
from one point called the center
– The distance from the center
is the radius r
– The equation for a circle centered
at (0, 0) is r
2
= x
2
+ y
2
Triangle Square Rectangle
(0, y)
(0, x)
r
(x, y)
Splines
• How they work: Parametric curves
governed by control points
– Mathematically: Several representations
to choose from. More complicated than
vertex lists.
• Pros and Cons:
– Advantage: Smooth with just a few
control points
– Disadvantage: Can be hard to control
• For more information about splines, see
the COMP150-7 (Visualization) course
– http://en.wikipedia.org/wiki/B%C3%A9zi
er_curve
Representing Shapes
• Vertex and Edge tables
– General purpose, minimal overhead, reasonably
efficient
– Each vertex listed once
– Each edge is an ordered pair of indices to the
vertex list
Vertices
Edges
E 4
E 3 E
2
E 0
E 1
V 0 V 1
V 2 V
3
V 4
Triangular Mesh Representation
Vertex, Face, (and Normal) Tables
v0 0, 0, Vertex List - f0 f1 f12 f
v1 1, 0, - f2 f3 f13 f
v2 1, 1, - f4 f5 f14 f
v3 0, 1, - f6 f7 f15 f
v4 0, 0, 1 f6 f7 f0 f8 f
v5 1, 0, 1 f0 f1 f2 f9 f
v6 1, 1, 1 f2 f3 f4 f10 f
v7 0 , 1, - f4 f5 f6 f
v8 .5, .5, 0 f8 f9 f10 f
v9 .5, .5, 1 f12 f13 f14 f - f0 v0 v4 v Face List - f1 v0 v5 v - f2 v1 v5 v - f3 v1 v6 v - f4 v2 v6 v - f5 v2 v7 v - f6 v3 v7 v - f7 v3 v4 v - f8 v8 v5 v - f9 v8 v6 v - f10 v8 v7 v - f11 v8 v4 v - f12 v9 v5 v - f13 v9 v6 v - f14 v9 v7 v - f15 v9 v4 v - v - v1 v - f - f - f - f - f - v - v4 v - v - f8 f - f - v
Normals