| 1 |
{ |
| 2 |
Copyright 2008-2023 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 |
{ Locations in game (Locations). } |
| 17 |
unit GameLocations; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, Generics.Collections, |
| 22 |
CastleUtils, CastleClassUtils, CastleScene, CastleVectors, CastleTransform, |
| 23 |
CastleGLImages, X3DNodes, CastleRectangles, CastleRenderOptions; |
| 24 |
|
| 25 |
type |
| 26 |
TLocation = class |
| 27 |
private |
| 28 |
public |
| 29 |
type |
| 30 |
TLocationScene = class(TCastleScene) |
| 31 |
end; |
| 32 |
|
| 33 |
TLocationImageTransform = class(TCastleTransform) |
| 34 |
private |
| 35 |
Image, ShadowedImage: TDrawableImage; |
| 36 |
public |
| 37 |
ViewportRect: TRectangle; |
| 38 |
procedure LocalRender(const Params: TRenderParams); override; |
| 39 |
end; |
| 40 |
|
| 41 |
private |
| 42 |
FName: string; |
| 43 |
FImageUrl: String; |
| 44 |
FShadowedImageUrl: String; |
| 45 |
FSceneUrl: String; |
| 46 |
FViewpoint: String; |
| 47 |
FScene: TLocationScene; |
| 48 |
FImageTransform: TLocationImageTransform; |
| 49 |
FImage, FShadowedImage: TDrawableImage; |
| 50 |
FSceneCameraDescription: string; |
| 51 |
FPlayerPosition: TVector3; |
| 52 |
FPlayerDirection: TVector3; |
| 53 |
Loaded: boolean; |
| 54 |
FRenderInternalModel: Boolean; |
| 55 |
procedure SetRenderInternalModel(const Value: Boolean); |
| 56 |
procedure RenderInternalModelChanged; |
| 57 |
public |
| 58 |
constructor Create; |
| 59 |
destructor Destroy; override; |
| 60 |
|
| 61 |
{ Create things necessary for playing (displaying this location). } |
| 62 |
procedure Load(const PrepareParams: TPrepareParams); |
| 63 |
|
| 64 |
{ Location short name, must be simple (needs to be valid XML element |
| 65 |
name and X3D node name, for easy data reading). } |
| 66 |
property Name: string read FName; |
| 67 |
|
| 68 |
property ImageUrl: String read FImageUrl; |
| 69 |
property ShadowedImageUrl: String read FShadowedImageUrl; |
| 70 |
property SceneUrl: String read FSceneUrl; |
| 71 |
|
| 72 |
property SceneCameraDescription: string read FSceneCameraDescription; |
| 73 |
|
| 74 |
property PlayerPosition: TVector3 read FPlayerPosition; |
| 75 |
property PlayerDirection: TVector3 read FPlayerDirection; |
| 76 |
|
| 77 |
property Scene: TLocationScene read FScene; |
| 78 |
property ImageTransform: TLocationImageTransform read FImageTransform; |
| 79 |
|
| 80 |
// Not needed publicly. |
| 81 |
// property Image: TDrawableImage read FImage; |
| 82 |
// property ShadowedImage: TDrawableImage read FShadowedImage; |
| 83 |
|
| 84 |
{ Should we render the internal location 3D model as usual |
| 85 |
for debug purposes. |
| 86 |
By default, when this is @false, the location 3D model |
| 87 |
is used for depth information, but not for colors. } |
| 88 |
property RenderInternalModel: Boolean read FRenderInternalModel |
| 89 |
write SetRenderInternalModel default false; |
| 90 |
end; |
| 91 |
|
| 92 |
TLocationList = class({$ifdef FPC}specialize{$endif} TObjectList<TLocation>) |
| 93 |
public |
| 94 |
StartLocation: TLocation; |
| 95 |
|
| 96 |
{ Create locations and set their parameters from GameConfig. } |
| 97 |
constructor Create; |
| 98 |
end; |
| 99 |
|
| 100 |
var |
| 101 |
{ List of all locations, should be created in Application.OnInitialize. } |
| 102 |
Locations: TLocationList; |
| 103 |
|
| 104 |
implementation |
| 105 |
|
| 106 |
uses SysUtils, DOM, |
| 107 |
CastleImages, CastleUIControls, CastleGLUtils, CastleXmlUtils, |
| 108 |
CastleSceneCore, CastleApplicationProperties, X3DLoad, CastleRenderContext, |
| 109 |
GameConfiguration; |
| 110 |
|
| 111 |
{ TLocation.TLocationImage --------------------------------------------------- } |
| 112 |
|
| 113 |
procedure TLocation.TLocationImageTransform. |
| 114 |
LocalRender(const Params: TRenderParams); |
| 115 |
|
| 116 |
{ Draw Image centered on screen, to fit inside the TCastleViewport, |
| 117 |
matching the 3D scene projection. } |
| 118 |
procedure DrawImage(const Image: TDrawableImage); |
| 119 |
var |
| 120 |
DrawRect: TRectangle; |
| 121 |
begin |
| 122 |
{ Draw Image such that Image.Height always fills the ViewportRect.Height, |
| 123 |
because that is the field of view of 3D scene, |
| 124 |
and that was the field of view used to render the image in Blender. } |
| 125 |
DrawRect := Image.Rect.ScaleToHeight(ViewportRect.Height); |
| 126 |
{ above calculated DrawRect size OK, but DrawRect position (Left, Bottom) |
| 127 |
should be fixed now. } |
| 128 |
DrawRect := ViewportRect.CenterInside(DrawRect.Width, DrawRect.Height); |
| 129 |
Image.Draw(DrawRect); |
| 130 |
end; |
| 131 |
|
| 132 |
var |
| 133 |
SavedProjectionMatrix: TMatrix4; |
| 134 |
SavedDepthTest: Boolean; |
| 135 |
begin |
| 136 |
{ Render the 2D image covering the location. |
| 137 |
|
| 138 |
This cooperates correctly with shadow volumes, because the Image.Draw call |
| 139 |
is correctly masked with the stencil buffer settings of the shadow volume |
| 140 |
renderer. |
| 141 |
|
| 142 |
To work correctly, the location scene must be rendered before creatures |
| 143 |
(like Player) scenes (otherwise Image.Draw would unconditionally cover |
| 144 |
the the Player). This is satisfied, since CurrentLocation.Scene |
| 145 |
is first in Viewport.Items. } |
| 146 |
|
| 147 |
if (not Params.Transparent) and |
| 148 |
(true in Params.ShadowVolumesReceivers) then |
| 149 |
begin |
| 150 |
{ Nole that the 3 lines that save, set and restore RenderContext.ProjectionMatrix |
| 151 |
are necessary only in case GLFeatures.EnableFixedFunction = true, |
| 152 |
which will be false on all modern GPUs and OpenGLES. |
| 153 |
When GLFeatures.EnableFixedFunction = false, |
| 154 |
then rendering Image doesn't need to have a projection matrix set. } |
| 155 |
SavedProjectionMatrix := RenderContext.ProjectionMatrix; |
| 156 |
OrthoProjection(FloatRectangle(ViewportRect)); // need 2D projection |
| 157 |
|
| 158 |
// do not test or change Z buffer |
| 159 |
SavedDepthTest := RenderContext.DepthTest; |
| 160 |
RenderContext.DepthTest := false; |
| 161 |
|
| 162 |
if Params.InShadow then |
| 163 |
DrawImage(ShadowedImage) |
| 164 |
else |
| 165 |
DrawImage(Image); |
| 166 |
|
| 167 |
RenderContext.ProjectionMatrix := SavedProjectionMatrix; // restore 3D projection |
| 168 |
RenderContext.DepthTest := SavedDepthTest; |
| 169 |
end; |
| 170 |
end; |
| 171 |
|
| 172 |
{ TLocation ------------------------------------------------------------------ } |
| 173 |
|
| 174 |
constructor TLocation.Create; |
| 175 |
begin |
| 176 |
inherited; |
| 177 |
end; |
| 178 |
|
| 179 |
destructor TLocation.Destroy; |
| 180 |
begin |
| 181 |
FreeAndNil(FScene); |
| 182 |
FreeAndNil(FImageTransform); |
| 183 |
FreeAndNil(FImage); |
| 184 |
FreeAndNil(FShadowedImage); |
| 185 |
inherited; |
| 186 |
end; |
| 187 |
|
| 188 |
procedure TLocation.Load(const PrepareParams: TPrepareParams); |
| 189 |
begin |
| 190 |
if Loaded then Exit; |
| 191 |
Loaded := true; |
| 192 |
|
| 193 |
FImage := TDrawableImage.Create(ImageUrl, [TRGBImage]); |
| 194 |
FShadowedImage := TDrawableImage.Create(ShadowedImageUrl, [TRGBImage]); |
| 195 |
|
| 196 |
FScene := TLocationScene.Create(nil); |
| 197 |
FScene.PreciseCollisions := true; |
| 198 |
{ The shadows are already drawn on location Image, |
| 199 |
so no need to cast them on location again. |
| 200 |
TODO: This also means that location cannot cast shadows on Player. |
| 201 |
A better approach would be to leave CastShadows = true (default), |
| 202 |
and change location Image to *not* contain location shadows "baked". } |
| 203 |
FScene.CastShadows := false; |
| 204 |
FScene.InitialViewpointName := FViewpoint; |
| 205 |
FScene.Load(SceneUrl); |
| 206 |
FScene.PrepareResources([prRenderSelf, prBoundingBox], PrepareParams); |
| 207 |
|
| 208 |
FImageTransform := TLocationImageTransform.Create(nil); |
| 209 |
FImageTransform.Image := FImage; |
| 210 |
FImageTransform.ShadowedImage := FShadowedImage; |
| 211 |
|
| 212 |
RenderInternalModelChanged; |
| 213 |
end; |
| 214 |
|
| 215 |
procedure TLocation.SetRenderInternalModel(const Value: Boolean); |
| 216 |
begin |
| 217 |
if FRenderInternalModel <> Value then |
| 218 |
begin |
| 219 |
FRenderInternalModel := Value; |
| 220 |
RenderInternalModelChanged; |
| 221 |
end; |
| 222 |
end; |
| 223 |
|
| 224 |
procedure TLocation.RenderInternalModelChanged; |
| 225 |
begin |
| 226 |
FImageTransform.Exists := not RenderInternalModel; |
| 227 |
|
| 228 |
if RenderInternalModel then |
| 229 |
begin |
| 230 |
FScene.RenderOptions.Mode := rmFull; |
| 231 |
FScene.RenderOptions.InternalColorChannels := AllColorChannels; |
| 232 |
end else |
| 233 |
begin |
| 234 |
// render scene only to depth buffer |
| 235 |
FScene.RenderOptions.Mode := rmDepth; |
| 236 |
FScene.RenderOptions.InternalColorChannels := []; |
| 237 |
end; |
| 238 |
end; |
| 239 |
|
| 240 |
{ TLocationList ------------------------------------------------------------- } |
| 241 |
|
| 242 |
constructor TLocationList.Create; |
| 243 |
|
| 244 |
procedure MissingLocationAttribute(const AttrName: string); |
| 245 |
begin |
| 246 |
raise Exception.CreateFmt('Location doesn''t have a required attribute "%s"', |
| 247 |
[AttrName]); |
| 248 |
end; |
| 249 |
|
| 250 |
var |
| 251 |
I: TXMLElementIterator; |
| 252 |
LocationsElement: TDOMElement; |
| 253 |
Location: TLocation; |
| 254 |
StartLocationName: string; |
| 255 |
begin |
| 256 |
inherited Create(true); |
| 257 |
|
| 258 |
LocationsElement := GameConfig.PathElement('locations'); |
| 259 |
if LocationsElement = nil then |
| 260 |
raise Exception.Create('Unable to find XML <locations> element'); |
| 261 |
|
| 262 |
if not LocationsElement.AttributeString('start_name', StartLocationName) then |
| 263 |
raise Exception.Create( |
| 264 |
'<locations> doesn''t have a required attribute "start_name"'); |
| 265 |
|
| 266 |
I := LocationsElement.ChildrenIterator; |
| 267 |
try |
| 268 |
while I.GetNext do |
| 269 |
begin |
| 270 |
Location := TLocation.Create; |
| 271 |
Add(Location); |
| 272 |
|
| 273 |
if not I.Current.AttributeString('name', Location.FName) then |
| 274 |
MissingLocationAttribute('name'); |
| 275 |
if Location.Name = StartLocationName then |
| 276 |
StartLocation := Location; |
| 277 |
|
| 278 |
Location.FImageUrl := I.Current.AttributeUrl('image_Url', GameConfig.Url); |
| 279 |
Location.FShadowedImageUrl := I.Current.AttributeUrl('shadowed_image_Url', GameConfig.Url); |
| 280 |
Location.FSceneUrl := I.Current.AttributeUrl('scene_Url', GameConfig.Url); |
| 281 |
Location.FViewpoint := I.Current.AttributeStringDef('viewpoint', ''); |
| 282 |
|
| 283 |
I.Current.AttributeString('scene_camera_description', |
| 284 |
Location.FSceneCameraDescription); |
| 285 |
|
| 286 |
Location.FPlayerPosition := I.Current.AttributeVector3Def( |
| 287 |
'player_position', TVector3.Zero); |
| 288 |
Location.FPlayerDirection := I.Current.AttributeVector3Def( |
| 289 |
'player_direction', Vector3(1, 0, 0)); |
| 290 |
end; |
| 291 |
finally FreeAndNil(I) end; |
| 292 |
|
| 293 |
if StartLocation = nil then |
| 294 |
raise Exception.CreateFmt('Start location name "%s" not found', |
| 295 |
[StartLocationName]); |
| 296 |
end; |
| 297 |
|
| 298 |
initialization |
| 299 |
finalization |
| 300 |
FreeAndNil(Locations); |
| 301 |
end. |