GLFileMD2.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. unit GLFileMD2;
  5. (* Quake2 MD2 vector file format implementation. *)
  6. interface
  7. {$I GLScene.inc}
  8. uses
  9. System.Classes,
  10. System.SysUtils,
  11. GLVectorFileObjects,
  12. GLApplicationFileIO,
  13. FileMD2;
  14. type
  15. (* The MD2 vector file (Quake2 actor file).
  16. Stores a set of "frames" describing the different postures of the actor,
  17. it may be animated by TGLActor. The "Skin" must be loaded indepentendly
  18. (the whole mesh uses a single texture bitmap).
  19. Based on code by Roger Cao. *)
  20. TGLMD2VectorFile = class(TGLVectorFile)
  21. public
  22. class function Capabilities : TGLDataFileCapabilities; override;
  23. procedure LoadFromStream(aStream : TStream); override;
  24. end;
  25. // ------------------------------------------------------------------
  26. implementation
  27. // ------------------------------------------------------------------
  28. // ------------------
  29. // ------------------ TGLMD2VectorFile ------------------
  30. // ------------------
  31. class function TGLMD2VectorFile.Capabilities : TGLDataFileCapabilities;
  32. begin
  33. Result:=[dfcRead];
  34. end;
  35. procedure TGLMD2VectorFile.LoadFromStream(aStream : TStream);
  36. var
  37. i, j : Integer;
  38. MD2File : TFileMD2;
  39. mesh : TGLMorphableMeshObject;
  40. faceGroup : TFGIndexTexCoordList;
  41. morphTarget : TGLMeshMorphTarget;
  42. begin
  43. MD2File:=TFileMD2.Create;
  44. MD2File.LoadFromStream(aStream);
  45. try
  46. // retrieve mesh data
  47. mesh:=TGLMorphableMeshObject.CreateOwned(Owner.MeshObjects);
  48. with mesh, MD2File do begin
  49. Mode:=momFaceGroups;
  50. faceGroup:=TFGIndexTexCoordList.CreateOwned(FaceGroups);
  51. with faceGroup do begin
  52. MaterialName:='';
  53. VertexIndices.Capacity:=iTriangles*3;
  54. TexCoords.Capacity:=iTriangles*3;
  55. // copy the face list
  56. for i:=0 to iTriangles-1 do with IndexList[i] do begin
  57. Add(a, a_s, -a_t);
  58. Add(b, b_s, -b_t);
  59. Add(c, c_s, -c_t);
  60. end;
  61. end;
  62. // retrieve frames data (morph targets)
  63. for i:=0 to iFrames-1 do begin
  64. morphTarget:=TGLMeshMorphTarget.CreateOwned(MorphTargets);
  65. with morphTarget do begin
  66. Name:='Frame'+IntToStr(i);
  67. Vertices.Capacity:=iVertices;
  68. for j:=0 to iVertices-1 do
  69. Vertices.Add(VertexList[i][j]);
  70. BuildNormals(faceGroup.VertexIndices, momTriangles);
  71. end;
  72. end;
  73. end;
  74. if GetOwner is TGLActor then with TGLActor(GetOwner).Animations do begin
  75. Clear;
  76. with MD2File do for i:=0 to frameNames.Count-1 do with Add do begin
  77. Name:=frameNames[i];
  78. Reference:=aarMorph;
  79. StartFrame:=Integer(frameNames.Objects[i]);
  80. if i<frameNames.Count-1 then
  81. EndFrame:=Integer(frameNames.Objects[i+1])-1
  82. else EndFrame:=iFrames-1;
  83. end;
  84. end;
  85. if mesh.MorphTargets.Count>0 then
  86. mesh.MorphTo(0);
  87. finally
  88. MD2File.Free;
  89. end;
  90. end;
  91. // ------------------------------------------------------------------
  92. initialization
  93. // ------------------------------------------------------------------
  94. RegisterVectorFileFormat('md2', 'Quake II model files', TGLMD2VectorFile);
  95. end.