| 1 |
{ |
| 2 |
Copyright 2002-2024 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 |
{ Rendering background (TBackgroundRenderer) based on X3D background node. } |
| 17 |
unit CastleInternalBackgroundRenderer; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses CastleVectors, SysUtils, CastleUtils, CastleImages, X3DNodes, |
| 24 |
CastleColors, CastleGLUtils, CastleTransform, CastleRectangles, CastleProjection, |
| 25 |
CastleInternalShapesRenderer, CastleInternalRenderer; |
| 26 |
|
| 27 |
type |
| 28 |
{ Background under X3D scene. |
| 29 |
|
| 30 |
It supports all features of X3D nodes descending |
| 31 |
from TAbstractBackgroundNode. |
| 32 |
|
| 33 |
@unorderedList( |
| 34 |
@itemSpacing Compact |
| 35 |
@item(May be a skybox: A cube with each face potentially textured |
| 36 |
(textures may have alpha channel),) |
| 37 |
@item(Behind the skybox: you can see ground sphere, |
| 38 |
with color rings for ground colors,) |
| 39 |
@item(Behind the ground sphere: you can see sky sphere, |
| 40 |
with color rings for sky colors,) |
| 41 |
@item(May also be a single full-screen quad (for TImageBackgroundNode).) |
| 42 |
) |
| 43 |
|
| 44 |
This is an abstract class. |
| 45 |
Do not construct it explicitly. |
| 46 |
The only way to create non-abstract instances of this class |
| 47 |
is to use @link(CreateBackgroundRenderer). |
| 48 |
} |
| 49 |
TBackgroundRenderer = class abstract |
| 50 |
private |
| 51 |
{ Calculate the sky sphere radius that fits nicely in given projection near/far. |
| 52 |
|
| 53 |
Background spheres (for sky and ground) are rendered at given radius. |
| 54 |
And inside these spheres, we have a cube (to apply background textures). |
| 55 |
Both spheres and cube must fit nicely within your projection near/far |
| 56 |
to avoid any artifacts. } |
| 57 |
class function NearFarToSkySphereRadius(const zNear, zFar: Single): Single; |
| 58 |
public |
| 59 |
procedure Render(const RenderingCamera: TRenderingCamera; |
| 60 |
const Wireframe: boolean; |
| 61 |
const RenderRect: TFloatRectangle; |
| 62 |
const CurrentProjection: TProjection; |
| 63 |
const ShapesCollector: TShapesCollector; |
| 64 |
const ShapesRenderer: TShapesRenderer); virtual; |
| 65 |
{ Change rotation. |
| 66 |
Initially rotation is taken from TBackgroundNode.TransformRotation |
| 67 |
(corresponds to the rotation of Background in the X3D file transformation). } |
| 68 |
procedure UpdateRotation(const Rotation: TVector4); virtual; |
| 69 |
procedure FreeResources; virtual; |
| 70 |
end; |
| 71 |
|
| 72 |
{ Create background renderer. |
| 73 |
Returns @nil if this Node class is not supported. } |
| 74 |
function CreateBackgroundRenderer(const Node: TAbstractBackgroundNode): TBackgroundRenderer; |
| 75 |
|
| 76 |
implementation |
| 77 |
|
| 78 |
uses Math, |
| 79 |
CastleLog, CastleScene, X3DFields, CastleSceneCore, CastleGLImages, |
| 80 |
CastleRenderContext, CastleRenderOptions, CastleInternalGLUtils; |
| 81 |
|
| 82 |
const |
| 83 |
{ Background rendering doesn't use the same projection near/far as other content. |
| 84 |
It is not necessary (background doesn't check depth at all, it has DepthTest=false) |
| 85 |
and it makes things simpler (we can assume some box/sphere sizes |
| 86 |
for background, we don't need to make them dependent on current projection near/far). } |
| 87 |
BgProjectionNear = 1; |
| 88 |
BgProjectionFar = 100; |
| 89 |
|
| 90 |
{ Relation of a cube size and a radius of it's bounding sphere. |
| 91 |
|
| 92 |
Sphere surrounds the cube, such that 6 cube corners touch the sphere. |
| 93 |
So cube diameter = 2 * sphere radius. |
| 94 |
Cube diameter = sqrt(sqr(cube size) + sqr(cube face diameter)), |
| 95 |
and cube face diameter = sqrt(2) * cube size. |
| 96 |
This gives constants below. } |
| 97 |
SphereRadiusToCubeSize = 2 / {$ifdef FPC}Sqrt(3){$else}1.732050807568877{$endif}; |
| 98 |
CubeSizeToSphereRadius = {$ifdef FPC}Sqrt(3){$else}1.732050807568877{$endif} / 2; |
| 99 |
|
| 100 |
{ TBackgroundRenderer ------------------------------------------------------------ } |
| 101 |
|
| 102 |
class function TBackgroundRenderer.NearFarToSkySphereRadius(const zNear, zFar: Single): Single; |
| 103 |
|
| 104 |
{ Conditions are ZNear < CubeSize/2, ZFar > SphereRadius. |
| 105 |
So conditions for radius are |
| 106 |
|
| 107 |
ZNear * 2 * CubeSizeToSphereRadius < SphereRadius < ZFar |
| 108 |
|
| 109 |
Note that 2 * CubeSizeToSphereRadius is Sqrt(3) =~ 1.7, |
| 110 |
so it's possible to choose |
| 111 |
ZNear <= ZFar that still yield no possible radius. |
| 112 |
} |
| 113 |
|
| 114 |
var |
| 115 |
Min, Max: Single; |
| 116 |
begin |
| 117 |
Min := zNear * 2 * CubeSizeToSphereRadius; |
| 118 |
Max := zFar; |
| 119 |
Result := (Min + Max) / 2; |
| 120 |
end; |
| 121 |
|
| 122 |
procedure TBackgroundRenderer.Render(const RenderingCamera: TRenderingCamera; |
| 123 |
const Wireframe: boolean; |
| 124 |
const RenderRect: TFloatRectangle; |
| 125 |
const CurrentProjection: TProjection; |
| 126 |
const ShapesCollector: TShapesCollector; |
| 127 |
const ShapesRenderer: TShapesRenderer); |
| 128 |
begin |
| 129 |
end; |
| 130 |
|
| 131 |
procedure TBackgroundRenderer.UpdateRotation(const Rotation: TVector4); |
| 132 |
begin |
| 133 |
end; |
| 134 |
|
| 135 |
procedure TBackgroundRenderer.FreeResources; |
| 136 |
begin |
| 137 |
end; |
| 138 |
|
| 139 |
{ TBackgroundScene ----------------------------------------------------------- } |
| 140 |
|
| 141 |
type |
| 142 |
{ Background implementation using internal TCastleScene to render. |
| 143 |
In overridden constructor, load Scene contents. |
| 144 |
You can also use ClearColor and UseClearColor if the background |
| 145 |
may be realized by simple clearing of the viewport with solid color. } |
| 146 |
TBackgroundScene = class(TBackgroundRenderer) |
| 147 |
protected |
| 148 |
Scene: TCastleScene; |
| 149 |
Params: TBasicRenderParams; |
| 150 |
ClearColor: TCastleColor; |
| 151 |
UseClearColor: Boolean; |
| 152 |
public |
| 153 |
constructor Create; |
| 154 |
destructor Destroy; override; |
| 155 |
procedure Render(const RenderingCamera: TRenderingCamera; |
| 156 |
const Wireframe: boolean; |
| 157 |
const RenderRect: TFloatRectangle; |
| 158 |
const CurrentProjection: TProjection; |
| 159 |
const ShapesCollector: TShapesCollector; |
| 160 |
const ShapesRenderer: TShapesRenderer); override; |
| 161 |
procedure FreeResources; override; |
| 162 |
end; |
| 163 |
|
| 164 |
constructor TBackgroundScene.Create; |
| 165 |
begin |
| 166 |
inherited; |
| 167 |
|
| 168 |
Scene := TCastleScene.Create(nil); |
| 169 |
{ We don't need depth test (we put our shapes in proper order), |
| 170 |
we even don't want it (because we don't clear depth buffer |
| 171 |
before drawing, so it may contain the depths on 3D world rendered |
| 172 |
in previous frame). } |
| 173 |
Scene.RenderOptions.DepthTest := false; |
| 174 |
{ We may share some nodes with the main scene. |
| 175 |
|
| 176 |
And both scenes must be associated with nodes, |
| 177 |
so we cannot use hacks (like no longer available Static |
| 178 |
or InternalNodesReadOnly). Because we will change them, |
| 179 |
e.g. in TBackground3D.UpdateRotation or TBackground2D.Render (UpdateProperties). |
| 180 |
|
| 181 |
So, for now, just hide the warning about |
| 182 |
"You cannot use the same X3D node in multiple instances of TCastleScene". |
| 183 |
Testcase: demo-models/background/ with ImageBackground or TextureBackground } |
| 184 |
Scene.InternalNodeSharing := true; |
| 185 |
Params := TBasicRenderParams.Create; |
| 186 |
end; |
| 187 |
|
| 188 |
destructor TBackgroundScene.Destroy; |
| 189 |
begin |
| 190 |
FreeAndNil(Scene); |
| 191 |
FreeAndNil(Params); |
| 192 |
inherited; |
| 193 |
end; |
| 194 |
|
| 195 |
procedure TBackgroundScene.Render(const RenderingCamera: TRenderingCamera; |
| 196 |
const Wireframe: boolean; const RenderRect: TFloatRectangle; |
| 197 |
const CurrentProjection: TProjection; |
| 198 |
const ShapesCollector: TShapesCollector; |
| 199 |
const ShapesRenderer: TShapesRenderer); |
| 200 |
var |
| 201 |
SavedOcclusionCulling: Boolean; |
| 202 |
SavedBlendingSort, SavedOcclusionSort: TShapeSort; |
| 203 |
begin |
| 204 |
inherited; |
| 205 |
|
| 206 |
Params.InShadow := false; |
| 207 |
Params.ShadowVolumesReceivers := [false, true]; |
| 208 |
Params.RenderingCamera := RenderingCamera; |
| 209 |
Params.Collector := ShapesCollector; |
| 210 |
Params.RendererToPrepareShapes := ShapesRenderer.Renderer; |
| 211 |
|
| 212 |
if Wireframe then |
| 213 |
Scene.RenderOptions.WireframeEffect := weWireframeOnly |
| 214 |
else |
| 215 |
Scene.RenderOptions.WireframeEffect := weNormal; |
| 216 |
|
| 217 |
if UseClearColor then |
| 218 |
RenderContext.Clear([cbColor], ClearColor); |
| 219 |
|
| 220 |
{ We don't calculate correct Params.Frustum (accounting for the fact that camera |
| 221 |
is rotated but never shifted during 3D background rendering) now. |
| 222 |
But also frustum culling for this would not be very useful, |
| 223 |
so just disable it by leaving Params.Frustum = nil. } |
| 224 |
//Params.Frustum := nil; // this is actually the default, we never set Params.Frustum |
| 225 |
Assert(Params.Frustum = nil); |
| 226 |
|
| 227 |
ShapesCollector.Clear; |
| 228 |
|
| 229 |
{ Scene.Render in this case should call Scene.LocalRender straight away, |
| 230 |
as Scene has no transformation. |
| 231 |
And that's good, the TCastleTransform.Render could not handle any transformation |
| 232 |
as Params.Frustum is nil. } |
| 233 |
Params.Transparent := false; Scene.Render(Params); |
| 234 |
Params.Transparent := true ; Scene.Render(Params); |
| 235 |
|
| 236 |
{ Disable occlusion culling on background. |
| 237 |
It works correctly... but the 1-frame delay is too noticeable, |
| 238 |
testcase: examples/fps_game/ . } |
| 239 |
SavedOcclusionCulling := ShapesRenderer.OcclusionCulling; |
| 240 |
ShapesRenderer.OcclusionCulling := false; |
| 241 |
|
| 242 |
SavedBlendingSort := ShapesRenderer.BlendingSort; |
| 243 |
ShapesRenderer.BlendingSort := sortNone; |
| 244 |
|
| 245 |
SavedOcclusionSort := ShapesRenderer.OcclusionSort; |
| 246 |
ShapesRenderer.OcclusionSort := sortNone; |
| 247 |
|
| 248 |
ShapesRenderer.Render(ShapesCollector, Params); |
| 249 |
|
| 250 |
ShapesRenderer.OcclusionCulling := SavedOcclusionCulling; |
| 251 |
ShapesRenderer.BlendingSort := SavedBlendingSort; |
| 252 |
ShapesRenderer.OcclusionSort := SavedOcclusionSort; |
| 253 |
end; |
| 254 |
|
| 255 |
procedure TBackgroundScene.FreeResources; |
| 256 |
begin |
| 257 |
inherited; |
| 258 |
Scene.FreeResources([frTextureDataInNodes]); |
| 259 |
end; |
| 260 |
|
| 261 |
{ TBackground3D --------------------------------------------------------------- } |
| 262 |
|
| 263 |
type |
| 264 |
TBackground3D = class(TBackgroundScene) |
| 265 |
strict private |
| 266 |
Transform: TTransformNode; |
| 267 |
public |
| 268 |
constructor Create(const Node: TAbstract3DBackgroundNode); |
| 269 |
procedure UpdateRotation(const Rotation: TVector4); override; |
| 270 |
procedure Render(const RenderingCamera: TRenderingCamera; |
| 271 |
const Wireframe: boolean; |
| 272 |
const RenderRect: TFloatRectangle; |
| 273 |
const CurrentProjection: TProjection; |
| 274 |
const ShapesCollector: TShapesCollector; |
| 275 |
const ShapesRenderer: TShapesRenderer); override; |
| 276 |
end; |
| 277 |
|
| 278 |
constructor TBackground3D.Create( |
| 279 |
const Node: TAbstract3DBackgroundNode); |
| 280 |
var |
| 281 |
SkySphereRadius: Single; |
| 282 |
|
| 283 |
procedure RenderCubeSides; |
| 284 |
var |
| 285 |
CubeSize, CubeSize2: Single; |
| 286 |
|
| 287 |
procedure RenderTextureSide(const Side: TBackgroundSide); |
| 288 |
const |
| 289 |
Coords: array [TBackgroundSide, 0..3] of TVector3 = |
| 290 |
( ((X: 1; Y: -1; Z: 1), (X:-1; Y: -1; Z: 1), (X:-1; Y: 1; Z: 1), (X: 1; Y: 1; Z: 1)), {back} |
| 291 |
((X:-1; Y: -1; Z: 1), (X: 1; Y: -1; Z: 1), (X: 1; Y: -1; Z: -1), (X:-1; Y: -1; Z: -1)), {bottom} |
| 292 |
((X:-1; Y: -1; Z: -1), (X: 1; Y: -1; Z: -1), (X: 1; Y: 1; Z: -1), (X:-1; Y: 1; Z: -1)), {front} |
| 293 |
((X:-1; Y: -1; Z: 1), (X:-1; Y: -1; Z: -1), (X:-1; Y: 1; Z: -1), (X:-1; Y: 1; Z: 1)), {left} |
| 294 |
((X: 1; Y: -1; Z: -1), (X: 1; Y: -1; Z: 1), (X: 1; Y: 1; Z: 1), (X: 1; Y: 1; Z: -1)), {right} |
| 295 |
((X:-1; Y: 1; Z: -1), (X: 1; Y: 1; Z: -1), (X: 1; Y: 1; Z: 1), (X:-1; Y: 1; Z: 1)) {top} |
| 296 |
); |
| 297 |
TexCoords: array [0..3] of TVector2 = ( |
| 298 |
(X: 0; Y: 0), |
| 299 |
(X: 1; Y: 0), |
| 300 |
(X: 1; Y: 1), |
| 301 |
(X: 0; Y: 1) |
| 302 |
); |
| 303 |
var |
| 304 |
Shape: TShapeNode; |
| 305 |
Appearance: TAppearanceNode; |
| 306 |
QuadSet: TQuadSetNode; |
| 307 |
Coord: TCoordinateNode; |
| 308 |
TexCoord: TTextureCoordinateNode; |
| 309 |
Texture: TAbstractTextureNode; |
| 310 |
V: TVector3; |
| 311 |
begin |
| 312 |
Texture := Node.Texture(Side); |
| 313 |
if Texture = nil then Exit; |
| 314 |
|
| 315 |
Coord := TCoordinateNode.Create('', Node.BaseUrl); |
| 316 |
for V in Coords[Side] do |
| 317 |
Coord.FdPoint.Items.Add(V * CubeSize2); |
| 318 |
|
| 319 |
TexCoord := TTextureCoordinateNode.Create('', Node.BaseUrl); |
| 320 |
TexCoord.FdPoint.Send(TexCoords); |
| 321 |
|
| 322 |
QuadSet := TQuadSetNode.Create('', Node.BaseUrl); |
| 323 |
QuadSet.Coord := Coord; |
| 324 |
QuadSet.TexCoord := TexCoord; |
| 325 |
|
| 326 |
Appearance := TAppearanceNode.Create('', Node.BaseUrl); |
| 327 |
Appearance.FdShaders.AssignValue(Node.FdShaders); |
| 328 |
Appearance.FdEffects.AssignValue(Node.FdEffects); |
| 329 |
Appearance.Texture := Texture; |
| 330 |
if Texture is TAbstractTexture2DNode then |
| 331 |
begin |
| 332 |
{ We have to change repeat mode of this texture, even if it came from |
| 333 |
TTextureBackgroundNode. The only reasonable way to render background |
| 334 |
is to use clamp mode. More correct alternative would be creating |
| 335 |
a copy of node in case of TTextureBackgroundNode, |
| 336 |
but this would often be wasteful --- the background texture is |
| 337 |
probably not DEF/USEd in other places (that need repeat mode), |
| 338 |
and it's probably repeat=true by accident (since this is the default value). } |
| 339 |
TAbstractTexture2DNode(Texture).RepeatS := false; |
| 340 |
TAbstractTexture2DNode(Texture).RepeatT := false; |
| 341 |
end; |
| 342 |
|
| 343 |
Shape := TShapeNode.Create('', Node.BaseUrl); |
| 344 |
Shape.FdGeometry.Value := QuadSet; |
| 345 |
Shape.Appearance := Appearance; |
| 346 |
|
| 347 |
Transform.AddChildren(Shape); |
| 348 |
end; |
| 349 |
|
| 350 |
var |
| 351 |
BS: TBackgroundSide; |
| 352 |
begin |
| 353 |
CubeSize := SkySphereRadius * SphereRadiusToCubeSize; |
| 354 |
CubeSize2 := CubeSize / 2; |
| 355 |
for BS := Low(BS) to High(BS) do RenderTextureSide(BS); |
| 356 |
end; |
| 357 |
|
| 358 |
var |
| 359 |
SphereCreated: boolean; |
| 360 |
SphereCoord: TMFVec3f; |
| 361 |
SphereCoordIndex: TMFInt32; |
| 362 |
SphereColor: TMFColor; |
| 363 |
|
| 364 |
procedure NeedsSphere; |
| 365 |
var |
| 366 |
Coord: TCoordinateNode; |
| 367 |
Color: TColorNode; |
| 368 |
Geometry: TIndexedFaceSetNode; |
| 369 |
Shape: TShapeNode; |
| 370 |
begin |
| 371 |
{ add a mesh for sphere, if not present already } |
| 372 |
if not SphereCreated then |
| 373 |
begin |
| 374 |
SphereCreated := true; |
| 375 |
|
| 376 |
Coord := TCoordinateNode.Create('', Node.BaseUrl); |
| 377 |
SphereCoord := Coord.FdPoint; |
| 378 |
|
| 379 |
Color := TColorNode.Create('', Node.BaseUrl); |
| 380 |
SphereColor := Color.FdColor; |
| 381 |
|
| 382 |
Geometry := TIndexedFaceSetNode.Create('', Node.BaseUrl); |
| 383 |
Geometry.FdCoord.Value := Coord; |
| 384 |
Geometry.FdColor.Value := Color; |
| 385 |
Geometry.FdSolid.Value := false; |
| 386 |
SphereCoordIndex := Geometry.FdCoordIndex; |
| 387 |
|
| 388 |
Shape := TShapeNode.Create('', Node.BaseUrl); |
| 389 |
Shape.FdGeometry.Value := Geometry; |
| 390 |
|
| 391 |
Transform.AddChildren(Shape); |
| 392 |
end; |
| 393 |
end; |
| 394 |
|
| 395 |
const |
| 396 |
{ slices of rings rendered in Render*Stack } |
| 397 |
Slices = 24; |
| 398 |
|
| 399 |
{ For given Angle (meaning: 0 = zenith, Pi = nadir), calculate the height |
| 400 |
and radius of given circle of sky sphere. } |
| 401 |
procedure StackCircleCalc(const Angle: Single; out Y, Radius: Single); |
| 402 |
var |
| 403 |
S, C: Single; |
| 404 |
begin |
| 405 |
SinCos(Angle, S, C); |
| 406 |
Radius := S * SkySphereRadius; |
| 407 |
Y := C * SkySphereRadius; |
| 408 |
end; |
| 409 |
|
| 410 |
function StackTipCalc(const Angle: Single): TVector3; |
| 411 |
begin |
| 412 |
// Result := Vector3(0, Cos(Angle) * SkySphereRadius, 0); |
| 413 |
{ simpler and more accurate version, since StackTipCalc is only called with |
| 414 |
Angle = 0 or Pi } |
| 415 |
if Angle = 0 then |
| 416 |
Result := Vector3(0, SkySphereRadius, 0) else |
| 417 |
begin |
| 418 |
Assert(Angle = Single(Pi)); |
| 419 |
Result := Vector3(0, -SkySphereRadius, 0); |
| 420 |
end; |
| 421 |
end; |
| 422 |
|
| 423 |
function CirclePoint(const Y, Radius: Single; const SliceIndex: Integer): TVector3; |
| 424 |
var |
| 425 |
S, C: Single; |
| 426 |
begin |
| 427 |
SinCos(SliceIndex * 2 * Pi / Slices, S, C); |
| 428 |
Result := Vector3(S * Radius, Y, C * Radius); |
| 429 |
end; |
| 430 |
|
| 431 |
{ Render*Stack: render one stack of sky/ground sphere. |
| 432 |
Angles are given in the sky connvention : 0 is zenith, Pi is nadir. } |
| 433 |
|
| 434 |
procedure RenderFirstStack( |
| 435 |
const TipColor : TVector3; const TipAngle : Single; |
| 436 |
const CircleColor: TVector3; const CircleAngle: Single); |
| 437 |
var |
| 438 |
CircleY, CircleRadius: Single; |
| 439 |
I, Start, Next, StartIndex, NextIndex: Integer; |
| 440 |
begin |
| 441 |
Start := SphereCoord.Count; |
| 442 |
Next := Start; |
| 443 |
Assert(Start = SphereColor.Count); |
| 444 |
SphereCoord.Count := Start + Slices + 1; |
| 445 |
SphereColor.Count := Start + Slices + 1; |
| 446 |
|
| 447 |
StartIndex := SphereCoordIndex.Count; |
| 448 |
NextIndex := StartIndex; |
| 449 |
SphereCoordIndex.Count := SphereCoordIndex.Count + Slices * 4; |
| 450 |
|
| 451 |
StackCircleCalc(CircleAngle, CircleY, CircleRadius); |
| 452 |
|
| 453 |
SphereCoord.Items.L[Start] := StackTipCalc(TipAngle); |
| 454 |
SphereColor.Items.L[Start] := TipColor; |
| 455 |
Inc(Next); |
| 456 |
|
| 457 |
for I := 0 to Slices - 1 do |
| 458 |
begin |
| 459 |
SphereCoord.Items.L[Next] := CirclePoint(CircleY, CircleRadius, I); |
| 460 |
SphereColor.Items.L[Next] := CircleColor; |
| 461 |
Inc(Next); |
| 462 |
|
| 463 |
SphereCoordIndex.Items.L[NextIndex ] := Start; |
| 464 |
SphereCoordIndex.Items.L[NextIndex + 1] := Start + 1 + I; |
| 465 |
if I <> Slices - 1 then |
| 466 |
SphereCoordIndex.Items.L[NextIndex + 2] := Start + 2 + I else |
| 467 |
SphereCoordIndex.Items.L[NextIndex + 2] := Start + 1; |
| 468 |
SphereCoordIndex.Items.L[NextIndex + 3] := -1; |
| 469 |
NextIndex := NextIndex + 4; |
| 470 |
end; |
| 471 |
end; |
| 472 |
|
| 473 |
procedure RenderNextStack( |
| 474 |
const CircleColor: TVector3; const CircleAngle: Single); |
| 475 |
var |
| 476 |
CircleY, CircleRadius: Single; |
| 477 |
I, Start, Next, StartIndex, NextIndex: Integer; |
| 478 |
begin |
| 479 |
Start := SphereCoord.Count; |
| 480 |
Next := Start; |
| 481 |
Assert(Start = SphereColor.Count); |
| 482 |
SphereCoord.Count := Start + Slices; |
| 483 |
SphereColor.Count := Start + Slices; |
| 484 |
|
| 485 |
StartIndex := SphereCoordIndex.Count; |
| 486 |
NextIndex := StartIndex; |
| 487 |
SphereCoordIndex.Count := SphereCoordIndex.Count + Slices * 5; |
| 488 |
|
| 489 |
StackCircleCalc(CircleAngle, CircleY, CircleRadius); |
| 490 |
|
| 491 |
for I := 0 to Slices - 1 do |
| 492 |
begin |
| 493 |
SphereCoord.Items.L[Next] := CirclePoint(CircleY, CircleRadius, I); |
| 494 |
SphereColor.Items.L[Next] := CircleColor; |
| 495 |
Inc(Next); |
| 496 |
|
| 497 |
SphereCoordIndex.Items.L[NextIndex ] := Start + I; |
| 498 |
if I <> Slices - 1 then |
| 499 |
begin |
| 500 |
SphereCoordIndex.Items.L[NextIndex + 1] := Start + 1 + I; |
| 501 |
SphereCoordIndex.Items.L[NextIndex + 2] := Start + 1 + I - Slices; |
| 502 |
end else |
| 503 |
begin |
| 504 |
SphereCoordIndex.Items.L[NextIndex + 1] := Start; |
| 505 |
SphereCoordIndex.Items.L[NextIndex + 2] := Start - Slices; |
| 506 |
end; |
| 507 |
SphereCoordIndex.Items.L[NextIndex + 3] := Start + I - Slices; |
| 508 |
SphereCoordIndex.Items.L[NextIndex + 4] := -1; |
| 509 |
NextIndex := NextIndex + 5; |
| 510 |
end; |
| 511 |
end; |
| 512 |
|
| 513 |
procedure RenderLastStack( |
| 514 |
const TipColor: TVector3; const TipAngle: Single); |
| 515 |
var |
| 516 |
I, Start, StartIndex, NextIndex: Integer; |
| 517 |
begin |
| 518 |
Start := SphereCoord.Count; |
| 519 |
Assert(Start = SphereColor.Count); |
| 520 |
SphereCoord.Count := Start + 1; |
| 521 |
SphereColor.Count := Start + 1; |
| 522 |
|
| 523 |
StartIndex := SphereCoordIndex.Count; |
| 524 |
NextIndex := StartIndex; |
| 525 |
SphereCoordIndex.Count := SphereCoordIndex.Count + Slices * 4; |
| 526 |
|
| 527 |
SphereCoord.Items.L[Start] := StackTipCalc(TipAngle); |
| 528 |
SphereColor.Items.L[Start] := TipColor; |
| 529 |
|
| 530 |
for I := 0 to Slices - 1 do |
| 531 |
begin |
| 532 |
SphereCoordIndex.Items.L[NextIndex ] := Start; |
| 533 |
if I <> Slices - 1 then |
| 534 |
SphereCoordIndex.Items.L[NextIndex + 1] := Start - Slices + I + 1 else |
| 535 |
SphereCoordIndex.Items.L[NextIndex + 1] := Start - Slices; |
| 536 |
SphereCoordIndex.Items.L[NextIndex + 2] := Start - Slices + I; |
| 537 |
SphereCoordIndex.Items.L[NextIndex + 3] := -1; |
| 538 |
NextIndex := NextIndex + 4; |
| 539 |
end; |
| 540 |
end; |
| 541 |
|
| 542 |
procedure RenderSky; |
| 543 |
var |
| 544 |
I, ColorCount, AngleCount: Integer; |
| 545 |
Angle: PSingle; |
| 546 |
Color: PVector3; |
| 547 |
GroundHighestAngle: Single; |
| 548 |
begin |
| 549 |
{ calculate GroundHighestAngle, will be usable to optimize rendering sky. |
| 550 |
GroundHighestAngle is measured in sky convention (0 = zenith, Pi = nadir). |
| 551 |
If there is no sky I simply set GroundHighestAngle to sthg > Pi. } |
| 552 |
if Node.FdGroundAngle.Count <> 0 then |
| 553 |
GroundHighestAngle := Pi - Node.FdGroundAngle.Items.Last else |
| 554 |
GroundHighestAngle := Pi + 1; |
| 555 |
|
| 556 |
ColorCount := Node.FdSkyColor.Count; |
| 557 |
AngleCount := Node.FdSkyAngle.Count; |
| 558 |
Color := PVector3(Node.FdSkyColor.Items.L); |
| 559 |
Angle := PSingle(Node.FdSkyAngle.Items.L); |
| 560 |
|
| 561 |
if ColorCount <= 0 then |
| 562 |
begin |
| 563 |
WritelnWarning('VRML/X3D', 'Background node incorrect: Sky must have at least one color'); |
| 564 |
Exit; |
| 565 |
end else |
| 566 |
if AngleCount + 1 <> ColorCount then |
| 567 |
begin |
| 568 |
WritelnWarning('VRML/X3D', 'Background node incorrect: Sky must have exactly one more Color than Angles'); |
| 569 |
{ We know now that ColorCount >= 1, and of course AngleCount >= 0 |
| 570 |
(since array always has >= 0 items). So we correct one of them to be |
| 571 |
smaller. } |
| 572 |
if AngleCount + 1 > ColorCount then |
| 573 |
AngleCount := ColorCount - 1 else |
| 574 |
ColorCount := AngleCount + 1; |
| 575 |
end; |
| 576 |
|
| 577 |
Assert(ColorCount >= 1); |
| 578 |
Assert(AngleCount + 1 = ColorCount); |
| 579 |
ClearColor := Vector4(Color[0], 1.0); |
| 580 |
|
| 581 |
UseClearColor := ColorCount = 1; |
| 582 |
|
| 583 |
if ColorCount > 1 then |
| 584 |
begin |
| 585 |
{ When ColorCount >= 2, the idea of rendering is to do: |
| 586 |
- RenderFirstStack |
| 587 |
- RenderNextStack many times |
| 588 |
- RenderLastStack |
| 589 |
But we try to break this early, to not waste time rendering |
| 590 |
something that will be covered anyway by the ground sphere, |
| 591 |
using GroundHighestAngle. } |
| 592 |
|
| 593 |
NeedsSphere; |
| 594 |
|
| 595 |
RenderFirstStack(Color[0], 0, |
| 596 |
Color[1], Angle[0]); |
| 597 |
for I := 1 to AngleCount - 1 do |
| 598 |
begin |
| 599 |
if Angle[I - 1] > GroundHighestAngle then Break; |
| 600 |
RenderNextStack(Color[I + 1], Angle[I]); |
| 601 |
end; |
| 602 |
{ Close the tip of the sky sphere with constant color (last on Color[] table). |
| 603 |
Add 0.01 epsilon, in case GroundHighestAngle is very close to the |
| 604 |
last sky angle. Better to make RenderLastStack then, to avoid bad artifacts |
| 605 |
(see https://github.com/castle-engine/castle-engine/issues/79 ). } |
| 606 |
if Angle[AngleCount - 1] <= GroundHighestAngle + 0.01 then |
| 607 |
RenderLastStack(Color[ColorCount - 1], Pi); |
| 608 |
end; |
| 609 |
end; |
| 610 |
|
| 611 |
procedure RenderGround; |
| 612 |
var |
| 613 |
I: Integer; |
| 614 |
ColorCount, AngleCount: Integer; |
| 615 |
Angle: PSingle; |
| 616 |
Color: PVector3; |
| 617 |
begin |
| 618 |
ColorCount := Node.FdGroundColor.Count; |
| 619 |
AngleCount := Node.FdGroundAngle.Count; |
| 620 |
Color := PVector3(Node.FdGroundColor.Items.L); |
| 621 |
Angle := PSingle(Node.FdGroundAngle.Items.L); |
| 622 |
|
| 623 |
if AngleCount <> 0 then |
| 624 |
begin |
| 625 |
if AngleCount + 1 <> ColorCount then |
| 626 |
begin |
| 627 |
WritelnWarning('VRML/X3D', 'Background node incorrect: Ground must have exactly one more Color than Angles'); |
| 628 |
if AngleCount + 1 > ColorCount then |
| 629 |
AngleCount := ColorCount - 1 else |
| 630 |
ColorCount := AngleCount + 1; |
| 631 |
end; |
| 632 |
Assert(AngleCount + 1 = ColorCount); |
| 633 |
|
| 634 |
NeedsSphere; |
| 635 |
|
| 636 |
RenderFirstStack(Color[0], Pi, |
| 637 |
Color[1], Pi - Angle[0]); |
| 638 |
for I := 1 to AngleCount - 1 do |
| 639 |
RenderNextStack(Color[I + 1], Pi - Angle[I]); |
| 640 |
end; |
| 641 |
end; |
| 642 |
|
| 643 |
var |
| 644 |
RootNode: TX3DRootNode; |
| 645 |
begin |
| 646 |
inherited Create; |
| 647 |
|
| 648 |
SkySphereRadius := NearFarToSkySphereRadius(BgProjectionNear, BgProjectionFar); |
| 649 |
SphereCreated := false; |
| 650 |
|
| 651 |
RootNode := TX3DRootNode.Create('', Node.BaseUrl); |
| 652 |
|
| 653 |
Transform := TTransformNode.Create('', Node.BaseUrl); |
| 654 |
Transform.Rotation := Node.TransformRotation; |
| 655 |
RootNode.AddChildren(Transform); |
| 656 |
|
| 657 |
RenderSky; |
| 658 |
RenderGround; |
| 659 |
RenderCubeSides; |
| 660 |
|
| 661 |
Scene.Load(RootNode, true); |
| 662 |
end; |
| 663 |
|
| 664 |
procedure TBackground3D.UpdateRotation(const Rotation: TVector4); |
| 665 |
begin |
| 666 |
inherited; |
| 667 |
Transform.Rotation := Rotation; |
| 668 |
end; |
| 669 |
|
| 670 |
procedure TBackground3D.Render(const RenderingCamera: TRenderingCamera; |
| 671 |
const Wireframe: boolean; |
| 672 |
const RenderRect: TFloatRectangle; |
| 673 |
const CurrentProjection: TProjection; |
| 674 |
const ShapesCollector: TShapesCollector; |
| 675 |
const ShapesRenderer: TShapesRenderer); |
| 676 |
var |
| 677 |
SavedProjectionMatrix: TMatrix4; |
| 678 |
AspectRatio: Single; |
| 679 |
TempProjection: TProjection; |
| 680 |
begin |
| 681 |
{ Change projection. |
| 682 |
|
| 683 |
Reason when ptOrthographic: |
| 684 |
The background rendering doesn't like custom orthographic Dimensions. |
| 685 |
They could make the background sky box very small, such that it |
| 686 |
doesn't fill the screen. See e.g. x3d/empty_with_background_ortho.x3dv |
| 687 |
testcase. So temporary set good perspective projection. |
| 688 |
|
| 689 |
Reason in both cases: |
| 690 |
We need to set our own BgProjectionNear/Far, |
| 691 |
in order for SkySphereRadius be valid. } |
| 692 |
|
| 693 |
SavedProjectionMatrix := RenderContext.ProjectionMatrix; |
| 694 |
|
| 695 |
AspectRatio := RenderRect.Width / RenderRect.Height; |
| 696 |
if CurrentProjection.ProjectionType = ptOrthographic then |
| 697 |
begin |
| 698 |
RenderContext.ProjectionMatrix := PerspectiveProjectionMatrixDeg( |
| 699 |
45, AspectRatio, BgProjectionNear, BgProjectionFar) |
| 700 |
end else |
| 701 |
begin |
| 702 |
TempProjection := CurrentProjection; |
| 703 |
TempProjection.ProjectionNear := BgProjectionNear; |
| 704 |
TempProjection.ProjectionFar := BgProjectionFar; |
| 705 |
RenderContext.ProjectionMatrix := TempProjection.Matrix(AspectRatio); |
| 706 |
end; |
| 707 |
|
| 708 |
inherited; |
| 709 |
|
| 710 |
RenderContext.ProjectionMatrix := SavedProjectionMatrix; |
| 711 |
end; |
| 712 |
|
| 713 |
{ TBackground2D --------------------------------------------------------------- } |
| 714 |
|
| 715 |
type |
| 716 |
TBackground2D = class(TBackgroundScene) |
| 717 |
strict private |
| 718 |
Node: TImageBackgroundNode; |
| 719 |
Shape: TShapeNode; |
| 720 |
Texture2D: TAbstractTexture2DNode; |
| 721 |
TexCoords: TTextureCoordinateNode; |
| 722 |
IndexedFaceSet: TIndexedFaceSetNode; |
| 723 |
Material: TUnlitMaterialNode; |
| 724 |
public |
| 725 |
constructor Create(const ANode: TImageBackgroundNode); |
| 726 |
destructor Destroy; override; |
| 727 |
procedure Render(const RenderingCamera: TRenderingCamera; |
| 728 |
const Wireframe: boolean; |
| 729 |
const RenderRect: TFloatRectangle; |
| 730 |
const CurrentProjection: TProjection; |
| 731 |
const ShapesCollector: TShapesCollector; |
| 732 |
const ShapesRenderer: TShapesRenderer); override; |
| 733 |
end; |
| 734 |
|
| 735 |
constructor TBackground2D.Create(const ANode: TImageBackgroundNode); |
| 736 |
var |
| 737 |
RootNode: TX3DRootNode; |
| 738 |
Coordinate: TCoordinateNode; |
| 739 |
begin |
| 740 |
inherited Create; |
| 741 |
|
| 742 |
Node := ANode; |
| 743 |
|
| 744 |
if Node.Texture is TAbstractTexture2DNode then // also checks is it non-nil |
| 745 |
Texture2D := TAbstractTexture2DNode(Node.Texture) |
| 746 |
else |
| 747 |
Texture2D := nil; |
| 748 |
|
| 749 |
if Texture2D <> nil then |
| 750 |
begin |
| 751 |
RootNode := TX3DRootNode.Create; |
| 752 |
|
| 753 |
Coordinate := TCoordinateNode.Create; |
| 754 |
Coordinate.SetPoint([ |
| 755 |
Vector3(-1, -1, 0), |
| 756 |
Vector3( 1, -1, 0), |
| 757 |
Vector3( 1, 1, 0), |
| 758 |
Vector3(-1, 1, 0) |
| 759 |
]); |
| 760 |
|
| 761 |
TexCoords := TTextureCoordinateNode.Create; |
| 762 |
|
| 763 |
IndexedFaceSet := TIndexedFaceSetNode.CreateWithShape(Shape); |
| 764 |
IndexedFaceSet.Coord := Coordinate; |
| 765 |
IndexedFaceSet.TexCoord := TexCoords; |
| 766 |
IndexedFaceSet.SetCoordIndex([0, 1, 2, 3]); |
| 767 |
|
| 768 |
Shape.Appearance := TAppearanceNode.Create; |
| 769 |
Shape.Appearance.Texture := Texture2D; |
| 770 |
|
| 771 |
Material := TUnlitMaterialNode.Create; |
| 772 |
Shape.Appearance.Material := Material; |
| 773 |
|
| 774 |
RootNode.AddChildren(Shape); |
| 775 |
|
| 776 |
Scene.Load(RootNode, true); |
| 777 |
end; |
| 778 |
end; |
| 779 |
|
| 780 |
destructor TBackground2D.Destroy; |
| 781 |
begin |
| 782 |
inherited; |
| 783 |
end; |
| 784 |
|
| 785 |
procedure TBackground2D.Render(const RenderingCamera: TRenderingCamera; |
| 786 |
const Wireframe: boolean; |
| 787 |
const RenderRect: TFloatRectangle; |
| 788 |
const CurrentProjection: TProjection; |
| 789 |
const ShapesCollector: TShapesCollector; |
| 790 |
const ShapesRenderer: TShapesRenderer); |
| 791 |
|
| 792 |
{ Apply various Node properties at the beginning of each Render, |
| 793 |
this way we can animate them. |
| 794 |
See demo-models/background/background_image_animated.x3d example. } |
| 795 |
procedure UpdateProperties; |
| 796 |
begin |
| 797 |
Assert(Node <> nil); |
| 798 |
Assert(Texture2D <> nil); |
| 799 |
|
| 800 |
Material.EmissiveColor := Node.Color.XYZ; |
| 801 |
Material.Transparency := 1 - Node.Color.W; |
| 802 |
|
| 803 |
// TODO: check do they differ |
| 804 |
TexCoords.SetPoint(Node.FdTexCoords.Items); |
| 805 |
end; |
| 806 |
|
| 807 |
var |
| 808 |
SavedProjectionMatrix, SavedModelviewMatrix: TMatrix4; |
| 809 |
begin |
| 810 |
if Texture2D = nil then Exit; |
| 811 |
|
| 812 |
UpdateProperties; |
| 813 |
|
| 814 |
{ Our Scene geometry assumes that modelview matrix is identity } |
| 815 |
if GLFeatures.EnableFixedFunction then |
| 816 |
begin |
| 817 |
{$ifndef OpenGLES} |
| 818 |
glLoadMatrix(TMatrix4.Identity); |
| 819 |
{$endif} |
| 820 |
end; |
| 821 |
SavedModelviewMatrix := RenderingCamera.RotationMatrix; |
| 822 |
RenderingCamera.RotationMatrix := TMatrix4.Identity; |
| 823 |
|
| 824 |
{ Our Scene geometry assumes an orthographic projection (-1,-1) - (1,1) } |
| 825 |
SavedProjectionMatrix := RenderContext.ProjectionMatrix; |
| 826 |
OrthoProjection(FloatRectangle(-1, -1, 2, 2)); |
| 827 |
|
| 828 |
inherited; |
| 829 |
|
| 830 |
RenderContext.ProjectionMatrix := SavedProjectionMatrix; |
| 831 |
RenderingCamera.RotationMatrix := SavedModelviewMatrix; |
| 832 |
end; |
| 833 |
|
| 834 |
{ global routines ------------------------------------------------------------ } |
| 835 |
|
| 836 |
function CreateBackgroundRenderer(const Node: TAbstractBackgroundNode): TBackgroundRenderer; |
| 837 |
begin |
| 838 |
if Node is TAbstract3DBackgroundNode then |
| 839 |
Result := TBackground3D.Create(TAbstract3DBackgroundNode(Node)) |
| 840 |
else |
| 841 |
if Node is TImageBackgroundNode then |
| 842 |
Result := TBackground2D.Create(TImageBackgroundNode(Node)) |
| 843 |
else |
| 844 |
Result := nil; |
| 845 |
end; |
| 846 |
|
| 847 |
end. |