在OpenGL中绘制中心偏离视图的圆圈

时间:2014-10-30 13:42:02

标签: python opengl shader vispy

我使用vispy绘制了很多圆圈,这里是参考程序:

http://vispy.org/examples/basics/gloo/display_points.html

当圆的中心超出范围(-1,-1) - (1,1)时,整个cirlce消失。

这是一个简单的例子:

from vispy import gloo
from vispy import app
import numpy as np

VERT_SHADER = """
attribute vec3  a_position;

void main (void) {
    /*the square will disappeared when change 1.0 to 1.01*/
    gl_Position = vec4(1.0, 0, 0, 1); 
    gl_PointSize = 100;
}
"""

FRAG_SHADER = """
void main() {
    gl_FragColor = vec4(0, 0, 0, 1);
}
"""

class Canvas(app.Canvas):

    def __init__(self):
        app.Canvas.__init__(self, keys='interactive')

    def on_initialize(self, event):
        self.program = gloo.Program(VERT_SHADER, FRAG_SHADER)
        self.program["a_position"] = np.zeros((10, 3))
        gloo.set_state(clear_color='white', blend=True,
                       blend_func=('src_alpha', 'one_minus_src_alpha'))

    def on_resize(self, event):
        w, h = event.size
        s = min(w, h)
        gloo.set_viewport(0, 0, s, s)

    def on_draw(self, event):
        gloo.clear(color=True, depth=True)
        self.program.draw('points')


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

当中心为(1.01,0)时,方格将消失:

enter image description here

2 个答案:

答案 0 :(得分:2)

这是openGL的正常行为,如果光栅化器位于视图框之外,则允许光栅化器跳过基元,而点数仅由中心点定义,而不管gl_PointSize

如果你想确保绘制“圆圈”,那么你应该渲染一些小四边形。

答案 1 :(得分:0)

使用示例的代码,但中间有一个红色圆圈:


    from vispy import gloo
    from vispy import app
    import numpy as np

    # Create vetices
    n = 1
    #f_position = 0.25 * np.random.randn(n, 2).astype(np.float32)
    v_position = np.float32([[0,0]])
    #v_color = np.random.uniform(0, 1, (n, 3)).astype(np.float32)
    v_color = np.float32([[1,0,0]]) #red
    #v_size = np.random.uniform(2, 12, (n, 1)).astype(np.float32)
    v_size = np.float32([[10]])

    VERT_SHADER = """
    attribute vec3  a_position;
    attribute vec3  a_color;
    attribute float a_size;

    varying vec4 v_fg_color;
    varying vec4 v_bg_color;
    varying float v_radius;
    varying float v_linewidth;
    varying float v_antialias;

    void main (void) {
        v_radius = a_size;
        v_linewidth = 1.0;
        v_antialias = 1.0;
        v_fg_color  = vec4(0.0,0.0,0.0,0.5);
        v_bg_color  = vec4(a_color,    1.0);

        gl_Position = vec4(a_position, 1.0);
        gl_PointSize = 2.0*(v_radius + v_linewidth + 1.5*v_antialias);
    }
    """

    FRAG_SHADER = """
    #version 120

    varying vec4 v_fg_color;
    varying vec4 v_bg_color;
    varying float v_radius;
    varying float v_linewidth;
    varying float v_antialias;
    void main()
    {
        float size = 2.0*(v_radius + v_linewidth + 1.5*v_antialias);
        float t = v_linewidth/2.0-v_antialias;
        float r = length((gl_PointCoord.xy - vec2(0.5,0.5))*size);
        float d = abs(r - v_radius) - t;
        if( d  v_radius)
                gl_FragColor = vec4(v_fg_color.rgb, alpha*v_fg_color.a);
            else
                gl_FragColor = mix(v_bg_color, v_fg_color, alpha);
        }
    }
    """


    class Canvas(app.Canvas):

        def __init__(self):
            app.Canvas.__init__(self, keys='interactive')

        def on_initialize(self, event):
            self.program = gloo.Program(VERT_SHADER, FRAG_SHADER)
            # Set uniform and attribute
            self.program['a_color'] = gloo.VertexBuffer(v_color)
            self.program['a_position'] = gloo.VertexBuffer(v_position)
            self.program['a_size'] = gloo.VertexBuffer(v_size)
            gloo.set_state(clear_color='white', blend=True,
                           blend_func=('src_alpha', 'one_minus_src_alpha'))

        def on_resize(self, event):
            gloo.set_viewport(0, 0, *event.size)

        def on_draw(self, event):
            gloo.clear(color=True, depth=True)
            self.program.draw('points')


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