Part 3

advertisement

Introduction to OpenGL

(Part 3)

Ref: OpenGL Programming Guide

(The Red Book)

Topics

Part 1

Introduction

Geometry

Viewing

Light & Material

Fall 2009 revised

Part 2

Display List

Alpha Channel

Polygon Offset

Part 3

Image

Texture Mapping

Part 4

FrameBuffers

Selection & Feedback

2

OpenGL

Pixels, Bitmaps, and Images

Bitmaps

A bitmap is a rectangular array of 0s and 1s that serves as a drawing mask for a rectangular portion of the window

Specification:

Specified, in hex codes, in one dimensional array, row by row, starting from lower-left corner

Width need not be multiples of 8

Fall 2009 revised 4

Bitmaps

glBitmap (w, h, xbo, ybo, xbi, ybi, bitmapPtr)

 xbo, ybo: origin

 xbi, ybi: increment CRP after display glBitmap(10, 12, 0, 0, 11, 0, bitmapPtr)

Fall 2009 revised

Fonts: series of bitmaps as display list

5

Bitmap (cont)

Note: You can't rotate bitmap fonts because the bitmap is always drawn aligned to the framebuffer axes.

x and y

Use glColor* to set

GL_CURRENT_RASTER_COLOR

Fall 2009 revised 6

Current Raster Position (CRP)

OpenGL maintains world coordinate raster position , a 3D-position in

Modified by:

 glRasterPos()

 specify the object coordinates (as in glVertex); transformed by

MODELVIEW & PROJECTION, passed to the clipping stage.

If the position is not culled, raster position is updated; otherwise, it is not valid.

glBitmap() glGetFloatv(GL_CURRENT_RASTER_POSITION, fptr) to obtain the CRP glGetBooleanv

(GL_CURRENT_RASTER_POSITION_VALID, &boolvar) to test validity of CRP

Fall 2009 revised 7

drawf.c

Fall 2009 revised 8

Bitmap Editor

This format is called xwindow bitmap, with xbm extension

Bitmaps can be created by the GIMP

(GNU Image Manipulation Program)

Or, seek format converters

Fall 2009 revised 9

Another XBM Editor ( Here )

A simple Tk Program; require Tcl/Tk installed

Get tcltk for Windows from

ActiveTcl

Note that xbm and the opengl xbitmap format is slightly different (

details

)

Fall 2009 revised 11

Ex: XBM Edit & Display

Fall 2009 revised 12

APIs for Images (Pixel Rectangles)

glReadPixels()

Reading pixel data from framebuffer to processor memory.

glDrawPixels()

Writing pixel data from processor memory to framebuffer glCopyPixels()

Copying pixel data within the framebuffer

Fall 2009 revised 13

Function Arguments

glReadPixels (x, y, w, h, F, T, ptr)

 x,y: window coordinate

F: pixel format

T: is data type ptr: pointer to image memory glDrawPixels (w, h, F, T, ptr)

Draw to current raster position glCopyPixels (x, y, w, h, buffer)

Buffer: GL_COLOR | GL_DEPTH | GL_STENCIL

Equivalent to: Read then Draw

Fall 2009 revised 14

More on glCopyPixels

Note that there's no need for a format or data parameter for glCopyPixels(), since the data is never copied into processor memory.

The read source buffer and the destination buffer of glCopyPixels() are specified by

glReadBuffer() and glDrawBuffer() respectively

Default:

 single-buffer: GL_FRONT

 Double-buffered: GL_BACK

Fall 2009 revised 15

Pixel Format

GREY SCALE

GREY SCALE with alpha

Fall 2009 revised 16

Data Type

Fall 2009 revised 17

Example

255:

0xFF

See also image.c for CopyPixels

Fall 2009 revised 18

Example image.c

Copy the lower left corner to the CRP (where the mouse is)

For single-buffer version, only GL_FRONT is involved

While motion is in action, display is not called

Double-buffer version: [from the API doc] glutSwapBuffers promotes the contents of the back buffer to become the contents of the front buffer. The contents of the back buffer then become undefined. Reality is … two have same content

Fall 2009 revised 19

PixelZoom (xfactor,yfactor)

Enlarge/shrink images

Use negative factors for reflected images

Fall 2009 revised 20

Image Loader (TGA)

simple TGA utility in gluit.rar

only load and save uncompressed greyscale, RGB or RGBA mode.

images in

Info in TGA header:

 image type [unsigned char]

1 - colour map image

2 - RGB(A) uncompressed

3 - greyscale uncompressed

9 - greyscale RLE (compressed)

10 - RGB(A) RLE (compressed)

 pixel depth [unsigned char]

 8 – greyscale | 24 – RGB | 32 - RGBA

Fall 2009 revised 21

Offline; Local copy at webhd2:game-lib

Image Loader ( PNG )

Fall 2009 revised 22

DevIL

Fall 2009 revised 23

int pngLoadRaw (filename,rawinfo)

Fall 2009 revised

Must be freed manually

24

Remark

Most graphics formats have image origin at upper/left corner

While OpenGL has the image origin at lower/left corner

Hence, if no correction is done, every image is drawn upside down.

Correction in PNG loader: pngSetStandardOrientation (1);

Fall 2009 revised 25

Imaging Pipeline

Fall 2009 revised 26

Imaging Pipeline(cont.)

glPixelStore()

Controlling Pixel-Storage Modes glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

 Packing and unpacking refer to the way that pixel data is written to and read from processor memory.

 tells OpenGL not to skip bytes at the end of a row

Fall 2009 revised 27

PixelStorei(GL_UNPACK_ALIGNMENT, x)

Specifies the alignment requirements for the start of each pixel row in memory. The allowable values are:

1 (byte-alignment),

2 (rows aligned to even-numbered bytes),

4 (word-alignment [default] ), and

8 (rows start on double-word boundaries).

Byte:8-bit

Word:16-bit

Double-word:32-bit

Fall 2009 revised 28

Details

Assuming the RGB image is of size 3x2:

(three bytes per pixel)

In client memory, the start of each row of pixels is … w=3,h=2

Byte aligned (1)

1st row 2nd row

Aligned to even bytes (2); Word aligned (4)

Double word aligned (8)

Fall 2009 revised 29

Settings

For RGBA images, it doesn ’ t matter (each pixel has 4 bytes:

RGBARGBA … )

For RGB and luminance images and images with odd width , it should be set to byte-aligned

(data are densely packed) glPixelStorei(GL_UNPACK_ALIGNMENT, 1) glPixelStorei(GL_UNPACK_ALIGNMENT, 4)

Fall 2009 revised 30

Improving Pixel Pipeline Performance

A series of fragment operations is applied to pixels as they are drawn into the framebuffer. For optimum performance, disable all fragment operations (depth test, … )

While performing pixel operations, disable other costly states, such as texturing and lighting

It is usually faster to draw a large pixel rectangle than to draw several small ones, since the cost of transferring the pixel data can be amortized ( 分攤 ) over many pixels

Fall 2009 revised 31

Example (depthbuffershow)

Get depth buffer content

 glReadPixels()

Reverse and scale!

Near (white) far (black)

Display it as a luminance image

 glDrawPixels()

Fall 2009 revised 32

Example (rasterpos3)

Illustrate that CRP is a point in R3

Image displayed is always parallel to projection plane

Fake perspective by zooming with distance to camera

Fall 2009 revised 33

Example (sprite)

Process sprite image

Add alpha layer

Load png image

Sprite animation

“ feel of depth ”

Back-to-front rendering

 pixelzoom

Fall 2009 revised 34

OpenGL

Texture Mapping (

Introduction )

Steps in Texture Mapping

Specify (create) the texture

Set texture parameters :

Indicate how the texture is to be applied to each pixel

Enable texture mapping

Draw the scene, supplying both texture and geometric coordinates

Works only in RGB mode

Fall 2009 revised 38

Specify the Texture

glTexImage1D() glTexImage2D() glTexImage3D()

Fall 2009 revised 39

Two Dimensional Texture

glTexImage2D

(GLenum target, GLint level, GLint components,

GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid * pixels )

 target

GL_TEXTURE_2D

 level : LOD number (0: base image) components

: number of color components

(1|2|3|4)

Format : pixel data format

Border: 0 or 1

Width & height

2 m + 2(border bit) w and h can be different

There are new extensions that removes this restriction

Fall 2009 revised 40

glTexImage*D supplement

In GL version 1.1 or greater, null pointer. pixels may be a

In this case texture memory is allocated to accommodate a texture of width height width

. You can then download and height subtextures to initialize this texture memory.

The image is undefined if the user tries to apply an uninitialized portion of the texture image to a primitive.

Fall 2009 revised 41

Setting Texture Environment

glTexEnv{if}{v}(GLenum target,

GLenum pname, TYPEparam);

Setting how textures are to be interpreted:

Target : GL_TEXTURE_ENV

Pname: GL_TEXTURE_ENV_MODE

Param: modes

(DECAL|REPLACE|MODULATE|BLEND)

Fall 2009 revised 42

Texture Environment Modes

GL_REPLACE

GL_MODULATE (default)

GL_DECAL

GL_BLEND

New environment modes:

GL_ADD: C v

= C f

+ C t

GL_COMBINE (ARB, see here )

Fall 2009 revised 43

GL_MODULATE

Color of polygon affects the display of texture

Tree: (r,g,b,a) a cutout

Polygon: (1,0,0)

= 0

Fall 2009 revised 44

GL_BLEND

Use with: glTexEnvfv

(GL_TEXTURE_ENV,

GL_TEXTURE_ENV_COLOR, colorPtr)

Fall 2009 revised

C c

: texture environment color

45

GL_REPLACE

Appearance solely determined by texture

Tree: (r,g,b,a) a cutout

Polygon: (1,0,0)

= 0

Fall 2009 revised

FOR TEXTURE

CUT-OUTS

46

GL_DECAL

Tree: (r,g,b,a) a cutout

Polygon: (1,0,0)

= 0

Cp: replace

RGB: DECAL=REPLACE

Fall 2009 revised 47

Texture + Lighting

To show fragment color, use

GL_MODULATE

Apply specular color AFTER texture mapping:

 glLightModeli

(GL_LIGHT_MODEL_COLOR_CONTROL,

GL_SEPARATE_SPECULAR_COLOR);

GL_SEPARATE_SPECULAR_COLOR

See FAQ 21.040

Fall 2009 revised

GL_SINGLE_COLOR

48

Texture Coordinates

Texture coordinate:

Associate texture location (in texture space) with vertices in the polygon glTexCoord2i (s, t); glVertex2i (x, y);

Order cannot be reversed!

[Think of TexCoord as state assignment]

Fall 2009 revised 49

Texture Coordinates of Quadrics

Most quadric primitives have default setting for texture coordinates

To turn on the default setting:

 gluQuadricTexture

(qobj, GL_TRUE)

Fall 2009 revised 50

Texture Access and Lookup

(1,1)

(0,.75) linear

(1,0)

Polygon (in screen

space) and texture coordinates

Interpolate (s,t) and lookup (r,g,b,a) in the texture map

(0,.75)

Filters nearest

(1,1) rasterization

Fall 2009 revised

Texture map (4x4) (1,0)

51

Magnification & Minification

1

Minification

Magnification

Texels

Pixels

Nature of problem:

Mismatch between texels and pixels

Pixels

Fall 2009 revised 52

Options

Magnification

GL_NEAREST

GL_LINEAR

Nearest: in Manhattan distance to the center of the pixel

Chooses the mipmap that most closely matches the size of the pixel being textured

Minification

GL_NEAREST,

GL_LINEAR,

GL_NEAREST_MIPMAP_NEAREST

GL_LINEAR_MIPMAP_NEAREST

GL_NEAREST_MIPMAP_LINEAR

GL_LINEAR_MIPMAP_LINEAR

Chooses the two mipmaps that most closely match the size of the pixel being textured

Fall 2009 revised 53

Magnification Options

GL_LINEAR

Interpolate the texels

More time consuming but more accurate

GL_NEAREST

Snap to the nearest texel

Fall 2009 revised 54

Problem with Minification

without mipmap

Fall 2009 revised with mipmap

56

Mipmapping

Mipmapping was invented by Lance

Williams in 1983 and is described in his paper Pyramidal parametrics .

The most popular method of anti-aliasing for textures

‘Mip’ stands for “ multum in parvo ” = “many things in a small place”

The texture is downsampled to a quarter of the original area.

Level 3 d = the Level Of Detail (LOD)

Level 2

If a pixel covers

2 2

2 2 texels , go to level 2 and get a texel .

2 1

2 1 , level 1

Level 1

2 m-1

2 n-1

Level 0

2 m

m, n

2 n

∈ 

Fall 2009 revised

+

Level 0 Texture 57

Minification Options

glTexParameteri( GL_TEXTURE_2D,

GL_TEXTURE_MIN_FILTER, GL_LINEAR,

GL_NEAREST, GL_LINEAR_MIPMAP_LINEAR,

GL_LINEAR_MIPMAP_NEAREST,

GL_NEAREST_MIPMAP_LINEAR,

GL_NEAREST_MIPMAP_NEAREST );

Remarks:

Mipmap selection is done per pixel

Using not-yet-ready mipmap disables the texture mapping function.

Fall 2009 revised 59

Mipmap Generation

gluBuild2DMipmaps

Storage overhead

1

1

4

1

4

2

  

1

1

1

4

4

3

Fall 2009 revised 60

Texture Wrap

When the texture coordinates are outside

[0,1] glTexParameteri(GL_TEXTURE_2D,

GL_TEXTURE_WRAP_S,param); glTexParameteri(GL_TEXTURE_2D,

GL_TEXTURE_WRAP_T,param); param: GL_CLAMP|GL_REPEAT

Fall 2009 revised 61

Parameters for Wrapping

GL_CLAMP

 use the border texel

Diagonal: corner texel

GL_REPEAT

 repeat the texels in

[0,1]

Fall 2009 revised 62

Texture Object

A texture object stores texture data and makes it readily available

You can now control many textures and go back to textures that have been previously loaded into your texture resources

Using texture objects is usually the fastest way to apply textures, resulting in big performance gains

 it is almost always much faster to bind (reuse) an existing texture object than it is to reload a texture image using glTexImage*D()

Fall 2009 revised 63

Texture Object API

glGenTextures

Allocate n textures (integer identifiers) glBindTexture

Bind the current texture to specified identifier glDeleteTextures

Free the allocated texture identifiers

Reverse of glGenTextures

Fall 2009 revised 64

Fall 2009 revised checker.c

65

texbind.c

Once created, a named texture may be re-bound to the target of the matching dimensionality as often as needed. It is usually much faster to use glBindTexture to bind an existing named texture to one of the texture targets than it is to reload the texture image using glTexImage1D, glTexImage2D, or

66

Texture Objects

Texture properties saved in texture objects:

Minification and magnification filters, wrapping modes, border color and texture priority

When a texture object is bound again, one may edit the contents of the bound texture object. Any commands that change the above properties will change the currently bound texture as well as the current texture state.

Texture environment, texgen, etc. are NOT stored in texture objects

Fall 2009 revised 67

Texture Loader (PNG, glpng.rar)

Features

Build mipmap

Load and bind 2D textures

Load image data (image loader)

Handle transparent image (cut-out texture)

Important API

 id = pngBind(filename, mipmap, trans, info, wrapst, minfilter, magfilter)

 pngSetStencil(red, green, blue) pngSetStandardOrientation(1) pngLoad(filename, mipmap, trans, info)

TEXTURE ID IS

RETURNED TO YOU

BY PNG LOADER

Fall 2009 revised 68

Texture Transforms

Texture coordinates are multiplied by a 4 by 4 matrix before any texture mapping occurs.

Texture animation: using this, you can make the texture slide over the surface, rotate around it, stretch and shrink, …

All matrix operations apply: Push/Pop/Mult/ …

Fall 2009 revised 69

Texture Suite

smoke cloud explode water

Fall 2009 revised fire 70

Automatic

TEX

ture Coordinate

GEN

eration void glTexGen{ifd}{v}(GLenum coord, GLenum

pname, TYPEparam);

Coord: GL_S, GL_T, GL_R, GL_Q

Pname: GL_TEXTURE_GEN_MODE

Type: GL_OBJECT_LINEAR, GL_EYE_LINEAR, or

GL_SPHERE_MAP

Used when the tex coords are too cumbersome to specify, or will be changed with time.

Texture environment, texgen, etc. are NOT stored in texture objects.

Fall 2009 revised 71

1D Texture According to Height

Continuous color gradation

(0,1/32,2/32, … , 31/32)

Fall 2009 revised

Discrete white lines

(0,0,0,1,0,0,0,1, … )

72

TexGen: Object/Eye_Linear

for a vertex with object coordinates (x o

,y o

,z o

,w o

), generated coordinate = p

1 x o

+ p

2 y o

+ p

3 z o

+ p

4 w

Meaning: when p

1

, p o

2 p

3 coord corresponds to signed distance

, are normalized, this

Similarly, EYE_LINEAR applies to eye coordinates

(0,0,1,0)  (x o

,y o

,z o

,w o

)

Initially, all texture generation functions are set to GL_EYE_LINEAR and are disabled. Both s plane equations are (1, 0, 0, 0), both t plane equations are (0, 1, 0, 0), and all r and q plane equations are (0, 0, 0, 0).

Fall 2009 revised 73

Eye_linear ( ref )

Computing this distance in eye coordinates is a little tricky, since the plane and vertex must first be transformed to eye coordinates. if M is the modelview matrix at the time glTexGenfv( GLX,

GL EYE PLANE, plane ) is called , then the transformed vertex V’ is V’ = M V, let’s call V’ = ( x e

, y e

, z e

, w e

). The plane must also be transformed to get a new plane

A’x +

(

B’y + C’z + D’w = 0 We get ( A’,B’,C’,D’ ) =

A,B,C,D )M − 1 and M is the modelview matrix when glTexGen is invoked . The texture coordinate is then computed as

A’x e

+ B’y e

+ C’z e

+ D’w e

.

Fall 2009 revised 74

Eye_linear Summary

 a

 x e y e z w e e

 b

M c

 x o y o z o w o d

, M

 

: a modelview b c d matrix

M

1 tex coord

 a

 b

 c

 d

 x e y e z e w e

Fall 2009 revised 75

Multi-Texture

What this is

about

Extension Wrangler (

GLEW )

Fall 2009 revised 76

Usages of Multitexture

Fall 2009 revised 77

ARB_multitexture ( ref )

(First) ARB-approved extension (1998)

Support up to 32 texture units*

Texture matrix and texgen affects the active texture environment can cascade up to 32 TE ’ s

Note: default blending mode is GL_MODULATE

* glGetIntegerv (GL_MAX_TEXTURE_UNITS, &units);

My platform has only 4! OpenGL 3.1 requires minimum of 16 texture units

Fall 2009 revised 78

Details

OpenGL's multitexture support requires that every texture unit be fully functional and maintain state that is independent of any other texture units.

Each texture unit has its own texture coordinate generation state, texture matrix state, texture enable state, and texture environment state.

However, each texture unit within an OpenGL context shares the same set of texture objects.

Fall 2009 revised 79

Multi Texture

Fall 2009 revised 80

TexGen with Multitexture

When the GL_ARB_multitexture extension is supported, glTexGen set the texture generation parameters for the currently active texture unit, selected with glActiveTexture.

TexUnit0: texture2D;(s,t) given

TexUnit1: texture1D; texGen

81 Fall 2009 revised

End of Part 3

Results from Xbm Editor

From top to bottom

Byte swapping

#define sample_width 16

#define sample_height 10 static char sample_bits[] =

{

0xff,0x00,

0x00,0xff,

0x00,0x00,

0x07,0xe0,

0x00,0x00,

0xf0,0xf8,

0x00,0x00,

0x00,0x00,

0x00,0x00,

0x00,0x00};

Fall 2009 revised 83

Converting to OpenGL Bitmap

Bottom to top & byte swapping

#define sample_width 16

#define sample_height 10 static char sample_bits[] =

{

0x00,0x00,

0x00,0x00,

0x00,0x00,

0x00,0x00,

0x0f,0x1f,

0x00,0x00,

0xe0,0x07,

0x00,0x00,

0x00,0xff,

0xff,0x00};

84

Solution

Byte swap: resolved by

 glPixelStorei (GL_UNPACK_LSB_FIRST, 1);

Bottom to top:

Recall width of bitmap need not be multiples of 8, but it is stored in units of unsigned characters

But, the XBM Editor only allows width of multiples of 8 (8,16,24, … )

See the example code on reading it reversely

Fall 2009 revised 85

(almost) Useless API!

There seems to no way to make the bitmap larger (than it should be, pixels)

 glPixelZoom won ’ t work!

Therefore, its use is limited

Nevertheless, the color is free to change …

Fall 2009 revised

BACK

86

Texture Mapping

A way to render realistic picture w/o using too many polygons

Fall 2009 revised

Parallax mapping

87

Texture Coordinate System

Texture: a 2D array of color values, each unit is called texel (texture element)

Every texture map has (s,t) coordinates [0,0] to [1,1]

Each vertex in a polygon specifies its texture coordinates (s,t), then map to the given image to determine the corresponding texture

Fall 2009 revised 88

Example

Texture Map

Screen space view

Texture space view

89

Example

Screen space view

Texture Map

Texture space view

OpenGL code

Fall 2009 revised

Fall 2009 revised 91

Fall 2009 revised 92

Fall 2009 revised 93

Fall 2009 revised

BACK

94

OpenGL Extension (Wikipedia)

The OpenGL standard allows individual vendors to provide additional functionality through extensions as new technology is created. Extensions may introduce new functions and new constants, and may relax or remove restrictions on existing

OpenGL functions.

NV: Each vendor has an alphabetic abbreviation that is used in naming their new functions and constants. For example,

NVIDIA's abbreviation ( NV

GL_NORMAL_MAP_NV .

) is used in defining their proprietary function glCombinerParameterfvNV() and their constant

EXT: It may happen that more than one vendor agrees to implement the same extended functionality. In that case, the abbreviation EXT is used.

ARB: It may further happen that the Architecture Review Board

"blesses" the extension. It then becomes known as a standard extension , and the abbreviation ARB is used.

Fall 2009 revised 95

Introduction

The second ARB extension was GL_ARB_multitexture , introduced in version 1.2.1. Following the official extension promotion path, multitexturing is no longer an optionally implemented ARB extension, but has been a part of the OpenGL core API since version 1.3.

Before using an extension a program must first determine its availability, and then obtain pointers to any new functions the extension defines. The mechanism for doing this is platformspecific and libraries such as GLEW and GLEE exist to simplify the process.

Specifications for nearly all extensions can be found at the official extension registry

Fall 2009 revised 96

Introduction

What extension wrangler does:

Your application may find some extensions already available through Microsoft's opengl32.lib. However, depending on your

OpenGL device and device driver, a particular vendor-specific extension may or may not be present at link time.

If it's not present in opengl32.lib, you'll need to obtain the address of the extension's entry points at run time from the device's ICD.

After you obtain the entry point addresses of the extension functions you wish to use, simply call through them as normal function pointers:

FAQ :

How to know which version of OpenGL you are running?

What ’ s the difference among versions?

How to know what extensions are supported?

Ref:

All About OpenGL Extensions

Fall 2009 revised 97

Utilities

GLEW: OpenGL extension wrangler

Glview: OpenGL extension viewer glewinfo.txt

Fall 2009 revised 98

Commonly Used Extensions

GL_ARB_multitexture

GL_ARB_texture_cube_map

GL_ARB_texture_env_dot3

ARB

– Extensions officially approved by the OpenGL Architecture Review

Board

EXT

– Extensions agreed upon by multiple

OpenGL vendors

GL_ARB_shadow NV

– NVIDIA Corporation

GL_ARB_vertex_program,

GL_ARB_fragment_program

SGI

– Silicon Graphics

SGIX

– Silicon Graphics (experimental)

GL_ARB_occlusion _query

GL_ARB_texture_non_power_of_two

GL_EXT_texture3D

GL_EXT_framebuffer_object

Fall 2009 revised

BACK

99

Download