@@ -68,83 +68,81 @@ def draw_depth(self, texture, near, far, pos=(0.0, 0.0), scale=(1.0, 1.0)):
6868 def _init_texture2d_draw (self ):
6969 """Initialize geometry and shader for drawing FBO layers"""
7070 from demosys import context , geometry # noqa
71- from demosys .opengl import ShaderProgram # noqa
7271
7372 if not TextureHelper ._quad :
7473 TextureHelper ._quad = geometry .quad_fs ()
7574 # Shader for drawing color layers
76- src = [
77- "#version 330" ,
78- "#if defined VERTEX_SHADER" ,
79- "in vec3 in_position;" ,
80- " in vec2 in_uv;" ,
81- "out vec2 uv;" ,
82- "uniform vec2 offset;" ,
83- " uniform vec2 scale;" ,
84- "" ,
85- "void main() {" ,
86- " uv = in_uv;"
87- " gl_Position = vec4((in_position.xy + vec2(1.0, 1.0)) * scale + offset, 0.0, 1.0);" ,
88- "}" ,
89- "" ,
90- "#elif defined FRAGMENT_SHADER " ,
91- "out vec4 out_color;" ,
92- "in vec2 uv;" ,
93- "uniform sampler2D texture0;" ,
94- "void main() {" ,
95- " out_color = texture(texture0, uv);" ,
96- "}" ,
97- "#endif" ,
98- ]
99- program = ShaderProgram ( "fbo_shader" )
100- program . set_source ( " \n " . join ( src ))
101- program . prepare ()
102-
103- TextureHelper . _texture2d_shader = program
75+ TextureHelper . _texture2d_shader = context . ctx (). program (
76+ vertex_shader = """
77+ #version 330
78+
79+ in vec3 in_position;
80+ in vec2 in_uv;
81+ out vec2 uv;
82+ uniform vec2 offset;
83+ uniform vec2 scale;
84+
85+ void main() {
86+ uv = in_uv;
87+ gl_Position = vec4((in_position.xy + vec2(1.0, 1.0)) * scale + offset, 0.0, 1.0);
88+ }
89+ "" " ,
90+ fragment_shader = """
91+ #version 330
92+
93+ out vec4 out_color;
94+ in vec2 uv;
95+ uniform sampler2D texture0;
96+
97+ void main() {
98+ out_color = texture(texture0, uv);
99+ }
100+ """
101+ )
102+
104103 TextureHelper ._texture2d_sampler = self .ctx .sampler (
105104 filter = (moderngl .LINEAR , moderngl .LINEAR ),
106105 )
107106
108107 def _init_depth_texture_draw (self ):
109108 """Initialize geometry and shader for drawing FBO layers"""
110109 from demosys import context , geometry # noqa
111- from demosys .opengl import ShaderProgram # noqa
112110
113111 if not TextureHelper ._quad :
114112 TextureHelper ._quad = geometry .quad_fs ()
115113 # Shader for drawing depth layers
116- src = [
117- "#version 330" ,
118- "#if defined VERTEX_SHADER" ,
119- "in vec3 in_position;" ,
120- " in vec2 in_uv;" ,
121- "out vec2 uv;" ,
122- "uniform vec2 offset;" ,
123- " uniform vec2 scale;" ,
124- "" ,
125- "void main() {" ,
126- " uv = in_uv;"
127- " gl_Position = vec4((in_position.xy + vec2(1.0, 1.0)) * scale + offset, 0.0, 1.0);" ,
128- "}" ,
129- "" ,
130- "#elif defined FRAGMENT_SHADER " ,
131- "out vec4 out_color;" ,
132- "in vec2 uv;" ,
133- "uniform sampler2D texture0;" ,
134- "uniform float near;"
135- "uniform float far;"
136- "void main() {" ,
137- " float z = texture(texture0, uv).r;"
138- " float d = (2.0 * near) / ( far + near - z * (far - near));"
139- " out_color = vec4(d);" ,
140- "}" ,
141- "#endif" ,
142- ]
143- program = ShaderProgram ( "depth_shader" )
144- program . set_source ( " \n " . join ( src ))
145- program . prepare ()
146-
147- TextureHelper . _depth_shader = program
114+ TextureHelper . _depth_shader = context . ctx (). program (
115+ vertex_shader = """
116+ #version 330
117+
118+ in vec3 in_position;
119+ in vec2 in_uv;
120+ out vec2 uv;
121+ uniform vec2 offset;
122+ uniform vec2 scale;
123+
124+ void main() {
125+ uv = in_uv;
126+ gl_Position = vec4((in_position.xy + vec2(1.0, 1.0)) * scale + offset, 0.0, 1.0);
127+ }
128+ "" " ,
129+ fragment_shader = """
130+ #version 330
131+
132+ out vec4 out_color;
133+ in vec2 uv;
134+ uniform sampler2D texture0;
135+ uniform float near;
136+ uniform float far;
137+
138+ void main() {
139+ float z = texture(texture0, uv).r;
140+ float d = (2.0 * near) / (far + near - z * (far - near));
141+ out_color = vec4(d);
142+ }
143+ """
144+ )
145+
148146 TextureHelper ._depth_sampler = self .ctx .sampler (
149147 filter = (moderngl .LINEAR , moderngl .LINEAR ),
150148 compare_func = '' ,
0 commit comments