1Intro to OpenGLAnimationWindows and ClippingCS148: Intro to CGInstructor: Dan MorrisTA: Sean WalkerJune 28, 2005GL Examples{ Today will be a learn-by-doing sort of lecture… examples are more important than slides{ Example programs will be available on the web{ Today’s class will be optimally fun if you encourage me to prod and poke at the code...Outline for today{ OpenGL conventions{ OpenGL program structure{ OpenGL primitives{ Animation{ SIGGRAPH video break{ Windows and viewports{ ClippingOpenGL conventions{ Function names look like:gl[action] [#][data type] [v](…)z Action tells you what the function doesz Data type tells you what type (float, double, int, etc.) it works withz Number tells you how many it takesz A ‘v’ tells you that this function takes vector (pointer) input{ Top-level documentation only refers to functions by ‘action’OpenGL Data typesGLuint, GLenum, GLbitfieldunsigned int32-bit unsigneduiGLushortunsigned short16-bit unsignedusGLshortshort16-bit intsGLubyte, GLbooleanunsigned char8-bit unsignedubGLint, GLsizeiint32-bit integeriGLbytechar8-bit integerbGLfloatfloat32-bit floatfGLdoubledouble64-bit floatdGL typeC typeData TypeSuffixExamplesvoid glVertex3f(GLfloat x, GLfloat y, GLfloat z) void glColor3b(GLbyte red, GLbytegreen, GLbyte blue) void glMaterialfv(GLenum face, GLenum pname, const GLfloat*params)2GL Errors{ Almost all functions return void{ If you want to find out whether there was an error, you need to call glGetError(){ glGetError() is, in technical terms, crazy stupid (editor’s opinion){ It’s usually easier to track down your problem without error codesOutline for today{ OpenGL conventions{ OpenGL program structure{ OpenGL primitives{ Animation{ SIGGRAPH video break{ Windows and viewports{ ClippingWhen do I draw stuff?{ GLUT can give you a callback when the window needs to be redrawnz glutDisplayFunc, glutReshapeFuncz Not useful for animation{ GLUT can give you a callback whenever it’s not busy or every few millisecondsz glutIdleFunc, glutTimerFunc{ You can also draw whenever you wantz pp1, for example, draws in response to mouse eventsAn OpenGL Drawing Functionvoid drawMyStuff(void) {// Clear the windowglClear(GL_COLOR_BUFFER_BIT);// Do my drawingglBegin(SOME_PRIMITIVE_TYPE);…glEnd();glBegin(SOME_OTHER_PRIMITIVE_TYPE);…glEnd();// I’m really done, put my pixels on the screenglFlush();}Drawing GL Primitives// Set up color, texture, location, etc.glColor3f(1.0f,0.0f,0.0f);// Tell GL what kind of data to get ready forglBegin(GL_POINTS);// Draw verticesglVertex3d(1.0,2.0,5.0);glVertex3d(2.0,3.0,10.0);// Maybe change some properties// and maybe draw some more vertices// Tell GL you’re done drawing for a whileglEnd();Too many slides without a picture3Outline for today{ OpenGL conventions{ OpenGL program structure{ OpenGL primitives{ Animation{ SIGGRAPH video break{ Windows and viewports{ ClippingImportant GL Primitives: Points{ GL_POINTSz Treats each vertex as a single point.Vertex n defines point n.N points are drawn.{ glPointSize(GLfloat size)z Sets the diameter (pixel) of points.Subtext: A GL_POINT is not necessarily a pixel[simple.cpp mathfunc.cpp sier.cpp]Important GL Primitives: Lines{ GL_LINESz Treats each pair of vertices as a line segment. N/2 lines are drawn.{ GL_LINE_STRIPz Draws a connected group of line segments from the first vertex to the last. N-1 lines are drawn.{ GL_LINE_LOOPz Like GL line strip but it connects the last point to the first point.{ glLineWidth(GLfloat width);z Specifies the width of lines (in pixels)[polyline.cpp stip.cpp]Important GL Primitives: Polygons{ GL_TRIANGLESz Treats each triplet of vertices as a triangle segment. N/3 triangles are drawn.{ GL_TRIANGLE_STRIPz Draws a connected group of triangles; each new vertex starting from the third adds a new triangle.{ GL_QUADS, GL_QUAD_STRIPz glRect is a shortcut for begin/quad/end{ GL_POLYGON[polygons.cpp, mystery1.cpp, mystery2.cpp]Outline for today{ OpenGL conventions{ OpenGL program structure{ OpenGL primitives{ Animation{ SIGGRAPH video break{ Windows and viewports{ ClippingAnimation 1 (mousemv.cpp)// somewhere in main()glutMotionfunc(myMouseMove);void myMouseMove (int x, int y) {// put the origin at the bottom-lefty = screenHeight - mousey;glClear(GL_COLOR_BUFFER_BIT);make_ngon(x, y, 300, 60); glFlush();}“What’s wrong with this approach?”-Dan Morris, 2005, every single lecture4Single-buffering{ All drawing takes place to a single framebuffer{ Graphics hardware scans buffer whenever it feels like it{ Memory-efficient{ Good for static scenes{ Prone to flickeringWhy is flickering worse for largeobjects?Double-buffering{ Only update the “real” framebuffer when you’re finished drawing{ Do all your drawing to a separate framebuffer{ Swap the buffers once per frame{ Terminology:z The “front buffer” is shown on the screenz The “back buffer” is where you drawWhat are some disadvantages of double-buffering?Double-buffering in OpenGLGLUT / CS148:// glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);// glFlush();glutSwapBuffers();Functions you might see down the road:// For non-GLUT windows GL appsSwapBuffers(hdc);// If you need to manually control the current buffer…glDrawBuffer(GL_BACK); // or GL_FRONTTearing{ If the buffer-swap happens while you’re drawing, can get part of one “finished” frame and part of another{ Solution: hardware makes sure not to buffer-swap while the monitor is refreshing{ Downside: your program blocksWhich way was this circle moving?Animated OpenGL programs{ glutIdleFunc(): please give me CPU time to think{ glutPostRedisplay(): please call my display function sometime soon{ Recommended software design:z Update your virtual world in idle()z Do all your drawing in display()Animation: Using time{ Bad (but common) way to move an object at constant velocity:void idle() {object_position += MAGIC_NUMBER;}What’s wrong with this approach?void idle() {double curtime = CS148::getTime();double elapsed = last_time – curtime;object_position += MAGIC_NUMBER * elapsed;last_time = curtime;}5Animation examples[animate.cpp rgb.cpp]{ Also some great hints about what’s coming next:z 3D positionsz 3D transformationsz OpenGL matrix stackSIGGRAPH video breakDual Photography, Pradeep Sen at al, SIGGRAPH 2005{ Terms:z Helmholtz reciprocityz Re-lightingOutline for today{ OpenGL conventions{ OpenGL program structure{ OpenGL primitives{ Animation{ SIGGRAPH video break{ Windows and
View Full Document