Debian Codegraph @

examples/images_videos/image_render_custom_shader/image_render_custom_shader.dpr

raw
1 {
2 Copyright 2018-2023 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 { Render image with TCastleImageControl using custom GLSL shader.
17 See README.md for details. }
18
19 uses SysUtils,
20 CastleWindow, CastleControls, CastleGLShaders, CastleTimeUtils,
21 CastleFilesUtils, CastleUtils, CastleColors;
22
23 var
24 Window: TCastleWindow;
25 Image: TCastleImageControl;
26 Shader: TGLSLProgram;
27 LifeTime: TFloatTime;
28
29 procedure ApplicationInitialize;
30 var
31 Background: TCastleRectangleControl;
32 begin
33 // simple black background underneath
34 Background := TCastleRectangleControl.Create(Application);
35 Background.Color := Black;
36 Background.FullSize := true;
37 Window.Controls.InsertFront(Background);
38
39 Image := TCastleImageControl.Create(Application);
40 Image.URL := 'castle-data:/test_texture.png';
41 Image.Stretch := true;
42 Image.FullSize := true;
43 Window.Controls.InsertFront(Image);
44 end;
45
46 procedure WindowOpen(Container: TCastleContainer);
47 begin
48 { Create TGLSLProgram instance.
49
50 Note that the TGLSLProgram instance may only exist when OpenGL(ES) context
51 is open. So it's cleanest to create / destroy TGLSLProgram in
52
53 - Window.OnOpen / Window.OnClose events,
54 - or overridden TCastleUserInterface.GLContextOpen / TCastleUserInterface.GLContextClose.
55
56 This approach will also work reliably on mobile platforms,
57 when the OpenGLES context may be lost and recreated in the middle
58 of the application too.
59
60 Note that Window.OnOpen occurs after Application.OnInitialize,
61 so Image (TCastleImageControl) is already created.
62 }
63
64 Shader := TGLSLProgram.Create;
65 Shader.AttachVertexShader(
66 'attribute vec2 vertex;' + NL +
67 'attribute vec2 tex_coord;' + NL +
68 'uniform vec2 viewport_size;' + NL +
69 'varying vec2 tex_coord_frag;' + NL +
70 'void main(void)' + NL +
71 '{' + NL +
72 ' gl_Position = vec4(vertex * 2.0 / viewport_size - vec2(1.0), 0.0, 1.0);' + NL +
73 ' tex_coord_frag = tex_coord;' + NL +
74 '}'
75 );
76 Shader.AttachFragmentShader(
77 'varying vec2 tex_coord_frag;' + NL +
78 'uniform sampler2D image_texture;' + NL +
79 'uniform float life_time;' + NL +
80 'void main(void)' + NL +
81 '{' + NL +
82 ' gl_FragColor = texture2D(image_texture, tex_coord_frag);' + NL +
83
84 // Uncomment to make simple edge detection
85 '#define IMAGE_WIDTH 100.0' + NL +
86 '#define IMAGE_HEIGHT 100.0' + NL +
87 ' gl_FragColor +=' + NL +
88 ' texture2D(image_texture, tex_coord_frag - vec2(0.0, 0.5/IMAGE_HEIGHT)) - texture2D(image_texture, tex_coord_frag + vec2(0.0, 0.5/IMAGE_HEIGHT)) +' + NL +
89 ' texture2D(image_texture, tex_coord_frag - vec2(0.5/IMAGE_WIDTH, 0.0)) - texture2D(image_texture, tex_coord_frag + vec2(0.5/IMAGE_WIDTH, 0.0));' + NL +
90
91 // Uncomment to animate rgb
92 ' gl_FragColor.rgb = gl_FragColor.rgb * fract(life_time * 0.5);' + NL +
93
94 '}'
95 );
96 Shader.Link;
97
98 Image.CustomShader := Shader;
99 end;
100
101 procedure WindowClose(Container: TCastleContainer);
102 begin
103 FreeAndNil(Shader);
104 Image.CustomShader := nil;
105 end;
106
107 procedure WindowUpdate(Container: TCastleContainer);
108 begin
109 LifeTime := LifeTime + Container.Fps.SecondsPassed;
110 Shader.Uniform('life_time').SetValue(LifeTime);
111 end;
112
113 begin
114 Window := TCastleWindow.Create(Application);
115
116 Application.OnInitialize := @ApplicationInitialize;
117 Window.OnOpen := @WindowOpen;
118 Window.OnClose := @WindowClose;
119 Window.OnUpdate := @WindowUpdate;
120
121 Window.OpenAndRun;
122 end.