Viewport

The TGorillaViewport is the primary container for Gorilla3D scenes. It replaces FireMonkey's standard TViewport3D and TForm3D components and serves as the main rendering surface for 3D content.

Important: Gorilla3D is not compatible with TForm3D and TViewport3D. Always use TGorillaViewport for Gorilla3D applications.

Creating a Viewport

Design-Time Creation

  1. Locate TGorillaViewport in the tool palette under the Gorilla3D section
  2. Drag and drop it onto your form
  3. The viewport will typically be set to align client by default

Runtime Creation

var
  Viewport: TGorillaViewport;
begin
  Viewport := TGorillaViewport.Create(Self);
  Viewport.Parent := Self;
  Viewport.Align := TAlignLayout.Client;
  Viewport.Color := TAlphaColors.Black;
end;

Rendering Pipeline

TGorillaViewport uses a sophisticated rendering pipeline based on Weighted-Blended-Order-Independent-Transparency (WBOIT). The pipeline processes scenes through multiple stages:

Pipeline Stages

  1. Pre-processing Render Passes - Custom render passes executed before main rendering
  2. Main Render Pass
    • Opaque objects rendering
    • Transparent objects rendering (WBOIT)
    • Emissive blurring
    • Voxelization (for global illumination)
    • Global illumination calculation
    • Anti-aliasing (FXAA)
    • Final compositing
  3. Post-processing Render Passes - Custom render passes executed after main rendering

Renderer Types

The viewport supports two renderer types:

Forward Renderer (Recommended)

Viewport.RendererType := TRendererType.ForwardRenderer;

The default renderer using WBOIT with multiple framebuffers and textures. Provides:

  • Order-independent transparency
  • Proper translucent object rendering
  • Full support for Gorilla3D default materials
  • Advanced effects (emissive blur, global illumination, etc.)

Legacy Renderer

Viewport.RendererType := TRendererType.LegacyRenderer;

A simple renderer with a single framebuffer. Use this when:

  • You need maximum compatibility with FMX materials
  • You're doing native OpenGL rendering
  • Advanced Gorilla3D features are not required

Note: Legacy renderer has limited support for Gorilla3D default materials.

Key Properties

Frame Rate Control

Fixed Frame Rate

// Enable fixed framerate rendering (default on Windows)
Viewport.UseFixedFrameRate := True;
Viewport.FixedFrameRate := 60;  // 60 FPS

When enabled, the viewport attempts to maintain a consistent frame rate. This is only available on Windows platforms.

VSync

Viewport.VSync := True;

Synchronizes rendering with the monitor's refresh rate, limiting the frame rate between FixedFrameRate and the monitor's maximum refresh rate.

Manual Rendering

Viewport.ManualRendering := True;

// Later, trigger rendering manually
Viewport.RenderManual();

Disables automatic rendering on property changes. Useful for:

  • Precise control over rendering timing
  • Performance optimization
  • Integration with custom render loops

Use with TGorillaTimer for consistent intervals:

var
  Timer: TGorillaTimer;
begin
  Timer := TGorillaTimer.Create;
  Timer.Interval := 16;  // ~60 FPS
  Timer.OnTimer := procedure(Sender: TObject)
    begin
      Viewport.RenderManual();
    end;
end;

Performance Optimization

Frustum Culling

Viewport.FrustumCulling := True;  // Default

Automatically culls objects outside the camera's view frustum. Significantly improves performance in large scenes.

Warning: May affect render passes using different view matrices.

Batching Updates

Viewport.BeginUpdate();
try
  // Modify multiple properties
  Viewport.FogDensity := 0.5;
  Viewport.FogStart := 10.0;
  Viewport.FogEnd := 100.0;
  Viewport.FogColor := TAlphaColors.White;
finally
  Viewport.EndUpdate();
end;

Batches multiple property changes into a single render instruction, improving performance.

Clipping Planes

Viewport.NearPlane := 0.1;    // Default
Viewport.FarPlane := 10000.0; // Default

Controls the rendering clipping distances. Objects closer than NearPlane or farther than FarPlane are not rendered.

Fog Configuration

Fog is a scene-wide atmospheric effect that requires TGorillaDefaultMaterialSource materials.

Enabling Fog

Viewport.Fog := True;
Viewport.FogKind := TGorillaFogKind.fkLinear;
Viewport.FogStart := 50.0;
Viewport.FogEnd := 200.0;
Viewport.FogColor := TAlphaColors.Lightgray;

Fog Modes

Mode Description Use Case
fkLinear Linear fog calculation Outdoor scenes, precise control
fkExp Exponential fog Atmospheric effects, realistic fog
fkExp2 Exponential squared Dense fog, underwater effects

Fog Parameters

// For linear fog
Viewport.FogStart := 50.0;   // Distance where fog starts
Viewport.FogEnd := 200.0;    // Distance where fog is maximum

// For exponential fog (fkExp, fkExp2)
Viewport.FogDensity := 0.05; // Density of fog

// Fog appearance
Viewport.FogColor := TAlphaColors.Lightgray;

Important: Fog only works with TGorillaDefaultMaterialSource materials, not standard FMX materials.

Rendering Effects

Saturation Control

Control color saturation for different rendering stages:

// Opaque objects saturation
Viewport.OpaqueSaturation := 0.0;  // 0 = no saturation effect

// Translucent objects saturation
Viewport.TranslucentSaturation := 1.5;

// Final composition saturation
Viewport.CompositeSaturation := 0.0;

Note: Saturation only affects ForwardRenderer and DeferredRenderer, not LegacyRenderer.

Emissive Blur

Viewport.EmissiveBlur := 3;  // 0 = disabled

Controls the intensity of emissive material blurring. Higher values create a stronger glow effect but significantly impact performance by increasing blur iterations and wideness.

Anti-Aliasing (FXAA)

Viewport.FXAA := 4;  // 0-8, 0 = disabled

Fast Approximate Anti-Aliasing. Higher values = smoother edges but lower performance. Alternative to MSAA.

Viewport.MSAA := TMSAASamples.Samples4X;  // Hardware MSAA

Fragment Depth Rendering

Viewport.FragmentDepthRendering := True;

Enables a pre-render pass that stores fragment depth for improved translucent rendering. Enhances visual quality but adds a render pass.

Tip: Disable if your scene has no transparent objects to improve performance.

Global Illumination

Global illumination provides realistic lighting with shadows and reflections through voxel-based rendering.

Availability: Only available in GAME_PACKAGE edition.

Configuration

// Set detail level (0-7)
Viewport.GlobalIllumDetail := 3;  // 0 = off, 7 = highest quality

// Adjust rendering quality
Viewport.GlobalIllumSoftness := 1.0;    // 0.0-5.0
Viewport.ShadowStrength := 0.4;         // 0.0-10.0
Viewport.ReflectionStrength := 0.5;     // 0.0-10.0

Detail Levels

Level Voxel Size GPU Requirement Min Texture3D Size
0 - - Global Illumination Off
1 32 Elder GPUs 256
2 64 Elder GPUs 512
3 128 Normal GPUs 1024
4 192 Normal GPUs 2048
5 256 Better GPUs 2048
6 384 High-performance GPUs 4096
7 512 High-performance GPUs 4096

Parameters

  • GlobalIllumSoftness: Controls how soft shadows and reflections appear (0.0 = sharp, 5.0 = very soft)
  • ShadowStrength: Controls the darkness of global-illumination shadows (0.0 = invisible, 10.0 = very dark). This applies only to the GI pass — it has no effect on Cascaded Shadow Maps; for CSM darkness use Viewport.Shadows.Strength (see Cascaded Shadow Maps).
  • ReflectionStrength: Controls reflection visibility (0.0 = invisible, 10.0 = very strong)

Advanced Features

Native OpenGL Rendering

You can implement custom OpenGL rendering via TDummy containers' OnRender events:

procedure TForm1.Dummy1Render(Sender: TObject; Context: TContext3D);
begin
  // Your custom OpenGL code here
  // You must manage transformation and projection matrices independently
  Context.SetMatrix(TMatrix3D.Identity);
  // ... custom rendering
end;

Graphics Library Selection

Modify the graphics library in your .dpr file before viewport initialization:

program MyApp;

uses
  Gorilla.Context.Types;

begin
  // Switch to OpenGL ES
  GORILLA_CONTEXT_GRAPHICS_SETTINGS := TGorillaGraphicsLibrary.OpenGLES;

  Application.Initialize;
  Application.CreateForm(TMainForm, MainForm);
  Application.Run;
end.

Available options (v1.1+):

  • TGorillaGraphicsLibrary.OpenGL
  • TGorillaGraphicsLibrary.OpenGLES

Design Camera

The viewport includes a built-in design camera for development:

// Use design camera (default at design-time)
Viewport.UsingDesignCamera := True;

// Access design camera
var DesignCam := Viewport.GetDesignCamera();

// Access camera controller
var Controller := Viewport.GetDesignCameraController();

Edit Mode

Viewport.EditMode := True;

Enables interactive object manipulation at design-time and runtime with the gizmo tool.

Gizmo Control

// Show gizmo for an object
Viewport.ShowGizmo(MyMesh);

// Change gizmo mode
Viewport.ChangeGizmoMode(0);  // 0=Translate, 1=Rotation, 2=Scale

// Check if active
if Viewport.IsGizmoActive() then
  ShowMessage('Gizmo is active');

// Hide gizmo
Viewport.HideGizmo();

Coordinate System Conversion

Screen to World

var
  ScreenPoint: TPointF;
  WorldPoint: TPoint3D;
begin
  ScreenPoint := PointF(100, 100);
  WorldPoint := Viewport.ScreenToWorld(ScreenPoint);
end;

World to Screen

var
  WorldPoint: TPoint3D;
  ScreenPoint: TPointF;
begin
  WorldPoint := Point3D(10, 0, 0);
  ScreenPoint := Viewport.WorldToScreen(WorldPoint);
end;

Object Selection

Ray Casting

var
  ClickedObject: TControl3D;
  MousePos: TPointF;
begin
  MousePos := PointF(X, Y);  // Mouse coordinates
  ClickedObject := Viewport.RayCastIntersect(MousePos);

  if Assigned(ClickedObject) then
    ShowMessage('Clicked: ' + ClickedObject.Name);
end;

Screenshots

var
  Screenshot: TBitmap;
begin
  Screenshot := Viewport.MakeScreenshot();
  try
    Screenshot.SaveToFile('screenshot.png');
  finally
    Screenshot.Free;
  end;
end;

Performance Monitoring

FPS Counter

// Get current FPS estimate
var CurrentFPS := Viewport.FPS;

Note: This measures Delphi rendering mechanism performance, not actual output FPS.

Diagnostics

// Enable detailed diagnostics
Viewport.DiagnosticsActive := True;

// Access diagnostics data
var Diag := Viewport.Diagnostics;
ShowMessage(Format(
  'FPS: %.2f, Draw Calls: %d, Objects: %d',
  [Diag.AverageFramesPerSecond, Diag.DrawCalls, Diag.RenderedObjects]
));

Diagnostic information includes:

  • Average frame time and FPS
  • Draw calls and instances
  • Vertex and index counts
  • Render passes and object counts
  • Display refresh rate

Important Considerations

Animation Frame Rate Limitation

FireMonkey's TAnimation base class limits animation frame rates to 30 FPS. This is a FireMonkey limitation, not a Gorilla3D limitation.

Translucent Rendering

Translucent rendering uses weighted color values requiring special GLSL calculations. The WBOIT algorithm provides order-independent transparency for correct rendering regardless of draw order.

Platform-Specific Features

  • HighRes: Primarily benefits Android platforms for high-DPI displays
  • Fixed Frame Rate: Only available on Windows (ALLOW_FIXEDFRAMERATE_RENDERING)

Material Compatibility

Feature Gorilla Materials FMX Materials
Fog ✓ Yes ✗ No
Forward Renderer ✓ Full Support ○ Limited
Legacy Renderer ○ Limited ✓ Full Support
WBOIT Transparency ✓ Yes ○ Partial

Best Practices

  1. Use BeginUpdate/EndUpdate when modifying multiple properties
  2. Enable FrustumCulling for large scenes
  3. Use ManualRendering with TGorillaTimer for consistent frame timing
  4. Disable FragmentDepthRendering if you have no transparent objects
  5. Start with GlobalIllumDetail level 3 and adjust based on performance
  6. Use ForwardRenderer unless you specifically need legacy compatibility
  7. Monitor Diagnostics during development to identify performance issues
  8. Keep EmissiveBlur low (0-3) unless you specifically need strong effects

See Also