opengl and parametric curves advanced multimedia technology: computer graphics yung-yu chuang...
TRANSCRIPT
OpenGL and Parametric Curves
Advanced Multimedia Technology: Computer GraphicsYung-Yu Chuang2005/12/21
with slides by Brian Curless, Zoran Popovic, Robin Chen and Doug James
Review of graphics pipeline
Transformation
Review of graphics pipeline
Projection & clipping
Review of graphics pipeline
• Rasterization • Visibility
Review of graphics pipeline
• Shading
Hierarchical modeling: a robot arm
First implementation
Better implementation
OpenGL implementation
Hierarchical modeling
Implementation
Human with hands
Better implementation
OpenGL implementation
OpenGL
• A low-level OS-independent graphics API for 2D and 3D interactive graphics.
• Initiated by SGI (called GL at early time)• Implementation, for Windows, hardware
vendors provide suitable drivers for their own products; for Linux, we have Mesa.
Helper libraries
• OpenGL does not provide OS-dependent functions such as windowing and input– GL: core graphics functions– GLU: graphics utilities in top of GL– GLUT: input and windowing functions
How does it work?
• From the programmer’s view– Specify geometric properties of the objects– Describe material properties– Define viewing– Define camera and object transformations
• OpenGL is a state machine– States: color, material properties, line width,
current viewing – States are applied to subsequent drawing
commands– Input: description of geometric objects– Output: shaded pixels
How does it work
• From the implementer’s perspective• Graphics pipeline
Primitives+ materialproperties
RotateTranslateScale
Is itVisible?
3D to 2D Scan conversionVisibilitydetermination
Display
Primitives: drawing a polygon
// put GL into polygon drawing mode
glBegin(GL_POLYGON);
// define vertices
glVertex2f(x0, y0);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEnd();
Code available at http://www.xmission.com/~nate/tutors.html
Primitives
Hardware may be more efficient on triangles; stripes require less data
Polygon restrictions
• In OpenGL, polygons must be simple and convex
not simple not convex
Attributes
• Part of the state of the graphics pipeline• Set before primitives are drawn. • Remain in effect!• Example:
– Color, including transparency– Reflection properties– Shading properties
Primitives: material properties
• glColor3f(r,g,b);
All subsequent primitives will use this color. Colors are not attached to objects. The above command only changes the system states.
• OpenGL uses red, green and blue color model. Each components are ranged within 0 and 1.
Primitives: material properties
Simple transformations
• Rotate by a given angle (in degrees) about ray from origin through (x,y,z)
glRotate{fd}(angle, x, y, z);• Translate by a given x, y, z values
glTranslate{fd}(x, y, z);• Scale with a factor in the x, y, and z directions
glScale{fd}(x, y, z);
• glPushMatrix(); glPopMatrix();
Orthographic projection
• glOrtho(left, right, bottom, top, near, far);
Camera transformations
• gluLookAt(eyex, eyey, eyez, cx, cy, cz, upx, upy, upz);
Example: drawing a box
Example: drawing a shaded polygon
Initializing attributes
Callback functions
• Handle “events”, Idle, Keyboard, Mouse, Menu, Motion, Reshape
• The display callback is installed by glutDisplayFunc()
Actual drawing function
Results
glShadeModel(GL_FLAT) glShadeModel(GL_SMOOTH)
Depth buffer in OpenGL
• glutInitDisplayMode(GLUT_DEPTH);• glEnable(GL_DEPTH_TEST);• glClear(GL_DEPTH_BUFFER_BIT);
Double buffering
• Flicker if drawing overlaps screen refresh• Solution: use two frame buffers
– Draw into one buffer– Swap and display, while drawing other buffer
• glutInitDisplayMode(GLUT_SINGLE)• glutInitDisplayMode(GLUT_DOUBLE)• glutSwapBuffers()
Example: rotate a color cube
• Step 1: define the vertices
Example: rotate a color cube
• Step 2: enable depth testing and double buffering
Example: rotate a color cube
• Step 3: create window and set callbacks
Example: rotate a color cube
• Step 4: reshape callback, enclose cube, preserve aspect ratio
Example: rotate a color cube
• Step 5: display callback, clear, rotate, draw, flush, swap
Example: rotate a color cube
• Step 6: draw cube by drawing faces, orientation consistency
Example: rotate a color cube
• Step 7: drawing face
Example: rotate a color cube
• Step 8: animation, set idle callback spinCube()
Example: rotate a color cube
• Step 9: change axis of rotation using mouse callback
Example: rotate a color cube
• Step 10: toggle rotation or exit using keyboard callback
Mathematical curve representation
Parametric polynomial curves
Cubic curves
N too small → less flexibility in controlling the shape of the curveN too large → often introduce unwanted wiggles
Compact representation
Constrain the cubics
Hermite: defined by two endpoints and two endpoint tangent vectorsBezier: defined by two endpoints and two other points that control the endpoint tangent vectorsSpline: defined by four control points
Hermite curves
Computing Hermite basis matrix
Computing Hermite basis matrix
Computing a point
Blending functions
Bezier curves
Bezier basis matrix
Bezier basis matrix
Bezier blending function
Alternative (de Casteljau’s algorithm)
Finding Q(u)
Bezier curve
•each is between 0 and 1•sum of all four is exactly 1
Curve lies within the convex Hull of its control points
Displaying Bezier curves
Testing for flatness
What do we want for a curve?
• Local control• Interpolation• Continuity
Local control
• One problem with Bezier curve is that every control points affect every point on the curve (except for endpoints). Moving a single control point affects the whole curve.• We’d like to have local control, that is, have each control point affect some well-defined neighborhood around that point.
Interpolation
• Bezier curves are approximating. The curve does not necessarily pass through all the control points. We’d like to have a curve that is interpolating, that is, that always passes through every control points.
Continuity
• We want our curve to have continuity: there shouldn’t be any abrupt changes as we move along the curve.
Splines• We will splice together a curve from individual
Bezier segments. We call these curves splines. When splicing Bezier together, we need to worry about continuity.
Ensuring C0 continuity
1st derivatives at the endpoints
Ensuring C1 continuity
The C1 Bezier spline
Catmull-Rom splines
Catmull-Rom basis matrix
3
2
1
0
61
61
61
61
3
2
1
0
0100
10
01
0010
P
P
P
P
V
V
V
V
3
2
1
0
3
2
1
0
0001
0033
0363
1331
)()(
V
V
V
V
T
V
V
V
V
tBtQ b
2nd derivatives at the endpoints
Ensuring C2 continuity
A-frames and continuity
B-spline
B-spline