Archived

This topic is now archived and is closed to further replies.

Christoph

Easy question concerning textures

Recommended Posts

Hello, I have an array of colors and want to apply it to a texture. Can somebody tell me exactly how to create a texture (512x512) in DX9 and change the pixels? (Must I use a surface for this?) Greetings, Chris

Share this post


Link to post
Share on other sites
Filling a texture with data has been covered many times in the forum. Do a search through pasts posts and you''ll find all the info you need.

In general, these are the steps:

D3DXCreateTexture(m_pd3dDevice, 512, 512, D3DX_DEFAULT, 0, /*some format*/, D3DPOOL_DEFAULT, &pTexture);

pTexture->GetSurfaceLevel(0, &pSurface);

D3DLOCKED_RECT lockedRect;
pSurface->LockRect(&lockedRect, NULL, 0);
// access pixels of the surface.

pSurface->UnlockRect();

// Fill your mipmap sublevels if you need to.

pTexture->GenerateMipSubLevels();

The most difficult part about this is accessing the individual pixels when you lock your surface. How you do that depends on the format of your texture. There have been many posts about this in the forum. A good search should turn up all the info you need.

neneboricua

Share this post


Link to post
Share on other sites
Hmmm, my trouble is that I''m using VB .NET, and nowhere is an example how to manipulate textures.
I''ve experimented a lot now but I can''t see any solution...
For example, what is wrong in the following second call (Error: "Value can''t be 0")?


texture = New Texture(d3dDev, 512, 512, 0, Usage.Dynamic, d3dDev.PresentationParameters.BackBufferFormat, Pool.Managed)

Dim data(,) As Color = CType(texture.LockRectangle(GetType(Color), 0, New Rectangle(0, 0, 512, 512), 0), Color(,))

Share this post


Link to post
Share on other sites
(Dunno if this is identical in managed/unmanaged, but...)

D3DXLoadSurfaceFromMemory works quite nicely. All you have to do is first call GetSurfaceLevel() to get a surface (usually index 0), fill in a RECT structure, and pass in a bunch of parameters, some of which will be NULL.
//Pardon the C++, hopefully the managed

//stuff won''t be much different.

//This just creates a texture, and

//uses a single D3DCOLOR value to fill

//in the entire texture, but instead

//of just a 4-byte color value, you can

//use an entire array in the same way.

LPDIRECT3DTEXTURE9 pTexture;
pDevice->CreateTexture(512, 512, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &pTexture, NULL);
LPDIRECT3DSURFACE9 pSurface;
(*ppTexture)->mpTexture->GetSurfaceLevel(0, &pSurface);
D3DCOLOR BackgroundColor = 0xFFFFFFFF;
SrcRect.left = 0;
SrcRect.top = 0;
SrcRect.right = 1;
SrcRect.bottom = 1;
//The third parameter is the rectangle

//for where to load the data TO.

//Passing NULL just makes it copy to the

//entire surface, and the linear filter

//makes sure that the source gets stretched

//to cover the destination. If you have

//a 512x512 array, you might as well use

//D3DX_FILTER_NONE as the filter.

D3DXLoadSurfaceFromMemory(pSurface, NULL, NULL, &BackgroundColor, D3DFMT_A8R8G8B8, 4, NULL, &SrcRect, D3DX_FILTER_LINEAR, 0x00000000);
pSurface->Release();

Share this post


Link to post
Share on other sites
quote:
Original post by Christoph
texture = New Texture(d3dDev, 512, 512, 0, Usage.Dynamic, d3dDev.PresentationParameters.BackBufferFormat, Pool.Managed)

Dim data(,) As Color = CType(texture.LockRectangle(GetType(Color), 0, New Rectangle(0, 0, 512, 512), 0), Color(,))


One problem with this code is that you''re trying to create a dynamic texture in the managed memory pool. The docs say that you cannot do this.

If this is a texture that you''re going to be updating often, then make it dynamic but store it in the default memory pool. If it is a texture that will only be updated when it is created, then there is no need to make it dynamic and you can have a usage parameter of 0 and use the managed memory pool.

I don''t know much VB but that second line looks very strange to me. Having parenthesis with a single comma inside them seems like an error to me but I could be wrong.

neneboricua

Share this post


Link to post
Share on other sites