def createScene(floorColors, sphereColors, lightColors, lightPositions): WIDTH = 960 HEIGHT = 540 RENDERED_IMG = "custom.ppm" CAMERA = Vector(0, -0.35, -1) OBJECTS = [ # Ground Plane Sphere( Point(0, 10000.5, 1), 10000.0, ChequeredMaterial( color1=Color.from_hex(floorColors[0]), color2=Color.from_hex(floorColors[1]), ambient=0.2, reflection=0.2, ), ), ] createSpheres(OBJECTS, sphereColors) LIGHTS = [] createLights(LIGHTS, lightColors, lightPositions) scene = Scene(CAMERA, OBJECTS, LIGHTS, WIDTH, HEIGHT) engine = RenderEngine() #Creates an instance of the render image = engine.prepareRender(scene) #Creates a image rendering the data #os.chdir(os.path.dirname(os.path.abspath(mod.__file__))) #Creates a dir using the name in the mod with open(RENDERED_IMG, "w") as img_file: #Open the ppm image and store the pixels image.write_ppm(img_file)
def add_style(self, name="Default", fontname="Arial", fontsize=20, primarycolor="fff", secondarycolor="fff", bordcolor="000", shadowcolor="000", bold=False, italic=False, scalex=100, scaley=100, spacing=0, bord=2, shadow=0, alignment=2, marginl=10, marginr=20, marginv=10): if not isinstance(primarycolor, Color): primarycolor = Color.from_hex(primarycolor) if not isinstance(secondarycolor, Color): secondarycolor = Color.from_hex(secondarycolor) if not isinstance(bordcolor, Color): bordcolor = Color.from_hex(bordcolor) if not isinstance(shadowcolor, Color): shadowcolor = Color.from_hex(shadowcolor) style_item = { "name": name, "font": {"name": fontname, "size": fontsize}, "color": { "primary": primarycolor.ass_long, "secondary": secondarycolor.ass_long, "bord": bordcolor.ass_long, "shadow": shadowcolor.ass_long}, "bold": bold, "italic": italic, "scale": [scalex, scaley], "spacing": spacing, "bord": bord, "shadow": shadow, "alignment": alignment, "margin": {"l": marginl, "r": marginr, "v": marginv}} self._script_data["style"][name] = style_item
def add_style(self, name="Default", fontname="Arial", fontsize=20, primarycolor="fff", secondarycolor="fff", bordcolor="000", shadowcolor="000", bold=False, italic=False, scalex=100, scaley=100, spacing=0, bord=2, shadow=0, alignment=2, marginl=10, marginr=20, marginv=10): if not isinstance(primarycolor, Color): primarycolor = Color.from_hex(primarycolor) if not isinstance(secondarycolor, Color): secondarycolor = Color.from_hex(secondarycolor) if not isinstance(bordcolor, Color): bordcolor = Color.from_hex(bordcolor) if not isinstance(shadowcolor, Color): shadowcolor = Color.from_hex(shadowcolor) style_item = { "name": name, "font": { "name": fontname, "size": fontsize }, "color": { "primary": primarycolor.ass_long, "secondary": secondarycolor.ass_long, "bord": bordcolor.ass_long, "shadow": shadowcolor.ass_long }, "bold": bold, "italic": italic, "scale": [scalex, scaley], "spacing": spacing, "bord": bord, "shadow": shadow, "alignment": alignment, "margin": { "l": marginl, "r": marginr, "v": marginv } } self._script_data["style"][name] = style_item
def main(): WIDTH = 320 HEIGHT = 200 camera = Vector(0, 0, -1) sphere1 = Sphere(Point(+0.3, 0.0, 0), 0.2, Color.from_hex("#FF0000")) sphere2 = Sphere(Point(+0.0, 0.0, 0), 0.2, Color.from_hex("#FFFF00")) sphere3 = Sphere(Point(-0.3, 0.0, 0), 0.2, Color.from_hex("#00FF00")) objects = [sphere1, sphere2, sphere3] scene = Scene(camera, objects, WIDTH, HEIGHT) engine = Engine() image = engine.render(scene) image.export('sphere2.ppm')
def __init__(self, color1 = Color.from_hex("#FFFFFF"), color2 = Color.from_hex("#000000"), ambient = 0.05, diffuse = 1.0, specular = 1.0, reflection = 0.5): self.color1 = color1 self.color2 = color2 self.ambient = ambient self.diffuse = diffuse self.specular = specular self.reflection = reflection
def color_at(self, obj_hit, hit_pos, normal, ray, scene): material = obj_hit.material obj_color = obj_hit.color(hit_pos) # 1. ambient color = Color.from_hex("#FFFFFF") * material.ambient to_cam = scene.camera - hit_pos specular_k = 50 for light in scene.lights: to_light = Ray(hit_pos, light.position - hit_pos) # 2. diffuse # to_light dot surface norm cos = max(to_light.direction.dot(normal), 0) color += (obj_color * cos * material.diffuse) reflect = 2 * cos * normal - to_light.direction # 3. Phong # color += ( # max((to_cam).dot(reflect.normalize()), 0)**material.specular * # light.color # ) # 4. Half-way half_vector = (to_light.direction + to_cam).normalize() HR = max(half_vector.dot(normal), 0) color += (material.specular * light.color * HR**specular_k) return color
def main(): console.clear() canvas.clear() multiplier = 0.6 WIDTH = 1920 HEIGHT = 1080 camera = Vector(0, 0, -1) objects = [ Sphere(Point(0, 0, 1.0), 0.3, Color.from_hex("#F24F00")), Sphere(Point(0.2, 0, 0.4), 0.1, Color.from_hex("#224F00")), Sphere(Point(-0.25, 0.0, 0.4), 0.1, Color.from_hex("#1f8fd8")), Sphere(Point(0.4, -0.9, 1.7), 0.6, Color.from_hex("#d80cac")), Sphere(Point(-0.4, -0.9, 1.7), 0.6, Color.from_hex("#d8b63e")) ] scene = Scene(camera, objects, WIDTH, HEIGHT) engine = RenderEngine() image = engine.render(scene, 10)
def __init__(self, color=Color.from_hex("#333333"), ambient=0.05, diffuse=1.0, specular=1.0): self.color = color self.ambient = ambient self.diffuse = diffuse self.specular = specular
def main(): camera = Vec3(0, 0, -1) objects = [Sphere(Point(0, 0, 0), 0.5, Color.from_hex("#FF0000"))] scene = Scene(camera, objects, WIDTH, HEIGHT) engine = RenderEngine() img = engine.render(scene) with open("sphere_test.ppm", 'w') as img_file: img.write_ppm(img_file)
def __init__(self, color=Color.from_hex("#FFFFFF"), ambient=0.05, diffuse=1, specular=1): """ Store the material color, ambient coefficient, diffuse coefficient, specular coefficient """ self.color = color self.ambient = ambient self.diffuse = diffuse self.specular = specular
def main(): parser = argparse.ArgumentParser() parser.add_argument("imageout", help="Path to the rendered image") args = parser.parse_args() WIDTH = 320 HEIGHT = 200 camera = Vector(0, 0, -1) objects = [ Sphere(Point(0, 0, 0), 0.5, Material(Color.from_hex("#FF0000"))), ] lights = [Light(Point(1.5, -0.5, -10.0), Color.from_hex("#FFFFFF"))] scene = Scene(camera, objects, lights, WIDTH, HEIGHT) engine = RenderEngine() image = engine.render(scene) with open(args.imageout, "w+") as f: image.write_ppm(f)
def createLights(LIGHTS, lightColors, lightPos): i = 0 for color in lightColors: x = (lightPos[i][0] * 3.5 / 700) - 1.75 # Gets a scale por the x axis y = (lightPos[i][1] * 3.5 / 394) - 1.75 # Gets a scale por the y axis print("X pos:", x, "Y pos: ", y) LIGHTS.append( Light(Point(x, y, random.randint(-5, 0)), Color.from_hex(color))) i += 1 pass
def __init__(self, color=Color.from_hex("#FFFFFF"), ambient=0.05, diffuse=1, specular=1, reflection=0.5): self.color = color self.ambient = ambient self.diffuse = diffuse self.specular = specular self.reflection = reflection
def c(arg1, arg2=None): """ Aplica el color especificado. c(tipo, color) c(color) @tipo: 1: Color Primario 2: Color Secundario 3: Color del borde 4: Color de la sombra sin tipo es lo mismo que 1 @color: Los codigos de color acepta hexadecimales en el siguiente orden: Rojo, Verde, Azul. Ejemplo: c(1,'fff') >>> '\\1c&HFFFFFF&' c('ffffff') >>> '\\c&HFFFFFF&' # Si es solo c (igual que tipo = 1) """ if arg2 != None: tipo = arg1 if isinstance(arg2, Color): color = arg2 else: color = Color.from_hex(arg2) if tipo not in (1, 2, 3, 4): raise ValueError('\n\nc(tipo,valor):\n<tipo> solo acepta' ' numeros entre 1 y 4') else: return '\\{:d}c{:s}'.format(tipo, color.ass) else: if isinstance(arg1, Color): color = arg1 else: color = Color.from_hex(arg1) return '\\c{:s}'.format(color.ass)
def throughRender(self,width,height,camera,pixels,scene,y0,ystep,x0,xstep): #Gets a pixel for j in range(height): y = y0 + j * ystep for i in range(width): x = x0 + i * xstep if(i%2!=0): pixels.set_pixel(i, j, Color.from_hex("#000000"))#Creates a black pixel else: ray = Ray(camera, Point(x, y) - camera) #Creates a ray from the camera to the point pixels.set_pixel(i, j, self.ray_trace(ray, scene)) #Raytracing method return pixels
def c(arg1, arg2=None): """ Aplica el color especificado. c(tipo, color) c(color) @tipo: 1: Color Primario 2: Color Secundario 3: Color del borde 4: Color de la sombra sin tipo es lo mismo que 1 @color: Los codigos de color acepta hexadecimales en el siguiente orden: Rojo, Verde, Azul. Ejemplo: c(1,'fff') >>> '\\1c&HFFFFFF&' c('ffffff') >>> '\\c&HFFFFFF&' # Si es solo c (igual que tipo = 1) """ if arg2 != None: tipo = arg1 if isinstance(arg2, Color): color = arg2 else: color = Color.from_hex(arg2) if tipo not in (1, 2, 3, 4): raise ValueError("\n\nc(tipo,valor):\n<tipo> solo acepta" " numeros entre 1 y 4") else: return "\\{:d}c{:s}".format(tipo, color.ass) else: if isinstance(arg1, Color): color = arg1 else: color = Color.from_hex(arg1) return "\\c{:s}".format(color.ass)
def probRender(self,width,height,camera,pixels,scene,y0,ystep,x0,xstep): #Gets a pixel for j in range(height): y = y0 + j * ystep for i in range(width): if(self.probability()): #Monte Carlo method, calculates a probability, if return true calculates x = x0 + i * xstep #The raytrace of the pixel, otherwise creates a black pixel ray = Ray(camera, Point(x, y) - camera) #Creates a ray from the camera to the point pixels.set_pixel(i, j, self.ray_trace(ray, scene)) #Raytracing method else: pixels.set_pixel(i, j, Color.from_hex("#000000"))#Creates a black pixel print("{:3.0f}%".format(float(j) / float(height) * 100), end="\r") #Progress bar return pixels
def createSpheres(OBJECTS, sphereColors): spherePoints = [ Point(0.75, -0.1, 1), Point(-0.75, -0.1, 2.25), Point(-0.80, -0.3, 0), Point(0, -0.1, 4.75), Point(1.2, -0.1, 6) ] i = 0 for color in sphereColors: OBJECTS.append( Sphere(spherePoints[i], 0.4, Material(Color.from_hex(color)))) i += 1
def scene_5(): WIDTH = 320 HEIGHT = 200 camera = Vector(0, -0.35, -1) pink = Material(color=Color.from_hex("#803980")) blue = Material(color=Color.from_hex("#0000FF")) checker = Material(color1=Color.from_hex("#420500"), color2=Color.from_hex("#E6b87d"), ambient=0.2, reflection=0.2) ball_blue = Sphere(Point(+0.75, -0.1, 1), 0.6, blue) ball_pink = Sphere(Point(-0.75, -0.1, 2.25), 0.6, pink) ground = Sphere(Point(0, 10000.5, 1), 10000.0, checker) light1 = Light(Point(1.5, -1.5, -10.0)) light2 = Light(Point(-0.5, -10.5, -0.0)) lights = [light1, light2] # lights = [light1] objects = [ball_blue, ball_pink, ground] # objects = [sphere1] scene = Scene(camera, objects, lights, WIDTH, HEIGHT) return scene
def __init__(self, parent, *args, **kwargs): self.title_font_name = kwargs.pop('title_font_name', 'Lucida Grande') self.title_font_size = kwargs.pop('title_font_size', 12) self.bg_color = kwargs.pop('bg_color', Color.from_hex('#0099cc')) self.title_color = kwargs.pop('title_color', Color(255, 255, 255)) self.title_x_pad = kwargs.pop('title_x_pad', 4) self.title_y_pad = kwargs.pop('title_y_pad', 4) super(MenuBar, self).__init__(parent, *args, **kwargs) self.menu_items = [] self.frame = Rectangle(0, 0, 0, 0, *args, **kwargs) assert (isinstance(self.parent, pyglet.window.Window) or isinstance(self.parent, Panel))
def ray_trace(self, ray, scene, depth=0): color = Color.from_hex("#000000") dist_hit, obj_hit = self.find_nearest(ray, scene) if obj_hit is None: return color hit_pos = ray.origin + dist_hit * ray.direction normal = obj_hit.normal(hit_pos) color += self.color_at(obj_hit, hit_pos, normal, ray, scene) if depth < 5: inray_dir = ray.direction reflect_dir = inray_dir - 2 * inray_dir.dot(normal) * normal new_ray = Ray(hit_pos + 0.00001 * reflect_dir, reflect_dir) color += self.ray_trace(new_ray, scene, depth + 1) * obj_hit.material.reflection return color
def color_at(self, obj_hit, hit_pos, normal, scene): material = obj_hit.material obj_color = material.color_at(hit_pos) to_cam = scene.camera - hit_pos specular_k = 50 color = material.ambient * Color.from_hex("#FFFFFF") for light in scene.lights: to_light = Ray(hit_pos, light.position - hit_pos) # difuese color += (obj_color * material.diffuse * \ max(normal.dot_product(to_light.direction), 0)) # specular shading half_vector = (to_light.direction + to_cam).normalize() color += (light.color * material.specular * \ max(normal.dot_product(half_vector), 0)**specular_k) return color
def __init__(self, text, value, *args, **kwargs): self.label_font_name = kwargs.pop('title_font_name', 'Lucida Grande') self.label_font_size = kwargs.pop('title_font_size', 12) self.bg_color = kwargs.pop('bg_color', Color.from_hex('#0099cc')) self.label_color = kwargs.pop('label_color', Color(255, 255, 255)) self.label_x_pad = kwargs.pop('label_x_pad', 4) self.label_y_pad = kwargs.pop('label_y_pad', 4) self.value = value self.depth = 0 self.expanded = False super(Option, self).__init__(*args, **kwargs) self.label = pyglet.text.Label(text) self.text = text self.width, self.height = self.calc_item_size() self.frame = Rectangle(0, 0, self.width, self.height, *args, **kwargs)
def color_at(self, obj_hit, hit_pos, hit_normal, scene): material = obj_hit.material obj_color = material.color_at(hit_pos) to_cam = scene.camera - hit_pos specular_k = 50 color = material.ambient * Color.from_hex("#000000") for light in scene.lights: to_light = Ray(hit_pos, light.position - hit_pos) # Lambert Shading color += (obj_color * material.diffuse * max(hit_normal.dot(to_light.direction), 0)) # Specular Shading half_vector = (to_light.direction + to_cam).normalized() color += (light.color * material.specular * max(hit_normal.dot(half_vector), 0)**specular_k) return color
def color_at(self, hit_obj, hit_pos, normal, scene): material = hit_obj.material obj_color = material.color_at(hit_pos) to_cam = scene.camera - hit_pos specular_k = 50 color = material.ambient * Color.from_hex("#FFFFFF") # Light calculations for light in scene.lights: to_light = Ray(hit_pos, light.position - hit_pos) # Diffuse shading (Lambert) color += obj_color * material.diffuse * \ max(normal.dot_product(to_light.direction), 0) # Specular shading (Phong Blinn) half_vector = (to_light.direction + to_cam).normalize() color += light.color * material.specular * \ max(normal.dot_product(half_vector), 0) ** specular_k return color
def __init__(self, x, y, *args, **kwargs): self.label_font_name = kwargs.pop('title_font_name', 'Lucida Grande') self.label_font_size = kwargs.pop('title_font_size', 12) self.label_color = kwargs.pop('label_color', Color(255, 255, 255)) self.label_x_pad = kwargs.pop('label_x_pad', 4) self.label_y_pad = kwargs.pop('label_y_pad', 4) self.bg_color = kwargs.pop('bg_color', Color(255, 0, 0)) self.click_color = kwargs.pop('click_color', Color.from_hex('#4286f4')) super(RadioGroup, self).__init__(*args, **kwargs) self.x = x self.y = y self.width = None self.height = None self.selection = None self.options = [] self.width, self.height = 0, 0 self.frame = Rectangle(x, y, self.width, self.height, *args, **kwargs) self.vertices = self.frame.vertices
def __init__(self, width=960, height=540, camera=Vector(0, -0.35, -1), rendered_img="2balls1tri.ppm", upset=Point(0, 0, 0), move=Point(0, 0, 0)): self.width = width self.height = height self.rendered_img = rendered_img self.camera = camera self.upset = upset self.move = move self.objects = [ #chess plane is just a big sphere Sphere( 1, Point(0, 10000.5, 1), 10000.0, ChessMaterial( color1=Color.from_hex("#FFFFFF"), color2=Color.from_hex("#000000"), ambient=0.2, reflection=0.2, ), upset), Triangle(2, Point(0, 100, 100), Point(10, 11, 0), Point(10, 10, 11), Material(Color.from_hex("#8FFFFF")), self.upset), Triangle(3, Point(0, 100, 100), Point(10, 0, 100), Point(100, 10, 11), Material(Color.from_hex("#8FFFFF")), self.upset), Sphere(4, Point(0.75, -0.10, 1), 0.6, Material(Color.from_hex("#0000FF")), self.upset), Sphere(5, Point(-0.75, -0.1, 2.25), 0.6, Material(Color.from_hex("#803980")), self.upset, self.move), ] self.lights = [ Light(Point(1.5, -0.5, -10.0), Color.from_hex("#FFFFFF")), Light(Point(-0.5, -10.5, 0), Color.from_hex("#E6E6E6")) ]
def color_at(self, obj_hit, hit_pos, normal, scene): material = obj_hit.material obj_color = material.color_at(hit_pos) to_cam = scene.camera - hit_pos specular_k = 50 color = material.ambient * Color.from_hex("#000000") # Light calculations for light in scene.lights: to_light = Ray(hit_pos, light.position - hit_pos) #Diffuse shading (Lambert) #max prevents the negatives color += (obj_color * material.diffuse * max(normal.dot_product(to_light.direction), 0)) #Specular shading (Bling / Blinn Phong) #max prevents the negatives half_vector = (to_light.direction + to_cam).normalize() color += (light.color * material.specular * max(normal.dot_product(half_vector), 0)**specular_k) return color
def __init__(self, label, x, y, *args, **kwargs): self.label_font_name = kwargs.pop('title_font_name', 'Lucida Grande') self.label_font_size = kwargs.pop('title_font_size', 12) self.label_color = kwargs.pop('label_color', Color(255, 255, 255)) self.label_x_pad = kwargs.pop('label_x_pad', 4) self.label_y_pad = kwargs.pop('label_y_pad', 4) self.bg_color = kwargs.pop('bg_color', Color(255, 0, 0)) self.click_color = kwargs.pop('click_color', Color.from_hex('#4286f4')) super(Button, self).__init__(*args, **kwargs) self.x = x self.y = y self.width = None self.height = None self.text = label self.label = pyglet.text.Label(label) self.width, self.height = self.calc_button_size(self.label) self.frame = Rectangle(x, y, self.width, self.height, *args, **kwargs) self.vertices = self.frame.vertices print self.frame
def __init__(self, color=Color.from_hex("#FFFFFF"), color1 = None, color2 = None, ambient=0.05, diffuse=1, specular=1, reflection=0.2 ): """ Store the material color, ambient coefficient, diffuse coefficient, specular coefficient color1, color2 for checkerboard pattern """ self.color = color self.color1 = color1 self.color2 = color2 self.ambient = ambient self.diffuse = diffuse self.specular = specular self.reflection = reflection
def maxPixelRender(self,width,height,camera,pixels,scene,y0,ystep,x0,xstep): #Only raytraces a maximun ammount of #Gets a pixel pixels max= width*height*0.75 flag= True for j in range(height): y = y0 + j * ystep for i in range(width): x = x0 + i * xstep pixels.set_pixel(i, j, Color.from_hex("#000000"))#Creates a black pixel print("Terminado") while flag: #for j in range(height): j=random.randint(0,height-1) y = y0 + j * ystep i=random.randint(0,width-1) x = x0 + i * xstep #The raytrace of the pixel, otherwise creates a black pixel ray = Ray(camera, Point(x, y) - camera) #Creates a ray from the camera to the point pixels.set_pixel(i, j, self.ray_trace(ray, scene)) #Raytracing method max-=1 #Decreases the max pixels allowed if(max<=0): flag=False #print("{:3.0f}%".format(float(j) / float(height) * 100), end="\r") #Progress bar return pixels
def scene_4(): WIDTH = 320 HEIGHT = 200 camera = Vector(0, 0, -1) red = Color.from_hex("#FF0000") yellow = Color.from_hex("#FFFF00") green = Color.from_hex("#00FF00") ambient = Color.from_hex("#111111") diffuse = Color.from_hex("#FFFFFF") specular = Color.from_hex("#FFFFFF") material1 = Material(color=red) material2 = Material(color=yellow) material3 = Material(color=green) sphere1 = Sphere(Point(+0.3, 0.0, 0), 0.2, material1) sphere2 = Sphere(Point(+0.0, 0.0, 1), 0.2, material2) sphere3 = Sphere(Point(-0.3, 0.0, 2), 0.2, material3) light1 = Light(Point(10.5, -10.5, -10)) light2 = Light(Point(10.5, 10.5, -5)) lights = [light1, light2] # lights = [light1] objects = [sphere1, sphere2, sphere3] # objects = [sphere1] scene = Scene(camera, objects, lights, WIDTH, HEIGHT) return scene