Opengl advanced features levon altunyan

Page 1

Special Features In OpenGL Levon Altunyan ISE MSc Computer Science and Communications Engineering


Display Lists Advantages:  Matrix operations.  Raster bitmaps and images.  Lights, material properties, and lighting models.  Polygon stipples patterns

Disadvantages:  Very small lists may not perform well since there

is some overhead when executing a list.

 Immutability of the contents of a display list.


Vertex Arrays ď‚— Large batches of data can be sent with a

small number of function calls.

ď‚— Through the use of indexed vertex arrays,

vertices can be send exactly once per triangle mesh, reducing bandwidth and potentially avoiding redundant transformation and lighting.


Arrays for position, color, and texture coordinate attributes. Vertex array

Color array

X V[0]

Y

R

Z

G

G

B

R V[1]

X Y

R V[0]

V[0]

Z

V[1]

texcoord array

R V[1]

G B

G


Display Lists and Vertex arrays(Marbles example)  Each marble shares the same data but is colored and

positioned independently.

Marbles.exe

 <SPACE> Toggles vertex arrays for the marbles using

glDrawElements().

 <TAB> Toggles display lists for everything.  <C> Toggles compiled vertex arrays.  A definite improvement in frame rate when enabling

vertex arrays should be observed. Display lists and compiler vertex arrays may or may not improve performance, depending on the underlying hardware.


Blending  Blending combines color values from a source

and a destination.

 Final effect is - parts of our scene appear

translucent.

 The color blending supports transparency -

can be used to simulate windows, drink glasses, and other transparent objects.

 Blending in OpenGL provides pixel-level

control of RGBA color storage in the color buffer.


Function GL_ZERO

RGB Blend Factors (0,0,0)

Alpha Blend Factor 0

GL_ONE

(1,1,1)

1

GL_SRC_COLOR

(Rs,Gs,Bs)

As

GL_ONE_MINUS_SRC_COLOR

(1,1,1) – (Rs,Gs,Bs)

1 – As

GL_DST_COLOR

(Rd,Gd,Bd)

Ad

GL_ONE_MINUS_DST_COLOR

(1,1,1) – (Rd,Gd,Bd)

1 – Ad

GL_SRC_ALPHA

(As,As,As)

As

GL_ONE_MINUS_SRC_ALPHA

(1,1,1) – (As,As,As)

1 – As

GL_DST_ALPHA

(Ad,Ad,Ad)

Ad

GL_ONE_MINUS_DST_ALPHA

(1,1,1) – (Ad,Ad,Ad)

1 – Ad

GL_CONSTANT_COLOR

(Rc,Gc,Bc)

Ac

GL_ONE_MINUS_CONSTANT_COLOR

(1,1,1) – (Rc,Gc,Bc)

1 – Ac

GL_CONSTANT_ALPHA

(Ac,Ac,Ac)

Ac

GL_ONE_MINUS_CONSTANT_ALPHA

(1,1,1) – (Ac,Ac,Ac)

– Ac

GL_SRC_ALPHA_SATURATE

(f,f,f) 1

1

[1] Where f = min(A , 1 – A ). s

d


Blending example - Disk Blender  To demonstrate some of the many blending combinations

the disk blender example is included.

Blender.exe

 Disk blender is drawn with a black background with an

alpha of 1.0. A green disk with some transparency is drawn first, without blending. Then a semi-transparent red disk is drawn on top of it with blending enabled.

 By pressing the S, D, and E keys, you can cycle through all

of the available source factors, destination factors, and blend equations.


Antialiasing  Some of the OpenGL pictures lines, especially nearly

horizontal and nearly vertical ones, appear jagged. These “jaggies” appear because the ideal line is approximated by a series of pixels that must lie on the pixel grid. The jaggedness is called aliasing, and the techniques for reducing it – antialiasing.


FOG  OpenGL’s built-in fog support  Colorout=blendFactor*colorin+(1+blendFactor)colorfog  Blending each pixel with the color of the fog using a blend

factor dependent on:

 distace from the viewer;  the density of the fog;  the currently selected fog mode;  GL_LINEAR: blendFactor = (end-depth)/(end-start)  GL_EXP:

blendFactor =

 GL_EXP2: blendFactor =


Fragment vs Vertex Fog  The distance to a fragment from the eye plane can be

calculated in one of two ways.

 Fragment fog (NVIDIA hardware) will use the actual

fragment depth.

 Vertex fog (ATI chipsets) use the vertex distance and

interpolate between vertices.

 Fragment fog requires more work than vertex fog, but

often has a higher quality appearance.


Simple Terrain with several Fog modes  Simple height map-based terrain with a large quad drawn Fog.exe

in blue at ground level to represent water.

 Top left panel shows the terrain without any fog  Top right panel uses GL_LINEAR fog  Bottom left panel shows GL_EXP fog with a fairly low

density

 Bottom right panel shows GL_EXP

2 fog with fog

coordinates set so that the fog is thicker where the terrain is lower(near the water), thinning quickly at higher elevations.


Feedback & Selection  Used to facilitate the user’s active interaction with a scene.  Selection and picking an object or region of a scene in OpenGL

coordinates rather than just window coordinates.

 Feedback – information on how an object or a primitive is

actually drawn in the window. It can be used to implement annotations or bounding boxes features.

 Typically implemented in software (the driver), even for

hardware accelerated OpenGL implementations.


Feedback & Selection ď‚— OpenGL supports an object selection mechanism in

which the object geometry is transformed and compared against a selection sub region (pick region) of the viewport.

ď‚— The mechanism uses the transformation pipeline to

compare object vertices against the view volume.


ď‚— To reduce the view volume to a screen-space sub region (in window

coordinates) of the viewport, the projected coordinates of the object are transformed by a scale and translation transform and combined to produce the matrix

where ox, oy, px, and py are the x and y origin and width and height of the viewport, and qx, qy, dx, and dy are the origin and width and height of the pick region.


Frame transform

Frame

ď‚— An illustration of a car frame with four

Front right wheels drawn as instances of the same transform wheel model. The figure shows a partial graph of the model hierarchy, with the car frame positioned in the Back right scene and the four wheel instances transform positioned relative to the frame. Wheel

Wheel

Front left transform Back left transform Wheel

Wheel


Feedback & Selection  A number of fragment operations are applied to rasterization fragments before

they are allowed to update pixels in the framebuffer.

 Fragment operations can be separated into two categories: test fragments

operations, and modify fragments operations.

 To maximize efficiency, the fragment operations are ordered so that the fragment

tests are applied first.

 The most interesting tests for advanced rendering are: alpha test, stencil test,

and depth buffer test. These tests can either pass, allowing the fragment to continue, or fail, discarding the fragment so it can’t pass on to later fragment operations or update the framebuffer. The stencil test is a special case, since it can produce useful side effects even when fragments fail the comparison.


Feedback & Selection  All of the fragment tests use the same set Constant

of comparison operators: Never, Always, Less, Less than or Equal, Equal, Greater than or Equal, Greater, and Not Equal.

 In each test, a fragment value is

Comparison

GL_ALWAYS

always pass

GL_NEVER

never pass

GL_LESS

pass if incoming < ref

compared against a reference value saved GL_LEQUAL in the current OpenGL state (including the depth and stencil buffers), and if the GL_GEQUAL comparison succeeds, the test passes.

pass if incoming≤ref pass if incoming≥ref

GL_GREATER

pass if incoming>ref

GL_EQUAL

pass if incoming=ref

GL_NOTEQUAL

pass if ≠ref


Stencil Buffer  The stencil buffer is like the depth and color buffers,

except stencil pixels don’t represent colors or depths, but have application-specific meanings.  The stencil buffer isn’t directly visible like the color

buffer, but the bits in the stencil planes form an unsigned integer that affects and is updated by drawing commands, through the stencil function and the stencil operations.


Stencil Buffer 1.

The stencil test fails.

2. The stencil test passes, but the depth test fails. 3. Both the stencil and the depth test pass. Stencil Operation

Results of Operation on Stencil Values

GL_KEEP

stencil value unchanged

GL_ZERO

stencil value set to zero

GL_REPLACE

stencil value replaced by stencil reference value

GL_INCR

stencil value incremented

GL_DECR

stencil value decremented

GL_INVERT

stencil value bitwise inverted


Dissolves with Stencil  Stencil buffers can be used to mask selected pixels on the screen. This allows for pixel by

pixel compositing of images.  You can draw geometry or arrays of stencil values to control, per pixel, what is drawn into

the color buffer. One way to use this capability is to composite multiple images.  A common film technique is the “dissolve”, where one image or animated sequence is

replaced with another, in a smooth sequence. The stencil buffer can be used to implement arbitrary dissolve patterns.  The alpha planes of the color buffer and the alpha function can also be used to implement

this kind of dissolve, but using the stencil buffer frees up the alpha planes for motion blur, transparency, smoothing, and other effects.


Dissolves with Stencil cont 1.

Turn on stenciling; glEnable(GL STENCIL TEST).

2. Set stencil function to always fail; glStencilFunc(GL NEVER, 1, 1). 3. Set stencil op to write 1 on stencil test failure; glStencilOp(GL

REPLACE, GL KEEP, GL KEEP). 4. Write the dissolve pattern to the stencil buffer by drawing

geometry or using glDrawPixels. 5. Disable writing to the stencil buffer with glStencilMask(GL

FALSE).

6. Set stencil function to pass on 0; glStencilFunc(GL EQUAL, 0, 1).


Dissolves with Stencil cont 7. Enable color buffer for writing with glColorMask(GL TRUE, GL TRUE, GL TRUE,

GL TRUE).

8. If you’re depth testing, turn depth buffer writes back on with glDepthMask. 9. Draw the first image. It will only be written where the stencil buffer values are 0. 10. Change the stencil test so only values that are 1 pass; glStencilFunc(GL EQUAL, 1,

1).

11. Draw the second image. Only pixels with stencil value of 1 will change. 12. Repeat the process, updating the stencil buffer, so that more and more stencil

values are 1, using your dissolve pattern, and redrawing image 1 and 2, until the entire stencil buffer has 1’s in it, and only image 2 is visible.


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.