Upload
morgan-knight
View
255
Download
4
Embed Size (px)
Citation preview
1
Programming With OpenGL
A Technical Overview of The OpenGL Graphics API
Computer Graphics
2
Graphics API•OpenGL(All Platform)
•DirectX(Windows)
-DirectDraw-Direct3D
-DirectSound-DirectPlay-DirectInput-DirectSetup
3
Programming With OpenGL
Computer Graphics
OpenGL, the standard software interface for graphics hardware, allows programmers to create interactive 2D and 3D graphics applications on a variety of systems. With OpenGL you can create high-quality color images. OpenGL makes it easy to build geometric models, change the viewing position, control the color and lighting of geometric primitives, and manipulate pixel and texture map images. This course will cover an immediately applicable subset of OpenGL, so that you can write a simple graphics program.
An Introduction
4
Programming With OpenGL
Computer Graphics
OpenGL Advantages
• Standard & Portability - Open• OpenGL is the most logical extension of brilliant API called GL
• Supported through a larger number of vendors• 1992, SGI• SGI, IBM, DEC, Intel, Microsoft,
• Vendor and hardware independent• Better integration into the windowing system
•Win95, Win98, WinNT, Win2000,WinXP
5
Objectives• Become familiar with the capabilities of OpenGL• Understand the order of operations, and the major libraries• Know how to use viewing, lighting, shading, and hidden surface removal functionality• Know how to draw images with OpenGL and process event-driven input• See how code is written and compiled
Programming With OpenGL
Computer Graphics
6
Programming With OpenGL
Computer Graphics
What is OpenGL?
• A low-level graphics modeling and rendering library (includes more than 100 functions)
• State machine• A layer of abstraction between graphics
hardware and an application program• Hardware, Window System and Operating
System independent
7
Programming With OpenGL
Computer Graphics
Where is in the Graphics SystemDeveloping &Application
OpenInventor, SoftImage 3D
Window System
Operating System
Graphics Hardware
OpenGL
X-Windows, MS Windows …
UNIX, MS Windows, OS/2...
SGI-XZ, SGI-Extreme, AGC-3D..
8
Programming With OpenGL
Computer Graphics
What OpenGL Can Do For You
• Wireframe Models, Viewing, Transformation• Antialiasing• Flat And Smooth Shading• Shadows• Texture Mapping• Motion Blur• Haze and Fog, Depth -Of -Field Effect• Evaluator Function: 1D & 2D Bezier Curves, NUR
BS ...
9
Programming With OpenGL
Computer Graphics
Graphics Functions• Primitive functions
• Attribute functions
• Viewing functions
• Transformation functions
• Input functions / control functions
10
Programming With OpenGL
Computer Graphics
Overview of OpenGL Components
• OpenGL Library - gl• OpenGL Utilities - glu• OpenGL Extension to X Windows - glx• OpenGL Programming Guide Auxiliary Lib
rary - aux• Windows Graphics Library - wgl
11
OpenGL Library GLProgramming With OpenGL
Computer Graphics
• Powerful But Primitive Set Of Rendering Commands• All high level drawing is done in terms of these commands• You can write your own toolkit based on these functions
• The GL Library Includes• Functions supporting drawing primitives
• Lines, Points, Circles, Polygons, Characters, ...• Clipping, Viewing, Projections, Transformation• Shading, Lighting• Direct colors• 3D rendering functions such as
• Antialiasing, Multi buffering, Texture, ...• Pixels manipulation (read, write, copy)
12
Programming With OpenGL
Computer Graphics
OpenGL Utility Library GLU• Utility library is a set of commonly used graphics routines
• built on top of OpenGL• Uses lower level OpenGL commands for
• Setting up matrixes for viewing and projection• Performing polygon tessellation
- concave polygons, polygons with holes, self- intersecting polygons, ..• Rendering surfaces (spheres, cylinders, disks, ..)• Mipmapping• NURBS (Non-Uniform Rational B-Spline) curves• Error handling
• This library is part of the OpenGL implementation
13
OpenGL Auxiliary Library• This is a simple, platform-dependent library• Makes programming examples simpler & more complete
• Managing windows• Handling input events• Drawing classic 3-D objects• Managing a background process• Running a program
• Do not use them in a production application
Programming With OpenGL
Computer Graphics
14
OpenGL Command Syntax• OpenGL commands use :
– the prefix gl– initial capital letters for each word making up the command name
• OpenGL define constants– begin with GL– use all capital letters– use underscore to separate words
• OpenGL uses command extensions for clarification– whenever the same command exists to use different arguments
• Examples :– glClearColor()– GL_COLOR_BUFFER_BIT– glColor3fv()
Programming With OpenGL
Computer Graphics
15
Programming With OpenGL
Computer Graphics
OpenGL Command Syntax• � glVertex3fv v indicates vector format, if present data type: f float d double float s signed short integer i signed integer number of components (2, 3, or 4)• Other data types in OpenGL commands - b character - ub unsigned character - us unsigned short integer - ui unsinged integer
16
Programming With OpenGL
Computer Graphics
Drawing Geometry• Vertices can define a variety of geometric objects, and
different numbers of vertices are required depending on the object.
• We can group as many vertices as desired, using the functions glBegin() and glEnd()
• Types of primitives glBegin (GLenum primitiveType)• Example: glBegin (GL_POLYGON); glVertex3f(-. 5f, -. 5f, -. 5f); glVertex3f(. 5f, -. 5f, -. 5f); glVertex3f(-. 5f, .5f, -. 5f); glVertex3f(. 5f, .5f, -. 5f); glEnd();
17
Programming With OpenGL
Computer Graphics
Primitive Types
18
Attributes• An attribute is any property that determines how a geometric primitive is to be rendered
• The present values of attribute are part of the state of the graphics system• Each geometric type has a set of attributes
Programming With OpenGL
Computer Graphics
19
Attributes• Vertex attributes: glPointSize() point size glColor*()/ glIndex*() current vertex color glNormal*() current vertex normal (lighting) glMaterial*() current material property (lighting) glTexCoord*() current texture coordinate glEdgeFlag*() edge status (surface primitives)• RGB colors: glColor3f(1.0,0.0,0.0) glClearColor(1.0,1.0,1.0,1.0)• Indexed color: glIndexi(element) Alpha channel
- opacity or transparency
Computer Graphics
Programming With OpenGL
20
Programming With OpenGL
Computer Graphics
Drawing Geometry• Example: Drawing a green, flat triangle strip: glBegin (GL_ TRIANGLE_ STRIP); glColor3f( 0. f, 1. f, 0. f); glNormal3f( 0. f, 0. f, 1. f); glVertex3f(-. 5f, -. 5f, -. 5f); glVertex3f(. 5f, -. 5f, -. 5f); glVertex3f(-. 5f, .5f, -. 5f); glVertex3f(. 5f, .5f, -. 5f); glEnd();
• glBegin - glEnd paradigm allows great flexibility in describing primitives.
• Any combination of color, normal, texture, material etc. information can be bound with any given vertex.
21
OpenGL The Statemachine• OpenGL knows about various different states or modes• These states remain in effect until you change them• Examples of states variables are:
– the current color– current viewing and transformation matrix– line and polygon stippling patterns– polygon drawing modes– shading modes– pixel packing conventions– position and characteristics of light sources– material properties
• Each state variable has a default value• You can query the current variable value
Programming With OpenGL
Computer Graphics
22
Programming With OpenGL
Computer Graphics
States
• glEnable (GLenum capability)
• glDisable (GLenum capability)
• GLboolean glIsEnabled (GLenum cap)
- turn on and off OpenGL states
- capability can be one of (partial list): GL_ BLEND (alpha blending)
GL_ DEPTH_ TEST (depth buffer)
GL_ FOG
GL_ LIGHTING
GL_ LINE_ SMOOTH (line antialiasing)
23
Programming With OpenGL
Computer Graphics
Structure of a Typical Program
main: create window initialize GL states (e. g., viewing, color, lighting) initialize display lists check for events (and process them) if window event (window moved, exposed, etc.) - modify viewport, if needed - redraw else if mouse or keyboard - do something, e. g., change states & redraw
24
Initialization• void auxInitWindow(Glbyte *titleString)
• void auxInitDisplayMode(Glbitfield mask)
• void auxInitPosition(Glint x,Glint y, Glsizei width,Glsizei height)
Mask: AUX_RGBA or AUX_INDEX AUX_SINGLE or AUX_DOUBLE AUX_DEPTH AUX_STENCIL AUX_ACCUM
Computer Graphics
Programming With OpenGL
- Win32 API: CreateWindow(…)
- Win32 API: MoveWindow(…)
25
Control Functions
• void auxMainLoop(void(*displayFunc)void))
-Windows message: WM_PAINT• void auxReshapeFunc(void(*function) (GLsizei,Glsizei))
-Windows message: WM_SIZE
• void auxMouseFunc(Glint button,Glint mode, void(*function)(AUX_EVENTREC*))
- Windows Message: WM_MOUSEDOWN
Computer Graphics
Programming With OpenGL
26
Programming With OpenGL
Computer Graphics
Redrawredraw: clear screen (to background color) change state( s), if needed render some graphics change more states render some more graphics ....
27
Homework: The Sierpinski Gasket1. Pick an initial point at random inside the
triangle.
2. Select one of the three vertices at random.
3. Find the point halfway between the initial point and the randomly selected vertex.
4. Display this new point by putting some sort of marker, such as a small circle, at its location.
5. Replace the initial point with this new point.
6. Return to step 2.
Programming With OpenGL
V1
V2
V3
P0
P1 P2
Computer Graphics