OpenGL Introduction

Preview:

Citation preview

3D Computer GraphicsOpenGL

David Kabala

Why Cool?

Computer Graphics

Raster Graphics Images are created

from pixel data Vector Graphics

Images are created from primitives of points, lines, curves, and other shapes

Final image is computed by processing this representation

OpenGL

OpenGL is a mixture of Vector and Raster graphics Vector graphics

polygons, surface normals, colors, etc. Raster graphics

textures(images) that are mapped onto surfaces Rasterizing vector graphics into frame buffer

Most platforms have an implementation of OpenGL Windows, Linux, Mac, iPhone, Android, PS3,

and many more

OpenGL Rendering Pipeline

VectorOperations

Rasterization

FragmentOperations

FrameBuffer

Data

OpenGL Rendering Pipeline

OpenGL Rendering Pipeline

ATI R600 Pipeline

OpenGL Changing with 3.0

Many parts of the OpenGL API have been deprecated in version 3.0, and removed in version 3.1

This includes much of the fixed functionality of the pipeline Lighting

Programing these portions in the pipeline is the path forward

OpenGL as a state machine

OpenGL is organized for functional programming not object-oriented

Stays in a persistent state until it receives a message that tells it to change

Objects are bound Changes are made to the state

Color, lighting, line width, etc

Immediate draw mode

OpenGL API

All methods are prefixed with gl e.g. glEnable(GLbool), glVertex2i(GLint,GLint)

All Types are prefixed with GL e.g. GLbool, GLint, GLflaot, GLenum

All constants are prefixed with GL_ e.g. GL_POINTS, GL_CULL_FACE, GL_QUADS

OpenGL defined types

C/C++ leave the number of bytes of each type up to the compiler writer

OpenGL has defined types that are the same size in all implementations

Important Types GLshort

16 bit integer GLint

32 bit integer GLfloat

32 bit floating point GLdouble

64 bit floating point GLboolean

8 bit unsigned integer GLenum

32 bit unsigned integer

Drawing

Primitives are drawn by sending vertex positions inside of a glBegin and glEnd block

Example:void display(void){ //Setup state to start drawing //points glBegin(GL_POINTS); //Draw a point at 10,10 glVertex2i(10,10); glEnd();}

glBegin/glEnd block

OpenGL receives primitive geometry data “inside” a glBegin and glEnd block

glBegin(GLenum primitiveType) primitiveType argument takes a GLenum value

of the type of primitives to drawvoid display(void){ glBegin(GL_POINTS); glVertex2i(10,10);

glVertex2i(20,10);

glVertex2i(0,0); glEnd();

glVertex2i(10,0); //Will not draw}

GlVertex variations

Some sets of OpenGL methods take multiple numbers and types of arguments

glVertex2i

Type of argumentNumber of arguments

glColor4f

Type SuffixGLbyte bGLshort s

i

f

d

ubGLushort us

ui

GLint, GLsizeiGLfloat, GLclampfGLdouble, GLclampdGLubyte, GLboolean

GLuint, GLenum, GLbitfield

Primitives

Points

Lines

Triangles

Quadrilaterals

Polygons

Points

send GL_POINTS as parameter to glBegin Every glVertex produces a single point

Changing point drawing parameters glPointSize(GLint size);

void display(void){ //Setup state to start drawing //points glPointSize(5.0); glBegin(GL_POINTS); //Draw a point at 10,10 glVertex2i(10,10); glEnd();}

Lines

Line Draw Modes GL_LINES

GL_LINE_STRIP

Changing line width glLineWidth(GLint size);

GL_LINE_LOOP

v1 v2

v2

v1

v3

v4

v1

v2

v3

v4

v5

Triangles

GL_TRIANGLES

GL_TRIANGLE_STRIP

GL_TRIANGLE_FAN

Quads

GL_QUADS

GL_QUAD_STRIP

Polygons

GL_POLYGONS

Must be convex Must be non-intersecting Separate polygons

must be in separateglBegin/glEnd blocks

v1

v2

v3

v4

v5

v1

v2 v3

v4

v5

v6v6

v8

Next Time

Using GLUT (OpenGL Utility) to manage window and context creation

Transformations using matrix and vector algebra

Recommended