本文列举了Direct3D中各种纹理应用实现:黑暗贴图,发光贴图,漫反射映射贴图,细节纹理,纹理混合,有较详尽的注解。其中黑暗贴图,发光贴图,细节纹理都是采用多重纹理的方法实现(也可以采用多次渲染混合实现)。
示例代码使用Beginning direct3D game programming中的框架,省去不少事情,可以专注纹理话题。代码:点此下载
下面来看代码与效果:
正常的纹理贴图效果:
正常的纹理贴图代码:
//基本纹理
void drawNormalTexture()
{
// 设置box纹理贴图
Device->SetTexture(0, texBox);
Device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0); //使用纹理坐标
Device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); // 颜色来源-材质
Device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); // 使用当前颜色作为第一个texture stage的输出
// 描绘box
Box->draw(0, 0, 0);
}
黑暗纹理贴图效果:
黑暗纹理贴图代码:
//黑暗映射纹理
void drawDarkMapTexture()
{
// Multi texture:多重纹理,此处为两重纹理
// finalColor = destPixelColor * sourcePixelColor
// 设置box纹理贴图
Device->SetTexture(0, texBox);
Device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
Device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); // 颜色来源-材质
Device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); // 使用当前颜色作为第一个texture stage的输出
// 设置黑暗纹理贴图
Device->SetTexture(1, texAlpha);
Device->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);
Device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); // 颜色来源-材质
Device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); // 颜色来源-前一个texture stage
Device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE); // 颜色混合:相乘
// 描绘box
Box->draw(0, 0, 0);
}
漫反射映射贴图效果:夜光镜效果
漫反射映射贴图代码:
//漫射光映射纹理
void drawDiffuseTexture()
{
// 设置box纹理贴图
Device->SetTexture(0, texBox);
Device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
Device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); // 颜色来源-材质
Device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); // 颜色来源-漫反射
Device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE); // 颜色混合
// 设置材质:绿色材质实现类似夜光镜的效果
Device->SetMaterial(&d3d::GREEN_MTRL);
// 描绘box
Box->draw(0, 0, 0);
}
发光映射纹理贴图效果:
发光映射纹理贴图代码:
//发光映射纹理
void drawGlowMapTexture()
{
// Multi texture:多重纹理,此处为两重纹理
// finalColor = sourcePixelColor * 1.0 + destPixelColor * 1.0
// 设置box纹理贴图
Device->SetTexture(0, texBox);
Device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
Device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); // 颜色来源-材质
Device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); // 使用当前颜色作为第一个texture stage的输出
// 设置黑暗纹理贴图
Device->SetTexture(1, texAlpha);
Device->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);
Device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); // 颜色来源-材质
Device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); // 颜色来源-前一个texture stage
Device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD); // 颜色混合:相加
// 描绘box
Box->draw(0, 0, 0);
}
细节映射纹理贴图:实现粗糙的凹凸效果
细节映射纹理贴图代码:
//细节映射纹理:实现凹凸效果
void drawDetailMapTexture()
{
// Multi texture:多重纹理,此处为两重纹理
// finalColor = sourcePixelColor * destPixelColor + destPixelColor * sourcePixelColor
// 设置box纹理贴图
Device->SetTexture(0, texBox);
Device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
Device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); // 颜色来源-材质
Device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); // 使用当前颜色作为第一个texture stage的输出
// 设置细节纹理贴图
Device->SetTexture(1, texDetail);
Device->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0);
Device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); // 颜色来源-材质
Device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); // 颜色来源-前一个渲染通道
Device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADDSIGNED); // 颜色混合
// 描绘box
Box->draw(0, 0, 0);
}
alpha纹理混合效果:多次渲染实现
alph纹理混合代码:
//alpha混合纹理
void drawAlphaBlendTexture()
{
// 多次渲染实现纹理混合
// finalColor = sourcePixelColor * sourceBlendFactor + destPixelColor * destBlendFactor
// 设置纹理混合参数
Device->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); // alpha值来自纹理
Device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
// 设置混合因子实现透明效果
Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
//使用box纹理贴图实现第一次渲染,无alpha混合
Device->SetTexture(0, texBox);
Device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
Device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); // 颜色来源-材质
Device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); // 使用当前颜色作为第一个texture stage的输出
// 第一次描绘box
Box->draw(&boxWorldMatrix, 0, 0);
//使用带alpha值得flower纹理贴图实现第二次渲染,有alpha混合
Device->SetTexture(0, texAlphaFlower);
Device->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
Device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); // 颜色来源-材质
Device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); // 使用当前颜色作为第一个texture stage的输出
// 打开纹理混合
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, true);
// 第一次描绘box
Box->draw(&boxWorldMatrix, 0, 0);
// 关闭纹理混合
Device->SetRenderState(D3DRS_ALPHABLENDENABLE, false);
}原文:http://www.cnblogs.com/zhehan54/p/5503580.html