简体   繁体   English

Pyglet 纹理不覆盖正方形

[英]Pyglet texture doesn't cover the square

Im programming a simple pyglet example in pyglet 1.3.0 but i have a problem.我在 pyglet 1.3.0 中编写了一个简单的 pyglet 示例,但我有一个问题。 I have also tested other pyglet versions but the problem still there.我还测试了其他 pyglet 版本,但问题仍然存在。 No error is displayed but the texture only appears in a part of the square.不显示错误,但纹理仅出现在正方形的一部分中。 I'm using python 3.7.我正在使用 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.当您使用透视投影gluPerspective时, (0, 0) 位于视口的中心。

You have to draw the quad with the texture from (-1, -1) to (1, 1) and at a depth of 1 ( z = -1 ).您必须绘制纹理从 (-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)

Furthermore use a field of view angle of 90°:此外,使用 90° 的视场角:

class Window(pyglet.window.Window):
    # [...]

    def set3d(self):
        self.Projection()
        gluPerspective(90, self.width/self.height, 0.05, 1000)
        self.Model()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM