def main(): objects = [] sonne = Point(30, 0, 10) ebene1 = Plane(Point(0, 0, -7), Vector(0, 0, 1), (255, 255, 255), False, True) #parralel zur kamera kugel1 = Sphere(Point(40, 0, 1), 2, (255, 0, 0), True) # hoehe, drehen, hinter, größe kugel2 = Sphere(Point(40, 3, -2), 2, (0, 255, 0), True) # hoehe, drehen, hinter, größe kugel3 = Sphere(Point(40, -3, -2), 2, (0, 0, 255), True) # hoehe, drehen, hinter, größe dreieck1 = Triangle(Point(40, 3, -2), Point(40, -3, -2), Point(40, 0, 1), (255, 255, 0), False) objects.append(ebene1) objects.append(kugel1) objects.append(kugel2) objects.append(kugel3) objects.append(dreieck1) # Definiere Kamera / Koordinatensystem e = Point(0, 0, 0) c = Point(1, 0, 0) up = Vector(0, 0, 1) angle = math.pi / 8 cam = Camera(angle, 1 / 1, e, c, up, objects, sonne) cam.takeAPicture(300, 300)
def colorAt(self, p): "Schachbrettmuster" if self.chess: v = Vector(p.x, p.y, p.z) v = v.scale(1.0 / self.checkSize) if (int(abs(v.x) + 0.5) + int(abs(v.y) + 0.5) + int(abs(v.z) + 0.5)) % 2: return self.otherColor return Vector(self.color[0], self.color[1], self.color[2])
def __sub__(self, other): if type(other) is Point: before = self.coords other = other.coords lst = before - other return Vector(lst[0], lst[1], lst[2])
def __init__(self, point, normal, color, reflect, chess): self.point = point #point self.normal = normal #vector self.color = color self.reflect = reflect self.chess = chess self.otherColor = Vector(0, 0, 0) self.checkSize = 6
def shade(self, level, hitPointData): "Berechnet die Farbe eines Pixels anhand der Schnittpunktinformationen, gegebenfalls Rekursiv" directColor = self.phong(hitPointData) resultcolor = directColor #als Vektor reflectedRay = self.computeReflectedRay(hitPointData) if hitPointData[3].reflect: #falls Objekt reflektieren soll reflectedColor = self.traceRay(level + 1, reflectedRay) #rekursionsaufruf directColor = directColor.scale(1-REFLECTION) #vector reflectedColor = Vector(reflectedColor[0], reflectedColor[1], reflectedColor[2]).scale(REFLECTION) #vector resultcolor = directColor + reflectedColor #überschreibe Farbe mit Reflektionsfarbe resultcolor = tuple(map(self.normalize, resultcolor)) #erstelle Tupel aus Vektor if hitPointData[4]: # wenn ein Schatten auf dem Pixel liegt t = (int(resultcolor[0]*0.6), int(resultcolor[1]*0.6), int(resultcolor[2]*0.6)) #farbwert abdunkeln resultcolor = t return resultcolor
import math from PIL import Image from computergrafik.Blatt2.Raytracer.ray import Ray from computergrafik.Blatt2.math.point import Point from computergrafik.Blatt2.math.vector import Vector BACKGROUND_COLOR = (0, 0, 0) MAXRECLEVEL = 0 REFLECTION = 0.3 SCHATTEN = True KA = 0.4 KD = 0.5 #verteilt das licht gleichmäßig in alle richtungen KS = 0.5 CA = Vector(50, 50, 50) class Camera(object): def __init__(self, fieldOfView, aspectRatio, e: Point, c: Point, up: Vector, objectList, sonne: Point): "Kameraobjekt" self.fieldOfView = fieldOfView self. aspectRatio = aspectRatio self.e = e #ursprung self.c = c #center self. up = up self.sonne = sonne #point self.objectList = objectList
def Algebratest(): v1 = Vector(12, 5, 11) v2 = Vector(33, 6, 1) print(v1 + v2)
def colorAt(self, ray): return Vector(self.color[0], self.color[1], self.color[2])