def key_event(window, key, scancode, action, mode): global eye if action == glfw.PRESS and key == glfw.KEY_RIGHT: rot = 5 rot = mat3.create_from_axis_rotation(vec3([0.0, 1.0, 0.0]), np.radians(rot)) eye = rot.dot(eye) elif action == glfw.PRESS and key == glfw.KEY_LEFT: rot = -5 rot = mat3.create_from_axis_rotation(vec3([0.0, 1.0, 0.0]), np.radians(rot)) eye = rot.dot(eye) if action == glfw.PRESS and key == glfw.KEY_UP: rot = 5 right = np.cross(center - eye, up) rot = mat3.create_from_axis_rotation(vec3(right), np.radians(rot)) eye = rot.dot(eye) elif action == glfw.PRESS and key == glfw.KEY_DOWN: rot = -5 right = np.cross(center - eye, up) rot = mat3.create_from_axis_rotation(vec3(right), np.radians(rot)) eye = rot.dot(eye) if action == glfw.PRESS and key == glfw.KEY_S: eye = 1.01 * eye elif action == glfw.PRESS and key == glfw.KEY_W: eye = 0.99 * eye if action == glfw.PRESS and key == glfw.KEY_SPACE: create_pes()
def initCamera(): eye = vec3([200, 200, 200], dtype='f') target = vec3([0, 0, 0], dtype='f') up = vec3([0, 1, 0], dtype='f') viewMatrix = lookAt(eye, target, up) return eye, viewMatrix
def colorRay(ray: Ray, world: Shape, depth: int) -> vec3: rec = HitRecord() if world.hit(ray, 0.001, MAXFLOAT, rec): scattered = Ray(vec3([0.0, 0.0, 0.0]), vec3([0.0, 0.0, 0.0])) attenuation = vec3() if depth < 50 and rec.material.scatter(ray, rec, attenuation, scattered): return attenuation * colorRay(scattered, world, depth + 1) else: return vec3([0.0, 0.0, 0.0]) return blueBlend(ray)
def __init__(self, name=None, position=None, texImg=None, specTexImg=None, normalMap=None, radius=None, mass=None, spin=None, shininess=None, ka=None, kd=None, ks=None, program=None): GameObject.__init__(self, name=name, position=position, texImg=texImg, specTexImg=specTexImg, normalMap=None, shininess=shininess, ka=ka, kd=kd, ks=ks, program=program) self.velocity = vec3([0,0,0]) self.radius = radius self.mass = mass self.spin = spin self.rot = 0 self._initModel()
def getNewViewMatrixAndEye(window, animation_speed, dt, position, width=1920.0, height=1080.0): """ Kind of global function. Mainly handles camera which behaves as an FPS camera. Additionally it had some input handling added to it(see animation_speed). """ global horizontalAngle, verticalAngle global oldx, oldy, first if first: oldx, oldy = glfw.GetCursorPos(window) first = False dt = dt * 1000 # Get mouse position x, y = glfw.GetCursorPos(window) # Reset mouse position for next frame #glfw.SetCursorPos(window, width/2.0, height/2.0); # Compute new orientation horizontalAngle += mouseSpeed * dt * float(oldx - x); verticalAngle += mouseSpeed * dt * float(oldy - y); oldx = x oldy = y # Direction : Spherical coordinates to Cartesian coordinates conversion direction = vec3([ math.cos(verticalAngle) * math.sin(horizontalAngle), math.sin(verticalAngle), math.cos(verticalAngle) * math.cos(horizontalAngle) ], dtype='f') # Right vector right = vec3([ math.sin(horizontalAngle - math.pi/2.0), 0.0, math.cos(horizontalAngle - math.pi/2.0) ], dtype='f') # Up vector up = right ^ direction # Move forward if glfw.GetKey(window, glfw.KEY_UP) == glfw.PRESS or glfw.GetKey(window, glfw.KEY_W) == glfw.PRESS: position += direction * dt * speed # Move backward if glfw.GetKey(window, glfw.KEY_DOWN) == glfw.PRESS or glfw.GetKey(window, glfw.KEY_S) == glfw.PRESS: position -= direction * dt * speed # Strafe right if glfw.GetKey(window, glfw.KEY_RIGHT) == glfw.PRESS or glfw.GetKey(window, glfw.KEY_D) == glfw.PRESS: position += right * dt * speed # Strafe left if glfw.GetKey(window, glfw.KEY_LEFT) == glfw.PRESS or glfw.GetKey(window, glfw.KEY_A) == glfw.PRESS: position -= right * dt * speed if glfw.GetKey(window, glfw.KEY_Q) == glfw.PRESS or glfw.GetKey(window, glfw.KEY_ESCAPE) == glfw.PRESS: exit() if glfw.GetKey(window, glfw.KEY_J) == glfw.PRESS: animation_speed += 100 if glfw.GetKey(window, glfw.KEY_K) == glfw.PRESS: animation_speed -= 100 # Camera matrix viewMatrix = lookAt(position, position + direction, up) return horizontalAngle, verticalAngle, position, direction, right, up, viewMatrix, animation_speed
def generateTangents(vertexPos, textureCoords, indexData): """ Generate tangents for normal mapping. Currently only works with indexData. """ tangents = [0 for i in vertexPos] # For each triangle for i in range(0, len(indexData), 3): v0 = vec3([ vertexPos[indexData[i] * 3], vertexPos[indexData[i] * 3 + 1], vertexPos[indexData[i] * 3 + 2] ]) v1 = vec3([ vertexPos[indexData[i + 1] * 3], vertexPos[indexData[i + 1] * 3 + 1], vertexPos[indexData[i + 1] * 3 + 2] ]) v2 = vec3([ vertexPos[indexData[i + 2] * 3], vertexPos[indexData[i + 2] * 3 + 1], vertexPos[indexData[i + 2] * 3 + 2] ]) edge1 = v1 - v0 edge2 = v2 - v0 dU1 = - (textureCoords[indexData[i + 1] * 2] - textureCoords[indexData[i] * 2]) dV1 = - (textureCoords[indexData[i + 1] * 2 + 1] - textureCoords[indexData[i] * 2 + 1]) dU2 = - (textureCoords[indexData[i + 2] * 2] - textureCoords[indexData[i] * 2]) dV2 = - (textureCoords[indexData[i + 2] * 2 + 1] - textureCoords[indexData[i] * 2 + 1]) f = 1.0 / (dU1 * dV2 - dU2 * dV1) tangent = f * (dV2 * edge1 - dV1 * edge2) tangents[indexData[i] * 3] += tangent[0] tangents[indexData[i] * 3 + 1] += tangent[1] tangents[indexData[i] * 3 + 2] += tangent[2] tangents[indexData[i + 1] * 3] += tangent[0] tangents[indexData[i + 1] * 3 + 1] += tangent[1] tangents[indexData[i + 1] * 3 + 2] += tangent[2] tangents[indexData[i + 2] * 3] += tangent[0] tangents[indexData[i + 2] * 3 + 1] += tangent[1] tangents[indexData[i + 2] * 3 + 2] += tangent[2] outTangents = [0 for i in tangents] # Normalize tangents for i in range(0, len(tangents), 3): x = float(tangents[i]) y = float(tangents[i + 1]) z = float(tangents[i + 2]) if x == y == z == 0: outTangents[i] = 0.0 outTangents[i + 1] = 0.0 outTangents[i + 2] = 1.0 else: length = math.sqrt(x ** 2 + y ** 2 + z ** 2) outTangents[i] = x / length outTangents[i + 1] = y / length outTangents[i + 2] = z / length return outTangents
def randomInUnitSphere(): while True: p = 2.0 * vec3([random(), random(), random()]) - vec3([1.0, 1.0, 1.0]) if p.squared_length < 1.0: return p
def paintWorld(): ppmDrawer = PpmDrawer("graduation.ppm", nx, ny) lookFrom = vec3([3.0, 3.0, 2.0]) lookAt = vec3([0.0, 0.0, -1.0]) vUp = vec3([0.0, 1.0, 0.0]) distToFocus = (lookFrom - lookAt).length aperture = 2.0 camera = Camera(lookFrom, lookAt, vUp, 90.0 / 4.0, float(nx) / float(ny), aperture, distToFocus) points = [] # world = ShapeList() # world.append(Sphere(vec3([-R, 0.0, -1.0]), # R, # Lambertian(vec3([0.0, 0.0, 1.0])))) # world.append(Sphere(vec3([R, 0.0, -1.0]), # R, # Lambertian(vec3([1.0, 0.0, 0.0])))) # world.append(Sphere(vec3([0.0, 0.0, -1.0]), # 0.5, # Lambertian(vec3([0.1, 0.2, 0.5])))) # world.append(Sphere(vec3([0.0, -100.5, -1.0]), # 100, # Lambertian(vec3([0.8, 0.8, 0.0])))) # world.append(Sphere(vec3([1.0, 0.0, -1.0]), # 0.5, # Metal(vec3([0.8, 0.6, 0.2]), 0.3))) # world.append(Sphere(vec3([-1.0, 0.0, -1.0]), # 0.5, # Dielectric(1.5))) # world.append(Sphere(vec3([-1.0, 0.0, -1.0]), # -0.45, # Dielectric(1.5))) world = graduation() count = 0 last = 0 for j in range(ny, 0, -1): for i in range(nx): new = int((count * 100) / (nx * ny)) if last != new: print("Progress: " + str(last) + "%", end="\n") last = new count += 1 col = vec3([0.0, 0.0, 0.0]) for s in range(ns): u = (i + random()) / nx v = (j + random()) / ny ray = camera.getRay(u, v) col += colorRay(ray, world, 0) col = col / ns points.append(col) ppmDrawer.writePpm(points)
def blueBlend(ray: Ray) -> vec3: t = 0.5 * (ray.direction.y + 1.0) return (1.0 - t) * vec3([1.0, 1.0, 1.0]) + t * vec3([0.5, 0.7, 1.0])
def graduation(): world = ShapeList() world.append( Sphere(vec3([0.0, -1000.0, 0]), 1000.0, Lambertian(vec3([0.5, 0.5, 0.5])))) for a in range(-11, 11): for b in range(-11, 11): chooseMat = random() center = vec3([a + 0.9 * random(), 0.2, b + 0.9 * random()]) if (center - vec3([4.0, 0.2, 0.0])).length > 0.9: if chooseMat < 0.8: # Lambertian world.append( Sphere( center, 0.2, Lambertian( vec3([ random() * random(), random() * random(), random() * random() ])))) elif chooseMat < 0.95: # Metal world.append( Sphere( center, 0.2, Metal( vec3( [1 + random(), 1 + random(), 1 + random()]), 0.5 * random()))) else: # Glass world.append(Sphere(center, 0.2, Dielectric(1.5))) world.append(Sphere(vec3([0.0, 1.0, 0.0]), 1.0, Dielectric(1.5))) world.append( Sphere(vec3([-4.0, 1.0, 0.0]), 1.0, Lambertian(vec3([0.4, 0.2, 0.1])))) world.append( Sphere(vec3([4.0, 1.0, 0.0]), 1.0, Metal(vec3([0.7, 0.6, 0.5]), 0.0))) return world
fs_tri = np.array([ -1.0, -1.0, 3.0, -1.0, -1.0, 3.0, ]) clear_vao = ctx.simple_vertex_array(clear_prog, ctx.buffer(fs_tri.astype('f4').tobytes()), 'V2F') print(min_pos, max_pos, center) model = np.identity(4) eye = np.copy(center) eye[2] -= 3*min_pos[2] up = [0.0, 1.0, 0.0] print(center, eye) # eye = [ 0.0, 0.0, 120.0 ] view = mat4.look_at(vec3(eye), vec3(center), vec3(up)) proj = mat4.perspective_projection(50, resolution[0]/resolution[1], 0.01, 1000.0) mvp = proj * view * model print("M"), print(model) print("V"), print(view) print("P"), print(proj) print("MVP"), print(mvp) # prog['MVP'].value = tuple(mvp.flatten()) gui.create_context() def key_event(window, key, scancode, action, mode): global eye if action == glfw.PRESS and key == glfw.KEY_RIGHT:
def _setRandomStartingPoint(self): # Using angle will place the planets on different planes #angle = random.randint(0, 360) x = self.distance * math.cos(0) y = self.distance * math.sin(0) self.position = vec3([x, y, 0]) + self.parent.position