之前学习了纹理映射相关知识,但是仅仅是知道读取一个纹理贴图,然后在绘制之前设置一下纹理,但是并不知道纹理设置的一些具体细节,今天就来填坑啦!
//顶点数据 vertex[0] = stVertex(-250.0f, 0.0f, 0.0f, 0.0f, 2.0f); vertex[1] = stVertex(-250.0f, 500.0f, 0.0f, 0.0f, 0.0f); vertex[2] = stVertex( 250.0f, 0.0f, 0.0f, 2.0f, 2.0f); vertex[3] = stVertex( 250.0f, 500.0f, 0.0f, 2.0f, 0.0f);这样,一张图片就不能完整的覆盖住模型啦,我们的纹理寻址方式就要派上用场啦!
typedef enum D3DTEXTUREADDRESS { D3DTADDRESS_WRAP = 1, //重复寻址模式 D3DTADDRESS_MIRROR = 2, //镜像寻址模式 D3DTADDRESS_CLAMP = 3, //夹取寻址模式 D3DTADDRESS_BORDER = 4, //边框颜色寻址模式 D3DTADDRESS_MIRRORONCE = 5, D3DTADDRESS_FORCE_DWORD =0x7fffffff } D3DTEXTUREADDRESS, *LPD3DTEXTUREADDRESS;我们只需要在绘制之前,设置纹理,再设置一下纹理寻址方式就可以啦!
//设置纹理寻址方式 g_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); g_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
//设置纹理寻址方式 g_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_MIRROR); g_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_MIRROR);
//设置纹理寻址方式 g_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); g_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
g_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER); g_pDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER);
//顶点数据 vertex[0] = stVertex(-250.0f, 0.0f, 0.0f, 0.0f, 0.5f); vertex[1] = stVertex(-250.0f, 500.0f, 0.0f, 0.0f, 0.0f); vertex[2] = stVertex( 250.0f, 0.0f, 0.0f, 0.5f, 0.5f); vertex[3] = stVertex( 250.0f, 500.0f, 0.0f, 0.5f, 0.0f);
//设置为最近点采样过滤方式 g_pDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); g_pDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);
//设置为线性纹理过滤方式 g_pDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); g_pDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
//设置为各向异性纹理过滤 g_pDevice->SetSamplerState(0, D3DSAMP_MAXANISOTROPY, 3);<span style="white-space:pre"> </span>//设置各向异性最大程度值(大于1的值) g_pDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC); g_pDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);
HRESULT D3DXCreateTextureFromFileEx( _In_ LPDIRECT3DDEVICE9pDevice, //D3D设备接口对象 _In_ LPCTSTR pSrcFile, //纹理贴图的文件地址 _In_ UINT Width, //纹理宽度,为0的话表示使用贴图的宽度 _In_ UINT Height, //纹理高度,为0的话表示使用贴图的高度 _In_ UINT MipLevels, //生成的渐进纹理的级数 _In_ DWORD Usage, //用法的标志,通常设为0 _In_ D3DFORMAT Format, //纹理贴图的格式 _In_ D3DPOOL Pool, //保存纹理的方式 _In_ DWORD Filter, //纹理过滤方式 _In_ DWORD MipFilter, //生成纹理序列的过滤方式 _In_ D3DCOLOR ColorKey, //替换Alpha值得颜色值 _Inout_ D3DXIMAGE_INFO *pSrcInfo, //通常设为NULL就可以了 _Out_ PALETTEENTRY *pPalette, //调色板的地址,通常设为NULL即可 _Out_ LPDIRECT3DTEXTURE9*ppTexture //纹理接口对象 );
<span style="white-space:pre"> </span>//设置纹理最大级过滤级数 g_pDevice->SetSamplerState(0, D3DSAMP_MAXMIPLEVEL, 5); //设置为多级渐进纹理过滤方式配合渐进纹理方式 g_pDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);