def Lighting(material, obj, light, point, eyev, normalv, inShadow=None):

    color = Color()
    if material.pattern != None:
        color = pattern_at_object(material.pattern, obj, point)
    else:
        color = material.color

    effective_color = Color.multiply(color, light.intensity)
    lightv = Vector3.Normalize(Tuples.sub(light.position, point))
    ambient = Color.multiply(effective_color, material.ambient)
    light_dot_normal = Tuples.dot(lightv, normalv)

    if light_dot_normal < 0 or inShadow == True:
        diffuse = Color()
        specular = Color()

    else:
        diffuse = Color.multiply(effective_color,
                                 material.diffuse * light_dot_normal)
        reflectv = Tuples.reflect(Tuples.NegateTuple(lightv), normalv)
        reflect_dot_eye = Tuples.dot(reflectv, eyev)

        if reflect_dot_eye <= 0:
            specular = Color()
        else:
            factor = pow(reflect_dot_eye, material.shininess)
            specular = Color.multiply(light.intensity,
                                      material.specular * factor)

    return Color.add(Color.add(ambient, diffuse), specular)
Exemple #2
0
 def intersect(self, ray):
     intersection = self.get_closest_intersection(ray)
     if not intersection:
         return None
     [index, point, angle] = intersection
     intensity = AngularLighting.light_intensity_from_angle(angle)
     angle_color = int(255 * intensity)
     return Color(angle_color, angle_color, angle_color)
Exemple #3
0
 def __init__( self, name, color, rgbValues ):
     self.__name = name;
     self.__color = color;
     self.__rgbValues = rgbValues;
     
     # Parse the list of RGB-values.
     minR = minG = minB = 255;
     maxR = maxG = maxB = 0;
     
     # Loop over the list.
     for rgb in rgbValues:
         minR = min( rgb[ 0 ], minR );
         maxR = max( rgb[ 0 ], maxR );
         
         minG = min( rgb[ 1 ], minG );
         maxG = max( rgb[ 1 ], maxG );
         
         minB = min( rgb[ 2 ], minB );
         maxB = max( rgb[ 2 ], maxB );
         
     self.__minRgb = Color( ( minR, minG, minB ) );
     self.__maxRgb = Color( ( maxR, maxG, maxB ) );
Exemple #4
0
 def __init__(self,
              color=Color(1, 1, 1),
              ambient=0.1,
              diffuse=0.9,
              specular=0.9):
     self.id = id(self)
     self.color = color
     self.ambient = ambient
     self.diffuse = diffuse
     self.specular = specular
     self.shininess = 200.0
     self.reflective = 0.0
     self.refractive_index = 1.0
     self.transparency = 0
     self.pattern = None
     self.name = "DefaultMaterial"
Exemple #5
0
def render_show_single_frame_tri():
    # Create camera
    camera = Camera(position=Vector(0, 0, 0),
                    rotation=Vector(0, 0, 0),
                    height=60,
                    width=60,
                    focal_length=25)

    # Create scene
    scene = Scene(camera=camera)
    scene.set_resolution(30)

    # Create object
    obj = Triangle(Vector(0, 0, 0), Vector(10, 0, 0), Vector(4, 0, 10),
                   Color(255, 255, 255))

    # Add object to scene
    scene.add_object(obj)

    camera_position_vector = Vector(0, 12, 0)
    scene.camera.setPosition(camera_position_vector)
    scene.camera.setRotation(Vector(0, 0, 0))

    s = time.time()

    # Render the scene to an rgb array
    rendered_scene = scene.render_perspective()

    e = time.time()

    print(
        f"{scene.camera.width * scene.resolution}x{scene.camera.height * scene.resolution} took {e - s} seconds"
    )

    # Render the image
    show_image(rendered_scene, scene.camera.width * scene.resolution,
               scene.camera.height * scene.resolution)
Exemple #6
0
class ColorBlob( object ):
    MIN_YUV = 0;
    MAX_YUV = 255;
    
    OFFSET_Y = 25;
    OFFSET_U = 25;
    OFFSET_V = 25;
    
    def __init__( self, name, color, rgbValues ):
        self.__name = name;
        self.__color = color;
        self.__rgbValues = rgbValues;
        
        # Parse the list of RGB-values.
        minR = minG = minB = 255;
        maxR = maxG = maxB = 0;
        
        # Loop over the list.
        for rgb in rgbValues:
            minR = min( rgb[ 0 ], minR );
            maxR = max( rgb[ 0 ], maxR );
            
            minG = min( rgb[ 1 ], minG );
            maxG = max( rgb[ 1 ], maxG );
            
            minB = min( rgb[ 2 ], minB );
            maxB = max( rgb[ 2 ], maxB );
            
        self.__minRgb = Color( ( minR, minG, minB ) );
        self.__maxRgb = Color( ( maxR, maxG, maxB ) );
        
    def calculateYuv( self, rgb, offset ):
        yuv = rgb.toYuv();
        
        y = max( min( yuv[ 0 ] + OFFSET, MAX_YUV ), MIN_YUV );
        u = max( min( yuv[ 1 ] + OFFSET, MAX_YUV ), MIN_YUV );
        v = max( min( yuv[ 2 ] + OFFSET, MAX_YUV ), MIN_YUV );
        
        return ( y, u, v );
        
    def formatColor( self ):
        return '{color} {merge} {expected} {name}'.format( color = self.__color,
                                                           merge = '0.000000',
                                                           expected = 1,
                                                           name = self.__name );
        
    def formatThreshold( self ):
        minYuv = self.__minRgb.toYuv();
        minY = int( round( max( min( minYuv[ 0 ] - self.OFFSET_Y, self.MAX_YUV ), self.MIN_YUV ) ) );
        minU = int( round( max( min( minYuv[ 1 ] - self.OFFSET_U, self.MAX_YUV ), self.MIN_YUV ) ) );
        minV = int( round( max( min( minYuv[ 2 ] - self.OFFSET_V, self.MAX_YUV ), self.MIN_YUV ) ) );
        
        maxYuv = self.__maxRgb.toYuv();
        maxY = int( round( max( min( maxYuv[ 0 ] + self.OFFSET_Y, self.MAX_YUV ), self.MIN_YUV ) ) );
        maxU = int( round( max( min( maxYuv[ 1 ] + self.OFFSET_U, self.MAX_YUV ), self.MIN_YUV ) ) );
        maxV = int( round( max( min( maxYuv[ 2 ] + self.OFFSET_V, self.MAX_YUV ), self.MIN_YUV ) ) );
        
        return '({minY}:{maxY}, {minU}:{maxU}, {minV}:{maxV})'.format( minY = minY,
                                                                       maxY = maxY,
                                                                       minU = minU,
                                                                       maxU = maxU,
                                                                       minV = minV,
                                                                       maxV = maxV );
    
    def __str__( self ):
        return '{name} ({n} samples)'.format( name = self.__name,
                                              n = len( self.__rgbValues ) );
                
            
 def __init__(self, position=Point.zeros(), intensity=Color.white()):
     super().__init__(position, intensity)