Pyglet 纹理不覆盖正方形
Pyglet texture doesn't cover the square
我正在 pyglet 1.3.0 中编写一个简单的 pyglet 示例,但我遇到了问题。我还测试了其他 pyglet 版本,但问题仍然存在。
没有错误显示,但纹理只出现在正方形的一部分。我正在使用 python 3.7。
这是代码:
from pyglet.gl import *
from pyglet.window import key
import resources
import math
class Model:
def get_tex(self, file):
tex = pyglet.image.load(file).texture
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
return pyglet.graphics.TextureGroup(tex)
def __init__(self):
self.top = self.get_tex("grass_tex.jpg")
self.bottom = self.get_tex("grass_tex.jpg")
self.side = self.get_tex("terrain_tex.jpg")
self.batch = pyglet.graphics.Batch()
tex_coords = ("t2f", (0,0, 1,0, 1,1, 0,1, ))
#color = ("c3f", (1, 1, 1)*4)
x, y, z = 0, 0, -1
X, Y, Z = x+1, y+1, z+1
self.batch.add(4, GL_QUADS, self.top, ("v3f", (x,y,z, X,y,z, X,Y,z, x,Y,z, )), tex_coords)
def draw(self):
self.batch.draw()
class Player:
def __init__(self):
self.pos = [0, 0, 0]
self.rot = [0, 0]
def update(self, dt, keys):
pass
class Window(pyglet.window.Window):
def Projection(self):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
def Model(self):
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def set2d(self):
self.Projection()
gluOrtho2D(0, self.width, 0, self.height)
self.Model()
def set3d(self):
self.Projection()
gluPerspective(70, self.width/self.height, 0.05, 1000)
self.Model()
def setLock(self, state):
self.lock = state
self.set_exclusive_mouse(state)
lock = False
mouse_lock = property(lambda self:self.lock, setLock)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.model = Model()
self.player = Player()
self.keys = key.KeyStateHandler()
self.push_handlers(self.keys)
pyglet.clock.schedule(self.update)
def on_key_press(self, KEY, MOD):
if KEY == key.ESCAPE:
self.close()
elif KEY == key.SPACE:
self.mouse_lock = not self.mouse_lock
def update(self, dt):
self.player.update(dt, self.keys)
def on_draw(self):
self.clear()
self.set3d()
self.model.draw()
if __name__ == "__main__":
window = Window(width=400, height=300, caption="gamelearn2", resizable=False)
glClearColor(0.5, 0.7, 1, 1)
pyglet.app.run()
这是错误:
当您使用透视投影时 gluPerspective
,则 (0, 0) 位于视口的中心。
您必须绘制纹理从 (-1, -1) 到 (1, 1) 且深度为 1 (z = -1
) 的四边形。
x, y, z = -1, -1, -1
X, Y = 1, 1
self.batch.add(4, GL_QUADS, self.top, ("v3f", (x,y,z, X,y,z, X,Y,z, x,Y,z, )), tex_coords)
再使用90°视场角:
class Window(pyglet.window.Window):
# [...]
def set3d(self):
self.Projection()
gluPerspective(90, self.width/self.height, 0.05, 1000)
self.Model()
我正在 pyglet 1.3.0 中编写一个简单的 pyglet 示例,但我遇到了问题。我还测试了其他 pyglet 版本,但问题仍然存在。 没有错误显示,但纹理只出现在正方形的一部分。我正在使用 python 3.7。 这是代码:
from pyglet.gl import *
from pyglet.window import key
import resources
import math
class Model:
def get_tex(self, file):
tex = pyglet.image.load(file).texture
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
return pyglet.graphics.TextureGroup(tex)
def __init__(self):
self.top = self.get_tex("grass_tex.jpg")
self.bottom = self.get_tex("grass_tex.jpg")
self.side = self.get_tex("terrain_tex.jpg")
self.batch = pyglet.graphics.Batch()
tex_coords = ("t2f", (0,0, 1,0, 1,1, 0,1, ))
#color = ("c3f", (1, 1, 1)*4)
x, y, z = 0, 0, -1
X, Y, Z = x+1, y+1, z+1
self.batch.add(4, GL_QUADS, self.top, ("v3f", (x,y,z, X,y,z, X,Y,z, x,Y,z, )), tex_coords)
def draw(self):
self.batch.draw()
class Player:
def __init__(self):
self.pos = [0, 0, 0]
self.rot = [0, 0]
def update(self, dt, keys):
pass
class Window(pyglet.window.Window):
def Projection(self):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
def Model(self):
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def set2d(self):
self.Projection()
gluOrtho2D(0, self.width, 0, self.height)
self.Model()
def set3d(self):
self.Projection()
gluPerspective(70, self.width/self.height, 0.05, 1000)
self.Model()
def setLock(self, state):
self.lock = state
self.set_exclusive_mouse(state)
lock = False
mouse_lock = property(lambda self:self.lock, setLock)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.model = Model()
self.player = Player()
self.keys = key.KeyStateHandler()
self.push_handlers(self.keys)
pyglet.clock.schedule(self.update)
def on_key_press(self, KEY, MOD):
if KEY == key.ESCAPE:
self.close()
elif KEY == key.SPACE:
self.mouse_lock = not self.mouse_lock
def update(self, dt):
self.player.update(dt, self.keys)
def on_draw(self):
self.clear()
self.set3d()
self.model.draw()
if __name__ == "__main__":
window = Window(width=400, height=300, caption="gamelearn2", resizable=False)
glClearColor(0.5, 0.7, 1, 1)
pyglet.app.run()
这是错误:
当您使用透视投影时 gluPerspective
,则 (0, 0) 位于视口的中心。
您必须绘制纹理从 (-1, -1) 到 (1, 1) 且深度为 1 (z = -1
) 的四边形。
x, y, z = -1, -1, -1
X, Y = 1, 1
self.batch.add(4, GL_QUADS, self.top, ("v3f", (x,y,z, X,y,z, X,Y,z, x,Y,z, )), tex_coords)
再使用90°视场角:
class Window(pyglet.window.Window):
# [...]
def set3d(self):
self.Projection()
gluPerspective(90, self.width/self.height, 0.05, 1000)
self.Model()