LZScene

Форк
0
/
FileOCT.pas 
218 строк · 6.2 Кб
1
//
2
// This unit is part of the GLScene Engine https://github.com/glscene
3
//
4
{
5
   Loader for FSRad OCT files.
6

7
	 History :  
8
       16/10/08 - UweR - Compatibility fix for Delphi 2009
9
       02/04/07 - DaStr - Added $I GLScene.inc
10
       30/01/03 - Egg - Creation
11
	 
12
}
13
unit FileOCT;
14

15
interface
16

17
{$I GLScene.inc}
18

19
uses Classes, GLVectorGeometry, GLVectorLists;
20

21
type
22

23
   TOCTHeader = record
24
		numVerts : Integer;
25
		numFaces : Integer;
26
		numTextures : Integer;
27
		numLightmaps : Integer;
28
		numLights : Integer;
29
	end;
30

31
	TOCTVertex = record
32
		tv : TTexPoint;      // texture coordinates
33
		lv : TTexpoint;      // lightmap coordinates
34
		pos : TAffineVector; // vertex position
35
   end;
36

37
	TOCTFace = record
38
		start : Integer;     // first face vert in vertex array
39
		num : Integer;			// number of verts in the face
40
		id : Integer;			// texture index into the texture array
41
		lid : Integer;			// lightmap index into the lightmap array
42
		p : THmgPlane;
43
	end;
44
   POCTFace = ^TOCTFace;
45

46
	TOCTTexture = record
47
		id : Integer;				      // texture id
48
		Name : array [0..63] of AnsiChar;	// texture name
49
   end;
50

51
	TOCTLightmap = record
52
		id : Integer;				         // lightmaps id
53
		map : array [0..49151] of Byte;	// 128 x 128 raw RGB data
54
   end;
55
   POCTLightmap = ^TOCTLightmap;
56

57
	TOCTLight = record
58
		pos : TAffineVector;		      // Position
59
		color : TAffineVector;			// Color (RGB)
60
		intensity : Integer;			   // Intensity
61
	end;
62

63
   // TOCTFile
64
   //
65
   TOCTFile = class (TObject)
66
      public
67
          
68
         Header         : TOCTHeader;
69
         Vertices       : array of TOCTVertex;
70
         Faces          : array of TOCTFace;
71
         Textures       : array of TOCTTexture;
72
         Lightmaps      : array of TOCTLightmap;
73
         Lights         : array of TOCTLight;
74
         PlayerPos      : TAffineVector;
75

76
         constructor Create; overload;
77
         constructor Create(octStream : TStream); overload;
78

79
         { Saves content to stream in OCT format.
80
            The Header is automatically prepared before streaming. }
81
         procedure SaveToStream(aStream : TStream);
82

83
         procedure AddTriangles(vertexCoords : TAffineVectorList;
84
                                texMapCoords : TAffineVectorList;
85
                                const textureName : String);
86
         procedure AddLight(const lightPos : TAffineVector;
87
                            const lightColor : TVector;
88
                            lightIntensity : Integer);
89

90
   end;
91

92
// ------------------------------------------------------------------
93
// ------------------------------------------------------------------
94
// ------------------------------------------------------------------
95
implementation
96
// ------------------------------------------------------------------
97
// ------------------------------------------------------------------
98
// ------------------------------------------------------------------
99

100
uses SysUtils, GLMeshUtils;
101

102
// ------------------
103
// ------------------ TOCTFile ------------------
104
// ------------------
105

106
// Create
107
//
108
constructor TOCTFile.Create;
109
begin
110
   inherited Create;
111
end;
112

113
// Create
114
//
115
constructor TOCTFile.Create(octStream : TStream);
116
begin
117
   inherited Create;
118
   
119
   // Read in the header
120
   octStream.Read(Header, SizeOf(Header));
121

122
   // then the rest of the stuff
123

124
   SetLength(Vertices, Header.numVerts);
125
   octStream.Read(Vertices[0], Header.numVerts*SizeOf(TOCTVertex));
126

127
   SetLength(Faces, Header.numFaces);
128
   octStream.Read(Faces[0], Header.numFaces*SizeOf(TOCTFace));
129

130
   SetLength(Textures, Header.numTextures);
131
   octStream.Read(Textures[0], Header.numTextures*SizeOf(TOCTTexture));
132

133
   SetLength(Lightmaps, Header.numLightmaps);
134
   octStream.Read(Lightmaps[0], Header.numLightmaps*SizeOf(TOCTLightmap));
135

136
   SetLength(Lights, Header.numLights);
137
   octStream.Read(Lights[0], Header.numLights*SizeOf(TOCTLight));
138

139
   octStream.Read(PlayerPos, SizeOf(PlayerPos))
140
end;
141

142
// SaveToStream
143
//
144
procedure TOCTFile.SaveToStream(aStream : TStream);
145
begin
146
   with Header, aStream do begin
147
      numVerts:=Length(Vertices);
148
      numFaces:=Length(Faces);
149
      numTextures:=Length(Textures);
150
      numLightmaps:=Length(Lightmaps);
151
      numLights:=Length(Lights);
152

153
      Write(Header, SizeOf(Header));
154
      Write(Vertices[0], numVerts*SizeOf(TOCTVertex));
155
      Write(Faces[0], numFaces*SizeOf(TOCTFace));
156
      Write(Textures[0], numTextures*SizeOf(TOCTTexture));
157
      Write(Lightmaps[0], numLightmaps*SizeOf(TOCTLightmap));
158
      Write(Lights[0], numLights*SizeOf(TOCTLight));
159
      Write(PlayerPos, SizeOf(PlayerPos))
160
   end;
161
end;
162

163
// AddTriangles
164
//
165
procedure TOCTFile.AddTriangles(vertexCoords : TAffineVectorList;
166
                                texMapCoords : TAffineVectorList;
167
                                const textureName : String);
168
var
169
   i : Integer;
170
   baseIdx, texIdx : Integer;
171
begin
172
   Assert((texMapCoords=nil) or (texMapCoords.Count=vertexCoords.Count));
173

174
   texIdx:=Length(Textures);
175
   SetLength(Textures, texIdx+1);
176
   Move(textureName[1], Textures[texIdx].Name[0], Length(textureName));
177
   SetLength(Lightmaps, 1);
178
   FillChar(Lightmaps[0].map[0], 128*3, 255);
179

180
   baseIdx:=Length(Vertices);
181
   SetLength(Vertices, baseIdx+vertexCoords.Count);
182
   for i:=0 to vertexCoords.Count-1 do with Vertices[baseIdx+i] do begin
183
      pos:=vertexCoords.List[i];
184
      if Assigned(texMapCoords) then
185
         tv:=PTexPoint(@texMapCoords.List[i])^;
186
   end;
187

188
   SetLength(Faces, vertexCoords.Count div 3);
189
   i:=0; while i<vertexCoords.Count do begin
190
      with Faces[i div 3] do begin
191
         start:=baseIdx+i;
192
         num:=3;
193
         id:=texIdx;
194
         p:=PlaneMake(vertexCoords[i],
195
                      CalcPlaneNormal(vertexCoords[i+0], vertexCoords[i+1], vertexCoords[i+0]));
196
      end;
197
      Inc(i, 3);
198
   end;
199
end;
200

201
// AddLight
202
//
203
procedure TOCTFile.AddLight(const lightPos : TAffineVector;
204
                            const lightColor : TVector;
205
                            lightIntensity : Integer);
206
var
207
   n : Integer;
208
begin
209
   n:=Length(Lights);
210
   SetLength(Lights, n+1);
211
   with Lights[n] do begin
212
      pos:=lightPos;
213
      color:=PAffineVector(@lightColor)^;
214
      intensity:=lightIntensity;
215
   end;
216
end;
217

218
end.
219

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

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

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

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