Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. ·...
Transcript of Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. ·...
![Page 1: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/1.jpg)
Computer Graphics
Bing-Yu ChenNational Taiwan University
![Page 2: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/2.jpg)
Introduction to OpenGLGeneral OpenGL IntroductionAn Example OpenGL ProgramDrawing with OpenGLTransformationsAnimation and Depth BufferingLightingEvaluation and NURBSTexture MappingAdvanced OpenGL TopicsImaging modified from
Dave Shreiner, Ed Angel, and Vicki Shreiner.An Interactive Introduction to OpenGL Programming.ACM SIGGRAPH 2001 Conference Course Notes #54.
& ACM SIGGRAPH 2004 Conference Course Notes #29.
![Page 3: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/3.jpg)
What is OpenGL? &What can it do for me?
OpenGL isa computer graphics rendering API
generate high-quality color images by rendering with geometric and image primitivescreate interactive applications with 3D graphics
window system independentoperating system independent
![Page 4: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/4.jpg)
OpenGL as a Renderer
Geometric primitivespoints, lines and polygons
Image Primitivesimages and bitmapsseparate pipeline for images & geometry
linked through texture mapping
Rendering depends on statecolors, materials, light sources, etc.
![Page 5: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/5.jpg)
OpenGL Libraries
OpenGL core libraryOpenGL32 on WindowsGL on most unix/linux systems
OpenGL Utility Library (GLU)part of OpenGLProvides functionality in OpenGL core but avoids having to rewrite codeNURBS, tessellators, quadric shapes, etc.
![Page 6: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/6.jpg)
OpenGL Libraries
Links with window systemGLX for X window systemsWGL for WidowsAGL for Macintoshglue between OpenGL & windowing systems
![Page 7: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/7.jpg)
GLUT
OpenGL Utility Library (GLUT)Provides functionality common to all window systems
Open a windowGet input from mouse and keyboardMenusEvent-driven
Code is portable but GLUT lacks the functionality of a good toolkit for a specific platform
Slide bars
![Page 8: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/8.jpg)
OpenGL and Related APIs
GLUT
GLU
GL
GLX, AGLor WGL
X, Win32, Mac O/S
software and/or hardware
application program
OpenGL Motifwidget or similar
![Page 9: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/9.jpg)
OpenGL Architecture
DisplayList
PolynomialEvaluator
Per VertexOperations &
PrimitiveAssembly
Rasterization Per FragmentOperations
Framebuffer
TextureMemory
CPU
PixelOperations
Geometricpipeline
![Page 10: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/10.jpg)
OpenGL FunctionsPrimitives
PointsLine SegmentsPolygons
AttributesTransformations
ViewingModeling
ControlInput (GLUT)
![Page 11: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/11.jpg)
What is Required for your Programs
Headers Files#include <GL/gl.h>#include <GL/glext.h>#include <GL/glu.h>#include <GL/glut.h>
LibrariesEnumerated Types
OpenGL defines numerous types for compatibility
GLfloat, GLint, GLenum, etc.
![Page 12: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/12.jpg)
Lack of Object Orientation
OpenGL is not object oriented so that there are multiple functions for a given logical function,
e.g. glVertex3f, glVertex2i, glVertex3dv,…..
Underlying storage mode is the sameEasy to create overloaded functions in C++ but issue is efficiency
![Page 13: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/13.jpg)
OpenGL Command Formats
glVertexglVertex33ffvv( v )( v )
Number ofNumber ofcomponentscomponents
2 2 -- (x,y) (x,y) 3 3 -- (x,y,z)(x,y,z)4 4 -- (x,y,z,w)(x,y,z,w)
Data TypeData Typeb b -- bytebyteub ub -- unsigned byteunsigned bytes s -- shortshortus us -- unsigned shortunsigned shorti i -- intintui ui -- unsigned intunsigned intf f -- floatfloatd d -- doubledouble
VectorVector
omit omit ““vv”” forforscalar formscalar form
glVertex2f( x, y )glVertex2f( x, y )
![Page 14: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/14.jpg)
OpenGL State
OpenGL is a state machineOpenGL functions are of two types
Primitive generatingCan cause output if primitive is visibleHow vertices are processes and appearance of primitive are controlled by the state
State changingTransformation functionsAttribute functions
![Page 15: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/15.jpg)
The OpenGL Pipeline
Processing is controlled by setting OpenGL’s state
colors, lights and object materials, texture mapsdrawing styles, depth testing
FragmentProcessing FramebufferVertex
Processing
![Page 16: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/16.jpg)
GLUT Basics
Application StructureConfigure and open windowInitialize OpenGL stateRegister input callback functions
renderresizeinput: keyboard, mouse, etc.
Enter event processing loop
![Page 17: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/17.jpg)
Sequence of Most OpenGL Programs
updateOpenGL’s
state(if necessary)
configureand open a
windowinitializeOpenGL’s
state
processuser events
Draw animage
![Page 18: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/18.jpg)
A Simple Program
Generate a square on a solid background
![Page 19: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/19.jpg)
simple.c#include <GL/glut.h>void mydisplay(){
glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5);
glEnd();glFlush();
}int main(int argc, char** argv){
glutCreateWindow("simple"); glutDisplayFunc(mydisplay); glutMainLoop();
}
![Page 20: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/20.jpg)
Event Loop
Note that the program defines a display callback function named mydisplay
Every glut program must have a display callbackThe display callback is executed whenever OpenGL decides the display must be refreshed, for example when the window is openedThe main function ends with the program entering an event loop
![Page 21: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/21.jpg)
Notes on Compilation
Unix/linuxInclude files usually in …/include/GLCompile with –lglut –lglu –lgl loader flagsMay have to add –L flag for X librariesMesa implementation included with most linux distributionsCheck web for latest versions of Mesa and glut
![Page 22: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/22.jpg)
Compilation on Windows
Visual C++ / Borland C Get glut.h, glut32.lib and glut32.dllCreate a console applicationAdd opengl32.lib, glu32.lib, glut32.lib to project settings (under link tab)
Cygwin (linux under Windows)Can use gcc and similar makefile to linuxUse –lopengl32 –lglu32 –lglut32 flags
![Page 23: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/23.jpg)
Another Sample Programvoid main( int argc, char** argv ){
int mode = GLUT_RGB|GLUT_DOUBLE; glutInitDisplayMode( mode );glutCreateWindow( argv[0] );init();glutDisplayFunc( display );glutReshapeFunc( resize ); glutKeyboardFunc( key );glutIdleFunc( idle );glutMainLoop();
}
![Page 24: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/24.jpg)
OpenGL Initialization
Set up whatever state you’re going to usevoid init( void ){
glClearColor( 0.0, 0.0, 0.0, 1.0 );glClearDepth( 1.0 );
glEnable( GL_LIGHT0 );glEnable( GL_LIGHTING );glEnable( GL_DEPTH_TEST );
}
![Page 25: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/25.jpg)
GLUT Callback Functions
Routine to callwhen something happens
window resize or redrawuser inputanimation
“Register” callbacks with GLUTglutDisplayFunc( display );glutIdleFunc( idle );glutKeyboardFunc( keyboard );
![Page 26: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/26.jpg)
Rendering CallbackDo all of your drawing here
glutDisplayFunc( display );void display( void ){glClear( GL_COLOR_BUFFER_BIT );glBegin( GL_TRIANGLE_STRIP );
glVertex3fv( v[0] );glVertex3fv( v[1] );glVertex3fv( v[2] );glVertex3fv( v[3] );
glEnd();glutSwapBuffers();
}
![Page 27: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/27.jpg)
Idle Callbacks
Use for animation & continuous updateglutIdleFunc( idle );
void idle( void ){t += dt;glutPostRedisplay();
}
![Page 28: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/28.jpg)
User Input CallbacksProcess user input
glutKeyboardFunc( keyboard );void keyboard( unsigned char key, int x, int y ){
switch( key ) {case ‘q’ : case ‘Q’ :
exit( EXIT_SUCCESS );break;
case ‘r’ : case ‘R’ :rotate = GL_TRUE;glutPostRedisplay();break;
}}
![Page 29: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/29.jpg)
On-Line Resourceshttp://www.opengl.org/
start here; up to date specification and lots of sample code
news:comp.graphics.api.openglhttp://www.sgi.com/software/opengl/http://www.mesa3d.org/
Brian Paul’s Mesa 3Dhttp://www.cs.utah.edu/~narobins/opengl.html
very special thanks to Nate Robins for the OpenGL Tutorssource code for tutors available here!
![Page 30: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/30.jpg)
Books
M. Woo,J. Neider,T. Davis,D. Shreiner.OpenGL®Programming Guide: The Official Guide to Learning OpenGL,ver. 1.2, 3rd. ed.,Addison-Wesley, 1999.
![Page 31: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/31.jpg)
Books
D. Shreiner.OpenGL®Reference Manual: The Official Reference Document to OpenGL, ver. 1.2, 3rd. ed.,Addison-Wesley, 1999.
![Page 32: Computer Graphicsgraphics.im.ntu.edu.tw/~robin/courses/3dcg08/ppt/3dcg08... · 2005. 2. 23. · Introduction to OpenGL General OpenGL Introduction An Example OpenGL Program Drawing](https://reader036.fdocuments.in/reader036/viewer/2022071109/5fe3e642baa84424372f7162/html5/thumbnails/32.jpg)
Books
E. Angel.InteractiveComputer Graphics:A Top-Down Approach with OpenGL™,3rd. ed.,Addison-Wesley, 2002.