GLFileMD2.pas 3.4 KB

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