-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrendergroup.py
More file actions
61 lines (53 loc) · 1.95 KB
/
Copy pathrendergroup.py
File metadata and controls
61 lines (53 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import pyglet
from pyglet.gl import (
glActiveTexture,
glEnable,
glBindTexture,
glBlendFunc,
glDisable,
GL_TEXTURE0,
GL_BLEND,
GL_SRC_ALPHA,
GL_ONE_MINUS_SRC_ALPHA,
)
from pyglet.graphics import Group
#####################################################
# Define a custom `Group` to encapsulate OpenGL state
#####################################################
class RenderGroup(Group):
"""A Group that enables and binds a Texture and ShaderProgram.
RenderGroups are equal if their Texture and ShaderProgram
are equal.
"""
def __init__(self, texture, program, order=0, parent=None):
"""Create a RenderGroup.
:Parameters:
`texture` : `~pyglet.image.Texture`
Texture to bind.
`program` : `~pyglet.graphics.shader.ShaderProgram`
ShaderProgram to use.
`order` : int
Change the order to render above or below other Groups.
`parent` : `~pyglet.graphics.Group`
Parent group.
"""
super().__init__(order, parent)
self.texture = texture
self.program = program
def set_state(self):
glActiveTexture(GL_TEXTURE0)
glBindTexture(self.texture.target, self.texture.id)
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
self.program.use()
def unset_state(self):
glDisable(GL_BLEND)
def __hash__(self):
return hash((self.texture.target, self.texture.id, self.order, self.parent, self.program))
def __eq__(self, other):
return (self.__class__ is other.__class__ and
self.texture.target == other.texture.target and
self.texture.id == other.texture.id and
self.order == other.order and
self.program == other.program and
self.parent == other.parent)