Computer Graphics, Chapt 08
Creating an Image Components, parts of a scene to be displayed Trees, terrain Furniture, walls Store fronts and street scenes Atoms and molecules Stars and galaxies Describe Internal structure of the individual objects Location (coordinates) within the scene Functions in a graphics package to describe the various image components: graphics output primitives or (geometric) primitives Computer Graphics, WS 07/08 2
Software Standards in Computer Graphics Goal of graphics standards Portability, independent of hardware platform Simple transfer between systems GKS (Graphical Kernel System) 1984 Adopted as first graphics software standard by International Standards Organization (ISO) American National Standards Institute (ANSI) Various national standards organizations Designed as 2D package, extended to 3D PHIGS (Programmer s Hierarchical Interactive Graphics Standard) Extension of GKS Capabilities for hierarchical modeling, color specification Surface rendering, picture manipulations Computer Graphics, WS 07/08 3
ERLGRAPH (Erlanger Graphik System) ~1985 Vom Regionalen Rechenzentrums Erlangen Weiterentwicklung des Erlanger Graphik Systems (EGS) Programmbibliothek für FORTRAN Funktionen für das Visualisieren wissenschaftlicher Daten Mathematischer Funktionen als 2-D- und 3-D-Plots Ausgabe auf verschiedenen Ausgabegeräten Computer Graphics, WS 07/08 4
GL (Graphics Library) Came with workstations ti from Silicon Graphics, Inc. (SGI) Widely used in graphics community and a de facto standard Set of routines designed for fast, real-time rendering Soon extended to other hardware systems OpenGL Hardware independent version of GL (early 1990s) OpenGL Architecture Review Board responsible for maintenance and updates Consortium of many graphics companies and organizations Specifically designed for efficient processing in 3D Also handles 2D scene descriptions as a special case Available language binding C, C++, Fortran, Ada Computer Graphics, WS 07/08 5
OpenGL in computer graphics Provides hardware abstraction layer through its Application Programmer Interface (API) Thereby provides low level, platform independent graphics allows non-standard extensions It does not have high level modeling constructs, such as scene graphs windowing facilities requires interface to window system: GLUT, GLX, AGL, WGL Computer Graphics, WS 07/08 6
Literature: OpenGL Reference Manual The Blue Book html-version: http://www.opengl.org/documentation/blue_book_1.0 org/documentation/blue book D. Hearn, M.B. Baker: Computer Graphics with OpenGL, (3rd edition) OpenGL(R) Programming Guide: The Official Guide to Learning OpenGL(R), Version 2 (5th Edition) (OpenGL) Computer Graphics, WS 07/08 7
Basic OpenGL Syntax Function names Prefixed with gl Each component word has its first letter capitalized: e.g.: glclear, glcopypixel, glpolygonmode Assignment of symbolic constants Certain functions require one or more e.g: GL_2D, GL_RGB, GL_POLYGON All begin with GL_ and component words in capital letters Data types Problem: specifications can be different on different machines (e.g. integer) Special built in data-types e.g.: GLbyte, GLshort, GLfloat, GLdouble Computer Graphics, WS 07/08 8
Related Libraries OpenGL Utility (GLU) Included by every OpenGL implementation Common prefix: glu Supplies advanced routines, e.g. Setting gup viewing gand dpojecto projection matrices Complex objects with line and polygon approximations Display of quadrics and B-splines using linear approximations Processing of surface rendering operations Open Inventor Object oriented toolkit based on OpenGL, written in C++ Routines and predefined object shapes for interactive 3D applications Scene description by scene graphs Computer Graphics, WS 07/08 9
Graphics display with OpenGL Set up a display window on the screen computer dependent d Not directly with OpenGL (device independent functions only!) Window system libraries supporting OpenGL GLX (OpenGL extension to the X Window System) set of routines prefixed with: glx AGL (Apple GL) on Apple systems set of routines prefixed with: agl WGL (Windows to OpenGL) on Microsoft Windows systems set of routines prefixed with: wgl PGL (Presentation Manager to OpenGL) for IBM OS/2 set of routines prefixed with: pgl OpenGL Utility Toolkit (GLUT) set of routines prefixed with: glut Interaction with any screen-windowing system Computer Graphics, WS 07/08 10
Header Files OpenGL core library GLU library for most applications Windowing systems Example for Microsoft Windows If GLUT is used #include <windows.h> #inlcude <GL/gl.h> #include <GL/glu.h> #include <windows.h> #inlcude <GL/glut.h> Computer Graphics, WS 07/08 11
Display-window management using GLUT Initialization glutinit(&argc, argv); Creation of a display window with caption glutcreatewindow( OpenGL example ); Specification what the display window should contain Supposing a code for line description called linesegment glutdisplayfunc(linesegment); Passes line-segment description to display window Activation of all display window including graphic content glutmainloop (); Must be the last function Displays the graphics and puts program to infinite loop Checks for input from devices (e.g.: mouse, keyboard) Computer Graphics, WS 07/08 12
Initial location of display window glutinitwindowposition(50, iti 100); Top left corner of the display Initial pixel width and height of display window glutinitwindowssize(400, 300); Other options (e.g.: buffering, color mode) glutinitdisplaymode(glut_single GLUT_RGB); Values of constants passed with logical or operations other values: GLUT_DOUBLE, GLUT_DEPTH 50 100 400 300 Computer Graphics, WS 07/08 13
To complete a program Set background color glclearcolor(1.0, 1.0, 1.0, 0.0); The 4 th value specifies the alpha value used in blending to mimic transparency Displaying the assigned window color glclear(gl_color_buffer_bit); GL_COLOR_BUFFER_BIT specifies the bit values in the color buffer (refresh buffer) to be set to the values indicated in glclearcolor Assignment of color to objects glcolor3f(1.0, 0.0, 0.0); RGB components using floating-point (f) values Note that each vertex can have a different color Computer Graphics, WS 07/08 14
Definition of output primitives Point position, straight lines, circles, other conic sections Quadric surfaces, spline curves and surfaces Polygon color areas, character strings After geometry definition Projection of output primitives Onto a 2D plane display area of output device Scan conversion: integer pixel positions in frame buffer glflush(); Process all OpenGL routines as quickly as possible Computer Graphics, WS 07/08 15
2D Cartesian reference frame in OpenGL (also see chapter 3 of Hearn & Baker for further details) Defining coordinate frame for screen display window gluortho2d orthogonal projection glmatrixmode (GL_PROJECTION); glloadidentity (); gluortho2d(xmin, xmax, ymin, ymax); Assign identity matrix in OpenGL projection matrix Prevents accumulation with previous values Place coordinate values in OpenGL projection matrix y max y min x min x max y min Computer Graphics, WS 07/08 16
Example program Main routine #include <GL/glut.h> void main (int argc, char** argv){ glutinit(&argc, argv); // initialize GLUT glutinitdisplaymode(glut_single GLUT_RGB); l glutinitwindowposition(50, 100); // top-left pos. glutinitwindowssize(400, 300); // width, height glutcreatewindow( OpenGL example); // create display window init(); // initialization procedure glutdisplayfunc(linesegment); // send graphics to display glutmainloop (); // display and wait } Computer Graphics, WS 07/08 17
Additional routines void init (void) { // Initialization p glclearcolor(1.0, 1.0, 1.0, 0.0); glmatrixmode (GL_PROJECTION); // projection parameters glloadidentity (); gluortho2d(0.0, 200.0, 0.0, 150.0); } void linesegment(void){ glclear(gl_color_buffer_bit); // clear display window glcolor3f(1.0, 0.0, 0.0); // set line color glbegin (GL_LINES); // line geometry glvertex2i (180, 15); glvertex2i (10, 145); glend (); glflush(); } Computer Graphics, WS 07/08 18
Graphics Output Primitives OpenGL point functions Specify geometry of a point glvertex * ( coordinate position in reference frame ) *: suffix codes are used Identify spatial dimension: values 2, 3, 4 State data type: i (integer), s (short), f (float), d (double) Identification of output primitive (here points) glbegin g (gl_ POINTS); glvertex* (); glend (); Coordinate values List explicitly Array referencing coordinate position append suffix code v (for vector) Computer Graphics, WS 07/08 19
Graphics Output Primitives Geometric primitives are passed through the lighting /shading algorithms, the 3D viewing (projection) algorithms, and finally rasterization (scan conversion) Default display Color (white) Size (one screen pixel) Coordinate extents of a primitive Within range of display window All of the primitive is displayed Beyond range of display window Only those parts within the coordinate range of the display window are shown (clipped object) Computer Graphics, WS 07/08 20
Graphics Output Primitives Example glbegin (GL_POINTS); glvertex2i (50, 100); glvertex2i (75, 150); glvertex2i (100, 200); glend (); Coordinate definition in arrays int point1 [] = {50, 100}; int point2 [] = {75, 150}; int point3 [] = {100, 200}; glbegin (GL_POINTS); glvertex2iv (point1); glvertex2iv (point2); glvertex2iv (point3); glend (); Computer Graphics, WS 07/08 21 200 100 100 200
Graphics Output Primitives OpenGL line functions Define the two endpoint coordinate positions Specify one or more straight-line segments Use glvertex funtions Enclose between glbegin / glend Use symbolic constant t for glbegin G_LINES GL_LINE_STRIPLINE GL_LINE_LOOP Default display Solid, white lines Computer Graphics, WS 07/08 22
Graphics Output Primitives Example glbegin (GL_LINES); p3 glvertex2iv (p1); glvertex2iv (p2); glvertex2iv (p3); glvertex2iv (p4); glvertex2iv (p5); glend (); p2 p4 glbegin (GL_LINE_STRIP); glvertex2iv (p1); glvertex2iv (p2); glvertex2iv (p3); glvertex2iv (p4); glvertex2iv (p5); glend (); p2 p4 p5 p3 p1 p1 Computer Graphics, WS 07/08 23
Graphics Output Primitives continued glbegin (GL_LINE_LOOP); glvertex2iv (p1); glvertex2iv (p2); glvertex2iv (p3); glvertex2iv (p4); glvertex2iv (p5); glend (); p5 p2 p3 p4 p1 Computer Graphics, WS 07/08 24
Tutorial (some kind of) : Tuto -- an OpenGL-Demo-Generator G available on CIP Computer Graphics, WS 07/08 25