2

2 つのオブジェクトを同時にレンダリングしようとしていますが、正しい方法が見つかりません。

私が進めている方法は、2 つの異なる位置ベクトル マトリックス "V1" と "V2" を作成して、タイマー コールバック内でそれらを個別に移動できるようにすることです。

それは可能ですか?、私は多くの方法でそれを試しましたが、どれもうまくいきませんでした...

#!/usr/bin/env python

import numpy as np
from os import path as op


from vispy import gloo, app, scene
from vispy.gloo import Program, VertexBuffer, IndexBuffer
from vispy.util.transforms import perspective, translate, rotate


vertex = """
uniform mat4 model1;
uniform mat4 model2;
uniform mat4 view;
uniform mat4 projection;
uniform sampler2D texture;

attribute vec3 position1;
attribute vec3 position2;
attribute vec2 texcoord;

varying vec2 v_texcoord;
void main()
{
    vec4 mp1 = model1 * vec4(position1,1.0);
    vec4 mp2 = model2 * vec4(position2,1.0);
    vec4 mp = mp1+mp2;
    gl_Position = projection * view * mp;
    v_texcoord = texcoord;
}
"""

fragment = """
void main()
{

}
"""

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

        V = np.zeros(4, [("position1", np.float32, 3),
                 ("texcoord",   np.float32, 2)])
        V2 = np.zeros(4, [("position2", np.float32, 3),
                 ("texcoord",   np.float32, 2)])

        V["position1"] = [[ 1, 1, 0], [-1, 1, 0], [-1,-1, 0], [ 1,-1, 0]]
        V2["position2"] = [[ 1, 1, 1], [-1, 1, 1], [-1,-1, 1], [ 1,-1, 1]]
        V["texcoord"] = [[0,0], [0,1], [1,1], [1,0]]
        V2["texcoord"] = [[0,0], [0,1], [1,1], [1,0]]
        I = [0, 1, 2, 0, 2, 3]

        vertices = VertexBuffer(V)
        self.indices = IndexBuffer(I)

        vertices2 = VertexBuffer(V2)
        self.indices2 = IndexBuffer(I)

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

        # Build view, model, projection & normal
        #view = np.eye(4, dtype=np.float32)
        view = translate((0, 0, -5)).dot(rotate(-45, (1, 0, 0)))
        model2 = np.eye(4, dtype=np.float32)
        self.x,self.y, self.z = 0,5,0
        model1 = translate((self.x, self.y, self.z))

        self.program1['model1'] = model1
        self.program1['view'] = view

        gloo.set_viewport(0, 0, self.physical_size[0], self.physical_size[1])
        self.projection = perspective(45.0, self.physical_size[0] /
                                            float(self.physical_size[1]), 2.0, 30.0)

        self.program1['projection'] = self.projection

        # OpenGL initalization
        gloo.set_state(clear_color=(1, 1, 1, 1.), depth_test=True)

        self.show()

    def on_draw(self, event):
        gloo.clear(color=True, depth=True)
        self.program1.draw('triangles', self.indices)
        #self.program2.draw('triangles', self.indices2)


if __name__ == '__main__':

    canvas = Canvas()
    app.run()
4

1 に答える 1

1

とにかく、それが最善の方法であるかどうかはわかりません。誰にも役立つ場合のコードは次のとおりです。

#!/usr/bin/env python

import numpy as np

from vispy import gloo, app, scene
from vispy.gloo import Program, VertexBuffer, IndexBuffer
from vispy.util.transforms import perspective, translate, rotate


vertex = """
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

attribute vec3 position;
void main()
{
    gl_Position = projection * view * model * vec4(position,1.0);
}
"""

fragment = """
void main()
{
    //gl_FragColor = vec4 (0.0, 0.5, 0.0, 0.1);
}
"""

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

        V = np.zeros(4, [("position", np.float32, 3)])

        V["position"] = [[ 1, 1, 0], [-1, 1, 0], [-1,-1, 0], [ 1,-1, 0]]
        I = [0, 1, 2, 0, 2, 3]

        vertices = VertexBuffer(V)
        self.indices = IndexBuffer(I)

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

        # Build view, model, projection & normal
        view = translate((0, 0, -5)).dot(rotate(-45, (1, 0, 0)))
        self.model1 = translate((0, 5, 0))
        self.model2 = self.model1.dot(translate((0, 0, 1)))

        self.program1['model'] = self.model1
        self.program2['model'] = self.model2
        self.program1['view'] = view
        self.program2['view'] = view

        gloo.set_viewport(0, 0, self.physical_size[0], self.physical_size[1])
        self.projection = perspective(45.0, self.physical_size[0] /
                                            float(self.physical_size[1]), 2.0, 30.0)

        self.program1['projection'] = self.projection
        self.program2['projection'] = self.projection

        # OpenGL initalization
        gloo.set_state(clear_color=(1, 1, 1, 1.), depth_test=True)

        self._timer = app.Timer('auto', connect=self.on_timer, start=True)
        self.show()

    def on_draw(self, event):
        gloo.clear(color=True, depth=True)
        self.program1.draw('triangles', self.indices)
        self.program2.draw('triangles', self.indices)

    def on_timer(self, event):
        self.model1 = self.model1.dot(rotate(1, (0, 1, 0)))
        self.model2 = self.model2.dot(rotate(-1, (0, 1, 0)))

        self.program1['model'] = self.model1
        self.program2['model'] = self.model2

        self.update()

if __name__ == '__main__':

    canvas = Canvas()
    app.run()
于 2015-07-01T22:42:11.640 に答える