GXS.FileMD2.pas 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. //
  3. // The graphics engine GLXEngine. The unit of GXScene for Delphi
  4. //
  5. //
  6. unit GXS.FileMD2;
  7. (* Quake2 MD2 vector file format implementation *)
  8. interface
  9. {$I Stage.Defines.inc}
  10. uses
  11. System.Classes,
  12. System.SysUtils,
  13. GXS.VectorFileObjects,
  14. GXS.ApplicationFileIO,
  15. Formatx.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 TgxActor. The "Skin" must be loaded indepentendly
  20. (the whole mesh uses a single texture bitmap).
  21. Based on code by Roger Cao. *)
  22. TgxMD2VectorFile = class(TgxVectorFile)
  23. public
  24. class function Capabilities: TDataFileCapabilities; override;
  25. procedure LoadFromStream(aStream: TStream); override;
  26. end;
  27. //===================================================================
  28. implementation
  29. //===================================================================
  30. // ------------------
  31. // ------------------ TgxMD2VectorFile ------------------
  32. // ------------------
  33. class function TgxMD2VectorFile.Capabilities: TDataFileCapabilities;
  34. begin
  35. Result := [dfcRead];
  36. end;
  37. procedure TgxMD2VectorFile.LoadFromStream(aStream: TStream);
  38. var
  39. i, j: Integer;
  40. MD2File: TFileMD2;
  41. mesh: TgxMorphableMeshObject;
  42. faceGroup: TgxFGIndexTexCoordList;
  43. morphTarget: TgxMeshMorphTarget;
  44. begin
  45. MD2File := TFileMD2.Create;
  46. MD2File.LoadFromStream(aStream);
  47. try
  48. // retrieve mesh data
  49. mesh := TgxMorphableMeshObject.CreateOwned(Owner.MeshObjects);
  50. with mesh, MD2File do
  51. begin
  52. Mode := momFaceGroups;
  53. faceGroup := TgxFGIndexTexCoordList.CreateOwned(FaceGroups);
  54. with faceGroup do
  55. begin
  56. MaterialName := '';
  57. VertexIndices.Capacity := iTriangles * 3;
  58. TexCoords.Capacity := iTriangles * 3;
  59. // copy the face list
  60. for i := 0 to iTriangles - 1 do
  61. with IndexList[i] do
  62. begin
  63. Add(a, a_s, -a_t);
  64. Add(b, b_s, -b_t);
  65. Add(c, c_s, -c_t);
  66. end;
  67. end;
  68. // retrieve frames data (morph targets)
  69. for i := 0 to iFrames - 1 do
  70. begin
  71. morphTarget := TgxMeshMorphTarget.CreateOwned(MorphTargets);
  72. with morphTarget do
  73. begin
  74. Name := 'Frame' + IntToStr(i);
  75. Vertices.Capacity := iVertices;
  76. for j := 0 to iVertices - 1 do
  77. Vertices.Add(VertexList[i][j]);
  78. BuildNormals(faceGroup.VertexIndices, momTriangles);
  79. end;
  80. end;
  81. end;
  82. if GetOwner is TgxActor then
  83. with TgxActor(GetOwner).Animations do
  84. begin
  85. Clear;
  86. with MD2File do
  87. for i := 0 to frameNames.Count - 1 do
  88. with Add do
  89. begin
  90. Name := frameNames[i];
  91. Reference := aarMorph;
  92. StartFrame := Integer(frameNames.Objects[i]);
  93. if i < frameNames.Count - 1 then
  94. EndFrame := Integer(frameNames.Objects[i + 1]) - 1
  95. else
  96. EndFrame := iFrames - 1;
  97. end;
  98. end;
  99. if mesh.MorphTargets.Count > 0 then
  100. mesh.MorphTo(0);
  101. finally
  102. MD2File.Free;
  103. end;
  104. end;
  105. // ------------------------------------------------------------------
  106. initialization
  107. // ------------------------------------------------------------------
  108. RegisterVectorFileFormat('md2', 'Quake II model files', TgxMD2VectorFile);
  109. end.