1 L Feb-18-04 SMD159, Buffers and Texture Mapping Overview - - PDF document

1 l
SMART_READER_LITE
LIVE PREVIEW

1 L Feb-18-04 SMD159, Buffers and Texture Mapping Overview - - PDF document

INSTITUTIONEN FR SYSTEMTEKNIK LULE TEKNISKA UNIVERSITET Buffers and Texture Mapping David Carr Fundamentals of Computer Graphics Spring 2004 Based on Slides by E. Angel 1 L Feb-18-04 SMD159, Buffers and Texture Mapping Overview


slide-1
SLIDE 1

1

Feb-18-04 SMD159, Buffers and Texture Mapping 1 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Buffers and Texture Mapping

David Carr Fundamentals of Computer Graphics Spring 2004

Based on Slides by E. Angel

Feb-18-04 SMD159, Buffers and Texture Mapping 2 L

Overview

  • Buffers
  • Additional OpenGL buffers
  • Reading and writing buffers
  • Blending
  • Texture mapping
  • Mapping Methods

+ Texture mapping + Environmental Mapping + Bump Mapping

  • Basic strategies

+ Forward versus backward mapping + Point sampling versus area averaging

Feb-18-04 SMD159, Buffers and Texture Mapping 3 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Buffers

slide-2
SLIDE 2

2

Feb-18-04 SMD159, Buffers and Texture Mapping 4 L

Buffer

Define a buffer by its spatial resolution (n x m) and its depth k, the number of bits/pixel pixel

Feb-18-04 SMD159, Buffers and Texture Mapping 5 L

OpenGL Buffers

  • Color buffers
  • Front
  • Back
  • Auxiliary

+ Undisplayed + System dependent

  • Overlay

+ Chroma key animation

  • Depth
  • Accumulation
  • High resolution buffer
  • Stencil
  • Holds masks

Feb-18-04 SMD159, Buffers and Texture Mapping 6 L

Writing in Buffers

  • Conceptually, we can consider all of memory as a large two-

dimensional array of pixels

  • We read and write rectangular block of pixels
  • Bit block transfer (bitblt) operations
  • The frame buffer is part of this memory

frame buffer (destination) writing into frame buffer source memory

slide-3
SLIDE 3

3

Feb-18-04 SMD159, Buffers and Texture Mapping 7 L

Writing Model

  • Read destination pixel before writing source

Feb-18-04 SMD159, Buffers and Texture Mapping 8 L

Writing Modes

  • Source and destination bits are combined bitwise
  • 16 possible functions (one per column in table)

replace OR XOR

Feb-18-04 SMD159, Buffers and Texture Mapping 9 L

XOR mode

  • Recall from Chapter 3 that we can use XOR by enabling logic
  • perations and selecting the XOR write mode
  • XOR is especially useful for swapping blocks of memory such as

menus that are stored off screen If S represents screen and M represents a menu the sequence S ¨ S ⊕ M M ¨ S ⊕ M ([S ⊕ M] ⊕ M) S ¨ S ⊕ M ([S ⊕ M] ⊕ S) swaps the S and M

slide-4
SLIDE 4

4

Feb-18-04 SMD159, Buffers and Texture Mapping 10 L

The Pixel Pipeline

  • OpenGL has a separate pipeline for pixels
  • Writing pixels involves

+ Moving pixels from processor memory to the frame buffer + Format conversions + Mapping, Lookups, Tests

  • Reading pixels

+ Format conversion

Feb-18-04 SMD159, Buffers and Texture Mapping 11 L

Raster Position

  • OpenGL maintains a raster position as part of the state
  • Set by gl.glRasterPos*()
  • 2D or 3D position in various forms
  • The raster position is a geometric entity
  • Passes through geometric pipeline
  • Eventually yields a 2D position in screen coordinates
  • This position in the frame buffer is where the next raster

primitive is drawn

Feb-18-04 SMD159, Buffers and Texture Mapping 12 L

Buffer Selection

  • OpenGL can draw into or read from any of the color buffers (front,

back, auxiliary)

  • Default to the back buffer
  • Change with gl.glDrawBuffer and gl.glReadBuffer
  • Note that format of the pixels in the frame buffer is different from that
  • f processor memory and these two types of memory reside in

different places

  • Need packing and unpacking
  • Drawing and reading can be slow
slide-5
SLIDE 5

5

Feb-18-04 SMD159, Buffers and Texture Mapping 13 L

Bitmaps

  • OpenGL treats 1-bit pixels (bitmaps) differently than

multi-bit pixels (pixelmaps)

  • Bitmaps are masks which determine if the

corresponding pixel in the frame buffer is drawn with the present raster color

  • 0 fi color unchanged
  • 1 fi color changed based on writing mode
  • Bitmaps are useful for raster text fonts

Feb-18-04 SMD159, Buffers and Texture Mapping 14 L

Raster Color

  • Same as drawing color set by gl.glColor*()
  • Fixed by last call to gl.glRasterPos*()
  • Geometry drawn in blue
  • Ones in bitmap use a drawing color of red

gl.glColor3f(1.0, 0.0, 0.0); gl.glRasterPos3f(x, y, z); gl.glColor3f(0.0, 0.0, 1.0); gl.glBitmap(……. gl.glBegin(GL_LINES); gl.glVertex3f(…..)

Feb-18-04 SMD159, Buffers and Texture Mapping 15 L

Drawing Bitmaps

gl.glBitmap(width, height, x0, y0, xi, yi, bitmap)

first raster position second raster position

  • ffset from raster

position increments in raster position after bitmap drawn

slide-6
SLIDE 6

6

Feb-18-04 SMD159, Buffers and Texture Mapping 16 L

Pixel Maps

  • OpenGL works with rectangular arrays of pixels called

pixel maps or images

  • Pixels are in one byte (8 bit) chunks
  • Luminance (gray scale) images 1 byte/pixel
  • RGB 3 bytes/pixel
  • Three functions
  • Draw pixels: processor memory to frame buffer
  • Read pixels: frame buffer to processor memory
  • Copy pixels: frame buffer to frame buffer

Feb-18-04 SMD159, Buffers and Texture Mapping 17 L

OpenGL Pixel Functions

gl.glReadPixels(x,y,width,height,format,type,myimage)

start pixel in frame buffer size type of image type of pixels pointer to processor memory

byte myimage[512][512][3]; gl.glReadPixels(0,0, 512, 512, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, myimage); gl.glDrawPixels(width,height,format,type,myimage)

starts at raster position

Feb-18-04 SMD159, Buffers and Texture Mapping 18 L

Image Formats

  • We often work with images in a standard format (JPEG,

TIFF, GIF)

  • How do we read/write such images with OpenGL?
  • No support in OpenGL
  • OpenGL knows nothing of image formats
  • Some code available on Web
  • Basic plan of attack
  • Find (write) a class that reads/writes into a standard data

structure

  • Write a translation to a pixel map
  • Transfer pixel map to OpenGL
slide-7
SLIDE 7

7

Feb-18-04 SMD159, Buffers and Texture Mapping 19 L

INSTITUTIONEN FÖR SYSTEMTEKNIK

LULEÅ TEKNISKA UNIVERSITET

Texture Mapping

Feb-18-04 SMD159, Buffers and Texture Mapping 20 L

Application

Image

  • Want to:
  • Map realistic variation onto an
  • bject
  • Limit model complexity
  • Solution directly map a digital

pattern to an object

  • Example, mapping

reflections to Geri’s glasses Box texture map Centered on the lens

Feb-18-04 SMD159, Buffers and Texture Mapping 21 L

The Limits of Geometric Modeling

  • Graphics cards can render over 10 million polygons per

second

  • But, that is insufficient for many phenomena
  • Clouds
  • Grass
  • Terrain
  • Skin
slide-8
SLIDE 8

8

Feb-18-04 SMD159, Buffers and Texture Mapping 22 L

Modeling an Orange

  • Consider the problem of modeling an orange (the fruit)
  • Start with an orange-colored sphere
  • Too simple
  • Replace sphere with a more complex shape
  • Does not capture surface characteristics (small dimples)
  • Takes too many polygons to model all the dimples

Feb-18-04 SMD159, Buffers and Texture Mapping 23 L

Modeling an Orange (Alternate)

  • Take a picture of a real orange, scan it, and “paste”
  • nto simple geometric model
  • This process is texture mapping
  • Still might not be sufficient because resulting surface

will be smooth

  • Need to change local shape
  • Bump mapping

Feb-18-04 SMD159, Buffers and Texture Mapping 24 L

Three Types of Mapping

  • Texture Mapping
  • Uses images to fill inside of polygons
  • Environmental (reflection mapping)
  • Uses a picture of the environment for texture maps
  • Allows simulation of highly specular surfaces
  • Bump mapping
  • Emulates altering normal vectors during the rendering

process

slide-9
SLIDE 9

9

Feb-18-04 SMD159, Buffers and Texture Mapping 25 L

Texture Mapping

Geometric Model Texture Mapped

Feb-18-04 SMD159, Buffers and Texture Mapping 26 L

Environment Mapping

Feb-18-04 SMD159, Buffers and Texture Mapping 27 L

Bump Mapping

slide-10
SLIDE 10

10

Feb-18-04 SMD159, Buffers and Texture Mapping 28 L

Where Does Mapping Take Place?

  • Mapping techniques are implemented at the end of the

rendering pipeline

  • Very efficient because few polygons pass down the

geometric pipeline

Feb-18-04 SMD159, Buffers and Texture Mapping 29 L

2D image 3D surface

Is It Simple?

  • Although the idea is simple
  • Map an image to a surface
  • There are 3 or 4 coordinate systems involved

Feb-18-04 SMD159, Buffers and Texture Mapping 30 L

Coordinate Systems

  • Parametric coordinates
  • May be used to model curved surfaces
  • Texture coordinates
  • Used to identify points in the image to be mapped
  • World Coordinates
  • Conceptually, where the mapping takes place
  • Screen Coordinates
  • Where the final image is really produced
slide-11
SLIDE 11

11

Feb-18-04 SMD159, Buffers and Texture Mapping 31 L

Texture Mapping

parametric coordinates texture coordinates world coordinates screen coordinates

Feb-18-04 SMD159, Buffers and Texture Mapping 32 L

Mapping Functions

  • Basic problem is how to find the maps
  • Consider mapping from texture coordinates to a point a

surface

  • Appear to need three functions

x = x(s,t) y = y(s,t) z = z(s,t)

  • But we really want to go

the other way

s t (x,y,z)

Feb-18-04 SMD159, Buffers and Texture Mapping 33 L

Backward Mapping

  • We really want to go backwards
  • Given a pixel, we want to know to which point on an object it

corresponds

  • Given a point on an object, we want to know to which point in

the texture it corresponds + Need a map of the form

s = s(x,y,z) t = t(x,y,z)

  • Such functions are difficult to find in general
slide-12
SLIDE 12

12

Feb-18-04 SMD159, Buffers and Texture Mapping 34 L

Two-Part Mapping

  • One solution to the mapping problem is to first map the

texture to a simple intermediate surface

  • Example: map to cylinder

Feb-18-04 SMD159, Buffers and Texture Mapping 35 L

Cylindrical Mapping

  • Parametric cylinder
  • Maps rectangle in u,v space to cylinder of:
  • Radius r and height h in world coordinates
  • Maps from texture space

x = r cos 2p u y = r sin 2pu z = v/h s = u t = v

Feb-18-04 SMD159, Buffers and Texture Mapping 36 L

Spherical Map

  • We can similarly use a parametric sphere
  • But, must decide where to put the distortion
  • Spheres are use in environmental maps

x = r cos 2pu y = r sin 2pu cos 2pv z = r sin 2pu sin 2pv

slide-13
SLIDE 13

13

Feb-18-04 SMD159, Buffers and Texture Mapping 37 L

Box Mapping

  • Easy to use with simple orthographic projection
  • Also used in environmental maps

Feb-18-04 SMD159, Buffers and Texture Mapping 38 L

intermediate actual

Second Mapping

  • Map from intermediate object to actual object
  • Normal vectors from intermediate to actual
  • Normal vectors from actual to intermediate
  • Vectors from center of intermediate

Feb-18-04 SMD159, Buffers and Texture Mapping 39 L

point samples in u,v (or x,y,z) space point samples in texture space miss blue stripes

Aliasing

  • Point sampling of the texture can lead to aliasing errors
slide-14
SLIDE 14

14

Feb-18-04 SMD159, Buffers and Texture Mapping 40 L

  • A better but slower option is to use area averaging
  • Note that pre image of pixel is curved

pixel pre image

Area Averaging

Feb-18-04 SMD159, Buffers and Texture Mapping 41 L

Questions?