LZScene

Форк
0
/
GLTypes.pas 
309 строк · 7.5 Кб
1
//
2
// This unit is part of the GLScene Engine https://github.com/glscene
3
//
4
{
5
   Defines vector types for geometry only aiming to imply
6
   compatibility of GLScene for Delphi with C+Builder.
7
   Do not include any other units in uses clause 
8

9
	 History :  
10
     01/11/13 - PW - Fixed XE5 error: E2376 static can only be used on non-virtual class methods
11
     12/12/12 - PW - Added TGLVector's and TGLMatrix's types
12
     11/11/11 - PW - Creation. Defined TGLPoint, TGLPolygon and TGLPolyhedron types
13

14
}
15
unit GLTypes;
16

17
interface
18

19
type
20
  TGLBox = record
21
    ALeft, ATop, ANear, ARight, ABottom, AFar: Single;
22
  end;
23

24
//-----------------------
25
//Point types
26
//-----------------------
27
type
28

29
  PGLPoint2D = ^TGLPoint2D;
30
  TGLPoint2D = record
31
    X: Single;
32
    Y: Single;
33
    public
34
      function Create(X, Y : Single): TGLPoint2D;{$IF (FPC_VERSION < 3)} static; {$ENDIF}
35
      procedure SetPosition(const X, Y : Single);
36
      function Add(const APoint2D: TGLPoint2D): TGLPoint2D;
37
      function Length: Single; //distance to origin
38
      function Distance(const APoint2D : TGLPoint2D) : Single;
39
      procedure Offset(const ADeltaX, ADeltaY : Single);
40
  end;
41

42
  PGLPoint3D = ^TGLPoint3D;
43
  TGLPoint3D = record
44
    X: Single;
45
    Y: Single;
46
    Z: Single;
47
    public
48
      function Create(X, Y, Z: Single): TGLPoint3D; {$IF (FPC_VERSION < 3)} static; {$ENDIF}
49
      procedure SetPosition(const X, Y, Z : Single);
50
      function Add(const AGLPoint3D: TGLPoint3D): TGLPoint3D;
51
      function Length: Single; //distance to origin
52
      function Distance(const APoint3D : TGLPoint3D) : Single;
53
      procedure Offset(const ADeltaX, ADeltaY, ADeltaZ : Single);
54
  end;
55

56
  TGLPoint2DArray = array of TGLPoint2D;
57
  TGLPoint3DArray = array of TGLPoint3D;
58

59

60
//-----------------------
61
//Polygon types
62
//-----------------------
63
  TGLPolygon2D = TGLPoint2DArray;
64
  TGLPolygon3D = TGLPoint3DArray;
65

66
const
67
   ClosedPolygon2D: TGLPoint2D = (X: $FFFF; Y: $FFFF);
68
   ClosedPolygon3D: TGLPoint3D = (X: $FFFF; Y: $FFFF; Z: $FFFF);
69

70

71
//-----------------------
72
//Polyhedron types
73
//-----------------------
74
type
75
  TGLPolyhedron = array of TGLPolygon3D;
76

77
//-----------------------
78
// Vector types
79
//-----------------------
80
  TGLVector2DType = array [0..1] of Single;
81
  TGLVector3DType = array [0..2] of Single;
82

83
  TGLVector2D = record
84
    private
85
      function Add(const AVector2D: TGLVector2D): TGLVector2D;
86
      function Norm: Single;
87
    public
88
      function Create(const AX, AY, AW : Single): TGLVector2D; {$IF (FPC_VERSION < 3)} static; {$ENDIF}
89
      function Length: Single;
90
    case Integer of
91
      0: (V: TGLVector2DType;);
92
      1: (X: Single;
93
          Y: Single;
94
          W: Single;)
95
  end;
96

97
  TGLVector3D = record
98
    private
99
      function Add(const AVector3D: TGLVector3D): TGLVector3D;
100
      function Norm: Single;
101
    public
102
      function Create(const AX, AY, AZ, AW : Single): TGLVector3D; {$IF (FPC_VERSION < 3)} static; {$ENDIF}
103
      function Length: Single;
104
    case Integer of
105
      0: (V: TGLVector3DType;);
106
      1: (X: Single;
107
          Y: Single;
108
          Z: Single;
109
          W: Single;)
110
  end;
111

112
//-----------------------
113
// Matrix types
114
//-----------------------
115
  TGLMatrix2DType = array[0..3] of TGLVector2D;
116
  {$NODEFINE TGLMatrix2DType}
117
  (*$HPPEMIT END OPENNAMESPACE*)
118
  (*$HPPEMIT END 'typedef TGLVector2D TGLMatrix2DArray[4];'*)
119
  (*$HPPEMIT END CLOSENAMESPACE*)
120
  TGLMatrix3DType = array[0..3] of TGLVector3D;
121
  {$NODEFINE TGLMatrix3DType}
122
  (*$HPPEMIT END OPENNAMESPACE*)
123
  (*$HPPEMIT END 'typedef TGLVector3D TGLMatrix3DType[4];'*)
124
  (*$HPPEMIT END CLOSENAMESPACE*)
125

126
  TGLMatrix2D = record
127
  private
128
  public
129
    case Integer of
130
      0: (M: TGLMatrix2DType;);
131
      1: (e11, e12, e13: Single;
132
          e21, e22, e23: Single;
133
          e31, e32, e33: Single);
134
  end;
135

136
  TGLMatrix3D = record
137
  private
138
  public
139
    case Integer of
140
      0: (M: TGLMatrix3DType;);
141
      1: (e11, e12, e13, e14: Single;
142
          e21, e22, e23, e24: Single;
143
          e31, e32, e33, e34: Single;
144
          e41, e42, e43, e44: Single);
145
  end;
146

147
  TGLMatrix2DArray = array of TGLMatrix2D;
148
  TGLMatrix3DArray = array of TGLMatrix3D;
149

150
type
151
   TGLMesh2DVertex = packed record
152
    X, Y: Single;
153
    NX, NY: Single;
154
    tU, tV: Single;
155
  end;
156

157
   TGLMesh3DVertex = packed record
158
    X, Y, Z: Single;
159
    NX, NY, NZ: Single;
160
    tU, tV: Single;
161
  end;
162

163
  TGLMesh2D = array of TGLMesh2DVertex;
164
  TGLMesh3D = array of TGLMesh3DVertex;
165

166

167
  TGLQuaternion3D = record
168
    ImPart: TGLVector3D;
169
    RePart: Single;
170
  end;
171

172
//---------------------------------------------------------------
173
//---------------------------------------------------------------
174
//---------------------------------------------------------------
175

176

177
implementation
178

179
{ TGLPoint2D }
180

181
function TGLPoint2D.Create(X, Y : Single): TGLPoint2D;
182
begin
183
  Result.X := X;
184
  Result.Y := Y;
185
end;
186

187
procedure TGLPoint2D.SetPosition(const X, Y: Single);
188
begin
189
  Self.X := X;
190
  Self.Y := Y;
191
end;
192

193
function TGLPoint2D.Length: Single;
194
begin
195
  Result := Sqrt(Self.X * Self.X + Self.Y * Self.Y);
196
end;
197

198
function TGLPoint2D.Add(const APoint2D: TGLPoint2D): TGLPoint2D;
199
begin
200
  Result.SetPosition(Self.X + APoint2D.X, Self.Y + APoint2D.Y);
201
end;
202

203
function TGLPoint2D.Distance(const APoint2D: TGLPoint2D): Single;
204
begin
205
  Result := Sqrt(Sqr(Self.X - APoint2D.X) +  Sqr(Self.Y - APoint2D.Y));
206
end;
207

208
procedure TGLPoint2D.Offset(const ADeltaX, ADeltaY: Single);
209
begin
210
  Self.X := Self.X + ADeltaX;
211
  Self.Y := Self.Y + ADeltaY;
212
end;
213

214
{ TGLPoint3D }
215

216
function TGLPoint3D.Create(X, Y, Z: Single): TGLPoint3D;
217
begin
218
  Result.X := X;
219
  Result.Y := Y;
220
  Result.Z := Z;
221
end;
222

223
function TGLPoint3D.Add(const AGLPoint3D: TGLPoint3D): TGLPoint3D;
224
begin
225
  Result.X := Self.X + AGLPoint3D.X;
226
  Result.Y := Self.Y + AGLPoint3D.Y;
227
  Result.Z := Self.Z + AGLPoint3D.Z;
228
end;
229

230
function TGLPoint3D.Distance(const APoint3D: TGLPoint3D): Single;
231
begin
232
  Result := Self.Length - APoint3D.Length;
233
end;
234

235
function TGLPoint3D.Length: Single;
236
begin
237
  Result := Sqrt(Self.X * Self.X + Self.Y * Self.Y + Self.Z * Self.Z);
238
end;
239

240
procedure TGLPoint3D.Offset(const ADeltaX, ADeltaY, ADeltaZ: Single);
241
begin
242
  Self.X := Self.X + ADeltaX;
243
  Self.Y := Self.Y + ADeltaY;
244
  Self.Z := Self.Z + ADeltaZ;
245
end;
246

247
procedure TGLPoint3D.SetPosition(const X, Y, Z: Single);
248
begin
249
  Self.X := X;
250
  Self.Y := Y;
251
  Self.Z := Z;
252
end;
253

254
{ TGLVector2D }
255

256
function TGLVector2D.Create(const AX, AY, AW: Single): TGLVector2D;
257
begin
258
  Result.X := AX;
259
  Result.Y := AY;
260
  Result.W := AW;
261
end;
262

263
function TGLVector2D.Add(const AVector2D: TGLVector2D): TGLVector2D;
264
begin
265
  Result.X := Self.X + AVector2D.X;
266
  Result.Y := Self.Y + AVector2D.Y;
267
  Result.W := 1.0;
268
end;
269

270
function TGLVector2D.Length: Single;
271
begin
272
  Result := Sqrt(Self.Norm);
273
end;
274

275
function TGLVector2D.Norm: Single;
276
begin
277
  Result := (Self.X * Self.X) + (Self.Y * Self.Y);
278
end;
279

280
{ TGLVector3D }
281

282
function TGLVector3D.Create(const AX, AY, AZ, AW: Single): TGLVector3D;
283
begin
284
  Result.X := AX;
285
  Result.Y := AY;
286
  Result.Z := AZ;
287
  Result.W := AW;
288
end;
289

290
function TGLVector3D.Add(const AVector3D: TGLVector3D): TGLVector3D;
291
begin
292
  Result.X := Self.X + AVector3D.X;
293
  Result.Y := Self.Y + AVector3D.Y;
294
  Result.Z := Self.Z + AVector3D.Z;
295
  Result.W := 1.0;
296
end;
297

298
function TGLVector3D.Norm: Single;
299
begin
300
  Result := (Self.X * Self.X) + (Self.Y * Self.Y) + (Self.Z * Self.Z);
301
end;
302

303
function TGLVector3D.Length: Single;
304
begin
305
  Result := Sqrt(Self.Norm);
306
end;
307

308

309
end.
310

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

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

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

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