CS 445 / CS 645 Texture Maps

advertisement
CS 445 / CS 645
Texture Maps
Supersampling Techniques
• Adaptive supersampling
– store more points when necessary
• Stochastic supersampling
– Place sample points at stochastically
determined points
• Eye has harder time detecting aliasing when
combined with the noise generated by
stochastics
Texture Maps
• Chapter 9 of Open GL Programming
Guide (Red Book)
• Images applied to polygons to enhance
the visual effect of a scene
– Rectangular arrays of data
• Color, luminance, alpha
• Components of array called texels
Example Texture Map
Example Texture Map
glVertex3d (s, s, s)
glTexCoord2d(1,1);
glVertex3d (-s, -s, -s)
glTexCoord2d(1,1);
Example Texture Map
glVertex3d (s, s, s)
glTexCoord2d(5, 5);
glVertex3d (s, s, s)
glTexCoord2d(0.1, 0.1);
Texture Coordinates
• Every polygon has object coordinates
and texture coordinates
– Object coordinates describe where polygon
vertices are on the screen
– Texture coordinates describe texel
coordinates of each vertex (usually 0 -> 1)
– Texture coordinates are interpolated along
vertex-vertex edges
• glTexCoord{1234}{sifd}(TYPE coords)
Textures in Assignment 2
• Data Types
– GL pointer to Texture Object
• GLuint texture[2]
– User-defined format to store raw image data
• struct Image {
unsigned long sizeX;
unsigned long sizeY;
char *data;};
typedef struct Image Image;
Textures in Assignment 2
• Texture Object
– An OpenGL data type that keeps textures resident
in memory and provides identifiers to easily
access them
– Provides efficiency gains over having to
repeatedly load and reload a texture
– You can prioritize textures to keep in memory
– OpenGL uses least recently used (LRU) if no
priority is assigned
Step 1 – Load image data
• Read .bmp from file
– Use Image data type
• getc() and fseek() to read image x & y size
• fread() fills the Image->data memory with actual
red/green/blue values from .bmp
– Note
• malloc() Image->data to appropriate size
• .bmp stores color in bgr order and we convert to rgb order
Step 2 – create Texture Objects
• glGenTextures(1, &texture[texture_num]);
– First argument tells GL how many Texture
Objects to create
– Second argument is a pointer to the place
where OpenGL will store the names
(unsigned integers) of the Texture Objects it
creates
• Remember, texture[] is of type GLuint
Step 3 – Specify which texture
object is about to be defined
• Tell OpenGL that you are going to
define the specifics of the Texture
Object it created
– glBindTexture(GL_TEXTURE_2D,
texture[texture_num]);
• Textures can be 1D and 3D
Step 4 – Begin defining texture
• glTexParameter()
– Sets various parameters that control how a texture
is treated as it’s applied to a fragment or stored in
a texture object
– // scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXT
URE_MAG_FILTER,GL_LINEAR);
– // scale linearly when image smaller than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXT
URE_MIN_FILTER,GL_LINEAR);
Step 5 – Assign image data
– glTexImage2D();
GL_TEXTURE_2D
0
3
image1->sizeX
image1->sizeY
0
GL_RGB
GL_UNSIGNED_BYTE
(2D Texture)
(level of detail 0)
(3 components, RGB)
(size)
(size)
(no border pixel)
image1->data
(pointer to the data))
(RGB color order)
(unsigned byte data)
glTexImage2D – Arg 1
• GLenum target
– GL_TEXTURE_2D
– GL_PROXY_TEXTURE_2D
• Provides queries for texture resources
• Proceed with hypothetical texture use (GL
won’t apply the texture)
• After query, call GLGetTexLevelParamter to
verify presence of required system components
• Doesn’t check possibility of multiple texture
interference
glTexImage2D – Arg 2
• GLint level
– Used for Level of Detail (LOD)
– LOD stores multiple versions of texture that
can be used at runtime (set of sizes)
– Runtime algorithms select appropriate
version of texture
• Pixel size of polygon used to select best texture
• Eliminates need for error-prone filtering
algorithms
glTexImage2D – Arg 3
• GLint internalFormat
– Describes which of R, G, B, and A are used
in internal representation of texels
– Provides control over things texture can do
• High bit depth alpha blending
• High bit depth intensity mapping
• General purpose RGB
– GL doesn’t guarantee all options are
available on given hardware
glTexImage2D – Args 4-6
• GLsizei width
• GLsizei height
– Dimensions of texture image
• Must be 2m + 2b (b=0 or 1 depending on border)
• min, 64 x 64
• GLint border
– Width of border (1 or 0)
• Border allows linear blending between overlapping
textures
• Useful when manually tiling textures
glTexImage2D – Args 7 & 8
• GLenum format
– Describe how texture data is stored in input
array
• GL_RGB, GL_RGBA, GL_BLUE…
• GLenum type
– Data size of array components
• GL_SHORT, GL_BYTE, GL_INT…
glTexImage2D – Arg 9
• Const GLvoid *texels
– Pointer to data describing texture map
Step 6 – Apply texture
• Before defining geometry
– glEnable(GL_TEXTURE_2D);
– glBindTexture(GL_TEXTURE_2D,
texture[0]);
– glTexEnvf(GL_TEXTURE_ENV,
GL_TEXTURE_ENV_MODE,
GL_REPLACE);
glTexEnv()
First argument to function is always GL_TEXTURE_ENV
GL_TEXTURE_ENV_MODE
GL_DECAL
GL_REPLACE
GL_MODULATE
GL_BLEND
GL_TEXTURE_ENV_COLOR
4-float array for R,G,B,A
blend
gluScaleImage()
Alters the size of an image to meet the 2m
size requirement of OpenGL
– Scaling performed by linear and box
filtering
glCopyTexImage2D()
• Use current frame buffer contents as
texture
• Copy frame buffer to named texture
location
glTexSubImage2D()
• Replace a region of current working
texture with a smaller texture
• SubImage need not adhere to 2m size
limitation
• This is how you add data from your
system’s camera to GL environment
• glCopyTexSubImage2D
– Frame buffer cut and paste possible too
Mipmaps
• multum in parvo -- many things in a
small place
• A texture LOD technique
• Prespecify a series of prefiltered texture
maps of decreasing resolutions
• Requires more texture storage
• Eliminates shimmering and flashing as
objects move
MIPMAPS
• With versus without MIPMAP
MIPMAPS
• Arrange different versions into one
block of memory
gluBuild2DMipmaps
• Automatically constructs a family of
textures from original texture size down
to 1x1
Advanced Mipmaps
• Runtime mipmap level determination
• User-specified parameters
– r = max texture size / max polygon size
– l = log2 r
– If l < 0.0, texture smaller than polygon and
magnification filter is used
– If l > 0.0, texture is larger than polygon and
minification is used (mipmapping)
• l specifies mipmap level
Advanced Mipmaps
• You can specify additional mipmap levels
on the fly
– MIN_LOD may reduce popping
– MAX_LOD may reduce over compression
• You can specify min mipmap level
– Useful for mosaicing (Alphabet on a texture)
Filtering
• OpenGL tries to pick best mipmap level
• Question: Which texel corresponds to a
particular pixel?
• GL_NEAREST (Point Sampling)
– Pick the texel with center nearest pixel
• GL_LINEAR (Bilinear Sampling)
– Weighted average of 2x2 closest texels
• GL_NEAREST_MIPMAP_LINEAR
– Average nearest texels from two mipmap levels
• GL_LINEAR_MIPMAP_LINEAR (Trilinear)
– Average two averaged texels from two mipmaps
Automatic Texture-Coordinate
Generation
• glTexGen
– Provides control over how texture is placed on polygon.
If first argument is GL_TEXTURE_GEN_MODE
– Second argument is
• GL_OBJECT_LINEAR
– Consistent mapping to object’s coordinate system
• GL_EYE_LINEAR
– Mapping is relative to eyepoint
• GL_SPHERE_MAP
– Environment Mapping
Environment Mapping
• Used to model a object that reflects
surrounding textures to the eye
– Polished sphere reflects walls and ceiling textures
– Cyborg in Terminator 2 reflects flaming destruction
• Texture is distorted fish-eye view of
environment
• Spherical texture mapping creates texture
coordinates that correctly index into this
texture map
Sphere Mapping
Blinn/Newell Lattitude Mapping
Cube Mapping
The q-coordinate
• Remember, texture coordinate
interpolation is linear
• Texture coordinates can contain an
optional q-parameter
• Q scales texture coordinates by dividing
each coordinate by q
• This captures perspective
transformations
Multitexturing
• Pipelining of multiple texture
applications to one polygon
• The results of each texture unit
application is passed to the next texture
unit, which adds its effects
• More bookkeeping is required to pull
this off
Sony Playstation 2
Xbox
Nintendo Game Cube
Playstation 2 Stats
•300 MHz 128 bit RISC “Emotion
Engine” chip (from Toshiba)
•32 MB Rambus memory
•Standard DVD-ROM (CD-ROM
compatible)
Xbox Stats
• CPU: Intel Pentium III 733 MHz processor technology
with Streaming SIMD Extensions.
• Graphics Chip: Custom 3-D NVIDIA graphics processor
(The chip will be X-Box only and not available for PC
users)
• Sound Processor: Custom 3-D audio processor, 64 Audio
Channels, 3D Audio Support, Midi/DLS2 support, AC3
encoded game audio.
• Operating System: Windows 2000 Kernel, DirectX API
• Main Memory: 64 MB of RAM (unified memory
architecture)
• CD-ROM Drive: 4X DVD drive with movie playback, 8GB
Hard Drive
• On-board Modem: T10/100 NIC
• Network: 100 MBps Ethernet
Gamecube Stats
• 485 Mhz IBM Power PC
• 40 MB RAM
Playstation 2 Graphics
• 150 MHz clock
• 4 MB video memory
• 75 Million Polygons Per Second is absolute
max
• 20 Million per second with 48 pixel quads, Zbuffering, alpha blending, and MIP mapping
• 32-bit z-buffer
Xbox Graphics
• 300 million pixels per second
• 150 million Transformed and Lit
(T&L)
• 100+ million shaded and textured
polygons (sustained performance)
• 4 GB/Sec Texture Bandwidth
• 4 simultaneous textures per poly
• Full Screen Anti-Aliasing (FSAA),
Gamecube Graphics
•
•
•
•
•
10.4 GB/sec texture bandwidth
24 bit color
24 bit z-buffer
Subpixel antialiasing
8 hardware lights
Download