1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
{
Copyright 2010-2022 Michalis Kamburelis.
This file is part of "view3dscene".
"view3dscene" is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"view3dscene" is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with "view3dscene"; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
----------------------------------------------------------------------------
}
{ }
unit V3DSceneViewports;
{$I v3dsceneconf.inc}
interface
uses CastleViewport, CastleWindow, CastleCameras,
V3DSceneNavigationTypes;
{ Compile also with CGE branches that don't yet have new-cameras work merged.
Once new-cameras merged -> master, we can remove this. }
{$if not declared(TCastleAutoNavigationViewport)}
{$define TCastleAutoNavigationViewport:=TCastleViewport}
{$endif}
type
TViewportsConfig = (vc1, vc2Horizontal, vc4);
TViewportClass = class of TCastleAutoNavigationViewport;
const
ViewportsConfigNames: array [TViewportsConfig] of string =
('_1 Viewport',
'_2 Horizontal Viewports',
'_4 Viewports'
);
var
ViewportsConfig: TViewportsConfig;
{ Main TCastleViewport.
Set this before calling InitializeViewports. }
MainViewport: TCastleAutoNavigationViewport;
{ Custom viewports, using the same Items as MainViewport.Items.
Note that MainViewport is @italic(not listed here). }
ExtraViewports: array [0..2] of TCastleAutoNavigationViewport;
procedure SetViewportsConfig(const Value: TViewportsConfig;
Window: TCastleWindow; MainViewport: TCastleAutoNavigationViewport);
{ Copy all Camera and Navigation settings from Source to Target. }
procedure AssignCameraAndNavigation(const Target, Source: TCastleAutoNavigationViewport);
procedure ResizeViewports(Window: TCastleWindow; MainViewport: TCastleViewport);
{ Copy NewNavigationType to all (existing) viewports. }
procedure SetNavigationType(const NewNavigationType: TUserNavigationType);
procedure InitializeViewports(ViewportClass: TViewportClass);
{ Redraw all viewports (and background underneath).
This renders viewports for the off-screen rendering. }
procedure ViewportsRender(const Container: TCastleContainer);
procedure ViewportsSetTransparent(const Transparent: Boolean);
implementation
uses CastleVectors, SysUtils, CastleUtils, CastleUIControls, CastleControls,
CastleGLUtils, CastleColors, CastleLog, CastleRenderContext;
{ global routines ------------------------------------------------------------ }
var
Background: TCastleRectangleControl;
procedure AssignCameraAndNavigation(const Target, Source: TCastleAutoNavigationViewport);
var
NavigationTypeStr: String;
begin
Target.NavigationType := Source.NavigationType;
if (Target.Navigation <> nil) and
(Source.Navigation <> nil) then
Target.Navigation.Assign(Source.Navigation)
else
if (Target.Navigation <> nil) or
(Source.Navigation <> nil) then
begin
WriteStr(NavigationTypeStr, Target.NavigationType);
WritelnLog('Both Source and Target have the same NavigationType, but only one of them has non-nil Navigation', [
NavigationTypeStr
]);
end;
Target.Camera.Assign(Source.Camera);
end;
procedure SetViewportsConfig(const Value: TViewportsConfig;
Window: TCastleWindow; MainViewport: TCastleAutoNavigationViewport);
procedure AddViewport(Viewport: TCastleViewport);
begin
Viewport.FullSize := false;
Window.Controls.InsertBackIfNotExists(Viewport);
end;
var
I: Integer;
OldValue: TViewportsConfig;
begin
if ViewportsConfig <> Value then
begin
OldValue := ViewportsConfig;
ViewportsConfig := Value;
MainViewport.FullSize := ViewportsConfig = vc1;
case ViewportsConfig of
vc1:
begin
{ make sure glViewport is also restored }
RenderContext.Viewport := Window.Rect; // TODO: This is most probably useless now?
for I := 0 to High(ExtraViewports) do
Window.Controls.Remove(ExtraViewports[I]);
end;
vc2Horizontal:
begin
AddViewport(ExtraViewports[0]);
Window.Controls.Remove(ExtraViewports[1]);
Window.Controls.Remove(ExtraViewports[2]);
{ Configure camera for newly appearing viewports }
if OldValue = vc1 then
AssignCameraAndNavigation(ExtraViewports[0], MainViewport);
end;
vc4:
begin
AddViewport(ExtraViewports[0]);
AddViewport(ExtraViewports[1]);
AddViewport(ExtraViewports[2]);
{ Configure camera for newly appearing viewports }
case OldValue of
vc1:
begin
AssignCameraAndNavigation(ExtraViewports[0], MainViewport);
AssignCameraAndNavigation(ExtraViewports[1], MainViewport);
AssignCameraAndNavigation(ExtraViewports[2], MainViewport);
end;
vc2Horizontal:
begin
AssignCameraAndNavigation(ExtraViewports[1], MainViewport);
AssignCameraAndNavigation(ExtraViewports[2], ExtraViewports[0]);
end;
else raise EInternalError.Create('ViewportsConfig OldValue was supposed to be <> new value?');
end;
end;
{$ifndef COMPILER_CASE_ANALYSIS}
else raise EInternalError.Create('ViewportsConfig?');
{$endif}
end;
{ Do this at the end, to make Background to most in the back.
Also, always remove first, and the InsertBack -- to again
make sure Background is in the back (instead of doing InsertBackIfNotExists). }
Window.Controls.Remove(Background);
if ViewportsConfig <> vc1 then
Window.Controls.InsertBack(Background);
ResizeViewports(Window, MainViewport);
end;
end;
procedure ResizeViewports(Window: TCastleWindow; MainViewport: TCastleViewport);
var
W, H: Cardinal;
begin
case ViewportsConfig of
vc1: ;
vc2Horizontal:
begin
W := Window.Width div 2;
H := Window.Height;
MainViewport.Left := 0;
MainViewport.Bottom := 0;
MainViewport.Width := W - 1;
MainViewport.Height := H;
ExtraViewports[0].Left := W + 1;
ExtraViewports[0].Bottom := 0;
ExtraViewports[0].Width := W - 1;
ExtraViewports[0].Height := H;
end;
vc4:
begin
W := Window.Width div 2;
H := Window.Height div 2;
MainViewport.Left := 0;
MainViewport.Bottom := H + 1;
MainViewport.Width := W - 1;
MainViewport.Height := H - 1;
ExtraViewports[0].Left := W + 1;
ExtraViewports[0].Bottom := H + 1;
ExtraViewports[0].Width := W - 1;
ExtraViewports[0].Height := H - 1;
ExtraViewports[1].Left := 0;
ExtraViewports[1].Bottom := 0;
ExtraViewports[1].Width := W - 1;
ExtraViewports[1].Height := H - 1;
ExtraViewports[2].Left := W + 1;
ExtraViewports[2].Bottom := 0;
ExtraViewports[2].Width := W - 1;
ExtraViewports[2].Height := H - 1;
end;
end;
end;
procedure SetNavigationType(const NewNavigationType: TUserNavigationType);
procedure CoreSetNavigationType(const Viewport: TCastleAutoNavigationViewport;
const Value: TUserNavigationType);
begin
case Value of
untExamine: Viewport.NavigationType := ntExamine;
untWalk: Viewport.NavigationType := ntWalk;
untFly: Viewport.NavigationType := ntFly;
unt2D: Viewport.Navigation := TCastle2DNavigation.Create(Viewport);
untNone: Viewport.NavigationType := ntNone;
{$ifndef COMPILER_CASE_ANALYSIS}
else raise EInternalError.Create('CoreSetNavigationType NavigationType?');
{$endif}
end;
end;
var
I: Integer;
begin
CoreSetNavigationType(MainViewport, NewNavigationType);
for I := 0 to High(ExtraViewports) do
CoreSetNavigationType(ExtraViewports[I], NewNavigationType);
end;
procedure InitializeViewports(ViewportClass: TViewportClass);
var
I: Integer;
begin
for I := 0 to High(ExtraViewports) do
begin
ExtraViewports[I] := ViewportClass.Create(nil);
// Move extra cameras to shared Items.
ExtraViewports[I].Items.Remove(ExtraViewports[I].Camera);
MainViewport.Items.Add(ExtraViewports[I].Camera);
ExtraViewports[I].Items := MainViewport.Items;
{ We will explicitly initialize camera and navigation.
This also prevents the AutoCamera mechanism from overriding
our camera set by AssignCameraAndNavigation. }
ExtraViewports[I].AutoCamera := false;
ExtraViewports[I].AutoNavigation := false;
end;
Background := TCastleRectangleControl.Create(nil);
Background.FullSize := true;
Background.Color := Gray;
end;
procedure ViewportsRender(const Container: TCastleContainer);
var
I: Integer;
const
Visible: array [TViewportsConfig] of Integer = (0, 1, 3);
begin
if ViewportsConfig <> vc1 then
Container.RenderControl(Background, Container.Rect);
for I := 0 to Visible[ViewportsConfig] - 1 do
Container.RenderControl(ExtraViewports[I], Container.Rect);
Container.RenderControl(MainViewport, Container.Rect);
end;
procedure ViewportsSetTransparent(const Transparent: Boolean);
var
I: Integer;
begin
MainViewport.Transparent := Transparent;
for I := 0 to High(ExtraViewports) do
ExtraViewports[I].Transparent := Transparent;
end;
procedure DoFinalization;
var
I: Integer;
begin
for I := 0 to High(ExtraViewports) do
FreeAndNil(ExtraViewports[I]);
FreeAndNil(Background);
end;
finalization
DoFinalization;
end.
|