Debian Codegraph @

examples/short_api_samples/save_screen_rgba/save_screen_rgba.dpr

raw
1 { TCastleContainer.SaveScreenRgba example.
2
3 This example relies on OpenGL color buffer that can store alpha information,
4 which you request by "Window.AlphaBits := 8".
5
6 An alternative approach, without needing "Window.AlphaBits := xxx",
7 is to initialize FBO with color framebuffer with alpha channel,
8 and render using this Fbo, like this:
9
10 Fbo := TGLRenderToTexture.Create(Window.Width, Window.Height);
11 Fbo.Buffer := tbNone;
12 Fbo.ColorBufferAlpha := true;
13 Fbo.GLContextOpen;
14 Fbo.RenderBegin;
15 ...
16 Fbo.RenderEnd;
17 }
18 uses SysUtils,
19 CastleWindow, CastleLog, CastleVectors, CastleUIControls, CastleScene,
20 CastleViewport, CastleKeysMouse, CastleImages, CastleCameras;
21
22 procedure Press(Container: TCastleContainer; const Event: TInputPressRelease);
23 var
24 Image: TRGBAlphaImage;
25 begin
26 if Event.IsKey(keyF5) then
27 begin
28 Image := Container.SaveScreenRgba;
29 try
30 SaveImage(Image, 'save_screen_rgba.png');
31 finally FreeAndNil(Image) end;
32 end;
33 end;
34
35 var
36 Window: TCastleWindow;
37 Viewport: TCastleViewport;
38 Scene: TCastleScene;
39 begin
40 Window := TCastleWindow.Create(Application);
41 // We must have buffer that stores alpha information, not only RGB
42 Window.AlphaBits := 8;
43 // Initially fill buffer with transparent white.
44 // When displaying, the window will be white, but it will be saved as transparent white.
45 Window.Container.BackgroundColor := Vector4(1, 1, 1, 0);
46 Window.OnPress := @Press;
47 Window.Open;
48
49 Viewport := TCastleViewport.Create(Application);
50 Viewport.FullSize := true;
51 Viewport.InsertBack(TCastleExamineNavigation.Create(Application));
52 Viewport.Transparent := true; // do not fill parent with Viewport.BackgroundColor
53 Window.Controls.InsertFront(Viewport);
54
55 Scene := TCastleScene.Create(Application);
56 Scene.Load('castle-data:/teapot.x3dv');
57 Scene.PreciseCollisions := true;
58 Scene.ProcessEvents := true;
59 Viewport.Items.Add(Scene);
60
61 // headlight
62 Viewport.Camera.Add(TCastleDirectionalLight.Create(Application));
63
64 // nice initial camera position
65 Viewport.AssignDefaultCamera;
66
67 Application.Run;
68 end.