| 1 |
{ |
| 2 |
Copyright 2018-2018 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Demo of drawing on GPU to TDrawableImage, using TDrawableImage.RenderToImageBegin |
| 17 |
and TDrawableImage.DrawFrom. } |
| 18 |
|
| 19 |
uses SysUtils, |
| 20 |
CastleGLImages, CastleWindow, CastleUIControls, CastleGLUtils, CastleColors, |
| 21 |
CastleFilesUtils, CastleImages, CastleRectangles, CastleVectors, CastleControls; |
| 22 |
|
| 23 |
var |
| 24 |
Window: TCastleWindow; |
| 25 |
|
| 26 |
SourceImage, DestImage: TDrawableImage; |
| 27 |
DestImageInitial, DestImageFinal: TCastleImage; |
| 28 |
SourceImageControl: TCastleImageControl; |
| 29 |
begin |
| 30 |
try |
| 31 |
Window := TCastleWindow.Create(Application); |
| 32 |
// uncomment these to test that viewport setting in RenderToImageBegin works |
| 33 |
// Window.Width := 100; |
| 34 |
// Window.Height := 100; |
| 35 |
Window.Open; |
| 36 |
Window.Container.BackgroundColor := Black; |
| 37 |
|
| 38 |
{ All of the TDrawableImage drawing must happen when OpenGL context is active, |
| 39 |
so after Window.Open. |
| 40 |
In a cross-platform application (https://castle-engine.io/manual_cross_platform.php), |
| 41 |
you would do this in Application.OnInitialize. } |
| 42 |
|
| 43 |
SourceImage := TDrawableImage.Create( |
| 44 |
//'castle-data:/2RGBA.png' // better alpha test |
| 45 |
'castle-data:/boss-preview.png' |
| 46 |
); |
| 47 |
|
| 48 |
DestImageInitial := TRGBAlphaImage.Create(800, 800); |
| 49 |
DestImageInitial.Clear(Yellow); |
| 50 |
DestImage := TDrawableImage.Create(DestImageInitial, true, true); |
| 51 |
|
| 52 |
{ Note that blending parameters on SourceImage control how the alpha |
| 53 |
is treated when drawing. } |
| 54 |
|
| 55 |
DestImage.DrawFrom(SourceImage, |
| 56 |
FloatRectangle(10, 10, 200, 200), FloatRectangle(SourceImage.Rect)); |
| 57 |
DestImage.DrawFrom(SourceImage, |
| 58 |
FloatRectangle(100, 10, 200, 200), FloatRectangle(SourceImage.Rect)); |
| 59 |
DestImage.DrawFrom(SourceImage, |
| 60 |
FloatRectangle(200, 10, 300, 300), FloatRectangle(SourceImage.Rect)); |
| 61 |
|
| 62 |
DestImage.RenderToImageBegin; |
| 63 |
DrawRectangle(Rectangle(500, 500, 250, 250), Blue); |
| 64 |
{ Note that blending is automatically used, because color has alpha < 1. |
| 65 |
Extra parameters to DrawCircle control how the alpha is treated. } |
| 66 |
DrawCircle(Vector2Integer(550, 550), 200, 200, Vector4(1, 0, 0, 0.5)); |
| 67 |
DestImage.RenderToImageEnd; |
| 68 |
|
| 69 |
SourceImageControl := TCastleImageControl.Create(Application); |
| 70 |
SourceImageControl.DrawableImage := DestImage; |
| 71 |
SourceImageControl.OwnsDrawableImage := false; |
| 72 |
Window.Controls.InsertFront(SourceImageControl); |
| 73 |
|
| 74 |
{ TDrawableImage.GetContents is not supported on OpenGLES. |
| 75 |
This makes sense, as the primary usage of TDrawableImage (and OpenGLES textures) |
| 76 |
is to load them to GPU, and then keep them on GPU, not get them back |
| 77 |
from GPU to normal memory. } |
| 78 |
if not Application.OpenGLES then |
| 79 |
begin |
| 80 |
{ Instead of using DestImage for drawing, you can also get it's contents |
| 81 |
back to normal (non-GPU) memory using DestImage.GetContents. |
| 82 |
This is reasonable if you have to save it back to disk |
| 83 |
(otherwise, you should avoid it, and work with GPU-only DestImage |
| 84 |
for maximum speed). } |
| 85 |
DestImageFinal := DestImage.GetContents(TRGBAlphaImage); |
| 86 |
try |
| 87 |
SaveImage(DestImageFinal, 'test.png'); |
| 88 |
finally FreeAndNil(DestImageFinal) end; |
| 89 |
end; |
| 90 |
|
| 91 |
Application.Run; |
| 92 |
finally |
| 93 |
FreeAndNil(SourceImage); |
| 94 |
FreeAndNil(DestImage); |
| 95 |
end; |
| 96 |
end. |