Record TTriangle

Hierarchy
Properties

Unit

Declaration

type TTriangle = record

Description

Triangle in 3D. This object should always be initialized by Init, and updated only by it's methods (never modify fields of this object directly).

Overview

Fields

Public Local: TTriangleGeometry;
Public World: TTriangleGeometry;
Public InternalShape: TObject;
Public MailboxSavedTag: TMailboxTag;
Public MailboxIsIntersection: boolean;
Public MailboxIntersection: TVector3;
Public MailboxIntersectionDistance: Single;
Public Normal: TTriangle3;
Public TexCoord: TTriangle4;
Public Face: TFaceIndex;

Methods

Public procedure Init(AShape: TObject; const ATriangle: TTriangle3; const ANormal: TTriangle3; const ATexCoord: TTriangle4; const AFace: TFaceIndex);
Public function SegmentDirCollision( out Intersection: TVector3; out IntersectionDistance: Single; const Segment0, SegmentVector: TVector3; const SegmentTag: TMailboxTag): boolean;
Public function RayCollision( out Intersection: TVector3; out IntersectionDistance: Single; const RayOrigin, RayDirection: TVector3; const RayTag: TMailboxTag): boolean;
Public function ITexCoord(const Point: TVector3): TVector4;
Public function ITexCoord2D(const Point: TVector3): TVector2;
Public function INormal(const Point: TVector3): TVector3;
Public function INormalCore(const Point: TVector3): TVector3;

Description

Fields

Public Local: TTriangleGeometry;

Geometry of this item. We need two geometry descriptions:

  • Local is based on initial Triangle, given when constructing this TTriangle. It's constant for this TTriangle. It's used by octree collision routines, that is things like TBaseTrianglesOctree.SphereCollision, TBaseTrianglesOctree.RayCollision and such expect parameters in the same coord space.

    This may be local coord space of this shape (this is used by TShape.OctreeTriangles) or world coord space (this is used by TCastleSceneCore.OctreeTriangles).

  • World is the geometry of Local transformed to be in world coordinates. Initially, World is just a copy of Local.

    If Local already contains world-space geometry, then World can just remain constant, and so is always Local copy.

    If Local contains local shape-space geometry, then World will have to be updated by TTriangle.UpdateWorld whenever some octree item's geometry will be needed in world coords. This will have to be done e.g. by TBaseTrianglesOctree.XxxCollision for each returned item.

Public World: TTriangleGeometry;

Geometry of this item. We need two geometry descriptions:

  • Local is based on initial Triangle, given when constructing this TTriangle. It's constant for this TTriangle. It's used by octree collision routines, that is things like TBaseTrianglesOctree.SphereCollision, TBaseTrianglesOctree.RayCollision and such expect parameters in the same coord space.

    This may be local coord space of this shape (this is used by TShape.OctreeTriangles) or world coord space (this is used by TCastleSceneCore.OctreeTriangles).

  • World is the geometry of Local transformed to be in world coordinates. Initially, World is just a copy of Local.

    If Local already contains world-space geometry, then World can just remain constant, and so is always Local copy.

    If Local contains local shape-space geometry, then World will have to be updated by TTriangle.UpdateWorld whenever some octree item's geometry will be needed in world coords. This will have to be done e.g. by TBaseTrianglesOctree.XxxCollision for each returned item.

Public InternalShape: TObject;

Shape containing this triangle. This is always an instance of TShape class, but due to unit dependencies it cannot be declared as such here. Use CastleShapes unit to have a "record helper" method that returns a Shape as TShape instance.

Public MailboxSavedTag: TMailboxTag;

Tag of an object (like a ray or a line segment) for which we have saved an intersection result. Intersection result is in MailboxIsIntersection, MailboxIntersection, MailboxIntersectionDistance.

To make things correct, we obviously assume that every segment and ray have different tags. Also, tag -1 is reserved. In practice, we simply initialize MailboxSavedTag to -1, and each new segment/ray get consecutive tags starting from 0.

History: a naive implementation at the beginning was not using tags, instead I had MailboxState (empty, ray or segment) and I was storing ray/line vectors (2 TVector3 values). This had much larger size (6 * SizeOf(Single) + SizeOf(enum) = 28 bytes) than tag, which is important (3D models have easily thousands of TTriangle). And it took longer to compare and assign, so it was working much slower.

Public MailboxIsIntersection: boolean;

This item has no description.

Public MailboxIntersection: TVector3;

This item has no description.

Public MailboxIntersectionDistance: Single;

This item has no description.

Public Normal: TTriangle3;

Normal vectors, at each triangle vertex.

Public TexCoord: TTriangle4;

Texture coordinates, for each triangle point.

Each texture coordinate is a 4D vector, since we may have 3D textures referenced by 4D (homogeneous) coordinates. For normal 2D textures, you can simply take the first 2 components of the vector, and ignore the remaining 2 components. The 3th component is always 0 if was not specified (if model had only 2D texture coords). The 4th component is always 1 if was not specified (if model had only 2D or 3D texture coords).

In case of multi-texturing, this describes coordinates of the first texture unit. In case no texture is defined, this is undefined.

Public Face: TFaceIndex;

The indexes of this face, for editing / removing it. See TFaceIndex.

Methods

Public procedure Init(AShape: TObject; const ATriangle: TTriangle3; const ANormal: TTriangle3; const ATexCoord: TTriangle4; const AFace: TFaceIndex);

Initialize new triangle. Given ATriangle must satisfy ATriangle.IsValid.

Public function SegmentDirCollision( out Intersection: TVector3; out IntersectionDistance: Single; const Segment0, SegmentVector: TVector3; const SegmentTag: TMailboxTag): boolean;

Check collisions between TTriangle and ray/segment.

Always use these routines to check for collisions, to use mailboxes if possible. Mailboxes are used only if this was compiled with TRIANGLE_OCTREE_USE_MAILBOX defined.

Increments TriangleCollisionTestsCounter if actual test was done (that is, if we couldn't use mailbox to get the result quickier).

Public function RayCollision( out Intersection: TVector3; out IntersectionDistance: Single; const RayOrigin, RayDirection: TVector3; const RayTag: TMailboxTag): boolean;

This item has no description.

Public function ITexCoord(const Point: TVector3): TVector4;

For a given position (in world coordinates), return the texture coordinate at this point. It is an interpolated texture coordinate from our per-vertex texture coordinates in TexCoord field.

This assumes that Position actually lies within the triangle.

The ITexCoord2D returns the same, but cut to the first 2 texture coordinate components. Usable for normal 2D textures.

Public function ITexCoord2D(const Point: TVector3): TVector2;

This item has no description.

Public function INormal(const Point: TVector3): TVector3;

For a given position (in world coordinates), return the smooth normal vector at this point. It is an interpolated normal from our per-vertex normals in the Normal field, thus is supports also the case when you have smooth shading (normals change throughout the triangle).

Like the Normal field, the returned vector is a normal vector in the local coordinates. Use TTriangleHelper.INormalWorldSpace to get a normal vector in scene coordinates.

This assumes that Position actally lies within the triangle.

Public function INormalCore(const Point: TVector3): TVector3;

Like INormal, but not necessarily normalized.


Generated by PasDoc 0.16.0-snapshot.