def mandelboxSDF(pos, scale): orbitTrap = glm.vec4(10000) iterations = 17 #0 to 300 colorIterations = 3 #0 to 300 minRad2 = 0.25 #0.0 to 2.0 scale = glm.vec4(scale, scale, scale, abs(scale)) / minRad2 rotVector = glm.vec3(1, 1, 1) #(0, 0, 0) to (1, 1, 1) rotAngle = 0 #0 to 180(?) rot = rotationMatrix3(glm.normalize(rotVector), rotAngle) absScalem1 = abs(scale - 1) absScaleRaisedTo1mIters = pow(abs(scale), float(1 - iterations)) p = pos w = 1 p0 = p w0 = w for i in range(iterations): p *= rot p = glm.clamp(p, -1, 1) * 2 - p r2 = glm.dot(p, p) if i < colorIterations: orbitTrap = glm.min(orbitTrap, abs(glm.vec4(p, r2))) p *= glm.clamp(glm.max(minRad2 / r2, minRad2), 0, 1) w *= glm.clamp(glm.max(minRad2 / r2, minRad2), 0, 1) p = p * glm.vec3(scale[0], scale[1], scale[2]) + p0 w = w * scale[3] + w0 if r2 > 1000: break return (glm.length(p) - absScalem1[3]) / w - absScaleRaisedTo1mIters[3]
def move_object(dt): global obj obj = moon if state.capslock else camera sign = -1 if obj is camera else 1 if state.right and state.shift: state.av.z += angular_acceleration * sign if state.right and not state.shift: state.av.y -= angular_acceleration if state.left and state.shift: state.av.z -= angular_acceleration * sign if state.left and not state.shift: state.av.y += angular_acceleration if state.up: state.av.x -= angular_acceleration if state.down: state.av.x += angular_acceleration if state.key_w and state.alt: state.lv.y += linear_acceleration if state.key_w and not state.alt: state.lv.z += linear_acceleration * sign if state.key_s and state.alt: state.lv.y -= linear_acceleration if state.key_s and not state.alt: state.lv.z -= linear_acceleration * sign if state.key_a: state.lv.x += linear_acceleration * sign if state.key_d: state.lv.x -= linear_acceleration * sign state.lv = glm.clamp(state.lv, -64, 64) state.av = glm.clamp(state.av, -64, 64) obj.move_local(dt * state.lv) obj.rotate_local(dt * state.av.x, (1, 0, 0)) obj.rotate_local(dt * state.av.y, (0, 1, 0)) obj.rotate_local(dt * state.av.z, (0, 0, 1)) state.lv *= 0.67**dt state.av *= 0.67**dt
def JobsWithSpiralRenderPattern(self, scene, horizontal_frame_step, vertical_frame_step): # generate worker jobs frame_step = glm.vec2(horizontal_frame_step, vertical_frame_step) origin = glm.vec2(self.width / 2, self.height / 2) - frame_step for (x, y) in spiral(int(self.width / horizontal_frame_step) + 1, int(self.height / vertical_frame_step) + 1): from_point = glm.vec2(x, y) * frame_step + origin to_point = from_point + frame_step from_point.x = glm.clamp(from_point.x, 0, self.width) from_point.y = glm.clamp(from_point.y, 0, self.height) to_point.x = glm.clamp(to_point.x, 0, self.width) to_point.y = glm.clamp(to_point.y, 0, self.height) render_job = RenderJob(self.frame, scene, int(from_point.x), int(to_point.x), int(from_point.y), int(to_point.y)) self.render_queue.put(render_job)
def handle_key(wnd, key: int, scancode: int, action, mods: int): if action != glfw.PRESS: return if key == glfw.KEY_ESCAPE: glfw.set_window_should_close(wnd, True) global update_rotation if key == glfw.KEY_TAB: if glfw.get_input_mode(wnd, glfw.CURSOR) == glfw.CURSOR_NORMAL: update_rotation = update_rotation_moused glfw.set_input_mode(wnd, glfw.CURSOR, glfw.CURSOR_DISABLED) else: update_rotation = update_rotation_base glfw.set_input_mode(wnd, glfw.CURSOR, glfw.CURSOR_NORMAL) update_rotation() global TRANSPARENCY if key == glfw.KEY_O: i = TRANSPARENCY_MODES.index(TRANSPARENCY) i = (i + 1) % len(TRANSPARENCY_MODES) TRANSPARENCY = TRANSPARENCY_MODES[i] global transparency_threshold if key == glfw.KEY_KP_DIVIDE or key == glfw.KEY_9: transparency_threshold = glm.clamp(transparency_threshold - 0.125, 0.0, 1.0) if key == glfw.KEY_KP_MULTIPLY or key == glfw.KEY_0: transparency_threshold = glm.clamp(transparency_threshold + 0.125, 0.0, 1.0) global FLY_CONTROLS if key == glfw.KEY_R: FLY_CONTROLS = not FLY_CONTROLS global FLY_FORWARD if key == glfw.KEY_F: FLY_FORWARD = not FLY_FORWARD global bill_threshold if key == glfw.KEY_EQUAL or key == glfw.KEY_KP_ADD: bill_threshold += 5.0 if key == glfw.KEY_MINUS or key == glfw.KEY_KP_SUBTRACT: bill_threshold = max(0.0, bill_threshold - 5.0) global vsync if key == glfw.KEY_V: set_vsync(not vsync)
def _update_key(self, key): is_dirty = False MOVE_SPD = 0.02 if key == glfw.KEY_A: self.move.x += MOVE_SPD is_dirty = True elif key == glfw.KEY_W: self.move.y += MOVE_SPD is_dirty = True elif key == glfw.KEY_D: self.move.x -= MOVE_SPD is_dirty = True elif key == glfw.KEY_S: self.move.y -= MOVE_SPD is_dirty = True MOVEMENT_CONSTRAINT = 0.8 self.move = glm.clamp( self.move, glm.vec2(-MOVEMENT_CONSTRAINT), glm.vec2(MOVEMENT_CONSTRAINT) ) if is_dirty: self.renderer.update_input_move((self.move.x, self.move.y))
def cast(arg): point, maxd, mind, obj, uv, x, y = arg t, p = get_dist(point, maxd, mind, obj, uv) l = glm.clamp((1/maxd)*(maxd-t), 0, 1) c = l * texture.get_point(p) return c, (x, y)
def handle_mouse(self, x_offset, y_offset): self.yaw += x_offset * self.mouse_sensitivity self.pitch += y_offset * self.mouse_sensitivity # make sure that when pitch is out of bounds, screen doesn't get flipped self.pitch = glm.clamp(self.pitch, -89, 89) # update Front, Right and Up Vectors using the updated Euler angles self.__update_camera_vectors()
def shade(self, ray, pos, normal, light): output = glm.vec3(0.0) nlightDirection, lightColor = light.getIllumination(pos) nnormal = glm.normalize(normal) diffuseIntensity = glm.dot(nnormal, nlightDirection) output = output + self.diffuse * lightColor * glm.clamp( diffuseIntensity, 0.0, 1.0) nview = glm.normalize(ray.origin - pos) nhalf = glm.normalize(nview + nlightDirection) specularIntensity = glm.pow(glm.dot(nhalf, nnormal), self.shininess) output = output + self.specular * lightColor * glm.clamp( specularIntensity, 0.0, 1.0) output = output + self.emission return glm.vec3(glm.clamp(output.x, 0.0, 1.0), glm.clamp(output.y, 0.0, 1.0), glm.clamp(output.z, 0.0, 1.0))
def move_to(x, y): if not Camera._enable_rotation: return diff_x = x - Camera._prev_x diff_y = y - Camera._prev_y Camera._yaw += diff_x Camera._pitch += diff_y Camera._pitch = glm.clamp(Camera._pitch, -89, 89) Camera.update_gl() Camera.update(x, y)
def tick_update(self): acceleration = glm.vec3(0,0,0) if (self.target != glm.vec3(0,0,0)): offsetToTarget = glm.vec3(self.target - self.center_pos) accelleration = self.steer_towards(offsetToTarget) * self.targetWeight # no flocking behavior here # if (numPerceivedFlockmates != 0) { # centreOfFlockmates /= numPerceivedFlockmates; # # Vector3 offsetToFlockmatesCentre = (centreOfFlockmates - position); # # var alignmentForce = SteerTowards (avgFlockHeading) * settings.alignWeight; # var cohesionForce = SteerTowards (offsetToFlockmatesCentre) * settings.cohesionWeight; # var seperationForce = SteerTowards (avgAvoidanceHeading) * settings.seperateWeight; # # acceleration += alignmentForce; # acceleration += cohesionForce; # acceleration += seperationForce; # } if (self.is_heading_for_collision()): collisionAvoidDir = glm.vec3(obstacle_rays()); collisionAvoidForce = glm.vec3(steer_towards(collisionAvoidDir) * self.avoidCollisionWeight) acceleration += collisionAvoidForce; self.velocity2 += acceleration * 50/1000 speed = glm.length(self.velocity2) #velocity.magnitude; (TODO: should be magnitude, confirm) direction = glm.vec3(self.velocity2 / speed) speed = glm.clamp(speed, self.minSpeed, self.maxSpeed); self.velocity2 = direction * speed; # cachedTransform.position += velocity * 50/1000; # cachedTransform.forward = direction; # position = cachedTransform.position; self.heading = direction; # calculate the new position based on velocity new_pos = draw.do_translate_point(self.center_pos, (glm.normalize(self.heading) * self.velocity)) if ((math.isnan(new_pos.x) == False) and (math.isnan(new_pos.y) == False) and (math.isnan(new_pos.z)== False)): if ((self.center_pos.x > 0) and (self.center_pos.y > 0)): self.center_pos.x = int(new_pos.x) self.center_pos.y = int(new_pos.y) self.center_pos.z = int(new_pos.z)
def CheckClampedCollision(self, Obj1, Obj2): # find center of call center = glm.vec2(Obj1.position + Obj1.Radius) # calculate halk extents halfExtent = glm.vec2(Obj2.Size.x / 2, Obj2.Size.y / 2) aabbCenter = glm.vec2(Obj2.position.x + halfExtent.x, Obj2.position.y + halfExtent.y) # get difference and clamped val difference = center - aabbCenter clamped = glm.clamp(difference, -halfExtent, halfExtent) closest = aabbCenter + clamped difference = closest - center if glm.length(difference) < Obj1.Radius: Collision = (True, self.CheckDirection(difference), difference) else: Collision = (False, "UP", glm.vec2(0, 0)) return Collision
def uniform_grid_volume_get_linear(data, coord, res, data_dim): if len(data) <= 0: return glm.vec4(0.0) # Clamp to nearly one to avoid issus c = glm.clamp(coord, 0.0, 0.9999) p = glm.floor(c * glm.vec3(res - 1)) cc = glm.fract(c * glm.vec3(res - 1)) # Get all samples v000 = get_vec4(data, index(p + glm.vec3(0, 0, 0), res) * data_dim, data_dim) v100 = get_vec4(data, index(p + glm.vec3(1, 0, 0), res) * data_dim, data_dim) v010 = get_vec4(data, index(p + glm.vec3(0, 1, 0), res) * data_dim, data_dim) v110 = get_vec4(data, index(p + glm.vec3(1, 1, 0), res) * data_dim, data_dim) v001 = get_vec4(data, index(p + glm.vec3(0, 0, 1), res) * data_dim, data_dim) v101 = get_vec4(data, index(p + glm.vec3(1, 0, 1), res) * data_dim, data_dim) v011 = get_vec4(data, index(p + glm.vec3(0, 1, 1), res) * data_dim, data_dim) v111 = get_vec4(data, index(p + glm.vec3(1, 1, 1), res) * data_dim, data_dim) # Interpolate x v00 = glm.mix(v000, v100, cc[0]) v01 = glm.mix(v010, v110, cc[0]) v10 = glm.mix(v001, v101, cc[0]) v11 = glm.mix(v011, v111, cc[0]) # Interpolate y v0 = glm.mix(v00, v01, cc[1]) v1 = glm.mix(v10, v11, cc[1]) # Interpolate z v = glm.mix(v0, v1, cc[2]) return v
def key_callback(self, window, key, scancode, action, mods): if key == glfw.KEY_ESCAPE: glfw.set_window_should_close(window, True) elif key in self.camera.get_keys(): self.camera.handle_keyboard(key) elif key == glfw.KEY_SPACE and action == glfw.PRESS: self.init_spheres() glClearColor(random.random(), random.random(), random.random(), 1.0) elif action == glfw.PRESS and (key == glfw.KEY_KP_ADD or key == glfw.KEY_KP_SUBTRACT): if key == glfw.KEY_KP_ADD: self.raytracing_max_bounces += 1 else: self.raytracing_max_bounces -= 1 self.raytracing_max_bounces = int( glm.clamp(self.raytracing_max_bounces, 0, 10))
def get_diffuse_color(self, face: Face, model: glm.mat4) -> glm.vec3: normal = glm.mat3(glm.transpose(glm.inverse(model))) * face.normal frag_pos = glm.vec3(model * glm.vec4(face.p0, 1.0)) light_dir = glm.normalize(self.position - frag_pos) diff = glm.clamp(glm.dot(normal, light_dir), 0.0, 1.0) return diff * self.color
def drag_vector(self): v = self.drag_vec + self.rotate_vec v.x, _ = math.modf(v.x) v.y = glm.clamp(v.y, -1, 1) return v
def steer_towards(self, vector): v = glm.vec3(glm.normalize(vector) * self.maxSpeed - self.velocity2) return glm.clamp(v, 0, self.maxSteerForce);
def distance_to(self, point): d2 = self.obj2.distance_to(point) d1 = self.obj1.distance_to(point) h = glm.clamp(0.5 - 0.5 * (d1 + d2) / self.k, 0.0, 1.0) return glm.mix(d2, -d1, h) + self.k * h * (1.0 - h)
def clamp(value, min_value, max_value): return glm.clamp(value, min_value, max_value)
def world_to_grid(self, pos): return glm.clamp((pos + self.size.xz * .5) / self.size.xz, 0.0, .99) * self.tex_size
def handle_wheel(wnd, dx: float, dy: float): global tree_scale tree_scale = glm.clamp(tree_scale + 0.1 * dy, 0.1, 5.0)
def clamp_color(color): return glm.clamp(color, 0, 255)
def update_rotation_base(): rotation_ypr.y = glm.clamp(rotation_ypr.y, -80.0, 80.0) rotation_ypr.z = glm.clamp(rotation_ypr.z, -60.0, 60.0)