class TextureWindow(pyglet.window.Window): ''' Concrete draw window. This uses the ShaderController to load and operate the shader at the path passed to the constructor. ''' def __init__(self, shader_path): ''' Load and attempt to run the shader at shader_path. ''' self.w = 512 self.h = 512 # Load shader code vspath = '%s.v.glsl' % shader_path fspath = '%s.f.glsl' % shader_path with io.open(vspath) as vstrm, io.open(fspath) as fstrm: vertexshader = ' '.join(vstrm) fragmentshader = ' '.join(fstrm) self.shader = Shader(vertexshader, fragmentshader) self.shader_controller = ShaderController(self.shader, shader_path) super(TextureWindow, self).__init__(caption=shader_path, width=self.w, height=self.h) self.create_key_help_labels() def create_key_help_labels(self): ''' Create the help labels to display overlaying the drawn shader ''' self.helpLabels = [] y = self.height for labelText in self.shader_controller.get_html_help(key): self.helpLabels.append(pyglet.text.HTMLLabel( "<font face='Courier New' color='white'>{}</font>".format(labelText), x=0, y=y, anchor_x='left', anchor_y='top')) y -= 20 def updateStatusLabels(self): self.statusLabels = [] y = 20 label = 0 for labelText in self.shader_controller.get_statuses(): # Create a new label if we need one (suddenly) if label >= len(self.statusLabels): self.statusLabels.append(pyglet.text.HTMLLabel("", x=0, y=y, anchor_x='left', anchor_y='top')) # Modify an existing label to give it status text self.statusLabels[label].text = "<font face='Courier New' color='white'>{}</font>".format(labelText) y += 20 label += 1 def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): self.shader_controller.mouse_drag(dx, dy) def on_mouse_scroll(self, x, y, scroll_x, scroll_y): self.shader_controller.mouse_scroll_y(scroll_y) def on_key_release(self, symbol, modifiers): self.shader_controller.binding_trigger(symbol) def saveFromShader(self): a = (gl.GLubyte * (4 * self.w * self.h))(0) # Save without any GUI elements self.drawGenerated() gl.glReadPixels(0, 0, self.w, self.h, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, a) image = pyglet.image.ImageData(self.w, self.h, 'RGBA', a) scriptPath = os.path.dirname(os.path.realpath(__file__)) filePath = scriptPath + "/TESTSAVE_" + time.strftime("%Y%m%d_%H%M%S") + ".png" print ("saved to {}".format(filePath)) image.save(filePath) def on_draw(self): self.drawGenerated() self.updateStatusLabels() self.drawGUI() def drawGenerated(self): gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(-1., 1., 1., -1., 0., 1.) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() self.shader.bind() self.shader_controller.set_uniforms() gl.glBegin(gl.GL_QUADS) gl.glVertex2i(-1, -1) gl.glTexCoord2i(-2, -2) gl.glVertex2f(1, -1) gl.glTexCoord2i(2, -2) gl.glVertex2i(1, 1) gl.glTexCoord2i(2, 2) gl.glVertex2i(-1, 1) gl.glTexCoord2i(-2, 2) gl.glEnd() self.shader.unbind() def drawGUI(self): gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, self.w, 0, self.h, -1, 1) for label in self.helpLabels: label.draw() for label in self.statusLabels: label.draw()
class TestSetUniforms(BaseCase): def setUp(self): self.shader = Mock(vertex_shader="", fragment_shader="") self.viewer = ShaderController(self.shader, "blank/blank_shader") # self.viewer.uniformi = create_autospec(self.viewer.uniformi) # self.viewer.uniformf = create_autospec(self.viewer.uniformf) def test_no_uniforms(self): self.viewer.set_uniforms() self.assertEqual(self.shader.uniformi.call_count, 0) self.assertEqual(self.shader.uniformf.call_count, 0) def test_int_uniform(self): self.viewer.bindings['an_int'] = {} self.viewer.bindings['an_int']['type'] = "int" self.viewer.bindings['an_int']['default'] = 1 self.viewer.set_uniforms() self.shader.uniformi.assert_called_once_with('an_int', 1) self.assertEqual(self.shader.uniformf.call_count, 0) def test_int_array_uniform(self): self.viewer.bindings['an_int_array'] = {} self.viewer.bindings['an_int_array']['type'] = "int" self.viewer.bindings['an_int_array']['default'] = [1, 2, 3, 4, 5] self.viewer.set_uniforms() self.shader.uniformi.assert_called_once_with('an_int_array', 1, 2, 3, 4, 5) self.assertEqual(self.shader.uniformf.call_count, 0) def test_ivec2_uniform(self): self.viewer.bindings['an_ivec2'] = {} self.viewer.bindings['an_ivec2']['type'] = "ivec2" self.viewer.bindings['an_ivec2']['default'] = [1, 2] self.viewer.set_uniforms() self.shader.uniformi.assert_called_once_with('an_ivec2', 1, 2) self.assertEqual(self.shader.uniformf.call_count, 0) def test_ivec3_uniform(self): self.viewer.bindings['an_ivec3'] = {} self.viewer.bindings['an_ivec3']['type'] = "ivec3" self.viewer.bindings['an_ivec3']['default'] = [1, 2, 3] self.viewer.set_uniforms() self.shader.uniformi.assert_called_once_with('an_ivec3', 1, 2, 3) self.assertEqual(self.shader.uniformf.call_count, 0) def test_ivec4_uniform(self): self.viewer.bindings['an_ivec4'] = {} self.viewer.bindings['an_ivec4']['type'] = "ivec4" self.viewer.bindings['an_ivec4']['default'] = [1, 2, 3, 4] self.viewer.set_uniforms() self.shader.uniformi.assert_called_once_with('an_ivec4', 1, 2, 3, 4) self.assertEqual(self.shader.uniformf.call_count, 0) def test_bool_uniform(self): self.viewer.bindings['a_bool'] = {} self.viewer.bindings['a_bool']['type'] = "bool" self.viewer.bindings['a_bool']['default'] = True self.viewer.set_uniforms() self.shader.uniformi.assert_called_once_with('a_bool', True) self.assertEqual(self.shader.uniformf.call_count, 0) def test_float_uniform(self): self.viewer.bindings['a_float'] = {} self.viewer.bindings['a_float']['type'] = "float" self.viewer.bindings['a_float']['default'] = 1.0 self.viewer.set_uniforms() self.assertEqual(self.shader.uniformi.call_count, 0) self.shader.uniformf.assert_called_once_with('a_float', 1.0) def test_float_array_uniform(self): self.viewer.bindings['a_float_array'] = {} self.viewer.bindings['a_float_array']['type'] = "float" self.viewer.bindings['a_float_array']['default'] = [1.0, 1.1, 1.2, 1.3, 1.4] self.viewer.set_uniforms() self.assertEqual(self.shader.uniformi.call_count, 0) self.shader.uniformf.assert_called_once_with('a_float_array', 1.0, 1.1, 1.2, 1.3, 1.4) def test_vec2_uniform(self): self.viewer.bindings['a_vec2'] = {} self.viewer.bindings['a_vec2']['type'] = "vec2" self.viewer.bindings['a_vec2']['default'] = [1.0, 1.1] self.viewer.set_uniforms() self.assertEqual(self.shader.uniformi.call_count, 0) self.shader.uniformf.assert_called_once_with('a_vec2', 1.0, 1.1) def test_vec3_uniform(self): self.viewer.bindings['a_vec3'] = {} self.viewer.bindings['a_vec3']['type'] = "vec3" self.viewer.bindings['a_vec3']['default'] = [1.0, 1.1, 1.2] self.viewer.set_uniforms() self.assertEqual(self.shader.uniformi.call_count, 0) self.shader.uniformf.assert_called_once_with('a_vec3', 1.0, 1.1, 1.2) def test_vec4_uniform(self): self.viewer.bindings['a_vec4'] = {} self.viewer.bindings['a_vec4']['type'] = "vec4" self.viewer.bindings['a_vec4']['default'] = [1.0, 1.1, 1.2, 1.3] self.viewer.set_uniforms() self.assertEqual(self.shader.uniformi.call_count, 0) self.shader.uniformf.assert_called_once_with('a_vec4', 1.0, 1.1, 1.2, 1.3)