| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-2018 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
{ Current state (transformation and such) when traversing VRML/X3D graph. |
| 19 |
|
| 20 |
For VRML/X3D >= 2.0 this could be simpler, as VRML/X3D >= 2.0 doesn't need |
| 21 |
to keep track for example of the @link(VRML1State). |
| 22 |
But we want to still handle VRML 1.0, 100% correctly, so here we are: |
| 23 |
this class contains whole state needed for any VRML/X3D version. } |
| 24 |
TX3DGraphTraverseState = class |
| 25 |
strict private |
| 26 |
FVRML1State: TVRML1State; |
| 27 |
procedure CommonCreate; |
| 28 |
public |
| 29 |
{ Nodes that affect how following nodes are rendered, |
| 30 |
mostly for VRML 1.0 state. } |
| 31 |
property VRML1State: TVRML1State read FVRML1State; |
| 32 |
{$ifdef FPC} |
| 33 |
property LastNodes: TVRML1State read FVRML1State; deprecated 'use VRML1State'; |
| 34 |
{$endif} |
| 35 |
public |
| 36 |
{ Lights active in this state. |
| 37 |
|
| 38 |
May be @nil if empty. This way we optimize creation / assignment time, |
| 39 |
which happen very often with TX3DGraphTraverseState during VRML/X3D |
| 40 |
traversing. |
| 41 |
|
| 42 |
Note that VRML >= 2.0 "global" lights are added from TCastleSceneCore, |
| 43 |
not during the traverse pass. } |
| 44 |
Lights: TLightInstancesList; |
| 45 |
|
| 46 |
procedure AddLight(const Light: TLightInstance); |
| 47 |
public |
| 48 |
{ Transformation of this state. |
| 49 |
Calculated efficiently while traversing the X3D graph. } |
| 50 |
Transformation: TTransformation; |
| 51 |
|
| 52 |
{ Copy transformation-related fields from Source. |
| 53 |
Copies @link(Transformation). |
| 54 |
Copies also the @link(ClipPlanes) list, as it contains the transformation information. } |
| 55 |
procedure AssignTransform(Source: TX3DGraphTraverseState); |
| 56 |
|
| 57 |
function Transform: TMatrix4; deprecated 'use Transformation.Transform'; |
| 58 |
function InvertedTransform: TMatrix4; deprecated 'use Transformation.InverseTransform'; |
| 59 |
|
| 60 |
public |
| 61 |
{ Current texture transformation. Usable only for VRML 1.0, in VRML 2.0 |
| 62 |
texture transformations don't accumulate like modelview transformations. } |
| 63 |
TextureTransform: TMatrix4; |
| 64 |
|
| 65 |
ShapeNode: TAbstractShapeNode; |
| 66 |
|
| 67 |
constructor CreateCopy(Source: TX3DGraphTraverseState); |
| 68 |
|
| 69 |
{ Standard constructor. |
| 70 |
Uses global VRML1DefaultState as default nodes for VRML1State. |
| 71 |
This makes it fast, and improves cache (more nodes have equal reference). } |
| 72 |
constructor Create; |
| 73 |
|
| 74 |
destructor Destroy; override; |
| 75 |
|
| 76 |
procedure Assign(Source: TX3DGraphTraverseState); |
| 77 |
|
| 78 |
{ Clear the whole state, just like this TX3DGraphTraverseState instance |
| 79 |
would be just constructed. } |
| 80 |
procedure Clear; |
| 81 |
|
| 82 |
{ Compare with other TX3DGraphTraverseState instance. |
| 83 |
True if these two states, when applied to the same geometry, |
| 84 |
result in the same TGeometryArrays output. |
| 85 |
If IgnoreTransform then we should ignore transformation during comparison |
| 86 |
(it means that renderer is absolutely sure that different transformation |
| 87 |
of geometry doesn't affect the generated arrays). } |
| 88 |
function Equals(SecondValue: TX3DGraphTraverseState; |
| 89 |
const IgnoreTransform: boolean): boolean; {$ifdef TOBJECT_HAS_EQUALS} reintroduce; {$endif} |
| 90 |
|
| 91 |
{ Primary texture that determines RGB color and opacity. |
| 92 |
See @link(TAppearanceNode.MainTexture) for definition. } |
| 93 |
function MainTexture: TAbstractTextureNode; overload; |
| 94 |
function MainTexture(const GeometryForTexCoord: TAbstractGeometryNode; |
| 95 |
out TexCoordIndex: Integer): TAbstractTextureNode; overload; |
| 96 |
|
| 97 |
{ Returns BlendMode for this state, or @nil if not present. } |
| 98 |
function BlendMode: TBlendModeNode; |
| 99 |
|
| 100 |
public |
| 101 |
{ Information if you're within any inline node or expanded prototype. |
| 102 |
InsideInline = 0 means you're not inside any inline node, |
| 103 |
1 means you're inside one inline, 2 means you're within content |
| 104 |
inlined from yet another inline node, and so on. |
| 105 |
Analogous for InsidePrototype. |
| 106 |
|
| 107 |
These are measured from the node where you |
| 108 |
started TX3DNode.Traverse call, that is they assume that the initial |
| 109 |
node from where you're traversing is at level 0 (not inside inline |
| 110 |
or expanded prototype). |
| 111 |
|
| 112 |
These are useful to establish "run-time name scope" of X3D, |
| 113 |
see X3D spec 4.4.7 (needed e.g. when handling Anchor node with |
| 114 |
"#Viewpoint" URL). |
| 115 |
Interpreting this for our implementation, |
| 116 |
specification says that if you traverse |
| 117 |
from node X, then all traversed nodes with |
| 118 |
InsideInline = InsidePrototype = 0 are within the same name scope. |
| 119 |
|
| 120 |
Also this is useful for searching for the first bindable node after |
| 121 |
loading the file. Specification says to ignore inline content |
| 122 |
in this case (although prototype content is Ok in this case). |
| 123 |
|
| 124 |
When scriping will be implemented, probably analogous |
| 125 |
InsideScriptCreatedNode will also be needed, as the spec says |
| 126 |
that bindable nodes should not be searched within things like |
| 127 |
"Browser.createX3DFromString()". |
| 128 |
|
| 129 |
@groupBegin } |
| 130 |
InsideInline: Cardinal; |
| 131 |
InsidePrototype: Cardinal; |
| 132 |
{ @groupEnd } |
| 133 |
|
| 134 |
{ This is > 0 when traversing nodes that do not participate in |
| 135 |
collision detection. |
| 136 |
|
| 137 |
This counts how many times are we inside Collision node that |
| 138 |
prevents us from colliding. |
| 139 |
More precise, this is increased when we traverse inside |
| 140 |
Collision.children with Collision.enabled = FALSE or |
| 141 |
Collision.proxy <> NULL (since then Collision.children are |
| 142 |
not collidable). } |
| 143 |
InsideIgnoreCollision: Cardinal; |
| 144 |
|
| 145 |
{ This is > 0 when traversing nodes that are not visible. |
| 146 |
|
| 147 |
This counts how many times are we inside Collision.proxy |
| 148 |
(with Collision.enabled = TRUE). Collision.proxy is never |
| 149 |
visible. } |
| 150 |
InsideInvisible: Cardinal; |
| 151 |
|
| 152 |
{ Active pointing device sensors in this state. |
| 153 |
This can contain only nodes descending from |
| 154 |
X3DPointingDeviceSensorNode, and additionally an Anchor node. |
| 155 |
|
| 156 |
This list automatically honours VRML / X3D rules for what |
| 157 |
pointing device sensor is active: pointing device within some |
| 158 |
group node affects all children in this group node. |
| 159 |
(And when multiple pointing device sensors are within the same |
| 160 |
grouping node, they all work.) } |
| 161 |
PointingDeviceSensors: TPointingDeviceSensorList; |
| 162 |
|
| 163 |
{ For Humanoid skeleton, these contain cummulated joint transformation. } |
| 164 |
HumanoidTransformation: TTransformation; |
| 165 |
{ Humanoid node containing us, or @nil if none. } |
| 166 |
Humanoid: THAnimHumanoidNode; |
| 167 |
|
| 168 |
{ ClipPlanes affecting nodes within this state. |
| 169 |
|
| 170 |
They are collected here regardless of whether they are enabled or not. |
| 171 |
This allows efficient implementation of @code(ClipPlane.enabled) |
| 172 |
dynamic changes. |
| 173 |
|
| 174 |
Ordered from the most global to most local ones. |
| 175 |
So, following the X3D specification, we should consider the first |
| 176 |
clip planes on this list more important. |
| 177 |
|
| 178 |
Always @nil if empty. This allows us to optimize TX3DGraphTraverseState |
| 179 |
processing. } |
| 180 |
ClipPlanes: TClipPlaneList; |
| 181 |
|
| 182 |
{ Local fog settings. When @nil, it means use global fog (or no fog, |
| 183 |
if no global fog defined in file). } |
| 184 |
LocalFog: TLocalFogNode; |
| 185 |
|
| 186 |
{ Effects (TEffectNode) affecting this state. } |
| 187 |
Effects: TX3DNodeList; |
| 188 |
|
| 189 |
function AddClipPlane: PClipPlane; |
| 190 |
|
| 191 |
{ Material information at this state. |
| 192 |
See TMaterialInfo for usage description. |
| 193 |
Returns @nil when no node determines material properties |
| 194 |
(which indicates white unlit look). |
| 195 |
|
| 196 |
Returned TMaterialInfo is valid only as long as the underlying |
| 197 |
Material or CommonSurfaceShader node exists. |
| 198 |
Do not free it yourself, it will be automatically freed. } |
| 199 |
function MaterialInfo: TMaterialInfo; |
| 200 |
end; |
| 201 |
|
| 202 |
{ Stack of TX3DGraphTraverseState. |
| 203 |
|
| 204 |
Allows you for much faster |
| 205 |
creation/destruction of TX3DGraphTraverseState instances. |
| 206 |
Although you can always construct / destruct TX3DGraphTraverseState |
| 207 |
as normal objects, in some cases this is too slow: when traversing VRML/X3D graph |
| 208 |
(e.g. profile animate_3d_model_by_code_2), merely |
| 209 |
creating/destroying TX3DGraphTraverseState instances takes a noticeable |
| 210 |
amount of time. |
| 211 |
|
| 212 |
This stack allows you to do this faster, first of all by |
| 213 |
internally using a prepared pool of instances. |
| 214 |
|
| 215 |
Each PushClear call creates a clear state instance, and places |
| 216 |
it on the stack. |
| 217 |
Each Push call creates a copy of current top and places it on the stack. |
| 218 |
Each Pop removes and destroys the last instance added by Push. |
| 219 |
|
| 220 |
Naturally, you can call Push and Top only when the stack is not empty. |
| 221 |
In practice, using the stack always starts in TX3DNode.Traverse, |
| 222 |
where we push initial clear state. So the stack passed to various |
| 223 |
callbacks, TX3DNode.BeforeTraverse and such is always guaranteed non-empty. |
| 224 |
|
| 225 |
Note that for speed purposes all Traverse calls actually |
| 226 |
share a single stack. That is, |
| 227 |
to avoid creating TX3DGraphTraverseStateStack instance each time |
| 228 |
(because even creating TX3DGraphTraverseStateStack |
| 229 |
takes some time (as it prepares a pool of TX3DGraphTraverseState |
| 230 |
instances, to allow fast push/pop)), TX3DNode simply reuses a single |
| 231 |
global TX3DGraphTraverseStateStack instance. This means that, |
| 232 |
if you execute Traverse while being inside other Traverse, you |
| 233 |
must first finish innermost Traverse before continuing with the outer. } |
| 234 |
TX3DGraphTraverseStateStack = class |
| 235 |
strict private |
| 236 |
Items: array of TX3DGraphTraverseState; |
| 237 |
ItemsAllocated: Cardinal; |
| 238 |
procedure GrowItems; |
| 239 |
public |
| 240 |
constructor Create; |
| 241 |
destructor Destroy; override; |
| 242 |
|
| 243 |
{ Remove everything. } |
| 244 |
procedure Clear; |
| 245 |
|
| 246 |
{ Push a clear state on the stack. Clear state has everything set |
| 247 |
like a TX3DGraphTraverseState right after creating. } |
| 248 |
procedure PushClear; |
| 249 |
{ Push a copy of current top on the stack. } |
| 250 |
procedure Push; overload; |
| 251 |
{ Push a copy of given Item on the stack. |
| 252 |
We copy by TX3DGraphTraverseState.Assign, we don't copy the reference. } |
| 253 |
procedure Push(const Item: TX3DGraphTraverseState); overload; |
| 254 |
procedure Pop; |
| 255 |
|
| 256 |
{ Peek at the top of the stack. } |
| 257 |
function Top: TX3DGraphTraverseState; |
| 258 |
function PreviousTop: TX3DGraphTraverseState; |
| 259 |
end; |
| 260 |
|
| 261 |
{$endif read_interface} |
| 262 |
|
| 263 |
{$ifdef read_implementation} |
| 264 |
|
| 265 |
{ TX3DGraphTraverseState ---------------------------------------------------- } |
| 266 |
|
| 267 |
var |
| 268 |
{ Starting state nodes for TX3DGraphTraverseState.Create. } |
| 269 |
VRML1DefaultState: TVRML1State; |
| 270 |
|
| 271 |
procedure TX3DGraphTraverseState.CommonCreate; |
| 272 |
begin |
| 273 |
inherited Create; |
| 274 |
PointingDeviceSensors := TPointingDeviceSensorList.Create(false); |
| 275 |
FVRML1State := TVRML1State.Create; |
| 276 |
end; |
| 277 |
|
| 278 |
constructor TX3DGraphTraverseState.CreateCopy(Source: TX3DGraphTraverseState); |
| 279 |
begin |
| 280 |
CommonCreate; |
| 281 |
Assign(Source); |
| 282 |
end; |
| 283 |
|
| 284 |
constructor TX3DGraphTraverseState.Create; |
| 285 |
begin |
| 286 |
CommonCreate; |
| 287 |
|
| 288 |
Transformation.Init; |
| 289 |
|
| 290 |
{ THAnimHumanoidNode.BeforeTraverse will initialize it anyway. |
| 291 |
But set it also here, just in case we have Joint without surrounding |
| 292 |
Humanoid node. (Otherwise Matrix.MultPoint may raise errors in |
| 293 |
THAnimJointNode.ApplyTransform, when multiplying points with 0 matrix, |
| 294 |
testcase is |
| 295 |
castle-model-viewer ~/3dmodels/vrmlx3d/hanim/tecfa.unige.ch/vrml/objects/avatars/blaxxun/kambi_hanim_10_test.wrl.) } |
| 296 |
HumanoidTransformation.Init; |
| 297 |
|
| 298 |
TextureTransform := TMatrix4.Identity; |
| 299 |
FVRML1State.Assign(VRML1DefaultState); |
| 300 |
end; |
| 301 |
|
| 302 |
destructor TX3DGraphTraverseState.Destroy; |
| 303 |
begin |
| 304 |
FreeAndNil(FVRML1State); |
| 305 |
FreeAndNil(Lights); |
| 306 |
FreeAndNil(PointingDeviceSensors); |
| 307 |
FreeAndNil(ClipPlanes); |
| 308 |
FreeAndNil(Effects); |
| 309 |
inherited; |
| 310 |
end; |
| 311 |
|
| 312 |
procedure TX3DGraphTraverseState.Clear; |
| 313 |
begin |
| 314 |
Transformation.Init; |
| 315 |
HumanoidTransformation.Init; |
| 316 |
|
| 317 |
TextureTransform := TMatrix4.Identity; |
| 318 |
FVRML1State.Assign(VRML1DefaultState); |
| 319 |
ShapeNode := nil; |
| 320 |
InsideInline := 0; |
| 321 |
InsidePrototype := 0; |
| 322 |
InsideIgnoreCollision := 0; |
| 323 |
InsideInvisible := 0; |
| 324 |
LocalFog := nil; |
| 325 |
|
| 326 |
PointingDeviceSensors.Count := 0; |
| 327 |
FreeAndNil(Lights); |
| 328 |
FreeAndNil(ClipPlanes); |
| 329 |
FreeAndNil(Effects); |
| 330 |
end; |
| 331 |
|
| 332 |
procedure TX3DGraphTraverseState.AddLight(const Light: TLightInstance); |
| 333 |
begin |
| 334 |
if Lights = nil then |
| 335 |
Lights := TLightInstancesList.Create; |
| 336 |
Lights.Add(Light); |
| 337 |
end; |
| 338 |
|
| 339 |
function TX3DGraphTraverseState.AddClipPlane: PClipPlane; |
| 340 |
begin |
| 341 |
if ClipPlanes = nil then |
| 342 |
ClipPlanes := TClipPlaneList.Create; |
| 343 |
Result := PClipPlane(ClipPlanes.Add()); |
| 344 |
end; |
| 345 |
|
| 346 |
procedure TX3DGraphTraverseState.Assign(Source: TX3DGraphTraverseState); |
| 347 |
begin |
| 348 |
AssignTransform(Source); |
| 349 |
|
| 350 |
TextureTransform := Source.TextureTransform; |
| 351 |
FVRML1State.Assign(Source.FVRML1State); |
| 352 |
ShapeNode := Source.ShapeNode; |
| 353 |
InsideInline := Source.InsideInline; |
| 354 |
InsidePrototype := Source.InsidePrototype; |
| 355 |
InsideIgnoreCollision := Source.InsideIgnoreCollision; |
| 356 |
InsideInvisible := Source.InsideInvisible; |
| 357 |
Humanoid := Source.Humanoid; |
| 358 |
LocalFog := Source.LocalFog; |
| 359 |
|
| 360 |
PointingDeviceSensors.Assign(Source.PointingDeviceSensors); |
| 361 |
|
| 362 |
if Source.Lights <> nil then |
| 363 |
begin |
| 364 |
if Lights = nil then |
| 365 |
Lights := TLightInstancesList.Create; |
| 366 |
Lights.Assign(Source.Lights); |
| 367 |
end else |
| 368 |
FreeAndNil(Lights); |
| 369 |
end; |
| 370 |
|
| 371 |
procedure TX3DGraphTraverseState.AssignTransform( |
| 372 |
Source: TX3DGraphTraverseState); |
| 373 |
begin |
| 374 |
Transformation := Source.Transformation; |
| 375 |
HumanoidTransformation := Source.HumanoidTransformation; |
| 376 |
|
| 377 |
if PointingDeviceSensors.Count <> 0 then |
| 378 |
PointingDeviceSensors.Transformation := Source.PointingDeviceSensors.Transformation; |
| 379 |
|
| 380 |
if Source.ClipPlanes <> nil then |
| 381 |
begin |
| 382 |
if ClipPlanes = nil then |
| 383 |
ClipPlanes := TClipPlaneList.Create; |
| 384 |
ClipPlanes.Assign(Source.ClipPlanes); |
| 385 |
end else |
| 386 |
FreeAndNil(ClipPlanes); |
| 387 |
|
| 388 |
if Source.Effects <> nil then |
| 389 |
begin |
| 390 |
if Effects = nil then |
| 391 |
Effects := TX3DNodeList.Create(false); |
| 392 |
Effects.Assign(Source.Effects); |
| 393 |
end else |
| 394 |
FreeAndNil(Effects); |
| 395 |
end; |
| 396 |
|
| 397 |
function TX3DGraphTraverseState.Transform: TMatrix4; |
| 398 |
begin |
| 399 |
Result := Transformation.Transform; |
| 400 |
end; |
| 401 |
|
| 402 |
function TX3DGraphTraverseState.InvertedTransform: TMatrix4; |
| 403 |
begin |
| 404 |
Result := Transformation.InverseTransform; |
| 405 |
end; |
| 406 |
|
| 407 |
function TX3DGraphTraverseState.Equals(SecondValue: TX3DGraphTraverseState; |
| 408 |
const IgnoreTransform: boolean): boolean; |
| 409 |
var |
| 410 |
SN: TVRML1StateNode; |
| 411 |
begin |
| 412 |
{ Many fields are ignored by Equals, as they have no effect on generated |
| 413 |
TGeometryArrays for shapes. Like InsideInline, InsidePrototype and many |
| 414 |
others. } |
| 415 |
|
| 416 |
Result := |
| 417 |
(IgnoreTransform or TMatrix4.PerfectlyEquals( |
| 418 |
Transformation.Transform, |
| 419 |
SecondValue.Transformation.Transform)) and |
| 420 |
TMatrix4.PerfectlyEquals(TextureTransform, SecondValue.TextureTransform) and |
| 421 |
(ShapeNode = SecondValue.ShapeNode) and |
| 422 |
(LocalFog = SecondValue.LocalFog); |
| 423 |
|
| 424 |
if Result then |
| 425 |
begin |
| 426 |
for SN := Low(SN) to High(SN) do |
| 427 |
if SecondValue.VRML1State.Nodes[SN] <> VRML1State.Nodes[SN] then |
| 428 |
Exit(false); |
| 429 |
end; |
| 430 |
end; |
| 431 |
|
| 432 |
function TX3DGraphTraverseState.MainTexture: TAbstractTextureNode; |
| 433 |
begin |
| 434 |
Result := nil; |
| 435 |
if ShapeNode = nil then |
| 436 |
Result := VRML1State.Texture2 |
| 437 |
else |
| 438 |
if ShapeNode.Appearance <> nil then |
| 439 |
Result := ShapeNode.Appearance.MainTexture; |
| 440 |
end; |
| 441 |
|
| 442 |
function TX3DGraphTraverseState.MainTexture(const GeometryForTexCoord: TAbstractGeometryNode; |
| 443 |
out TexCoordIndex: Integer): TAbstractTextureNode; |
| 444 |
begin |
| 445 |
Result := nil; |
| 446 |
TexCoordIndex := -1; |
| 447 |
if ShapeNode = nil then |
| 448 |
Result := VRML1State.Texture2 |
| 449 |
else |
| 450 |
if ShapeNode.Appearance <> nil then |
| 451 |
Result := ShapeNode.Appearance.MainTexture(GeometryForTexCoord, TexCoordIndex); |
| 452 |
end; |
| 453 |
|
| 454 |
function TX3DGraphTraverseState.BlendMode: TBlendModeNode; |
| 455 |
var |
| 456 |
Node: TX3DNode; |
| 457 |
begin |
| 458 |
Result := nil; |
| 459 |
if ShapeNode <> nil then |
| 460 |
begin |
| 461 |
Node := ShapeNode.FdAppearance.Value; |
| 462 |
if (Node <> nil) and (Node is TAppearanceNode) then |
| 463 |
begin |
| 464 |
Node := TAppearanceNode(Node).FdBlendMode.Value; |
| 465 |
if (Node <> nil) and (Node is TBlendModeNode) then |
| 466 |
Result := TBlendModeNode(Node); |
| 467 |
end; |
| 468 |
end; |
| 469 |
end; |
| 470 |
|
| 471 |
function TX3DGraphTraverseState.MaterialInfo: TMaterialInfo; |
| 472 |
var |
| 473 |
Mat: TAbstractMaterialNode; |
| 474 |
SurfaceShader: TCommonSurfaceShaderNode; |
| 475 |
begin |
| 476 |
Result := nil; |
| 477 |
if ShapeNode <> nil then |
| 478 |
begin |
| 479 |
if ShapeNode.Appearance <> nil then |
| 480 |
begin |
| 481 |
{$warnings off} // using deprecated to keep backward compatibility |
| 482 |
SurfaceShader := ShapeNode.Appearance.CommonSurfaceShader; |
| 483 |
{$warnings on} |
| 484 |
if SurfaceShader <> nil then |
| 485 |
Result := SurfaceShader.MaterialInfo |
| 486 |
else |
| 487 |
begin |
| 488 |
Mat := ShapeNode.Appearance.Material; |
| 489 |
if Mat <> nil then |
| 490 |
Result := Mat.MaterialInfo |
| 491 |
else |
| 492 |
Result := nil; |
| 493 |
end; |
| 494 |
end; |
| 495 |
end else |
| 496 |
Result := VRML1State.Material.MaterialInfo; |
| 497 |
end; |
| 498 |
|
| 499 |
{ TX3DGraphTraverseStateStack --------------------------------------------- } |
| 500 |
|
| 501 |
constructor TX3DGraphTraverseStateStack.Create; |
| 502 |
begin |
| 503 |
inherited; |
| 504 |
end; |
| 505 |
|
| 506 |
destructor TX3DGraphTraverseStateStack.Destroy; |
| 507 |
var |
| 508 |
I: Integer; |
| 509 |
begin |
| 510 |
for I := 0 to Length(Items) - 1 do |
| 511 |
FreeAndNil(Items[I]); |
| 512 |
inherited; |
| 513 |
end; |
| 514 |
|
| 515 |
procedure TX3DGraphTraverseStateStack.GrowItems; |
| 516 |
var |
| 517 |
I, OldLen: Integer; |
| 518 |
begin |
| 519 |
OldLen := Length(Items); |
| 520 |
SetLength(Items, OldLen + 8); |
| 521 |
for I := OldLen to Length(Items) - 1 do |
| 522 |
Items[I] := nil; |
| 523 |
end; |
| 524 |
|
| 525 |
procedure TX3DGraphTraverseStateStack.PushClear; |
| 526 |
begin |
| 527 |
if ItemsAllocated = Cardinal(Length(Items)) then GrowItems; |
| 528 |
if Items[ItemsAllocated] = nil then |
| 529 |
Items[ItemsAllocated] := TX3DGraphTraverseState.Create; |
| 530 |
|
| 531 |
{ We could instead do Clear in Pop, and then we would know that all |
| 532 |
non allocated instances are always clear. |
| 533 |
|
| 534 |
But this would be slower: Push is called very often, |
| 535 |
much more often than PushClear (which is called only once |
| 536 |
for every traverse, while Push possibly many times). |
| 537 |
And Pop is called for every Push and PushClear. |
| 538 |
So it's better to optimize the most often called (Pop) than the |
| 539 |
least often called (PushClear). } |
| 540 |
|
| 541 |
Items[ItemsAllocated].Clear; |
| 542 |
Inc(ItemsAllocated); |
| 543 |
end; |
| 544 |
|
| 545 |
procedure TX3DGraphTraverseStateStack.Push; |
| 546 |
begin |
| 547 |
if ItemsAllocated = Cardinal(Length(Items)) then GrowItems; |
| 548 |
if Items[ItemsAllocated] = nil then |
| 549 |
Items[ItemsAllocated] := TX3DGraphTraverseState.Create; |
| 550 |
|
| 551 |
Items[ItemsAllocated].Assign(Items[ItemsAllocated - 1]); |
| 552 |
Inc(ItemsAllocated); |
| 553 |
end; |
| 554 |
|
| 555 |
procedure TX3DGraphTraverseStateStack.Push(const Item: TX3DGraphTraverseState); |
| 556 |
begin |
| 557 |
if ItemsAllocated = Cardinal(Length(Items)) then GrowItems; |
| 558 |
if Items[ItemsAllocated] = nil then |
| 559 |
Items[ItemsAllocated] := TX3DGraphTraverseState.Create; |
| 560 |
|
| 561 |
Items[ItemsAllocated].Assign(Item); |
| 562 |
Inc(ItemsAllocated); |
| 563 |
end; |
| 564 |
|
| 565 |
procedure TX3DGraphTraverseStateStack.Pop; |
| 566 |
begin |
| 567 |
Dec(ItemsAllocated); |
| 568 |
end; |
| 569 |
|
| 570 |
function TX3DGraphTraverseStateStack.Top: TX3DGraphTraverseState; |
| 571 |
begin |
| 572 |
Result := Items[ItemsAllocated - 1]; |
| 573 |
end; |
| 574 |
|
| 575 |
function TX3DGraphTraverseStateStack.PreviousTop: TX3DGraphTraverseState; |
| 576 |
begin |
| 577 |
Result := Items[ItemsAllocated - 2]; |
| 578 |
end; |
| 579 |
|
| 580 |
procedure TX3DGraphTraverseStateStack.Clear; |
| 581 |
begin |
| 582 |
ItemsAllocated := 0; |
| 583 |
end; |
| 584 |
|
| 585 |
{$endif read_implementation} |