Jump to content
  • Advertisement
Sign in to follow this  
Crazy Chicken

OpenGL Creating a texture form array ????

This topic is 5424 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post

Link to post
Share on other sites
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,

Share this post

Link to post
Share on other sites
No problem:

LPDIRECT3DTEXTURE9 CreateTexture(const int w, const int h, const void *data) {
// create 32 bit ARGB texture, no mipmaps.
if (FAILED(D3DXCreateTexture(g_pDevice, w, h, 1, D3DUSAGE_DYNAMIC,
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)) {
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.

return result;

From the top of my head - please don't shoot me if it blows up your box [lol].


[edit]Fixed broken code[/edit]

Share this post

Link to post
Share on other sites
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]),

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!