Results 1 to 7 of 7

Thread: D3DTexture write to and from a stream...

  1. #1
    Legendary Member NecroDOME's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands, Eindhoven
    Posts
    1,059

    D3DTexture write to and from a stream...

    Hello,

    is it posible to read and write a IDirect3DTexture9 directly to or from a TFileStream of TMemoryStream?
    NecroSOFT - End of line -

  2. #2

    D3DTexture write to and from a stream...

    Yes, use LockRect and UnlockRect methods to get direct access to texture's pixels.

  3. #3
    Legendary Member NecroDOME's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands, Eindhoven
    Posts
    1,059

    D3DTexture write to and from a stream...

    Yes I know, but is there a way to do it with LoadFromMemory ?
    NecroSOFT - End of line -

  4. #4

    D3DTexture write to and from a stream...

    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;

  5. #5

    D3DTexture write to and from a stream...

    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.

  6. #6

    D3DTexture write to and from a stream...

    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.

  7. #7
    Legendary Member NecroDOME's Avatar
    Join Date
    Mar 2004
    Location
    The Netherlands, Eindhoven
    Posts
    1,059

    D3DTexture write to and from a stream...

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •