LZScene

Форк
0
/
GLFileMD2.pas 
130 строк · 4.2 Кб
1
//
2
// This unit is part of the GLScene Engine https://github.com/glscene
3
//
4
{
5
   Quake2 MD2 vector file format implementation.
6

7
	 History : 
8
       28/08/10 - Yar - Bugfix for FPC 2.5.1 (Thanks Predator)
9
       31/03/07 - DaStr - Added $I GLScene.inc
10
       05/06/03 - SG - Separated from GLVectorFileObjects.pas
11

12
}
13
unit GLFileMD2;
14

15
interface
16

17
{$I GLScene.inc}
18

19
uses
20
  Classes, SysUtils,
21
  GLVectorFileObjects, GLApplicationFileIO, 
22
  FileMD2;
23

24
type
25
   // TGLMD2VectorFile
26
   //
27
   { The MD2 vector file (Quake2 actor file).
28
      Stores a set of "frames" describing the different postures of the actor,
29
      it may be animated by TGLActor. The "Skin" must be loaded indepentendly
30
      (the whole mesh uses a single texture bitmap).
31
      Based on code by Roger Cao. }
32
   TGLMD2VectorFile = class(TGLVectorFile)
33
      public
34
          
35
         class function Capabilities : TGLDataFileCapabilities; override;
36
         procedure LoadFromStream(aStream : TStream); override;
37
   end;
38

39
// ------------------------------------------------------------------
40
// ------------------------------------------------------------------
41
// ------------------------------------------------------------------
42
implementation
43
// ------------------------------------------------------------------
44
// ------------------------------------------------------------------
45
// ------------------------------------------------------------------
46

47

48

49

50
// ------------------
51
// ------------------ TGLMD2VectorFile ------------------
52
// ------------------
53

54
// Capabilities
55
//
56
class function TGLMD2VectorFile.Capabilities : TGLDataFileCapabilities;
57
begin
58
   Result:=[dfcRead];
59
end;
60

61
// LoadFromStream
62
//
63
procedure TGLMD2VectorFile.LoadFromStream(aStream : TStream);
64
var
65
   i, j : Integer;
66
   MD2File : TFileMD2;
67
   mesh : TMorphableMeshObject;
68
   faceGroup : TFGIndexTexCoordList;
69
   morphTarget : TMeshMorphTarget;
70
begin
71
   MD2File:=TFileMD2.Create;
72
   MD2File.LoadFromStream(aStream);
73
   try
74
      // retrieve mesh data
75
      mesh:=TMorphableMeshObject.CreateOwned(Owner.MeshObjects);
76
      with mesh, MD2File do begin
77
         Mode:=momFaceGroups;
78
         faceGroup:=TFGIndexTexCoordList.CreateOwned(FaceGroups);
79
         with faceGroup do begin
80
            MaterialName:='';
81
            VertexIndices.Capacity:=iTriangles*3;
82
            TexCoords.Capacity:=iTriangles*3;
83
            // copy the face list
84
            for i:=0 to iTriangles-1 do with IndexList[i] do begin
85
               Add(a, a_s, -a_t);
86
               Add(b, b_s, -b_t);
87
               Add(c, c_s, -c_t);
88
            end;
89
         end;
90
         // retrieve frames data (morph targets)
91
         for i:=0 to iFrames-1 do begin
92
            morphTarget:=TMeshMorphTarget.CreateOwned(MorphTargets);
93
            with morphTarget do begin
94
               Name:='Frame'+IntToStr(i);
95
               Vertices.Capacity:=iVertices;
96
               for j:=0 to iVertices-1 do
97
                  Vertices.Add(VertexList[i][j]);
98
               BuildNormals(faceGroup.VertexIndices, momTriangles);
99
            end;
100
         end;
101
      end;
102
      if GetOwner is TGLActor then with TGLActor(GetOwner).Animations do begin
103
         Clear;
104
         with MD2File do for i:=0 to frameNames.Count-1 do with Add do begin
105
            Name:=frameNames[i];
106
            Reference:=aarMorph;
107
            StartFrame:=Integer(frameNames.Objects[i]);
108
            if i<frameNames.Count-1 then
109
               EndFrame:=Integer(frameNames.Objects[i+1])-1
110
            else EndFrame:=iFrames-1;
111
         end;
112
      end;
113
      if mesh.MorphTargets.Count>0 then
114
         mesh.MorphTo(0);
115
   finally
116
      MD2File.Free;
117
   end;
118
end;
119

120
// ------------------------------------------------------------------
121
// ------------------------------------------------------------------
122
// ------------------------------------------------------------------
123
initialization
124
// ------------------------------------------------------------------
125
// ------------------------------------------------------------------
126
// ------------------------------------------------------------------
127

128
   RegisterVectorFileFormat('md2', 'Quake II model files', TGLMD2VectorFile);
129

130
end.
131

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.