Exemple #1
0
    def __init__(self,
                 screen_width=400,
                 screen_height=300,
                 position=vec3(0., 0.25, 1.),
                 look_at=vec3(0., 0.25, 0.),
                 camera_width=1.,
                 camera_depth=1.):
        self.screen_width = screen_width
        self.screen_height = screen_height
        self.camera_screen_scale = float(screen_width) / screen_height

        self.position = position
        self.camera_width = camera_width
        self.camera_height = camera_width / self.camera_screen_scale
        self.camera_depth = camera_depth
        self.cameraFwd = (look_at - position).normalize()
        self.cameraRight = (self.cameraFwd.cross(vec3(0., 1., 0.))).normalize()
        self.cameraUp = self.cameraRight.cross(self.cameraFwd)

        # Screen coordinates:
        x = np.linspace(-self.camera_width, self.camera_width,
                        self.screen_width)
        y = np.linspace(self.camera_height, -self.camera_height,
                        self.screen_height)

        xx, yy = np.meshgrid(x, y)
        x = xx.flatten()
        y = yy.flatten()

        self.ray_dir = (self.cameraUp * y + self.cameraRight * x +
                        self.cameraFwd * self.camera_depth).normalize()
    def __init__(self, ss):
        self.ss = ss
        self.camera = Camera.Camera(self)
        self.camera.offset.x = self.ss[0]/2
        
        self.rock_tile_image = pygame.image.load("res/RockIso.png").convert()
        self.base_tile_image = pygame.image.load("res/BaseIso.png").convert()
        self.water_tile_image = pygame.image.load("res/WaterIso.png").convert()
        self.snow_tile_image = pygame.image.load("res/SnowIso.png").convert()
        self.rock_tile_image.set_colorkey((255, 0, 255))
        self.base_tile_image.set_colorkey((255, 0, 255))
        self.water_tile_image.set_colorkey((255, 0, 255))
        self.snow_tile_image.set_colorkey((255, 0, 255))


        self.world_size = (32, 32)
        self.map_generator = md.terrain(5, 1)
        self.map_generator.generate(.5)
        #print len(self.map_generator.map)
        #self.map_array = self.map_generator.whole_new(20, self.world_size)

        max_height = 0

        self.tiles = []
        for i in range(self.world_size[0]):
            previous_offset = 0.0
            for j in range(self.world_size[1]):
                #previous_offset += random.randint(-2,2)/40.0
                previous_offset = self.map_generator.map[i][j] / 16.0
                max_height = max(previous_offset, max_height)
                self.tiles.append(Tile.Tile(vec3(i,j,previous_offset), self))

        print max_height
Exemple #3
0
 def __init__(self, file_name, center, material, shadow=True):
     super().__init__(center, material, shadow=shadow)
     self.collider_list += []
     vs = []
     fs = []
     with open(file_name, 'r') as f:
         r = f.read()
         r = r.split('\n')
         for i in r:
             i = i.split()
             if not i:
                 continue
             elif i[0] == 'v':
                 x = float(i[1])
                 y = float(i[2])
                 z = float(i[3])
                 vs.append(vec3(x, y, z))
             elif i[0] == 'f':
                 f1 = int(i[1].split('/')[0]) - 1
                 f2 = int(i[2].split('/')[0]) - 1
                 f3 = int(i[3].split('/')[0]) - 1
                 fs.append([f1, f2, f3])
     for i in fs:
         p1 = vs[i[0]] + center
         p2 = vs[i[1]] + center
         p3 = vs[i[2]] + center
         self.collider_list += [
             colliders.Triangle_Collider(assigned_surface=self,
                                         p1=p1,
                                         p2=p2,
                                         p3=p3)
         ]
Exemple #4
0
    def __init__(self, ss):
        self.ss = ss
        self.camera = Camera.Camera(self)
        self.camera.offset.x = self.ss[0] / 2

        self.rock_tile_image = pygame.image.load("res/RockIso.png").convert()
        self.base_tile_image = pygame.image.load("res/BaseIso.png").convert()
        self.water_tile_image = pygame.image.load("res/WaterIso.png").convert()
        self.snow_tile_image = pygame.image.load("res/SnowIso.png").convert()
        self.rock_tile_image.set_colorkey((255, 0, 255))
        self.base_tile_image.set_colorkey((255, 0, 255))
        self.water_tile_image.set_colorkey((255, 0, 255))
        self.snow_tile_image.set_colorkey((255, 0, 255))

        self.world_size = (32, 32)
        self.map_generator = md.terrain(5, 1)
        self.map_generator.generate(.5)
        #print len(self.map_generator.map)
        #self.map_array = self.map_generator.whole_new(20, self.world_size)

        max_height = 0

        self.tiles = []
        for i in range(self.world_size[0]):
            previous_offset = 0.0
            for j in range(self.world_size[1]):
                #previous_offset += random.randint(-2,2)/40.0
                previous_offset = self.map_generator.map[i][j] / 16.0
                max_height = max(previous_offset, max_height)
                self.tiles.append(Tile.Tile(vec3(i, j, previous_offset), self))

        print max_height
Exemple #5
0
    def __init__(self,
                 ambient_color=rgb(0.01, 0.01, 0.01),
                 n=vec3(1.0, 1.0, 1.0)):
        # n = index of refraction (by default index of refraction of air)

        self.scene_surfaces = []
        self.collider_list = []
        self.shadowed_collider_list = []
        self.Light_list = []
        self.ambient_color = ambient_color
        self.n = n
Exemple #6
0
    def __init__(self, center, material, width, height, length, shadow=True):
        super().__init__(center, material, shadow=shadow)
        self.height = height
        self.length = length

        #we model a cube as six planes
        w, h, l = width / 2, height / 2, length / 2

        #BOTTOM                                                                                                                                       #BOTTOM
        self.collider_list += [
            colliders.Plane_Collider(assigned_surface=self,
                                     center=center + vec3(0.0, -h, 0.0),
                                     pu=vec3(1.0, 0.0, 0.0),
                                     pv=vec3(0.0, 0.0, 1.0),
                                     w=w,
                                     h=l,
                                     uv_shift=(1, 0))
        ]
        #TOP                                                                                                                                       #TOP
        self.collider_list += [
            colliders.Plane_Collider(assigned_surface=self,
                                     center=center + vec3(0.0, h, 0.0),
                                     pu=vec3(1.0, 0.0, 0.0),
                                     pv=vec3(0.0, 0.0, -1.0),
                                     w=w,
                                     h=l,
                                     uv_shift=(1, 2))
        ]
        #RIGHT                                                                                                                                       #RIGHT
        self.collider_list += [
            colliders.Plane_Collider(assigned_surface=self,
                                     center=center + vec3(w, 0.0, 0.0),
                                     pu=vec3(0.0, 0.0, -1.0),
                                     pv=vec3(0.0, 1.0, 0.0),
                                     w=l,
                                     h=h,
                                     uv_shift=(2, 1))
        ]
        #LEFT                                                                                                                                       #LEFT
        self.collider_list += [
            colliders.Plane_Collider(assigned_surface=self,
                                     center=center + vec3(-w, 0.0, 0.0),
                                     pu=vec3(0.0, 0.0, 1.0),
                                     pv=vec3(0.0, 1.0, 0.0),
                                     w=l,
                                     h=h,
                                     uv_shift=(0, 1))
        ]
        #FRONT                                                                                                                                       #FRONT
        self.collider_list += [
            colliders.Plane_Collider(assigned_surface=self,
                                     center=center + vec3(0, 0, l),
                                     pu=vec3(1.0, 0.0, 0.0),
                                     pv=vec3(0.0, 1.0, 0.0),
                                     w=w,
                                     h=h,
                                     uv_shift=(1, 1))
        ]
        #BACK                                                                                                                                       #BACK
        self.collider_list += [
            colliders.Plane_Collider(assigned_surface=self,
                                     center=center + vec3(0, 0, -l),
                                     pu=vec3(-1.0, 0.0, 0.0),
                                     pv=vec3(0.0, 1.0, 0.0),
                                     w=w,
                                     h=h,
                                     uv_shift=(3, 1))
        ]
Exemple #7
0
    def __init__(self, cubemap, center=vec3(0., 0., 0.)):
        super().__init__(center,
                         materials.SkyBox_Material(cubemap),
                         shadow=False)
        l = SKYBOX_DISTANCE

        # 1.005 factor to avoid skybox corners
        #BOTTOM                                                                                                                                       #BOTTOM
        self.collider_list += [
            colliders.Plane_Collider(assigned_surface=self,
                                     center=center + vec3(0, -l, 0.0),
                                     pu=vec3(1.0, 0.0, 0.0),
                                     pv=vec3(0.0, 0.0, -1.0),
                                     w=l * 1.005,
                                     h=l * 1.005,
                                     uv_shift=(1, 0))
        ]
        #TOP                                                                                                                                           #TOP
        self.collider_list += [
            colliders.Plane_Collider(assigned_surface=self,
                                     center=center + vec3(0, l, 0.0),
                                     pu=vec3(1.0, 0.0, 0.0),
                                     pv=vec3(0.0, 0.0, 1.0),
                                     w=l * 1.005,
                                     h=l * 1.005,
                                     uv_shift=(1, 2))
        ]
        #RIGHT                                                                                                                                       #RIGHT
        self.collider_list += [
            colliders.Plane_Collider(assigned_surface=self,
                                     center=center + vec3(l, 0, 0),
                                     pu=vec3(0.0, 0.0, 1.0),
                                     pv=vec3(0.0, 1.0, 0.0),
                                     w=l * 1.005,
                                     h=l * 1.005,
                                     uv_shift=(2, 1))
        ]
        #LEFT                                                                                                                                       #LEFT
        self.collider_list += [
            colliders.Plane_Collider(assigned_surface=self,
                                     center=center + vec3(-l, 0, 0),
                                     pu=vec3(0.0, 0.0, -1.0),
                                     pv=vec3(0.0, 1.0, 0.0),
                                     w=l * 1.005,
                                     h=l * 1.005,
                                     uv_shift=(0, 1))
        ]
        #FRONT                                                                                                                                       #FRONT
        self.collider_list += [
            colliders.Plane_Collider(assigned_surface=self,
                                     center=center + vec3(0, 0, l),
                                     pu=vec3(-1.0, 0.0, 0.0),
                                     pv=vec3(0.0, 1.0, 0.0),
                                     w=l * 1.005,
                                     h=l * 1.005,
                                     uv_shift=(3, 1))
        ]
        #BACK                                                                                                                                       #BACK
        self.collider_list += [
            colliders.Plane_Collider(assigned_surface=self,
                                     center=center + vec3(0, 0, -l),
                                     pu=vec3(1.0, 0.0, 0.0),
                                     pv=vec3(0.0, 1.0, 0.0),
                                     w=l * 1.005,
                                     h=l * 1.005,
                                     uv_shift=(1, 1))
        ]
Exemple #8
0
 def get_texture_color(self, M, collider, D, O):
     u,v = self.assigned_surface.get_uv(M, collider)
     im = self.texture[-((v * self.texture.shape[0]*self.repeat ).astype(int)% self.texture.shape[0]) , (u   * self.texture.shape[1]*self.repeat).astype(int) % self.texture.shape[1]  ].T
     color = vec3(im[0],im[1],im[2])
     return color*self.diff_color
Exemple #9
0
    def shader(self, collider, ray_origin, ray_dir,  hit_distance ,hit_orientation, scene, ray_depth, ray_n):
        M = (ray_origin + ray_dir * hit_distance)                     # intersection point
        N = collider.get_Normal(M).normalize()  * hit_orientation     # normal 

        diff_color = self.get_diffuse_color(M, collider, ray_dir, ray_origin) * self.diff_c

        # Ambient
        color = scene.ambient_color * diff_color

                
        V = ray_dir*-1.                            # direction to ray origin
        nudged = M + N * .000001                  # M nudged to avoid itself


        # compute reflection and refraction 
        # a paper explaining formulas used: 
        # https://graphics.stanford.edu/courses/cs148-10-summer/docs/2006--degreve--reflection_refraction.pdf
        # reallistic refraction is expensive. (requires exponential complexity because each ray is divided in two)

        if ray_depth < self.max_ray_depth:

            """
            if hit_orientation== UPWARDS:
               #ray enter in the material
            if hit_orientation== UPDOWN:
               #ray get out of the material   
            """
            n1 = ray_n
            n2 = vec3.where(hit_orientation== UPWARDS,self.n,scene.n)

            n1_div_n2 =  vec3.real(n1)/vec3.real(n2) 
            cosθi = V.dot(N)
            sin2θt = (n1_div_n2)**2 * (1.-cosθi**2)

            # compute complete fresnel term
            cosθt = vec3.sqrt(1. - (n1/n2)**2 * (1.-cosθi**2)  )
            r_per = (n1*cosθi - n2*cosθt)/(n1*cosθi + n2*cosθt)
            r_par = -1.*(n1*cosθt - n2*cosθi)/(n1*cosθt + n2*cosθi) 
            F = (np.abs(r_per)**2 + np.abs(r_par)**2)/2.
            
            # compute reflection
            reflected_ray_dir = (ray_dir - N * 2. * ray_dir.dot(N)).normalize()
            color += (raytrace(nudged, reflected_ray_dir, scene, ray_depth + 1,ray_n))*F
            


            # compute refraction 
            # Spectrum dispersion is not implemented. 
            # We approximate refraction direction averaging index of refraction of each wavelenght
            n1_div_n2_aver = n1_div_n2.average()
            sin2θt = (n1_div_n2_aver)**2 * (1.-cosθi**2)

            non_TiR = (sin2θt <= 1.)
            if np.any(non_TiR): # avoid total internal reflection

                refracted_ray_dir = (ray_dir*(n1_div_n2_aver) + N*(n1_div_n2_aver * cosθi - np.sqrt(1-np.clip(sin2θt,0,1)))).normalize() 
                nudged = M - N * .000001  #nudged for refraction
                T = 1. - F
                refracted_color = (raytrace(nudged.extract(non_TiR), 
                                           refracted_ray_dir.extract(non_TiR), 
                                           scene, 
                                           ray_depth + 1, 
                                           n2.extract(non_TiR)))*T.extract(non_TiR) 
                color += refracted_color.place(non_TiR)
            
                
            # absorption:
            # approximation using wavelength for red = 630 nm, green 550 nm, blue 475 nm
            color = color *vec3.exp(-2.*vec3.imag(ray_n)*2.*np.pi/vec3(630,550,475) * 1e9* hit_distance)
        return color