23
Game Programming Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1 Texturing May 02, 2005 Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1 Texturing

Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Game Programming

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Texturing

May 02, 2005

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Texturing

Page 2: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Objectives

To learn how to specify the part of a texturehow to specify the part of a texture

that gets mapped to the triangle

To find out how to create textureshow to create textures

To learn how textures can be filteredhow textures can be filtered to create

a smoother image

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Texture Mapping

Technique that allows us to map image data onto a triangle

to increase the details and realism of our scene

TextureTexturea matrix pixels similar to a surface but can be mapped to triangles

TextureTexture

MappingMapping

Page 3: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Texture Coordinates (1)

Texture coordinate systemconsist of a u-axis and v-axis

TexelTexela pair of u, v coordinates to identify an element on the texture (0, 0) (1, 0)

(0, 1) (1, 1)

+U

+V

normalizednormalizedcoordinatecoordinate

intervalinterval[0, 1][0, 1]

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Texture Coordinates (2)

Define a corresponding triangle on the texture that is to be mapped to the 3D triangle

(0, 0) (1, 0)

(0, 1) (1, 1)

+U

+V

(x1, y1, z1) (x2, y2, z2)

(x3, y3, z3)

(u2, v2)

(u3, v3)

(u1, v1)

3D Triangle Corresponding Texture Triangle

TextureTexture

MappingMapping

Page 4: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Texture Coordinates (3)

Modify a vertex structure

texture isn’t mapping until the rasterization stage where the 3D triangle has been transformed to screen space

struct Vertex{

float x, y, z;float nx, ny, nz;float u, v;float u, v;

static const DWORD FVF;};const DWORD Vertex::FVF = D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEXD3DFVF_TEX;

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Creating and Enabling a Texture (1)

Load texture data into IDirect3DTexture9object

read from an image file: BMP, DDS, DIB, JPG, PNG, and TGA

ex)

HRESULT D3DXCreateTextureFromFile( LPDIRECT3DDEVICE9 pDevice,LPCSTR pSrcFile,LPDIRECT3DTEXTURE9* ppTexture

);

IDirect3DTexture9* _stonewall;D3DXCreateTextureFromFileD3DXCreateTextureFromFile( _device, “stonwall.bmp”,

&_stonwall );

Page 5: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Creating and Enabling a Texture (2)

Set the current texture

cf) multitexturing – eight textures can be set up

ex)

Disable a texture at a particular texturing stage

Use different textures

HRESULT IDirect3DDevice9::SetTexture( DWORD Stage,IDriect3DBaseTexture9* pTexture

);

_device->SetTextureSetTexture( 0, _stonwall );

_device->SetTexture( 0, 00 );renderObjectWithoutTexture( );

_device->SetTexture( 0, tex0tex0 );drawTriangleTex0( );_device->SetTexture( 0, tex1tex1 );drawTriangleTex1( );

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Texture Mapping Steps (1)

Define a texture

Specify mapping from a texture to a surface

Look up texture values during rasterization

v

u

ys

xs

(0, 0, 0)

(1, 0, 0)

(1, 1, 0)

(0, 1, 0)

2DTexture

CoordinateSystem

3DModeling

CoordinateSystem

(Local Space)

2DDevice

CoordinateSystem

(Screen or Viewport Space)

y

xz

Page 6: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Texture Mapping Steps (2)

Rasterizationdevice modeling texture coordinate systems

2D projective transformation (texture device)

v

u

ys

xs

2DTexture

CoordinateSystem

3DModeling

CoordinateSystem

2DDevice

CoordinateSystem

y

xz

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Texture Mapping Steps (3)

Interpolate texture coordinates

Problem distortion!!!due to bilinear interpolation

cut polygons into smaller ones

perspectively divide at each pixel

α βγ

(u1, v1)

(u2, v2)(u3, v3)

Page 7: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Distortion vs. Perspective Division

Linear Interpolationof Texture Coordinates

Corrected Interpolationwith Perspective Division

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Texture Filtering (1)

Sample texture to determine color at each pixel in image (or screen)

v

u

ys

xs

y

xz

Page 8: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Texture Filtering (2)

Problem aliasing!!!

Point Sampling Area Filtering

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Filters (1)

Texture triangle is not the same size as the screen triangle

magnificationmagnification – texture triangle < screen triangle

minificationminification – texture triangle > screen triangle

v

u xs

ys

TexelPixel

Magnification

v

u

Texel

xs

ys

Pixel

Minification

Page 9: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Filters (2)

FilteringFilteringtechnique to help smooth out the distortions which will occur in magnification or minification

Direct3D provides three different types of filters:nearest point sampling

linear filtering

aniotropic filtering

determine the quality of the anisotropic filtering

FastFast_device->SetSampleState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINTD3DTEXF_POINT);_device->SetSampleState(0, D3DSAMP_MINFILTER, D3DTEXF_POINTD3DTEXF_POINT);

_device->SetSampleState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEARD3DTEXF_LINEAR);_device->SetSampleState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEARD3DTEXF_LINEAR);

_device->SetSampleState(0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPICD3DTEXF_ANISOTROPIC);_device->SetSampleState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);

HighHighQualityQuality

_device->SetSampleState(0, D3DSAMP_MAXANISOTROPIYD3DSAMP_MAXANISOTROPIY, 4);

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Mipmaps (1)

A chain of mipmapsmipmaps for a texturepre-filtering images

take a texture and create a series of smaller lower resolution textures but customize the filtering

Each successive mipmap is half the size of the prevous mipmap

256x256 128x128 64x64 32x32 16x16

Page 10: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Mipmaps (2)

Mipmap filter

Filter – D3DTEXF_NONE

D3DTEXF_POINT

D3DTEXF_LINEAR

256x256 128x128 64x64 32x32 16x16

_device->SetSampleState(0, D3DSAMP_MIPFILTERD3DSAMP_MIPFILTER, Filter);

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Mipmap (3)

Using mipmaps with Direct3DD3DXCreateTextureFromFile will generate a

mipmap chain if the device supports mipmaps

Direct3D automatically selects the mipmap that matches the screen triangle the best

Page 11: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Address Modes

Behavior for texture coordinates that go outsize the [0, 1] range

four types – wrap, border color, clamp, mirror

Wrap Border Color Clamp Mirror

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Sample: Textured Quad

Page 12: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Tasks Required for Adding Textures

Construct the vertices of the objects with the texture coordinates specifiedLoad a texture into an IDirect3DTexture9interface using the D3DXCreateTextureFromFile function

Set the minification, magnification, and mipmapfilters

Before you draw an object, set the texture that is associated with the object with IDirect3DDevice9::SetTexture

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Global Variables

Page 13: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Constructing a Quad

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Loading a Texture

Page 14: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Rendering a Quad

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Sample: Address Modes

Page 15: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Specifying Texture Coordinates

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Setting Address Modes

Page 16: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Result – CLAMP

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Result – Mirror

Page 17: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Sample: Textured Cube

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Vertex Structure

Page 18: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Cube Class

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Constructor (1)

Page 19: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Constructor (2)

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Constructor (3)

Page 20: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Destructor & draw( )

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Setup( ) (1)

Page 21: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Setup( ) (2)

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Display( ) (1)

Page 22: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Display( ) (2)

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Display( ) (2-1)

Error!!Error!!'draw' : cannot convert parameter 2 from 'const struct _D3DMATERIAL9 *' to 'struct _D3DMATERIAL9 *'

Page 23: Texturing - hallym.ac.krgraphics.hallym.ac.kr/teach/2005/gp1/src/08texture.pdf · 2008-09-10 · To learn how to specify the part of a texture that gets mapped to the triangle

Sun-Jeong Kim http://www.hallym.ac.kr/~sunkim/teach/2005/gp1

Display( ) (2-2)