Today … • • • • The rendering pipeline in detail What is OpenGL First OpenGL program Details about modeling Basic Rendering Pipeline Modeling Transform ation (OS -> WS) Viewing Transfor mation (WS -> CS) Visibility Culling Illuminat ion Project ion Rasteriz ation & Clipping & Display L Database of 3D models N 1R 1V OpenGL and GLUT Overview • • • • What is OpenGL & what can it do for me? OpenGL in windowing systems Why GLUT A GLUT program template What is a “library”. • Static • Dynamic Relationship between OpenGL GLU and windowing APIs. •OpenGL applications use the window system’s window, input, and event mechanism •GLU supports quadrics, NURBS, complex polygons, matrix utilities, and more What Is OpenGL? • Graphics rendering API – high-quality color images composed of geometric and image primitives – window system independent – operating system independent OpenGL Library Functions GL GL library contains all primitive and attribute functions associated with OpenGL GLU GLU library builds on the GL library to include more complex primitives (e.g. spheres) and convenience functions GLUT GLUT (GL Utility Toolkit) includes functions to interface with the native window system, including window creation, management of input devices Related APIs • AGL, GLX, WGL – glue between OpenGL and windowing systems • GLU (OpenGL Utility Library) – part of OpenGL – NURBS, tessellators, quadric shapes, etc. • GLUT (OpenGL Utility Toolkit) – portable windowing API – not officially part of OpenGL OpenGL and Related APIs application program OpenGL Motif widget or similar GLX, AGL or WGL X, Win32, Mac O/S GLUT GLU GL software and/or hardware OpenGL Architecture Polynomial Evaluator CPU Display List Per Vertex Operations & Primitive Assembly Rasterization Texture Memory Pixel Operations Per Fragment Operations Frame Buffer Features of OpenGL • Delivers fast and complete 3D hardware acceleration • makes real-time 3D effects possible • designed to support future innovations in software and hardware • Available on many platforms • Stable OpenGL as a Renderer • Geometric primitives – points, lines and polygons • Image Primitives – images and bitmaps – separate pipeline for images and geometry • linked through texture mapping • Rendering depends on state – colors, materials, light sources, etc. Other issues • Rendering acceleration (rasterization, texture map, spacial subdivision, collision detection, progressive rendering, view dependent rendering, image-based rendering,…) • Color, Texture, Anti-aliasing • Animation or simulation Student 007: “I am dying to see an OpenGL program !…” Prof: Be patient, it is only few more block(diagram)s away !! Block Diagram Preliminaries • Headers Files • #include <GL/gl.h> • #include <GL/glu.h> • #include <GL/glut.h> • Libraries • Enumerated Types – OpenGL defines numerous types for compatibility – GLfloat, GLint, GLenum, etc. GLUT Basics • Application Structure – Configure and open window – Initialize OpenGL state – Register input callback functions • render • resize • input: keyboard, mouse, etc. – Enter event processing loop Sample Program – draw()function void drawScene(void) { // Clear the rendering window glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Set drawing color to white glColor3f( 1.0, 1.0, 1.0 ); // Draw triangle glBegin(GL_TRIANGLES); glColor3f( 1.0, 0.0, glVertex2f( 1.0, 1.0 glVertex2f( 2.0, 1.0 glVertex2f( 2.0, 2.0 glEnd(); // Flush the pipeline. glFlush(); } 0.0 ); ); ); ); (Not usually necessary.) OpenGL Command Formats glVertex3fv( v ) Number of components 2 - (x,y) 3 - (x,y,z) 4 - (x,y,z,w) Data Type b ub s us i ui f d - byte unsigned byte short unsigned short int unsigned int float double Vector omit “v” for scalar form glVertex2f( x, y ) Specifying Geometric Primitives • Primitives are specified using glBegin( primType ); glEnd(); – primType determines how vertices are combined GLfloat red, greed, blue; Glfloat coords[3]; glBegin( primType ); for ( i = 0; i < nVerts; ++i ) { glColor3f( red, green, blue ); glVertex3fv( coords ); } glEnd(); OpenGL Command Syntax • Function calls: glXxxxx[type] ( [args] ) – Example: glVertex2f (1.0, 2.0); • Defined constants: GL_XXXXXXXXXX – Example: GL_COLOR_BUFFER_BIT • Type definition: GLxxxxxxx – Example: GLfloat Command Suffixes/ Argument Data Types Suffix Data Type Typical Corresponding C-Language Type OpenGL Type Definition b 8-bit integer signed char GLbyte s 16-bit integer short GLshort f 32-bit integer int or long GLint, GLbyte i 32-bit floating-point float GLfloat, GLclampf d 64-bit floating-point double GLdouble,GLclampd ub 8-bit unsigned integer unsinged char GLubyte,GLboolean us 16-bit unsinged integer unsinged short ui 32-bit unsinged integer unsinged int or unsinged long GLushort GLuint, GLenum, GLbitfield Shared Namespace Model Model Function Calls Application } Run in same process Graphics Lib. OS glFlush(); unnecessary O I Client-Server Model: Model Function Calls Application Graphics Server O Graphics Lib. OS OS Network Protocol glFlush(); May be necessary I OpenGL Architecture Polynomial Evaluator CPU Display List Per Vertex Operations & Primitive Assembly Rasterization Texture Memory Pixel Operations Per Fragment Operations Frame Buffer State Management and Drawing in OpenGL OpenGL Initialization • Set up whatever state you’re going to use void init( void ) { glClearColor( 0.0, 0.0, 0.0, 1.0 ); glClearDepth( 1.0 ); glEnable( GL_LIGHT0 ); glEnable( GL_LIGHTING ); glEnable( GL_DEPTH_TEST ); } GLUT Callback Functions • Routine to call when something happens – window resize or redraw – user input – animation • “Register” callbacks with GLUT glutDisplayFunc( display ); glutIdleFunc( idle ); glutKeyboardFunc( keyboard ); A Typical Event-Driven Program Initialization Main Loop Event Handler Input Processing Procedures Background Processing Procedure Route messages to the application Button on Mouse Window s Event Route Event to Correct Application Window s Application WinMain() Message Left Button Dow n Message Processed WinProc() GLUT Event Processing Call Idle Function No Event in Queue Yes Call Function n Call Function n Call Function n Call Function Call n Function n Pop Event From Queue Switch on Event Type Callbacks Event Type Event A m1, m2 “Registered” Function function_A(m1, m2) Event B function_B( ) Event C function_C( ) Event D Event E Event F No Event Idle function( ) NB: Function parameters must match measures contained in associated event. Display Event Trigger: GLUT determines that the window needs to be redisplayed. A display event is generated when the window is first drawn. Callback function form: void display(); Registration: glutDisplayFunc(display); A display callback function must be registered for each window. GLUT Mouse Event Trigger: Any mouse button is depressed or released. Callback function form: void mouse_callback_func(int button, int state, int x, int y); Registration: glutMouseFunc(mouse_callback_function); GLUT Defined Mouse Constants GLUT_LEFT_BUTTON GLUT_MIDDLE_BUTTON GLUT_RIGHT_BUTTON GLUT_UP GLUT_DOWN Systems with only one mouse button can only generate a GLUT_LEFT_BUTTON callback. GLUT Reshape Event Trigger: Active window is resized Callback function form: void reshape_func(GLsizei w, GLsizei h); Registration: glutReshapeFunc(reshape_func); GLUT Move Event Trigger: The mouse is moved while one or more mouse buttons are pressed. Callback function form: void motion_func(int x, int y); Registration: glutMotionFunc(motion_func); GLUT Keyboard Event Trigger: Any key is depressed. Callback function form: void keyboard_function(unsigned char key, int x, int y); Registration: glutKeyboardFunc(keyboard_function); Idle Callbacks • Use for animation and continuous update glutIdleFunc( idle ); void idle( void ) { t += dt; glutPostRedisplay(); } Other Defined Events in GLUT glutPassiveMotionFunc glutVisibilityFunc glutEntryFunc glutSpecialFunc glutSpaceballMotionFunc glutSpaceballRotateFunc glutSpaceballButtonFunc glutButtonBoxFunc glutDialsFunc glutTabletMotionFunc glutTabletButtonFunc glutMenuStatusFunc Simple GLUT Window Management Functions glutInit(int *argc, char** argv); Initializes a window session. glutCreateWindow(char *name); Creates a window with title *name. glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); Sets the display mode to single buffered and RGB color. glutInitWindowSize (GLsizei h, GLsizei w); Sets initial window size to h x w. glutInitWindowPosition(x,y); Sets initial window position to (x, y). • TA help is there, but use “Help” and try to help yourselves !! • Show simple demo program OpenGL in MS Windows GL Commands OpenGL Lib GDI Processor Graphics Hardware GDI – Graphics Device Interface (Windows-specific) Basic Program Skeleton • • • • • • • Header files, Global variables Rendering initialization Keyboard / Mouse functions Drawing / animation Window resizing Idle function main() Try this … Draw a red line in a window with black background Try changing line thickness Change color combination What if you want a regular triangle as against a “filled” one Now try drawing various figures from the library Try modeling concave polygons