|
|
我是一个D3D初学者.
现在想从caputre接收到数据后,使用D3D进行渲染.
现在可以实现,但是不能够实现对视频图像的缩放.
设置的纹理为320x240大小,视频源大小也为320x240.显示窗口为800X600.
则显示后,有一部分为黑色,即不是正好显示320X240大小的区域.(参见附件的[视频源.JPG])
但是我从BMP文件建立一个纹理,则可以正确显示和缩放.
即使用下面的函数:
hr = D3DXCreateTextureFromFileEx( m_pD3DDevice, szTextureFilePath, 0,0,0,0,
D3DFMT_UNKNOWN,D3DPOOL_MANAGED,
D3DX_DEFAULT,D3DX_DEFAULT,dwKeyColor,
NULL,NULL,&m_pTexture);
对320X240大小的bmp图像,可以正确显示(参见附件的[BMP图像.JPG])
设置摄像机代码如下:
void C2DPanel::Setup2DCamera()
{
D3DXMATRIX matOrtho;
D3DXMATRIX matIdentity;
D3DXMatrixOrthoLH( &matOrtho,
(float)m_nScreenWidth, (float)m_nScreenHeight,
0.0,1.0);
D3DXMatrixIdentity(&matIdentity);
m_pD3DDevice->SetTransform(D3DTS_PROJECTION,&matOrtho);
m_pD3DDevice->SetTransform(D3DTS_WORLD, &matIdentity);
m_pD3DDevice->SetTransform(D3DTS_VIEW, &matIdentity);
m_pD3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE );
m_pD3DDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
}
初始化顶点程序如下:
bool C2DPanel::UpateVertices()
{
int i;
static int ver[4][2]={ -1,-1,
-1,1,
1,-1,
1,1
};
static float txt[4][2]={//0.0,1.0,
//0.0,0.0,
//1.0,1.0,
//1.0,0.0
1.0,0.0,
1.0,1.0,
0.0,0.0,
0.0,1.0
};
PANEL_CUSTOMVERTEX *pVertices = NULL;
m_pVertexBuffer->Lock( 0, 4*sizeof(PANEL_CUSTOMVERTEX), (BYTE**)&pVertices, 0 );
for( i=0; i<4; i++ )
{
pVertices.colour = m_dwColor;
pVertices.x = ver[0]*(m_nWidth)/2.0f;
pVertices.y = ver[1]*(m_nHeight)/2.0f;
pVertices.z = 1.0f;
pVertices.tu = txt[0];
pVertices.tv = txt[1];
}
m_pVertexBuffer->Unlock();
return true;
}
从视频源拷贝数据到纹理代码如下:
D3DLOCKED_RECT d3dlr;
if (FAILED(g_pTexture->LockRect(0, &d3dlr, 0, 0)))
return E_FAIL;
// Get the texture buffer & pitch
pTxtBuffer = static_cast<byte *>(d3dlr.pBits);
lTxtPitch = d3dlr.Pitch;
// Copy the bits
if (g_TextureFormat == D3DFMT_X8R8G8B8)
{
// Instead of copying data bytewise, we use DWORD alignment here.
// We also unroll loop by copying 4 pixels at once.
//
// original BYTE array is [b0][g0][r0][b1][g1][r1][b2][g2][r2][b3][g3][r3]
//
// aligned DWORD array is [b1 r0 g0 b0][g2 b2 r1 g1][r3 g3 b3 r2]
//
// We want to transform it to [ff r0 g0 b0][ff r1 g1 b1][ff r2 g2 b2][ff r3 b3 g3]
// below, bitwise operations do exactly this.
dwordWidth = m_lVidWidth / 4; // aligned width of the row, in DWORDS
// (pixel by 3 bytes over sizeof(DWORD))
for( row = 0; row< (UINT)m_lVidHeight; row++)
{
pdwS = ( DWORD*)pBmpBuffer;
pdwD = ( DWORD*)pTxtBuffer;
for( col = 0; col < dwordWidth; col ++ )
{
pdwD[0] = pdwS[0] | 0xFF000000;
pdwD[1] = ((pdwS[1]<<8) | 0xFF000000) | (pdwS[0]>>24);
pdwD[2] = ((pdwS[2]<<16) | 0xFF000000) | (pdwS[1]>>16);
pdwD[3] = 0xFF000000 | (pdwS[2]>>8);
pdwD +=4;
pdwS +=3;
}
// we might have remaining (misaligned) bytes here
pbS = (BYTE*) pdwS;
for( col = 0; col < (UINT)m_lVidWidth % 4; col++)
{
*pdwD = 0xFF000000 |
(pbS[2] << 16) |
(pbS[1] << 8) |
(pbS[0]);
pdwD++;
pbS += 3;
}
pBmpBuffer += m_lVidPitch;
pTxtBuffer += lTxtPitch;
}// for rows
}
谢谢各位高手指点?
|
|