| 1 |
{%MainUnit x3dnodes.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-2023 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 |
{ Make a sphere. } |
| 18 |
procedure Sphere_Proxy(CoordIndex: TInt32List; |
| 19 |
Coord: TVector3List; Normal: TVector3List; |
| 20 |
TexCoord: TVector2List; |
| 21 |
const Radius: Single; |
| 22 |
const QuadricSlices, QuadricStacks: Cardinal); |
| 23 |
|
| 24 |
{ We have rings numbered 0 ... QuadricStacks. |
| 25 |
The rings nr 0 and QuadricStacks have 0 radius (bottom and top of the sphere). } |
| 26 |
|
| 27 |
{ For stack number calculate it's radius and height (in y). } |
| 28 |
procedure StackCircle(StackNum: integer; out StackRadius, Height: Single); |
| 29 |
var |
| 30 |
Alpha, S, C: Single; |
| 31 |
begin |
| 32 |
{ Calculate correct StackRadius and Height for sphere of radius 1, |
| 33 |
and then scale by Radius. } |
| 34 |
|
| 35 |
{ Divide the vertical ring into QuadricStacks equal pieces. |
| 36 |
This is much better idea than simply dividing along the Height, |
| 37 |
because it allows the top/bottom of the sphere to look much better. |
| 38 |
This is also what OpenGL quadrics do. |
| 39 |
You can compare by using below |
| 40 |
|
| 41 |
StackRadius := Sqrt( 1 - Sqr(1 - 2*StackNum/QuadricStacks) ) * Radius; |
| 42 |
Height := (-1 + StackNum * 2/QuadricStacks) * Radius; |
| 43 |
} |
| 44 |
Alpha := Pi * StackNum / QuadricStacks; |
| 45 |
SinCos(Alpha, S, C); |
| 46 |
StackRadius := S * Radius; |
| 47 |
Height := -C * Radius; |
| 48 |
end; |
| 49 |
|
| 50 |
var |
| 51 |
StackRadius, StackHeight: Single; |
| 52 |
StackFaceIndex, BottomIndex, TopIndex, BottomCircleIndex, CircleIndex, |
| 53 |
StackNum, I: Integer; |
| 54 |
begin |
| 55 |
{ For VRML 1.0, some of these MF fields by default have non-empty content. |
| 56 |
It's safest to just clean them. } |
| 57 |
CoordIndex.Count := 0; |
| 58 |
Coord.Count := 0; |
| 59 |
Normal.Count := 0; |
| 60 |
if TexCoord <> nil then TexCoord.Count := 0; |
| 61 |
|
| 62 |
{ We generate QuadricSlices + 1 points, not just QuadricSlices for each stack. |
| 63 |
Reason: texture coordinates must be different at the last point |
| 64 |
(1.0) than the first (0.0). |
| 65 |
Otherwise texture seam would not be correctly closed. } |
| 66 |
|
| 67 |
{ Even for the bottom/top point, we create many points, because |
| 68 |
they need different tex coords. } |
| 69 |
BottomIndex := Coord.Count; |
| 70 |
Coord.Count := Coord.Count + QuadricSlices + 1; |
| 71 |
Normal.Count := Normal.Count + QuadricSlices + 1; |
| 72 |
for I := 0 to QuadricSlices do |
| 73 |
begin |
| 74 |
Coord.L[BottomIndex + I] := Vector3(0, -Radius, 0); |
| 75 |
Normal.L[BottomIndex + I] := Vector3(0, -1, 0); |
| 76 |
end; |
| 77 |
if TexCoord <> nil then |
| 78 |
begin |
| 79 |
TexCoord.Count := TexCoord.Count + QuadricSlices + 1; |
| 80 |
for I := 0 to QuadricSlices do |
| 81 |
TexCoord.L[BottomIndex + I] := Vector2(I / QuadricSlices, 0); |
| 82 |
end; |
| 83 |
|
| 84 |
BottomCircleIndex := Coord.Count; |
| 85 |
Coord.Count := Coord.Count + QuadricSlices + 1; |
| 86 |
StackCircle(1, StackRadius, StackHeight); |
| 87 |
GenerateCircle(QuadricSlices, StackRadius, StackHeight, PVector3(Coord.Ptr(BottomCircleIndex))); |
| 88 |
Coord[Coord.Count - 1] := Coord.L[BottomCircleIndex]; |
| 89 |
Normal.Count := Normal.Count + QuadricSlices + 1; |
| 90 |
for I := 0 to QuadricSlices - 1 do |
| 91 |
Normal.L[BottomCircleIndex + I] := (Coord.L[BottomCircleIndex + I]).Normalize; |
| 92 |
Normal[Normal.Count - 1] := Normal.L[BottomCircleIndex]; |
| 93 |
if TexCoord <> nil then |
| 94 |
begin |
| 95 |
TexCoord.Count := TexCoord.Count + QuadricSlices + 1; |
| 96 |
for I := 0 to QuadricSlices do |
| 97 |
TexCoord.L[BottomCircleIndex + I] := Vector2(I / QuadricSlices, 1 / QuadricStacks); |
| 98 |
end; |
| 99 |
|
| 100 |
{ Bottom stack, from triangles } |
| 101 |
StackFaceIndex := CoordIndex.Count; |
| 102 |
CoordIndex.Count := CoordIndex.Count + QuadricSlices * 4; |
| 103 |
for I := 0 to QuadricSlices - 1 do |
| 104 |
begin |
| 105 |
CoordIndex.L[StackFaceIndex + I * 4 ] := BottomCircleIndex + I + 1; |
| 106 |
CoordIndex.L[StackFaceIndex + I * 4 + 1] := BottomCircleIndex + I; |
| 107 |
CoordIndex.L[StackFaceIndex + I * 4 + 2] := BottomIndex + I; |
| 108 |
CoordIndex.L[StackFaceIndex + I * 4 + 3] := -1; |
| 109 |
end; |
| 110 |
|
| 111 |
for StackNum := 2 to QuadricStacks - 1 do |
| 112 |
begin |
| 113 |
StackCircle(StackNum, StackRadius, StackHeight); |
| 114 |
|
| 115 |
CircleIndex := Coord.Count; |
| 116 |
Coord.Count := Coord.Count + QuadricSlices + 1; |
| 117 |
StackCircle(StackNum, StackRadius, StackHeight); |
| 118 |
GenerateCircle(QuadricSlices, StackRadius, StackHeight, PVector3(Coord.Ptr(CircleIndex))); |
| 119 |
Coord[Coord.Count - 1] := Coord.L[CircleIndex]; |
| 120 |
Normal.Count := Normal.Count + QuadricSlices + 1; |
| 121 |
for I := 0 to QuadricSlices - 1 do |
| 122 |
Normal.L[CircleIndex + I] := (Coord.L[CircleIndex + I]).Normalize; |
| 123 |
Normal[Normal.Count - 1] := Normal.L[CircleIndex]; |
| 124 |
if TexCoord <> nil then |
| 125 |
begin |
| 126 |
TexCoord.Count := TexCoord.Count + QuadricSlices + 1; |
| 127 |
for I := 0 to QuadricSlices do |
| 128 |
TexCoord.L[CircleIndex + I] := Vector2(I / QuadricSlices, StackNum / QuadricStacks); |
| 129 |
end; |
| 130 |
|
| 131 |
{ Next stack, from quads } |
| 132 |
StackFaceIndex := CoordIndex.Count; |
| 133 |
CoordIndex.Count := CoordIndex.Count + QuadricSlices * 5; |
| 134 |
for I := 0 to QuadricSlices - 1 do |
| 135 |
begin |
| 136 |
CoordIndex.L[StackFaceIndex + I * 5 ] := BottomCircleIndex + I; |
| 137 |
CoordIndex.L[StackFaceIndex + I * 5 + 1] := BottomCircleIndex + I + 1; |
| 138 |
CoordIndex.L[StackFaceIndex + I * 5 + 2] := CircleIndex + I + 1; |
| 139 |
CoordIndex.L[StackFaceIndex + I * 5 + 3] := CircleIndex + I; |
| 140 |
CoordIndex.L[StackFaceIndex + I * 5 + 4] := -1; |
| 141 |
end; |
| 142 |
|
| 143 |
BottomCircleIndex := CircleIndex; |
| 144 |
end; |
| 145 |
|
| 146 |
TopIndex := Coord.Count; |
| 147 |
Coord.Count := Coord.Count + QuadricSlices + 1; |
| 148 |
Normal.Count := Normal.Count + QuadricSlices + 1; |
| 149 |
for I := 0 to QuadricSlices do |
| 150 |
begin |
| 151 |
Coord.L[TopIndex + I] := Vector3(0, Radius, 0); |
| 152 |
Normal.L[TopIndex + I] := Vector3(0, 1, 0); |
| 153 |
end; |
| 154 |
if TexCoord <> nil then |
| 155 |
begin |
| 156 |
TexCoord.Count := TexCoord.Count + QuadricSlices + 1; |
| 157 |
for I := 0 to QuadricSlices do |
| 158 |
TexCoord.L[TopIndex + I] := Vector2(I / QuadricSlices, 1); |
| 159 |
end; |
| 160 |
|
| 161 |
{ Top stack, from triangles } |
| 162 |
StackFaceIndex := CoordIndex.Count; |
| 163 |
CoordIndex.Count := CoordIndex.Count + QuadricSlices * 4; |
| 164 |
for I := 0 to QuadricSlices - 1 do |
| 165 |
begin |
| 166 |
CoordIndex.L[StackFaceIndex + I * 4 ] := BottomCircleIndex + I; |
| 167 |
CoordIndex.L[StackFaceIndex + I * 4 + 1] := BottomCircleIndex + I + 1; |
| 168 |
CoordIndex.L[StackFaceIndex + I * 4 + 2] := TopIndex + I; |
| 169 |
CoordIndex.L[StackFaceIndex + I * 4 + 3] := -1; |
| 170 |
end; |
| 171 |
end; |
| 172 |
|
| 173 |
{ Make a wireframe sphere. } |
| 174 |
procedure SphereWireframe_Proxy(CoordIndex: TInt32List; |
| 175 |
Coord: TVector3List; const Radius: Single; |
| 176 |
const QuadricSlices, QuadricStacks: Cardinal); |
| 177 |
|
| 178 |
{ We have rings numbered 0 ... QuadricStacks. |
| 179 |
The rings nr 0 and QuadricStacks have 0 radius (bottom and top of the sphere). } |
| 180 |
|
| 181 |
{ For stack number calculate it's radius and height (in y). } |
| 182 |
procedure StackCircle(StackNum: integer; out StackRadius, Height: Single); |
| 183 |
var |
| 184 |
Alpha, S, C: Single; |
| 185 |
begin |
| 186 |
{ Calculate correct StackRadius and Height for sphere of radius 1, |
| 187 |
and then scale by Radius. } |
| 188 |
|
| 189 |
{ Divide the vertical ring into QuadricStacks equal pieces. |
| 190 |
This is much better idea than simply dividing along the Height, |
| 191 |
because it allows the top/bottom of the sphere to look much better. |
| 192 |
This is also what OpenGL quadrics do. |
| 193 |
You can compare by using below |
| 194 |
|
| 195 |
StackRadius := Sqrt( 1 - Sqr(1 - 2*StackNum/QuadricStacks) ) * Radius; |
| 196 |
Height := (-1 + StackNum * 2/QuadricStacks) * Radius; |
| 197 |
} |
| 198 |
Alpha := Pi * StackNum / QuadricStacks; |
| 199 |
SinCos(Alpha, S, C); |
| 200 |
StackRadius := S * Radius; |
| 201 |
Height := -C * Radius; |
| 202 |
end; |
| 203 |
|
| 204 |
var |
| 205 |
StackRadius, StackHeight: Single; |
| 206 |
StackFaceIndex, BottomIndex, TopIndex, BottomCircleIndex, CircleIndex, |
| 207 |
StackNum, I, NextI: Integer; |
| 208 |
begin |
| 209 |
CoordIndex.Count := 0; |
| 210 |
Coord.Count := 0; |
| 211 |
|
| 212 |
BottomIndex := Coord.Count; |
| 213 |
Coord.Count := Coord.Count + 1; |
| 214 |
Coord.L[BottomIndex] := Vector3(0, -Radius, 0); |
| 215 |
|
| 216 |
BottomCircleIndex := Coord.Count; |
| 217 |
Coord.Count := Coord.Count + QuadricSlices; |
| 218 |
StackCircle(1, StackRadius, StackHeight); |
| 219 |
GenerateCircle(QuadricSlices, StackRadius, StackHeight, PVector3(Coord.Ptr(BottomCircleIndex))); |
| 220 |
|
| 221 |
{ Bottom stack, from triangles } |
| 222 |
StackFaceIndex := CoordIndex.Count; |
| 223 |
CoordIndex.Count := CoordIndex.Count + QuadricSlices * 4; |
| 224 |
for I := 0 to QuadricSlices - 1 do |
| 225 |
begin |
| 226 |
NextI := (I + 1) mod QuadricSlices; |
| 227 |
CoordIndex.L[StackFaceIndex + I * 4 ] := BottomCircleIndex + NextI; |
| 228 |
CoordIndex.L[StackFaceIndex + I * 4 + 1] := BottomCircleIndex + I; |
| 229 |
CoordIndex.L[StackFaceIndex + I * 4 + 2] := BottomIndex; |
| 230 |
CoordIndex.L[StackFaceIndex + I * 4 + 3] := -1; |
| 231 |
end; |
| 232 |
|
| 233 |
for StackNum := 2 to QuadricStacks - 1 do |
| 234 |
begin |
| 235 |
StackCircle(StackNum, StackRadius, StackHeight); |
| 236 |
|
| 237 |
CircleIndex := Coord.Count; |
| 238 |
Coord.Count := Coord.Count + QuadricSlices; |
| 239 |
StackCircle(StackNum, StackRadius, StackHeight); |
| 240 |
GenerateCircle(QuadricSlices, StackRadius, StackHeight, PVector3(Coord.Ptr(CircleIndex))); |
| 241 |
|
| 242 |
{ Next stack, from quads (but we only need to add 3 lines) } |
| 243 |
StackFaceIndex := CoordIndex.Count; |
| 244 |
CoordIndex.Count := CoordIndex.Count + QuadricSlices * 5; |
| 245 |
for I := 0 to QuadricSlices - 1 do |
| 246 |
begin |
| 247 |
NextI := (I + 1) mod QuadricSlices; |
| 248 |
CoordIndex.L[StackFaceIndex + I * 5 ] := BottomCircleIndex + NextI; |
| 249 |
CoordIndex.L[StackFaceIndex + I * 5 + 1] := CircleIndex + NextI; |
| 250 |
CoordIndex.L[StackFaceIndex + I * 5 + 2] := CircleIndex + I; |
| 251 |
CoordIndex.L[StackFaceIndex + I * 5 + 3] := BottomCircleIndex + I; |
| 252 |
CoordIndex.L[StackFaceIndex + I * 5 + 4] := -1; |
| 253 |
end; |
| 254 |
|
| 255 |
BottomCircleIndex := CircleIndex; |
| 256 |
end; |
| 257 |
|
| 258 |
TopIndex := Coord.Count; |
| 259 |
Coord.Count := Coord.Count + 1; |
| 260 |
Coord.L[TopIndex] := Vector3(0, Radius, 0); |
| 261 |
|
| 262 |
{ Top stack, from triangles } |
| 263 |
StackFaceIndex := CoordIndex.Count; |
| 264 |
CoordIndex.Count := CoordIndex.Count + QuadricSlices * 4; |
| 265 |
for I := 0 to QuadricSlices - 1 do |
| 266 |
begin |
| 267 |
NextI := (I + 1) mod QuadricSlices; |
| 268 |
CoordIndex.L[StackFaceIndex + I * 4 ] := BottomCircleIndex + I; |
| 269 |
CoordIndex.L[StackFaceIndex + I * 4 + 1] := BottomCircleIndex + NextI; |
| 270 |
CoordIndex.L[StackFaceIndex + I * 4 + 2] := TopIndex; |
| 271 |
CoordIndex.L[StackFaceIndex + I * 4 + 3] := -1; |
| 272 |
end; |
| 273 |
end; |
| 274 |
|
| 275 |
function TSphereNode.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; |
| 276 |
|
| 277 |
procedure SphereWireframe; |
| 278 |
var |
| 279 |
CoordNode: TCoordinateNode; |
| 280 |
ILS: TIndexedLineSetNode absolute Result; |
| 281 |
begin |
| 282 |
ILS := TIndexedLineSetNode.Create(X3DName, BaseUrl); |
| 283 |
try |
| 284 |
CoordNode := TCoordinateNode.Create('', BaseUrl); |
| 285 |
ILS.FdCoord.Value := CoordNode; |
| 286 |
|
| 287 |
SphereWireframe_Proxy(ILS.FdCoordIndex.Items, CoordNode.FdPoint.Items, |
| 288 |
FdRadius.Value, CalculateSlices, CalculateStacks); |
| 289 |
except FreeAndNil(Result); raise end; |
| 290 |
end; |
| 291 |
|
| 292 |
procedure SphereFilled; |
| 293 |
var |
| 294 |
CoordNode: TCoordinateNode; |
| 295 |
NormalNode: TNormalNode; |
| 296 |
TexCoordNode: TTextureCoordinateNode; |
| 297 |
TexCoords: TVector2List; |
| 298 |
IFS: TIndexedFaceSetNode absolute Result; |
| 299 |
begin |
| 300 |
IFS := TIndexedFaceSetNode.Create(X3DName, BaseUrl); |
| 301 |
try |
| 302 |
CoordNode := TCoordinateNode.Create('', BaseUrl); |
| 303 |
IFS.FdCoord.Value := CoordNode; |
| 304 |
|
| 305 |
NormalNode := TNormalNode.Create('', BaseUrl); |
| 306 |
IFS.FdNormal.Value := NormalNode; |
| 307 |
IFS.FdNormalPerVertex.Value := true; |
| 308 |
|
| 309 |
if (FdTexCoord.Value <> nil) and FdTexCoord.CurrentChildAllowed then |
| 310 |
begin |
| 311 |
{ No need for CylinderCone_Proxy to create tex coords. } |
| 312 |
IFS.FdTexCoord.Value := FdTexCoord.Value; |
| 313 |
TexCoords := nil; |
| 314 |
end else |
| 315 |
begin |
| 316 |
TexCoordNode := TTextureCoordinateNode.Create('', BaseUrl); |
| 317 |
IFS.FdTexCoord.Value := TexCoordNode; |
| 318 |
TexCoords := TexCoordNode.FdPoint.Items; |
| 319 |
end; |
| 320 |
|
| 321 |
Sphere_Proxy(IFS.FdCoordIndex.Items, |
| 322 |
CoordNode.FdPoint.Items, NormalNode.FdVector.Items, TexCoords, |
| 323 |
FdRadius.Value, |
| 324 |
CalculateSlices, CalculateStacks); |
| 325 |
|
| 326 |
IFS.FdSolid.Value := FdSolid.Value; |
| 327 |
|
| 328 |
{ Smooth everything. } |
| 329 |
IFS.FdCreaseAngle.Value := 4; |
| 330 |
except FreeAndNil(Result); raise end; |
| 331 |
end; |
| 332 |
|
| 333 |
begin |
| 334 |
if (State.ShapeNode <> nil) and |
| 335 |
(State.ShapeNode.Shading = shWireframe) then |
| 336 |
SphereWireframe |
| 337 |
else |
| 338 |
SphereFilled; |
| 339 |
end; |
| 340 |
|
| 341 |
{$warnings off} // using deprecated to keep supporting VRML 1 |
| 342 |
function TSphereNode_1.Proxy(var State: TX3DGraphTraverseState): TAbstractGeometryNode; |
| 343 |
var |
| 344 |
CoordNode: TCoordinate3Node_1; |
| 345 |
NormalNode: TNormalNode; |
| 346 |
NormalBinding: TNormalBindingNode_1; |
| 347 |
TexCoordNode: TTextureCoordinate2Node_1; |
| 348 |
ShapeHints: TShapeHintsNode_1; |
| 349 |
MaterialBinding: TMaterialBindingNode_1; |
| 350 |
IFS: TIndexedFaceSetNode_1 absolute Result; |
| 351 |
begin |
| 352 |
IFS := TIndexedFaceSetNode_1.Create(X3DName, BaseUrl); |
| 353 |
try |
| 354 |
{ we have to modify State, so make a copy of it } |
| 355 |
State := TX3DGraphTraverseState.CreateCopy(State); |
| 356 |
|
| 357 |
CoordNode := TCoordinate3Node_1.Create('', BaseUrl); |
| 358 |
State.VRML1State.SetOwnNode(vsCoordinate3, CoordNode); |
| 359 |
|
| 360 |
NormalNode := TNormalNode.Create('', BaseUrl); |
| 361 |
State.VRML1State.SetOwnNode(vsNormal, NormalNode); |
| 362 |
|
| 363 |
NormalBinding := TNormalBindingNode_1.Create('', BaseUrl); |
| 364 |
{ NormalBinding.value = PER_VERTEX means we use niPerVertexCoordIndexed, |
| 365 |
so coordIndex chooses the normal. } |
| 366 |
NormalBinding.FdValue.Value := BIND_PER_VERTEX; |
| 367 |
State.VRML1State.SetOwnNode(vsNormalBinding, NormalBinding); |
| 368 |
|
| 369 |
TexCoordNode := TTextureCoordinate2Node_1.Create('', BaseUrl); |
| 370 |
State.VRML1State.SetOwnNode(vsTextureCoordinate2, TexCoordNode); |
| 371 |
|
| 372 |
ShapeHints := TShapeHintsNode_1.Create('', BaseUrl); |
| 373 |
{ For VRML 1.0, Sphere is never solid. } |
| 374 |
ShapeHints.FdshapeType.Value := SHTYPE_UNKNOWN; |
| 375 |
ShapeHints.FdvertexOrdering.Value := VERTORDER_COUNTERCLOCKWISE; |
| 376 |
{ Smooth everything. Not really needed, we use explicit normal node now. } |
| 377 |
ShapeHints.FdCreaseAngle.Value := 4; |
| 378 |
State.VRML1State.SetOwnNode(vsShapeHints, ShapeHints); |
| 379 |
|
| 380 |
{ calculate MaterialBinding. Sphere has always only 1 material. } |
| 381 |
MaterialBinding := TMaterialBindingNode_1.Create('', BaseUrl); |
| 382 |
MaterialBinding.FdValue.Value := BIND_OVERALL; |
| 383 |
State.VRML1State.SetOwnNode(vsMaterialBinding, MaterialBinding); |
| 384 |
|
| 385 |
Sphere_Proxy(IFS.FdCoordIndex.Items, |
| 386 |
CoordNode.FdPoint.Items, NormalNode.FdVector.Items, |
| 387 |
TexCoordNode.FdPoint.Items, |
| 388 |
FdRadius.Value, |
| 389 |
DefaultTriangulationSlices, DefaultTriangulationStacks); |
| 390 |
|
| 391 |
{ For VRML 1.0, unfortunately textureCoordIndex must be set |
| 392 |
(even though it's exactly equivalent to coordIndex). |
| 393 |
This is a problem of VRML 1.0 "state" idea --- there is no |
| 394 |
other way to "turn off" texture than to just use empty textureCoordIndex. } |
| 395 |
IFS.FdTextureCoordIndex.Items.Assign(IFS.FdCoordIndex.Items); |
| 396 |
except FreeAndNil(Result); raise end; |
| 397 |
end; |
| 398 |
{$warnings on} |