Using OpenGL. 2 What is OpenGL? A software interface to graphics hardware It is a Graphics Rendering...

Post on 28-Dec-2015

240 views 2 download

Tags:

Transcript of Using OpenGL. 2 What is OpenGL? A software interface to graphics hardware It is a Graphics Rendering...

Using OpenGL

2

What is OpenGL?

• A software interface to graphics hardware • It is a Graphics Rendering API (Application

Programmer’s Interface) that is a set of function with well defined interface.

• OpenGL is intended for use with computer hardware that is designed and optimized for the display and manipulation of 3D graphics.

• Software-only implementations of OpenGL are also possible (MESA)

2

A History of OpenGL

• Was SGI’s Iris GL – basis for “Open”GL• “Open” standard allowing for wide range

hardware platforms• OpenGL v1.0 (1992)• OpenGL v1.1 (1995)• OpenGL v1.5

• “Mesa” – an Open source implementation of OpenGL (http://www.mesa3d.org)

4

The OpenGL ARB

• OpenGL Architecture Review Board (ARB)– Control license and conformance test of OpenGL

• The Khronos Group(after 2006) is industry consortium focused on the creation and maintenance of open media standards. Most ARB members are already members of Khronos.

• A vendor who wants to create and market an OpenGL implementation must first license OpenGL from The Khronos Group.

5

Useful Websites and Books

• Official Sitehttp://www.opengl.org

• Non official sites– http://nehe.gamedev.net/

• BOOKS – OpenGL Red Book & – OpenGL Blue Book

5

6

Useful Links

• Online Reference manual sites

• GL and GLUhttp://www.mevis.de/~uwe/opengl/opengl.html

• GLUThttp://pyopengl.sourceforge.net/documentation/manual/reference-

GLUT.html

6

7

OpenGL API Functions

• OpenGL contains over 200 functions – Primitive functions : define the elements (eg. A

point, line, polygon, etc)

– Attribute functions : control the appearance of primitives (eg. colors, line types, light source, textures.)

– Viewing functions : determine the properties of camera. Transformation

– Windowing functions: not part of core OpenGL

– Other functions

7

8

Window Management

• OpenGL is meant to be platform independent. i.e. OpenGL is window and operating system independent.

• OpenGL does not include any functions for window management, user interaction, and file I/O.

• Host environment is responsible for window management.

8

9

Related APIs

• GL– “core” library of OpenGL that is platform independent

• GLU (OpenGL Utility Library)– part of OpenGL

– an auxiliary library that handles a variety of graphics accessory functions

– NURBS, tessellators, quadric shapes, etc.

• AGL, GLX, WGL– glue between OpenGL and windowing systems

• GLUT (OpenGL Utility Toolkit)– utility toolkits that handle window managements – portable windowing API– not officially part of OpenGL

OpenGL API Hierarchy

GLUT, JOGL

GLU

GL

GLX, AGLor WGL

X, Win32, Mac O/S Java Virtual Machine

software and/or hardware

application program

OpenGL Motifwidget or similar

Programming Convention : OpenGL Function Naming

OpenGL functions all follow a naming convention that tells you which library the function is from, and how many and what type of arguments that the function takes

<Library prefix><Root command><Argument count><Argument type>

Programming Convention : OpenGL Function Naming

glVertex3fv( v )

Number ofcomponents

2 - (x,y) 3 - (x,y,z)4 - (x,y,z,w)

Data Typeb - byteub - unsigned bytes - shortus - unsigned shorti - intui - unsigned intf - floatd - double

Vector

omit “v” forscalar form

glVertex2f( x, y )

13

Programming Convention : OpenGL Function Naming

• Multiple forms of OpenGL functions to support the variety of data types– glVertex3i(ix, iy, iz)– glVertex3f(x, y, z)– glVertex2i(ix, iy)– glVertex2f(x, y)– ..– We shall use the notation glVertex*() to

refer to all the forms of the vertex function13

14

OpenGL Data Type Internal Representation

Literal Suffix

GLbyte 8-bit integer b

GLshort 16-bit integer s

GLint, GLsizei 32-bit integer i

GLfloat, GLclampf 32-bit floating point f

GLdouble, GLclampd 64-bit floating point d

GLubyte, GLboolean 8-bit unsigned integer

ub

GLushort 16bit unsigned integer

us

GLuint, GLenum, GLbitfield

32bit unsigned integer

ui

15

General Structure of an OpenGL Program

Configure and open a

window Initialize OpenGL’s

state Process user events

Draw an image

16

2D Geometric Primitives

• Primitives – fundamental entities such as point and polygons

• Basic types of geometric primitives– Points– Line segments– Polygons

16

17

2D Geometric Primitives

17

GL_POINTS GL_LINES GL_LINE_STRIP

GL_LINE_LOOP

GL_POLYGON

GL_QUADS GL_TRIANGLES

GL_TRIANGLE_FAN

All geometric primitives are specified by vertices

18

Geometry Commands

• glBegin(GLenum type)

marks the beginning of a vertex-data list that describes a geometric primitives

18

• glEnd (void)

marks the end of a vertex-data list

• glVertex*(…)

specifies vertex for describing a geometric object

19

Specifying Geometric Primitives

glBegin( type ); glVertex*(…); …… glVertex*(…);glEnd();

19

type determines how vertices are combined

20

Types

21

TypesGL_POINTSGL_LINES : each successive pair for a ling segmentGL_LINE_STRIP: vertices defining a sequence of line segmentsGL_LINE_LOOP: GL_LINE_STRIP + the last vertex connects to

the firstGL_POLYGON : sequence of vertices of polygon, filledGL_QUADS: each successive group of four vertices for a

quadrilateralsGL_TRIANGLES: each successive group of three vertices for a

triangleGL_TRIANGLE_FAN: first three vertices for the first triangle

and each subsequent vertex with the first vertex and the previous vertex for the next triangle

21

22

Example

22

void drawSquare (){ glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glVertex2f ( 0.0, 0.0 ); glVertex2f ( 1.0, 0.0 ); glVertex2f ( 1.1, 1.1 ); glVertex2f ( 0.0, 1.0 ); glEnd(); glFlush(); // force the renderer to output the results }

23

Winding

• glFrontFace(GL_CCW);

24

Triangle Strips

• When drawing several connected triangles, you can save a lot of time by drawing a strip of connected triangles

• The pattern is V0, V1, V2; then V2, V1, V3; then V2, V3, V4; and so on.

25

Triangle Fans

• Produce a group of connected triangles that fan around a central point

• The first vertex, V0, forms the origin of the fan.• After the first three vertices are used to draw the

initial triangle, all subsequent vertices are used with the origin (V0) and the vertex immediately preceding it (Vn–1) to form the next triangle.

26

How OpenGL Works:The Conceptual Model

Configurehow OpenGLshould draw

stuff

Draw stuff

27

Controlling OpenGL’s Drawing

• Set OpenGL’s rendering state– State controls how things are drawn

•shading – lighting•texture maps – line styles

(stipples)•polygon patterns – transparency

28

The Power of Setting OpenGL State

Appearance is controlled by setting OpenGL’s state.

29

Setting OpenGL State

• Three ways to set OpenGL state:1. Set values to be used for processing

vertices• most common methods of setting state

– glColor() / glIndex()– glNormal()– glTexCoord()

• state must be set before calling glVertex()

30

Setting OpenGL State (cont’d.)

2.2. Turning on a rendering modeTurning on a rendering modeglEnable() / glDisable()

3.3. Configuring the specifics of a particular Configuring the specifics of a particular rendering moderendering mode• Each mode has unique commands for setting its

values

glMaterialfv()

31

OpenGL Color

• There are two color models in OpenGL– RGB Color (True Color)– Indexed Color (Color map)

• Colors are specified as floating-pointnumbers in the range [ 0.0, 1.0 ]

31

32

RGB Color Model

32

•R, G, B components are stored for each pixel

33

How Many Colors?

Color number =

33

For example:

4-bit color

= 16 colors

8-bit color

= 256 colors

24-bit color

= 16.77 million colors

hcolor_dept2

42

82

242

34

How Much Memory?

Buffer size = width * height *color depth

34

For example:

If width = 640, height = 480, color depth = 24 bits

Buffer size = (640 * 480 * 2) bytes

If width = 640, height = 480, color depth = 32 bits

Buffer size = (640 * 480 * 4) bytes

35

Alpha Component

Alpha value A value indicating the pixels opacity 0 usually represents totally transparent and

the 1 represents completely opaque

Alpha buffer Hold the alpha value for every pixel Alpha values are commonly represented in 8

bits, in which case transparent to opaque ranges from 0 to 255

35

36

RGB Color Commands

• glColor*(…)

specifies vertex colors

36

• glClearColor(r, g, b, a)

sets current color for cleaning color buffer

37

Example

37

void drawLine (GLfloat *color){ glColor3fv ( color ); glBegin(GL_LINE); glVertex2f ( 0.0, 0.0 ); glVertex2f ( 1.0, 0.0 ); glEnd();}

38

Example

38

void drawLine (GLfloat *color){ glBegin(GL_LINE);

glColor3f(1.0,0.0,0.0 ); glVertex2f ( 0.0, 0.0 );

glColor3f(0.0,0.0,1.0);

glVertex2f ( 1.0, 0.0 ); glEnd();}

39

Color Interpolation

glShadeModel(GL_SMOOTH);

OrglShadeModel(GL_FLAT); - the last

vertex color

• Linear interpolation for a line• Bilinear interpolation for a polygons

39