Debian Codegraph @

src/base_rendering/castleglimages_video.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: videos rendered in OpenGL. }
18
19 {$ifdef read_interface}
20 {$ifdef read_interface_type}
21 // type (continue type section from previous include file)
22 { Video as a sequence of OpenGL textures that can be easily played.
23 Use TGLVideo3D to have a list of normal OpenGL textures,
24 e.g. for rendering video as texture on free 3D objects.
25 Use TGLVideo2D to have a list of GUI textures (TDrawableImage),
26 e.g. for rendering video as simple 2D control. }
27 TGLVideo = class
28 private
29 FCount: Integer;
30 FTimeLoop: boolean;
31 FTimeBackwards: boolean;
32 FFramesPerSecond: Single;
33 FWidth, FHeight: Cardinal;
34 public
35 { Constructor that initializes video from TVideo class.
36
37 TVideo passed here must be already @link(TVideo.Loaded Loaded).
38
39 Note that this class doesn't descend
40 or keep reference to TVideo instance. The idea is that after
41 creating TGLVideo instance, you can often free original TVideo
42 instance (if you care only about playing the movie). This can
43 conserve memory greatly, as TVideo keeps all frames in the memory,
44 and so is rather memory-costly.
45 (Actually, TGLVideo itself may eat a lot of texture memory,
46 so be careful with large videos anyway.) }
47 constructor Create(Video: TVideo);
48
49 property Count: Integer read FCount;
50 function IndexFromTime(const Time: TFloatTime): Integer;
51
52 { See TVideo.FramesPerSecond. }
53 property FramesPerSecond: Single read FFramesPerSecond write FFramesPerSecond;
54
55 { See TVideo.TimeLoop. }
56 property TimeLoop: boolean read FTimeLoop write FTimeLoop;
57
58 { See TVideo.TimeBackwards. }
59 property TimeBackwards: boolean
60 read FTimeBackwards write FTimeBackwards;
61
62 { Duration of the movie, in seconds.
63 This is just the number of frames @link(Count) divided by FramesPerSecond. }
64 function Duration: Single;
65
66 property Width: Cardinal read FWidth;
67 property Height: Cardinal read FHeight;
68 end;
69
70 { Video expressed as a series of textures, to play as texture on any 3D object. }
71 TGLVideo3D = class(TGLVideo)
72 private
73 FItems: array of TGLTextureId;
74 public
75 constructor Create(Video: TVideo;
76 const Filter: TTextureFilter;
77 const Anisotropy: TGLfloat;
78 const Wrap: TTextureWrap2D;
79 const GUITexture: boolean);
80 destructor Destroy; override;
81
82 function GLTextureFromTime(const Time: TFloatTime): TGLTextureId;
83 end;
84
85 { Video expressed as a series of TDrawableImage, to play as 2D GUI control. }
86 TGLVideo2D = class(TGLVideo)
87 private
88 FItems: array of TDrawableImage;
89 public
90 constructor Create(Video: TVideo;
91 const SmoothScaling: boolean = true); overload;
92 constructor Create(const Url: String;
93 const SmoothScaling: boolean = true); overload;
94 constructor Create(const Url: String;
95 const ResizeToX: Cardinal = 0;
96 const ResizeToY: Cardinal = 0;
97 const Interpolation: TResizeInterpolation = riBilinear); overload;
98 destructor Destroy; override;
99
100 function DrawableImageFromTime(const Time: TFloatTime): TDrawableImage;
101 function GLImageFromTime(const Time: TFloatTime): TDrawableImage;
102 deprecated 'use DrawableImageFromTime';
103 end;
104 {$endif read_interface_type}
105
106 {$endif read_interface}
107
108 {$ifdef read_implementation}
109
110 { TGLVideo ------------------------------------------------------------------- }
111
112 constructor TGLVideo.Create(Video: TVideo);
113 begin
114 inherited Create;
115 Check(Video.Loaded, 'Video must be loaded before using TGLVideo.Create');
116 FCount := Video.Count;
117 FTimeLoop := Video.TimeLoop;
118 FTimeBackwards := Video.TimeBackwards;
119 FFramesPerSecond := Video.FramesPerSecond;
120 FWidth := Video.Width;
121 FHeight := Video.Height
122 end;
123
124 function TGLVideo.IndexFromTime(const Time: TFloatTime): Integer;
125 begin
126 Result := TVideo.FrameIndexFromTime(Time, Count, FramesPerSecond,
127 TimeLoop, TimeBackwards);
128 end;
129
130 function TGLVideo.Duration: Single;
131 begin
132 Result := Count / FramesPerSecond;
133 end;
134
135 { TGLVideo3D ----------------------------------------------------------------- }
136
137 constructor TGLVideo3D.Create(Video: TVideo;
138 const Filter: TTextureFilter; const Anisotropy: TGLfloat;
139 const Wrap: TTextureWrap2D; const GUITexture: boolean);
140 var
141 I: Integer;
142 begin
143 inherited Create(Video);
144
145 SetLength(FItems, Count);
146 for I := 0 to High(FItems) do
147 begin
148 FItems[I] := LoadGLTexture(Video.Items[I], Filter, Wrap, nil, GUITexture);
149 TexParameterMaxAnisotropy(GL_TEXTURE_2D, Anisotropy);
150 end;
151 end;
152
153 destructor TGLVideo3D.Destroy;
154 var
155 I: Integer;
156 begin
157 for I := 0 to High(FItems) do
158 glFreeTexture(FItems[0]);
159 inherited;
160 end;
161
162 function TGLVideo3D.GLTextureFromTime(const Time: TFloatTime): TGLTextureId;
163 begin
164 Result := FItems[IndexFromTime(Time)];
165 end;
166
167 { TGLVideo2D ----------------------------------------------------------------- }
168
169 constructor TGLVideo2D.Create(Video: TVideo;
170 const SmoothScaling: boolean = true);
171 var
172 I: Integer;
173 begin
174 inherited Create(Video);
175
176 SetLength(FItems, Count);
177 for I := 0 to High(FItems) do
178 FItems[I] := TDrawableImage.Create(Video.Items[I], SmoothScaling, false);
179 end;
180
181 constructor TGLVideo2D.Create(const Url: String;
182 const SmoothScaling: boolean = true);
183 var
184 Video: TVideo;
185 begin
186 Video := TVideo.Create;
187 try
188 Video.LoadFromFile(Url);
189 Create(Video, SmoothScaling);
190 finally FreeAndNil(Video) end;
191 end;
192
193 constructor TGLVideo2D.Create(const Url: String;
194 const ResizeToX: Cardinal;
195 const ResizeToY: Cardinal;
196 const Interpolation: TResizeInterpolation);
197 var
198 Video: TVideo;
199 begin
200 Video := TVideo.Create;
201 try
202 Video.LoadFromFile(Url, ResizeToX, ResizeToY, Interpolation);
203 Create(Video);
204 finally FreeAndNil(Video) end;
205 end;
206
207 destructor TGLVideo2D.Destroy;
208 var
209 I: Integer;
210 begin
211 for I := 0 to High(FItems) do
212 FreeAndNil(FItems[I]);
213 inherited;
214 end;
215
216 function TGLVideo2D.DrawableImageFromTime(const Time: TFloatTime): TDrawableImage;
217 begin
218 Result := FItems[IndexFromTime(Time)];
219 end;
220
221 function TGLVideo2D.GLImageFromTime(const Time: TFloatTime): TDrawableImage;
222 begin
223 Result := DrawableImageFromTime(Time);
224 end;
225
226 {$endif read_implementation}