Hello,
is it posible to read and write a IDirect3DTexture9 directly to or from a TFileStream of TMemoryStream?
Hello,
is it posible to read and write a IDirect3DTexture9 directly to or from a TFileStream of TMemoryStream?
NecroSOFT - End of line -
Yes, use LockRect and UnlockRect methods to get direct access to texture's pixels.
Yes I know, but is there a way to do it with LoadFromMemory ?
NecroSOFT - End of line -
Do you mean something like this:
Code:var i: Integer; LRect: TD3DLocked_Rect; Stream: TStream; MemAddr: Pointer; begin Texture.LockRect(0, LRect, nil, 0); for i:= 0 to TextureHeight - 1 do begin // point to location of specific scanline MemAddr:= Pointer(Integer(LRect.Bits) + (LRect.Pitch * i)); // read pixel data from stream Stream.ReadBuffer(MemAddr^, TextureWidth * BytesPerPixel); end; Texture.UnlockRect(0); end;
Lifepower advice is good if you are confident in texture format.
Another (easier) method could be (if you read real texture with header etc.:
[pascal]procedure Load(..., out Texture: IDirect3DTexture9);
var
Stream: TStream;
MemAddr: Pointer;
begin
MemAddr := nil;
Stream := TFileStream.Create(...); // or TAnyStream.Create...
try
GetMem(MemAddr, Stream.Size);
Stream.ReadBuffer(MemAddr^, Stream.Size); // read pixel data from stream
if FAILED(D3DXCreateTextureFromFileInMemory(d3dDevice , MemAddr, Stream.Size, Texture)) then
begin
// error while creating texture - handle me!
end;
finally
FreeMem(MemAddr);
Stream.Free;
end;
end;
[/pascal]
// Edit: fixed code (added missing GetMem() call) //
There are only 10 types of people in this world; those who understand binary and those who don't.
Notice that in Clootie's code the memory for MemAddr is not allocated. You should add "GetMem(MemAddr, Stream.Size)" before reading. Also, there is no possibility to save the texture to stream this way.
As for texture format, you can use GetLevelDesc to retreive information about the texture and its format. Of course, to support multiple texture formats you'll have to write the necessary conversion functions.
thanks for your repleys
Both are interesting, but it's main use is to write lightmaps someware to a file and read them when a level loads.
So I think I go with option A (from Lifepower)
NecroSOFT - End of line -
Bookmarks