先上图
纹理作为材质好处在于,在像素着色器赋值(值相当于采样的纹理坐标),而不是在顶点着色器赋值(顶点数组中赋值)
1,先在结构体添加纹理坐标,从而在顶点数组中设定值
struct Vertex
{
.......
D3DXVECTOR2 _texC;
Vertex(float x, float y, float z,
float nx, float ny, float nz,
float u, float v
)
{
.......
_texC = D3DXVECTOR2(u, v);
}
};
2,在layout中添加纹理坐标
// Create the vertex input layout.
D3D10_INPUT_ELEMENT_DESC vertexDesc[] =
{
.......
{"TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D10_INPUT_PER_VERTEX_DATA, 0},
};
3,创建漫反射纹理和高光纹理的着色器资源视图
D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"WoodCrate01.dds", 0, 0, &mDiffuseMapRV, 0);
D3DX10CreateShaderResourceViewFromFile(md3dDevice, L"defaultspec.dds", 0, 0, &mSpecMapRV, 0);
4,高光和漫反射纹理着色器视图的c++和shader交互
ID3D10EffectShaderResourceVariable* mfxDiffuseMapVar = mFX->GetVariableByName("gDiffuseMap")->AsShaderResource();
ID3D10EffectShaderResourceVariable* mfxSpecMapVar = mFX->GetVariableByName("gSpecMap")->AsShaderResource();
mfxDiffuseMapVar->SetResource(mDiffuseMapRV);
mfxSpecMapVar->SetResource(mSpecMapRV);
5,shader中纹理采样
Texture2D gDiffuseMap;
Texture2D gSpecMap;
SamplerState gTriLinearSam
{
Filter = MIN_MAG_MIP_LINEAR;
};
6,常量缓冲区变量纹理矩阵
ID3D10EffectMatrixVariable* mfxTexMtxVar = mFX->GetVariableByName("gTexMtx")->AsMatrix();
D3DXMATRIX texMtx;
D3DXMatrixIdentity(&texMtx);
mfxTexMtxVar->SetMatrix((float*)&texMtx);
7,顶点着色器中纹理矩阵转换
vOut.texC = mul(float4(vIn.texC, 0.0f, 1.0f), gTexMtx );
8,在纹理采样时,高光贴图的alpha分量的返回值在[0,1]之间,所以,必须调整为[0,256]
spec.a *= 256.0f;
来源:CSDN
作者:directx3d_beginner
链接:https://blog.csdn.net/directx3d_beginner/article/details/104167083