18.07.2014 Views

An Interactive Introduction to OpenGL Programming

An Interactive Introduction to OpenGL Programming

An Interactive Introduction to OpenGL Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>An</strong> <strong>Interactive</strong> <strong>Introduction</strong> <strong>to</strong><br />

<strong>OpenGL</strong> <strong>Programming</strong><br />

Course # 29<br />

Dave Shreiner<br />

Ed <strong>An</strong>gel<br />

Vicki Shreiner


Welcome


Welcome<br />

• Today’s Goals and Agenda<br />

– Describe <strong>OpenGL</strong> and its uses<br />

– Demonstrate and describe <strong>OpenGL</strong>’s<br />

capabilities and features<br />

– Enable you <strong>to</strong> write an interactive, 3-D<br />

computer graphics program in <strong>OpenGL</strong>


What Is <strong>OpenGL</strong>, and What<br />

Can It Do for Me?<br />

• <strong>OpenGL</strong> is a computer graphics rendering API<br />

– Generate high-quality color images by rendering with<br />

geometric and image primitives<br />

– Create interactive applications with 3D graphics<br />

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

• operating system independent<br />

• window system independent


Related APIs<br />

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

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

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

• AGL, GLX, WGL<br />

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

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

– portable windowing API<br />

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


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

application program<br />

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

widget or similar<br />

GLX, AGL<br />

or WGL<br />

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

GLUT<br />

GLU<br />

GL<br />

software and/or hardware


What Is Required For Your<br />

Programs<br />

• Headers Files<br />

#include <br />

#include <br />

#include <br />

#include <br />

• Libraries<br />

• Enumerated Types<br />

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

•GLfloat, GLint, GLenum, etc.


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

Formats<br />

glVertex3fv( v )<br />

Number of<br />

components<br />

2 - (x,y)<br />

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

4 - (x,y,z,w)<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 )


The <strong>OpenGL</strong> Pipeline<br />

Vertex<br />

Processing<br />

Fragment<br />

Processing<br />

Framebuffer<br />

• Processing is controlled by setting<br />

<strong>OpenGL</strong>’s state<br />

– colors, lights and object materials, texture<br />

maps<br />

– drawing styles, depth testing


<strong>An</strong> Example <strong>OpenGL</strong> Program


Sequence of Most <strong>OpenGL</strong><br />

Programs<br />

Configure<br />

and open a<br />

window<br />

Initialize<br />

<strong>OpenGL</strong>’s<br />

state<br />

Process<br />

user events<br />

Update<br />

<strong>OpenGL</strong>’s<br />

State<br />

(if necessary)<br />

Draw an<br />

image


<strong>An</strong> <strong>OpenGL</strong> Program<br />

#include <br />

#include "cube.h"<br />

void main( int argc, char *argv[] )<br />

{<br />

glutInit( &argc, argv );<br />

glutInitDisplayMode( GLUT_RGBA |<br />

GLUT_DEPTH );<br />

glutCreateWindow( “cube” );<br />

}<br />

init();<br />

glutDisplayFunc( display );<br />

glutReshapeFunc( reshape );<br />

glutMainLoop();<br />

The main part of<br />

the program.<br />

GLUT is used <strong>to</strong><br />

open the <strong>OpenGL</strong><br />

window, and handle<br />

input from the user.


<strong>An</strong> <strong>OpenGL</strong> Program<br />

(cont’d.)<br />

void init( void )<br />

{<br />

glClearColor( 0, 0, 0, 1 );<br />

gluLookAt( 2, 2, 2, 0, 0, 0, 0, 1, 0 );<br />

glEnable( GL_DEPTH_TEST );<br />

}<br />

Set up some initial<br />

<strong>OpenGL</strong> state<br />

void reshape( int width, int height )<br />

{<br />

glViewport( 0, 0, width, height );<br />

glMatrixMode( GL_PROJECTION );<br />

Handle when the<br />

glLoadIdentity();<br />

user resizes the<br />

gluPerspective( 60, (GLdouble) width / height, window<br />

1.0, 10.0 );<br />

glMatrixMode( GL_MODELVIEW );<br />

}


<strong>An</strong> <strong>OpenGL</strong> Program<br />

(cont’d.)<br />

void display( void )<br />

{<br />

int i, j;<br />

glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );<br />

glBegin( GL_QUADS );<br />

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

glColor3fv( faceColor[i] );<br />

for ( j = 0; j < NUM_VERTICES_PER_FACE; ++j ) {<br />

glVertex3fv( vertex[face[i][j]] );<br />

}<br />

}<br />

glEnd();<br />

Have <strong>OpenGL</strong><br />

draw a cube<br />

from some<br />

3D points<br />

(vertices)<br />

}<br />

glFlush();


GLUT Callback Functions<br />

• Routine <strong>to</strong> call when something happens<br />

– window resize or redraw<br />

– user input<br />

– animation<br />

• “Register” callbacks with GLUT<br />

glutDisplayFunc( display );<br />

glutIdleFunc( idle );<br />

glutKeyboardFunc( keyboard );


Drawing with <strong>OpenGL</strong>


What can <strong>OpenGL</strong> Draw?<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.


<strong>OpenGL</strong> Geometric<br />

Primitives<br />

• All geometric primitives are specified by<br />

vertices<br />

GL_POINTS<br />

GL_LINES<br />

GL_LINE_STRIP<br />

GL_POLYGON<br />

GL_LINE_LOOP<br />

GL_TRIANGLES<br />

GL_QUAD_STRIP<br />

GL_TRIANGLE_STRIP<br />

GL_TRIANGLE_FAN<br />

GL_QUADS


Specifying Geometric<br />

Primitives<br />

• Primitives are specified using<br />

glBegin( primType );<br />

glEnd();<br />

• primType determines how vertices are combined<br />

glBegin( primType );<br />

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

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

glVertex3fv( coords[i] );<br />

}<br />

glEnd();


The Power of Setting<br />

<strong>OpenGL</strong> State<br />

Appearance<br />

is controlled<br />

by setting<br />

<strong>OpenGL</strong>’s<br />

state.


How <strong>OpenGL</strong> Works: The<br />

Conceptual Model<br />

Configure<br />

how <strong>OpenGL</strong><br />

should draw<br />

stuff<br />

Draw stuff


Controlling <strong>OpenGL</strong>’s<br />

Drawing<br />

• Set <strong>OpenGL</strong>’s rendering state<br />

– State controls how things are drawn<br />

• shading<br />

• texture maps<br />

• polygon patterns<br />

– lighting<br />

– line styles (stipples)<br />

– transparency


Setting <strong>OpenGL</strong> State<br />

• Three ways <strong>to</strong> set <strong>OpenGL</strong> state:<br />

1. Set values <strong>to</strong> be used for processing<br />

vertices<br />

• most common methods of setting state<br />

– glColor() / glIndex()<br />

– glNormal()<br />

– glTexCoord()<br />

• state must be set before calling glVertex()


Setting <strong>OpenGL</strong> State<br />

(cont’d.)<br />

2. Turning on a rendering mode<br />

glEnable() / glDisable()<br />

3. Configuring the specifics of a particular<br />

rendering mode<br />

• Each mode has unique commands for setting<br />

its values<br />

glMaterialfv()


<strong>OpenGL</strong> and Color<br />

• The <strong>OpenGL</strong> Color Model<br />

– <strong>OpenGL</strong> uses the RGB(A) color model<br />

• There is also a color-index mode, but we won’t<br />

discuss it <strong>to</strong>day<br />

• Colors are specified as floating-point<br />

numbers in the range [ 0.0, 1.0 ]<br />

– for example, <strong>to</strong> set a window’s background<br />

color, you would call<br />

glClearColor( 1.0, 0.3, 0.6, 1.0 );<br />

R G B A


Shapes Tu<strong>to</strong>rial


<strong>An</strong>imation and Depth Buffering


Double Buffering<br />

1<br />

2<br />

4<br />

8<br />

16<br />

1<br />

2<br />

4<br />

8<br />

16<br />

Front<br />

Buffer<br />

Back<br />

Buffer<br />

Display


<strong>An</strong>imation Using Double<br />

Buffering<br />

1. Request a double buffered color buffer<br />

glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );<br />

2. Clear color buffer<br />

glClear( GL_COLOR_BUFFER_BIT );<br />

3. Render scene<br />

4. Request swap of front and back buffers<br />

glutSwapBuffers();<br />

• Repeat steps 2 - 4 for animation


Depth Buffering and<br />

Hidden Surface Removal<br />

1<br />

2<br />

4<br />

8<br />

16<br />

1<br />

2<br />

4<br />

8<br />

16<br />

Color<br />

Buffer<br />

Depth<br />

Buffer<br />

Display


Depth Buffering Using<br />

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

1. Request a depth buffer<br />

glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE |<br />

GLUT_DEPTH );<br />

2. Enable depth buffering<br />

glEnable( GL_DEPTH_TEST );<br />

3. Clear color and depth buffers<br />

glClear( GL_COLOR_BUFFER_BIT |<br />

GL_DEPTH_BUFFER_BIT );<br />

4. Render scene<br />

5. Swap color buffers


Transformations


Camera <strong>An</strong>alogy<br />

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

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

camera<br />

viewing<br />

volume<br />

tripod<br />

model


Camera <strong>An</strong>alogy and<br />

Transformations<br />

• Projection transformations<br />

– adjust the lens of the camera<br />

• Viewing transformations<br />

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

volume in the world<br />

• Modeling transformations<br />

– moving the model<br />

• Viewport transformations<br />

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


Transformation Pipeline<br />

v<br />

e<br />

r<br />

t<br />

e<br />

x<br />

object<br />

Modelview<br />

Matrix<br />

ey<br />

e<br />

Projection<br />

Matrix<br />

clip<br />

Perspective<br />

Division<br />

normalized<br />

device<br />

Viewport<br />

Transform<br />

window<br />

Modelview<br />

Modelview<br />

<br />

<br />

<br />

Projection<br />

• other calculations here<br />

– material color<br />

– shade model (flat)<br />

– polygon rendering mode<br />

– polygon culling<br />

– clipping


Coordinate Systems and<br />

Transformations<br />

• Steps in forming an image<br />

1. specify geometry (object coordinates)<br />

2. specify camera (camera coordinates)<br />

3. project (window coordinates)<br />

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

• Each step uses transformations<br />

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

in coordinate systems (frames)


Homogeneous Coordinates<br />

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

⎡x⎤<br />

⎢ ⎥<br />

⎢<br />

y<br />

v r = ⎥<br />

⎢z<br />

⎥<br />

⎢ ⎥<br />

⎣w⎦<br />

– w is usually 1.0<br />

– all operations are matrix multiplications<br />

– directions (directed line segments) can be represented<br />

with w = 0.0


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 v v M<br />

M<br />

=<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 />

12<br />

13<br />

14<br />

15<br />

⎤<br />

⎥<br />

⎥<br />

⎥<br />

⎥<br />


Specifying<br />

Transformations<br />

• Programmer has two styles of specifying<br />

transformations<br />

– specify matrices (glLoadMatrix,<br />

glMultMatrix)<br />

– specify operation (glRotate, glOrtho)<br />

• Programmer does not have <strong>to</strong> remember<br />

the exact matrices<br />

– see appendix of the <strong>OpenGL</strong> <strong>Programming</strong><br />

Guide


<strong>Programming</strong><br />

Transformations<br />

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

orient:<br />

– eye/camera position<br />

– 3D geometry<br />

• Manage the matrices<br />

– including matrix stack<br />

• Combine (composite) transformations


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 />

• Viewport<br />

– usually same as window size<br />

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

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

glViewport( x, y, width, height )


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


Applying Projection<br />

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


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 />

tripod


Projection Tu<strong>to</strong>rial


Modeling Transformations<br />

• Move object<br />

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

( z)<br />

• Rotate object around arbitrary axis x y<br />

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

– angle is in degrees<br />

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

glScale{fd}( x, y, z )


Transformation Tu<strong>to</strong>rial


Connection: Viewing and<br />

Modeling<br />

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

every object in the world <strong>to</strong>wards a<br />

stationary camera<br />

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

several modeling transformations<br />

– gluLookAt() has its own command<br />

– can make your own polar view or pilot view


Common Transformation<br />

Usage<br />

• 2 examples of resize() routine<br />

– restate projection & viewing transformations<br />

• Usually called when window resized<br />

• Registered as callback for<br />

glutReshapeFunc()


Example 1: Perspective &<br />

LookAt<br />

void resize( int width, int height )<br />

{<br />

glViewport( 0, 0, width, height );<br />

glMatrixMode( GL_PROJECTION );<br />

glLoadIdentity();<br />

gluPerspective( 65.0,<br />

(GLdouble)width/height,<br />

1.0, 100.0 );<br />

glMatrixMode( GL_MODELVIEW );<br />

glLoadIdentity();<br />

gluLookAt( 0.0, 0.0, 5.0,<br />

0.0, 0.0, 0.0,<br />

0.0, 1.0, 0.0 );<br />

}


Example 2: Ortho<br />

void resize( int width, int height )<br />

{<br />

GLdouble aspect = (GLdouble) width /<br />

height;<br />

GLdouble left = -2.5, right = 2.5;<br />

GLdouble bot<strong>to</strong>m = -2.5, <strong>to</strong>p = 2.5;<br />

glViewport( 0, 0, width, height );<br />

glMatrixMode( GL_PROJECTION );<br />

glLoadIdentity();<br />

… continued …


Example 2: Ortho (cont’d)<br />

}<br />

if ( aspect < 1.0 ) {<br />

left /= aspect;<br />

right /= aspect;<br />

} else {<br />

bot<strong>to</strong>m *= aspect;<br />

<strong>to</strong>p *= aspect;<br />

}<br />

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

near, far );<br />

glMatrixMode( GL_MODELVIEW );<br />

glLoadIdentity();


Compositing Modeling<br />

Transformations<br />

• Problem: hierarchical objects<br />

– one position depends upon a previous<br />

position<br />

– robot arm or hand; sub-assemblies<br />

• Solution: moving local coordinate system<br />

– modeling transformations move coordinate<br />

system<br />

– post-multiply column-major matrices<br />

– <strong>OpenGL</strong> post-multiplies matrices


Compositing Modeling<br />

Transformations<br />

• Problem: objects move relative <strong>to</strong><br />

absolute world origin<br />

– my object rotates around the wrong origin<br />

• make it spin around its center or something else<br />

• Solution: fixed coordinate system<br />

– modeling transformations move objects<br />

around fixed coordinate system<br />

– pre-multiply column-major matrices<br />

– <strong>OpenGL</strong> post-multiplies matrices<br />

– must reverse order of operations <strong>to</strong><br />

achieve desired effect


Lighting


Lighting Principles<br />

• Lighting simulates how objects reflect light<br />

– material composition of object<br />

– light’s color and position<br />

– global lighting parameters<br />

• ambient light<br />

• two sided lighting<br />

– available in both color index<br />

and RGBA mode


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

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


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 )


Material Properties<br />

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

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

GL_DIFFUSE<br />

GL_SPECULAR<br />

GL_AMBIENT<br />

GL_EMISSION<br />

GL_SHININESS<br />

Base color<br />

Highlight Color<br />

Low-light Color<br />

Glow Color<br />

Surface Smoothness<br />

– separate materials for front and back


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


Light Sources (cont'd.)<br />

• Light color properties<br />

– GL_AMBIENT<br />

– GL_DIFFUSE<br />

– GL_SPECULAR


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 = 0<br />

w ≠ 0<br />

Infinite Light directed<br />

along<br />

Local Light positioned at<br />

( x y z)<br />

( )<br />

x y z<br />

w<br />

w<br />

w


Turning on the Lights<br />

• Flip each light’s switch<br />

glEnable( GL_LIGHTn );<br />

• Turn on the power<br />

glEnable( GL_LIGHTING );


Light Material Tu<strong>to</strong>rial


Controlling a Light’s<br />

Position<br />

• The model-view matrix affects a light’s<br />

position<br />

– Different effects based on when position is<br />

specified<br />

• eye coordinates<br />

• world coordinates<br />

• model coordinates<br />

– Push and pop matrices <strong>to</strong> uniquely control a<br />

light’s position


Light Position Tu<strong>to</strong>rial


Tips for Better Lighting<br />

• Recall lighting computed only at vertices<br />

– model tessellation heavily affects lighting<br />

results<br />

• better results but more geometry <strong>to</strong> process<br />

• Use a single infinite light for fastest<br />

lighting<br />

– minimal computation per vertex


Texture Mapping


Pixel-based primitives<br />

• Bitmaps<br />

– 2D array of bit masks for pixels<br />

• update pixel color based on current color<br />

• Images<br />

– 2D array of pixel color information<br />

• complete color information for each pixel<br />

• <strong>OpenGL</strong> does not understand image<br />

formats


Positioning Image<br />

Primitives<br />

glRasterPos3f( x, y, z )<br />

– raster position transformed like geometry<br />

– discarded if raster position<br />

is outside of viewport<br />

• may need <strong>to</strong> fine tune<br />

viewport for desired<br />

results<br />

Raster Position


Rendering Bitmaps and<br />

Images<br />

• <strong>OpenGL</strong> can render blocks of pixels<br />

– Doesn’t understand image formats<br />

• Raster position controls placement of<br />

entire image<br />

– If the raster position is clipped, the entire<br />

block of pixels is not rendered<br />

glDrawPixels( width, height, format,<br />

type, pixels );<br />

glBitmap( width, height, xorig, yorig,<br />

xmove, ymove, bitmap );


Reading the Framebuffer<br />

• Generated images can be read from the<br />

framebuffer<br />

– You get back a block of pixels<br />

• Read width×height rectangle of pixels<br />

– Lower-left corner of rectangle positioned<br />

at (x, y)<br />

glReadPixels( x, y, width, height,<br />

format, type, pixels );


Pixel Pipeline<br />

• Programmable pixel s<strong>to</strong>rage<br />

and transfer operations<br />

glBitmap(), glDrawPixels()<br />

CPU<br />

Pixel<br />

S<strong>to</strong>rage<br />

Modes<br />

Pixel-Transfer<br />

Operations<br />

(and Pixel Map)<br />

Rasterization<br />

(including<br />

Pixel Zoom)<br />

Per Fragment<br />

Operations<br />

Frame<br />

Buffer<br />

Texture<br />

Memory<br />

glCopyTex*Image();<br />

glReadPixels(), glCopyPixels()


Texture Mapping<br />

y<br />

z<br />

x<br />

geometry<br />

screen<br />

t<br />

image<br />

s


Texture Example<br />

• The texture (below) is a<br />

256 x 256 image that has<br />

been mapped <strong>to</strong> a<br />

rectangular polygon<br />

which is viewed in<br />

perspective


Applying Textures I<br />

• Three steps <strong>to</strong> applying a texture<br />

1. specify the texture<br />

• read or generate image<br />

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

• enable texturing<br />

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

3. specify texture parameters<br />

• wrapping, filtering


Texture Objects<br />

• Have <strong>OpenGL</strong> s<strong>to</strong>re your images<br />

– one image per texture object<br />

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

• Generate texture names<br />

glGenTextures( n, *texIds );


Texture Objects (cont'd.)<br />

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

state<br />

glBindTexture( target, id );<br />

• Bind textures before using<br />

glBindTexture( target, id );


Specify the 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 />

– dimensions of image must be powers of 2<br />

• Texel colors are processed by pixel pipeline<br />

– pixel scales, biases and lookups can be<br />

done


Converting A Texture<br />

Image<br />

• If dimensions of image are not power of 2<br />

gluScaleImage( format, w_in, h_in,<br />

type_in, *data_in, w_out, h_out,<br />

type_out, *data_out );<br />

– *_in is for source image<br />

– *_out is for destination image<br />

• Image interpolated and filtered during scaling


Mapping a Texture<br />

• Based on parametric texture coordinates<br />

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

t<br />

0, 1<br />

Texture Space<br />

a<br />

1, 1<br />

Object Space<br />

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

A<br />

b<br />

c<br />

0, 0 1, 0<br />

s<br />

(0.4, 0.2)<br />

B<br />

C<br />

(0.8, 0.4)


Tu<strong>to</strong>rial: Texture


Applying Textures II<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


Texture Application<br />

Methods<br />

• Filter Modes<br />

– minification or magnification<br />

– special mipmap minification filters<br />

• Wrap Modes<br />

– clamping or repeating<br />

• Texture Functions<br />

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

• blend, modulate or replace texels


Filter Modes<br />

Example:<br />

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

Texture Polygon<br />

Magnification<br />

Texture Polygon<br />

Minification


Mipmapped Textures<br />

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

decreasing resolutions<br />

• Lessens interpolation errors for smaller textured<br />

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


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


Texture Functions<br />

• Controls how texture is applied<br />

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

param )<br />

• GL_TEXTURE_ENV_MODE modes<br />

– GL_MODULATE<br />

– GL_BLEND<br />

– GL_REPLACE<br />

• Set blend color with<br />

GL_TEXTURE_ENV_COLOR


Perspective Correction<br />

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


Advanced <strong>OpenGL</strong> Topics


Working with <strong>OpenGL</strong><br />

Extensions<br />

• <strong>OpenGL</strong> is always changing<br />

– features are first introduced as extensions<br />

• Call glGetString( GL_EXTENSIONS ) <strong>to</strong> see<br />

the extensions for your <strong>OpenGL</strong> implementation<br />

• glext.h contains latest function names and<br />

<strong>to</strong>kens<br />

• May need <strong>to</strong> query a function pointer <strong>to</strong><br />

gain access <strong>to</strong> extension’s function<br />

– Window system dependent pointer request<br />

function<br />

• glXGetProcAddress(), wglGetProcAddress()


Alpha: the 4 th Color<br />

Component<br />

• Measure of Opacity<br />

– simulate translucent objects<br />

• glass, water, etc.<br />

– composite images<br />

– antialiasing<br />

– ignored if blending is not enabled<br />

glEnable( GL_BLEND )


Blending<br />

• Combine fragments with pixel values that<br />

are already in the framebuffer<br />

glBlendFunc( src, dst )<br />

v<br />

C<br />

r<br />

v<br />

= src C<br />

f<br />

+<br />

dst<br />

v<br />

C<br />

p<br />

Fragment<br />

(src)<br />

Framebuffer<br />

Pixel<br />

(dst)<br />

Blending<br />

Equation<br />

Blended<br />

Pixel


<strong>An</strong>tialiasing<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


Summary / Q & A


On-Line Resources<br />

– http://www.opengl.org<br />

• start here; up <strong>to</strong> date specification and lots of sample code<br />

– news:comp.graphics.api.opengl<br />

– http://www.sgi.com/software/opengl<br />

– http://www.mesa3d.org/<br />

• Brian Paul’s Mesa 3D<br />

– http://www.cs.utah.edu/~narobins/opengl.html<br />

• very special thanks <strong>to</strong> Nate Robins for the <strong>OpenGL</strong> Tu<strong>to</strong>rs<br />

• source code for tu<strong>to</strong>rs available here!


Books<br />

• <strong>OpenGL</strong> <strong>Programming</strong> Guide, 4 th Edition<br />

• <strong>OpenGL</strong> Reference Manual, 4 th Edition<br />

• <strong>OpenGL</strong> Shading Language<br />

• <strong>Interactive</strong> Computer Graphics: A <strong>to</strong>pdown<br />

approach with <strong>OpenGL</strong>, 3 rd Edition<br />

• <strong>OpenGL</strong> <strong>Programming</strong> for the X Window<br />

System<br />

– includes many GLUT examples

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

Saved successfully!

Ooh no, something went wrong!