550 likes | 1.42k Views
3D Graphics Pipeline WORLD SCENE/OBJECT Modelling coordinates: - world coordinate system, - object coordinate system 3D MODELLING VIEWING 3D CLIPPING Camera coordinates PROJECTION Screen/Window coordinates RASTERIZATION Device coordinates 2D PIXELMAP DISPLAY Object Modelling
E N D
3D Graphics Pipeline WORLD SCENE/OBJECT Modelling coordinates: - world coordinate system, - object coordinate system 3D MODELLING VIEWING 3D CLIPPING Camera coordinates PROJECTION Screen/Window coordinates RASTERIZATION Device coordinates 2D PIXELMAP DISPLAY
Object Modelling (N.B. not an exhaustive list) B-rep Parametric surface Implict surface Polygon Mesh CSG Tree Voxels Octree
Modelling Transformations Translation Rotation Scaling
3D Viewing A.K.A. Camera Transformation
Clipping 3D view frustrum outside view so must be clipped
Projection Parallel Projections viewport 3d models Perspective Projections
The OpenGL® API http://www.opengl.org
OpenGL® • cross-platform standard for 3D rendering and 3D hardware acceleration. • software runtime library ships with all Windows, MacOS, Linux and Unix systems • most PCs these days will come with some level of 3D acceleration – OpenGL intended for use with computer hardware that is designed and optimized for 3d graphics and allows you to make use of these capabilities • “a software interface to graphics hardware” • a library for 3d graphics and modelling • portable and fast
Overview • OpenGL is a procedural graphics API: programmer describes the steps involved to achieve a certain display • “steps” involve C style function calls to a highly portable API • fairly direct control over fundamental operations of two and three dimensional graphics • Intuitive naming convention and default setup make it a relatively easy API to learn • OpenGL has been around for a few years (1992) … long time in graphics hardware terms • forerunner: GL from Silicon Graphics • IrisGL - a 3D API for high-end IRIS graphics workstations
The OpenGL® Standard • an API not a language • Some other graphics packages include: • GKS (ISO & ANSI approved standard), • PHIGS (standard), • PEX, • GL / IrisGL, • Renderman (API & Language), • PostScript, • BGI (Borland Graphics Interface), • Renderware, • DirectX! • OpenGL attempts to be more portable • OpenGL Architecture Review Board (ARB) decides on all enhancements
What can it do ? • Display primitives • Coordinate transformations (transformation matrix manipulation) • Texture and Lighting calculations • Antialiasing • Pixel Update Operations • Display-Lists … and usually much faster than you can!
OpenGL® Pipeline Transform Geometry Clip to View Volume Project to Viewport Rasterise vertices pixels perform per-vertex rotations translations and scaling to achieve final geometry, then transform to the camera co-ordinate system project vertices onto the 2D plane representing the viewport/screen convert all polygons, line and point to pixel values eliminate vertices that will not be visible in the final image OpenGL functions like a state machine
v4 v4 v4 v4 v3 v3 v3 v3 v5 v5 v5 v5 v2 v2 v2 v2 v6 v6 v6 v6 v1 v1 v1 v1 v7 v7 v7 v7 v8 v8 v8 v8 Modeling in OpenGL® • All geometric objects in OpenGL are created from a set of basic point based primitives. • Certain primitives are provided to allow optimisation of geometry for improved rendering speed. • Line based primitives: GL_LINE_LOOP GL_LINE_STRIP GL_LINES GL_POINTS
v3 v3 v3 v5 v5 v5 v2 v2 v2 v6 v6 v6 v1 v1 v1 v7 v7 v7 v8 v8 v8 OpenGL® Primitives • Polygon primitives v4 v4 v4 GL_POLYGON GL_QUADS GL_TRIANGLES v4 v3 v8 v2 v4 v5 v6 v3 v2 v5 v2 v1 v4 v1 v6 v3 v5 v7 v1 GL_QUAD_STRIP GL_TRIANGLE_STRIP GL_TRIANGLE_FAN
OpenGL® Primitives • Primitives are declared following the state machine philosophy. • Example: glBegin(GL_QUADS); glVertex3d(1.0, 1.0, 0.0); glVertex3d(-1.0, 1.0, 0.0); glVertex3d(-1.0, -1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd();
OpenGL® Lighting and Texturing • The lighting, coloring and texturing of primitives also work by state modification. • Example: glColor3f(1.0, 0.0, 0.0); glBegin(GL_TRIANGLES); glVertex3d(1.0, 1.0, 0.0); glVertex3d(1.0, -1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glEnd(); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glBegin(GL_TRIANGLES); glVertex3d(1.0, 1.0, 0.0); glVertex3d(-1.0, -1.0, 0.0); glVertex3d(-1.0, 1.0, 0.0); glEnd();
Transformations and OpenGL® Vertex Geometry Pipeline MODELVIEW matrix PROJECTION matrix perspective division viewport transformation original vertex final window coordinates normalised device coordinates (foreshortened) 2d projection of vertex onto viewing plane vertex in the eye coordinate space
Transformations and OpenGL® • OpenGL defines 3 matrices for manipulation of 3D scenes: • GL_MODELVIEW: manipulates the view and models simultaneously • GL_PROJECTION: performs 3D to 2D projection for display • GL_TEXTURE: for manipulating textures prior to mapping on objects • Each acts as a state parameter; once set it remains until altered. • Having defined a GL_MODELVIEW matrix, all subsequent vertices are created with the specified transformation. • Matrix transformation operations apply to the currently selected system matrix: • use glMatrixMode(GL_MODELVIEW) to select modeling matrix • The Matrices are organised in stacks, for each stack, the matrix discribing the current transformation is the one at the top • glPopMatrix() and glPushMatrix() manipulate the current stack
Transformations and OpenGL • glRotatef(angle, vx, vy, vz) • rotates about axis (vx, vy, vz) by angle (specified in degrees) • glTranslatef(dx, dy, dz) • translates by displacement vector (dx, dy, dz) • glScalef(sx, sy, sz) • apply scaling of sx in x direction, sy in y direction and sz in z direction (note that these values specify the diagonal of a required matrix) • glLoadIdentity() • creates an identity matrix (used for clearing all transformations) • glLoadMatrixf(matrixptr) • loads a user specified transformation matrix where matrixptr is defined as GLfloat matrixptr[16]; • glMultMatrixf(matrixptr) • multiply current matrix with user specified matrix
Transformations and OpenGL® • Each time an OpenGL transformation M is called the matrix C at the top of the current stack is altered: M glTranslatef(1.5, 0.0, 0.0); glRotatef(45.0, 0.0, 0.0, 1.0);
Transformations and OpenGL® • The MODELVIEW matrix is a 4x4 affine transformation matrix and therefore has 12 degrees of freedom: • The MODELVIEW matrix is used for both the model and the camera transformation • rotating the model is equivalent to rotating the camera in the opposite direction OpenGL uses the same transformation matrix • this sometimes causes confusion!
Scale • all vectors are scaled from the origin: Original scale all axes scale Y axis offset from origin distance from origin also scales
Rotation • Rotations are anti-clockwise about the origin: rotation of 45o about the Z axis offset from origin rotation
Translation translation along y
Transformations and OpenGL® • Transformations are applied in the order specified (with respect to the vertex) which appears to be in reverse: glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(1.5, 0.0, 0.0); glRotatef(45.0, 0.0, 0.0, 1.0); glVertex3f(1.0, 0.0, 0.0); original rotate translate
Relevant Libraries • OpenGL has no windowing functions of its own • We need to use something like the GLUT libraries for windowing operations etc. OpenGL Window System C/C++ Code Graphics Hardware GLU OpenGL Application GLUT C/C++ Libraries
Relevant Libraries • The GL library • core functions of OpenGL e.g. modelling, viewing, clipping, lighting • The GL Utility (GLU) library: • creation of common objects (e.g. spheres, quadrics) • specification of standard views (e.g. perspective, orthographic) • The GL Utility Toolkit (GLUT) • provides the interface with the windowing system. • window management, menus, mouse interaction
The OpenGL Utility Toolkit • OpenGL has no window system interface of its own • GLUT is a programming interface for writing windows system independent OpenGL programs • Implementations of the GLUT API exist for most popular platforms • Alternatives are native window systems APIs e.g. Xlib, Motif, Win32, tk, aux • GLUT is much simpler, but less powerful • GLUT is not part of the standard OpenGL distribution
GLUT support - Multiple windows for OpenGL rendering - Callback driven event processing - Sophisticated input devices - An 'idle' routine and timers - A simple, cascading pop-up menu facility - Utility routines to generate various solid and wire frame objects - Support for bitmap and stroke fonts - Miscellaneous window management functions
OpenGL® GLUT Event Loop User Expose Mouse Key Windows System Resize Event List Operating System Application while (TRUE) e = get_next_event(); switch (e) case MOUSE: call register MouseFunc case Resize call registered ResizeFunc ... glutMainLoop()=
Simple GLUT Program int main() { glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutCreateWindow(“First GLUT program”); init(); glutDisplayFunc(myDisplay); glutMainLoop(); return 0; } • Set up GLUT with RGB mode and single buffer • Create a GLUT window – this requires that you specify a display callback with glutDisplayFunc • glutMainLoop starts the GLUT event handler transfers control of the program to GLUT
Simple GLUT Program: the display callback void display() { glClear (GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); glBegin(GL_QUADS); glVertex3d(1.0, 1.0, 0.0); glVertex3d(-1.0, 1.0, 0.0); glVertex3d(-1.0, -1.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); } • This is where most of your openGL functions will be called • In this example… • Clear the screen (or clear the frame buffer) • Set the drawing color to white • Draw a quad primitive with the specified vertices
Simple GLUT program: openGL initialization void init() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glOrtho(-2, 2, -2, 2, -2, 2); } • Some OpenGL functions associated with setting up initial states only need to be called once in most programs. It is useful to place these in a separate initialization function. • E.g. • Set the clear color (the background) to black • Tell OpenGL to use Orthographic projection with a bounding box defined by (xmin, xmax, ymin, ymax, zmin, zmax)=(-2, 2, -2, 2, -2, 2)
Initialisation Command line processing, window system initialisation initial window state Beginning Event Processing Enter event processing loop Window Management Overlay Management Menu Management Callback Registration Registers procedures which will be called by GLUT event processing loop Color Index, ColorMap management State Retrieval Font Rendering Geometric Shape Rendering GLUT sub-APIs
Conventions • GLUT coordinates are expressed in pixels with (0,0) as the upper-left • In most GLUT routines basic types (in, char*) are used as parameters. In routines where parameters are passed directly to OpenGL OpenGL types are used (e.g. GLfloat) • GLUT functions are prefixed glut… (e.g. glutInitDisplayMode) • Constants are prefixed by GLUT_.. (e.g. GLUT_RGB)
Initialisation • glutInit initializes the GLUT library • Display modes: • GLUT_RGBA, GLUT_RGB, GLUT_INDEX, GLUT_SINGLE, GLUT_DOUBLE, GLUT_ACCUM, GLUT_ALPHA, GLUT_DEPTH, GLUT_STENCIL, GLUT_MULTISAMPLE, GLUT_STEREO void glutInit(int* argcp, char** argv); void glutInitWindowSize(int width, int height); void glutInitWindowPosition(int y, int y); void glutInitDisplayMode(unsigned int mode);
Beginning Event Processing void glutMainLoop(void); • glutMainLoop enters the GLUT event processing loop • Should only be called once in a GLUT program – never returns • Calls as necesarry any registered callbacks While (TRUE) { e=getNextEvent(); switch (e) { case (MOUSE_EVENT): call registered MouseFunc break; case (RESIZE_EVENT): call registered ReshapeFunc break; … }
Window Management • glutCreateWindow creates a top level window returns window id – display is actually only called when glutMainLoop is entered • glutPostRedisplay marks current window as needing to be redisplayed int glutCreateWindow(char* name); void glutPostRedisplay(void); void glutSwapBuffers(void); void glutSetWindow(int win); int glutGetWindow(void);
Call Back Registration • void glutDisplayFunc(void (*func) (void)); • void glutReshapeFunc(void (*func) (int width, int height)); • void glutKeyboardFunc(void (*func) (unsigned char key, int x, int y)); • void glutMouseFunc(void (*func) (int button, int state, int x, int y)); • void glutIdleFunc(void (*func) (void)); • void glutTimerFunc(unsigned int msecs, void (*func) (int value), value);
Geometric Object Rendering • void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks); • void glutWireSphere(GLdouble radius, GLint slices, GLint stacks); • void glutSolidCube(GLdouble size); • void glutWireCube(GLdouble size); 20, 6 20, 20 8, 20
GLUT References • The full GLUT manual (PS/PDF) is available locally for download/viewing at: • http://isg.cs.tcd.ie/dingliaj/openGL/glut.ps • http://isg.cs.tcd.ie/dingliaj/openGL/glut.pdf • HTML version online • http://reality.sgi.com/mjk_asd/spec3/spec3.html • http://www.opengl.org/Documentation/GLUT.html • More GLUT info at opengl.org: • http://www.opengl.org/developers/documentation/glut.html • GLUT for win32 • http://www.xmission.com/~nate/glut.html
OpenGL info • Hearn and Baker OpenGL Supplement: • http://www.ncsa.uiuc.edu/Vis/Graphics/OpenGL.html • “OpenGL Programming Guide (2nd Ed)” - Mason Woo, Jackie Neider, and Tom Davis • “OpenGL Reference Manual (2nd Ed)” - OpenGL ARB, Editors: R. Kempf, C. Frazier • “OpenGL SuperBible” - R.S. Wright, M. Sweet