Archived

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

thedo

Terrains and bitmaps

Recommended Posts

thedo    124
I am writing a terrain loader which take a loaded grayscale bitmap and convertes it to a vertex buffer. I have written the bulk of my code BUT (and this is the reason i''m posting) how do i read pixel data from a bitmap? It is in a LPDIRECT3DTEXTURE8 object. I have tried locking the texture, but i am not sure what to do now. Help please, thanks in advance. Neil WHATCHA GONNA DO WHEN THE LARGEST ARMS IN THE WORLD RUN WILD ON YOU?!?!

Share this post


Link to post
Share on other sites
malachii    122
Thedo,

I have been looking into this in order to apply colors to my landscape (I''m writing the bitmap rather than reading it, but the concept is the same)

First you have to lock the surface (I didn''t have any luck locking just a portion, so I just locked it all by passing 0).

Then you have to update the pixels directly in the memory pointer returned to you. In order to do this you must know the surface format, but thankfully, you should know what it is since you created (or supplied) the bitmap.

To be even better, you can guarantee the format (force it to a specified format) (eg: 8 bits of red, 8 bits of green, 8 bits of blue, 8 bits of alpha, etc) at load-time using LoadTextureEx (can''t remember the exact name of the Dx8 function for loading textures, but it''s in the D3D8X library).

Here is some code I''ve just whipped together (untested) but it should work. Sorry I can''t get by tab key to work in here, so there''s no indenting. The code assumes as 32 bit image (RGBA). The x4 in the color assignment is due to the fact that each pixel requires 4 bytes of space in a 32 bit image (8 bits for each of R, G, B, A).

Good luck!


// lock the entire texture and update it
D3DLOCKED_RECT lr;
unsigned char *bits;
unsigned char r,g,b,a;

res= m_lightMap->LockRect(0, &lr, NULL, NULL);
buf= (unsigned char*)lr.pBits;

for (ty=0; ty<=256; ty++)
{
for (tx=0; tx<=256; tx++)
{
b= bits[256*4*ty + tx*4+0]= shade;
g= bits[256*4*ty + tx*4+1]= shade;
r= bits[256*4*ty + tx*4+2]= shade;
a= bits[256*4*ty + tx*4+3]= 255;
}
}

m_lightMap->UnlockRect(0);



Share this post


Link to post
Share on other sites