Im programming a simple pyglet example in pyglet 1.3.0 but i have a problem. I have also tested other pyglet versions but the problem still there. No error is displayed but the texture only appears in a part of the square. I'm using python 3.7. Here is the code:
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()
And this is the error:
When you use a perspective projection gluPerspective
, then the (0, 0) is in the center of the viewport.
You have to draw the quad with the texture from (-1, -1) to (1, 1) and at a depth of 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)
Furthermore use a field of view angle of 90°:
class Window(pyglet.window.Window):
# [...]
def set3d(self):
self.Projection()
gluPerspective(90, self.width/self.height, 0.05, 1000)
self.Model()