def __init__(self, material, origin, hext): """Draws from the baseobject's attributes, but also has a vector representing the half extents.""" super().__init__(material, origin) self.hext = hext self.Xhat = vector.Vector3(1, 0, 0) self.Yhat = vector.Vector3(0, 1, 0) self.Zhat = vector.Vector3(0, 0, 1)
def renderOneLine(self, y): """Where most of the raytracer happens, the z in range determines the quality of anti aliasing""" for x in range(self.surf.get_width()): total = 0 color = vector.Vector3(0, 0, 0) temp_color = vector.Vector3(0, 0, 0) for z in range(1): L = [] dx = random.uniform(-1, 1) dy = random.uniform(-1, 1) pixelvector = vector.Vector2(x + dx, y + dy) otherpixel = vector.Vector2(x, y) e = math.exp(-((dx**2) / (2 * (0.333**2)) + (dy**2) / (2 * (0.333**2)))) total += e direction = (self.camera.getpixelpos(pixelvector) - self.camera.pos).normalized() ray = Ray(color, self.camera.getpixelpos(pixelvector), direction, 1) b = self.raycast(ray) temp_color += b * e avg_color = temp_color / total color = avg_color.clamp() color = (color * 255).i self.surf.set_at(otherpixel.i, color)
def draw(self): pyglet.gl.glPushMatrix() pyglet.gl.glTranslatef(*self.pos) pyglet.gl.glColor4f(*self.color) major = 0 if (self.normal.z != 0 or self.normal.y != 0): major = vector.Vector3(0, -self.normal.z, self.normal.y) else: major = vector.Vector3(-self.normal.z, 0, self.normal.x) minor = self.normal.cross(major) if (minor.magnitude() != 0): minor = minor.unit() * 2.0 if (major.magnitude() != 0): major = major.unit() * 2.0 Mx, My, Mz = major mx, my, mz = minor pyglet.graphics.draw_indexed( 4, pyglet.gl.GL_LINES, [0, 1, 1, 2, 2, 3, 3, 0], ('v3f', [ Mx + mx, My + my, Mz + mz, Mx - mx, My - my, Mz - mz, -Mx - mx, -My - my, -Mz - mz, -Mx + mx, -My + my, -Mz + mz ])) pyglet.gl.glPopMatrix()
def __init__(self, screen, res): self.pos = vector.Vector3(0, 0, 0) # Not actually a vector, x represents looking right/left, y is up/down, z is just rotating view # Default (0,0,0) is looking in the positive x direction (left handed coordinate system) self.dir = vector.Vector3(0, 0, 0) self.screen = screen # Screen to render camera on self.res = res # Resolution of camera self.fov = math.pi / 3 self.speed = 10
def __init__(self, p1, p2, p3): self.equation = [0, 0, 0, 0] self.origin = vector.Vector3() self.normal = vector.Vector3() self.normal = cross(p2 - p1, p3 - p1) self.normal = self.normal.normalized() self.origin = p1 self.equation[0] = self.normal.x self.equation[1] = self.normal.y self.equation[2] = self.normal.z self.equation[3] = -(self.normal.x * self.origin.x + self.normal.y * self.origin.y + self.normal.z * self.origin.z)
def barycentric(self, P): xaxis, yaxis = self.getAxes() pts3d = [] for p in self.mPoints: v = self.mPos + p[0] * xaxis + p[1] * yaxis pts3d.append(vector.Vector3(v.x, v.y, 0)) bcoord = [] den = self.mArea * 2.0 p3d = vector.Vector3(P.x, P.y, 0) bcoord.append(vector.cross(pts3d[1] - p3d, pts3d[2] - p3d).magnitude / den) bcoord.append(vector.cross(pts3d[0] - p3d, pts3d[2] - p3d).magnitude / den) bcoord.append(vector.cross(pts3d[0] - p3d, pts3d[1] - p3d).magnitude / den) return bcoord
def __init__(self): self.pos = vector.Vector3(1, 1, 1) self.rot = vector.Vector2() self.meshFaces = [] self.colliderFaces = [] self.isKinetic = True self.velocity = vector.Vector3() self.useGravity = True self.grounded = False self.fixedTime = .01 self.t = 0
def rayTest(self, R): """Returns a list of sorted Raycollisions or none if its empty.""" hits = [] hat_rack = [ self.Xhat, -self.Xhat, self.Yhat, -self.Yhat, self.Zhat, -self.Zhat ] hext_test = [ self.hext.x, self.hext.x, self.hext.y, self.hext.y, self.hext.z, self.hext.z ] for i in range(6): norm = hat_rack[i] d = vector.dot((self.origin + (hext_test[i] * norm)), norm) if vector.dot(R.Dhat, norm) != 0: t = (d - vector.dot(R.origin, norm)) / (vector.dot( R.Dhat, norm)) if t < 0: continue Pw = R.getPoint(t) Q = Pw - self.origin Pl = vector.Vector3(vector.dot(Q, self.Xhat), vector.dot(Q, self.Yhat), vector.dot(Q, self.Zhat)) if -self.hext.x - 0.0001 <= Pl.x and Pl.x <= self.hext.x + 0.0001: if -self.hext.y - 0.0001 <= Pl.y and Pl.y <= self.hext.y + 0.0001: if -self.hext.z - 0.0001 <= Pl.z and Pl.z <= self.hext.z + 0.0001: hits += [ Raycollision(R, self, t, R.getPoint(t), norm) ] return hits
def lighting(self, lights, camera, transform): temp_color = Vector3(0, 0, 0) supern = (self.normal * transform).normalized() for l in range(len(lights)): center = self.center * transform amb = self.scene.pairwise_mult(self.md[self.material]["amb"]) temp_color += amb n = (lights[l].pos - center).normalized() o = vector.dot(n, supern) if o < 0: pass else: r = 2 * o * supern - n i = VectorN(camera.pos.x, camera.pos.y, camera.pos.z, 1) v = (i - center).normalized() m = vector.dot(v, r) diff_c = o * lights[l].diff.pairwise_mult( self.md[self.material]["diff"]) if m < 0: spec_c = vector.Vector3(0, 0, 0) else: spec_c = m ** self.md[self.material]["hard"] * \ (lights[l].spec.pairwise_mult(self.md[self.material]["spec"])) temp_color += spec_c + diff_c return temp_color.clamp()
def matrix_x_vector(m: Matrix4, i: Vector3): v = vector.Vector3( i.x * m.m[0][0] + i.y * m.m[1][0] + i.z * m.m[2][0] + i.w * m.m[3][0], i.x * m.m[0][1] + i.y * m.m[1][1] + i.z * m.m[2][1] + i.w * m.m[3][1], i.x * m.m[0][2] + i.y * m.m[1][2] + i.z * m.m[2][2] + i.w * m.m[3][2], i.x * m.m[0][3] + i.y * m.m[1][3] + i.z * m.m[2][3] + i.w * m.m[3][3] ) return v
def pygameDraw(self, surf): """Draws the plane line to the surf.""" if abs(self.norm[1]) >= abs(self.norm[0]): # Horizontal self.B = vector.Vector3( self.width, ((self.distance - self.width * self.norm.x) / self.norm.y), 0) self.startpos = vector.Vector3(0, (self.distance / self.norm.y), 0) else: # Vertical self.B = vector.Vector3( (self.distance - self.height * self.norm.y) / self.norm.x, self.height, 0) self.startpos = vector.Vector3((self.distance / self.norm.x), 0, 0) pygame.draw.line(surf, (self.material["diff"] * 255).i, (int(self.startpos.x), int(self.startpos.y)), (int(self.B.x), int(self.B.y)), 1)
def __init__(self, color, pos, points): super().__init__(color, pos) self.mPoints = [] self.mPoints3d = [] for p in points: v = p - self.mPos self.mPoints.append(v) self.mPoints3d.append(vector.Vector3(v.x, v.y, 0)) self.mArea = vector.cross(self.mPoints3d[1] - self.mPoints3d[0], self.mPoints3d[2] - self.mPoints3d[0]).magnitude / 2.0
def __init__(self): self.res = (960, 540) self.screen = pygame.display.set_mode(self.res) self.clock = pygame.time.Clock() self.cam = camera.Camera(self.screen, self.res) self.font_log = pygame.font.SysFont(None, 24) #textrect = text.get_rect() #textrect.centerx = screen.get_rect().centerx #textrect.centery = screen.get_rect().centery self.mouseLock = False self.polyList = [ poly.Poly([ vector.Vector3(10, 1, 0), vector.Vector3(10, 0, 1), vector.Vector3(10, 0, -1) ]) ] for i in range(100): self.polyList.append( poly.Poly([ vector.Vector3(10 + 5 * math.sin(i / 20), 1 + i / 10, 0), vector.Vector3(10 + 5 * math.sin(i / 20), 0, 1 + i / 10), vector.Vector3(10 + 5 * math.sin(i / 20), 0, -1 - i / 10) ]))
def draw(self, surf, camera): """ draw function for actors, currently animates basic temporary sprites :param surf: pygame style Surface :param camera: camera object for world coordinate conversion :return: nothing """ if self.is_dead(): # dead men tell no tales return temp_rect = camera.apply(self.current_pos) # create temporary list to protect the current position # converts from world coord to screen coords temp_cube = Cuboid(vector.Vector3(0, 0, 0), self.current_weapon.Hitbox.mPos.copy(), vector.Vector2(48, 4)) temp_cube.mAngle = self.current_weapon.Hitbox.mAngle temp_vec = camera.apply(temp_cube.mPos) temp_cube.mPos = temp_vec if isinstance(self, Enemy) and self.is_chasing: if self.sprite_row == 4 or self.sprite_row == 1 or self.sprite_row == 2 or self.sprite_row == 3 or self.sprite_row == 0: x = temp_cube.mPos.x - self.current_weapon.rotated_weap_img.get_width() // 2 y = temp_cube.mPos.y - self.current_weapon.rotated_weap_img.get_height() // 2 surf.blit(self.current_weapon.rotated_weap_img, (x, y)) # Drawing the actor from the sprite sheet using known attributes, and offsets the sprite so current_pos is the #... center of the sprite surf.blit(self.sprite_sheet, (temp_rect[0] - (self.sprite_size // 2), temp_rect[1] - (self.sprite_size // 2)), (self.sprite_column * self.sprite_size + self.sprite_offset[0], self.sprite_row * self.sprite_size + self.sprite_offset[1], self.sprite_size, self.sprite_size)) if isinstance(self, Enemy) and self.is_chasing: if self.sprite_row == 5 or self.sprite_row == 6 or self.sprite_row == 7: x = temp_cube.mPos.x - self.current_weapon.rotated_weap_img.get_width()//2 y = temp_cube.mPos.y - self.current_weapon.rotated_weap_img.get_height()//2 surf.blit(self.current_weapon.rotated_weap_img, (x, y)) pygame.draw.rect(surf, (0, 0, 0), (temp_rect[0] - 20, temp_rect[1] - 20, 40, 3), 0) pygame.draw.rect(surf, (255, 0, 0), (temp_rect[0] - 20, temp_rect[1] - 20, 40 * (self.health/self.max_health), 3), 0) elif isinstance(self, NPC) and self.gui_runner.Visible: self.gui_runner.Draw(surf)
def draw(self, surf, camera): """ draw function for actors, currently animates basic temporary sprites :param surf: pygame style Surface :param camera: camera object for world coordinate conversion :return: nothing """ if self.is_dead(): # dead men tell no tales return # print(self.current_weapon) # print(self.Spear.Hitbox) temp_cube = Cuboid(vector.Vector3(0, 0, 0), self.current_weapon.Hitbox.mPos.copy(), vector.Vector2(48, 4)) # temp_cube = Cuboid(vector.Vector3(0, 0, 0), self.current_weapon.Hitbox.mPos.copy(), vector.Vector2(self.current_weapon.Hitbox.mExtents[0] * 2, self.current_weapon.Hitbox.mExtents[1] * 2)) temp_cube.mAngle = self.current_weapon.Hitbox.mAngle temp_vec = camera.apply(temp_cube.mPos) temp_cube.mPos = temp_vec # ##--Lane(Weapon_drawing)--## if self.current_weapon.weapon_drawn is True: # if weapon_drawn is True then draw weapon hit box if self.sprite_row == 4 or self.sprite_row == 1 or self.sprite_row == 2 or self.sprite_row == 3 or self.sprite_row == 0: x = temp_cube.mPos.x - self.current_weapon.rotated_weap_img.get_width()//2 y = temp_cube.mPos.y - self.current_weapon.rotated_weap_img.get_height()//2 surf.blit(self.current_weapon.rotated_weap_img, (x, y)) #temp_cube.drawPygame(surf) temp_rect = camera.apply(self.current_pos) # create temporary list to protect the current position # converts from world coord to screen coords # Drawing the actor from the sprite sheet using known attributes, and offsets the sprite so current_pos is the # ... center of the sprite surf.blit(self.sprite_sheet, (temp_rect[0] - (self.sprite_size // 2), temp_rect[1] - (self.sprite_size // 2)), (self.sprite_column * self.sprite_size + self.sprite_offset[0], self.sprite_row * self.sprite_size + self.sprite_offset[1], self.sprite_size, self.sprite_size)) # ##--Lane(Weapon_drawing)--## if self.current_weapon.weapon_drawn is True: # if weapon_drawn is True then draw weapon hit box if self.sprite_row == 5 or self.sprite_row == 6 or self.sprite_row == 7: x = temp_cube.mPos.x - self.current_weapon.rotated_weap_img.get_width()//2 y = temp_cube.mPos.y - self.current_weapon.rotated_weap_img.get_height()//2 surf.blit(self.current_weapon.rotated_weap_img, (x, y)) pygame.draw.rect(surf, (0, 0, 0), (temp_rect[0] - 20, temp_rect[1] - 20, 40, 3), 0) pygame.draw.rect(surf, (255, 255, 0), (temp_rect[0] - 20, temp_rect[1] - 20, 40 * (self.health/self.max_health), 3), 0)
def pygameDraw(self, surf): """Draws the box to the surf""" # First Box A_local = vector.Vector3(-self.hext.x, -self.hext.y, -self.hext.z) B_local = vector.Vector3(self.hext.x, -self.hext.y, -self.hext.z) C_local = vector.Vector3(self.hext.x, self.hext.y, -self.hext.z) D_local = vector.Vector3(-self.hext.x, self.hext.y, -self.hext.z) A_world = self.origin + A_local.x * self.Xhat + A_local.y * self.Yhat + A_local.z * self.Zhat B_world = self.origin + B_local.x * self.Xhat + B_local.y * self.Yhat + B_local.z * self.Zhat C_world = self.origin + C_local.x * self.Xhat + C_local.y * self.Yhat + C_local.z * self.Zhat D_world = self.origin + D_local.x * self.Xhat + D_local.y * self.Yhat + D_local.z * self.Zhat # Second Box A2_local = vector.Vector3(-self.hext.x, -self.hext.y, self.hext.z) B2_local = vector.Vector3(self.hext.x, -self.hext.y, self.hext.z) C2_local = vector.Vector3(self.hext.x, self.hext.y, self.hext.z) D2_local = vector.Vector3(-self.hext.x, self.hext.y, self.hext.z) A2_world = self.origin + A2_local.x * self.Xhat + A2_local.y * self.Yhat + A2_local.z * self.Zhat B2_world = self.origin + B2_local.x * self.Xhat + B2_local.y * self.Yhat + B2_local.z * self.Zhat C2_world = self.origin + C2_local.x * self.Xhat + C2_local.y * self.Yhat + C2_local.z * self.Zhat D2_world = self.origin + D2_local.x * self.Xhat + D2_local.y * self.Yhat + D2_local.z * self.Zhat # Drawing the boxes pygame.draw.line(surf, (self.material["diff"] * 255).i, (A_world.x, A_world.y), (A2_world.x, A2_world.y), 1) pygame.draw.line(surf, (self.material["diff"] * 255).i, (B_world.x, B_world.y), (B2_world.x, B2_world.y), 1) pygame.draw.line(surf, (self.material["diff"] * 255).i, (C_world.x, C_world.y), (C2_world.x, C2_world.y), 1) pygame.draw.line(surf, (self.material["diff"] * 255).i, (D_world.x, D_world.y), (D2_world.x, D2_world.y), 1) pygame.draw.polygon(surf, (self.material["diff"] * 255).i, ((A_world.x, A_world.y), (B_world.x, B_world.y), (C_world.x, C_world.y), (D_world.x, D_world.y)), 1) pygame.draw.polygon(surf, (self.material["diff"] * 255).i, ((A2_world.x, A2_world.y), (B2_world.x, B2_world.y), (C2_world.x, C2_world.y), (D2_world.x, D2_world.y)), 1)
def __init__(self, w, h): self.w = w self.h = h self.cx = w // 2 self.cy = h // 2 self.pos = vector.Vector3() self.rot = vector.Vector2() self.half_w, self.half_h = w / 2, h / 2 self.setFov(90) """ self.fov = 90/180*math.pi; self.half_fov = self.fov/2 self.half_w, self.half_h = w/2,h/2 self.projY = self.half_h/math.tan(self.half_fov) self.projX = self.half_w/math.tan(self.half_fov)/(w/h) """ os.environ['SDL_VIDEO_CENTERED'] = '1' self.display = pygame.display.set_mode((w, h))
self.normal = cross(p2 - p1, p3 - p1) #self.normal = np.cross(p2-p1, p3-p1) self.normal = self.normal.normalized() self.origin = p1 self.equation[0] = self.normal.x self.equation[1] = self.normal.y self.equation[2] = self.normal.z self.equation[3] = -(self.normal.x * self.origin.x + self.normal.y * self.origin.y + self.normal.z * self.origin.z) # bool of whether direction intersects tri def isFrontFacingTo(self, direction): dot_product = dot(self.normal, direction) #print(dot_product) return dot_product <= 0 def signedDistanceTo(self, point): print(self.equation[3]) return dot(point, self.normal) + self.equation[3] tri = Plane() tri.plane_triangle( vector.Vector3(1, 1, 0), # 0,0,0 vector.Vector3(2, 1, 0), # 1,0,0 vector.Vector3(2, 2, 0)) # 1,0,1 #print(tri.isFrontFacingTo(vector.Vector3(-1,0,0))) print(tri.signedDistanceTo(vector.Vector3(743, 5000, 6)))
def raycast(self, ray): L = [] maxi = 0 weight = ray.weight temp_color = vector.Vector3(0, 0, 0) for s in range(len(self.shapelist)): a = self.shapelist[s].rayTest(ray) L += a if L == []: background = self.scene.pairwise_mult(vector.Vector3( 0.5, 0.5, 0.5)) temp_color += background else: L.sort() amb_c = self.scene.pairwise_mult(L[0].otherobject.material["amb"]) temp_color += amb_c for l in self.lightlist: n = (l.pos - L[0].point).normalized() o = vector.dot(n, L[0].normal) if o < 0: pass else: r = 2 * o * L[0].normal - n v = (self.camera.pos - L[0].point).normalized() m = vector.dot(v, r) if m > maxi: maxi = m shadow = False c = Ray(L[0].otherobject.material, L[0].point + 0.0001 * L[0].normal, n, weight) abba = ((L[0].point + 0.0001 * L[0].normal) - l.pos).magnitude() for s in self.shapelist: temp = s.rayTest(c) for z in temp: if z.distance <= abba: shadow = True elif z.distance >= abba and shadow is not True: shadow = False if shadow is False: diff_c = o * l.diff.pairwise_mult( L[0].otherobject.material["diff"]) if m < 0: spec_c = vector.Vector3(0, 0, 0) else: spec_c = m ** L[0].otherobject.material["shiny"] * \ (l.spec.pairwise_mult(L[0].otherobject.material["spec"])) temp_color += spec_c + diff_c if maxi > 0: weight -= maxi if weight < 0: weight = 0 z = 2 * (vector.dot(-ray.Dhat, L[0].normal) * L[0].normal - (-ray.Dhat)) newray = Ray(L[0].otherobject.material, L[0].point + (z), z, 1) o = self.raycast(newray) temp_color = weight * temp_color + (1 - weight) * o return temp_color
def __init__(self, material, origin, objfile): """Draws from the baseobject's attributes, but also takes an objfile exported from blender.""" super().__init__(material, origin) self.Xhat = vector.Vector3(1, 0, 0) self.Yhat = vector.Vector3(0, 1, 0) self.Zhat = vector.Vector3(0, 0, 1) self.vlist = [] self.plist = [] self.nlist = [] self.max_x = 0 self.max_y = 0 self.max_z = 0 fp = open(objfile) for line in fp: line.strip() if line[0] == "v" and line[1] == "n": deparsed = line.split("vn ") deparsed.remove(deparsed[0]) for i in range(1): refined = deparsed[i].split("\n") refined.remove(refined[-1]) almost = refined[0].split(" ") temp_n = [] for i in range(len(almost)): temp_n += [float(almost[i])] x = vector.Vector3(temp_n[0], temp_n[1], temp_n[2]) self.nlist.append(x) if line[0] == "v" and line[1] != "n": parsed = line.split(" ") temp_v = [] for i in parsed: if i != "v": temp_v.append(float(i)) x = vector.Vector3(temp_v[0], temp_v[1], temp_v[2]) self.vlist.append(x) if line[0] == "f": unparsed = line.split("f ") for i in unparsed: if i == "": unparsed.remove(i) refined = unparsed[0].split(" ") refined[-1] = refined[-1].rstrip("\n") rl = [] for i in refined: almost = i.split("//") rl += almost temp_p = [] for r in rl: temp_p.append(int(r) - 1) self.plist.append(temp_p) for i in self.vlist: if abs(i.x) > self.max_x: self.max_x = abs(i.x) if abs(i.y) > self.max_y: self.max_y = abs(i.y) if abs(i.z) > self.max_z: self.max_z = abs(i.z) hext = vector.Vector3(self.max_x, self.max_y, self.max_z) self.boundbox = Box(self.material, self.origin, hext) self.tlist = [] for i in range(len(self.plist)): a = self.plist[i][0] b = self.plist[i][2] c = self.plist[i][4] a1 = self.plist[i][1] b2 = self.plist[i][3] c2 = self.plist[i][5] n1 = self.nlist[a1] n2 = self.nlist[b2] n3 = self.nlist[c2] v1 = self.vlist[a] v2 = self.vlist[b] v3 = self.vlist[c] p1 = self.origin + v1.x * self.Xhat + v1.y * self.Yhat + v1.z * self.Zhat p2 = self.origin + v2.x * self.Xhat + v2.y * self.Yhat + v2.z * self.Zhat p3 = self.origin + v3.x * self.Xhat + v3.y * self.Yhat + v3.z * self.Zhat triangle = Triangle(self.material, p1, p2, p3, n1, n2, n3) self.tlist.append(triangle)
def __init__(self): self.equation = [0, 0, 0, 0] self.origin = vector.Vector3() self.normal = vector.Vector3()
def cross(a, b): c = vector.Vector3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x) return c
Main_Menu_Music = pygame.mixer.Sound("Assets/Sounds/MainMusic.wav") Start_Button = pygame.mixer.Sound("Assets/Sounds/Main_Menu_Effect_1.wav") Menu_Sound = pygame.mixer.Sound("Assets/Sounds/Menu_Sounds.wav") Good_Image = pygame.image.load("Assets/Good_Image.png") Evil_Image = pygame.image.load("Assets/Evil_Image.png") Title = pygame.font.SysFont("Arial", 70) Start = pygame.font.SysFont("Arial", 45) Quit = pygame.font.SysFont("Arial", 45) Select = pygame.font.SysFont("Arial", 55) Text = Title.render("Journey To Vauss", True, (255, 215, 0)) Start_Object = Start.render("Start", True, (255, 215, 0)) Quit_Object = Quit.render("Quit", True, (255, 215, 0)) Select = Select.render("Select your destiny ...it's your choice.", True, (255, 215, 0)) Start_Bounds = Cuboid(vector.Vector3(0, 0, 0), vector.Vector2(485, 465), vector.Vector2(90, 40)) Quit_Bounds = Cuboid(vector.Vector3(255, 255, 255), vector.Vector2(485, 515), vector.Vector2(90, 40)) Good_Bounds = Cuboid(vector.Vector3(255, 255, 255), vector.Vector2(200, 300), vector.Vector2(390, 590)) Evil_Bounds = Cuboid(vector.Vector3(0, 0, 0), vector.Vector2(600, 300), vector.Vector2(390, 590)) Sword_Image = pygame.image.load('Assets/Sword_Image.png').convert_alpha() theMap = m.Map("Test_map") done = 0 game_won = 0 In_Menu = 1