pub struct Sphere {
pub origin: Vec3,
pub radius: Float,
/* private fields */
}Expand description
Sphere specified by its radius and origin
Fields§
§origin: Vec3Origin of the sphere
radius: FloatRadius of the sphere
Implementations§
Source§impl Sphere
impl Sphere
Sourcepub fn new(origin: Vec3, radius: Float, material: Material) -> Box<Self>
pub fn new(origin: Vec3, radius: Float, material: Material) -> Box<Self>
§Arguments
origin- Origin of the sphereradius- Radius of the spherematerial- Material of the sphere
Examples found in repository?
examples/fireplace.rs (lines 21-25)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let camera = Camera::perspective(
9 Vec3::new(4.0, 1.0, -3.0),
10 0.5 * Vec3::Y,
11 Vec3::Y,
12 90.0,
13 0.0,
14 1.0,
15 1024,
16 768,
17 );
18
19 let mut scene = parser::scene_from_url(SCENE_URL, SCENE_NAME)?;
20
21 scene.add_light(Sphere::new(
22 Vec3::new(2.0, 1.5, -2.0),
23 0.3,
24 Material::Light(Texture::Solid(Color::WHITE)),
25 ));
26
27 let renderer = Renderer::new(scene, camera);
28 renderer.render().save("fireplace.png")?;
29
30 Ok(())
31}More examples
examples/conference.rs (lines 21-25)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 let camera = Camera::perspective(
9 Vec3::new(-50.0, 400.0, -350.0),
10 500.0 * Vec3::X + 250.0 * Vec3::Z,
11 Vec3::Y,
12 90.0,
13 0.0,
14 1.0,
15 1024,
16 768,
17 );
18
19 let mut scene = parser::scene_from_url(SCENE_URL, SCENE_NAME)?;
20
21 scene.add_light(Sphere::new(
22 Vec3::new(-200.0, 40.0, -400.0),
23 100.0,
24 Material::Light(Texture::Solid(Color::WHITE)),
25 ));
26
27 scene.add_light(Sphere::new(
28 Vec3::new(900.0, 300.0, -600.0),
29 100.0,
30 Material::Light(Texture::Solid(Color::WHITE)),
31 ));
32
33 let renderer = Renderer::new(scene, camera);
34 renderer.render().save("conference.png")?;
35
36 Ok(())
37}examples/hello_sphere.rs (lines 14-18)
4fn main() -> Result<(), png::EncodingError> {
5 let camera = Camera::default(1280, 720);
6 let mut scene = Scene::default();
7
8 scene.add(Plane::new(
9 Vec3::NEG_Y,
10 Vec3::Y,
11 Material::diffuse(Texture::Solid(Color::new(190, 200, 210))),
12 ));
13
14 scene.add_light(Sphere::new(
15 8.0 * Vec3::Y + 1.5 * Vec3::NEG_Z,
16 4.0,
17 Material::Light(Texture::Solid(Color::WHITE)),
18 ));
19
20 scene.add(
21 Sphere::new(
22 Vec3::ZERO,
23 1.0,
24 Material::diffuse(Texture::Solid(Color::new(0, 0, 255))),
25 )
26 .scale(0.3, 0.3, 0.3)
27 .translate(0.0, -0.7, -1.5),
28 );
29
30 let mut renderer = Renderer::new(scene, camera);
31 renderer.set_samples(36);
32 renderer.render().save("hello.png")
33}examples/box.rs (lines 15-22)
4fn main() -> Result<(), std::io::Error> {
5 let camera = Camera::default(1024, 768);
6 let def_color = Color::new(242, 242, 242);
7 let mut scene = Scene::empty_box(
8 def_color,
9 // left
10 Material::diffuse(Texture::Solid(Color::new(255, 0, 255))),
11 // right
12 Material::diffuse(Texture::Solid(Color::new(0, 255, 255))),
13 );
14
15 scene.add(Sphere::new(
16 Vec3::new(-0.45, -0.5, -1.5),
17 0.25,
18 Material::metallic(
19 Texture::Solid(Color::WHITE),
20 0.0,
21 )
22 ));
23
24 scene.add(Sphere::new(
25 Vec3::new(0.45, -0.5, -1.3),
26 0.25,
27 Material::transparent(
28 Texture::Solid(Color::WHITE),
29 0.0,
30 2.5,
31 ),
32 ));
33
34 let renderer = Renderer::new(scene, camera);
35 renderer.render().save("box.png")?;
36 Ok(())
37}examples/circle.rs (lines 39-43)
16fn main() -> Result<(), std::io::Error> {
17 let camera = Camera::perspective(
18 Vec3::new(0.0, 1.5, 1.5),
19 Vec3::ZERO,
20 Vec3::new(0.0, 1.0, -1.0),
21 90.0,
22 0.0,
23 0.0,
24 1024,
25 768,
26 );
27
28 let mut scene = Scene::default();
29 let ground = -0.2;
30
31 // ground
32 scene.add(Plane::new(
33 ground * Vec3::Y,
34 Vec3::Y,
35 Material::metallic(Texture::Solid(Color::new(150, 40, 39)), 0.009999),
36 ));
37
38 let r = 0.2;
39 scene.add_light(Sphere::new(
40 Vec3::new(0.0, ground + r + 0.1, 0.0),
41 r,
42 Material::Light(Texture::Solid(Color::WHITE)),
43 ));
44
45 let circle_s = 8;
46 let offset = PI / circle_s as Float;
47
48 for i in 0..circle_s {
49 let theta = (i as Float / circle_s as Float) * 2.0 * PI + offset;
50 let x = theta.cos();
51 let y = ground + r;
52 let z = theta.sin();
53
54 scene.add(Sphere::new(
55 Vec3::new(x, y, z),
56 r,
57 Material::specular(Texture::Solid(hsv_to_rgb(theta - offset)), 0.2),
58 ));
59 }
60
61 scene.set_medium(
62 Medium::new(
63 Vec3::new(0.002, 0.003, 0.0001),
64 Vec3::new(0.175, 0.125, 0.11),
65 0.9,
66 )
67 );
68
69 let renderer = Renderer::new(scene, camera);
70 renderer.render().save("circle.png")?;
71
72 Ok(())
73}Trait Implementations§
Source§impl Bounded for Sphere
impl Bounded for Sphere
Source§fn bounding_box(&self) -> AaBoundingBox
fn bounding_box(&self) -> AaBoundingBox
Axis aligned box that contains the object
Source§impl Sampleable for Sphere
impl Sampleable for Sphere
Source§fn sample_towards(&self, xo: Vec3, rand_sq: DVec2) -> Vec3
fn sample_towards(&self, xo: Vec3, rand_sq: DVec2) -> Vec3
Visible area from xo forms a cone. Sample a random point on the
spherical cap that the visible area forms. Return a ray with direction
towards the sampled point.
Source§fn sample_towards_pdf(&self, ri: &Ray) -> (Float, Option<Hit<'_>>)
fn sample_towards_pdf(&self, ri: &Ray) -> (Float, Option<Hit<'_>>)
PDF (w.r.t area) for sampling area of the sphere
that is visible from xo (a spherical cap formed by a cone)
Source§fn sample_leaving(&self, rand_sq0: DVec2, rand_sq1: DVec2) -> (Ray, Hit<'_>)
fn sample_leaving(&self, rand_sq0: DVec2, rand_sq1: DVec2) -> (Ray, Hit<'_>)
Samples a ray leaving at random point on the surface of the object.
Direction cos weighed on the hemisphere. Returns also normal at ray origin
Auto Trait Implementations§
impl Freeze for Sphere
impl RefUnwindSafe for Sphere
impl Send for Sphere
impl Sync for Sphere
impl Unpin for Sphere
impl UnwindSafe for Sphere
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instanceable<T> for Twhere
T: Object,
impl<T> Instanceable<T> for Twhere
T: Object,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more