2017-07-29 108 views
1

我想使用Vispy在3D中旋轉紋理四邊形,但似乎無法解決問題。代碼沒有產生任何特定的錯誤,但它根本不旋轉。我對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() 

回答

1

的基質均勻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