viewing with opengl courtesy of drs. carol o’sullivan / yann morvan trinity college dublin

57
Viewing With OpenGL Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Upload: beatrix-conley

Post on 16-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Viewing With OpenGLViewing With OpenGL

Courtesy of Drs. Carol O’Sullivan / Yann Morvan

Trinity College Dublin

Page 2: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

OpenGL Geometry Pipeline

MODELVIEWmatrix

PROJECTIONmatrix

perspectivedivision

viewporttransformation

w

z

y

x

eye

eye

eye

eye

w

z

y

x

1dev

dev

y

x

proj

proj

proj

w

y

x

win

win

y

x

originalvertex

vertex in theeye coordinate

space

2d projectionof vertex ontoviewing plane

normalised devicecoordinates

(foreshortened)

final windowcoordinates

Page 3: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Summary - 1Summary - 1

• Object Coordinates are transformed by the ModelView matrix to produce Eye Coordinates.

• Eye Coordinates are transformed by the Projection matrix to produce Clip Coordinates.

• Clip Coordinates X, Y, and Z are divided by Clip Coordinate W to produce Normalized Device Coordinates.

• Normalized Device Coordinates are scaled and translated by the viewport parameters to produce Window Coordinates.

Page 4: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Summary - 2Summary - 2• Object coordinates are the raw coordinates you submit to

OpenGL with a call to glVertex*() or glVertexPointer(). They represent the coordinates of your object or other geometry you want to render.

• Many programmers use a World Coordinate system. – Objects are often modeled in one coordinate system, then scaled,

translated, and rotated into the world you're constructing. – World Coordinates result from transforming Object Coordinates by

the modelling transforms stored in the ModelView matrix. – However, OpenGL has no concept of World Coordinates. World

Coordinates are purely an application construct.

Page 5: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Summary - 3Summary - 3• Eye Coordinates result from transforming Object

Coordinates by the ModelView matrix. – The ModelView matrix contains both modelling and viewing

transformations that place the viewer at the origin with the view direction aligned with the negative Z axis.

• Clip Coordinates result from transforming Eye Coordinates by the Projection matrix. – Clip Coordinate space ranges from -Wc to Wc in all three axes,

where Wc is the Clip Coordinate W value. OpenGL clips all coordinates outside this range.

Page 6: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Summary - 4Summary - 4• Perspective division performed on the Clip Coordinates

produces Normalized Device Coordinates, ranging from -1 to 1 in all three axes.

• Window Coordinates result from scaling and translating Normalized Device Coordinates by the viewport. – The parameters to glViewport() and glDepthRange() control this

transformation. – With the viewport, you can map the Normalized Device

Coordinate cube to any location in your window and depth buffer.

Page 7: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

The Camera SystemThe Camera System

• To create a view of a scene we need:– a description of the scene geometry– a camera or view definition

• Default OpenGL camera is located at the origin looking down the -z axis.

• The camera definition allows projection of the 3D scene geometry onto a 2D surface for display.

• This projection can take a number of forms:– orthographic (parallel lines preserved)– perspective (foreshortening): 1-point, 2-point or 3-point– skewed orthographic

Page 8: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Camera TypesCamera Types

• Before generating an image we must choose our viewer:• The pinhole camera model is most widely used:

– infinite depth of field (everything is in focus)

• Advanced rendering systems model the camera– double gauss lens as used in many professional cameras– model depth of field and non-linear optics (including lens flare)

• Photorealistic rendering systems often employ a physical model of the eye for rendering images– model the eyes response to varying brightness and colour levels– model the internal optics of the eye itself (diffraction by lens fibres

etc.)

Page 9: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Pinhole Camera ModelPinhole Camera Model

Page 10: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Modeling the Eye’s ResponseModeling the Eye’s Response

Adaptation(see aside on Eye)

Glare & Diffraction

Page 11: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Camera SystemsCamera Systems

A camera model implementedin Princeton University (1995)

Page 12: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin
Page 13: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Viewing SystemViewing System• We are only concerned with

the geometry of viewing at this stage.

• The camera’s position and orientation define a view-volume or view-frustrum.– objects completely or partially

within this volume are potentially visible on the viewport.

– objects fully outside this volume cannot be seen clipped

clipped

view frustrum

clipping planes

Page 14: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Camera ModelsCamera Models

• Each vertex in our model must be projected onto the 2D camera viewport plane in order to be displayed on the screen.

• The CTM is employed to determine the location of each vertex in the camera coordinate system:

• We then employ a projection matrix defined by GL_PROJECTION to map this to a 2D viewport coordinate.

• Finally, this 2D coordinate is mapped to device coordinates using the viewport definition (given by glViewport()).

xx CTM

M

Page 15: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Camera Modeling in OpenGL Camera Modeling in OpenGL ®®

glMatrixMode(GL_MODELVIEW)...

glMatrixMode(GL_PROJECTION)...

glViewport(0,0,xres,yres)

camera coordinatesystem

viewport coordinatesystem

device/screencoordinate system

Page 16: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

3-point

3D 3D 2D Projection 2D Projection• Type of projection depends on a number of factors:

– location and orientation of the viewing plane (viewport)– direction of projection (described by a vector)– projection type:

Projection

Perspective Parallel

2-point

Oblique

Axonometric

Orthographic1-point

Page 17: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Parallel ProjectionsParallel Projections

axonometricorthographic

oblique

Page 18: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Orthogonal ProjectionsOrthogonal Projections• The simplest of all projections, parallel project onto view-

plane.• Usually view-plane is axis aligned (often at z=0)

1000

0000

0010

0001

where

1

0

1

MMPPy

x

z

y

x

Page 19: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Orthogonal ProjectionsOrthogonal Projections

• The result is an orthographic projection if the object is axis aligned, otherwise it is an axonometric projection.

• If the projection plane intersects the principle axes at the same distance from the origin the projection is isometric.

1 zyx

Page 20: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Parallel Projections in OpenGLParallel Projections in OpenGLglOrtho(xmin, xmax, ymin, ymax, zmin, zmax);

Note: we always view in -z direction need to transform world in order to viewin other arbitrary directions.

Page 21: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Perspective ProjectionsPerspective Projections

• Perspective projections are more complex and exhibit fore-shortening (parallel appear to converge at points).

• Parameters:– centre of projection (COP)– field of view (, )– projection direction– up direction

Page 22: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Perspective ProjectionsPerspective Projections3-point perspective

2-point perspective

1-point perspective

Page 23: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Perspective ProjectionsPerspective Projections

Page 24: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Perspective ProjectionsPerspective ProjectionsConsider a perspective projection with the viewpoint at the originand a viewing direction oriented along the positive -z axis and the

view-plane located at z = -d

y

yp

dz

yy

d

y

z

yP

P

-z

d

10100

0100

0010

0001

11

z

y

x

ddz

z

y

x

ddz

ydz

x

z

y

x

P

P

P

divide by homogenous ordinate tomap back to 3D space

a similar construction for xp

Page 25: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Perspective Projections DetailsPerspective Projections Details

dz

z

y

x

ddz

ydz

x

z

y

x

P

P

P

11

10000

0100

0010

0001

1

z

y

x

z

y

x

Flip z to transform to a left handed co-ordinatesystem increasing z values mean increasing

distance from the viewer.

PROJECTIONmatrix

perspectivedivision

Page 26: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Perspective ProjectionPerspective Projection

• Depending on the application we can use different mechanisms to specify a perspective view.

• Example: the field of view angles may be derived if the distance to the viewing plane is known.

• Example: the viewing direction may be obtained if a point in the scene is identified that we wish to look at.

• OpenGL supports this by providing different methods of specifying the perspective view:– gluLookAt, glFrustrum and gluPerspective

Page 27: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Perspective ProjectionsPerspective ProjectionsglFrustrum(xmin, xmax, ymin, ymax, zmin, zmax);

Page 28: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

glFrustrumglFrustrum• Note that all points on the line defined by (xmin,ymin,-

zmin) and COP are mapped to the lower left point on the viewport.

• Also all points on the line defined by (xmax,ymax,-zmin) and COP are mapped to the upper right corner of the viewport.

• The viewing direction is always parallel to -z• It is not necessary to have a symmetric frustrum like:

• Non symmetric frustrums introduce obliqueness into the projection.

• zmin and zmax are specified as positive distances along -z

glFrustrum(-1.0, 1.0, -1.0, 1.0, 5.0, 50.0);

Page 29: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Perspective ProjectionsPerspective ProjectionsgluPerspective(fov, aspect, near, far);

2tan2

2tan

2 nearh

near

h

Page 30: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

gluPerspectivegluPerspective

• A utility function to simplify the specification of perspective views.

• Only allows creation of symmetric frustrums.• Viewpoint is at the origin and the viewing direction is the -z

axis.• The field of view angle, fov, must be in the range

[0..180]

• apect allows the creation of a view frustrum that matches the aspect ratio of the viewport to eliminate distortion.

Page 31: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Perspective ProjectionsPerspective Projections

Page 32: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

10mm Lens (fov = 122°) 20mm Lens (fov = 84°)

35mm Lens (fov = 54°) 200mm Lens (fov = 10°)

Lens Configurations

Page 33: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Positioning the CameraPositioning the Camera• The previous projections had limitations:

– usually fixed origin and fixed projection direction

• To obtain arbitrary camera orientations and positions we manipulate the MODELVIEW matrix prior to creation of the models. This positions the camera w.r.t. the model.

• We wish to position the camera at (10, 2, 10) w.r.t. the world

• Two possibilities:– transform the world prior to creation of objects using translatef

and rotatef: glTranslatef(-10, -2, -10);– use gluLookAt to position the camera with respect to the world

co-ordinate system: gluLookAt(10, 2, 10, … );

• Both are equivalent.

Page 34: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Positioning the CameraPositioning the CameragluLookAt(eyex, eyey, eyez, lookx, looky, lookz, upx, upy, upz);

glTranslatef(-eyex, -eyey, -eyez);glRotatef(theta, 1.0, 0.0, 0.0);glRotatef(phi, 0.0, 1.0, 0.0);

equivalent to:

theta

phi

Page 35: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

The ViewportThe Viewport• The projection matrix defines the mapping from a 3D

world co-ordinate to a 2D viewport co-ordinate.• The viewport extents are defined as a parameter of the

projection:– glFrustrum(l,r,b,t,n,f)

– gluPerspective(fv,a,n,f)

(l,b,-n)

(r,t,-n)

(w,h,-n)

(-w,-h,-n)ahw

fvnh

2

tan

Page 36: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

The ViewportThe Viewport• We need to associate the 2D viewport co-ordinate system

with the window co-ordinate system in order to determine the correct pixel associated with each vertex.

normalised deviceco-ordinates window co-ordinates

Page 37: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Viewport to Window Viewport to Window TransformationTransformation

• An affine planar transformation is used.• After projection to the viewplane, all points are

transformed to normalised device co-ordinates: [-1…+1, -1…+1]

• glViewport used to relate the co-ordinate systems:

12

12

minmax

min

minmax

min

yy

yyy

xx

xxx

pn

pn

glViewport(int x, int y, int width, int height);

Page 38: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Viewport to Window Viewport to Window TransformationTransformation

• (x,y) = location of bottom left of viewport within the window• width,height = dimension in pixels of the viewport

• normally we re-create the window after a window resize event to ensure a correct mapping between viewport and window dimensions:

static void reshape(int width, int height){

glViewport(0, 0, width, height);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(85.0, 1.0, 5, 50);

}

static void reshape(int width, int height){

glViewport(0, 0, width, height);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(85.0, 1.0, 5, 50);

}

yheight

xwidth

21

21 nwnw yyxx

Page 39: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Aspect RatioAspect Ratio• The aspect ratio defines the relationship between the

width and height of an image.• Using gluPerspective an viewport aspect ratio may be

explicitly provided, otherwise the aspect ratio is a function of the supplied viewport width and height.

• The aspect ratio of the window (defined by the user) must match the viewport aspect ratio to prevent unwanted affine distortion:

aspect ratio = 1.25 aspect ratio = 0.5

Page 40: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Multiple ProjectionsMultiple Projections• To help 3D understanding, it can be useful to have

multiple projections available at any given time– usually: plan (top) view, front & left or right elevation (side) view

Perspective

Front Right

Top

Page 41: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

// top left: top viewglViewport(0, win_height/2, win_width/2, win_height/2);glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-3.0, 3.0, -3.0, 3.0, 1.0, 50.0);gluLookAt(0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glCallList(object);

// top right: right viewglViewport(win_width/2, win_height/2, win_width/2, win_height/2);glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-3.0, 3.0, -3.0, 3.0, 1.0, 50.0);gluLookAt(5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glCallList(object);

// bottom left: front viewglViewport(0, 0, win_width/2, win_height/2);glMatrixMode(GL_PROJECTION);glLoadIdentity();glOrtho(-3.0, 3.0, -3.0, 3.0, 1.0, 50.0);gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glCallList(object);

// bottom right: rotating perspective viewglViewport(win_width/2, 0, win_width/2, win_height/2);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(70.0, 1.0, 1, 50);gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glRotatef(30.0, 1.0, 0.0, 0.0);glRotatef(Angle, 0.0, 1.0, 0.0);glCallList(object);

Sample ViewportSample ViewportApplicationApplication

Page 42: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

StateState• OpenGL is a big state machine• State encapsulates control for operations like:

– Lighting– Shading– Texture Mapping– Depth testing

• Boolean state settings can be turned on and off with glEnable and glDisable

• Anything that can be set can be queried using glGet

Page 43: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Turning on Depth test (Z-buffer)Turning on Depth test (Z-buffer)OpenGL uses a Z-buffer for depth tests

– For each pixel, store nearest Z value (to camera) so far– If new fragment is closer, it replaces old z, color – Simple technique to get accurate visibility– (Be sure you know what fragments and pixels are)

Changes in main fn, display to Z-buffer

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

In init function glEnable(GL_DEPTH_TEST) ; glDepthFunc(GL_LESS) ; // The default option

Page 44: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Double BufferingDouble Buffering• New primitives draw over (replace) old objects• Can lead to jerky sensation• Solution: double buffer. Render into back (offscreen) buffer.

When finished, swap buffers to display entire image at once. • Changes in main and display glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutSwapBuffers() ;

glFlush ();

Page 45: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Moving a Light SourceMoving a Light Source• Lights transform like other geometry• Only modelview matrix (not projection). The only real application

where the distinction is important• See types of light motion pages 222-

– Stationary light: set the transforms to identity before specifying it

– Moving light: Push Matrix, move light, Pop Matrix

– Moving light source with viewpoint (attached to camera). Can simply set light to 0 0 0 so origin wrt eye coords (make modelview matrix identity before doing this)

Page 46: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Material PropertiesMaterial Properties

• Need normals (to calculate how much diffuse, specular, find reflected direction and so on)

• Four terms: Ambient, Diffuse, Specular, Emissive

Page 47: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Stencil Buffer & DecalsStencil Buffer & Decals

- Stencil buffer & OpenGl commands

- Using the stencil buffer to apply polygonal decals

- Using the stencil buffer to apply text decals

- Decals

Page 48: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

DecalsDecals

2 step process:

1. As surface to be stenciled is written into frame buffer, mark what pixels it modifies

2. Scan convert decal into frame buffer restricted to the pixels marked in step 1

Page 49: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

DecalsDecals

color

depth stencil

444444444444444444444444444444444444444422222244222222442222229922222299

11111111111111111111111111111111111

000…

1111

999…

Page 50: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Stencil BufferStencil Buffer

• Same spatial resolution as color and depth buffers

• Usually (and at least) 8-bits, but can vary

• Used to hold values related to elements being written into frame buffer

Page 51: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

OpenGl Commands OpenGl Commands

• glStencilFunc() - sets function to test stencil bits with

• glStencilMask(), glStencilMaskSeparate() - specifies which bits in Stencil Buffer are involved

• glStencilOp(), glStencilOpSeparate() - specifies operation to perform as result of stencil test and depth test

Page 52: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

glStencilFunc()glStencilFunc()

• glStencilFunc(GLenum func, Glint ref, GLuint mask)

• Specifies test to perform on reference value and masked bits in stencil buffer

• func - test function e.g., GL_LEQUAL, GL_ALWAYS• ref - reference value for test• mask - ANDed with ref & stencil value - selects what bits

to use

Page 53: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

glStencilMask()glStencilMask()

• glStencilMask(GLuint mask)

• Enables and disables writing of individual bits in the stencil planes

Page 54: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

glStencilMaskSeparate()glStencilMaskSeparate()

• glStencilMaskSeparate(GLenum face, GLuint mask)

• Face - GL_FRONT, GL_BACK, GL_FRONT_AND_BACK

• Enables and disables writing of individual bits in the stencil planes

Page 55: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

glStencilOp()glStencilOp()

• glStencilOp(GLenum sfail, GLenum dpfail, GLenum dppass)

• Specifies what action to take as a result of stencil test and depth test: GL_KEEP, GL_ZERO, GL_REPLACE, etc.

• sfail - fails stencil test• dpfail - passes stencil test, fails depth test• dppass- passes both stencil and depth test

Page 56: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

glStencilOpSeparate()glStencilOpSeparate()

• glStencilOpSeparate(GLenum face, GLenum sfail, GLenum dpfail, GLenum dppass)

• Specifies what action to take as a result of stencil test and depth test: GL_KEEP, GL_ZERO, GL_REPLACE, etc.

• sfail - fails stencil test• dpfail - passes stencil test, fails depth test• dppass- passes both stencil and depth test

Page 57: Viewing With OpenGL Courtesy of Drs. Carol O’Sullivan / Yann Morvan Trinity College Dublin

Applying polygonal decalsApplying polygonal decals

Draw decal wherever stencil has a ‘1’

glStencilFunc(GL_EQUAL,1,1); // test if 1 bit is set in stencil buffer, glStencilMask(GL_FALSE); // turn off stencil writing ('0' ?)

glDisable(GL_DEPTH_TEST); // don't do depth test (so it ‘passes’)draw decal polygon

glEnable(GL_DEPTH_TEST);glDisable(GL_STENCIL_TEST);