gam531 dps931 – week 12
DESCRIPTION
GAM531 DPS931 – Week 12. Textures and Samplers. Actor Composition. What is a Texture?. 2 x 2 pixels. 26 x 26 pixels. 265 x 265 pixels. Red | Green | Blue | Alpha. Red | Green | Blue. SF_3X1BYTE_UINT. SF_4X4BYTE_FLOAT. SF_4X1BYTE_UINT. SF_3X4BYTE_FLOAT. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/1.jpg)
GAM531DPS931 – Week 12Textures and Samplers
![Page 2: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/2.jpg)
Actor Composition
Actor
Mesh
Vertex Buffer
Index Buffer
Vertex Format
Material
Shaders
Uniform Buffers
Blend States
Textures
Samplers
![Page 3: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/3.jpg)
What is a Texture?
Red | Green | Blue | Alpha
265 x 265 pixels
26 x 26 pixels 2 x 2 pixels
SF_4X4BYTE_FLOAT
Red | Green | Blue
SF_4X1BYTE_UINT SF_3X4BYTE_FLOAT SF_3X1BYTE_UINT
![Page 4: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/4.jpg)
How Do We Make Use of Textures?
[ 0, 10, 0 ]
[ 7, -7, 0 ][ -7, -7, 0 ]
![Page 5: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/5.jpg)
How Does The Texture Map to The Object?
Class Vertex {
};Vector<float,2> uv;Vector<> pos; [ 0, 10,
0 ]
[ 7, -7, 0 ][ -7, -7, 0 ]
[ 0, 0 ]
[ 1, 0 ]
[ 0, 1 ]
[ 1, 1 ]
[ 0.5, 0 ]
[ 0, 1 ]
[ 1, 1 ]
UVW Mapping
![Page 6: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/6.jpg)
Rasterization And Interpolation?
[ 0, 0 ]
[ 1, 0 ]
[ 0, 1 ]
[ 1, 1 ]
[ 0, 10, 0 ]
[ 7, -7, 0 ][ -7, -7, 0 ]
[ 0.5, 0 ]
[ 0, 1 ]
[ 1, 1 ]
[ 0, 1.5, 0 ][ 0.5, 0.5 ]
[ -3 , -2, 0 ][ 0.28, 0.29 ]
Texel
![Page 7: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/7.jpg)
What Types of Textures Are There?
1D Textures
Width (U)
2D Textures
Width (U)
Height (V)
3D Textures
Width (U)
Height (V)
Depth (W)
![Page 8: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/8.jpg)
Where are Textures Defined?
Image Files Custom Textures
auto a = newTex2D(“custom”);
a->initCustom(2, 2, SF_4X4BYTE_FLOAT);
float t[] = {0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f};
a->fill((byte*)t, sizeof(t));
Render Targets
![Page 9: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/9.jpg)
Programming Textures to Load From File
int w, h, c; unsigned char* data = SOIL_load_image(filename, &w, &h, &c, SOIL_LOAD_RGB);glGenTextures(1, &textureID); dim = GL_TEXTURE_2D;glBindTexture(GL_TEXTURE_2D, textureID);switch(c) { case 1: iFormat = GL_LUMINANCE; format = GL_RGB; break; case 2: iFormat = GL_LUMINANCE_ALPHA; format = GL_RGBA; break; case 3: iFormat = GL_RGB; format = GL_RGB; break; case 4: iFormat = GL_RGBA; format = GL_RGBA; break; }type = GL_UNSIGNED_BYTE; GLint maxTextureSize;glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);glTexStorage2D(GL_TEXTURE_2D, intLog2(min(w,h)), iFormat, w, h);glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, format, type, data);glGenerateMipmap(GL_TEXTURE_2D); SOIL_free_image_data(data);
D3DX11CreateShaderResourceViewFromFile(dev, filename, 0, 0, &texture, 0);//zeroes are load options, pump thread, hresult
![Page 10: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/10.jpg)
Programming Custom Textures
glGenTextures(1, &textureID);dim = GL_TEXTURE_1D;glBindTexture(GL_TEXTURE_1D, textureID);//Translate formatiFormat = _glTranslateFormat(fmt, format, type);glTexStorage1D(GL_TEXTURE_1D, 1, iFormat, size);
//This is only for 1D Textures
D3D11_TEXTURE1D_DESC desc;ZeroMemory(&desc, sizeof(D3D11_TEXTURE1D_DESC));
desc.ArraySize = //number of arrays (1 usually)desc.BindFlags = //how the texture will be useddesc.CPUAccessFlags = //how the CPU can accessdesc.Format = //color format (use translation)desc.MipLevels = //number of mip maps (more later)desc.MiscFlags = //other flags (set to 0)desc.Usage = //dynamic for custom meshesdesc.Width = //the width in texels
Dev->CreateTexture1D(&desc, 0, &tex1d);Dev->CreateShaderResourceView(tex1d, 0, &texture);
//This is only for 1D Textures
![Page 11: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/11.jpg)
Binding Textures to Shaders
glActiveTexture(GL_TEXTURE0 + slot);glBindTexture(dim, textureID);
//OpenGL does not require separate binds for each //shader type, dim is the type of texture to bind
//bind to fragment shadercon->PSSetShaderResources(slot, 1, &texture);//bind to vertex shadercon->VSSetShaderResources(slot, 1, &texture);
//slot is the memory slot in the shader the //texture will be associated with, 1 is the number//of textures to bind
![Page 12: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/12.jpg)
Filling Custom Textures
glBindTexture(GL_TEXTURE_1D, textureID);
glTexSubImage1D(GL_TEXTURE_1D, 0, 0, size / texelSize, format, type, data);
D3D11_MAPPED_SUBRESOURCE sub;
con->Map(tex1d, 0, D3D11_MAP_WRITE_DISCARD, 0, &sub);
memcpy(sub.pData, data, size);
con->Unmap(tex1d, 0);
![Page 13: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/13.jpg)
Texture Samplers
Texture Sampler defines how texels will be sampled from a texture to be applied to geometry in the world
![Page 14: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/14.jpg)
Minification and Magnification
20 x 20 texelsTexture
[ 0, 0 ]
[ 1, 0 ]
[ 0, 1 ]
[ 1, 1 ]
[ 0, 0 ]
[ 1, 0 ]
[ 0, 1 ]
[ 1, 1 ]
10 x 10 pixelsGeometry
(clip space)
Interpolated UVs
[0,0] [0.1,0][0.2,0] [0.3,0][0.4,0] [0.5,0][0.6,0] [0.7,0][0.8,0] [0.9,0]
Texel Coords[0,0] [0.05,0]
[0.1,0] [0.15,0][0.2,0] [0.25,0][0.3,0] [0.35,0][0.4,0] [0.45,0][0.5,0] [0.55,0][0.6,0] [0.65,0][0.7,0] [0.75,0][0.8,0] [0.85,0][0.9,0] [0.95,0]
Point Sampling
![Page 15: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/15.jpg)
Texture Filtering
Point Sampling(Sample Once)
UV = [0.2,0]
[0, 0, 0]
Linear Sampling(Sample Two Closest, Average)
UV = [0.2,0]
[26,26,26]
[ 0, 0, 0 ] * 1.0
0.2-(1/3)/(1/3)=0.1
[ 0, 0, 0 ] * 0.9 +
[255,255,255] * 0.1
Bilinear Sampling
(Sample Four Closest, Average)
UV = [0.2,0]
[26,26,26]~Equation above,
but in 4 directions
![Page 16: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/16.jpg)
Texture Address Modes
Wrap
UV[3.4,2.1] = Texel[0.4,0.1]
Mirror
UV[3.4,2.1] = Texel[0.6,0.1]
Clamp
UV[3.4,2.1] = Texel[1.0,1.0]
Border
UV[3.4,2.1] != Texel[*,*]
![Page 17: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/17.jpg)
Texture Samplers Revisited
![Page 18: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/18.jpg)
Mipmaps
512 x 512256 x 256128 x 128 64 x 64 32 x 32 16 x 16 8 x 8 4 x 4 2 x 2 1 x 1
1024 x 1024Geometry
280 x 280Geometry
80 x 80Geometry
![Page 19: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/19.jpg)
Texture Filtering With Mipmaps
Point Sampling(Sample Once)
Linear Sampling(Sample Two Closest, Average)
Bilinear Sampling
(Sample Four Closest, Average)
Trilinear Sampling
(Sample Four Closest On Two Closest Mipmaps,
Average)
Increases granularity of the scene,
removes sudden changes in
texture quality
![Page 20: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/20.jpg)
Anisotropic Filtering512 x 512512 x 256512 x 128512 x 64512 x 32512 x 16512 x 8512 x 4512 x 2512 x 1
256 x 512256 x 256256 x 128256 x 64256 x 32256 x 16256 x 8256 x 4256 x 2256 x 1
128 x 512128 x 256128 x 128128 x 64128 x 32128 x 16128 x 8128 x 4128 x 2128 x 1
…
2X Anisotropic Filtering
![Page 21: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/21.jpg)
Anisotropic Comparison
![Page 22: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/22.jpg)
Programming Texture SamplersglGenSamplers(1, &samplerID);glSamplerParameteri(samplerID, GL_TEXTURE_WRAP_S, um);glSamplerParameteri(samplerID, GL_TEXTURE_WRAP_T, vm);glSamplerParameteri(samplerID, GL_TEXTURE_WRAP_R, wm);glSamplerParameterf(samplerID, GL_TEXTURE_MAX_LOD, xl);glSamplerParameterf(samplerID, GL_TEXTURE_MIN_LOD, nl);glSamplerParameteri(samplerID,GL_TEXTURE_MAG_FILTER,g);glSamplerParameteri(samplerID,GL_TEXTURE_MIN_FILTER,n);glSamplerParameterf(samplerID, GL_TEXTURE_LOD_BIAS,lb);glSamplerParameteri(samplerID, GL_TEXTURE_COMPARE_FUNC, cmf);glSamplerParameterfv(samplerID, GL_TEXTURE_BORDER_COLOR, bc.data);if(GLEW_EXT_texture_filter_anisotropic) { GLfloat mA; glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &mA); if (ma > maxAnisotropy) ma = (uint32)maxAnisotropy; glSamplerParameterf(samplerID, GL_TEXTURE_MAX_ANISOTROPY_EXT, (GLfloat)ma);}
D3D11_SAMPLER_DESC sd;
sd.Filter = //Texture Filter for min/mag/mipsd.AddressU = //Address mode for the U coordsd.AddressV = //Address mode for the V coordsd.AddressW = //Address mode for the W coordsd.MipLODBias = //Mip’s Level of detail biassd.MaxAnisotropy = //Max level of anisotropymemcpy(sd.BorderColor, bc.data, sizeof(float)*4);//Copy in the color value for the bordersd.MinLOD = //Minimum Level of Detailsd.MaxLOD = //Maximum Level of Detailsd.ComparisonFunc = //Compares sample data
dev->CreateSamplerState(&sd, &sampler);
![Page 23: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/23.jpg)
Binding Textures to Shaders
glActiveTexture(GL_TEXTURE0 + slot);glBindSampler(slot, samplerID);
//OpenGL does not require separate binds for each //shader type
//bind to fragment shadercon->VSSetSamplers(slot, 1, &sampler);//bind to vertex shadercon->VSSetSamplers(slot, 1, & sampler);
//slot is the memory slot in the shader the //texture will be associated with, 1 is the number//of samplers to bind
![Page 24: GAM531 DPS931 – Week 12](https://reader036.vdocuments.mx/reader036/viewer/2022062305/568162cc550346895dd35acc/html5/thumbnails/24.jpg)
To Do• Finish up work on engine enhancement• Read this week’s lab• Read this week’s notes• Re-implement OpenGL Texture and Sampler functions