0

我试图在我的场景上覆盖一些 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()

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

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

这是没有任何注释的场景 有文字的场景

没有文字的场景 没有文字

4

1 回答 1

0

文本渲染代码可能禁用了深度测试,而您的立方体绘制代码没有(重新)启用它。

线

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) #draw text

    gloo.set_state(depth_test=True)
    self.program.draw('triangles', self.indices) #draw the cube
于 2016-03-03T14:35:21.337 回答