Skip to content

Commit

Permalink
Upgrade CGE usage
Browse files Browse the repository at this point in the history
  • Loading branch information
michaliskambi committed Nov 27, 2024
1 parent 137c472 commit 1c66801
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
1 change: 1 addition & 0 deletions CastleEngineManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
<include path="README.md" />
<include path="test.sh" />
</package>
<data exists="false" />
</project>
49 changes: 28 additions & 21 deletions rayhunter.lpr
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
CastleImages, CastleUtils,
CastleParameters, CastleURIUtils, CastleProjection, CastleRectangles,
X3DFields, X3DNodes, CastleInternalRays, CastleStringUtils, CastleApplicationProperties,
CastleTimeUtils, CastleFilesUtils, CastleLog,
CastleTimeUtils, CastleFilesUtils, CastleLog, CastleInternalTriangleOctree,

{ TODO: CastleViewport and CastleScene use OpenGL,
and we would prefer rayhunter to not depend on OpenGL
Expand Down Expand Up @@ -110,13 +110,16 @@ procedure PixelsMadeNotify(PixelsMadeCount: Cardinal; Data: Pointer);
end;
end;

function LightsForRaytracer(const Viewport: TCastleViewport): TLightInstancesList;
var
HI: TLightInstance;
type
TMyViewport = class(TCastleViewport)
public
function BaseLightsForRaytracer: TLightInstancesList;
end;

function TMyViewport.BaseLightsForRaytracer: TLightInstancesList;
begin
Result := TLightInstancesList.Create;
if Viewport.HeadlightInstance(HI) then
Result.Add(HI);
InitializeGlobalLights(Result);
end;

const
Expand Down Expand Up @@ -175,8 +178,8 @@ function LightsForRaytracer(const Viewport: TCastleViewport): TLightInstancesLis
'Options can be actually anywhere on the command-line, mixed between' +nl+
'the required parameters shown in the above "usage" specification.' +nl+
'Accepted options are:' +nl+
HelpOptionHelp +nl+
VersionOptionHelp +nl+
OptionDescription('-h / --help', 'Print this help message and exit.') + NL +
OptionDescription('-v / --version', 'Print the version number and exit.') + NL +
' -p / --camera-pos POS.X POS.Y POS.Z ,' +nl+
' -d / --camera-dir DIR.X DIR.Y DIR.Z ,' +nl+
' -u / --camera-up UP.X UP.Y UP.Z' +nl+
Expand Down Expand Up @@ -208,7 +211,7 @@ function LightsForRaytracer(const Viewport: TCastleViewport): TLightInstancesLis
' --primary-samples-count COUNT' +nl+
' Set primary samples count (default 1)' +nl+
nl+
SCastleEngineProgramHelpSuffix('rayhunter', Version, true));
ApplicationProperties.Description);
Halt;
end;
12: begin
Expand All @@ -235,8 +238,9 @@ function LightsForRaytracer(const Viewport: TCastleViewport): TLightInstancesLis
ModelProjectionType: TProjectionType;
Viewpoint: TAbstractViewpointNode;
FieldOfView: TMFFloat;
Viewport: TCastleViewport;
Viewport: TMyViewport;
Stats: TStringList;
OctreeVisibleTriangles: TTriangleOctree;
begin
{ defaults for Projection }
Projection.ProjectionType := ptPerspective;
Expand Down Expand Up @@ -264,13 +268,15 @@ function LightsForRaytracer(const Viewport: TCastleViewport): TLightInstancesLis
SceneURL := Parameters[1]; Parameters.Delete(0);
OutImageURL := Parameters[1]; Parameters.Delete(0);

{ init some vars to nil values (to allow simple try..finally..end clause
{ inititialize some variables to nil values
(to allow simple try..finally..end clause
instead of nested try..try.. ... finally .. finally ...end) }
Scene := nil;
Image := nil;
Viewport := nil;
Stats := nil;
MyRayTracer := nil;
OctreeVisibleTriangles := nil;

try
{ read scene and build SceneOctree }
Expand All @@ -279,15 +285,13 @@ function LightsForRaytracer(const Viewport: TCastleViewport): TLightInstancesLis
Scene := TCastleScene.Create(nil);
Scene.Load(SceneURL);
Writeln('done.');
Writeln(Format('Scene contains %d triangles and %d vertices.',
[Scene.TrianglesCount(false),
Scene.VerticesCount(false)]));

{ calculate Scene.TriangleOctree }
Scene.Spatial := [ssVisibleTriangles];
Writeln(Format('Scene contains %d triangles and %d vertices.', [
Scene.TrianglesCount,
Scene.VerticesCount
]));

{ calculate Viewport (will be used for headlight in LightsForRaytracer) }
Viewport := TCastleViewport.Create(nil);
Viewport := TMyViewport.Create(nil);
Viewport.Items.MainScene := Scene;
Viewport.Items.Add(Scene);

Expand Down Expand Up @@ -358,15 +362,17 @@ function LightsForRaytracer(const Viewport: TCastleViewport): TLightInstancesLis
Projection.PerspectiveAnglesRad.Y := AdjustViewAngleRadToAspectRatio(
Projection.PerspectiveAnglesRad.X, ImageHeight/ImageWidth);

OctreeVisibleTriangles := CreateOctreeVisibleTrianglesForScene(Scene);

{ create MyRayTracer instance, set it's properties }
case RTKind of
rtkClassic:
begin
MyRayTracer := TClassicRayTracer.Create;
TClassicRayTracer(MyRayTracer).InitialDepth := RTDepth;
TClassicRayTracer(MyRayTracer).FogNode := Scene.FogStack.Top;
TClassicRayTracer(MyRayTracer).BaseLights := LightsForRaytracer(Viewport);
TClassicRayTracer(MyRayTracer).OwnsBaseLights := true;
TClassicRayTracer(MyRayTracer).GlobalLights := Viewport.BaseLightsForRaytracer;
TClassicRayTracer(MyRayTracer).OwnsGlobalLights := true;
end;
rtkPathTracer:
begin
Expand All @@ -379,7 +385,7 @@ function LightsForRaytracer(const Viewport: TCastleViewport): TLightInstancesLis
end;
end;
MyRayTracer.Image := Image;
MyRayTracer.Octree := Scene.InternalOctreeVisibleTriangles;
MyRayTracer.Octree := OctreeVisibleTriangles;
MyRayTracer.CamPosition := CamPos;
MyRayTracer.CamDirection := CamDir;
MyRayTracer.CamUp := CamUp;
Expand All @@ -397,6 +403,7 @@ function LightsForRaytracer(const Viewport: TCastleViewport): TLightInstancesLis

SaveImage(Image, OutImageURL);
finally
FreeAndNil(OctreeVisibleTriangles);
FreeAndNil(Scene);
FreeAndNil(Image);
FreeAndNil(Viewport);
Expand Down

0 comments on commit 1c66801

Please sign in to comment.