38
OpenGL Programming

OpenGL Programming

Embed Size (px)

Citation preview

OpenGL Programming

What is OpenGL?A low level graphics API for 2D and 3D interactive graphics. OS independent.Descendent of GL (from SGI)

OpenGLContains a library of over 200 functionsPortable

Implementations available for nearly all hardware and operating systemsInput or windowing are not included in OpenGL

Options for Windows: GLUT, FLTK, or MFCGLUT = OpenGL Utility ToolkitImplementations of GLUT exist for most computing environment

Controlled by the OpenGL Architecture Review Board

SGI, IBM, nVidia, ATI, … -- some major players in CG

What OpenGL isn’t:A windowing program or input driver, since those couldn’t be OS independent.

GL: core graphics capabilityGLU: utilities on top of GLGLUT: input and windowing functions

GLUOpenGL Utility – GLU

All OpenGL implementations include GLUFunctions are with prefix gluSetting up viewing and projection matricesDescribing complex surfaces using line and polygon approximationsDisplaying quadrics and B-spline surfaces using linear approximations

GLUTOpenGL Utility Toolkit – GLUT

Provides a library functions for interacting with any screen-window systemFunctions are with prefix glutThe source code are available at:http://www.opengl.org/resources/libraries/glut/

Installation:1. Put header file (.h) in the GL/ include directory of Visual

Studio2. Put library file (.lib) in the lib directory3. Put dynamic link library (.dll) in the system directory

Computer Graphics Pipeline

Geometric Model Rendered Image

OpenGL Rendering Algorithm

The Visualization Problem

The Visualization Problem

The Visualization Problem

How does OpenGL work?From the programmer’s point of view:

Specify geometric objectsDescribe object propertiesDefine how they should be viewedMove camera or objects around for animation

How does OpenGL work?State machine with input and output

State variables: color, current viewing position, line width, material properties,…The variables (the state) then apply to every subsequent drawing commandInput is description of geometric objectOutput is pixels sent to the display

How does OpenGL work?OpenGL pipeline:

Model Transformation Projection Rasterization Display

How does OpenGL work?OpenGL pipeline:

Model Transformation Projection Rasterization Display

• Map from WCS to CCS

• Culling• Lighting

How does OpenGL work?OpenGL pipeline:

Model Transformation Projection Rasterization Display

• Map from WCS to CCS

• Culling• Lighting

• Map from CCSto Screen CS

• Clipping• Persp. division

How does OpenGL work?OpenGL pipeline:

Model Transformation Projection Rasterization Display

• Map from WCS to CCS

• Culling• Lighting

• Map from CCSto Screen CS

• Clipping• Persp. division

• Color• Visibility

How does OpenGL work?OpenGL pipeline:

Model Transformation Projection Rasterization Display

• Map from WCS to CCS

• Culling• Lighting

• Map from CCSto Screen CS

• Clipping• Persp. division

• Color• Visibility

• Frame BufferDisplay

How does OpenGL work?OpenGL pipeline:

Model Transformation Projection Rasterization Display

Basic OpenGL SyntaxPrefix: glglBegin, glEnd, glClear, glPolygonMode, …

Constant symbols: GL_GL_RGB, GL_POLYGON, GL_LINES, …

Data types: GLGLbyte, GLshort, GLint, GLfloat, GLdouble, …

OpenGL Commands: a Quick Look

Just function calls:glColor3f (1.0, 1.0, 1.0);

Same command, different arguments:glColor3ub(255,255,255); -- same result

GL prefix

Command name

Type suffix (if variable),can also end with “v”

Number of arguments (if variable)

OpenGL Data Typesb 8 GLbyte signed chars 16 GLshort shorti 32 GLint, GLsizei int / longf 32 GLfloat, GLclampf floatd 64 GLdouble, GLclampd doubleub 8 GLubyte, Glboolean unsigned charus 16 GLushort unsigned shortui 32 GLuint, GLenum,

GLbitfieldunsigned int

OpenGL as a State MachineOpenGL has a state

And associated state variables (line width, color, … etc.)eg. GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

Some states are binary states that are either GL_FALSE or GL_TRUEEach state variable has a default value.

Changing statesFor binary state variables, use glEnable/glDisable (eg. GL_LIGHTING)Mode state variables require specific commands (eg. glShadeModel)Value state variables require specific commands too. (eg. glColor3f)

Remains in effect until changed!

State QueriesChecking if enabled: glIsEnabled(GLenum cap)

Cap: symbolic constant indicating an OpenGL capability (eg. GL_DEPTH_TEST)Returns Glboolean

Getting state variable valueglGetIntegerv(pname, params)Stores value of pname at location papramsSimilarly, glGetBooleanv, glGetFloatv, etc.

More specific queries for some important variablesglGetString(GL_VERSION)glGetLight()glGetError()

Primitives: drawing a polygonPut GL into draw-polygon state:glBegin (GL_POLYGON);

Send it the points making up the polygon:glVertex2f (x0, y0);glVertex2f (x1, y1);glVertex2f (x2, y2); …

Tell it we’re finishedglEnd();

Specifying Primitives

Code for all of the examples are available from:http://www.xmission.com/~nate/tutors.html

Shapes.exe

OpenGL PrimitivesGeometric object is described by the type of the primitive to be drawn and a set of vertices.

GL_POINTSGL_LINES, GL_LINE_STRIP, GL_LINE_LOOPGL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FANGL_QUADS, GL_QUAD_STRIPGL_POLYGON

OpenGL PrimitivesWhy triangles, quads, and strips?

Hardware may be more efficient for trianglesStrips require processing less data

fewer glVertex calls

Primitives: Light Material PropertiesAmbient: same at every point on the surfaceDiffuse: scattered light independent of angle (rough)

Specular: dependent on angle (shiny)

lightmaterial.exe

Primitives: Light SourcesPoint light sources are common:

lightposition.exe

TransformationglTranslateglRotateglScaleglPushMatrix(); glPopMatrix();

transformation.exe

Camera ViewsDifferent views of an object in the world

Camera ViewsLines from each point on the image are draw through the center of the camera lens (the center of projection – COP)

Camera ViewsMany camera parameters…For a physical camera

PositionOrientationLens (field of view)

Camera ProjectionsOrthographic projection

Long telephoto lensFlat but preserving distances and shapes. All the projectors are parallel.

glOrtho (left, right, bottom, top, near, far);

Camera ProjectionsPerspective projectionExample: pin hole cameraObjects farther away are smaller in size

Camera TransformationsCamera positioning just results in more transformations on the objects:

Transformations that position the object relative to the camera

Example:void gluLookAt (eyex, eyey, eyez, centerx, center y, centerz, upx, upy, upz);

projection.exe

ClippingNot everything is visible on the screen

RasterizerTransform pixel values in the projected coordinates to pixel values in screen (raster) coordinates -- glViewport(x,y,width, height)