| 1 | { TBGRAObject3D }
|
|---|
| 2 |
|
|---|
| 3 | procedure TBGRAObject3D.AddFace(AFace: IBGRAFace3D);
|
|---|
| 4 | begin
|
|---|
| 5 | if FFaceCount = length(FFaces) then
|
|---|
| 6 | setlength(FFaces,FFaceCount*2+3);
|
|---|
| 7 | FFaces[FFaceCount] := AFace;
|
|---|
| 8 | inc(FFaceCount);
|
|---|
| 9 | end;
|
|---|
| 10 |
|
|---|
| 11 | constructor TBGRAObject3D.Create(AScene: TBGRAScene3D);
|
|---|
| 12 | begin
|
|---|
| 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;
|
|---|
| 22 | end;
|
|---|
| 23 |
|
|---|
| 24 | destructor TBGRAObject3D.Destroy;
|
|---|
| 25 | begin
|
|---|
| 26 | FMaterial := nil;
|
|---|
| 27 | fillchar(FTexture,sizeof(FTexture),0);
|
|---|
| 28 | inherited Destroy;
|
|---|
| 29 | end;
|
|---|
| 30 |
|
|---|
| 31 | procedure TBGRAObject3D.Clear;
|
|---|
| 32 | begin
|
|---|
| 33 | FFaces := nil;
|
|---|
| 34 | FFaceCount := 0;
|
|---|
| 35 | FMainPart.Clear(True);
|
|---|
| 36 | end;
|
|---|
| 37 |
|
|---|
| 38 | procedure TBGRAObject3D.InvalidateColor;
|
|---|
| 39 | begin
|
|---|
| 40 | FFaceColorsInvalidated := true;
|
|---|
| 41 | end;
|
|---|
| 42 |
|
|---|
| 43 | procedure TBGRAObject3D.InvalidateMaterial;
|
|---|
| 44 | begin
|
|---|
| 45 | FMaterialInvalidated := true;
|
|---|
| 46 | end;
|
|---|
| 47 |
|
|---|
| 48 | function TBGRAObject3D.GetColor: TBGRAPixel;
|
|---|
| 49 | begin
|
|---|
| 50 | result := FColor;
|
|---|
| 51 | end;
|
|---|
| 52 |
|
|---|
| 53 | function TBGRAObject3D.GetLight: Single;
|
|---|
| 54 | begin
|
|---|
| 55 | result := FLight;
|
|---|
| 56 | end;
|
|---|
| 57 |
|
|---|
| 58 | function TBGRAObject3D.GetTexture: IBGRAScanner;
|
|---|
| 59 | begin
|
|---|
| 60 | result := FTexture;
|
|---|
| 61 | end;
|
|---|
| 62 |
|
|---|
| 63 | function TBGRAObject3D.GetMainPart: IBGRAPart3D;
|
|---|
| 64 | begin
|
|---|
| 65 | result := FMainPart;
|
|---|
| 66 | end;
|
|---|
| 67 |
|
|---|
| 68 | procedure TBGRAObject3D.SetColor(const AValue: TBGRAPixel);
|
|---|
| 69 | begin
|
|---|
| 70 | FColor := AValue;
|
|---|
| 71 | FTexture := nil;
|
|---|
| 72 | InvalidateColor;
|
|---|
| 73 | end;
|
|---|
| 74 |
|
|---|
| 75 | procedure TBGRAObject3D.SetLight(const AValue: Single);
|
|---|
| 76 | begin
|
|---|
| 77 | FLight := AValue;
|
|---|
| 78 | end;
|
|---|
| 79 |
|
|---|
| 80 | procedure TBGRAObject3D.SetTexture(const AValue: IBGRAScanner);
|
|---|
| 81 | begin
|
|---|
| 82 | FTexture := AValue;
|
|---|
| 83 | InvalidateMaterial;
|
|---|
| 84 | end;
|
|---|
| 85 |
|
|---|
| 86 | procedure TBGRAObject3D.SetMaterial(const AValue: IBGRAMaterial3D);
|
|---|
| 87 | begin
|
|---|
| 88 | FMaterial := AValue;
|
|---|
| 89 | InvalidateMaterial;
|
|---|
| 90 | end;
|
|---|
| 91 |
|
|---|
| 92 | procedure TBGRAObject3D.RemoveUnusedVertices;
|
|---|
| 93 | begin
|
|---|
| 94 | GetMainPart.RemoveUnusedVertices;
|
|---|
| 95 | end;
|
|---|
| 96 |
|
|---|
| 97 | procedure TBGRAObject3D.SeparatePart(APart: IBGRAPart3D);
|
|---|
| 98 | var
|
|---|
| 99 | vertexInfo: array of record
|
|---|
| 100 | orig,dup: IBGRAVertex3D;
|
|---|
| 101 | end;
|
|---|
| 102 |
|
|---|
| 103 | i,j: integer;
|
|---|
| 104 | inPart,outPart: boolean;
|
|---|
| 105 | idxV: integer;
|
|---|
| 106 | begin
|
|---|
| 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);
|
|---|
| 139 | end;
|
|---|
| 140 |
|
|---|
| 141 | function TBGRAObject3D.GetScene: TObject;
|
|---|
| 142 | begin
|
|---|
| 143 | result := FScene;
|
|---|
| 144 | end;
|
|---|
| 145 |
|
|---|
| 146 | function TBGRAObject3D.GetRefCount: integer;
|
|---|
| 147 | begin
|
|---|
| 148 | result := RefCount;
|
|---|
| 149 | end;
|
|---|
| 150 |
|
|---|
| 151 | procedure TBGRAObject3D.SetBiface(AValue: boolean);
|
|---|
| 152 | var i: integer;
|
|---|
| 153 | begin
|
|---|
| 154 | for i := 0 to GetFaceCount-1 do
|
|---|
| 155 | GetFace(i).Biface := AValue;
|
|---|
| 156 | end;
|
|---|
| 157 |
|
|---|
| 158 | procedure TBGRAObject3D.ForEachVertex(ACallback: TVertex3DCallback);
|
|---|
| 159 | begin
|
|---|
| 160 | FMainPart.ForEachVertex(ACallback);
|
|---|
| 161 | end;
|
|---|
| 162 |
|
|---|
| 163 | procedure TBGRAObject3D.ForEachFace(ACallback: TFace3DCallback);
|
|---|
| 164 | var i: integer;
|
|---|
| 165 | begin
|
|---|
| 166 | for i := 0 to GetFaceCount-1 do
|
|---|
| 167 | ACallback(GetFace(i));
|
|---|
| 168 | end;
|
|---|
| 169 |
|
|---|
| 170 | procedure TBGRAObject3D.Update;
|
|---|
| 171 | var
|
|---|
| 172 | i: Integer;
|
|---|
| 173 | begin
|
|---|
| 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;
|
|---|
| 190 | end;
|
|---|
| 191 |
|
|---|
| 192 | function TBGRAObject3D.GetLightingNormal: TLightingNormal3D;
|
|---|
| 193 | begin
|
|---|
| 194 | result := FLightingNormal;
|
|---|
| 195 | end;
|
|---|
| 196 |
|
|---|
| 197 | function TBGRAObject3D.GetParentLighting: boolean;
|
|---|
| 198 | begin
|
|---|
| 199 | result := FParentLighting;
|
|---|
| 200 | end;
|
|---|
| 201 |
|
|---|
| 202 | procedure TBGRAObject3D.SetLightingNormal(const AValue: TLightingNormal3D);
|
|---|
| 203 | begin
|
|---|
| 204 | FLightingNormal := AValue;
|
|---|
| 205 | FParentLighting:= False;
|
|---|
| 206 | end;
|
|---|
| 207 |
|
|---|
| 208 | procedure TBGRAObject3D.SetParentLighting(const AValue: boolean);
|
|---|
| 209 | begin
|
|---|
| 210 | FParentLighting:= AValue;
|
|---|
| 211 | end;
|
|---|
| 212 |
|
|---|
| 213 | procedure TBGRAObject3D.ComputeWithMatrix(constref AMatrix: TMatrix3D; constref AProjection: TProjection3D);
|
|---|
| 214 | var
|
|---|
| 215 | i: Integer;
|
|---|
| 216 | begin
|
|---|
| 217 | FMainPart.ComputeWithMatrix(AMatrix,AProjection);
|
|---|
| 218 | for i := 0 to FFaceCount-1 do
|
|---|
| 219 | FFaces[i].ComputeViewNormalAndCenter;
|
|---|
| 220 | FMainPart.NormalizeViewNormal;
|
|---|
| 221 | end;
|
|---|
| 222 |
|
|---|
| 223 | function TBGRAObject3D.AddFaceReversed(const AVertices: array of IBGRAVertex3D
|
|---|
| 224 | ): IBGRAFace3D;
|
|---|
| 225 | var
|
|---|
| 226 | tempVertices: array of IBGRAVertex3D;
|
|---|
| 227 | i: Integer;
|
|---|
| 228 | begin
|
|---|
| 229 | setlength(tempVertices,length(AVertices));
|
|---|
| 230 | for i := 0 to high(tempVertices) do
|
|---|
| 231 | tempVertices[i] := AVertices[high(AVertices)-i];
|
|---|
| 232 | result := AddFace(tempVertices);
|
|---|
| 233 | end;
|
|---|
| 234 |
|
|---|
| 235 | function TBGRAObject3D.AddFace(const AVertices: array of IBGRAVertex3D): IBGRAFace3D;
|
|---|
| 236 | begin
|
|---|
| 237 | result := TBGRAFace3D.Create(self,AVertices);
|
|---|
| 238 | AddFace(result);
|
|---|
| 239 | end;
|
|---|
| 240 |
|
|---|
| 241 | function TBGRAObject3D.AddFace(const AVertices: array of IBGRAVertex3D;
|
|---|
| 242 | ABiface: boolean): IBGRAFace3D;
|
|---|
| 243 | begin
|
|---|
| 244 | result := TBGRAFace3D.Create(self,AVertices);
|
|---|
| 245 | result.Biface := ABiface;
|
|---|
| 246 | AddFace(result);
|
|---|
| 247 | end;
|
|---|
| 248 |
|
|---|
| 249 | function TBGRAObject3D.AddFace(const AVertices: array of IBGRAVertex3D; ATexture: IBGRAScanner): IBGRAFace3D;
|
|---|
| 250 | var Face: IBGRAFace3D;
|
|---|
| 251 | begin
|
|---|
| 252 | Face := TBGRAFace3D.Create(self,AVertices);
|
|---|
| 253 | Face.Texture := ATexture;
|
|---|
| 254 | AddFace(Face);
|
|---|
| 255 | result := face;
|
|---|
| 256 | end;
|
|---|
| 257 |
|
|---|
| 258 | function TBGRAObject3D.AddFace(const AVertices: array of IBGRAVertex3D;
|
|---|
| 259 | AColor: TBGRAPixel): IBGRAFace3D;
|
|---|
| 260 | var Face: IBGRAFace3D;
|
|---|
| 261 | begin
|
|---|
| 262 | Face := TBGRAFace3D.Create(self,AVertices);
|
|---|
| 263 | Face.SetColor(AColor);
|
|---|
| 264 | Face.Texture := nil;
|
|---|
| 265 | AddFace(Face);
|
|---|
| 266 | result := face;
|
|---|
| 267 | end;
|
|---|
| 268 |
|
|---|
| 269 | function TBGRAObject3D.AddFace(const AVertices: array of IBGRAVertex3D;
|
|---|
| 270 | AColors: array of TBGRAPixel): IBGRAFace3D;
|
|---|
| 271 | var
|
|---|
| 272 | i: Integer;
|
|---|
| 273 | begin
|
|---|
| 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);
|
|---|
| 280 | end;
|
|---|
| 281 |
|
|---|
| 282 | function TBGRAObject3D.GetFace(AIndex: integer): IBGRAFace3D;
|
|---|
| 283 | begin
|
|---|
| 284 | if (AIndex < 0) or (AIndex >= FFaceCount) then
|
|---|
| 285 | raise Exception.Create('Index out of bounds');
|
|---|
| 286 | result := FFaces[AIndex];
|
|---|
| 287 | end;
|
|---|
| 288 |
|
|---|
| 289 | function TBGRAObject3D.GetFaceCount: integer;
|
|---|
| 290 | begin
|
|---|
| 291 | result := FFaceCount;
|
|---|
| 292 | end;
|
|---|
| 293 |
|
|---|
| 294 | function TBGRAObject3D.GetTotalVertexCount: integer;
|
|---|
| 295 | begin
|
|---|
| 296 | result := GetMainPart.TotalVertexCount;
|
|---|
| 297 | end;
|
|---|
| 298 |
|
|---|
| 299 | function TBGRAObject3D.GetTotalNormalCount: integer;
|
|---|
| 300 | begin
|
|---|
| 301 | result := GetMainPart.TotalNormalCount;
|
|---|
| 302 | end;
|
|---|
| 303 |
|
|---|
| 304 | function TBGRAObject3D.GetMaterial: IBGRAMaterial3D;
|
|---|
| 305 | begin
|
|---|
| 306 | result := FMaterial;
|
|---|
| 307 | end;
|
|---|
| 308 |
|
|---|
| 309 |
|
|---|