opengl programmingpraba/4392/openglprogramming.pdf · opengl has a state and associated state...

38
OpenGL Programming

Upload: others

Post on 18-Aug-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

OpenGL Programming

Page 2: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Page 3: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Page 4: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Page 5: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Page 6: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Page 7: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

Computer Graphics Pipeline

Geometric Model Rendered Image

OpenGL Rendering Algorithm

Page 8: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

The Visualization Problem

Page 9: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

The Visualization Problem

Page 10: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

The Visualization Problem

Page 11: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Page 12: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Page 13: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

How does OpenGL work?OpenGL pipeline:

Model Transformation Projection Rasterization Display

Page 14: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

How does OpenGL work?OpenGL pipeline:

Model Transformation Projection Rasterization Display

• Map from WCS to CCS

• Culling• Lighting

Page 15: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Page 16: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Page 17: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Page 18: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

How does OpenGL work?OpenGL pipeline:

Model Transformation Projection Rasterization Display

Page 19: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Constant symbols: GL_GL_RGB, GL_POLYGON, GL_LINES, …

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

Page 20: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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)

Page 21: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Page 22: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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!

Page 23: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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()

Page 24: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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();

Page 25: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

Specifying Primitives

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

Shapes.exe

Page 26: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Page 27: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

OpenGL PrimitivesWhy triangles, quads, and strips?

Hardware may be more efficient for trianglesStrips require processing less data

fewer glVertex calls

Page 28: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Specular: dependent on angle (shiny)

lightmaterial.exe

Page 29: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

Primitives: Light SourcesPoint light sources are common:

lightposition.exe

Page 30: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

TransformationglTranslateglRotateglScaleglPushMatrix(); glPopMatrix();

transformation.exe

Page 31: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

Camera ViewsDifferent views of an object in the world

Page 32: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Page 33: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

Camera ViewsMany camera parameters…For a physical camera

PositionOrientationLens (field of view)

Page 34: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

Camera ProjectionsOrthographic projection

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

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

Page 35: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Page 36: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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

Page 37: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

ClippingNot everything is visible on the screen

Page 38: OpenGL Programmingpraba/4392/OpenGLProgramming.pdf · OpenGL has a state And associated state variables (line width, color, … etc.) eg.GL_FLAT & GL_SMOOTH are two states of GL_SHADE_MODEL

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