예제 #1
0
    def getPixelColor(self, world, ray, curPoint, normal):
        initialColor = self.Color
        #light color
        finalColor = (0,0,0)

        #SPECULAR START
        #get the reflection of the ray by the normal
        reflected = ray.vector.Reflect(normal)

        #create a reflected ray
        reflectedRay = reverseRay(curPoint, reflected)
        
        #find the ray color of the reflected ray
        reflectedColor = world.rayColor(reflectedRay)

        #get reflected ray until hits nothing
        finalColor = combine(finalColor, self.specular, reflectedColor)
        #SPECULAR END

        #DIFUSE START
       
        difusePower = 0

        #this is the portion that does shadows
        #visibleLights = []
        flag=True
        
        #find if the light intersects any of the objects
        for (obj, material) in world.objectArray:
            lightRay = reverseRay(curPoint,world.lightPoint - curPoint)
            isIntersect = obj.intersection(lightRay)
            if isIntersect is not None and isIntersect > 0.00001:
                flag=False

        #if a light intersects the object and the current point
        #add the difuse
        #helps create soft shadows
        if flag:
            contribution = (world.lightPoint - curPoint).normalized().dot(normal)
            if contribution > 0:
                difusePower = difusePower + 1.5*contribution
        flag = True


    
        #cap the diffuse power
        difusePower = min(1,difusePower)


        finalColor = combine(finalColor, self.difuse * difusePower, initialColor)
        #DIFUSE END


        #AMBIENT START
        #combine the ambient into the color
        finalColor = combine(finalColor, self.ambient, initialColor)
        #AMBIENT End
    
        return finalColor
예제 #2
0
        sphere = Sphere(Point(-6 + (offset* 4), 3.4+ (offset%2), -5+(offset * 5)), 2)
        sphereColor = (
            1/float(random.randrange(1,5+1)),
            1/float(random.randrange(1,5+1)),
            1/float(random.randrange(1,5+1))
            )
        mat = Material(sphereColor)
        world.add(sphere, mat)

    #add plane
    world.add(Plane(), Material((0.1,0.3,0.4)))

    #print welcome message
    startingIntro(numSphere,depth, image.height, image.width, image.name, world.lightPoint)
    
    gaze = reverseRay(world.position, world.eyeDirection - world.position)
    cameraUP = gaze.vector.cross(Vector(0,1,0)).normalized()
    cameraDir = cameraUP.cross(gaze.vector).normalized()

    #compute field of view
    ratio =  float (image.height)/float(image.width)

    #45 degree field of view
    fieldRad = math.pi * (float(45) / float(2) / float(180))


    #get world dimensions
    width = (math.tan(fieldRad) * 2.0)
    height = ratio * math.tan(fieldRad) * 2.0
    pixelWidth = width / (image.width - 1)
    pixelHeight = height / (image.height - 1)