Creating Textures from arrays in memory
Hi! I''m more used to Opengl than Direct 3D, and I have a little problem.
When creating textures in Opengl, I can create textures without need of files like .bmp, .jpg and stuff..
like this: gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width,height,GL_RGB,GL_UNSIGNED_BYTE,pData);
where pData is an array of bytes. (3 bytes is one pixel).
In D3D, I thought I should use D3DXCreateTexture to create a empty texture and D3DXFillTexture to fill it with the data.
(Correct me if I''m wrong..)
Later on, in the FillTexture func. I have to make my own fill function with a predefined parameterlist. But I don''t have a clue on how to make that function.
Here is what I thought.. But it won''t work.. Maybe I''m doin it totaly wrong or something.
VOID WINAPI fillFunc (D3DXVECTOR4* pOut, const D3DXVECTOR2* pTexCoord, const D3DXVECTOR2* pTexelSize, LPVOID pData)
{
static long pos = 0;
float r,g,b,a;
unsigned char* pImage = (unsigned char*)pData;
r = pImage[pos];
g = pImage[pos+1];
b = pImage[pos+2];
a = 0;
*pOut = D3DXVECTOR4(r,g,b,a);
pos +=3;
}
Please can someone help me?
Like most other surfaces in D3D, you have to lock it to gain access to the texture bits.
Read up on the LockRect() member function of the texture surface, and have a look at some of the samples which fill textures.
Read up on the LockRect() member function of the texture surface, and have a look at some of the samples which fill textures.
Take a look at the source code of D3DFont provided with the SDK. The code creates a texture on the fly from a memory Windows bitmap and then gains access to the surface, locks it, and finally adjusts the alpha bits.
HTH
Laurent - http://jeux-directx.com/
HTH
Laurent - http://jeux-directx.com/
Here''s a quick example of using LockRect() to lock the texture, then copying an image from memory onto the texture. Note that LockRect() won''t work if you create a texture using D3DPOOL_DEFAULT. Here it''s assumed the texture is 32-bit, and I''m just setting all the alpha values to 0xFF.
I hope there are no bugs in this.
bool CopyImageToTexture(UCHAR *pSrc, int xWidth, int yHeight, LPDIRECT3DTEXTURE8 pTex, int xPos, int yPos){ D3DLOCKED_RECT d3drc; UCHAR r, g, b; UINT *pDest; int nRow, nPixel; // lock the texture if (FAILED(pTex->LockRect(0, &d3drc, NULL, 0))) return false; // adjust pitch from bytes to UINTs d3drc.Pitch >>= 2; // copy the image for (nRow = 0; nRow < yHeight; nRow++) { // set destination pointer for this row pDest = (UINT*)d3drc.pBits + (nRow + yPos) * d3drc.Pitch + xPos; // copy the row for (nPixel = 0; nPixel < xWidth; nPixel++) { // extract pixel data r = *pSrc++; g = *pSrc++; b = *pSrc++; // write color word to texture (*pDest++) = 0xFF000000 | (r << 16) | (g << 8) | b; } } // unlock texture pTex->UnlockRect(0); // return success return true;}
I hope there are no bugs in this.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement