Archived

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

zask

Once more. How can i load the texture NOT from file in DX8?

Recommended Posts

I don''t know if there''s a very simple way to load a texture from a windows handle.

You can load from a resource:
D3DXCreateTextureFromResource

You can load from a file that is in memory:
D3DXCreateTextureFromFileInMemory

And you can create a blank texture:
D3DXCreateTexture

But if you want to load from a windows bitmap, then you''ll probably have to copy the data in yourself.

John B

Share this post


Link to post
Share on other sites
Whats meen "you''ll probably have to copy the data in yourself."?
And if it isn''t difficult the sample of code for each variant,
please.

Share this post


Link to post
Share on other sites
Take a look at what''s done in D3DFont.

Stop whining: You already have the source code.


Laurent - http://www.lafaqmfc.com/
My little game: http://www.lafaqmfc.com/home/play_starshooter.htm

Share this post


Link to post
Share on other sites
Basically, what everyone is saying is, "just load the dang thing from file."

You aren''t explaining why the thing must come from a handle. Indeed most of us don''t use handles for graphics when dealing with DirectX. The only time i ever used handles with bitmaps is 1) resources (johnbsmall tells you the function you need for resources) 2) using the default windows graphics renderer (in which case, why do you need directx functions?)

The graphic could just as easily come from a file, or a file in memory, or a resource. You also aren''t explaining what you plan on doing with the bitmap. Blit it to a directx tex? Use it as a texture? Write a paint program?

Without these two details, everyone is going to keep suggesting "D3DXCreateTextureFromFileInMemory" or "D3DXCreateTextureFromResource".

And i don''t mean to be a jerk, but in these forums most people will point you to the right function, then it''s up to YOU to figure out how to use it (i suggest looking at www.msdn.microsoft.com to figure out how to use the functions.) Most ppl will not take the time to right you sample code -- we have our own code to write.

DmGoober

Share this post


Link to post
Share on other sites
IDirect3DTexture8::LockRect

and something like

GetDIBits

Make sure your bitmap is in the same color format as the direct3d surface. If not, use GDI to blt it to a bitmap with the same color resolution (CreateDIBSection).

Anyways, you lock the texture and copy all the pixels in one line at a time. At the end of the line you have to add the pitch given by IDirect3DTexture8::LockRect, since you are not guarenteed sucessive lines are contiguous in memory.

Share this post


Link to post
Share on other sites
To invective: May you write a piece of code? I not absolutely have understood how the destination surface described.

Share this post


Link to post
Share on other sites
quote:
Original post by zask
To invective: May you write a piece of code? I not absolutely have understood how the destination surface described.


As has been said a million times before, people here will point you in the right direction, but its up to you to learn it.

Share this post


Link to post
Share on other sites
Has loaded texture through a handle! Resume:

JohnBSmall - set of banal things.
Laurent - has confused the text and texture.
DmGoober - is at least polite.
Invective - my sincere thanks.

Today Siberia has gone sharply upward!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi there. I have the same questions that Zask. I''m making bitmaps in real_time and I have a rectangular( 2 triangles ) that I use to show my bitmaps. The way I doing it now is using
the function "D3DXCreateTextureFromFile()", but i have to write a file to my disk and then read it back again, so the performance is really bad. Here is the function I''m using to set the new texture with the new bitmap just created with the function "populated_this_bitmap()"
bool graphics::SetTexture(
int slide_id
){
HBITMAP slide_bitmap;
pointer_to_screen->populate_this_bitmap(slide_id);
{
CImage my_image;
my_image.Attach(slide_bitmap);
my_image.Save("c:\\my_image.jpg";
my_image.Destroy();
}
if(FAILED(D3DXCreateTextureFromFile(g_pd3dDevice,"c:\\my_image.jpg" ,&m_pTexture)))
{
return false;
}
show_slide=1;
DeleteObject((HGDIOBJ)(HBITMAP)(slide_bitmap));
return true;
}

I was reading the answer from invective about using IDirect3DTexture8::LockRect() but could somoene explain better how to use it. Iknow you will have some kind of for loop populating the new texture but how can you do that..Also could you use "Blit" to copy the entire bitmap into the texture

Share this post


Link to post
Share on other sites
AP: I posted this code in another thread just a few days ago:

   
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;
}


This is a pretty specific function; it assumes pSrc points to an array of 24-bit color words, so there''s no alpha channel. It assumes the texture format is A8R8G8B8 (and sets the alpha channel to 0xFF), or X8R8G8B8. It doesn''t check to make sure you''re not trying to copy something larger than the texture can hold. And so on. But you really should be able to figure it out from here.

Also note that a texture cannot have been created using D3DPOOL_DEFAULT for LockRect() to work. In that case you always have other options. For example, you could just load the image data onto a texture that''s in system memory, then use IDirect3DDevice9::UpdateTexture() or something like that.

"Your superior intellect is no match for our puny weapons!"

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
thanks for yours answer Ironblade. I will try that and let you know..

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
HI there again..this is the code i did base on the last post from IronBlayde
bool graphics::CopyImageToTexture(
UCHAR *pImageArray,
int xWidth,
int yHeight,
LPDIRECT3DTEXTURE9& pTex
){
D3DLOCKED_RECT d3drc;
UINT *pTexture;


if(D3DXCreateTexture(
g_pd3dDevice,
256,
256,
D3DX_DEFAULT,
0,
D3DFMT_A8R8G8B8,
D3DPOOL_MANAGED,
&pTex
)!= D3D_OK){
ASSERT(0);
}

// 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

UCHAR r, g, b;
for (int nRow = 0; nRow < yHeight; nRow++) {
// set destination pointer for this row
pTexture = (UINT*)d3drc.pBits + nRow*d3drc.Pitch;
// copy the row
for (int nPixel = 0; nPixel < xWidth; nPixel++) {
for(int bytes=0; bytes<number_of_bytes_per_pixel; bytes++){
// extract pixel data
r = *pImageArray++;
g = *pImageArray++;
b = *pImageArray++;
// write color word to texture
(*pTexture++) = 0xFF000000 | (r << 16) | (g << 8) | b;
}
}
}
// unlock texture
pTex->UnlockRect(0);

// return success
return true;
}
It is pretty much the same code with just few modifications. But i realize that is more complicate than just trying to populate the texture. The call to D3DXCreateTexture() does not guarantee that your are going to get the format you requested. My computer can''t handle 32 bit colors so the A8R8G8B8 is not suported. Also the bitmap i''m creating does not have sizes that are goods for texture like 256*256 or 128*64...etc. So I will have to work out the extreching of my bitmaps to the right size for the texture and also work out the color transformations for the color_format available. This two things are taking care for me in the code i posted before, but again the performance is bad..Does anyone could tell me where i can find more about this 2 problems that i have...converting any size to size of power of 2(textures sizes) and also converting the color to the color_format available. Or anyone with a better idea....

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

Sorry the is an error in the code that i posted before...This is the right part of that code..


// copy the row
for (int nPixel = 0; nPixel < xWidth; nPixel++) {
// extract pixel data
r = *pImageArray++;
g = *pImageArray++;
b = *pImageArray++;
// write color word to texture
(*pTexture++) = 0xFF000000 | (r << 16) | (g << 8) | b;
}

Share this post


Link to post
Share on other sites