| 1 |
"""This example shows how to use multiple textures in a shader with Pyglet.""" |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
from typing import TYPE_CHECKING |
| 5 |
|
| 6 |
import pyglet |
| 7 |
from pyglet.gl import glActiveTexture, GL_TEXTURE0, glBindTexture, glEnable, GL_BLEND, glBlendFunc, glDisable, \ |
| 8 |
glClearColor, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA |
| 9 |
|
| 10 |
if TYPE_CHECKING: |
| 11 |
from pyglet.image import Texture, AbstractImage |
| 12 |
from pyglet.graphics import Batch, Group |
| 13 |
from pyglet.graphics.shader import ShaderProgram |
| 14 |
|
| 15 |
vertex_source = """#version 150 core |
| 16 |
in vec3 translate; |
| 17 |
in vec4 colors; |
| 18 |
in vec3 tex_coords; |
| 19 |
in vec2 scale; |
| 20 |
in vec3 position; |
| 21 |
in float rotation; |
| 22 |
|
| 23 |
out vec4 vertex_colors; |
| 24 |
out vec3 texture_coords; |
| 25 |
|
| 26 |
uniform WindowBlock |
| 27 |
{ |
| 28 |
mat4 projection; |
| 29 |
mat4 view; |
| 30 |
} window; |
| 31 |
|
| 32 |
mat4 m_scale = mat4(1.0); |
| 33 |
mat4 m_rotation = mat4(1.0); |
| 34 |
mat4 m_translate = mat4(1.0); |
| 35 |
|
| 36 |
void main() |
| 37 |
{ |
| 38 |
m_scale[0][0] = scale.x; |
| 39 |
m_scale[1][1] = scale.y; |
| 40 |
m_translate[3][0] = translate.x; |
| 41 |
m_translate[3][1] = translate.y; |
| 42 |
m_translate[3][2] = translate.z; |
| 43 |
m_rotation[0][0] = cos(-radians(rotation)); |
| 44 |
m_rotation[0][1] = sin(-radians(rotation)); |
| 45 |
m_rotation[1][0] = -sin(-radians(rotation)); |
| 46 |
m_rotation[1][1] = cos(-radians(rotation)); |
| 47 |
|
| 48 |
gl_Position = window.projection * window.view * m_translate * m_rotation * m_scale * vec4(position, 1.0); |
| 49 |
|
| 50 |
vertex_colors = colors; |
| 51 |
texture_coords = tex_coords; |
| 52 |
} |
| 53 |
""" |
| 54 |
|
| 55 |
fragment_source = """#version 150 core |
| 56 |
in vec4 vertex_colors; |
| 57 |
in vec3 texture_coords; |
| 58 |
out vec4 final_colors; |
| 59 |
|
| 60 |
uniform sampler2D kitten_texture; |
| 61 |
uniform sampler2D pyglet_texture; |
| 62 |
|
| 63 |
void main() |
| 64 |
{ |
| 65 |
final_colors = texture(kitten_texture, texture_coords.xy) * texture(pyglet_texture, texture_coords.xy) * |
| 66 |
vertex_colors; |
| 67 |
} |
| 68 |
""" |
| 69 |
|
| 70 |
|
| 71 |
class MultiTextureSpriteGroup(pyglet.sprite.SpriteGroup): |
| 72 |
"""A sprite group that uses multiple active textures.""" |
| 73 |
|
| 74 |
def __init__(self, textures: dict[str, Texture], blend_src: int, blend_dest: int, |
| 75 |
program: ShaderProgram | None = None, parent: Group | None = None) -> None: |
| 76 |
"""Create a sprite group for multiple textures and samplers. |
| 77 |
|
| 78 |
All textures must share the same target type. |
| 79 |
|
| 80 |
Args: |
| 81 |
textures: |
| 82 |
A dictionary of textures, with the keys being the GLSL sampler name. |
| 83 |
blend_src: |
| 84 |
OpenGL blend source mode; for example, ``GL_SRC_ALPHA``. |
| 85 |
blend_dest: |
| 86 |
OpenGL blend destination mode; for example, ``GL_ONE_MINUS_SRC_ALPHA``. |
| 87 |
parent: |
| 88 |
Optional parent group. |
| 89 |
""" |
| 90 |
self.textures = textures |
| 91 |
texture = list(self.textures.values())[0] |
| 92 |
self.target = texture.target |
| 93 |
super().__init__(texture, blend_src, blend_dest, program, parent) |
| 94 |
|
| 95 |
def set_state(self) -> None: |
| 96 |
self.program.use() |
| 97 |
|
| 98 |
for idx, name in enumerate(self.textures): |
| 99 |
self.program[name] = idx |
| 100 |
|
| 101 |
for i, texture in enumerate(self.textures.values()): |
| 102 |
glActiveTexture(GL_TEXTURE0 + i) |
| 103 |
glBindTexture(self.target, texture.id) |
| 104 |
|
| 105 |
glEnable(GL_BLEND) |
| 106 |
glBlendFunc(self.blend_src, self.blend_dest) |
| 107 |
|
| 108 |
def unset_state(self) -> None: |
| 109 |
glDisable(GL_BLEND) |
| 110 |
self.program.stop() |
| 111 |
glActiveTexture(GL_TEXTURE0) |
| 112 |
|
| 113 |
def __repr__(self) -> str: |
| 114 |
return f'{self.__class__.__name__}({self.texture}-{self.texture.id})' |
| 115 |
|
| 116 |
def __eq__(self, other: object | Group | MultiTextureSpriteGroup) -> bool: |
| 117 |
return (other.__class__ is self.__class__ and |
| 118 |
self.program is other.program and |
| 119 |
self.textures == other.textures and |
| 120 |
self.blend_src == other.blend_src and |
| 121 |
self.blend_dest == other.blend_dest) |
| 122 |
|
| 123 |
def __hash__(self) -> int: |
| 124 |
return hash((id(self.parent), |
| 125 |
id(self.textures), |
| 126 |
self.blend_src, self.blend_dest)) |
| 127 |
|
| 128 |
|
| 129 |
class MultiTextureSprite(pyglet.sprite.Sprite): |
| 130 |
"""An example of a Sprite that can utilize multiple textures for an effect.""" |
| 131 |
group_class = MultiTextureSpriteGroup |
| 132 |
|
| 133 |
def __init__(self, |
| 134 |
images: dict[str, AbstractImage], |
| 135 |
x: float = 0, y: float = 0, z: float = 0, |
| 136 |
blend_src: int = GL_SRC_ALPHA, |
| 137 |
blend_dest: int = GL_ONE_MINUS_SRC_ALPHA, |
| 138 |
batch: Batch | None = None, |
| 139 |
group: Group | None = None, |
| 140 |
subpixel: bool = False, |
| 141 |
program: ShaderProgram | None = None) -> None: |
| 142 |
"""Create a Sprite instance. |
| 143 |
|
| 144 |
Args: |
| 145 |
images: |
| 146 |
A dictionary of images, with the keys being the GLSL sampler name. |
| 147 |
x: |
| 148 |
X coordinate of the sprite. |
| 149 |
y: |
| 150 |
Y coordinate of the sprite. |
| 151 |
z: |
| 152 |
Z coordinate of the sprite. |
| 153 |
blend_src: |
| 154 |
OpenGL blend source mode. The default is suitable for |
| 155 |
compositing sprites drawn from back-to-front. |
| 156 |
blend_dest: |
| 157 |
OpenGL blend destination mode. The default is suitable for |
| 158 |
compositing sprites drawn from back-to-front. |
| 159 |
batch: |
| 160 |
Optional batch to add the sprite to. |
| 161 |
group: |
| 162 |
Optional parent group of the sprite. |
| 163 |
subpixel: |
| 164 |
Allow floating-point coordinates for the sprite. By default, |
| 165 |
coordinates are restricted to integer values. |
| 166 |
program: |
| 167 |
A specific shader program to initialize the sprite with. By default, a pre-made shader will be chosen |
| 168 |
based on the texture type passed. |
| 169 |
""" |
| 170 |
|
| 171 |
self.textures: dict[str, Texture] = {name: img.get_texture() for name, img in images.items()} |
| 172 |
assert all(tex.target for tex in self.textures.values()) is True, "All textures need to be the same target." |
| 173 |
|
| 174 |
|
| 175 |
super().__init__(list(self.textures.values())[0], x, y, z, blend_src, blend_dest, batch, group, subpixel, |
| 176 |
program) |
| 177 |
|
| 178 |
def get_sprite_group(self) -> MultiTextureSpriteGroup: |
| 179 |
return self.group_class(self.textures, self._blend_src, self._blend_dest, self._program, self._user_group) |
| 180 |
|
| 181 |
|
| 182 |
window = pyglet.window.Window() |
| 183 |
|
| 184 |
|
| 185 |
pyglet.resource.path = ['../resources'] |
| 186 |
pyglet.resource.reindex() |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
pyglet_image = pyglet.resource.image("pyglet.png", atlas=False) |
| 191 |
kitten_image = pyglet.resource.image("kitten.jpg", atlas=False) |
| 192 |
|
| 193 |
window.set_size(kitten_image.width, kitten_image.height) |
| 194 |
|
| 195 |
|
| 196 |
batch = pyglet.graphics.Batch() |
| 197 |
|
| 198 |
|
| 199 |
multi_vert_shader = pyglet.graphics.shader.Shader(vertex_source, 'vertex') |
| 200 |
multi_frag_shader = pyglet.graphics.shader.Shader(fragment_source, 'fragment') |
| 201 |
|
| 202 |
|
| 203 |
multitex_shader_program = pyglet.graphics.shader.ShaderProgram(multi_vert_shader, multi_frag_shader) |
| 204 |
|
| 205 |
|
| 206 |
shader_images = { |
| 207 |
"kitten_texture": kitten_image, |
| 208 |
"pyglet_texture": pyglet_image, |
| 209 |
} |
| 210 |
|
| 211 |
sprite = MultiTextureSprite(shader_images, |
| 212 |
x=0, |
| 213 |
y=0, |
| 214 |
batch=batch, |
| 215 |
program=multitex_shader_program) |
| 216 |
|
| 217 |
glClearColor(1.0, 1.0, 1.0, 1.0) |
| 218 |
|
| 219 |
|
| 220 |
@window.event |
| 221 |
def on_draw(): |
| 222 |
window.clear() |
| 223 |
batch.draw() |
| 224 |
|
| 225 |
|
| 226 |
pyglet.app.run() |