Class TDrawableImage
Unit
Declaration
type TDrawableImage = class(TNoParameterlessContructor)
Description
Image that can be drawn. This image can be drawn in various rendering methods and events, e.g. inside overridden TCastleUserInterface.Render or from events TCastleWindow.OnRender or TCastleControl.OnRender.
See the manual about drawing using TDrawableImage
: https://castle-engine.io/manual_2d_ui_custom_drawn.php . See the description how to render 2D stuff using Castle Game Engine: https://castle-engine.io/how_to_render_2d .
We have a few (probably too many) ways to show a 2D image. Alternatives to TDrawableImage
are:
TCastleImagePersistent: Wraps TEncodedImage and
TDrawableImage
in a class that has comfortable published properties (so it can be edited in CGE editor, can be saved to CGE design files). Also adds caching.TCastleImageControl: UI control to draw an image, using TCastleImagePersistent. This will perform the drawing by itself.
TCastleScene: You can also load an image to TCastleScene.Load to create a rectangle displaying this texture, which can be rendered in 2D and 3D.
Hierarchy
- TObject
- TNoParameterlessContructor
- TDrawableImage
Overview
Nested Types
TColorTreatment = (...); |
Fields
nested const DefaultBlendingSourceFactor = bsSrcAlpha; |
|
nested const DefaultBlendingDestinationFactor = bdOneMinusSrcAlpha; |
|
nested const DefaultBlendingConstantColor: TVector4 = (X: 1; Y: 1; Z: 1; W: 1); |
Methods
constructor Create(const AImage: TEncodedImage; const ASmoothScaling: boolean; const AOwnsImage: boolean); overload; |
|
constructor Create(const AURL: string; const ASmoothScaling: boolean = true); overload; |
|
constructor Create(const AURL: string; const LoadAsClass: array of TEncodedImageClass; const ResizeToX: Cardinal = 0; const ResizeToY: Cardinal = 0; const Interpolation: TResizeInterpolation = riBilinear); overload; |
|
constructor Create(const AURL: string; const LoadAsClass: array of TEncodedImageClass; const ASmoothScaling: boolean); overload; |
|
destructor Destroy; override; |
|
function Rect: TRectangle; |
|
procedure Draw(const X, Y: Single); overload; |
|
procedure Draw(const X, Y, DrawWidth, DrawHeight: Single); overload; |
|
procedure Draw(const X, Y, DrawWidth, DrawHeight: Single; const ImageX, ImageY, ImageWidth, ImageHeight: Single); overload; |
|
procedure Draw(const Pos: TVector2Integer); overload; |
|
procedure Draw(const ScreenRect: TFloatRectangle); overload; |
|
procedure Draw(const ScreenRect, ImageRect: TFloatRectangle); overload; |
|
procedure Draw(const ScreenRect: TRectangle); overload; |
|
procedure Draw(const ScreenRect: TRectangle; const ImageX, ImageY, ImageWidth, ImageHeight: Single); overload; |
|
procedure Draw(const ScreenRect, ImageRect: TRectangle); overload; |
|
procedure Draw(ScreenRects, ImageRects: PFloatRectangleArray; const Count: Integer); overload; virtual; |
|
procedure DrawFlipped(const ScreenRect: TRectangle; const FlipHorizontal, FlipVertical: boolean); overload; |
|
procedure DrawFlipped(const ScreenRect: TFloatRectangle; const FlipHorizontal, FlipVertical: boolean); overload; |
|
procedure DrawFlipped(const ScreenRect: TFloatRectangle; ImageRect: TFloatRectangle; const FlipHorizontal, FlipVertical: boolean); overload; |
|
procedure Draw3x3(const X, Y, DrawWidth, DrawHeight: Single; const CornerTop, CornerRight, CornerBottom, CornerLeft: Single); overload; |
|
procedure Draw3x3(const X, Y, DrawWidth, DrawHeight: Single; const Corner: TVector4Integer); overload; |
|
procedure Draw3x3(const ScreenRect: TRectangle; const Corner: TVector4Integer); overload; |
|
procedure Draw3x3(const ScreenRect: TFloatRectangle; const Corner: TVector4Integer); overload; |
|
procedure Draw3x3(const ScreenRect: TFloatRectangle; const Corner: TVector4); overload; |
|
procedure Load(const AImage: TEncodedImage); virtual; |
|
procedure PrepareResources; |
|
function GetContents(const ImageClass: TCastleImageClass): TCastleImage; |
|
procedure RenderToImageBegin(const SetAndRestoreViewport: boolean = true); |
|
procedure RenderToImageEnd; |
|
procedure RenderToImageFree; |
|
procedure DrawFrom(const SourceImage: TDrawableImage; const DestinationRect, SourceRect: TFloatRectangle); |
Properties
property Width: Cardinal read FWidth; |
|
property Height: Cardinal read FHeight; |
|
property Alpha: TAutoAlphaChannel read FAlpha write FAlpha
default acAuto; |
|
property BlendingSourceFactor: TBlendingSourceFactor
read FBlendingSourceFactor
write FBlendingSourceFactor
default DefaultBlendingSourceFactor; |
|
property BlendingDestinationFactor: TBlendingDestinationFactor
read FBlendingDestinationFactor
write FBlendingDestinationFactor
default DefaultBlendingDestinationFactor; |
|
property BlendingConstantColor: TVector4
read FBlendingConstantColor
write FBlendingConstantColor; |
|
property ScaleCorners: Single
read FScaleCorners write FScaleCorners default 1; |
|
property IgnoreTooLargeCorners: boolean
read FIgnoreTooLargeCorners write FIgnoreTooLargeCorners default true; |
|
property Color: TCastleColor read FColor write FColor; |
|
property SmoothScaling: boolean read FSmoothScaling write SetSmoothScaling; |
|
property ScalingPossible: boolean read FSmoothScaling write SetSmoothScaling; deprecated 'use SmoothScaling'; |
|
property CenterX: Single read GetCenterX write SetCenterX default 0.5; deprecated 'use RotationCenter'; |
|
property CenterY: Single read GetCenterY write SetCenterY default 0.5; deprecated 'use RotationCenter'; |
|
property RotationCenter: TVector2 read FRotationCenter write FRotationCenter; |
|
property Rotation: Single
read FRotation write FRotation default 0; |
|
property Clip: boolean read FClip write FClip; |
|
property ClipLine: TVector3 read FClipLine write FClipLine; |
|
property Texture: TGLTextureId read FTexture; |
|
property CustomShader: TGLSLProgram read FCustomShader write FCustomShader; |
|
property Image: TEncodedImage read FImage write SetImage; |
|
property OwnsImage: boolean read FOwnsImage write FOwnsImage; |
|
property URL: String read FURL write SetURL; |
|
property RepeatS: Boolean read FRepeatS write SetRepeatS default false; |
|
property RepeatT: Boolean read FRepeatT write SetRepeatT default false; |
Description
Nested Types
Fields
nested const DefaultBlendingSourceFactor = bsSrcAlpha; |
|
nested const DefaultBlendingDestinationFactor = bdOneMinusSrcAlpha; |
|
nested const DefaultBlendingConstantColor: TVector4 = (X: 1; Y: 1; Z: 1; W: 1); |
|
Methods
constructor Create(const AImage: TEncodedImage; const ASmoothScaling: boolean; const AOwnsImage: boolean); overload; |
|
Prepare image for drawing.
Parameters
Exceptions raised
|
constructor Create(const AURL: string; const ASmoothScaling: boolean = true); overload; |
|
Load image from disk, and prepare for drawing.
Parameters
|
constructor Create(const AURL: string; const LoadAsClass: array of TEncodedImageClass; const ResizeToX: Cardinal = 0; const ResizeToY: Cardinal = 0; const Interpolation: TResizeInterpolation = riBilinear); overload; |
|
Load image from disk, and prepare for drawing.
Parameters
Exceptions raised
|
constructor Create(const AURL: string; const LoadAsClass: array of TEncodedImageClass; const ASmoothScaling: boolean); overload; |
|
Load image from disk, and prepare for drawing.
Parameters
|
destructor Destroy; override; |
|
function Rect: TRectangle; |
|
Rectangle representing the inside of this image. Always (Left,Bottom) are zero, and (Width,Height) correspond to image sizes. |
procedure Draw(const X, Y: Single); overload; |
|
Draw the image as 2D on screen. The X, Y parameters determine where the left-bottom corner of the image will be placed (from 0 to size - 1). You should only use this inside TCastleUserInterface.Render. We require that current projection is 2D and lighting / depth test and such are off, which is the default state there. The image is drawn in 2D. In normal circumstances 1 pixel of the image is just placed over 1 pixel of the screen, and we draw the whole image. But you can use the overloaded versions where you specify DrawWidth, DrawHeight or ScreenRect, and then the indicated image portion is stretched over the designated screen area. Note: if you plan to use such stretching when drawing the image, then you usually want to create the image with SmoothScaling = Note that the image position (ImageX, ImageY) is specified like a texture coordinate. So (0, 0) is actually the left-bottom corner of the left-bottom pixel, and (Width,Height) is the right-top corner of the right-top pixel. That is why image position and sizes are floats, it makes sense to render partial pixels this way (make sure you have SmoothScaling = You can also flip the image horizontally or vertically, e.g. use ImageX = Width and ImageWidth = -Width to mirror image horizontally. Although it's usually more comfortable to flip using DrawFlipped methods. |
procedure Draw(const X, Y, DrawWidth, DrawHeight: Single); overload; |
|
procedure Draw(const X, Y, DrawWidth, DrawHeight: Single; const ImageX, ImageY, ImageWidth, ImageHeight: Single); overload; |
|
procedure Draw(const Pos: TVector2Integer); overload; |
|
procedure Draw(const ScreenRect: TFloatRectangle); overload; |
|
procedure Draw(const ScreenRect, ImageRect: TFloatRectangle); overload; |
|
procedure Draw(const ScreenRect: TRectangle); overload; |
|
procedure Draw(const ScreenRect: TRectangle; const ImageX, ImageY, ImageWidth, ImageHeight: Single); overload; |
|
procedure Draw(const ScreenRect, ImageRect: TRectangle); overload; |
|
procedure Draw(ScreenRects, ImageRects: PFloatRectangleArray; const Count: Integer); overload; virtual; |
|
procedure DrawFlipped(const ScreenRect: TRectangle; const FlipHorizontal, FlipVertical: boolean); overload; |
|
procedure DrawFlipped(const ScreenRect: TFloatRectangle; const FlipHorizontal, FlipVertical: boolean); overload; |
|
procedure DrawFlipped(const ScreenRect: TFloatRectangle; ImageRect: TFloatRectangle; const FlipHorizontal, FlipVertical: boolean); overload; |
|
procedure Draw3x3(const X, Y, DrawWidth, DrawHeight: Single; const CornerTop, CornerRight, CornerBottom, CornerLeft: Single); overload; |
|
Draw the image on the screen, divided into 3x3 parts for corners, sides, and inside. Just like the regular Draw method, this fills a rectangle on the 2D screen, with bottom-left corner in (X, Y), and size (DrawWidth, DrawHeight). The image is divided into 3 * 3 = 9 parts:
|
procedure Draw3x3(const X, Y, DrawWidth, DrawHeight: Single; const Corner: TVector4Integer); overload; |
|
procedure Draw3x3(const ScreenRect: TRectangle; const Corner: TVector4Integer); overload; |
|
procedure Draw3x3(const ScreenRect: TFloatRectangle; const Corner: TVector4Integer); overload; |
|
procedure Draw3x3(const ScreenRect: TFloatRectangle; const Corner: TVector4); overload; |
|
procedure Load(const AImage: TEncodedImage); virtual; |
|
Load the given image contents. Use this to efficiently replace the image contents on GPU. Updates the Width, Height, Alpha to correspond to new image. It is valid (and reasonable) to call this with AImage instance equal to the image instance that you used when creating this TDrawableImage. That's because TDrawableImage does not automatically update the image contents (once they were loaded to GPU) when you change the underlying TEncodedImage instance. It is valid to pass Do not call this method too often. This methos copies the image contents from TEncodedImage to GPU, which is not something you want to do every frame. Doing this occasionally, or during the loading/preparation stage, is OK. Doing this often, e.g. during every TCastleWindow.OnRender or TCastleWindow.OnUpdate, will hurt your performance. During the game, it's most efficient to treat the TDrawableImage instances as "constant" – avoid changing their contents, avoid creating new ones. |
procedure PrepareResources; |
|
Prepare all the possible resources. Since we may load OpenGL resources on-demand (if the TDrawableImage instance was created when OpenGL context was not available yet), this method can be used to force loading OpenGL resources now (if possible). This is only useful if you want to avoid slowdown when loading it later, and prefer to do the loading now. |
function GetContents(const ImageClass: TCastleImageClass): TCastleImage; |
|
Get the image contents from GPU to CPU. Exceptions raised
|
procedure RenderToImageBegin(const SetAndRestoreViewport: boolean = true); |
|
Start rendering to this image, using an efficient GPU method ("Framebuffer Object", FBO). Remember that the result lives only on GPU (the underlying TEncodedImage, in normal RAM memory, is not updated by this drawing in any way). Between RenderToImageBegin and RenderToImageEnd you can use various rendering methods, like
Calling the |
procedure RenderToImageEnd; |
|
procedure RenderToImageFree; |
|
procedure DrawFrom(const SourceImage: TDrawableImage; const DestinationRect, SourceRect: TFloatRectangle); |
|
Draw a SourceImage over this image. The drawing is done completely on GPU (using the RenderToImageBegin). This is a much faster alternative to drawing using TCastleImage.DrawFrom (that happens on CPU). The result "lives" only on GPU (the underlying TEncodedImage, in normal RAM memory, is not updated by this drawing in any way). If you use this only occasionally, it's nice to later free the resources by RenderToImageFree. |
Properties
property Width: Cardinal read FWidth; |
|
property Height: Cardinal read FHeight; |
|
property Alpha: TAutoAlphaChannel read FAlpha write FAlpha
default acAuto; |
|
How to render the alpha channel of the texture.
This is initialized based on loaded image class and data. This means that e.g. if you have smooth alpha channel in the image, it will be automatically rendered with nice blending. You can change the value of this property to force a specific rendering method, for example to force using alpha test or alpha blending regardless of alpha values. Or to disable alpha channel usage, because your image must always cover pixels underneath. Remember that you can also change the alpha channel existence at loading: use LoadAsClass parameters of LoadImage or TDrawableImage.Create to force your image to have/don't have an alpha channel (e.g. use LoadAsClass=[TRGBImage] to force RGB image without alpha, use LoadAsClass=[TRGBAlphaImage] to force alpha channel). |
property BlendingSourceFactor: TBlendingSourceFactor
read FBlendingSourceFactor
write FBlendingSourceFactor
default DefaultBlendingSourceFactor; |
|
Blending source factor, if we use blending (see Alpha). |
property BlendingDestinationFactor: TBlendingDestinationFactor
read FBlendingDestinationFactor
write FBlendingDestinationFactor
default DefaultBlendingDestinationFactor; |
|
Blending destination factor, if we use blending (see Alpha). |
property BlendingConstantColor: TVector4
read FBlendingConstantColor
write FBlendingConstantColor; |
|
For some blending source or destination factors (see BlendingSourceFactor, BlendingDestinationFactor), this constant color (or it's alpha value) take part in the blending equation. Used only if blending (see Alpha). By default, opaque white. |
property ScaleCorners: Single
read FScaleCorners write FScaleCorners default 1; |
|
In case of Draw3x3 the corners on screen are scaled by this amount. This is especially useful for UI scaling, see TCastleContainer.UIScaling and TCastleUserInterface.UIScale. |
property IgnoreTooLargeCorners: boolean
read FIgnoreTooLargeCorners write FIgnoreTooLargeCorners default true; |
|
Ignore (do not log to CastleLog) situations when Draw3x3 cannot work because corners are larger than draw area size. Set this to This is |
property Color: TCastleColor read FColor write FColor; |
|
Color to multiply the texture contents (all RGBA channels). By default this is White, which is (1, 1, 1, 1) as RGBA, and it means that texture contents are not actually modified. This case is also optimized when possible, to no multiplication will actually happen. When the color's alpha is < 1, then we automatically use blending, if Alpha is acAuto (regardless of the image alpha channel, in this case). Note that if you use TGrayscaleImage with TGrayscaleImage.TreatAsAlpha (which means that texture does not contain any RGB information), then only this color's RGB values determine the drawn RGB color. |
property ScalingPossible: boolean read FSmoothScaling write SetSmoothScaling; deprecated 'use SmoothScaling'; |
|
Warning: this symbol is deprecated: use SmoothScaling |
property RotationCenter: TVector2 read FRotationCenter write FRotationCenter; |
|
Center of rotation. Expressed as a fraction within the drawn ScreenRectangle, (0,0) means bottom-left corner, (1,1) means top-right corner. Default (0.5,0.5). |
property Clip: boolean read FClip write FClip; |
|
|
property ClipLine: TVector3 read FClipLine write FClipLine; |
|
If Clip, this is the line equation used to determine whether we clip the given pixel. Given a line (A, B, C) and pixel (x, y), the pixel is clipped (rejected) if The line equation works in texture coordinates. If the image is drawn complete on the screen, texture coordinates span from (0,0) at bottom-left to (1,1) top-right. For example |
property Texture: TGLTextureId read FTexture; |
|
Internal OpenGL texture identifier. You cannot free it (e.g. by glFreeTexture), and you cannot change it's parameters (because TDrawableImage assumes it controls all the texture parameters). For normal drawing, you do not need this (use Draw or other rendering routines in this class), but it may be useful to integrate with external rendering systems. |
property CustomShader: TGLSLProgram read FCustomShader write FCustomShader; |
|
Use a custom shader for image rendering. The shader must define the same uniform variables and attributes as the standard TDrawableImage shader, like viewport_size, vertex, tex_coord. See for example our default shader code: https://github.com/castle-engine/castle-engine/blob/master/src/images/opengl/glsl/source/image.vs https://github.com/castle-engine/castle-engine/blob/master/src/images/opengl/glsl/source/image.fs . See also example examples/images_videos/image_render_custom_shader.lpr . Note that some uniforms/attributes are only used in some cases. For example, when TDrawableImage.Color is White (1, 1, 1, 1), then we don't pass "color" uniform. When TDrawableImage.Clip is You can (and should, if possible) reuse a single TGLSLProgram instance for many TDrawableImage and TCastleImageControl instances (as |
property Image: TEncodedImage read FImage write SetImage; |
|
property OwnsImage: boolean read FOwnsImage write FOwnsImage; |
|
property URL: String read FURL write SetURL; |
|
|
property RepeatS: Boolean read FRepeatS write SetRepeatS default false; |
|
Repeat or clamp texture in S (width) or T (height) dimensions. |
property RepeatT: Boolean read FRepeatT write SetRepeatT default false; |
|
Generated by PasDoc 0.16.0.