在Vispy中使用Visual使用gloo着色器程序

时间:2016-03-03 13:53:04

标签: python opengl vispy

我试图在我的场景中叠加一些TextVisuals,但是我无法正常显示所有内容。这是我的代码,它从vispy示例here

进行了轻微修改
import numpy as np

from vispy import app, gloo, visuals
from vispy.gloo import Program, VertexBuffer, IndexBuffer
from vispy.util.transforms import perspective, translate, rotate
from vispy.geometry import create_cube


vertex = """
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
attribute vec3 position;
attribute vec2 texcoord;
attribute vec3 normal;
attribute vec4 color;
varying vec4 v_color;
void main()
{
    v_color = color;
    gl_Position = projection * view * model * vec4(position,1.0);
}
"""

fragment = """
varying vec4 v_color;
void main()
{
    gl_FragColor = v_color;
}
"""

vertexcb = """
attribute vec2 position;
void main()
{
    gl_Position = vec4(position, 0.0, 1.0);
}

"""

fragmentcb = """
uniform vec2 screen;

void main()
{
    float color = (gl_FragCoord.x-0.1*screen.x)/(0.8*screen.x);
    gl_FragColor = vec4(0.0416+max(min(2.625106*color,0.9584),0),max(min(2.624996*color-0.958331,1),0),0.1+max(min(3.937504*color-2.937504,0.9),0),0);
}
"""

class Canvas(app.Canvas):
    def __init__(self):
        app.Canvas.__init__(self, size=(800, 600), title='Colored cube',
                            keys='interactive')

        # Build cube data
        V, I, _ = create_cube()
        vertices = VertexBuffer(V)
        self.indices = IndexBuffer(I)

        # Build program
        self.program = Program(vertex, fragment)
        self.program.bind(vertices)

        # Build view, model, projection & normal
        view = translate((0, 0, -5))
        model = np.eye(4, dtype=np.float32)
        self.program['model'] = model
        self.program['view'] = view
        self.phi, self.theta = 0, 0

        # Add colorbar
        self.programcb = Program(vertexcb, fragmentcb)
        self.programcb['screen'] = self.physical_size        
        self.programcb['position'] = [[-0.8, -0.8],[-0.8,-0.6],[0.8,-0.8],[0.8,-0.6]]
        self.indicescb = IndexBuffer([[0,1,2],[1,2,3]])

        # Add text
        self.testText = visuals.TextVisual('Testy',pos=(20,20))
        self.tr_sys = visuals.transforms.TransformSystem(self)   

        gloo.set_state(clear_color=(0.30, 0.30, 0.35, 1.00), depth_test=True)

        self.activate_zoom()

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

        self.show()

    def on_draw(self, event):
        gloo.clear(color=True, depth=True)
        self.testText.draw(self.tr_sys) #draw text
        self.program.draw('triangles', self.indices) #draw the cube
        self.programcb.draw('triangles', self.indicescb) # draw the colorbar


    def on_resize(self, event):
        self.activate_zoom()

    def activate_zoom(self):
        gloo.set_viewport(0, 0, *self.physical_size)
        self.programcb['screen'] = self.physical_size
        projection = perspective(45.0, self.size[0] / float(self.size[1]),
                                 2.0, 10.0)
        self.program['projection'] = projection

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

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

这会产生一个带有旋转立方体和文本的场景,但缺少颜色条。此外,立方体的某些面仅显示该三角形的内部。当我从self.testText.draw(self.tr_sys)方法中删除on_draw时,颜色条和多维数据集都会正确显示。如果我注释掉立方体绘图并留下颜色条和文字图纸,我就会'只得到文字。

这里发生了什么? TransformSystem是否以某种方式改变了colorbar的位置,使VertexBuffer知道如何处理,以便仍然显示多维数据集?与立方体和颜色条相比,当显示文本和损坏的多维数据集时,代码也会慢得多。文本绘图是否会更改某些导致速度减慢的OpenGL设置?

这里没有任何评论的场景 scene with text

没有文字的场景 without text

1 个答案:

答案 0 :(得分:0)

文本呈现代码可能会禁用深度测试,而您的多维数据集绘图代码不会(重新)启用它。

该行

gloo.set_state(clear_color=(0.30, 0.30, 0.35, 1.00), depth_test=True)

确实启用深度测试,但它仅在构造函数中设置。这是一条重要规则(与我交谈):没有"初始化"在OpenGL中。只有状态,必须在需要之前设置,直到它需要它为止。添加此

在立方体三角形绘制代码之前,即

def on_draw(自我,事件):         gloo.clear(color = True,depth = True)         self.testText.draw(self.tr_sys)#draw text

    gloo.set_state(depth_test=True)
    self.program.draw('triangles', self.indices) #draw the cube