source: trunk/Packages/bgrabitmap/object3d.inc

Last change on this file was 2, checked in by chronos, 5 years ago
File size: 6.7 KB
Line 
1{ TBGRAObject3D }
2
3procedure TBGRAObject3D.AddFace(AFace: IBGRAFace3D);
4begin
5 if FFaceCount = length(FFaces) then
6 setlength(FFaces,FFaceCount*2+3);
7 FFaces[FFaceCount] := AFace;
8 inc(FFaceCount);
9end;
10
11constructor TBGRAObject3D.Create(AScene: TBGRAScene3D);
12begin
13 FColor := BGRAWhite;
14 FLight := 1;
15 FTexture := nil;
16 FMainPart := TBGRAPart3D.Create(self,nil);
17 FLightingNormal:= AScene.DefaultLightingNormal;
18 FParentLighting:= True;
19 FScene := AScene;
20 FFaceColorsInvalidated := true;
21 FMaterialInvalidated := false;
22end;
23
24destructor TBGRAObject3D.Destroy;
25begin
26 FMaterial := nil;
27 fillchar(FTexture,sizeof(FTexture),0);
28 inherited Destroy;
29end;
30
31procedure TBGRAObject3D.Clear;
32begin
33 FFaces := nil;
34 FFaceCount := 0;
35 FMainPart.Clear(True);
36end;
37
38procedure TBGRAObject3D.InvalidateColor;
39begin
40 FFaceColorsInvalidated := true;
41end;
42
43procedure TBGRAObject3D.InvalidateMaterial;
44begin
45 FMaterialInvalidated := true;
46end;
47
48function TBGRAObject3D.GetColor: TBGRAPixel;
49begin
50 result := FColor;
51end;
52
53function TBGRAObject3D.GetLight: Single;
54begin
55 result := FLight;
56end;
57
58function TBGRAObject3D.GetTexture: IBGRAScanner;
59begin
60 result := FTexture;
61end;
62
63function TBGRAObject3D.GetMainPart: IBGRAPart3D;
64begin
65 result := FMainPart;
66end;
67
68procedure TBGRAObject3D.SetColor(const AValue: TBGRAPixel);
69begin
70 FColor := AValue;
71 FTexture := nil;
72 InvalidateColor;
73end;
74
75procedure TBGRAObject3D.SetLight(const AValue: Single);
76begin
77 FLight := AValue;
78end;
79
80procedure TBGRAObject3D.SetTexture(const AValue: IBGRAScanner);
81begin
82 FTexture := AValue;
83 InvalidateMaterial;
84end;
85
86procedure TBGRAObject3D.SetMaterial(const AValue: IBGRAMaterial3D);
87begin
88 FMaterial := AValue;
89 InvalidateMaterial;
90end;
91
92procedure TBGRAObject3D.RemoveUnusedVertices;
93begin
94 GetMainPart.RemoveUnusedVertices;
95end;
96
97procedure TBGRAObject3D.SeparatePart(APart: IBGRAPart3D);
98var
99 vertexInfo: array of record
100 orig,dup: IBGRAVertex3D;
101 end;
102
103 i,j: integer;
104 inPart,outPart: boolean;
105 idxV: integer;
106begin
107 setlength(vertexInfo, APart.VertexCount);
108 for i := 0 to high(vertexInfo) do
109 with vertexInfo[i] do
110 begin
111 orig := APart.Vertex[i];
112 dup := APart.Add(orig.SceneCoord_128);
113 end;
114
115 for i := 0 to GetFaceCount-1 do
116 with GetFace(i) do
117 begin
118 inPart := false;
119 outPart := false;
120 for j := 0 to VertexCount-1 do
121 if (APart.IndexOf(Vertex[j]) <> -1) then
122 inPart := true
123 else
124 outPart := true;
125
126 if inPart and not outPart then
127 begin
128 for j := 0 to VertexCount-1 do
129 begin
130 idxV := APart.IndexOf(Vertex[j]);
131 if idxV <> -1 then
132 Vertex[j] := vertexInfo[idxV].dup;
133 end;
134 end;
135 end;
136
137 for i := APart.VertexCount-1 downto 0 do
138 APart.RemoveVertex(i);
139end;
140
141function TBGRAObject3D.GetScene: TObject;
142begin
143 result := FScene;
144end;
145
146function TBGRAObject3D.GetRefCount: integer;
147begin
148 result := RefCount;
149end;
150
151procedure TBGRAObject3D.SetBiface(AValue: boolean);
152var i: integer;
153begin
154 for i := 0 to GetFaceCount-1 do
155 GetFace(i).Biface := AValue;
156end;
157
158procedure TBGRAObject3D.ForEachVertex(ACallback: TVertex3DCallback);
159begin
160 FMainPart.ForEachVertex(ACallback);
161end;
162
163procedure TBGRAObject3D.ForEachFace(ACallback: TFace3DCallback);
164var i: integer;
165begin
166 for i := 0 to GetFaceCount-1 do
167 ACallback(GetFace(i));
168end;
169
170procedure TBGRAObject3D.Update;
171var
172 i: Integer;
173begin
174 if FParentLighting and (FLightingNormal <> FScene.DefaultLightingNormal) then
175 FLightingNormal := FScene.DefaultLightingNormal;
176
177 if FFaceColorsInvalidated then
178 begin
179 for i := 0 to FFaceCount-1 do
180 FFaces[i].ComputeVertexColors;
181 FFaceColorsInvalidated := false;
182 end;
183
184 if FMaterialInvalidated then
185 begin
186 for i := 0 to FFaceCount-1 do
187 FFaces[i].UpdateMaterial;
188 FMaterialInvalidated := false;
189 end;
190end;
191
192function TBGRAObject3D.GetLightingNormal: TLightingNormal3D;
193begin
194 result := FLightingNormal;
195end;
196
197function TBGRAObject3D.GetParentLighting: boolean;
198begin
199 result := FParentLighting;
200end;
201
202procedure TBGRAObject3D.SetLightingNormal(const AValue: TLightingNormal3D);
203begin
204 FLightingNormal := AValue;
205 FParentLighting:= False;
206end;
207
208procedure TBGRAObject3D.SetParentLighting(const AValue: boolean);
209begin
210 FParentLighting:= AValue;
211end;
212
213procedure TBGRAObject3D.ComputeWithMatrix(constref AMatrix: TMatrix3D; constref AProjection: TProjection3D);
214var
215 i: Integer;
216begin
217 FMainPart.ComputeWithMatrix(AMatrix,AProjection);
218 for i := 0 to FFaceCount-1 do
219 FFaces[i].ComputeViewNormalAndCenter;
220 FMainPart.NormalizeViewNormal;
221end;
222
223function TBGRAObject3D.AddFaceReversed(const AVertices: array of IBGRAVertex3D
224 ): IBGRAFace3D;
225var
226 tempVertices: array of IBGRAVertex3D;
227 i: Integer;
228begin
229 setlength(tempVertices,length(AVertices));
230 for i := 0 to high(tempVertices) do
231 tempVertices[i] := AVertices[high(AVertices)-i];
232 result := AddFace(tempVertices);
233end;
234
235function TBGRAObject3D.AddFace(const AVertices: array of IBGRAVertex3D): IBGRAFace3D;
236begin
237 result := TBGRAFace3D.Create(self,AVertices);
238 AddFace(result);
239end;
240
241function TBGRAObject3D.AddFace(const AVertices: array of IBGRAVertex3D;
242 ABiface: boolean): IBGRAFace3D;
243begin
244 result := TBGRAFace3D.Create(self,AVertices);
245 result.Biface := ABiface;
246 AddFace(result);
247end;
248
249function TBGRAObject3D.AddFace(const AVertices: array of IBGRAVertex3D; ATexture: IBGRAScanner): IBGRAFace3D;
250var Face: IBGRAFace3D;
251begin
252 Face := TBGRAFace3D.Create(self,AVertices);
253 Face.Texture := ATexture;
254 AddFace(Face);
255 result := face;
256end;
257
258function TBGRAObject3D.AddFace(const AVertices: array of IBGRAVertex3D;
259 AColor: TBGRAPixel): IBGRAFace3D;
260var Face: IBGRAFace3D;
261begin
262 Face := TBGRAFace3D.Create(self,AVertices);
263 Face.SetColor(AColor);
264 Face.Texture := nil;
265 AddFace(Face);
266 result := face;
267end;
268
269function TBGRAObject3D.AddFace(const AVertices: array of IBGRAVertex3D;
270 AColors: array of TBGRAPixel): IBGRAFace3D;
271var
272 i: Integer;
273begin
274 if length(AColors) <> length(AVertices) then
275 raise Exception.Create('Dimension mismatch');
276 result := TBGRAFace3D.Create(self,AVertices);
277 for i := 0 to high(AColors) do
278 result.VertexColor[i] := AColors[i];
279 AddFace(result);
280end;
281
282function TBGRAObject3D.GetFace(AIndex: integer): IBGRAFace3D;
283begin
284 if (AIndex < 0) or (AIndex >= FFaceCount) then
285 raise Exception.Create('Index out of bounds');
286 result := FFaces[AIndex];
287end;
288
289function TBGRAObject3D.GetFaceCount: integer;
290begin
291 result := FFaceCount;
292end;
293
294function TBGRAObject3D.GetTotalVertexCount: integer;
295begin
296 result := GetMainPart.TotalVertexCount;
297end;
298
299function TBGRAObject3D.GetTotalNormalCount: integer;
300begin
301 result := GetMainPart.TotalNormalCount;
302end;
303
304function TBGRAObject3D.GetMaterial: IBGRAMaterial3D;
305begin
306 result := FMaterial;
307end;
308
309
Note: See TracBrowser for help on using the repository browser.