| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-2024 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 |
function TElevationGridNode.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; |
| 18 |
var |
| 19 |
TriangleSet: TIndexedTriangleSetNode absolute Result; |
| 20 |
begin |
| 21 |
TriangleSet := InternalCreateTriangles( |
| 22 |
FdXDimension.Value, FdZDimension.Value, |
| 23 |
FdXSpacing.Value, FdZSpacing.Value, |
| 24 |
FdHeight.Items, |
| 25 |
TexCoord, |
| 26 |
{ Idea: We could expose TElevationGridNode.TriangulationIgnoreHeights |
| 27 |
as extra X3D field some day, if needed? } |
| 28 |
false |
| 29 |
); |
| 30 |
try |
| 31 |
// copy all additional fields, that are not handled by InternalCreateTriangles |
| 32 |
TriangleSet.X3DName := X3DName; |
| 33 |
TriangleSet.BaseUrl := BaseUrl; |
| 34 |
TriangleSet.Solid := Solid; |
| 35 |
TriangleSet.Ccw := Ccw; |
| 36 |
{ We order our coords such that we can simply copy normal/color nodes } |
| 37 |
TriangleSet.NormalPerVertex := NormalPerVertex; |
| 38 |
// TODO: InternalCreateTriangles should take this, and generate own normals if none provided |
| 39 |
TriangleSet.Normal := Normal; |
| 40 |
TriangleSet.ColorPerVertex := ColorPerVertex; |
| 41 |
TriangleSet.Color := Color; |
| 42 |
// Not available: TriangleSet.CreaseAngle := CreaseAngle; |
| 43 |
{$ifndef CASTLE_SLIM_NODES} |
| 44 |
TriangleSet.FogCoord := FogCoord; |
| 45 |
TriangleSet.FdAttrib.AssignValue(FdAttrib); |
| 46 |
{$endif} |
| 47 |
except FreeAndNil(TriangleSet); raise end; |
| 48 |
end; |
| 49 |
|
| 50 |
class function TElevationGridNode.InternalCreateTriangles( |
| 51 |
const AXDimension, AZDimension: Integer; |
| 52 |
const AXSpacing, AZSpacing: Single; |
| 53 |
const AHeight: TSingleList; |
| 54 |
const ATexCoord: TAbstractTextureCoordinateNode; |
| 55 |
const TriangulationIgnoreHeights: Boolean): TIndexedTriangleSetNode; |
| 56 |
|
| 57 |
{ How to render TElevationGridNode? |
| 58 |
|
| 59 |
- Quad strip? (direct GL_QUAD_STRIP or proxy on T[Indexed]QuadSetNode)? |
| 60 |
|
| 61 |
Not good: |
| 62 |
|
| 63 |
- When colorPerVertex is different than normalPerVertex, |
| 64 |
neither flat nor smooth shading is good. |
| 65 |
|
| 66 |
- With direct GL_QUAD_STRIP, normal generation was a pain. |
| 67 |
We want to instead use proxy on something that already implements |
| 68 |
normal support, honoring creaseAngle. |
| 69 |
|
| 70 |
- Rendering by quads was sometimes not fully correct, as elevation grid |
| 71 |
quads were not really planar. So with GPU performing any triangulation |
| 72 |
it wants, results are undefined and not always the best. |
| 73 |
|
| 74 |
- By TIndexedFaceSetNode proxy? |
| 75 |
|
| 76 |
Mostly good, but it's not as straightforward to render by renderer |
| 77 |
as TIndexedTriangleSetNode. It needs to be converted to triangles. |
| 78 |
|
| 79 |
- By TIndexedTriangleSetNode proxy? |
| 80 |
|
| 81 |
Better than TIndexedFaceSetNode for renderer. |
| 82 |
|
| 83 |
We lose support for creaseAngle, but it's not a big deal. |
| 84 |
Our TCastleTerrainData wasn't using it anyway, always setting creaseAngle to 4. |
| 85 |
} |
| 86 |
var |
| 87 |
Coords: TVector3List; |
| 88 |
CoordIndexes: TInt32List; |
| 89 |
|
| 90 |
{ Add to CoordIndexes a quad from given indexes. |
| 91 |
Actually we add two triangles, looking at Coord to choose the best |
| 92 |
triangulation. } |
| 93 |
procedure Quad(const IndexNum, I1, I2, I3, I4: Integer); |
| 94 |
begin |
| 95 |
if TriangulationIgnoreHeights or |
| 96 |
( PointsDistanceSqr(Coords.L[I1], Coords.L[I3]) > |
| 97 |
PointsDistanceSqr(Coords.L[I2], Coords.L[I4]) ) then |
| 98 |
begin |
| 99 |
CoordIndexes.L[IndexNum ] := I1; |
| 100 |
CoordIndexes.L[IndexNum + 1] := I2; |
| 101 |
CoordIndexes.L[IndexNum + 2] := I4; |
| 102 |
CoordIndexes.L[IndexNum + 3] := I2; |
| 103 |
CoordIndexes.L[IndexNum + 4] := I3; |
| 104 |
CoordIndexes.L[IndexNum + 5] := I4; |
| 105 |
end else |
| 106 |
begin |
| 107 |
CoordIndexes.L[IndexNum ] := I1; |
| 108 |
CoordIndexes.L[IndexNum + 1] := I2; |
| 109 |
CoordIndexes.L[IndexNum + 2] := I3; |
| 110 |
CoordIndexes.L[IndexNum + 3] := I3; |
| 111 |
CoordIndexes.L[IndexNum + 4] := I4; |
| 112 |
CoordIndexes.L[IndexNum + 5] := I1; |
| 113 |
end; |
| 114 |
end; |
| 115 |
|
| 116 |
var |
| 117 |
I, J, NextIndex: Integer; |
| 118 |
CoordNode: TCoordinateNode; |
| 119 |
TexCoordsToGenerate: TVector2List; |
| 120 |
begin |
| 121 |
Result := TIndexedTriangleSetNode.Create; |
| 122 |
try |
| 123 |
if ClassIsNotEmpty( |
| 124 |
AXDimension, AZDimension, |
| 125 |
AXSpacing, AZSpacing, AHeight) then |
| 126 |
begin |
| 127 |
CoordNode := TCoordinateNode.Create; |
| 128 |
Result.Coord := CoordNode; |
| 129 |
Coords := CoordNode.FdPoint.Items; |
| 130 |
|
| 131 |
{ calculate TexCoordProvided, set Result.FdTexCoord, maybe set TexCoords } |
| 132 |
if ATexCoord <> nil then |
| 133 |
begin |
| 134 |
Result.FdTexCoord.Value := ATexCoord; |
| 135 |
TexCoordsToGenerate := nil; |
| 136 |
end else |
| 137 |
begin |
| 138 |
Result.FdTexCoord.Value := TTextureCoordinateNode.Create; |
| 139 |
TexCoordsToGenerate := TTextureCoordinateNode(Result.FdTexCoord.Value). |
| 140 |
FdPoint.Items; |
| 141 |
end; |
| 142 |
|
| 143 |
{ generate coords (and other per-vertex stuff: tex coords) } |
| 144 |
Coords.Count := AXDimension * AZDimension; |
| 145 |
if TexCoordsToGenerate <> nil then |
| 146 |
TexCoordsToGenerate.Count := AXDimension * AZDimension; |
| 147 |
for J := 0 to AZDimension - 1 do |
| 148 |
for I := 0 to AXDimension - 1 do |
| 149 |
begin |
| 150 |
Coords.L[I + J * AXDimension] := Vector3( |
| 151 |
AXSpacing * I, |
| 152 |
AHeight.L[I + J * AXDimension], |
| 153 |
AZSpacing * J); |
| 154 |
|
| 155 |
if TexCoordsToGenerate <> nil then |
| 156 |
TexCoordsToGenerate.L[I + J * AXDimension] := Vector2( |
| 157 |
I / (AXDimension - 1), |
| 158 |
J / (AZDimension - 1)); |
| 159 |
end; |
| 160 |
|
| 161 |
{ generate quads indexes } |
| 162 |
CoordIndexes := Result.CoordIndexField.Items; |
| 163 |
CoordIndexes.Count := (AXDimension - 1) * (AZDimension - 1) * 6; |
| 164 |
NextIndex := 0; |
| 165 |
for J := 1 to AZDimension - 1 do |
| 166 |
for I := 1 to AXDimension - 1 do |
| 167 |
begin |
| 168 |
{ Vertices are ordered such that face is CCW from up |
| 169 |
(i.e. looking from positive Y axis). } |
| 170 |
Quad(NextIndex, |
| 171 |
I + (J - 1) * AXDimension, |
| 172 |
I - 1 + (J - 1) * AXDimension, |
| 173 |
I - 1 + J * AXDimension, |
| 174 |
I + J * AXDimension); |
| 175 |
NextIndex := NextIndex + 6; |
| 176 |
end; |
| 177 |
Assert(NextIndex = CoordIndexes.Count); |
| 178 |
end; |
| 179 |
|
| 180 |
except FreeAndNil(Result); raise end; |
| 181 |
end; |
| 182 |
|