Esempio n. 1
0
    # u es para la A, v es para B, w para C
    try:
        u = ( ((B.y - C.y)*(P.x - C.x) + (C.x - B.x)*(P.y - C.y) ) /
              ((B.y - C.y)*(A.x - C.x) + (C.x - B.x)*(A.y - C.y)) )

        v = ( ((C.y - A.y)*(P.x - C.x) + (A.x - C.x)*(P.y - C.y) ) /
              ((B.y - C.y)*(A.x - C.x) + (C.x - B.x)*(A.y - C.y)) )

        w = 1 - u - v
    except:
        return -1, -1, -1

    return u, v, w


BLACK = color(0,0,0)
WHITE = color(1,1,1)

class Raytracer(object):
    def __init__(self, width, height):
        self.curr_color = WHITE
        self.clear_color = BLACK
        self.glCreateWindow(width, height)

        self.camPosition = V3(0,0,0)
        self.fov = 60

        self.scene = []

    def glCreateWindow(self, width, height):
        self.width = width
    def castRay(self, orig, direction, origObj=None, recursion=0):

        material, intersect = self.scene_intercept(orig, direction, origObj)

        if material is None or recursion >= MAX_RECURSION_DEPTH:
            if self.envmap:
                return self.envmap.getColor(direction)
            return self.clear_color

        objectColor = np.array([
            material.diffuse[2] / 255, material.diffuse[1] / 255,
            material.diffuse[0] / 255
        ])

        ambientColor = np.array([0, 0, 0])
        diffuseColor = np.array([0, 0, 0])
        specColor = np.array([0, 0, 0])

        reflectColor = np.array([0, 0, 0])
        refractColor = np.array([0, 0, 0])

        finalColor = np.array([0, 0, 0])

        shadow_intensity = 0

        # Direccion de vista
        view_dir = substractNPArray(self.camPosition, intersect.point)
        view_dir = normNPArray(view_dir)

        if self.ambientLight:
            ambientColor = np.array([
                self.ambientLight.strength * self.ambientLight.color[2] / 255,
                self.ambientLight.strength * self.ambientLight.color[1] / 255,
                self.ambientLight.strength * self.ambientLight.color[0] / 255
            ])

        if self.pointLight:
            # Sacamos la direccion de la luz para este punto
            light_dir = substractNPArray(self.pointLight.position,
                                         intersect.point)
            light_dir = normNPArray(light_dir)

            # Calculamos el valor del diffuse color
            intensity = self.pointLight.intensity * max(
                0, dotNPArray(light_dir, intersect.normal))
            diffuseColor = np.array([
                intensity * self.pointLight.color[2] / 255,
                intensity * self.pointLight.color[1] / 255,
                intensity * self.pointLight.color[2] / 255
            ])

            # Iluminacion especular
            reflect = reflectVector(intersect.normal,
                                    light_dir)  # Reflejar el vector de luz

            # spec_intensity: lightIntensity * ( view_dir dot reflect) ** especularidad
            spec_intensity = self.pointLight.intensity * (max(
                0, dotNPArray(view_dir, reflect))**material.spec)
            specColor = np.array([
                spec_intensity * self.pointLight.color[2] / 255,
                spec_intensity * self.pointLight.color[1] / 255,
                spec_intensity * self.pointLight.color[0] / 255
            ])

            shadMat, shadInter = self.scene_intercept(intersect.point,
                                                      light_dir,
                                                      intersect.sceneObject)
            if shadInter is not None and shadInter.distance < magnitudeNpArray(
                    substractNPArray(self.pointLight.position,
                                     intersect.point)):
                shadow_intensity = 1

        if material.matType == OPAQUE:
            # Formula de iluminacion, PHONG
            finalColor = (ambientColor + (1 - shadow_intensity) *
                          (diffuseColor + specColor))
        elif material.matType == REFLECTIVE:
            reflect = reflectVector(intersect.normal, np.array(direction) * -1)
            reflectColor = self.castRay(intersect.point, reflect,
                                        intersect.sceneObject, recursion + 1)
            reflectColor = np.array([
                reflectColor[2] / 255, reflectColor[1] / 255,
                reflectColor[0] / 255
            ])

            finalColor = reflectColor + (1 - shadow_intensity) * specColor

        elif material.matType == TRANSPARENT:

            outside = dotNPArray(direction, intersect.normal) < 0
            bias = 0.001 * intersect.normal
            kr = fresnel(intersect.normal, direction, material.ior)

            reflect = reflectVector(intersect.normal, np.array(direction) * -1)
            reflectOrig = sumNPArray(intersect.point,
                                     bias) if outside else substractNPArray(
                                         intersect.point, bias)
            reflectColor = self.castRay(reflectOrig, reflect, None,
                                        recursion + 1)
            reflectColor = np.array([
                reflectColor[2] / 255, reflectColor[1] / 255,
                reflectColor[0] / 255
            ])

            if kr < 1:
                refract = refractVector(intersect.normal, direction,
                                        material.ior)
                refractOrig = substractNPArray(
                    intersect.point, bias) if outside else sumNPArray(
                        intersect.point, bias)
                refractColor = self.castRay(refractOrig, refract, None,
                                            recursion + 1)
                refractColor = np.array([
                    refractColor[2] / 255, refractColor[1] / 255,
                    refractColor[0] / 255
                ])

            finalColor = reflectColor * kr + refractColor * (1 - kr) + (
                1 - shadow_intensity) * specColor

        # Le aplicamos el color del objeto
        finalColor *= objectColor

        #Nos aseguramos que no suba el valor de color de 1
        r = min(1, finalColor[0])
        g = min(1, finalColor[1])
        b = min(1, finalColor[2])

        return color(r, g, b)
 def glColor(self, r, g, b):
     rgb_array = decimalToRgb([r, g, b])
     self.curr_color = color(rgb_array[0], rgb_array[1], rgb_array[2])
    def glColor(self, r, g, b):

        self.curr_color = color(r, g, b)
    def glClearColor(self, r, g, b):

        self.clear_color = color(r, g, b)
from obj import Obj
from utils.gl_color import color, decimalToRgb
from utils.gl_encode import char, word, dword

BLACK = color(0, 0, 0)
WHITE = color(255, 255, 255)


class Render(object):
    def __init__(self, width, height):
        self.curr_color = WHITE
        self.clear_color = BLACK
        self.glCreateWindow(width, height)

    def glCreateWindow(self, width, height):
        self.width = width
        self.height = height
        self.glClear()
        self.glViewport(0, 0, width, height)

    def glClear(self):
        self.pixels = [[self.clear_color for x in range(self.width)]
                       for y in range(self.height)]

    def glViewport(self, x, y, width, height):
        self.viewport_initial_x = x
        self.viewport_initial_y = y
        self.viewport_width = width
        self.viewport_height = height
        self.viewport_final_x = x + width
        self.viewport_final_y = x + height
Esempio n. 7
0
from utils.gl_color import color
from utils.gl_math import V2, V3
from gl import Raytracer
from obj import Obj, Texture
from sphere import Sphere, Material
import random

width = 600
height = 400

snow = Material(diffuse=color(1, 1, 1))
button = Material(diffuse=color(0, 0, 0))
mouth = Material(diffuse=(color(0.5, 0.5, 0.5)))
nose = Material(diffuse=(color(1, 0.65, 0)))

r = Raytracer(width, height)
r.scene.append(Sphere(V3(0, -3.1, -10), 2.3, snow))
r.scene.append(Sphere(V3(0, 0, -10), 1.8, snow))
r.scene.append(Sphere(V3(0, 2.9, -10), 1.3, snow))

r.scene.append(Sphere(V3(0, 2.15, -8), 0.24, nose))

r.scene.append(Sphere(V3(0.43, 1.8, -8), 0.1, mouth))
r.scene.append(Sphere(V3(0.15, 1.6, -8), 0.1, mouth))
r.scene.append(Sphere(V3(-0.15, 1.6, -8), 0.1, mouth))
r.scene.append(Sphere(V3(-0.43, 1.8, -8), 0.1, mouth))

r.scene.append(Sphere(V3(0.40, 2.5, -8), 0.12, button))
r.scene.append(Sphere(V3(-0.40, 2.5, -8), 0.12, button))

r.scene.append(Sphere(V3(0, 0.5, -6), 0.26, button))
Esempio n. 8
0
from gl import Raytracer
from utils.gl_color import color
from utils.gl_math import V2, V3
from obj import Obj, Texture, Envmap
from sphere import *
import random

brick = Material(diffuse = color(0.8, 0.25, 0.25 ), spec = 16)
stone = Material(diffuse = color(0.4, 0.4, 0.4 ), spec = 32)
mirror = Material(spec = 64, matType = REFLECTIVE)

glass = Material(spec = 64, ior = 1.5, matType= TRANSPARENT) 


width = 256
height = 256
r = Raytracer(width,height)
r.glClearColor(0.2, 0.6, 0.8)
r.glClear()

r.envmap = Envmap('envmap.bmp')

r.pointLight = PointLight(position = V3(0,0,0), intensity = 1)
r.ambientLight = AmbientLight(strength = 0.1)

#r.scene.append( Sphere(V3( 1, 1, -10), 1.5, brick) )
# r.scene.append( Sphere(V3( 0, -1, -5),  1, glass) )
#r.scene.append( Sphere(V3(-3, 3, -10),  2, mirror) )

#r.scene.append( Plane( V3(-2,-3,0), V3(1,1,0), stone))
import numpy as np
from utils.gl_math import norm, PI, V2, V3, substract, dot, cross, substractNPArray, dotNPArray, normNPArray, multiplyConstant, multiplyColor, sumNPArray, magnitudeNpArray
from utils.gl_color import color

OPAQUE = 0
REFLECTIVE = 1
TRANSPARENT = 2

WHITE = color(1, 1, 1)


class AmbientLight(object):
    def __init__(self, strength=0, _color=WHITE):
        self.strength = strength
        self.color = _color


class PointLight(object):
    def __init__(self, position=V3(0, 0, 0), _color=WHITE, intensity=1):
        self.position = position
        self.intensity = intensity
        self.color = _color


class Material(object):
    def __init__(self, diffuse=WHITE, spec=0, ior=1, matType=OPAQUE):
        self.diffuse = diffuse
        self.spec = spec

        self.matType = matType
        self.ior = ior
Esempio n. 10
0
from utils.gl_color import color
from utils.gl_math import V2, V3
from gl import Raytracer
from obj import Obj, Texture
from sphere import Sphere, Material,PointLight, AmbientLight
import random

# brick = Material(diffuse = color(0.8, 0.25, 0.25 ), spec = 16)
# stone = Material(diffuse = color(0.4, 0.4, 0.4 ), spec = 32)
# grass = Material(diffuse = color(0.5, 1, 0), spec = 32)
# glass = Material(diffuse = color(0.25, 1, 1), spec = 64)
coal = Material(diffuse = color(0.15,0.15,0.15), spec = 32)
snow = Material(diffuse = color(1, 1, 1), spec = 64)
carrot= Material(diffuse = color(1, 0.54, 0), spec = 64)
eyes= Material(diffuse = color(0.90, 0.90, 0.90),spec = 64)

width = 600
height = 400

r = Raytracer(width,height)
r.pointLight = PointLight(position = (-1,2,0), intensity = 1)
r.ambientLight = AmbientLight(strength = 0.1)

r.scene.append( Sphere(V3(0, -3.1, -10), 2.3, snow) )
r.scene.append( Sphere(V3(0, 0,  -10), 1.8, snow) )
r.scene.append( Sphere(V3(0, 2.9, -10), 1.3, snow) )


r.scene.append( Sphere(V3(0, 2.15, -8), 0.24, carrot) )