Post on 23-Dec-2015
transcript
Today …
• The rendering pipeline in detail
• What is OpenGL
• First OpenGL program
• Details about modeling
Basic Rendering Pipeline
Projection
Illumination
Rasterization&Clipping &Display
1V
1R
N
L
Database of 3D models
Modeling Transformation(OS -> WS)
VisibilityCulling
ViewingTransformation (WS -> CS)
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
•OpenGL applications use the window system’s window, input, and event mechanism •GLU supports quadrics, NURBS, complex polygons, matrix utilities, and more
Relationship between OpenGL GLU and windowing APIs.
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
GLU
GL
GLUT
GL library contains all primitive and attribute functionsassociated with OpenGL
GLU library builds on the GL library to include morecomplex primitives (e.g. spheres) and convenience functions
GLUT (GL Utility Toolkit) includes functions to interfacewith 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
GLUT
GLU
GL
GLX, AGLor WGL
X, Win32, Mac O/S
software and/or hardware
application program
OpenGL Motifwidget or similar
OpenGL Architecture
DisplayList
PolynomialEvaluator
Per VertexOperations &
PrimitiveAssembly
RasterizationPer Fragment
OperationsFrameBuffer
TextureMemory
CPU
PixelOperations
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()functionvoid 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 triangleglBegin(GL_TRIANGLES);
glColor3f( 1.0, 0.0, 0.0 );glVertex2f( 1.0, 1.0 );glVertex2f( 2.0, 1.0 );glVertex2f( 2.0, 2.0 );
glEnd();
// Flush the pipeline. (Not usually necessary.) glFlush();}
OpenGL Command Formats
glVertex3fv( glVertex3fv( vv ) )
Number ofNumber ofcomponentscomponents
2 - (x,y) 2 - (x,y) 3 - (x,y,z)3 - (x,y,z)4 - (x,y,z,w)4 - (x,y,z,w)
Data TypeData Typeb - byteb - byteub - unsigned byteub - unsigned bytes - shorts - shortus - unsigned shortus - unsigned shorti - inti - intui - unsigned intui - unsigned intf - floatf - floatd - doubled - double
VectorVector
omit “v” foromit “v” forscalar formscalar form
glVertex2f( x, y )glVertex2f( x, y )
Specifying Geometric Primitives• Primitives are specified using
glBegin( glBegin( primType primType ););
glEnd();glEnd();
– primType determines how vertices are combinedGLfloat red, greed, blue;GLfloat red, greed, blue;Glfloat coords[3];Glfloat coords[3];glBegin( glBegin( primType primType ););for ( i = 0; i < nVerts; ++i ) { for ( i = 0; i < nVerts; ++i ) { glColor3f( red, green, blue );glColor3f( red, green, blue ); glVertex3fv( coords );glVertex3fv( coords );}}glEnd();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 D ata Type Typ ica l C orresponding C -Language Type
O penG L Type D efin ition
b 8-b it in teger s igned char G Lbyte
s 16-b it in teger short G Lshort
f 32-b it in teger in t o r long G Lin t, G Lbyte
i 32-b it floating -po in t floa t G Lfloat, G Lclam pf
d 64-b it floating-po in t doub le G Ldouble ,G Lclam pd
ub 8-b it unsigned in teger unsinged char G Lubyte,G Lboolean
us 16-b it unsinged in teger unsinged short G Lushort
u i 32-b it unsinged in teger unsinged in t o r unsinged long
G Luin t, G Lenum , G Lbitfie ld
Shared Namespace ModelModel
Application
Graphics Lib.
O I
FunctionCalls
OS
} Run in same process
glFlush();unnecessary
Client-Server Model:
GraphicsServer
O
I
Network Protocol
Model
Application
Graphics Lib.
FunctionCalls
OS OS
glFlush();May be necessary
OpenGL Architecture
DisplayList
PolynomialEvaluator
Per VertexOperations &
PrimitiveAssembly
RasterizationPer Fragment
OperationsFrameBuffer
TextureMemory
CPU
PixelOperations
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 GLUTglutDisplayFunc( display );
glutIdleFunc( idle );
glutKeyboardFunc( keyboard );
A Typical Event-Driven Program
Initialization
Main Loop
InputProcessingProcedures
BackgroundProcessingProcedure
EventHandler
Route messages to the application
EventButton on Mouse
Windows Route Event to Correct Application
Windows Application
WinMain()
WinProc()
Message
Left Button Down
Message Processed
CallFunction n
GLUT Event Processing
Eventin
Queue
Pop EventFrom Queue
CallFunction n
Switch onEvent Type
CallFunction n
Call IdleFunction
No
Yes
CallFunction nCall
Function nCallFunction n
Callbacks
Event A
Event B
Event C
Event D
Event E
Event F
No Event Idle function( )
m1, m2 function_A(m1, m2)
function_B( )
function_C( )
Event Type “Registered” Function
NB: Function parameters must match measures contained in associated event.
Display EventTrigger: GLUT determines that the window needs to be redisplayed. A display event is generated when the windowis 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_BUTTONGLUT_MIDDLE_BUTTONGLUT_RIGHT_BUTTON
GLUT_UPGLUT_DOWN
Systems with only one mouse button can only generatea 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 mousebuttons 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 updateglutIdleFunc( idle );
void idle( void ){ t += dt; glutPostRedisplay();}
Other Defined Events in GLUTglutPassiveMotionFuncglutVisibilityFuncglutEntryFuncglutSpecialFuncglutSpaceballMotionFuncglutSpaceballRotateFuncglutSpaceballButtonFuncglutButtonBoxFuncglutDialsFuncglutTabletMotionFuncglutTabletButtonFuncglutMenuStatusFunc
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 Lib
GL Commands
GDI Processor
GraphicsHardware
OpenGL in MS Windows
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 backgroundTry changing line thicknessChange color combinationWhat if you want a regular triangle as against a “filled” oneNow try drawing various figures from the libraryTry modeling concave polygons