3. Drawing the Textured Object
It
might seem like a lot of work just to map a single texture to a 3D
object, but once you have the changes made to the shader, that code can
be reused, simplifying your job in the future.
At
this point the vertex format has been changed to support texture
coordinates, the texture is loaded, and the shader knows how to map
that texture to the object; finally, you can see what it looks like.
The Render function that makes use of the changes you’ve already made is shown next, with the additional SetResource call made to update the texture with the shader.
/*******************************************************************
* Render
* All drawing happens in the Render function
* Inputs - void
* Outputs - void
*******************************************************************/
void Render()
{
if (pD3DDevice != NULL)
{
// clear the target buffer
pD3DDevice->ClearRenderTargetView(pRenderTargetView, D3DXCOLOR(0.0f,
1.0f, 1.0f, 0.0f));
// Set the input layout
pD3DDevice->IASetInputLayout(gModelObject.pVertexLayout);
// Set vertex buffer
UINT stride = sizeof(VertexPosUVStruct);
UINT offset = 0;
pD3DDevice->IASetVertexBuffers(0, 1, &gModelObject.pVertexBuffer,
&stride, &offset);
// Set primitive topology
pD3DDevice-
>IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
// Set the texture resource view
pBaseTextureVariable->SetResource(pTextureRV);
// Render a model object
D3D10_TECHNIQUE_DESC techniqueDescription;
gModelObject.pTechnique->GetDesc(&techniqueDescription);
// Loop through the technique passes
for(UINT p=0; p < techniqueDescription.Passes; ++p)
{
gModelObject.pTechnique->GetPassByIndex(p)->Apply(0);
pD3DDevice->Draw(gModelObject.numVertices, 0);
}
// display the next item in the swap chain
pSwapChain->Present(0, 0);
}
}
Figure 3 shows what the texture-mapped quad should look like.