繁体   English   中英

在Vispy中将Visuals与gloo着色器程序一起使用

[英]Using Visuals with gloo shader programs in Vispy

我试图在场景上覆盖一些TextVisuals,但是我无法使所有内容正确显示。 这是我的代码,从这里的vispy示例略作修改

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()

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

这是怎么回事 TransformSystem是否以VertexBuffer知道如何处理的方式以某种方式更改了颜色条的位置,从而仍显示多维数据集? 与立方体和颜色条相比,当同时显示文本和残破的立方体时,代码也要慢得多。 文字绘图是否更改了某些OpenGL设置,从而导致速度降低?

这是没有评论的场景 带有文字的场景

没有文字的场景 没有文字

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

线

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

确实可以启用深度测试,但只能在构造函数中进行设置。 这是一个重要规则(与我交谈):OpenGL中没有“初始化”。 只有状态,它必须在需要时设置,就在需要之前设置为需要的状态。 加上这个

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

def on_draw(self,event):gloo.clear(color = True,depth = True)self.testText.draw(self.tr_sys)#绘制文本

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM