Presentation is loading. Please wait.

Presentation is loading. Please wait.

GAM531 DPS931 – Week 12 Textures and Samplers. Actor Composition Actor Mesh Vertex Buffer Index Buffer Vertex Format Material Shaders Uniform Buffers.

Similar presentations


Presentation on theme: "GAM531 DPS931 – Week 12 Textures and Samplers. Actor Composition Actor Mesh Vertex Buffer Index Buffer Vertex Format Material Shaders Uniform Buffers."— Presentation transcript:

1 GAM531 DPS931 – Week 12 Textures and Samplers

2 Actor Composition Actor Mesh Vertex Buffer Index Buffer Vertex Format Material Shaders Uniform Buffers Blend States Textures Samplers

3 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

4 How Do We Make Use of Textures? [ 0, 10, 0 ] [ 7, -7, 0 ][ -7, -7, 0 ]

5 How Does The Texture Map to The Object? Class Vertex { }; Vector 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

6 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

7 What Types of Textures Are There? 1D Textures Width (U) 2D Textures Width (U) Height (V) 3D Textures Width (U) Height (V) Depth (W)

8 Where are Textures Defined? Image FilesCustom 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

9 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

10 Programming Custom Textures glGenTextures(1, &textureID); dim = GL_TEXTURE_1D; glBindTexture(GL_TEXTURE_1D, textureID); //Translate format iFormat = _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 used desc.CPUAccessFlags = //how the CPU can access desc.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 meshes desc.Width = //the width in texels Dev->CreateTexture1D(&desc, 0, &tex1d); Dev->CreateShaderResourceView(tex1d, 0, &texture); //This is only for 1D Textures

11 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 shader con->PSSetShaderResources(slot, 1, &texture); //bind to vertex shader con->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

12 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);

13 Texture Samplers Texture Sampler defines how texels will be sampled from a texture to be applied to geometry in the world

14 Minification and Magnification 20 x 20 texels Texture [ 0, 0 ] [ 1, 0 ] [ 0, 1 ][ 1, 1 ] [ 0, 0 ] [ 1, 0 ] [ 0, 1 ][ 1, 1 ] 10 x 10 pixels Geometry (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

15 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/3)/(1/3)=0.1 [ 0, 0, 0 ] * [255,255,255] * 0.1 Bilinear Sampling (Sample Four Closest, Average) UV = [0.2,0] [26,26,26] ~Equation above, but in 4 directions

16 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[*,*]

17 Texture Samplers Revisited

18 Mipmaps 512 x x x x x x 16 8 x 8 4 x 4 2 x 2 1 x x 1024 Geometry 280 x 280 Geometry 80 x 80 Geometry

19 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

20 Anisotropic Filtering 512 x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 1 … 2X Anisotropic Filtering

21 Anisotropic Comparison

22 Programming Texture Samplers glGenSamplers(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/mip sd.AddressU = //Address mode for the U coord sd.AddressV = //Address mode for the V coord sd.AddressW = //Address mode for the W coord sd.MipLODBias = //Mip’s Level of detail bias sd.MaxAnisotropy = //Max level of anisotropy memcpy(sd.BorderColor, bc.data, sizeof(float)*4); //Copy in the color value for the border sd.MinLOD = //Minimum Level of Detail sd.MaxLOD = //Maximum Level of Detail sd.ComparisonFunc = //Compares sample data dev->CreateSamplerState(&sd, &sampler);

23 Binding Textures to Shaders glActiveTexture(GL_TEXTURE0 + slot); glBindSampler(slot, samplerID); //OpenGL does not require separate binds for each //shader type //bind to fragment shader con->VSSetSamplers(slot, 1, &sampler); //bind to vertex shader con->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

24 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


Download ppt "GAM531 DPS931 – Week 12 Textures and Samplers. Actor Composition Actor Mesh Vertex Buffer Index Buffer Vertex Format Material Shaders Uniform Buffers."

Similar presentations


Ads by Google