| 1 |
{ |
| 2 |
Copyright 2010-2022 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Deconstruct scene into triangles using `TCastleShape.Triangulate`. } |
| 17 |
program triangulate_demo; |
| 18 |
|
| 19 |
{$ifdef MSWINDOWS} {$apptype CONSOLE} {$endif} |
| 20 |
|
| 21 |
uses SysUtils, CastleVectors, CastleSceneCore, CastleShapes, CastleTriangles, |
| 22 |
CastleFilesUtils; |
| 23 |
|
| 24 |
type |
| 25 |
TTriangleHandler = class |
| 26 |
procedure HandleTriangle(Shape: TObject; |
| 27 |
const Position: TTriangle3; |
| 28 |
const Normal: TTriangle3; const TexCoord: TTriangle4; |
| 29 |
const Face: TFaceIndex); |
| 30 |
end; |
| 31 |
|
| 32 |
procedure TTriangleHandler.HandleTriangle(Shape: TObject; |
| 33 |
const Position: TTriangle3; |
| 34 |
const Normal: TTriangle3; const TexCoord: TTriangle4; |
| 35 |
const Face: TFaceIndex); |
| 36 |
begin |
| 37 |
Writeln('Triangle position (in world coordinates):'); |
| 38 |
Write(Position.ToString); |
| 39 |
end; |
| 40 |
|
| 41 |
var |
| 42 |
Scene: TCastleSceneCore; |
| 43 |
ShapeList: TShapeList; |
| 44 |
Shape: TShape; |
| 45 |
Handler: TTriangleHandler; |
| 46 |
begin |
| 47 |
Scene := TCastleSceneCore.Create(nil); |
| 48 |
try |
| 49 |
Scene.Load('castle-data:/teapot.x3dv'); |
| 50 |
|
| 51 |
ShapeList := Scene.Shapes.TraverseList(true); |
| 52 |
Handler := TTriangleHandler.Create; |
| 53 |
try |
| 54 |
for Shape in ShapeList do |
| 55 |
{ Try also LocalTriangulate instead of Triangulate, |
| 56 |
to have Position in local shape coordinates. } |
| 57 |
Shape.Triangulate({$ifdef FPC}@{$endif} Handler.HandleTriangle); |
| 58 |
finally FreeAndNil(Handler) end; |
| 59 |
|
| 60 |
{ An alternative method: use Scene.InternalOctreeVisibleTriangles.Triangles. |
| 61 |
This is available only when Scene.Spatial contains appropriate flag. |
| 62 |
This method is useful in larger programs, when besides writing triangles, |
| 63 |
you want to e.g. render or perform collision detection with the scene. |
| 64 |
In such case, you will have Scene.InternalOctreeVisibleTriangles |
| 65 |
created anyway. So you can use it also to get triangles list. |
| 66 |
|
| 67 |
var |
| 68 |
TriangleInfo: PTriangle; |
| 69 |
|
| 70 |
... |
| 71 |
|
| 72 |
Scene.Spatial := Scene.Spatial + [ssVisibleTriangles]; |
| 73 |
Scene.TriangleOctreeLimits^.MaxDepth := 1; |
| 74 |
for I := 0 to Scene.InternalOctreeVisibleTriangles.Triangles.Count - 1 do |
| 75 |
begin |
| 76 |
TriangleInfo := PTriangle(Scene.InternalOctreeVisibleTriangles.Triangles.Ptr(I)); |
| 77 |
Writeln('Triangle position (in world coordinates):'); |
| 78 |
Write(TriangleInfo^.World.Triangle.ToString); |
| 79 |
end; |
| 80 |
} |
| 81 |
|
| 82 |
finally FreeAndNil(Scene) end; |
| 83 |
end. |