Debian Codegraph @

examples/opengl/minimal_shader.py

raw symbols SCIP
1 """Minimal ShaderProgram example.
2
3 This example shows how to use Shaders and ShaderPrograms in pyglet. This is a semi-minimal
4 example, in that it also goes over pyglet's `Group` and `Batch` objects for easy batched
5 rendering of GPU resources. The result of this example is a single textured quad, but the
6 basics shown here will scale easily.
7
8 The code shown here illustrates:
9 1. Compiling Shaders and linking a ShaderProgram.
10 2. Creating a custom `graphics.Group` to control GL state setting (for Batched rendering).
11 3. Using the `ShaderProgram.vertex_list_index` helper method to create GPU resources.
12
13 """
14 import pyglet
15
16 from pyglet.gl import (
17 glActiveTexture,
18 glBindTexture,
19 glEnable,
20 glBlendFunc,
21 glDisable,
22 GL_TEXTURE0,
23 GL_TRIANGLES,
24 GL_BLEND,
25 GL_SRC_ALPHA,
26 GL_ONE_MINUS_SRC_ALPHA,
27 )
28 from pyglet.graphics import Group
29 from pyglet.graphics.shader import Shader, ShaderProgram
30
31
32 ###################################
33 # Create a Window, and render Batch
34 ###################################
35 window = pyglet.window.Window()
36 batch = pyglet.graphics.Batch()
37
38 label = pyglet.text.Label("A minimal shader to display a textured quad.", x=5, y=5, batch=batch)
39
40
41 @window.event
42 def on_draw():
43 window.clear()
44 batch.draw()
45
46
47 ###############################
48 # Define a basic Shader Program
49 ###############################
50 _vertex_source = """#version 330 core
51 in vec2 position;
52 in vec3 tex_coords;
53 out vec3 texture_coords;
54
55 uniform WindowBlock
56 { // This UBO is defined on Window creation, and available
57 mat4 projection; // in all Shaders. You can modify these matrixes with the
58 mat4 view; // Window.view and Window.projection properties.
59 } window;
60
61 void main()
62 {
63 gl_Position = window.projection * window.view * vec4(position, 1, 1);
64 texture_coords = tex_coords;
65 }
66 """
67
68 _fragment_source = """#version 330 core
69 in vec3 texture_coords;
70 out vec4 final_colors;
71
72 uniform sampler2D our_texture;
73
74 void main()
75 {
76 final_colors = texture(our_texture, texture_coords.xy);
77 }
78 """
79
80 vert_shader = Shader(_vertex_source, 'vertex')
81 frag_shader = Shader(_fragment_source, 'fragment')
82 shader_program = ShaderProgram(vert_shader, frag_shader)
83
84
85 #####################################################
86 # Define a custom `Group` to encapsulate OpenGL state
87 #####################################################
88 class RenderGroup(Group):
89 """A Group that enables and binds a Texture and ShaderProgram.
90
91 RenderGroups are equal if their Texture and ShaderProgram
92 are equal.
93 """
94 def __init__(self, texture, program, order=0, parent=None):
95 """Create a RenderGroup.
96
97 :Parameters:
98 `texture` : `~pyglet.image.Texture`
99 Texture to bind.
100 `program` : `~pyglet.graphics.shader.ShaderProgram`
101 ShaderProgram to use.
102 `order` : int
103 Change the order to render above or below other Groups.
104 `parent` : `~pyglet.graphics.Group`
105 Parent group.
106 """
107 super().__init__(order, parent)
108 self.texture = texture
109 self.program = program
110
111 def set_state(self):
112 glActiveTexture(GL_TEXTURE0)
113 glBindTexture(self.texture.target, self.texture.id)
114 glEnable(GL_BLEND)
115 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
116 self.program.use()
117
118 def unset_state(self):
119 glDisable(GL_BLEND)
120
121 def __hash__(self):
122 return hash((self.texture.target, self.texture.id, self.order, self.parent, self.program))
123
124 def __eq__(self, other):
125 return (self.__class__ is other.__class__ and
126 self.texture.target == other.texture.target and
127 self.texture.id == other.texture.id and
128 self.order == other.order and
129 self.program == other.program and
130 self.parent == other.parent)
131
132
133 #########################################################
134 # Load a Texture, and create a VertexList from the Shader
135 #########################################################
136
137
138 def create_quad(x, y, texture):
139 x2 = x + texture.width
140 y2 = y + texture.height
141 return x, y, x2, y, x2, y2, x, y2
142
143
144 tex = pyglet.resource.texture('pyglet.png')
145 group = RenderGroup(tex, shader_program)
146 indices = (0, 1, 2, 0, 2, 3)
147
148 vertex_positions = create_quad(576, 296, tex)
149
150 # count, mode, indices, batch, group, *data
151 vertex_list = shader_program.vertex_list_indexed(4, GL_TRIANGLES, indices, batch, group,
152 position=('f', vertex_positions),
153 tex_coords=('f', tex.tex_coords))
154
155
156 #####################
157 # Enter the main loop
158 #####################
159 pyglet.app.run()