在Vispy中使用gloo着色器程序的视觉效果

2024-03-28 11:34:24 发布

您现在位置:Python中文网/ 问答频道 /正文

但我要把所有的画面都显示出来。这是我的代码,它是根据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()

这将生成一个具有旋转立方体和文本的场景,但缺少colorbar。另外,立方体的一些面只显示三角形的内部。当我从on_draw方法中删除self.testText.draw(self.tr_sys)时,colorbar和多维数据集都正确显示。如果我注释掉立方体图形,而不使用颜色条和文本绘图,我只能得到文本。在

这是怎么回事?TransformSystem是否以某种方式改变了colorbar的位置,以VertexBuffer知道如何处理的方式,使得立方体仍然显示?与cube和colorbar相比,在显示文本和断开的立方体时,代码也要慢得多。文本绘图是否更改了OpenGL的某些设置,从而导致速度减慢?在

这是一个没有任何评论的场景 scene with text

没有文字的场景 without text


Tags: importselfviewappsizemodelprogramcolor
1条回答
网友
1楼 · 发布于 2024-03-28 11:34:24

文本呈现代码可能正在禁用深度测试,而您的多维数据集绘制代码无法(重新)启用它。在

线

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

启用深度测试,但它只在构造函数中设置。这里有一个重要的规则(跟我说):OpenGL中没有“初始化”。只有状态,在需要的时候必须设置,就在需要它之前。加上这个

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

def on_draw(自身,事件): 清除(颜色=真,深度=真) self.testText.draw(自动变速器系统)#绘制文本

^{pr2}$

相关问题 更多 >