緣起: 本篇在此篇基礎上來寫的
《精通DirectX 3D》第六章 紋理映射基礎 01_TextureBase 為看如下鏈接做知識儲備,因為不熟悉
動態紋理 玩玩DirectShow--(2) NVIDIA SDK 9.5 GPU Video Effects 截圖:
過程: 這句是從圖片文件來創建紋理
D3DXCreateTextureFromFile( g_pd3dDevice, L"texture.rectangle.uv.png", &g_pTexture )
我們換成動態創建紋理
g_pd3dDevice->CreateTexture(nTextureWidth, nTextureHeight, 1, D3DUSAGE_DYNAMIC
, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &g_pTexture, NULL)
此時運行效果如下,texture里面每個像素的數據都是未初始化的(不一定是0x00000000)

我們鎖住紋理再解鎖試試
// 鎖住紋理
D3DLOCKED_RECT d3dLocked_rect;
if( FAILED(g_pTexture->LockRect(0, &d3dLocked_rect, 0, D3DLOCK_DISCARD)))
return E_FAIL;
// 紋理解鎖
if (FAILED(g_pTexture->UnlockRect(0)))
return E_FAIL;
此時效果

看來每個像素的數據都初始化了(變成0x00000000)
我們填充下數據,讓全國河山一片紅
每個像素使用4個字節,即一個DWORD,我們每次填充4個像素的數據
// 填充數據
BYTE* pTextureBuffer = static_cast<BYTE*>(d3dLocked_rect.pBits);
INT nTexturePitch = d3dLocked_rect.Pitch;
for (UINT row = 0; row < nTextureHeight; ++row)
{
DWORD* pdwDest = (DWORD*)pTextureBuffer;
for (UINT column = 0; column < nTextureWidth/4; ++column)
{
pdwDest[0] = 0xFFFF0000;
pdwDest[1] = 0xFFFF0000;
pdwDest[2] = 0xFFFF0000;
pdwDest[3] = 0xFFFF0000;
pdwDest +=4;
}
pTextureBuffer += nTexturePitch;
}
啊,紅了

再動動花花腸子
// 填充數據
BYTE* pTextureBuffer = static_cast<BYTE*>(d3dLocked_rect.pBits);
INT nTexturePitch = d3dLocked_rect.Pitch;
for (UINT row = 0; row < nTextureHeight; ++row)
{
DWORD* pdwDest = (DWORD*)pTextureBuffer;
UINT nPartWidth = nTextureWidth/4/4;
for (UINT column = 0; column < nPartWidth; ++column)
{
pdwDest[0] = 0xFFFF0000;
pdwDest[1] = 0xFFFF0000;
pdwDest[2] = 0xFFFF0000;
pdwDest[3] = 0xFFFF0000;
pdwDest +=4;
}
for (UINT column = nPartWidth; column < nPartWidth*2; ++column)
{
pdwDest[0] = 0xFF00FF00;
pdwDest[1] = 0xFF00FF00;
pdwDest[2] = 0xFF00FF00;
pdwDest[3] = 0xFF00FF00;
pdwDest +=4;
}
for (UINT column = nPartWidth*2; column < nPartWidth*3; ++column)
{
pdwDest[0] = 0xFF0000FF;
pdwDest[1] = 0xFF0000FF;
pdwDest[2] = 0xFF0000FF;
pdwDest[3] = 0xFF0000FF;
pdwDest +=4;
}
for (UINT column = nPartWidth*3; column < nTextureWidth/4; ++column)
{
pdwDest[0] = 0xFF000000;
pdwDest[1] = 0xFF000000;
pdwDest[2] = 0xFF000000;
pdwDest[3] = 0xFF000000;
pdwDest +=4;
}
pTextureBuffer += nTexturePitch;
}
啊,好像哪個國家的國旗啊

當然這樣子的也不在話下了
posted on 2008-12-25 03:06
七星重劍 閱讀(2931)
評論(3) 編輯 收藏 引用 所屬分類:
Game Graphics