26
©Zachary Wartell, UNCC 9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights Reserved

©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

Embed Size (px)

Citation preview

Page 1: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 1

Overview of OpenGL

Revision: 1.2Copyright Professor Zachary Wartell, University of North Carolina

All Rights Reserved

Page 2: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 2

Graphics APIs

Generally these API’s support both 2D and 3D graphics:

●GKS (Graphics Kernal System)●PHIGS (Programmer Hierarchical Interactive Graphics Standard)●PHIGS+●GL (Graphics Library) by SGI●OpenGL●DirectDraw, Direct3D by Microsoft

Page 3: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 3

OpenGL

●OpenGL – hardware independent, only specifies graphics outputlow level, no GUI operations (events, windows, menu’s)

●OpenGL Utility (GLU) – higher level graphics primitives (spheres,curves, curved surfaces)

●OpenGL Utility Toolkit (GLUT) – primitive window setup & event handling

Page 4: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 4

OpenGL Block Diagram

• input commands– vertex coordinates and vertex attributes– pixel blocks– state changes (affects how input data is rendered)

• output commands– read current state, read framebuffer, etc.

commands

Page 5: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 5

OpenGL Basic Syntax (C Language Binding)

●Functions:glXXXX – glBegin, glEnd, glPolygonMode

●Constants:GL_XXXX – GL_2D, GL_RGB, GL_CCW

●Types:GLxxx – GLbyte, GLshort, GLint, GLfloat,

GLdouble, GLboolean

Page 6: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 6

OpenGL Geometric Output Primitives

●Types: GL_POINTS, GL_LINES, GL_LINE_LOOP, GL_LINE_STRIP,

GL_POLYGON, GL_QUADS, GL_QUAD_STRIP, GL_TRIANGLE, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN

●Basic Syntax:glBegin (PrimitiveConstant);

glVertex3f (x1,y1,z1)glVertex3f (x2,y2,z2) ….

glEnd();

glVertex(2|3|4)(s|i|d|f)[v]

Page 7: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 7

OpenGL: A State Machine

• OpenGL state remains in effect until it is changed• Examples: current - color, viewing/projection transforms,

line/polygon fill pattern, light positions, etc, etc, etc..• Each state variable has default value and can be queried

(glGetIntegerv, glGetFloatv, etc.) and enabled/disable• collections of state can also be saved (glPushAttrib,glPopAttrib)

commands

Page 8: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 8

Display Lists

• immediate vs retained mode• display list can collect geometry and attributes to be stored for

later use; allows optimizations• display list are write-only; no other structure within objects

(OpenInventor, Java3D, VRML, etc.)

commands

Page 9: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 9

Evaluators (3D stuff)

• a set of functions that generates vertex coordinates lying on various types of curves surfaces

• uses polynomial mapping. Can produce surface normals, texture coordinates, colors, vertex coordinates given a set of control points

commands

Page 10: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 10

Per-Vertex Operations

• vertices are transformed by 4x4 matrices (model&view, projection) in preparation for primitive assembly

• texture coordinates can be directly specified or generated and also transformed

• lighting calculations are also performed with transformed vertex and normals based on lighting attributes

commands

Page 11: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 11

Primitive Assembly

• points, lines, polygon edges are clipped and transformed to window coordinates

• resulting primitives are then passed to rasterization stage

commands

Page 12: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 12

Points & Lines

v0v1

v2

v3

glBegin (PrimitiveConstant);glVertex3f (x1,y1,z1)glVertex3f (x2,y2,z2) ….

glEnd();

v0

v1

v2

v3

v4

v5

GL_POINTSGL_LINES

GL_LINE_STRIP

v0

v1

v2

v3v4

GL_LINE_LOOP

Page 13: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 13

Polygons

glBegin (PrimitiveConstant);glVertex3f (x1,y1,z1)glVertex3f (x2,y2,z2) ….

glEnd();

GL_POLYGON

v0

v1 v

2

v3

v4

v0

v1v2

v3

v4v5

v6v7

GL_QUADS

v0

v1

v2

v3v4

v5

v6

v7

GL_QUAD_STRIP

Page 14: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 14©Zachary Wartell - 1/26/2005

Polygons (2)

glBegin (PrimitiveConstant);glVertex3f (x1,y1,z1)glVertex3f (x2,y2,z2) ….

glEnd();

v0v1

v2

v3

v4v5

GL_TRIANGLESGL_TRIANGLE_STRIP

v0

v1

v2

v3

v4

v5

GL_TRIANGLE_FAN

v0

v1v2

v3

v4v5

N verticesN/3 Triangles

N+2 verticesN Triangles

N+2 verticesN Triangles

Page 15: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 15

Why glBegin/glEnd?

●trying to define a separate function for each possible combinations of vertex data too cumbersome:

glBegin (PrimitiveConstant);glColor3f (c1,c1,c1)glTexCoord2f(t1,t2)glVertex3f (x1,y1,z1)

glColor3i (c1,c2,c3)

glNormal3f (n1,n2,n3)glVertex3f (x2,y2,z2) ….

glEnd();

-vertex uses last specifiedcolor/normal/texCoord. Allowsapplication to avoid redundantspecification, but every app. maywant different combinations

-Also lots of combinations of (s|i|f|d)[v] especially when combinedwith all possible combinationsof glColor ×glTexCoord × glNormal × glVertex

Page 16: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 16

Why glBegin/glEnd?

●allow maximum possible parallelism in host CPU and Graphics Processor

glBegin (PrimitiveConstant);compute x1,y1,z1 coordinateglVertex3f (x1,y1,z1)

compute x2,y2,z2 coordinateglVertex3f (x2,y2,z2) ….

glEnd();

CPU to GP communicationand GP processing of x1,y1,z1 canpotentially occur while CPU does this

Page 17: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 17

Vertex Arrays

●downside of glBegin/glEnd is lots of function calls

●OpenGL Vertex Arrays -drastically reduce function call overhead -support most common combinations of vertex data-allow sharing of vertex coordinates for adjacent primitives

glEnableClientState (GL_VERTEX_ARRAY);glVertexPointer ( 3 /* N-coordinates */,

GL_FLOAT /* coordinate type */, 0 /* stride */, vertex_coordinates);

glDrawElements (GL_QUADS /* primitive type */, 24, /* number of vertices */

GL_UNSIGNED_BYTE, /* type of index value */ vertex_indices );

-void* parameter, actual typedetermined by 3rd parameter

Page 18: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 18

Pixel Operations

• pixels packed or unpacked into RGB(A) components; also may be scaled, biased or transformed

• results are clamped and sent to texture memory OR rasterization

• pixel data also can be read from framebuffer

commands

Page 19: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 19

Pixel-Array Primitives

glBitmap (width, height, x0, y0, xOffset, yOffset, bit_array)

●width, height - size of bit map to draw (in last specified color)●x0, y0 – defines window coordinate of lower-left corner of

bit_array● xOffset, yOffset – added to current raster position

(see glRasterPos2i); useful for text output● bit_array – bits encode in unsigned byte array

Page 20: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 20

glBitmap

Figure 3-61: Hearn & Baker

Page 21: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 21

glDrawPixels

glDrawPixels (width, height, dataFormat, dataType, pixel_array)

●width, height - size of pixel array map to draw ●dataFormat – GL_RGB,GL_RED, GL_RGBA,

GL_DEPTH_COMPONENT, etc.●dataType – GL_UNSIGNED_BYTE, GL_BYTE, etc.●pixel_array – memory encoding data

-data write to current raster position (glRasterPos)

Page 22: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 22

OpenGL Raster Operations

●raster operations, bitblt’s (bit-block transfers), pixblt’s

●glReadPixels (xmin, ymin, width, height, dataFormat, dataType, pixel_array)-reads pixel data from framebuffer to main memory-xmin, ymin, width, height – rectangle in framebuffer to read-dataFormat – GL_RGB,GL_RED, GL_RGBA,

GL_DEPTH_COMPONENT, etc.-dataType – GL_UNSIGNED_BYTE, GL_BYTE, etc.-pixel_array – memory encoding data

●glCopyPixels – from glReadBuffer to glDrawBuffer

Page 23: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 23

Texture Assembly

• Textures (images + lots of texture rendering options) can be bound to texture objects; then you can switch between texture objects

• Multiple textures layers

commands

Page 24: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 24

Rasterization

• scan convert coordinate primitives (lines, polygons, etc.) or pixel data into fragments

• fragment is pixel color + depth value + stencil + accumulation

commands

Page 25: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 25

Fragment Operations

• sequence of operations that may discard fragment• blending, dithering, logic combination (and/or/xor)

commands

Page 26: ©Zachary Wartell, UNCC9/28/2006 11:30 AM 1 Overview of OpenGL Revision: 1.2 Copyright Professor Zachary Wartell, University of North Carolina All Rights

©Zachary Wartell, UNCC 9/28/2006 11:30 AM 26

Revisions

• Revision 1.1:

1. typos

2. Added copyright

3. formatted master Slide

1. Revision 1.2

1. added GL pipeline slides