MULTIMEDIA

DirectX 10 Game Programming : Direct3D - Textures and Details (part 1) - Adding Support for Texture Mapping - Texturing Coordinates and Sampling

1/7/2014 2:57:51 AM
2.2 Texture Loading and Shaders

When texturing a 3D object, the texture and the method used to load it is very similar to the way textures were loaded for sprites. First the texture itself needs to be loaded into an ID3D10Texture2D object and a resource view created from it.

Secondly, the shader needs to know how to gain access to the texture you just loaded in. Letting the shader know to expect a texture is simply a matter of creating a named variable that is set as external using the extern keyword.

// Set up a named variable within the shader to hold the texture.
extern Texture2D baseTexture;

The variable is then accessible to the runtime portion of your code through binding.

// Bind the texture variable
ID3D10EffectShaderResourceVariable* pBaseTextureVariable = NULL;
pBaseTextureVariable = modelObject->pEffect->
GetVariableByName("baseTexture")->AsShaderResource();

The pBaseTextureVariable is now bound to the baseTexture variable declared in the shader. Similar to the matrix and float variables you’ve bound before, this gives you access to change and update the values used by the shader.

The texture can be updated using the SetResource method available through the pBaseTextureVariable.

// Set the texture resource view
pBaseTextureVariable->SetResource(pTextureRV);

Note

Shaders make use of the texture resource view, not the actual texture object.


2.3 Texturing Coordinates and Sampling

Since the texture coordinates were added to the vertex format previously, the shader now needs to be updated to handle them as well. Since all the vertices pass through the vertex shader portion first, the input parameters need to be changed. Additionally, the PS_INPUT structure must support the inclusion of texture coordinates to pass on to the pixel shader.

The new versions of the PS_INPUT structure and vertex shader are shown next.

////// This structure is created and filled in by the
// vertex shader
struct PS_INPUT
{
float4 Pos : SV_POSITION;
float4 Color : COLOR0;
float2 Tex : TEXCOORD0;
};

////////////////////////////////////////////////
// Vertex Shader - Main Function
///////////////////////////////////////////////
PS_INPUT VS(float4 Pos : POSITION, float2 Tex: TEXCOORD)
{
PS_INPUT psInput;
psInput.Pos = Pos;
psInput.Color = float4(1.0f, 1.0f, 1.0f, 1.0f);
psInput.Tex = Tex;

return psInput;
}

The vertex shader isn’t responsible for doing anything to the texture coordinates other than just setting them and passing them along to the pixel shader.

Texturing is performed in the pixel shader portion of an effect. Instead of the pixel shader returning a preset color as its output, the shader returns a color based on the texture coordinate location in the texture file; this process is called sampling.

Sampling uses the texture coordinates to determine which area of the texture is going to be used as the output color. How the texture is sampled is controlled by the SamplerState. The SamplerState is a named section within the shader file that contains information as to how the texture is to be filtered as well as how the texture should be mapped. There are three ways the texture can be mapped:

  • Wrap— The texture repeats across the surface.

  • Clamp— If the texture does not completely map to the area, the edge color will be repeated causing the texture to look streaked.

  • Mirror— The texture is repeated but reversed each time it is drawn.

Figure 2 shows an example of each type of mapping.

Figure 2. A wrapped, clamped, and mirrored texture.


An example SamplerState is shown next. It uses linear filtering and wraps the texture during sampling.

SamplerState samLinear
{
Filter = MIN_MAG_MIP_LINEAR;
AddressU = Wrap;
AddressV = Wrap;
};

The SamplerState is needed within the pixel shader during the actual sampling process. The ability to sample a texture is built-in to Direct3D and uses the shader function Sample.

Sample is one of the functions made available through the Texture2D object and takes two parameters. The first parameter is a SamplerState object. The second parameter is the pair of texture coordinates to be sampled. The Sample function uses the input texture coordinates to do a lookup in a texture and then uses the SamplerState to determine the color to be mapped.

///////////////////////////////////////////////
// Pixel Shader
///////////////////////////////////////////////
float4 PS(PS_INPUT psInput) : SV_Target
{
return baseTexture.Sample(samLinear, psInput.Tex) * psInput.Color;
}

Note

Previous versions of HLSL used the tex2D function for sampling from 2D textures. The Sample function was introduced in Shader Model 4.0 and has the advantage of supporting 2D and 3D textures based on the texture object.

Other  
  •  DirectX 10 Game Programming : Shaders and Effects - Geometry Shaders
  •  Adobe Photoshop CS5 : Managing Color from Monitor to Print - Working with Rendering Intents
  •  Adobe Photoshop CS5 : Managing Color from Monitor to Print - Changing from Additive (RGB) to Subtractive (CMYK) Color
  •  Adobe Photoshop CS5 : Managing Color from Monitor to Print - Setting Up Soft-Proof Colors
  •  Adobe Photoshop CS5 : Managing Color from Monitor to Print - Producing Consistent Color
  •  Adobe Flash Professional CS5 : Creating Frame-by-Frame Animation - Editing Keyframes
  •  Adobe Flash Professional CS5 : Creating Frame-by-Frame Animation - Clearing Keyframes Versus Removing Frames
  •  Adobe Flash Professional CS5 : Creating Frame-by-Frame Animation - Converting Frames to Keyframes, Using Frame-Based Versus Span-Based Selection
  •  Adobe Flash Professional CS5 : Creating Frame-by-Frame Animation - Creating Keyframes
  •  Adobe Flash Professional CS5 : Creating Frame-by-Frame Animation - Understanding Frame-by-Frame Animation
  •  
    Video
    Video tutorials
    - How To Install Windows 8

    - How To Install Windows Server 2012

    - How To Install Windows Server 2012 On VirtualBox

    - How To Disable Windows 8 Metro UI

    - How To Change Account Picture In Windows 8

    - How To Unlock Administrator Account in Windows 8

    - How To Restart, Log Off And Shutdown Windows 8

    - How To Login To Skype Using A Microsoft Account

    - How To Enable Aero Glass Effect In Windows 8

    - How To Install Windows Store Apps From Windows 8 Classic Desktop

    - How To Disable Windows Update in Windows 8

    - How To Disable Windows 8 Metro UI

    - How To Add Widgets To Windows 8 Lock Screen

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010
    programming4us programming4us
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone