Pyglet:如果我应用 TextureGroup,则不会绘制形状
Pyglet: shape not drawn if I apply a TextureGroup
我正在尝试使用通过 TextureGroup
应用的纹理贴图绘制一些形状:
from pathlib import Path
import pyglet
from pyglet import gl
BASE_PATH = Path(__file__).resolve().parent.parent
TEXTURE_PATH = BASE_PATH / "textures"
def load_texture(filename):
return pyglet.image.load(TEXTURE_PATH / filename).get_texture()
class HexWindow(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
textures = {
"desert": load_texture("hexmap-iso-desert.png"),
"forest": load_texture("hexmap-iso-forest.png"),
"hills": load_texture("hexmap-iso-hills.png"),
"mountains": load_texture("hexmap-iso-mountains.png"),
"ocean": load_texture("hexmap-iso-ocean.png"),
"plains": load_texture("hexmap-iso-plains.png"),
"swamp": load_texture("hexmap-iso-swamp.png"),
}
self.batch = pyglet.graphics.Batch()
self.texture_groups = {
name: pyglet.graphics.TextureGroup(texture)
for name, texture in textures.items()
}
self._shapes = [
pyglet.shapes.Circle(
batch=self.batch,
group=self.texture_groups["desert"],
segments=6,
x=0,
y=0,
radius=100,
color=(50, 225, 30),
),
pyglet.shapes.Circle(
batch=self.batch,
group=self.texture_groups["ocean"],
segments=6,
x=300,
y=300,
radius=100,
color=(225, 30, 30),
)
]
def on_draw(self):
gl.glClearColor(0, 0.3, 0.5, 0)
self.clear()
self.batch.draw()
if __name__ == "__main__":
_ = HexWindow(1280, 720)
pyglet.app.run()
如果我删除 group=self.texture_groups["ocean"],
args 到我的 Circle
s 然后我得到一个红色和绿色的十六进制。
但是当我应用纹理组时没有绘制任何东西。
虽然纹理有效,因为我还有一些其他类似的代码:
class TexturedSquare:
def __init__(self, width, height, xpos, ypos, texture):
self.xpos = xpos
self.ypos = ypos
self.angle = 30 # degrees
self.size = 2
self.texture = texture
x = width/2.0
y = height/2.0
self.vlist = pyglet.graphics.vertex_list(
4,
('v2f', [-x, -y, x, -y, -x, y, x, y]),
('t2f', [0, 0, 1, 0, 0, 1, 1, 1]),
)
def draw(self):
gl.glPushMatrix()
gl.glTranslatef(self.xpos, self.ypos, 0)
gl.glRotatef(self.angle, 0, 0, 1)
gl.glScalef(self.size, self.size, self.size)
gl.glEnable(self.texture.target)
gl.glActiveTexture(gl.GL_TEXTURE0)
gl.glBindTexture(self.texture.target, self.texture.id)
self.vlist.draw(gl.GL_TRIANGLE_STRIP)
gl.glDisable(self.texture.target)
gl.glPopMatrix()
如果我将其添加到我的 HexWindow
class 中,则会绘制带纹理的正方形:
def __init__(self, *args, **kwargs):
...
# everything else as before, plus
self.square1 = TexturedSquare(
120, 120, 700, 300, textures["forest"]
)
def on_draw(self):
gl.glClearColor(0, 0.3, 0.5, 0)
self.clear()
self.square1.draw()
self.batch.draw()
为什么 pyglet.graphics.TextureGroup
不起作用?
之所以不起作用是因为Shapes模块本身不处理纹理,所以没有设置纹理坐标顶点数据。您将必须对 Shape
进行子类化,并更改它们设置顶点数据的区域以说明 t2f
,就像在您的 TexturedSquare
示例中一样。
我正在尝试使用通过 TextureGroup
应用的纹理贴图绘制一些形状:
from pathlib import Path
import pyglet
from pyglet import gl
BASE_PATH = Path(__file__).resolve().parent.parent
TEXTURE_PATH = BASE_PATH / "textures"
def load_texture(filename):
return pyglet.image.load(TEXTURE_PATH / filename).get_texture()
class HexWindow(pyglet.window.Window):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
textures = {
"desert": load_texture("hexmap-iso-desert.png"),
"forest": load_texture("hexmap-iso-forest.png"),
"hills": load_texture("hexmap-iso-hills.png"),
"mountains": load_texture("hexmap-iso-mountains.png"),
"ocean": load_texture("hexmap-iso-ocean.png"),
"plains": load_texture("hexmap-iso-plains.png"),
"swamp": load_texture("hexmap-iso-swamp.png"),
}
self.batch = pyglet.graphics.Batch()
self.texture_groups = {
name: pyglet.graphics.TextureGroup(texture)
for name, texture in textures.items()
}
self._shapes = [
pyglet.shapes.Circle(
batch=self.batch,
group=self.texture_groups["desert"],
segments=6,
x=0,
y=0,
radius=100,
color=(50, 225, 30),
),
pyglet.shapes.Circle(
batch=self.batch,
group=self.texture_groups["ocean"],
segments=6,
x=300,
y=300,
radius=100,
color=(225, 30, 30),
)
]
def on_draw(self):
gl.glClearColor(0, 0.3, 0.5, 0)
self.clear()
self.batch.draw()
if __name__ == "__main__":
_ = HexWindow(1280, 720)
pyglet.app.run()
如果我删除 group=self.texture_groups["ocean"],
args 到我的 Circle
s 然后我得到一个红色和绿色的十六进制。
但是当我应用纹理组时没有绘制任何东西。
虽然纹理有效,因为我还有一些其他类似的代码:
class TexturedSquare:
def __init__(self, width, height, xpos, ypos, texture):
self.xpos = xpos
self.ypos = ypos
self.angle = 30 # degrees
self.size = 2
self.texture = texture
x = width/2.0
y = height/2.0
self.vlist = pyglet.graphics.vertex_list(
4,
('v2f', [-x, -y, x, -y, -x, y, x, y]),
('t2f', [0, 0, 1, 0, 0, 1, 1, 1]),
)
def draw(self):
gl.glPushMatrix()
gl.glTranslatef(self.xpos, self.ypos, 0)
gl.glRotatef(self.angle, 0, 0, 1)
gl.glScalef(self.size, self.size, self.size)
gl.glEnable(self.texture.target)
gl.glActiveTexture(gl.GL_TEXTURE0)
gl.glBindTexture(self.texture.target, self.texture.id)
self.vlist.draw(gl.GL_TRIANGLE_STRIP)
gl.glDisable(self.texture.target)
gl.glPopMatrix()
如果我将其添加到我的 HexWindow
class 中,则会绘制带纹理的正方形:
def __init__(self, *args, **kwargs):
...
# everything else as before, plus
self.square1 = TexturedSquare(
120, 120, 700, 300, textures["forest"]
)
def on_draw(self):
gl.glClearColor(0, 0.3, 0.5, 0)
self.clear()
self.square1.draw()
self.batch.draw()
为什么 pyglet.graphics.TextureGroup
不起作用?
之所以不起作用是因为Shapes模块本身不处理纹理,所以没有设置纹理坐标顶点数据。您将必须对 Shape
进行子类化,并更改它们设置顶点数据的区域以说明 t2f
,就像在您的 TexturedSquare
示例中一样。