Creating a texture form array ????
Hi I am currently working on some procedural texture generation code. I have an one dimensional array which holds all my new texture data. How can I dynamically create a texture using this data. I am aware of a function in OpenGL called glTexImage2D. Is there something like this in DirectX ?
Thx
Luke
Use D3DXCreateTexture to create an empty texture, then lock the texture (pTexture->LockRect()) , write your data and unlock it.
Be sure to use the correct pixel format.
Good luck,
Pat
Be sure to use the correct pixel format.
Good luck,
Pat
Thx I understand how it works but I am still not very comfortable with DirectX. Could you maybe be a little more specific please.
Luke
Luke
No problem:
From the top of my head - please don't shoot me if it blows up your box [lol].
Pat.
[edit]Fixed broken code[/edit]
LPDIRECT3DTEXTURE9 CreateTexture(const int w, const int h, const void *data) { LPDIRECT3DTEXTURE9 result = 0; // create 32 bit ARGB texture, no mipmaps. if (FAILED(D3DXCreateTexture(g_pDevice, w, h, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, // <- just read that dynamic textures don't work with managed pool &result)) { return 0; } // lock the texture to access pixel data. D3DLOCKED_RECT lockedRect; if (FAILED(result->LockRect(0, &lockedRect, 0, D3DLOCK_DISCARD)) { result->Release(); return (result = 0); } // copy data. LPBYTE textureData = static_cast<LPBYTE>(lockedRect.pBits); const BYTE * sourceData = static_cast<const BYTE*>(data); const int stride = w * 4; for (int y = 0; y < h; ++y) { memcpy(textureData, sourceData, stride); textureData += lockedrect.Pitch; sourceData += stride; } // unlock the texture. result->UnlockRect(); return result;)
From the top of my head - please don't shoot me if it blows up your box [lol].
Pat.
[edit]Fixed broken code[/edit]
Ah well - some comments on the code snippet above.
It assumes DX9 but should work equally on DX8 (just change result's type to LPDIRECT3DTEXTURE8).
g_pDevicve is the pointer to your D3D device instance.
The function will return a newly created texture if everything works. You can specify width and height of the target texture (w and h parameters) and pass your generated texture data in.
The for loop takes care about copying data. It is done line by line since the created texture might have a different line pitch than the pixel width you've specified.
If you want to re-use the texture you can modify the function a little (e.g. not creating a new texture but locking and filling an aexisting one).
Note that the function does not check for NULL input data.
Hope this helps (and the code won't blow your machine[wink]),
Pat.
It assumes DX9 but should work equally on DX8 (just change result's type to LPDIRECT3DTEXTURE8).
g_pDevicve is the pointer to your D3D device instance.
The function will return a newly created texture if everything works. You can specify width and height of the target texture (w and h parameters) and pass your generated texture data in.
The for loop takes care about copying data. It is done line by line since the created texture might have a different line pitch than the pixel width you've specified.
If you want to re-use the texture you can modify the function a little (e.g. not creating a new texture but locking and filling an aexisting one).
Note that the function does not check for NULL input data.
Hope this helps (and the code won't blow your machine[wink]),
Pat.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement