(self, camera_angle=None, camera_ratio=None, autocolor=None,
        camera_zrange=None, position=None, color=None, normal=None, index=None,
        vertex_shader=None)
Initialize the template.
        
        Arguments:
          * camera_angle: the view angle of the camera, in radians.
          * camera_ratio: the W/H ratio of the camera.
          * camera_zrange: a pair with the far and near z values for the camera
            projection.
        
        Template fields are:
          * `position`: an attribute with the positions as 3D vertices,
          * `normal`: an attribute with the normals as 3D vectors,
          * `color`: an attribute with the color of each vertex, as 4D vertices.
          * `projection`: an uniform with the 4x4 projection matrix, returned by
            `projection_matrix()`.
          * `camera`: an uniform with the 4x4 camera matrix, returned by
            `camera_matrix()`.
          * `transform`: an uniform with the 4x4 transform matrix, returned by
            a dot product of `scale_matrix()`, `rotation_matrix()` and
            `translation_matrix()`.
          * `light_direction`: the direction of the diffuse light as a
            3-vector.
          * `ambient_light`: the amount of ambient light (white color).
Initialize the template.
        
        Arguments:
          * camera_angle: the view angle of the camera, in radians.
          * camera_ratio: the W/H ratio of the camera.
          * camera_zrange: a pair with the far and near z values for the camera
            projection.
        
        Template fields are:
          * `position`: an attribute with the positions as 3D vertices,
          * `normal`: an attribute with the normals as 3D vectors,
          * `color`: an attribute with the color of each vertex, as 4D vertices.
          * `projection`: an uniform with the 4x4 projection matrix, returned by
            `projection_matrix()`.
          * `camera`: an uniform with the 4x4 camera matrix, returned by
            `camera_matrix()`.
          * `transform`: an uniform with the 4x4 transform matrix, returned by
            a dot product of `scale_matrix()`, `rotation_matrix()` and
            `translation_matrix()`.
          * `light_direction`: the direction of the diffuse light as a
            3-vector.
          * `ambient_light`: the amount of ambient light (white color).
Initialize the template . Arguments : * camera_angle : the view angle of the camera in radians . * camera_ratio : the W / H ratio of the camera . * camera_zrange : a pair with the far and near z values for the camera projection . Template fields are : * position : an attribute with the positions as 3D vertices * normal : an attribute with the normals as 3D vectors * color : an attribute with the color of each vertex as 4D vertices . * projection : an uniform with the 4x4 projection matrix returned by projection_matrix () . * camera : an uniform with the 4x4 camera matrix returned by camera_matrix () . * transform : an uniform with the 4x4 transform matrix returned by a dot product of scale_matrix () rotation_matrix () and translation_matrix () . * light_direction : the direction of the diffuse light as a 3 - vector . * ambient_light : the amount of ambient light ( white color ) .
def initialize(self, camera_angle=None, camera_ratio=None, autocolor=None,
        camera_zrange=None, position=None, color=None, normal=None, index=None,
        vertex_shader=None):
        """Initialize the template.
        
        Arguments:
          * camera_angle: the view angle of the camera, in radians.
          * camera_ratio: the W/H ratio of the camera.
          * camera_zrange: a pair with the far and near z values for the camera
            projection.
        
        Template fields are:
          * `position`: an attribute with the positions as 3D vertices,
          * `normal`: an attribute with the normals as 3D vectors,
          * `color`: an attribute with the color of each vertex, as 4D vertices.
          * `projection`: an uniform with the 4x4 projection matrix, returned by
            `projection_matrix()`.
          * `camera`: an uniform with the 4x4 camera matrix, returned by
            `camera_matrix()`.
          * `transform`: an uniform with the 4x4 transform matrix, returned by
            a dot product of `scale_matrix()`, `rotation_matrix()` and
            `translation_matrix()`.
          * `light_direction`: the direction of the diffuse light as a
            3-vector.
          * `ambient_light`: the amount of ambient light (white color).
            
        """
        
        if autocolor is not None:
            color = get_color(autocolor)
        
        
        # default camera parameters
        if camera_angle is None:
            camera_angle = np.pi / 4
        if camera_ratio is None:
            camera_ratio = 4./3.
        if camera_zrange is None:
            camera_zrange = (.5, 10.)
            
        self.size = position.shape[0]
        if self.primitive_type is None:
            self.primitive_type = 'TRIANGLES'
        
        # attributes
        self.add_attribute("position", vartype="float", ndim=3, data=position)
        self.add_attribute("normal", vartype="float", ndim=3, data=normal)
        self.add_attribute("color", vartype="float", ndim=4, data=color)
        if index is not None:
            self.add_index("index", data=index)
        
        # varying color
        self.add_varying("varying_color", vartype="float", ndim=4)
        
        # default matrices
        projection = projection_matrix(camera_angle, camera_ratio, *camera_zrange)
        camera = camera_matrix(np.array([0., 0., -4.]),  # position
                               np.zeros(3),              # look at
                               np.array([0., 1., 0.]))   # top
        transform = np.eye(4)
                
        # matrix uniforms
        self.add_uniform("projection", ndim=(4, 4), size=None, data=projection)
        self.add_uniform("camera", ndim=(4, 4), size=None, data=camera)
        self.add_uniform("transform", ndim=(4, 4), size=None, data=transform)
        
        # diffuse and ambient light
        light_direction = normalize(np.array([0., 0., -1.]))
        ambient_light = .25
        self.add_uniform("light_direction", size=None, ndim=3, data=light_direction)
        self.add_uniform("ambient_light", size=None, ndim=1, data=ambient_light)
        
        # vertex shader with transformation matrices and basic lighting
        if not vertex_shader:
            vertex_shader = """
            // convert the position from 3D to 4D.
            gl_Position = vec4(position, 1.0);
            // compute the amount of light
            float light = dot(light_direction, normalize(mat3(camera) * mat3(transform) * normal));
            light = clamp(light, 0, 1);
            // add the ambient term
            light = clamp(ambient_light + light, 0, 1);
            // compute the final color
            varying_color = color * light;
            // keep the transparency
            varying_color.w = color.w;
            """
        self.add_vertex_main(vertex_shader)
        
        # basic fragment shader
        self.add_fragment_main("""
            out_color = varying_color;
        """)
def initialize ( self , camera_angle = None , camera_ratio = None , autocolor = None , camera_zrange = None , position = None , color = None , normal = None , index = None , vertex_shader = None ) : if autocolor is not None : color = get_color ( autocolor ) # default camera parameters if camera_angle is None : camera_angle = np . pi / 4 if camera_ratio is None : camera_ratio = 4. / 3. if camera_zrange is None : camera_zrange = ( .5 , 10. ) self . size = position . shape [ 0 ] if self . primitive_type is None : self . primitive_type = 'TRIANGLES' # attributes self . add_attribute ( "position" , vartype = "float" , ndim = 3 , data = position ) self . add_attribute ( "normal" , vartype = "float" , ndim = 3 , data = normal ) self . add_attribute ( "color" , vartype = "float" , ndim = 4 , data = color ) if index is not None : self . add_index ( "index" , data = index ) # varying color self . add_varying ( "varying_color" , vartype = "float" , ndim = 4 ) # default matrices projection = projection_matrix ( camera_angle , camera_ratio , * camera_zrange ) camera = camera_matrix ( np . array ( [ 0. , 0. , - 4. ] ) , # position np . zeros ( 3 ) , # look at np . array ( [ 0. , 1. , 0. ] ) ) # top transform = np . eye ( 4 ) # matrix uniforms self . add_uniform ( "projection" , ndim = ( 4 , 4 ) , size = None , data = projection ) self . add_uniform ( "camera" , ndim = ( 4 , 4 ) , size = None , data = camera ) self . add_uniform ( "transform" , ndim = ( 4 , 4 ) , size = None , data = transform ) # diffuse and ambient light light_direction = normalize ( np . array ( [ 0. , 0. , - 1. ] ) ) ambient_light = .25 self . add_uniform ( "light_direction" , size = None , ndim = 3 , data = light_direction ) self . add_uniform ( "ambient_light" , size = None , ndim = 1 , data = ambient_light ) # vertex shader with transformation matrices and basic lighting if not vertex_shader : vertex_shader = """
            // convert the position from 3D to 4D.
            gl_Position = vec4(position, 1.0);
            // compute the amount of light
            float light = dot(light_direction, normalize(mat3(camera) * mat3(transform) * normal));
            light = clamp(light, 0, 1);
            // add the ambient term
            light = clamp(ambient_light + light, 0, 1);
            // compute the final color
            varying_color = color * light;
            // keep the transparency
            varying_color.w = color.w;
            """ self . add_vertex_main ( vertex_shader ) # basic fragment shader self . add_fragment_main ( """
            out_color = varying_color;
        """ )