28.08.2013 Views

Introduction to OpenGL Prof. Dr.-Ing. Lars Linsen - Faculty.jacobs ...

Introduction to OpenGL Prof. Dr.-Ing. Lars Linsen - Faculty.jacobs ...

Introduction to OpenGL Prof. Dr.-Ing. Lars Linsen - Faculty.jacobs ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Jacobs University<br />

<strong>Introduction</strong> <strong>to</strong> <strong>OpenGL</strong><br />

<strong>Prof</strong>. <strong>Dr</strong>.-<strong>Ing</strong>. <strong>Lars</strong> <strong>Linsen</strong><br />

School of Engineering and Science<br />

Jacobs University Bremen<br />

320621: Advanced Visualization Lab<br />

Visualization and Computer Graphics Lab


Jacobs University<br />

1. What is <strong>OpenGL</strong>?<br />

Visualization and Computer Graphics Lab


Jacobs University<br />

Graphics Programming<br />

• Graphical user interface (GUI)<br />

• Windowing system<br />

– X<br />

– Integrated in OS: Microsoft Windows, Mac OS<br />

• Microsoft Direct3D<br />

• <strong>OpenGL</strong> (Open Graphics Library)<br />

• <strong>OpenGL</strong> 2.0 (and higher)<br />

– GLSL (GL Shading Language)<br />

• Cg (nVidia)<br />

• HLSL (high level shader language, DirectX)<br />

• GPGPU programming: CUDA (nVidia), CTM (ATI),<br />

OpenCL<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 3


Jacobs University<br />

What is <strong>OpenGL</strong>?<br />

• <strong>OpenGL</strong> is a software interface for 3D computer graphics<br />

• Originally developed by SGI (IRIS GL)<br />

since 1992 under control of ARB (Architecture Review Board)<br />

• <strong>OpenGL</strong> specification is<br />

• hardware-independent,<br />

• window system independent and<br />

• operating system independent.<br />

• Different implementations (as hardware and/or software)<br />

•C library<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 4


CPU<br />

Jacobs University<br />

<strong>OpenGL</strong> Architecture<br />

Polynomial<br />

Evalua<strong>to</strong>r<br />

Display<br />

List<br />

Pixel<br />

Operations<br />

Per Vertex<br />

Operations &<br />

Primitive<br />

Assembly<br />

Rasterization<br />

Texture<br />

Memory<br />

Per Fragment<br />

Operations<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 5<br />

Frame<br />

Buffer


Jacobs University<br />

<strong>OpenGL</strong> as a Renderer<br />

• Geometric primitives<br />

– points, lines, and polygons<br />

• Image Primitives<br />

– images and bitmaps<br />

• Separate pipeline for images and geometry<br />

– linked through texture mapping<br />

• Rendering depends on state<br />

– colors, materials, light sources, etc.<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 6


Setting State<br />

Controlling current state<br />

Jacobs University<br />

glPointSize( size );<br />

glLineStipple( repeat, pattern );<br />

glShadeModel( GL_SMOOTH );<br />

Enabling Features<br />

glEnable( GL_LIGHTING );<br />

glDisable( GL_TEXTURE_2D );<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 7


Number of<br />

components<br />

2 - (x,y)<br />

3 - (x,y,z)<br />

4 - (x,y,z,w)<br />

<strong>OpenGL</strong> Command Formats<br />

Jacobs University<br />

glVertex3fv( v )<br />

Data Type<br />

b - byte<br />

ub - unsigned byte<br />

s - short<br />

us - unsigned short<br />

i - int<br />

ui - unsigned int<br />

f - float<br />

d - double<br />

Vec<strong>to</strong>r<br />

omit “v” for<br />

scalar form<br />

glVertex2f( x, y )<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 8


• AGL, GLX, WGL<br />

Jacobs University<br />

Related APIs<br />

– glue between <strong>OpenGL</strong> and windowing systems<br />

• GLU (<strong>OpenGL</strong> Utility Library)<br />

– part of <strong>OpenGL</strong><br />

– NURBS, tessella<strong>to</strong>rs, quadric shapes, etc.<br />

• GLUT (<strong>OpenGL</strong> Utility Toolkit)<br />

– portable windowing API<br />

– not officially part of <strong>OpenGL</strong><br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 9


<strong>OpenGL</strong> and Related APIs<br />

<strong>OpenGL</strong> Motif<br />

widget or similar<br />

Jacobs University<br />

GLX, AGL<br />

or WGL<br />

X, Win32, Mac O/S<br />

application program<br />

GLUT<br />

GLU<br />

software and/or hardware<br />

GL<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 10


• Headers Files<br />

• #include <br />

• #include <br />

• #include <br />

• Libraries<br />

• Enumerated Types<br />

Jacobs University<br />

Preliminaries<br />

– <strong>OpenGL</strong> defines numerous types for compatibility<br />

– GLfloat, GLint, GLenum, etc.<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 11


Jacobs University<br />

2. Getting Started<br />

Visualization and Computer Graphics Lab


Getting started with <strong>OpenGL</strong><br />

Installation of Mesa3D: http://www.mesa3d.org<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 13


Getting started with <strong>OpenGL</strong><br />

Installation of Mesa3D: http://www.mesa3d.org<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 14


Jacobs University<br />

Setting up GLUT<br />

• To use GLUT its header file has <strong>to</strong> be included in the<br />

source:<br />

– #include <br />

• The program has <strong>to</strong> be linked against the GLUT library<br />

– For GCC just compile with -lglut<br />

– For MSVC and other of the sort add glut <strong>to</strong> the libraries in<br />

the project options<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 15


• Application Structure<br />

– Configure and open window<br />

Jacobs University<br />

GLUT Basics<br />

– Initialize <strong>OpenGL</strong> state<br />

– Register input callback functions<br />

• render<br />

• resize<br />

• input: keyboard, mouse, etc.<br />

– Enter event processing loop<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 16


Jacobs University<br />

Initializing GLUT<br />

• void glutInit(int argc, char **argv);<br />

– Initializes GLUT library. Should be called before any other<br />

routine. Processes window system specific command line<br />

arguments.<br />

• void glutInitDisplayMode(unsigned int mode);<br />

– Sets the GLUT display mode for windows created with<br />

glutCreateWindow()<br />

– The display mode is a bitwise or combination of GLUT_RGBA<br />

or GLUT_INDEX, GLUT_SINGLE or GLUT_DOUBLE, and any<br />

of the buffer-enabling flags GLUT_DEPTH, GLUT_STENCIL,<br />

or GLUT_ACCUM.<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 17


Jacobs University<br />

Initializing GLUT<br />

• void glutInitWindowSize(int width, int height);<br />

– width and height of the window<br />

• void glutInitWindowPosition(int x, int y);<br />

– Sets the coordinates of the <strong>to</strong>p-left corner<br />

• int glutCreateWindow(char *name);<br />

– Creates a window with the name name and returns a unique<br />

window identifying integer (handle). Useful when rendering is<br />

done in multiple windows of the same application.<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 18


Handling events with GLUT<br />

• Callback functions are registered <strong>to</strong> the corresponding<br />

event, i.e., when the respective event takes place, the<br />

function is called <strong>to</strong> handle it.<br />

• Window events:<br />

– void glutDisplayFunc(void (*func)(void));<br />

• The function given as argument is called whenever the window has<br />

<strong>to</strong> be redrawn<br />

– void glutReshapeFunc(void (*func)(int width, int height));<br />

• The function is called when the window is resized. Width and<br />

height are the new width and height of the resized window.<br />

• When no function is specified (NULL), a default one is assigned.<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 19


Handling Events with GLUT<br />

• Input events<br />

– void glutKeyboardFunc(void (*func)(unsigned int key, int x, int y);<br />

• The function is called when a key is pressed. Key is the ASCII of<br />

the pressed key, x and y are the mouse coordinates (window<br />

relative) at the moment the key is pressed<br />

– void glutSpecialFunc(void (*func)(int key, int x, int y));<br />

• Triggered when a special key is pressed (F1..F12, arrows, Insert,<br />

Home, etc.).<br />

– void glutMouseFunc(void (*func)(int but<strong>to</strong>n, int state, int x, int y));<br />

• Triggered when a mouse but<strong>to</strong>n is pressed or released. But<strong>to</strong>n is left,<br />

right or middle, state is down or up, x and y are as above.<br />

– void glutMotionFunc(void (*func)(int x, int y));<br />

• Specifies the function that is called when the mouse is moved while<br />

one or more but<strong>to</strong>ns are pressed.<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 20


Handling Events with GLUT<br />

• Redisplaying<br />

– void glutPostRedisplay(void);<br />

• Posts a message <strong>to</strong> the queue that redisplaying is required. At the<br />

first opportunity, the function is called.<br />

• Idling<br />

– void glutIdleFunc(void (*func)(void));<br />

• Function is called when nothing is happening. Sometimes this<br />

function is set <strong>to</strong> the displaying function and timed <strong>to</strong> force a<br />

certain FPS<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 21


Jacobs University<br />

Running the Application<br />

• Entering the process loop<br />

– void glutMainLoop(void);<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 22


First example: Main function (1)<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 23


Jacobs University<br />

Main function (2)<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 24


Jacobs University<br />

Reshape function<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 25


Jacobs University<br />

Keyboard function<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 26


Jacobs University<br />

Display function (1)<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 27


Jacobs University<br />

Display function (2)<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 28


Jacobs University<br />

Display function (3)<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 29


Jacobs University<br />

3. Viewing<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 30


Jacobs University<br />

Camera analogy<br />

• 3D is just like taking a pho<strong>to</strong>graph (lots of<br />

pho<strong>to</strong>graphs!)<br />

camera<br />

tripod<br />

viewing<br />

volume<br />

model<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 31


Camera analogy and transformations<br />

• Projection transformations<br />

– adjust the lens of the camera<br />

• Viewing transformations<br />

– tripod–define position and orientation of the viewing volume in the<br />

world<br />

• Modeling transformations<br />

– moving the model<br />

• Viewport transformations<br />

– enlarge or reduce the physical pho<strong>to</strong>graph<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 32


Programming transformations<br />

• Prior <strong>to</strong> rendering, view, locate, and orient:<br />

– eye/camera position<br />

– 3D geometry<br />

• Manage the matrices<br />

– including matrix stack<br />

• Combine (composite) transformations<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 33


v<br />

e<br />

r<br />

t<br />

e<br />

x<br />

Modelview<br />

Matrix<br />

Modelview<br />

Modelview<br />

<br />

<br />

<br />

Jacobs University<br />

Transformation pipeline<br />

object eye clip normalized<br />

device<br />

Projection<br />

Matrix<br />

Projection<br />

CPU<br />

CPU<br />

Perspective<br />

Division<br />

Per<br />

Poly. Per<br />

Poly. Vertex<br />

Vertex<br />

DL<br />

DL<br />

Pixel<br />

Pixel<br />

Texture<br />

Texture<br />

Viewport<br />

Transform<br />

• other calculations here<br />

– material color<br />

– shade model (flat)<br />

– polygon rendering mode<br />

– polygon culling<br />

– clipping<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 34<br />

Raster<br />

Raster<br />

Frag<br />

Frag<br />

window<br />

FB<br />

FB


Coordinate systems and transformations<br />

• Steps in forming an image<br />

– specify geometry (world coordinates)<br />

– specify camera (camera coordinates)<br />

– project (window coordinates)<br />

– map <strong>to</strong> viewport (screen coordinates)<br />

• Each step uses transformations<br />

• Every transformation is equivalent <strong>to</strong> a change in coordinate<br />

systems (frames)<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 35


Homogeneous coordinates<br />

– each vertex is a column vec<strong>to</strong>r<br />

– w is usually 1.0<br />

– all operations are matrix multiplications<br />

– directions (directed line segments) can be represented with w = 0.0<br />

Jacobs University<br />

⎡x<br />

⎤<br />

⎢<br />

y<br />

⎥<br />

v = ⎢ ⎥<br />

⎢z<br />

⎥<br />

⎢ ⎥<br />

⎣w⎦<br />

r<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 36


M<br />

=<br />

Jacobs University<br />

3D transformations<br />

• A vertex is transformed by 4 x 4 matrices<br />

– all affine operations are matrix multiplications<br />

– all matrices are s<strong>to</strong>red column-major in <strong>OpenGL</strong><br />

– matrices are always post-multiplied<br />

– product of matrix and vec<strong>to</strong>r is<br />

v M<br />

⎡m<br />

⎢<br />

m<br />

⎢<br />

⎢m<br />

⎢<br />

⎣m<br />

0<br />

1<br />

2<br />

3<br />

m<br />

m<br />

m<br />

m<br />

4<br />

5<br />

6<br />

7<br />

m<br />

m<br />

m<br />

m<br />

8<br />

9<br />

10<br />

11<br />

m<br />

m<br />

m<br />

m<br />

⎤<br />

⎥<br />

⎥<br />

⎥<br />

⎥<br />

⎦<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 37<br />

12<br />

13<br />

14<br />

15


Specifying transformations<br />

• Specify Current Matrix Stack<br />

glMatrixMode(<br />

glMatrixMode(<br />

GL_MODELVIEW or GL_PROJECTION )<br />

• Programmer has two styles of specifying<br />

transformations<br />

– specify matrices (glLoadMatrix, glLoadMatrix, glMultMatrix)<br />

glMultMatrix<br />

– specify operation (glTranslate<br />

glTranslate, , glRotate, glRotate,<br />

glOrtho)<br />

glOrtho<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 38


Projection Transformation<br />

•Shape of viewing frustum<br />

• Perspective projection<br />

gluPerspective( fovy, aspect, zNear, zFar )<br />

glFrustum( left, right, bot<strong>to</strong>m, <strong>to</strong>p, zNear, zFar )<br />

• Orthographic parallel projection<br />

glOrtho( left, right, bot<strong>to</strong>m, <strong>to</strong>p, zNear, zFar )<br />

gluOrtho2D( left, right, bot<strong>to</strong>m, <strong>to</strong>p )<br />

calls glOrtho with z values near zero<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 39


Jacobs University<br />

Orthogonal projection<br />

• void glOrtho(GLdouble left, GLdouble right, GLdouble<br />

bot<strong>to</strong>m, GLdouble <strong>to</strong>p, GLdouble near, GLdouble far);<br />

– Creates a matrix for an orthographic parallel view and<br />

multiplies it by the current matrix<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 40


Applying projection transformations<br />

• Typical use (orthographic projection)<br />

glMatrixMode( GL_PROJECTION );<br />

glLoadIdentity();<br />

glOrtho( left, right, bot<strong>to</strong>m, <strong>to</strong>p, zNear, zFar );<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 41


Jacobs University<br />

Perspective projection<br />

• void glFrustum(GLdouble left, Gldouble right, Gldouble<br />

bot<strong>to</strong>m, Gldouble <strong>to</strong>p, Gldouble near, Gldouble far);<br />

– Creates a matrix for the frustrum and multiplies the current<br />

matrix by it. Near and far are the distances <strong>to</strong> the clipping<br />

planes<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 42


Jacobs University<br />

Perspective projection<br />

• More intuitive:<br />

void gluPerspective(GLdouble fovy, Gldouble aspect,<br />

Gldouble near, Gldouble far);<br />

– fovy is the field of view in x-z plane. Aspect is the aspect ratio<br />

width/height<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 43


Jacobs University<br />

Viewing transformations<br />

• Position the camera/eye in the scene<br />

– place the tripod down; aim camera<br />

• To “fly through” a scene<br />

– change viewing transformation and<br />

redraw scene<br />

gluLookAt( eye x , eye y , eye z ,<br />

aim x , aim y , aim z ,<br />

up x , up y , up z )<br />

– up vec<strong>to</strong>r determines unique orientation<br />

– careful of degenerate positions<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 44<br />

tripod


Jacobs University<br />

Viewing transformation<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 45


Modeling transformations<br />

• Move object<br />

glTranslate{fd}( x, y, z )<br />

• Rotate object around arbitrary axis<br />

glRotate{fd}(<br />

glRotate{fd}(<br />

angle, x, y, z )<br />

– angle is in degrees<br />

• Dilate (stretch or shrink) or mirror object<br />

glScale{fd}( x, y, z )<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 46


Jacobs University<br />

Viewing and modeling<br />

• Moving camera is equivalent <strong>to</strong> moving every object in<br />

the world <strong>to</strong>wards a stationary camera<br />

• Viewing transformations are equivalent <strong>to</strong> several<br />

modeling transformations<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 47


Viewport Transformations<br />

• Viewport<br />

– usually same as window size<br />

– can be used <strong>to</strong> see part of the screen <strong>to</strong> which is rendered<br />

– viewport aspect ratio should be same as projection transformation or<br />

resulting image may be dis<strong>to</strong>rted<br />

glViewport(<br />

glViewport<br />

glViewport( ( x, y, width, height )<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 48


Jacobs University<br />

Matrix Operations<br />

• Specify Current Matrix Stack<br />

glMatrixMode( GL_MODELVIEW or GL_PROJECTION )<br />

• Other Matrix or Stack Operations<br />

glLoadIdentity() glPushMatrix()<br />

glPopMatrix()<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 49


Jacobs University<br />

Matrix stacks<br />

• The matrices manipulated by all operations discussed<br />

so far are the ones that sit on the <strong>to</strong>p of the matrix<br />

stack.<br />

• When a matrix is pushed on<strong>to</strong> the stack, the current<br />

<strong>to</strong>pmost matrix is duplicated, so that first and second<br />

matrices contain the same entries.<br />

• When popping a matrix, the <strong>to</strong>pmost matrix is deleted<br />

and the second one becomes the <strong>to</strong>pmost one.<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 50


Matrix stacks - some applications<br />

• Modelview matrices<br />

– E.g.: Save the current position, go somewhere else, draw<br />

something, return <strong>to</strong> the previous position, go elsewhere, draw<br />

the same thing scaled by a fac<strong>to</strong>r of 2, return <strong>to</strong> the initial<br />

position, …<br />

– The stack may contain at least 32 matrices (depends on the<br />

<strong>OpenGL</strong> implementation)<br />

• Projection matrices<br />

– E.g. useful when a text box needs <strong>to</strong> be displayed (since text is<br />

best viewed in orthogonal projection and realistic applications<br />

are using perspective projections).<br />

– At least 2 matrices (again depends on implementation)<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 51


4. Object Representation<br />

Jacobs University<br />

Visualization and Computer Graphics Lab


Points:<br />

Jacobs University<br />

OPenGL primitive types<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 53


Jacobs University<br />

OPenGL primitive types<br />

Line segments & sequences thereof:<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 54


Jacobs University<br />

<strong>OpenGL</strong> primitive types<br />

Convex simple polygons & meshes:<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 55


Using <strong>OpenGL</strong> primitives<br />

• Primitives are specified using<br />

glBegin( primType );<br />

glEnd();<br />

– primType is any of the primitive types and determines how<br />

vertices are combined.<br />

GLfloat red, green, blue;<br />

Glfloat coords[3];<br />

glColor3f( red, green, blue );<br />

glBegin( glBegin(<br />

primType );<br />

for ( i = 0; i < nVerts; ++i ) {<br />

glVertex3fv( coords );<br />

}<br />

glEnd();<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 56


Using <strong>OpenGL</strong> Primitives<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 57


Using <strong>OpenGL</strong> Primitives<br />

Jacobs University<br />

glBegin(GL_POLYGON);<br />

glVertex2f(0.0, 0.0);<br />

glVertex2f(0.0, 3.0);<br />

glVertex2f(4.0, 3.0);<br />

glVertex2f(6.0, 1.5);<br />

glVertex2f(4.0, 0.0);<br />

glEnd();<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 58


Jacobs University<br />

GLUT objects<br />

• void glutWireSphere(GLdouble radius, GLint slices, GLint stacks);<br />

void glutSolidSphere(GLdouble radius, GLint slices, GLint stacks);<br />

• void glutWireCube(GLdouble size);<br />

void glutSolidCube(GLdouble size);<br />

• void glutWireTorus(GLdouble innerRadius, GLdouble outerRadius,<br />

GLint nsides, GLint rings);<br />

• void glutSolidTorus(GLdouble innerRadius, GLdouble outerRadius,<br />

GLint nsides, GLint rings);<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 59


Jacobs University<br />

GLUT objects<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 60


Jacobs University<br />

GLUT objects<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 61


Jacobs University<br />

GLUT objects<br />

• void glutWireIcosahedron(void);<br />

void glutSolidIcosahedron(void);<br />

• void glutWireOctahedron(void);<br />

void glutSolidOctahedron(void);<br />

• void glutWireTetrahedron(void);<br />

void glutSolidTetrahedron(void);<br />

• void glutWireDodecahedron(GLdouble radius);<br />

void glutSolidDodecahedron(GLdouble radius);<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 62


Jacobs University<br />

GLUT objects<br />

• void glutWireCone(GLdouble radius, GLdouble height, GLint slices,<br />

GLint stacks);<br />

void glutSolidCone(GLdouble radius, GLdouble height, GLint slices,<br />

GLint stacks);<br />

• void glutWireTeapot(GLdouble size);<br />

void glutSolidTeapot(GLdouble size);<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 63


Jacobs University<br />

GLUT objects<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 64


Jacobs University<br />

5. Displaying<br />

Visualization and Computer Graphics Lab


CPU<br />

CPU<br />

Jacobs University<br />

DL<br />

DL<br />

Rasterization<br />

Per<br />

Poly. Per<br />

Poly. Vertex<br />

Vertex<br />

Pixel<br />

Pixel<br />

Texture<br />

Texture<br />

Raster<br />

Raster<br />

Frag<br />

Frag<br />

FB<br />

FB<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 66


Front<br />

Buffer<br />

1<br />

2 4 8 16<br />

Jacobs University<br />

Double Buffering<br />

1<br />

2 4 8 16<br />

Display<br />

Back<br />

Buffer<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 67


Animation Using Double Buffering<br />

Request a double buffered color buffer<br />

glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );<br />

Clear color buffer<br />

glClear( GL_COLOR_BUFFER_BIT );<br />

Render scene<br />

Request swap of front and back buffers<br />

glutSwapBuffers();<br />

Repeat steps 2 - 4 for animation<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 68


An Updated Program Template (cont.)<br />

• • void drawScene( void )<br />

{<br />

GLfloat vertices[] = { … };<br />

GLfloat colors[] = { … };<br />

glClear( GL_COLOR_BUFFER_BIT);<br />

• glBegin( GL_TRIANGLE_STRIP );<br />

/* calls <strong>to</strong> glColor*() and glVertex*() */<br />

glEnd();<br />

glutSwapBuffers();<br />

}<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 69


Jacobs University<br />

Antialiasing<br />

• Removing the Jaggies<br />

• glEnable( mode )<br />

• GL_POINT_SMOOTH<br />

• GL_LINE_SMOOTH<br />

• GL_POLYGON_SMOOTH<br />

– alpha value computed by computing<br />

sub-pixel coverage<br />

– available in both RGBA and colormap modes<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 70


Full Scene Antialiasing : Jittering the view<br />

• Each time we move the viewer, the image shifts<br />

– Different aliasing artifacts in each image<br />

– Averaging images using accumulation<br />

buffer averages out<br />

these artifacts<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 71


Jacobs University<br />

Blending<br />

• Combine pixels with what’s in already<br />

in the framebuffer<br />

• glBlendFunc( src, dst )<br />

v<br />

C<br />

r<br />

=<br />

Fragment<br />

(src src)<br />

src<br />

v<br />

C<br />

f<br />

+<br />

Framebuffer<br />

Pixel<br />

(dst dst)<br />

dst<br />

v<br />

C<br />

p<br />

Blending<br />

Equation<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 72<br />

Blended<br />

Pixel


– operations<br />

Jacobs University<br />

Accumulation Buffer<br />

• glAccum( op, value )<br />

• within the accumulation buffer: GL_ADD, GL_MULT<br />

• from read buffer: GL_ACCUM, GL_LOAD<br />

• transfer back <strong>to</strong> write buffer: GL_RETURN<br />

– glAccum(GL_ACCUM, 0.5) multiplies each value in write<br />

buffer by 0.5 and adds <strong>to</strong> accumulation buffer<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 73


Jacobs University<br />

Accumulation Buffer<br />

• Problems of compositing in<strong>to</strong> color buffers<br />

– limited color resolution<br />

• clamping<br />

• loss of accuracy<br />

– Accumulation buffer acts as a “floating point” color buffer<br />

• accumulate in<strong>to</strong> accumulation buffer<br />

• transfer results <strong>to</strong> frame buffer<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 74


Jacobs University<br />

Multi-pass Rendering<br />

• Blending allows results from multiple drawing passes<br />

<strong>to</strong> be combined <strong>to</strong>gether<br />

– enables more complex rendering algorithms<br />

Example of bump-mapping<br />

done with a multi-pass<br />

<strong>OpenGL</strong> algorithm<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 75


Color<br />

Buffer<br />

1<br />

2 4 8 16<br />

Jacobs University<br />

Depth Buffering<br />

1<br />

2 4 8 16<br />

Display<br />

Depth<br />

Buffer<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 76


Depth Buffering Using <strong>OpenGL</strong><br />

Request a depth buffer<br />

glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE |<br />

GLUT_DEPTH );<br />

Enable depth buffering<br />

glEnable( GL_DEPTH_TEST );<br />

Clear color and depth buffers<br />

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );<br />

Render scene<br />

Swap color buffers<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 77


Jacobs University<br />

6. Colors & Shading<br />

Visualization and Computer Graphics Lab


Specifying Geometric Primitives<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 79


How <strong>OpenGL</strong> Simulates Lights<br />

• Phong lighting model<br />

– Computed at vertices<br />

• Lighting contribu<strong>to</strong>rs<br />

– Surface material properties<br />

– Light properties<br />

– Lighting model properties<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 80


Jacobs University<br />

Surface Normals<br />

• Normals define how a surface reflects light<br />

• glNormal3f( x, y, z )<br />

– Current normal is used <strong>to</strong> compute vertex’s color<br />

– Use unit normals for proper lighting<br />

• scaling affects a normal’s length<br />

glEnable( GL_NORMALIZE )<br />

or<br />

glEnable( GL_RESCALE_NORMAL )<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 81


Jacobs University<br />

Material Properties<br />

• Define the surface properties of a primitive<br />

• glMaterialfv( face, property, value );<br />

GL_DIFFUSE Base color<br />

GL_SPECULAR Highlight Color<br />

GL_AMBIENT Low-light Color<br />

GL_EMISSION Glow Color<br />

GL_SHININESS Surface Smoothness<br />

– separate materials for front and back<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 82


Jacobs University<br />

Light Properties<br />

• glLightfv( light, property, value );<br />

– light specifies which light<br />

• multiple lights, starting with GL_LIGHT0<br />

glGetIntegerv( GL_MAX_LIGHTS, &n );<br />

– properties<br />

• colors<br />

• position and type<br />

• attenuation<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 83


• Light color properties<br />

– GL_AMBIENT<br />

– GL_DIFFUSE<br />

– GL_SPECULAR<br />

Jacobs University<br />

Light Colors<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 84


Jacobs University<br />

Types of Lights<br />

• <strong>OpenGL</strong> supports two types of Lights<br />

– Local (Point) light sources<br />

– Infinite (Directional) light sources<br />

• Type of light controlled by w coordinate<br />

w =<br />

0<br />

w ≠ 0<br />

Infinite<br />

Local<br />

Light<br />

Light<br />

directed<br />

positioned<br />

along<br />

at<br />

( x y z)<br />

( )<br />

x y z<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 85<br />

w<br />

w<br />

w


• Light attenuation<br />

Jacobs University<br />

Attenuation<br />

– decrease light intensity with distance<br />

• GL_CONSTANT_ATTENUATION<br />

• GL_LINEAR_ATTENUATION<br />

• GL_QUADRATIC_ATTENUATION<br />

f<br />

i<br />

=<br />

k<br />

c<br />

+<br />

k<br />

l<br />

1<br />

d + k<br />

q<br />

d<br />

2<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 86


• Flip each light’s switch<br />

Jacobs University<br />

Turning on the Lights<br />

glEnable( GL_LIGHTn );<br />

• Turn on the power<br />

glEnable( GL_LIGHTING );<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 87


Jacobs University<br />

Light & Material<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 88


• Spotlights<br />

Advanced Lighting Features<br />

– localize lighting affects<br />

• GL_SPOT_DIRECTION<br />

• GL_SPOT_CUTOFF<br />

• GL_SPOT_EXPONENT<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 89


• Setting State<br />

Jacobs University<br />

Shading<br />

glShadeModel(<br />

glShadeModel<br />

glShadeModel( ( GL_SMOOTH<br />

GL GL_SMOOTH SMOOTH | GL_FLAT);<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 90


Jacobs University<br />

8. Textures<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 91


CPU<br />

CPU<br />

• Apply a 1D, 2D, or 3D image <strong>to</strong> geometric<br />

primitives<br />

Jacobs University<br />

Texture Mapping<br />

Per<br />

Poly. Per<br />

Poly. Vertex<br />

Vertex<br />

DL<br />

DL<br />

Pixel<br />

Pixel<br />

Texture<br />

Texture<br />

Raster<br />

Raster<br />

Frag<br />

Frag<br />

FB<br />

FB<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 92


Texture Mapping and the <strong>OpenGL</strong> Pipeline<br />

• Images and geometry flow through separate pipelines<br />

that join at the rasterizer<br />

– “complex” textures do not affect geometric complexity<br />

vertices<br />

image<br />

Jacobs University<br />

geometry pipeline<br />

pixel pipeline<br />

rasterizer<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 93


• Three steps<br />

specify texture<br />

Jacobs University<br />

Applying Textures<br />

• read or generate image<br />

• assign <strong>to</strong> texture<br />

• enable texturing<br />

assign texture coordinates <strong>to</strong> vertices<br />

specify texture parameters<br />

• wrapping, filtering<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 94


Jacobs University<br />

Texture Objects<br />

• Like display lists for texture images<br />

– one image per texture object<br />

– may be shared by several graphics contexts<br />

• Generate texture names<br />

glGenTextures( n, *texIds );<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 95


Texture Objects (cont.)<br />

• Create texture objects with texture data and state<br />

• Bind textures before using<br />

glBindTexture( target, id );<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 96


Jacobs University<br />

Specify Texture Image<br />

• Define a texture image from an array of<br />

texels in CPU memory<br />

glTexImage2D( target, level, components,<br />

w, h, border, format, type, *texels );<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 97


• Based on parametric texture coordinates<br />

• glTexCoord*() specified at each vertex<br />

t<br />

0, 1<br />

a<br />

b<br />

Jacobs University<br />

1, 1<br />

0, 0 1, 0<br />

Mapping a Texture<br />

Texture Space Object Space<br />

c<br />

s<br />

(0.4, 0.2)<br />

(s, t) = (0.2, 0.8)<br />

A<br />

B C<br />

(0.8, 0.4)<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 98


Jacobs University<br />

Example<br />

// Loading a Texture, specifying properties<br />

glGenTextures(1, &texName);<br />

glBindTexture(GL_TEXTURE_2D, texName);<br />

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);<br />

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);<br />

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,<br />

GL_UNSIGNED_BYTE, Image_Buffer);<br />

// later while describing polygons...<br />

glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0);<br />

glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0);<br />

…<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 99


Jacobs University<br />

Example<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 100


Jacobs University<br />

Applying Textures<br />

– specify textures in texture objects<br />

– set texture filter<br />

– set texture function<br />

– set texture wrap mode<br />

– set optional perspective correction hint<br />

– bind texture object<br />

– enable texturing<br />

– supply texture coordinates for vertex<br />

• coordinates can also be generated<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 101


• Filter Modes<br />

– minification or magnification<br />

– special mipmap minification filters<br />

• Wrap Modes<br />

Texture Application Methods<br />

– clamping or repeating (tiling)<br />

• Texture Functions<br />

– how <strong>to</strong> mix primitive’s color with texture’s color<br />

• blend, modulate or replace texels<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 102


Jacobs University<br />

Filter Modes<br />

Example:<br />

glTexParameteri( target, type, mode );<br />

Texture Polygon<br />

Texture Polygon<br />

Magnification Minification<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 103


Jacobs University<br />

Mipmapped Textures<br />

• Mipmap allows for prefiltered texture maps of decreasing resolutions<br />

• Lessens interpolation errors for smaller textured objects<br />

• Declare mipmap level during texture definition<br />

glTexImage*D( GL_TEXTURE_*D, level, … )<br />

• GLU mipmap builder routines<br />

gluBuild*DMipmaps( … )<br />

• <strong>OpenGL</strong> 1.2 introduces advanced LOD controls<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 104


Jacobs University<br />

Wrapping Mode<br />

• Example:<br />

glTexParameteri( GL_TEXTURE_2D,<br />

GL_TEXTURE_WRAP_S, GL_CLAMP )<br />

glTexParameteri( GL_TEXTURE_2D,<br />

GL_TEXTURE_WRAP_T, GL_REPEAT )<br />

t<br />

texture<br />

s<br />

GL_REPEAT<br />

wrapping<br />

GL_CLAMP<br />

wrapping<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 105


Jacobs University<br />

Texture Functions<br />

• Controls how texture is applied<br />

• glTexEnv{fi}[v]( GL_TEXTURE_ENV, prop, param )<br />

• GL_TEXTURE_ENV_MODE modes<br />

– GL_MODULATE<br />

– GL_BLEND<br />

– GL_REPLACE<br />

• Set blend color with GL_TEXTURE_ENV_COLOR<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 106


Perspective Correction Hint<br />

• Texture coordinate and color interpolation<br />

– either linearly in screen space<br />

– or using depth/perspective values (slower)<br />

• Noticeable for polygons “on edge”<br />

• glHint( GL_PERSPECTIVE_CORRECTION_HINT, hint )<br />

where hint is one of<br />

• GL_DONT_CARE<br />

• GL_NICEST<br />

• GL_FASTEST<br />

Jacobs University<br />

Visualization and Computer Graphics Lab<br />

320621: Advanced Visualization Lab 107


Jacobs University<br />

Done.<br />

Visualization and Computer Graphics Lab

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!