Class TCastleRenderUnlitMesh

Unit

Declaration

type TCastleRenderUnlitMesh = class(TObject)

Description

Render a set of vertexes, with optional indexes. Can be used multiple times, to render the same vertexes many times.

This is a deliberately simple and thin wrapper over creating and drawing a bunch of vertexes on modern GPUs. E.g. on OpenGL(ES) it creates 1 VBO (vertex buffer object) for position data and optional 1 VBO for indexes (if SetIndexes is ever called).

Make sure all usage is within the same rendering context. This has to be created and destroyed while the OpenGL(ES) context is active.

This is suitable to render only trivial unlit (or invisible) meshes. This is not a full-featured mesh renderer. Some deliberate limitations / hardcoded assumptions to keep this class simple:

  • It doesn't define any per-vertex attributes other than vertex positions.

  • If it uses indexes, it always uses 16-bit indexes, not 32-bit. So it works even with OpenGLES 2.0 (not 3.0) or WebGL 1.0.

  • Always passes vectors as 4D (in homogeneous coordinates). 4D coordinates are useful e.g. for shadow volume quads.

  • It follows various current RenderContext state, like TRenderContext.DepthTest, TRenderContext.LineWidth – it doesn't control this state.

For a full-featured mesh rendering, just use TCastleScene, and construct your mesh as TIndexedTriangleSetNode or TIndexedFaceSetNode, see https://castle-engine.io/viewport_and_scenes_from_code#_building_a_mesh_using_code .

The things you really have to set before rendering are:

  • Vertexes, using SetVertexes.

  • Matrix (combined projection * camera * model transformation) ModelViewProjection.

  • That's it, the rest has sensible defaults. You can just call Render.

For shading this uses a simple shader:

  • If UseColor (default True) then the shader writes Color to the color buffer. Effectively this makes unlit rendering.

    Note: This class does not set up blending if Color alpha is less than 1. If you want blending, set it yourself using TRenderContext.BlendingEnable and disable using TRenderContext.BlendingDisable.

  • If UseColor is False then the shader writes undefined value to the color buffer.

    But it still writes proper values to depth and stencil.

    This is esp. useful when you render things with color buffer disabled (using RenderContext.ColorChannels := []). The rendering may still be useful, e.g. to fill depth buffer (for shadow maps), stencil buffer, or define shape for GPU occlusion query.

    Note: This class does not configure RenderContext.ColorChannels. It is up to you to decide how to hide from user the undefined effect this has on the color buffer.

Example usage:

SavedDepthTest := RenderContext.DepthTest;
RenderContext.DepthTest := true;

Mesh := TCastleRenderUnlitMesh.Create(true);
try
  Mesh.Color := Yellow;
  Mesh.ModelViewProjection := RenderContext.ProjectionMatrix * RenderingCamera.CurrentMatrix;
  Mesh.SetVertexes([
    Vector3(0, 0, 0, 1),
    Vector3(10, 0, 0, 1),
    Vector3(0, 10, 0, 1)
  ], false);
  Mesh.Render(pmTriangles);
finally FreeAndNil(Mesh) end;

RenderContext.DepthTest := SavedDepthTest;

Hierarchy

  • TObject
  • TCastleRenderUnlitMesh

Overview

Fields

Public ModelViewProjection: TMatrix4;

Methods

Public constructor Create(const AUseColor: Boolean = true);
Public destructor Destroy; override;
Public procedure SetIndexes(const Indexes: array of UInt16); overload;
Public procedure SetIndexes(const Indexes: PUInt16; const IndexesCount: Cardinal); overload;
Public procedure SetVertexes(const Vertexes: array of TVector4; const UsageDynamic: Boolean); overload;
Public procedure SetVertexes(const Vertexes: TVector4List; const UsageDynamic: Boolean); overload;
Public procedure SetVertexes(const Vertexes: PVector4; const VertexesCount: Cardinal; const UsageDynamic: Boolean); overload;
Public procedure Render(const Mode: TPrimitiveMode);

Properties

Public property UseColor: Boolean read FUseColor write SetUseColor;
Public property Color: TCastleColor read FColor write FColor;

Description

Fields

Public ModelViewProjection: TMatrix4;

By default all zeros, you must set this before any rendering.

Methods

Public constructor Create(const AUseColor: Boolean = true);

This item has no description.

Public destructor Destroy; override;

This item has no description.

Public procedure SetIndexes(const Indexes: array of UInt16); overload;

Define indexes for all future rendering. Note: Passing indexes that do not exist in vertexes you define in SetVertexes may result in crashes, neither CGE nor rendering API necessarily check it.

Public procedure SetIndexes(const Indexes: PUInt16; const IndexesCount: Cardinal); overload;

This item has no description.

Public procedure SetVertexes(const Vertexes: array of TVector4; const UsageDynamic: Boolean); overload;

Set vertexes (points) to render.

UsageDynamic is an optimization hint, pass True if you plan to change this VBO often, e.g. always or almost-always before each Render you will send new vertexes with SetVertexes. Pass False if this VBO stays constant for a long time, and will be drawn many times (so you call SetVertexes once, then many times Render).

Public procedure SetVertexes(const Vertexes: TVector4List; const UsageDynamic: Boolean); overload;

This item has no description.

Public procedure SetVertexes(const Vertexes: PVector4; const VertexesCount: Cardinal; const UsageDynamic: Boolean); overload;

This item has no description.

Public procedure Render(const Mode: TPrimitiveMode);

This item has no description.

Properties

Public property UseColor: Boolean read FUseColor write SetUseColor;

Should we render with Color (if False, output color is undefined).

Public property Color: TCastleColor read FColor write FColor;

Color used for unlit rendering, if only UseColor is True. Opaque white by default.


Generated by PasDoc 0.16.0-snapshot.