예제 #1
0
    def ray_intersect(self, orig, dirr):
        L = sub(self.center, orig)
        tca = dot(L, dirr)
        l = frobeniusNorm(L)
        d = (l ** 2 - tca ** 2) ** 0.5
        if d > self.radius:
            return None

        thc = (self.radius ** 2 - d ** 2) ** 0.5
        t0 = tca - thc
        t1 = tca + thc
        if t0 < 0:
            t0 = t1

        if t0 < 0:
            return None

        hit = suma(orig, mulEscalarVector(t0, dirr))
        norm = sub(hit, self.center)
        norm = div(norm, frobeniusNorm(norm))

        u = 1 - arctan2(norm[2], norm[0]) / (2 * pi) + 0.5
        v = arccos(-norm[1]) / pi

        uvs = [u, v]

        return Intersect(
            distance = t0,
            point = hit,
            normal = norm,
            textCoords = uvs,
            sceneObject = self
        )
예제 #2
0
    def lookAt(self, eye, camPosition=None):
        camPosition = self.vector(0, 0, 0) if not camPosition else camPosition

        forward = glmath.sub(camPosition, eye)
        forward = glmath.div(forward, glmath.frobeniusNorm(forward))

        right = glmath.cross(self.vector(0, 1, 0), forward)
        right = glmath.div(right, glmath.frobeniusNorm(right))

        up = glmath.cross(forward, right)
        up = glmath.div(up, glmath.frobeniusNorm(up))

        camMatrix = [[
            round(right['x']),
            round(up['x']),
            round(forward['x']),
            round(camPosition['x'])
        ],
                     [
                         round(right['y']),
                         round(up['y']),
                         round(forward['y']),
                         round(camPosition['y'])
                     ],
                     [
                         round(right['z']),
                         round(up['z']),
                         round(forward['z']),
                         round(camPosition['z'])
                     ], [0, 0, 0, 1]]

        self.viewMatrix = glmath.inverse(camMatrix)
예제 #3
0
 def __init__(self,
              direction=(0, -1, 0),
              _color=Color.white(),
              intensity=1):
     self.direction = div(direction, frobeniusNorm(direction))
     self.intensity = intensity
     self.color = _color
예제 #4
0
    def getColor(self, direction):

        direction = div(direction, frobeniusNorm(direction))

        x = int((arctan2(direction['z'], direction['x']) / (2 * pi) + 0.5) *
                self.width)
        y = int(arccos(-direction['y']) / pi * self.height)

        return self.pixels[y][x]
예제 #5
0
    def rtRender(self):
        for y in range(self.height):
            for x in range(self.width):

                Px = 2 * ((x + 0.5) / self.width) - 1
                Py = 2 * ((y + 0.5) / self.height) - 1

                t = tan((self.fov * pi / 180) / 2)
                r = t * self.width / self.height
                Px *= r
                Py *= t

                direction = self.vector(Px, Py, -1)
                direction = glmath.div(direction,
                                       glmath.frobeniusNorm(direction))
                self.glVertex_coords(x, y,
                                     self.castRay(self.camPosition, direction))
예제 #6
0
def refractVector(N, I, ior):
    cosi = max(-1, min(1, glmath.dot(I, N)))
    etai = 1
    etat = ior

    if cosi < 0:
        cosi = -cosi
    else:
        etai, etat = etat, etai
        N = glmath.mulEscalarVector(-1, N)

    eta = etai / etat
    k = 1 - eta * eta * (1 - (cosi * cosi))

    if k < 0:
        return None

    R = glmath.suma(glmath.mulEscalarVector(eta, I),
                    glmath.mulEscalarVector((eta * cosi - k**0.5), N))
    return glmath.div(R, glmath.frobeniusNorm(R))
예제 #7
0
 def __init__(self, position, normal, material):
     self.position = position
     self.material = material
     self.normal = div(normal, frobeniusNorm(normal))
예제 #8
0
    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.window_color

        objectColor = self.vector(material.diffuse[2] / 255,
                                  material.diffuse[1] / 255,
                                  material.diffuse[0] / 255)

        ambientColor = self.vector(0, 0, 0)
        dirLightColor = self.vector(0, 0, 0)
        pLightColor = self.vector(0, 0, 0)
        reflectColor = self.vector(0, 0, 0)
        refractColor = self.vector(0, 0, 0)
        finalColor = self.vector(0, 0, 0)
        view_dir = glmath.sub(self.camPosition, intersect.point)
        view_dir = glmath.div(view_dir, glmath.frobeniusNorm(view_dir))

        if self.ambientLight:
            ambientColor = self.vector(
                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.dirLight:
            diffuseColor = [0, 0, 0]
            specColor = [0, 0, 0]
            shadow_intensity = 0

            light_dir = glmath.mulEscalarVector(-1, self.dirLight.direction)

            intensity = self.dirLight.intensity * max(
                0, glmath.dot(light_dir, intersect.normal))
            diffuseColor = self.vector(
                intensity * self.dirLight.color[2] / 255,
                intensity * self.dirLight.color[1] / 255,
                intensity * self.dirLight.color[0] / 255)

            reflect = reflectVector(intersect.normal, light_dir)

            spec_intensity = self.dirLight.intensity * (max(
                0, glmath.dot(view_dir, reflect))**material.spec)
            specColor = self.vector(
                spec_intensity * self.dirLight.color[2] / 255,
                spec_intensity * self.dirLight.color[1] / 255,
                spec_intensity * self.dirLight.color[0] / 255)

            shadMat, shadInter = self.scene_intercept(intersect.point,
                                                      light_dir,
                                                      intersect.sceneObject)
            if shadInter:
                shadow_intensity = 1

            dirLightColor = glmath.mulEscalarVector(
                (1 - shadow_intensity), glmath.suma(diffuseColor, specColor))

        for pointLight in self.pointLights:
            diffuseColor = [0, 0, 0]
            specColor = [0, 0, 0]
            shadow_intensity = 0

            light_dir = glmath.sub(pointLight.position, intersect.point)
            light_dir = glmath.div(light_dir, glmath.frobeniusNorm(light_dir))

            intensity = pointLight.intensity * max(
                0, glmath.dot(light_dir, intersect.normal))
            diffuseColor = self.vector(intensity * pointLight.color[2] / 255,
                                       intensity * pointLight.color[1] / 255,
                                       intensity * pointLight.color[0] / 255)

            reflect = reflectVector(intersect.normal, light_dir)

            spec_intensity = pointLight.intensity * (max(
                0, glmath.dot(view_dir, reflect))**material.spec)
            specColor = self.vector(spec_intensity * pointLight.color[2] / 255,
                                    spec_intensity * pointLight.color[1] / 255,
                                    spec_intensity * pointLight.color[0] / 255)

            shadMat, shadInter = self.scene_intercept(intersect.point,
                                                      light_dir,
                                                      intersect.sceneObject)
            if shadInter and shadInter.distance < glmath.frobeniusNorm(
                    glmath.sub(pointLight.position, intersect.point)):
                shadow_intensity = 1

            pLightColor = glmath.suma(
                pLightColor,
                glmath.mulEscalarVector((1 - shadow_intensity),
                                        glmath.suma(diffuseColor, specColor)))

        if material.matType == OPAQUE:
            finalColor1 = glmath.suma(ambientColor, dirLightColor)
            finalColor = glmath.suma(pLightColor, finalColor1)
            if material.texture and intersect.textCoords:
                texColor = material.texture.getColor(intersect.textCoords[0],
                                                     intersect.textCoords[1])
                finalColor = self.vector((texColor[2] / 255) * finalColor['x'],
                                         (texColor[1] / 255) * finalColor['y'],
                                         (texColor[0] / 255) * finalColor['z'])

        elif material.matType == REFLECTIVE:
            reflect = reflectVector(intersect.normal,
                                    glmath.mulEscalarVector(-1, direction))
            reflectColor = self.castRay(intersect.point, reflect,
                                        intersect.sceneObject, recursion + 1)
            reflectColor = self.vector(reflectColor[2] / 255,
                                       reflectColor[1] / 255,
                                       reflectColor[0] / 255)

            finalColor = reflectColor

        elif material.matType == TRANSPARENT:
            outside = glmath.dot(direction, intersect.normal) < 0
            bias = glmath.mulEscalarVector(0.001, intersect.normal)
            kr = fresnel(intersect.normal, direction, material.ior)

            reflect = reflectVector(intersect.normal,
                                    glmath.mulEscalarVector(-1, direction))
            reflectOrig = glmath.suma(intersect.point,
                                      bias) if outside else glmath.sub(
                                          intersect.point, bias)
            reflectColor = self.castRay(reflectOrig, reflect, None,
                                        recursion + 1)
            reflectColor = self.vector(reflectColor[2] / 255,
                                       reflectColor[1] / 255,
                                       reflectColor[0] / 255)

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

            finalColor = glmath.suma(
                glmath.mulEscalarVector(kr, reflectColor),
                glmath.mulEscalarVector((1 - kr), refractColor))

        finalColor = glmath.mulVectores(finalColor, objectColor)

        r = min(1, finalColor['x'])
        g = min(1, finalColor['y'])
        b = min(1, finalColor['z'])

        return Color.color(r, g, b)
예제 #9
0
def reflectVector(normal, dirVector):
    reflect = 2 * glmath.dot(normal, dirVector)
    reflect = glmath.mulEscalarVector(reflect, normal)
    reflect = glmath.sub(reflect, dirVector)
    reflect = glmath.div(reflect, glmath.frobeniusNorm(reflect))
    return reflect