簡體   English   中英

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM