gpt4 book ai didi

python - 尝试使用 Vispy 在 3d 中旋转四边形

转载 作者:太空宇宙 更新时间:2023-11-03 14:57:44 26 4
gpt4 key购买 nike

我正在尝试使用 Vispy 以 3D 形式旋转纹理四边形,但我似乎无法解决。该代码不会产生任何特定错误,但它根本不旋转。我是 Vispy 的新手,也许我的代码中缺少一些重要的组件。也许你们中的一些人之前已经解决过类似的问题。给我一些见解将会有很大的帮助。这是代码:

import numpy as np

from vispy import gloo, app
app.use_app('pyqt5')
from vispy.gloo import Program
from vispy.util.transforms import perspective, translate, rotate
import imageio


im = imageio.imread('C:\\vhosts\\VIDEO_TWO_CLONE\\fol1\\im.jpg')


vertex = """
uniform mat4 u_model;
attribute vec2 position;
attribute vec2 texcoord;
varying vec2 v_texcoord;
void main()
{
gl_Position = u_model * vec4(position, 0.0, 1.0);
v_texcoord = texcoord;
} """

fragment = """
uniform sampler2D texture;
varying vec2 v_texcoord;
void main()
{
gl_FragColor = texture2D(texture, v_texcoord);
} """


def checkerboard(grid_num=8, grid_size=32):
row_even = grid_num // 2 * [0, 1]
row_odd = grid_num // 2 * [1, 0]
Z = np.row_stack(grid_num // 2 * (row_even, row_odd)).astype(np.uint8)
return 255 * Z.repeat(grid_size, axis=0).repeat(grid_size, axis=1)


class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, size=(512, 512), title='Textured quad',
keys='interactive')

self.model = np.eye(4, dtype=np.float32)
# Build program & data
self.program = Program(vertex, fragment, count=4)
self.program['position'] = [(1, 1), (-1, 1),
(1, -1), (-1, -1)]
self.program['texcoord'] = [(0, 0), (1, 0), (0, 1), (1, 1)]
self.program['texture'] = im # checkerboard()
self.program['u_model'] = self.model



self.theta = 0
self.phi = 0


gloo.set_viewport(0, 0, *self.physical_size)

self.show()

def on_draw(self, event):
gloo.set_clear_color('white')
gloo.clear(color=True)
self.program.draw('triangle_strip')


def on_timer(self, event):
self.theta += .5
self.phi += .5
self.model = np.dot(rotate(self.theta, (0, 1, 0)),
rotate(self.phi, (0, 0, 1)))
self.program['u_model'] = self.model
self.update()


def on_resize(self, event):
gloo.set_viewport(0, 0, *event.physical_size)

if __name__ == '__main__':
c = Canvas()
app.run()

最佳答案

矩阵uniform model,定义模型的位置和旋转为在on_timer方法中设置。每次执行 on_timer 时,模型矩阵都会发生变化并旋转模型。但似乎计时器从未启动,并且 on_timer 从未执行。

要启动计时器,您必须在初始化期间调用app.Timer。在类 Canvas 的构造函数中放入类似以下代码:

class Canvas(app.Canvas):
def __init__(self):
app.Canvas.__init__(self, size=(512, 512), title='Textured quad', keys='interactive')

.......

self._timer = app.Timer('auto', connect=self.on_timer, start=True)

另请参阅vispy/examples/tutorial/app/interactive.py

关于python - 尝试使用 Vispy 在 3d 中旋转四边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45393637/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com