Пример #1
0
    def __init__(self):
        app.Canvas.__init__(self,
                            size=(512, 512),
                            title='Monkey',
                            keys='interactive')
        verts, faces, normals, texcoords = io.read_mesh(
            "../model/monkey/monkey.obj")
        obj = MeshData(verts, faces)

        self.program = gloo.Program(vert=vertex, frag=fragment)

        V = verts.astype('float32')

        F = obj.get_faces().astype('uint32')

        E = obj.get_edges().astype('uint32')

        C = np.array([(1, 1, 1, 1)])
        for i in range(len(V) - 1):
            if i % 2 != 0:
                C = np.append(C, [(1, 1, 1, 1)], axis=0)
            else:
                C = np.append(C, [(0, 0, 0, 1)], axis=0)

        self.program['a_position'] = V
        self.program['a_color'] = C.astype('float32')
        self.F = gloo.IndexBuffer(F)
        self.E = gloo.IndexBuffer(E)

        gloo.set_viewport(0, 0, *self.physical_size)
        gloo.set_polygon_offset(1.0, 1.0)

        # intialize transformation matrix
        view = np.eye(4, dtype=np.float32)
        model = np.eye(4, dtype=np.float32)
        projection = np.eye(4, dtype=np.float32)

        # set view
        view = translate((0, 0, -5))
        self.program['u_model'] = model
        self.program['u_view'] = view
        self.program['u_projection'] = projection

        # bind a timer
        self.timer = app.Timer('auto', self.on_timer)
        self.theta = 0.0
        self.phi = 0.0
        self.timer.start()

        # show the canvas
        self.show()
Пример #2
0
    def __init__(self, radius=1.0, directions=None, colors=None):

        # Convert spherical to cartesian
        points = np.array([util.tp2xyz(*x) for x in directions])

        # Create mesh
        import scipy.spatial
        ch = scipy.spatial.ConvexHull(points)
        mesh = MeshData(vertices=ch.points, faces=ch.simplices)

        self._mesh = MeshVisual(vertices=mesh.get_vertices(),
                                faces=mesh.get_faces(),
                                vertex_colors=colors)

        CompoundVisual.__init__(self, [self._mesh])
        self.mesh.set_gl_state(depth_test=True)
Пример #3
0
class TriangleMeshVisual(Visual):
    def __init__(self,
                 vertices,
                 faces,
                 colors=None,
                 texture=None,
                 textureCoordinates=None,
                 light=None,
                 ambientMaterialConstant=0.5,
                 diffuseMaterialConstant=0.4,
                 specularMaterialConstant=0.3,
                 vertexShader=defaultVertexShader,
                 fragmentShader=defaultFragmentShader,
                 camera=None):
        Visual.__init__(self, vertexShader, fragmentShader)

        # Culling mode (for debugging)
        #wrappers.set_cull_face(mode='back')

        # Default arguments
        if colors is None and texture is None:
            colors = np.array([(0.5, 0.5, 0.5)
                               for index in range(vertices.shape[0])],
                              dtype=np.float32)
        elif colors is not None and len(colors) is 3 and (isinstance(
                colors[0], float) or isinstance(colors[0], int)):
            colors = np.array([colors for index in range(len(vertices))],
                              dtype=np.float32)

        if light is None:
            light = SimpleLight(self, color=(1.0, 1.0, 1.0))
        else:
            light = light(self)

        # Geometry setup
        self._meshData = MeshData(vertices=vertices,
                                  faces=faces,
                                  vertex_colors=colors)
        self._vertices = None
        self._colors = None
        self._texture = None
        self._textureCoordinates = None
        self._normals = None
        self._draw_mode = 'triangles'

        self.set_gl_state('opaque', depth_test=True, cull_face=True)

        # Light setup
        self._light = light
        self._camera = camera

        self._ambientMaterialConstant = ambientMaterialConstant
        self._diffuseMaterialConstant = diffuseMaterialConstant
        self._specularMaterialConstant = specularMaterialConstant

        # Initial updates
        self.updateMesh(vertices=vertices,
                        faces=faces,
                        colors=colors,
                        texture=texture,
                        textureCoordinates=textureCoordinates)
        self.updateLight()

    # --- Function override ---
    def _prepare_transforms(self, view):
        view.view_program.vert['transform'] = view.get_transform()

    def _prepare_draw(self, view):
        #self.updateMesh()
        self.updateLight()

    # --- State update ---
    def updateMesh(self,
                   vertices=None,
                   faces=None,
                   colors=None,
                   texture=None,
                   textureCoordinates=None):
        # Set
        if vertices is not None:
            self._meshData.set_vertices(verts=vertices)
        if faces is not None:
            self._meshData.set_faces(faces)
        if colors is not None:
            self._meshData.set_vertex_colors(colors)

        # Update
        if vertices is not None or faces is not None:
            self._vertices = VertexBuffer(
                self._meshData.get_vertices(indexed='faces').astype(
                    np.float32))
        if faces is not None and vertices is not None:
            self._normals = VertexBuffer(
                self._meshData.get_vertex_normals(indexed='faces').astype(
                    np.float32))
        if colors is not None:
            self._colors = VertexBuffer(
                self._meshData.get_vertex_colors(indexed='faces').astype(
                    np.float32))
        if texture is not None:
            self._texture = Texture2D(np.array(texture, dtype=np.float32),
                                      interpolation='linear',
                                      wrapping='mirrored_repeat')
        if textureCoordinates is not None:
            textureMesh = MeshData(vertices=textureCoordinates,
                                   faces=self._meshData.get_faces())
            self._textureCoordinates = VertexBuffer(
                textureMesh.get_vertices(indexed='faces').astype(np.float32))

        # Update GPU - geometry
        self.shared_program.vert['position'] = self._vertices
        self.shared_program.vert['normal'] = self._normals

        # Update GPU - color
        if self._texture is None:
            self.shared_program.vert['color'] = self._colors
        else:
            self.shared_program.vert[
                'textureCoordinates'] = self._textureCoordinates
            self.shared_program['u_objectTexture'] = self._texture

    def updateLight(self):
        self.shared_program.frag['ambientLight'] = self._light._ambient
        self.shared_program.frag[
            'ambientMaterialConstant'] = self._ambientMaterialConstant
        self.shared_program.frag[
            'diffuseMaterialConstant'] = self._diffuseMaterialConstant
        self.shared_program.frag[
            'specularMaterialConstant'] = self._specularMaterialConstant
        self.shared_program.frag['lightPos'] = self._light._pos
        self.shared_program.frag['lightColor'] = self._light._color
        self.shared_program.frag['cameraPos'] = np.array(
            self._camera._quaternion.rotate_point([0.0, 3.0, 0.0]),
            dtype=np.float32)
        self.update()