Debian Codegraph @

src/base_rendering/castleglimages_savescreen.inc

raw
1 {%MainUnit castleglimages.pas}
2 {
3 Copyright 2001-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Part of CastleGLImages unit: saving screen. }
18
19 {$ifdef read_interface}
20
21 {$ifdef read_interface_type}
22 // type (continue type section from previous include file)
23 { Color buffer to grab, used by @link(SaveScreen_NoFlush). }
24 TColorBuffer = (
25 cbFront,
26 cbBack,
27 cbColorAttachment0
28 );
29
30 { Notes about saving images from cbFront buffer:
31
32 Don't do it. It just not defined what will be returned when you read from
33 the front buffer. When our OpenGL context is covered by some other window,
34 then glReadPixels *may* return pixels with contents of obscuring window.
35 It doesn't help to draw right before trying to save buffer contents,
36 reading from front buffer is just not reliable.
37
38 The only reliable way to save screen contents is to draw something to back
39 buffer and (without doing any swapbuffers) read it from cbBack buffer.
40 This is only possible if you have double-buffered window, of course.
41 }
42
43 {$endif read_interface_type}
44
45 {$ifdef read_interface_func}
46 { Save the current color buffer contents to image.
47
48 The suffix "NoFlush" is there to remind you that this
49 function grabs the @italic(current) buffer contents. Usually you want to
50 redraw the screen to the back buffer, and call this function to capture
51 back buffer @italic(before) swapping, since this is the only reliable
52 way to capture OpenGL screen.
53 Just use TCastleWindow.SaveScreen to do it automatically.
54
55 Version with ImageClass can save to any image format from PixelsImageClasses.
56
57 Version with TCastleImage instance just uses this instance to save the image.
58 You must pass here already created TCastleImage instance, it's class,
59 Width and Height will be used when saving.
60
61 @raises(EImageClassNotSupportedForOpenGL When Image class is not supported
62 by OpenGL.)
63
64 @groupBegin }
65 function SaveScreen_NoFlush(
66 const Rect: TRectangle; const ReadBuffer: TColorBuffer): TRGBImage; overload;
67
68 function SaveScreen_NoFlush(const ImageClass: TCastleImageClass;
69 const Rect: TRectangle; const ReadBuffer: TColorBuffer): TCastleImage; overload;
70
71 procedure SaveScreen_NoFlush(const Image: TCastleImage;
72 const Left, Bottom: Integer; const ReadBuffer: TColorBuffer); overload;
73 { @groupEnd }
74
75 { Captures current screen as a TDrawableImage instance, ready to be drawn on 2D screen. }
76 function SaveScreenToGL_NoFlush(const Rect: TRectangle;
77 const ReadBuffer: TColorBuffer;
78 const SmoothScaling: boolean = false): TDrawableImage;
79
80 { Save depth buffer contents. }
81 function SaveScreenDepth_NoFlush(const Rect: TRectangle): TGrayscaleImage;
82
83 {$endif read_interface_func}
84
85 {$endif read_interface}
86
87 {$ifdef read_implementation}
88
89 const
90 ColorBufferGL: array [TColorBuffer] of TGLenum = (
91 GL_FRONT,
92 GL_BACK,
93 GL_COLOR_ATTACHMENT0
94 );
95
96 { This is the basis for all other SaveScreen* functions below. }
97 procedure SaveScreen_NoFlush(const Image: TCastleImage;
98 const Left, Bottom: Integer; const ReadBuffer: TColorBuffer);
99
100 procedure ReadPixels(const Image: TCastleImage);
101 begin
102 BeforePackImage(Image);
103 try
104 SetReadBuffer(ColorBufferGL[ReadBuffer]);
105 glReadPixels(Left, Bottom, Image.Width, Image.Height, ImageGLFormat(Image),
106 ImageGLType(Image), Image.RawPixels);
107 finally AfterPackImage(Image) end;
108 end;
109
110 {$ifdef OpenGLES}
111
112 { Under OpenGLES, the only type+format combination that is guaranteed
113 to work is GL_RGBA + GL_UNSIGNED_BYTE,
114 see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glReadPixels.xml .
115 So grab image to TRGBAlphaImage, and later convert it as necessary. }
116
117 var
118 ImageRGBA: TRGBAlphaImage;
119 begin
120 if Image is TRGBAlphaImage then
121 ReadPixels(Image) else
122 begin
123 ImageRGBA := TRGBAlphaImage.Create(Image.Width, Image.Height, Image.Depth);
124 try
125 ReadPixels(ImageRGBA);
126 Image.Assign(ImageRGBA);
127 finally FreeAndNil(ImageRGBA) end;
128 end;
129 {$else}
130 begin
131 ReadPixels(Image);
132 {$endif}
133 end;
134
135 function SaveScreen_NoFlush(const ImageClass: TCastleImageClass;
136 const Rect: TRectangle; const ReadBuffer: TColorBuffer): TCastleImage;
137 begin
138 Result := ImageClass.Create(Rect.Width, Rect.Height);
139 try
140 Result.URL := 'save-screen';
141 SaveScreen_NoFlush(Result, Rect.Left, Rect.Bottom, ReadBuffer);
142 except Result.Free; raise end;
143 end;
144
145 function SaveScreen_NoFlush(const Rect: TRectangle;
146 const ReadBuffer: TColorBuffer): TRGBImage;
147 begin
148 Result := SaveScreen_NoFlush(TRGBImage, Rect, ReadBuffer) as TRGBImage;
149 end;
150
151 function SaveScreenToGL_NoFlush(const Rect: TRectangle;
152 const ReadBuffer: TColorBuffer;
153 const SmoothScaling: boolean): TDrawableImage;
154 var
155 ScreenImage: TRGBImage;
156 begin
157 ScreenImage := SaveScreen_NoFlush(Rect, ReadBuffer);
158 Result := TDrawableImage.Create(ScreenImage, SmoothScaling, true);
159 end;
160
161 function SaveScreenDepth_NoFlush(const Rect: TRectangle): TGrayscaleImage;
162 begin
163 Result := TGrayscaleImage.Create(Rect.Width, Rect.Height);
164 try
165 BeforePackImage(Result);
166 try
167 glReadPixels(Rect.Left, Rect.Bottom, Rect.Width, Rect.Height, GL_DEPTH_COMPONENT,
168 ImageGLType(Result), Result.RawPixels);
169 finally
170 AfterPackImage(Result);
171 end;
172 except
173 FreeAndNil(Result);
174 raise;
175 end;
176 end;
177
178 {$endif read_implementation}