无法让 Pyglet 进行渲染
Cannot Get Pyglet To Render
我正在尝试构建一个小的 API 来简化使用 pyglet 时的一些事情。我希望它与处理语法相似,这样我就可以在纯 python 环境中使用我的 Python 处理代码,而不是处理代码。
我已经得到了一些代码来绘制一个矩形,但它只在我复制/粘贴时有效。我目前使用的模型似乎没有任何作用。
以下是我正在使用的应用 class 的重要部分:
import pyglet
class App(pyglet.window.Window):
def __init__(self, w=1280, h=720, fullscreen = False, resizable = True):
super(App, self).__init__(w, h, fullscreen = fullscreen, resizable=resizable)
self.size = self.width, self.height = w, h
self.alive = 1
def on_draw(self):
self.render()
def setBinds(self, drawFunction):
self.drawFunction = drawFunction
def render(self):
self.clear()
self.drawFunction()
self.flip()
def run(self):
while self.alive == 1:
self.render()
event = self.dispatch_events()
def Rect(self, x, y, w, h):
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x, y, w, y, w, h, x, h]))
这是 运行 应用程序的代码。它应该打开一个 window 然后每一帧在屏幕上绘制一个矩形。
from processing import App
def draw(dt=0):
width, height = APP.width, APP.height
APP.Rect(100,100,100,100)
if __name__ == "__main__":
APP = App()
APP.setBinds(drawFunction=draw)
APP.run()
但是它没有在屏幕上绘制任何东西。没有显示任何错误或任何内容,只是一个空白的黑屏。
当使用以下简化代码时,我已经获得 pyglet.grapics.draw 函数在屏幕上绘制内容:
import pyglet
from pyglet.window import mouse
window = pyglet.window.Window()
@window.event
def on_draw():
window.clear()
x, y, dx, dy = 100, 100, 20, 30
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x, y, dx, y, dx, dy, x, dy]))
pyglet.app.run()
我不确定这两组代码之间的区别是什么导致它不起作用
当您使用以下参数调用方法Rect
时
APP.Rect(100,100,100,100)
然后指令
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x, y, w, y, w, h, x, h]))
不绘制矩形,因为所有顶点坐标都相同。
要绘制矩形,您必须分别通过 x+w
计算右侧和顶部顶点坐标 y+h
:
x1, y1, x2, y2 = x, y, x+w, y+h
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x1, y1, x2, y1, x2, y2, x1, y2]))
我正在尝试构建一个小的 API 来简化使用 pyglet 时的一些事情。我希望它与处理语法相似,这样我就可以在纯 python 环境中使用我的 Python 处理代码,而不是处理代码。
我已经得到了一些代码来绘制一个矩形,但它只在我复制/粘贴时有效。我目前使用的模型似乎没有任何作用。
以下是我正在使用的应用 class 的重要部分:
import pyglet
class App(pyglet.window.Window):
def __init__(self, w=1280, h=720, fullscreen = False, resizable = True):
super(App, self).__init__(w, h, fullscreen = fullscreen, resizable=resizable)
self.size = self.width, self.height = w, h
self.alive = 1
def on_draw(self):
self.render()
def setBinds(self, drawFunction):
self.drawFunction = drawFunction
def render(self):
self.clear()
self.drawFunction()
self.flip()
def run(self):
while self.alive == 1:
self.render()
event = self.dispatch_events()
def Rect(self, x, y, w, h):
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x, y, w, y, w, h, x, h]))
这是 运行 应用程序的代码。它应该打开一个 window 然后每一帧在屏幕上绘制一个矩形。
from processing import App
def draw(dt=0):
width, height = APP.width, APP.height
APP.Rect(100,100,100,100)
if __name__ == "__main__":
APP = App()
APP.setBinds(drawFunction=draw)
APP.run()
但是它没有在屏幕上绘制任何东西。没有显示任何错误或任何内容,只是一个空白的黑屏。 当使用以下简化代码时,我已经获得 pyglet.grapics.draw 函数在屏幕上绘制内容:
import pyglet
from pyglet.window import mouse
window = pyglet.window.Window()
@window.event
def on_draw():
window.clear()
x, y, dx, dy = 100, 100, 20, 30
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x, y, dx, y, dx, dy, x, dy]))
pyglet.app.run()
我不确定这两组代码之间的区别是什么导致它不起作用
当您使用以下参数调用方法Rect
时
APP.Rect(100,100,100,100)
然后指令
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x, y, w, y, w, h, x, h]))
不绘制矩形,因为所有顶点坐标都相同。
要绘制矩形,您必须分别通过 x+w
计算右侧和顶部顶点坐标 y+h
:
x1, y1, x2, y2 = x, y, x+w, y+h
pyglet.graphics.draw(4, pyglet.gl.GL_QUADS, ('v2f', [x1, y1, x2, y1, x2, y2, x1, y2]))