Debian Codegraph @

examples/opengl/opengl_scissor.py

raw symbols SCIP
1 """In some cases, you may want to restrict drawing to a specific part
2 of the Window. To accomplish this, you can use an OpenGL Scissor
3 area. In this example we define a custom Group that enforces this.
4 Any Sprites/Labels that are assigned to this Group will not be drawn
5 outside the specified area. Drag the mouse to move the position.
6 """
7
8 import pyglet
9
10 from pyglet.sprite import Sprite
11 from pyglet.gl import glEnable, glScissor, glDisable, GL_SCISSOR_TEST
12
13
14 window = pyglet.window.Window(width=500, height=500)
15 batch = pyglet.graphics.Batch()
16
17 label = pyglet.text.Label("Drag the mouse to move the scissor area.", x=5, y=5, batch=batch)
18
19
20 @window.event
21 def on_draw():
22 window.clear()
23 batch.draw()
24
25
26 ###################################################
27 # A custom Group class that enforces a Scissor area
28 ###################################################
29
30 class ScissorGroup(pyglet.graphics.Group):
31 """A Custom Group that defines a "Scissor" area.
32
33 If a Sprite/Label is in this Group, any parts of it that
34 fall outside the specified area will not be drawn.
35 NOTE: You should use the same exact group instance
36 for every object that will use the group, equal groups
37 will still be kept separate.
38
39 :Parameters:
40 `x` : int
41 The X coordinate of the Scissor area.
42 `x` : int
43 The X coordinate of the Scissor area.
44 `width` : int
45 The width of the Scissor area.
46 `height` : int
47 The height of the Scissor area.
48
49 """
50
51 def __init__(self, x, y, width, height, order=0, parent=None):
52 super().__init__(order, parent)
53 self.x, self.y = x, y
54 self.width, self.height = width, height
55
56 @property
57 def area(self):
58 return self.x, self.y, self.width, self.height
59
60 @area.setter
61 def area(self, area):
62 self.x, self.y, self.width, self.height = area
63
64 def set_state(self):
65 glEnable(GL_SCISSOR_TEST)
66 glScissor(int(self.x), int(self.y), self.width, self.height)
67
68 def unset_state(self):
69 glDisable(GL_SCISSOR_TEST)
70
71
72 ###################################################
73 # Create an instance of our Group, and some Sprites
74 ###################################################
75
76 # Create an instance of the ScissorGroup that defines the center of the window:
77 scissor_group = ScissorGroup(x=50, y=50, width=300, height=300)
78
79 # Create a bunch of Sprites, and assign them to our custom Group. Any parts
80 # of these Sprites that are outside the specified area will not be drawn.
81 sprites = []
82 img = pyglet.resource.image('pyglet.png')
83 for x in range(5):
84 for y in range(5):
85 sprite = Sprite(img, x*img.width, y*img.height, group=scissor_group, batch=batch)
86 sprites.append(sprite)
87
88
89 @window.event
90 def on_mouse_drag(x, y, dx, dy, *etc):
91 scissor_group.x += dx
92 scissor_group.y += dy
93
94
95 pyglet.app.run()