View Single Post
  #5  
Old 10-03-2004, 06:22 AM
Windcatcher
Demi-God
 
Join Date: Jan 2002
Posts: 1,175
Default

I don't know...we should probably talk about it. That's the way it's done in WLD, but I'm not sure it's the best way. Is it better to tie alpha to polygons or textures? When rendering, alpha is done at the poly level.

Edit: Here's what I've got so far:

Code:
Unit XWFFiles;

Interface

Uses Classes;

Const
  fourccTexture        = 'tex ';
  fourccVertex         = 'vert';
  fourccPolygon        = 'poly';
  fourccObject         = 'obj ';
  fourccOctreeParent   = 'octp';
  fourccOctreeEndpoint = 'octe';

Type
  TFourCC = Packed Array[0..3] Of Char;

  TXWFAtomRec = Packed Record
    FourCC   : TFourCC;
    Children : LongWord;
    Size     : LongWord;
  End;

  TXWFVertexGroupRec = Packed Record
    GroupID     : LongWord;
    NumVertices : LongWord;
  End;

  TXWFVertexRec = Packed Record
    X,Y,Z : Double;     // Position
    U,V   : Double;     // Texture coordinates
    I,J,K : Double;     // Normal
  End;

  TXWFPolygonGroupRec = Packed Record
    VertexGroupID : LongWord;
    NumPolygons   : LongWord;
  End;

  TXWFPolygonRec = Packed Record
    V1,V2,V3  : LongWord; // Vertex indices in the indicated vertex group
    TextureID : LongWord; // Texture ID
    Flags     : LongWord; // Bit 1 ... 0 = solid, 1 = can be walked through
                          // Bit 2 ... 1 = transparent (TEMPORARY UNTIL SHADERS ARE DEFINED)
  End;

  TXWFOctreeNodeRec = Packed Record
    Center : Packed Array[0..2] Of Double;
    Size   : Packed Array[0..2] Of Double;
  End;

  // Classes for dealing with atoms

  TXWFAtom = Class
    Atom     : TXWFAtomRec;
    Data     : Pointer;
    Children : TStringList; // Child atoms
    Constructor Create(Const AtomRec: TXWFAtomRec);
    Destructor  Destroy; Override;
    Procedure   ReadFromStream(Stream: TStream);
    Procedure   WriteToStream(Stream: TStream);
  End;

Implementation

Uses Math,SysUtils;

// ------------------------------
// Utility routines
// ------------------------------

Function StrLPas(P: PChar; MaxLen: Integer): String;
Var
  I  : Integer;
  St : String;

Begin
  I  := Min(MaxLen,StrLen(P));
  St := '';
  While Length(St) < I Do St := St + ' ';
  Move(P^,St[1],I);
  Result := St;
End; // StrLPas

// ------------------------------
// TXWFAtom
// ------------------------------

Constructor TXWFAtom.Create(Const AtomRec: TXWFAtomRec);
Begin
  Atom     := AtomRec;
  Children := TStringList.Create;
  If Atom.Size > 0 Then GetMem(Data,Atom.Size) Else Data := Nil;
End; // TXWFAtom.Create

Destructor TXWFAtom.Destroy;
Var I: Integer;
Begin
  For I := 0 To Children.Count - 1 Do Children.Objects[I].Free;
  Children.Free;
  If Data <> Nil Then FreeMem(Data,Atom.Size);
End; // TXWFAtom.Destroy

Procedure TXWFAtom.ReadFromStream(Stream: TStream);
Var
  ChildAtom : TXWFAtomRec;
  Child     : TXWFAtom;

Begin
  If Atom.Size > 0 Then Stream.Read(Data^,Atom.Size);
  Stream.Read(ChildAtom,SizeOf(ChildAtom));
  Child := TXWFAtom.Create(ChildAtom);
  Children.AddObject(StrLPas(Atom.FourCC,4),Child);
  Child.ReadFromStream(Stream);
End; // TXWFAtom.ReadFromStream

Procedure TXWFAtom.WriteToStream(Stream: TStream);
Var I: Integer;
Begin
  If Atom.Size > 0 Then Stream.Write(Data^,Atom.Size);
  For I := 0 To Children.Count - 1 Do TXWFAtom(Children.Objects[I]).WriteToStream(Stream);
End; // TXWFAtom.WriteToStream

End.
Reply With Quote