source: trunk/Packages/bgrabitmap/vertex3d.inc

Last change on this file was 2, checked in by chronos, 5 years ago
File size: 14.4 KB
Line 
1type
2 { TBGRAObject3D }
3
4 TBGRAObject3D = class(TInterfacedObject,IBGRAObject3D)
5 private
6 FColor: TBGRAPixel;
7 FLight: Single;
8 FTexture: IBGRAScanner;
9 FMainPart: IBGRAPart3D;
10 FFaces: array of IBGRAFace3D;
11 FFaceCount: integer;
12 FLightingNormal : TLightingNormal3D;
13 FParentLighting: boolean;
14 FMaterial: IBGRAMaterial3D;
15 FScene: TBGRAScene3D;
16 FFaceColorsInvalidated,
17 FMaterialInvalidated: boolean;
18 procedure AddFace(AFace: IBGRAFace3D); overload;
19 public
20 constructor Create(AScene: TBGRAScene3D);
21 destructor Destroy; override;
22 procedure Clear;
23 procedure InvalidateColor;
24 procedure InvalidateMaterial;
25 function AddFace(const AVertices: array of IBGRAVertex3D): IBGRAFace3D; overload;
26 function AddFace(const AVertices: array of IBGRAVertex3D; ABiface: boolean): IBGRAFace3D; overload;
27 function AddFace(const AVertices: array of IBGRAVertex3D; ATexture: IBGRAScanner): IBGRAFace3D; overload;
28 function AddFace(const AVertices: array of IBGRAVertex3D; AColor: TBGRAPixel): IBGRAFace3D; overload;
29 function AddFace(const AVertices: array of IBGRAVertex3D; AColors: array of TBGRAPixel): IBGRAFace3D; overload;
30 function AddFaceReversed(const AVertices: array of IBGRAVertex3D): IBGRAFace3D;
31 procedure ComputeWithMatrix(constref AMatrix: TMatrix3D; constref AProjection: TProjection3D);
32 function GetColor: TBGRAPixel;
33 function GetLight: Single;
34 function GetTexture: IBGRAScanner;
35 function GetMainPart: IBGRAPart3D;
36 function GetLightingNormal: TLightingNormal3D;
37 function GetParentLighting: boolean;
38 function GetFace(AIndex: integer): IBGRAFace3D;
39 function GetFaceCount: integer;
40 function GetTotalVertexCount: integer;
41 function GetTotalNormalCount: integer;
42 function GetMaterial: IBGRAMaterial3D;
43 procedure SetLightingNormal(const AValue: TLightingNormal3D);
44 procedure SetParentLighting(const AValue: boolean);
45 procedure SetColor(const AValue: TBGRAPixel);
46 procedure SetLight(const AValue: Single);
47 procedure SetTexture(const AValue: IBGRAScanner);
48 procedure SetMaterial(const AValue: IBGRAMaterial3D);
49 procedure RemoveUnusedVertices;
50 procedure SeparatePart(APart: IBGRAPart3D);
51 function GetScene: TObject;
52 function GetRefCount: integer;
53 procedure SetBiface(AValue : boolean);
54 procedure ForEachVertex(ACallback: TVertex3DCallback);
55 procedure ForEachFace(ACallback: TFace3DCallback);
56 procedure Update;
57 end;
58
59 { TBGRAVertex3D }
60
61 TBGRAVertex3D = class(TInterfacedObject,IBGRAVertex3D)
62 private
63 FColor: TBGRAPixel;
64 FParentColor: boolean;
65 FLight: Single;
66 FTexCoord: TPointF;
67 FCoordPool: TBGRACoordPool3D;
68 FCoordPoolIndex: integer;
69 FCustomFlags: DWord;
70 FObject3D: TBGRAObject3D;
71 function GetCoordData: PBGRACoordData3D;
72 procedure Init(AObject3D: TBGRAObject3D; ACoordPool: TBGRACoordPool3D; ASceneCoord: TPoint3D_128);
73 public
74 constructor Create(AObject3D: TBGRAObject3D; ACoordPool: TBGRACoordPool3D; ASceneCoord: TPoint3D); overload;
75 constructor Create(AObject3D: TBGRAObject3D; ACoordPool: TBGRACoordPool3D; ASceneCoord: TPoint3D_128); overload;
76 destructor Destroy; override;
77 function GetColor: TBGRAPixel;
78 function GetLight: Single;
79 function GetViewNormal: TPoint3D;
80 function GetViewNormal_128: TPoint3D_128;
81 function GetCustomNormal: TPoint3D;
82 function GetCustomNormal_128: TPoint3D_128;
83 function GetSceneCoord: TPoint3D;
84 function GetSceneCoord_128: TPoint3D_128;
85 function GetTexCoord: TPointF;
86 function GetViewCoord: TPoint3D;
87 function GetViewCoord_128: TPoint3D_128;
88 function GetUsage: integer;
89 function GetCustomFlags: DWord;
90 procedure SetColor(const AValue: TBGRAPixel);
91 procedure SetLight(const AValue: Single);
92 procedure SetViewNormal(const AValue: TPoint3D);
93 procedure SetViewNormal_128(const AValue: TPoint3D_128);
94 procedure SetCustomNormal(AValue: TPoint3D);
95 procedure SetCustomNormal_128(AValue: TPoint3D_128);
96 procedure NormalizeViewNormal;
97 procedure AddViewNormal(const AValue: TPoint3D_128);
98 procedure SetCustomFlags(AValue: DWord);
99 procedure SetSceneCoord(const AValue: TPoint3D);
100 procedure SetSceneCoord_128(const AValue: TPoint3D_128);
101 procedure SetTexCoord(const AValue: TPointF);
102 procedure SetViewCoord(const AValue: TPoint3D);
103 procedure SetViewCoord_128(const AValue: TPoint3D_128);
104 function GetViewCoordZ: single;
105 function GetParentColor: Boolean;
106 procedure SetParentColor(const AValue: Boolean);
107 function GetProjectedCoord: TPointF;
108 procedure SetProjectedCoord(const AValue: TPointF);
109 procedure ComputeCoordinateAndClearNormal(const AMatrix: TMatrix3D; const AProjection: TProjection3D);
110 property SceneCoord: TPoint3D read GetSceneCoord write SetSceneCoord;
111 property SceneCoord_128: TPoint3D_128 read GetSceneCoord_128 write SetSceneCoord_128;
112 property ViewCoord: TPoint3D read GetViewCoord write SetViewCoord;
113 property ViewCoord_128: TPoint3D_128 read GetViewCoord_128 write SetViewCoord_128;
114 property ViewCoordZ: single read GetViewCoordZ;
115 property ProjectedCoord: TPointF read GetProjectedCoord write SetProjectedCoord;
116 property TexCoord: TPointF read GetTexCoord write SetTexCoord;
117 property Color: TBGRAPixel read GetColor write SetColor;
118 property ParentColor: Boolean read GetParentColor write SetParentColor;
119 property Light: Single read GetLight write SetLight;
120 property ViewNormal: TPoint3D read GetViewNormal write SetViewNormal;
121 property ViewNormal_128: TPoint3D_128 read GetViewNormal_128 write SetViewNormal_128;
122 property CustomNormal: TPoint3D read GetCustomNormal write SetCustomNormal;
123 property CustomNormal_128: TPoint3D_128 read GetCustomNormal_128 write SetCustomNormal_128;
124 property Usage: integer read GetUsage;
125 property CoordData: PBGRACoordData3D read GetCoordData;
126 function GetAsObject: TObject;
127 end;
128
129 { TBGRANormal3D }
130
131 TBGRANormal3D = class(TInterfacedObject,IBGRANormal3D)
132 private
133 FPool: TBGRANormalPool3D;
134 FPoolIndex: integer;
135 function GetCustomNormal: TPoint3D;
136 function GetCustomNormal_128: TPoint3D_128;
137 function GetUsage: integer;
138 function GetViewNormal: TPoint3D;
139 function GetViewNormal_128: TPoint3D_128;
140 procedure SetCustomNormal(AValue: TPoint3D);
141 procedure SetCustomNormal_128(AValue: TPoint3D_128);
142 procedure SetViewNormal(AValue: TPoint3D);
143 procedure SetViewNormal_128(AValue: TPoint3D_128);
144 public
145 constructor Create(ANormalPool: TBGRANormalPool3D; ACustomNormal: TPoint3D); overload;
146 constructor Create(ANormalPool: TBGRANormalPool3D; ACustomNormal: TPoint3D_128); overload;
147 destructor Destroy; override;
148 property ViewNormal: TPoint3D read GetViewNormal write SetViewNormal;
149 property ViewNormal_128: TPoint3D_128 read GetViewNormal_128 write SetViewNormal_128;
150 property CustomNormal: TPoint3D read GetCustomNormal write SetCustomNormal;
151 property CustomNormal_128: TPoint3D_128 read GetCustomNormal_128 write SetCustomNormal_128;
152 property Usage: integer read GetUsage;
153 end;
154
155{ TBGRANormal3D }
156
157function TBGRANormal3D.GetCustomNormal: TPoint3D;
158begin
159 result := Point3D((FPool.NormalData[FPoolIndex])^.customNormal);
160end;
161
162function TBGRANormal3D.GetCustomNormal_128: TPoint3D_128;
163begin
164 result := (FPool.NormalData[FPoolIndex])^.customNormal;
165end;
166
167function TBGRANormal3D.GetUsage: integer;
168begin
169 result := frefcount;
170end;
171
172function TBGRANormal3D.GetViewNormal: TPoint3D;
173begin
174 result := Point3D((FPool.NormalData[FPoolIndex])^.viewNormal);
175end;
176
177function TBGRANormal3D.GetViewNormal_128: TPoint3D_128;
178begin
179 result := (FPool.NormalData[FPoolIndex])^.viewNormal;
180end;
181
182procedure TBGRANormal3D.SetCustomNormal(AValue: TPoint3D);
183begin
184 (FPool.NormalData[FPoolIndex])^.customNormal := Point3D_128(AValue);
185end;
186
187procedure TBGRANormal3D.SetCustomNormal_128(AValue: TPoint3D_128);
188begin
189 (FPool.NormalData[FPoolIndex])^.customNormal := AValue;
190end;
191
192procedure TBGRANormal3D.SetViewNormal(AValue: TPoint3D);
193begin
194 (FPool.NormalData[FPoolIndex])^.viewNormal := Point3D_128(AValue);
195end;
196
197procedure TBGRANormal3D.SetViewNormal_128(AValue: TPoint3D_128);
198begin
199 (FPool.NormalData[FPoolIndex])^.viewNormal := AValue;
200end;
201
202constructor TBGRANormal3D.Create(ANormalPool: TBGRANormalPool3D;
203 ACustomNormal: TPoint3D);
204begin
205 FPool := ANormalPool;
206 FPoolIndex:= FPool.Add;
207 CustomNormal := ACustomNormal;
208end;
209
210constructor TBGRANormal3D.Create(ANormalPool: TBGRANormalPool3D;
211 ACustomNormal: TPoint3D_128);
212begin
213 FPool := ANormalPool;
214 FPoolIndex:= FPool.Add;
215 CustomNormal_128 := ACustomNormal;
216end;
217
218destructor TBGRANormal3D.Destroy;
219begin
220 FPool.Remove(FPoolIndex);
221 inherited Destroy;
222end;
223
224{ TBGRAVertex3D }
225
226procedure TBGRAVertex3D.Init(AObject3D: TBGRAObject3D; ACoordPool: TBGRACoordPool3D; ASceneCoord: TPoint3D_128);
227begin
228 FObject3D := AObject3D;
229 FCoordPool := ACoordPool;
230 FCoordPoolIndex := FCoordPool.Add;
231 FColor := BGRAWhite;
232 FParentColor := True;
233 FLight := 1;
234 SceneCoord_128 := ASceneCoord;
235end;
236
237procedure TBGRAVertex3D.SetCustomNormal(AValue: TPoint3D);
238begin
239 with FCoordPool.CoordData[FCoordPoolIndex]^ do
240 begin
241 customNormal := Point3D_128(AValue);
242 customNormalUsed := not CompareMem(@customNormal,@Point3D_128_Zero,sizeof(Point3D_128_Zero));
243 end;
244end;
245
246procedure TBGRAVertex3D.SetCustomNormal_128(AValue: TPoint3D_128);
247begin
248 with FCoordPool.CoordData[FCoordPoolIndex]^ do
249 begin
250 customNormal := AValue;
251 customNormalUsed := not CompareMem(@customNormal,@Point3D_128_Zero,sizeof(Point3D_128_Zero));
252 end;
253end;
254
255function TBGRAVertex3D.GetCoordData: PBGRACoordData3D;
256begin
257 result := FCoordPool.CoordData[FCoordPoolIndex];
258end;
259
260function TBGRAVertex3D.GetCustomNormal: TPoint3D;
261begin
262 result := Point3D(FCoordPool.CoordData[FCoordPoolIndex]^.customNormal);
263end;
264
265function TBGRAVertex3D.GetCustomNormal_128: TPoint3D_128;
266begin
267 result := FCoordPool.CoordData[FCoordPoolIndex]^.customNormal;
268end;
269
270constructor TBGRAVertex3D.Create(AObject3D: TBGRAObject3D; ACoordPool: TBGRACoordPool3D; ASceneCoord: TPoint3D);
271begin
272 Init(AObject3D, ACoordPool, Point3D_128(ASceneCoord));
273end;
274
275constructor TBGRAVertex3D.Create(AObject3D: TBGRAObject3D; ACoordPool: TBGRACoordPool3D; ASceneCoord: TPoint3D_128);
276begin
277 Init(AObject3D, ACoordPool, ASceneCoord);
278end;
279
280destructor TBGRAVertex3D.Destroy;
281begin
282 FCoordPool.Remove(FCoordPoolIndex);
283 inherited Destroy;
284end;
285
286function TBGRAVertex3D.GetColor: TBGRAPixel;
287begin
288 result := FColor;
289end;
290
291function TBGRAVertex3D.GetLight: Single;
292begin
293 result := FLight;
294end;
295
296function TBGRAVertex3D.GetViewNormal: TPoint3D;
297begin
298 result := Point3D(FCoordPool.CoordData[FCoordPoolIndex]^.viewNormal);
299end;
300
301function TBGRAVertex3D.GetViewNormal_128: TPoint3D_128;
302begin
303 result := FCoordPool.CoordData[FCoordPoolIndex]^.viewNormal;
304end;
305
306function TBGRAVertex3D.GetSceneCoord: TPoint3D;
307begin
308 result := Point3D(FCoordPool.CoordData[FCoordPoolIndex]^.sceneCoord);
309end;
310
311function TBGRAVertex3D.GetSceneCoord_128: TPoint3D_128;
312begin
313 result := FCoordPool.CoordData[FCoordPoolIndex]^.sceneCoord;
314end;
315
316function TBGRAVertex3D.GetTexCoord: TPointF;
317begin
318 result := FTexCoord;
319end;
320
321function TBGRAVertex3D.GetViewCoord: TPoint3D;
322begin
323 result := Point3D(FCoordPool.CoordData[FCoordPoolIndex]^.viewCoord);
324end;
325
326function TBGRAVertex3D.GetViewCoord_128: TPoint3D_128;
327begin
328 result := FCoordPool.CoordData[FCoordPoolIndex]^.viewCoord;
329end;
330
331function TBGRAVertex3D.GetUsage: integer;
332begin
333 result := frefcount;
334end;
335
336function TBGRAVertex3D.GetCustomFlags: DWord;
337begin
338 result := FCustomFlags;
339end;
340
341procedure TBGRAVertex3D.SetColor(const AValue: TBGRAPixel);
342begin
343 FColor := AValue;
344 FParentColor := false;
345 FObject3D.InvalidateColor;
346end;
347
348procedure TBGRAVertex3D.SetLight(const AValue: Single);
349begin
350 FLight := AValue;
351end;
352
353procedure TBGRAVertex3D.SetViewNormal(const AValue: TPoint3D);
354begin
355 FCoordPool.CoordData[FCoordPoolIndex]^.viewNormal := Point3D_128(AValue);
356end;
357
358procedure TBGRAVertex3D.SetViewNormal_128(const AValue: TPoint3D_128);
359begin
360 FCoordPool.CoordData[FCoordPoolIndex]^.viewNormal := AValue;
361end;
362
363procedure TBGRAVertex3D.SetSceneCoord(const AValue: TPoint3D);
364begin
365 FCoordPool.CoordData[FCoordPoolIndex]^.sceneCoord := Point3D_128(AValue);
366end;
367
368procedure TBGRAVertex3D.SetSceneCoord_128(const AValue: TPoint3D_128);
369begin
370 FCoordPool.CoordData[FCoordPoolIndex]^.sceneCoord := AValue;
371end;
372
373procedure TBGRAVertex3D.SetTexCoord(const AValue: TPointF);
374begin
375 FTexCoord := AValue;
376end;
377
378procedure TBGRAVertex3D.SetViewCoord(const AValue: TPoint3D);
379begin
380 FCoordPool.CoordData[FCoordPoolIndex]^.viewCoord := Point3D_128(AValue);
381end;
382
383procedure TBGRAVertex3D.SetViewCoord_128(const AValue: TPoint3D_128);
384begin
385 FCoordPool.CoordData[FCoordPoolIndex]^.viewCoord := AValue;
386end;
387
388function TBGRAVertex3D.GetViewCoordZ: single;
389begin
390 result := FCoordPool.CoordData[FCoordPoolIndex]^.viewCoord.Z;
391end;
392
393function TBGRAVertex3D.GetParentColor: Boolean;
394begin
395 result := FParentColor;
396end;
397
398procedure TBGRAVertex3D.SetParentColor(const AValue: Boolean);
399begin
400 FParentColor := AValue;
401 FObject3D.InvalidateColor;
402end;
403
404function TBGRAVertex3D.GetProjectedCoord: TPointF;
405begin
406 result := FCoordPool.CoordData[FCoordPoolIndex]^.projectedCoord;
407end;
408
409procedure TBGRAVertex3D.SetProjectedCoord(const AValue: TPointF);
410begin
411 FCoordPool.CoordData[FCoordPoolIndex]^.projectedCoord := AValue;
412end;
413
414procedure TBGRAVertex3D.ComputeCoordinateAndClearNormal(const AMatrix: TMatrix3D; const AProjection : TProjection3D);
415var P: PBGRACoordData3D;
416begin
417 P := FCoordPool.CoordData[FCoordPoolIndex];
418 with p^ do
419 begin
420 viewCoord := AMatrix*sceneCoord;
421 if customNormalUsed then
422 viewNormal := MultiplyVect3DWithoutTranslation(AMatrix,customNormal)
423 else
424 ClearPoint3D_128(viewNormal);
425 if viewCoord.z > 0 then
426 begin
427 InvZ := 1/viewCoord.z;
428 projectedCoord := PointF(viewCoord.x*InvZ*AProjection.Zoom.x + AProjection.Center.x,
429 viewCoord.y*InvZ*AProjection.Zoom.Y + AProjection.Center.y);
430 end else
431 projectedCoord := PointF(0,0);
432 end;
433end;
434
435function TBGRAVertex3D.GetAsObject: TObject;
436begin
437 result := self;
438end;
439
440procedure TBGRAVertex3D.NormalizeViewNormal;
441begin
442 Normalize3D_128(FCoordPool.CoordData[FCoordPoolIndex]^.viewNormal);
443end;
444
445procedure TBGRAVertex3D.AddViewNormal(const AValue: TPoint3D_128);
446begin
447 with FCoordPool.CoordData[FCoordPoolIndex]^ do
448 if not customNormalUsed then
449 Add3D_Aligned(viewNormal, AValue);
450end;
451
452procedure TBGRAVertex3D.SetCustomFlags(AValue: DWord);
453begin
454 FCustomFlags:= AValue;
455end;
456
Note: See TracBrowser for help on using the repository browser.