use std::io::Read;
use truck_modeling::*;
use truck_platform::*;
use truck_rendimpl::*;
use wgpu::*;
use winit::{dpi::*, event::*, event_loop::ControlFlow};
mod app;
use app::*;
struct MyApp {
scene: Scene,
rotate_flag: bool,
prev_cursor: Option<Vector2>,
light_changed: Option<std::time::Instant>,
camera_changed: Option<std::time::Instant>,
}
impl MyApp {
fn create_camera() -> Camera {
let matrix = Matrix4::look_at_rh(
Point3::new(1.5, 1.5, 1.5),
Point3::origin(),
Vector3::unit_y(),
);
Camera::perspective_camera(
matrix.invert().unwrap(),
Rad(std::f64::consts::PI / 4.0),
0.1,
40.0,
)
}
fn create_cube() -> Solid {
let v = builder::vertex(Point3::origin());
let edge = builder::tsweep(&v, Vector3::unit_x());
let face = builder::tsweep(&edge, Vector3::unit_y());
builder::tsweep(&face, Vector3::unit_z())
}
}
impl App for MyApp {
fn init(handler: &DeviceHandler, info: AdapterInfo) -> MyApp {
let sample_count = match info.backend {
Backend::Vulkan => 2,
Backend::Dx12 => 2,
_ => 1,
};
let desc = SceneDescriptor {
camera: MyApp::create_camera(),
lights: vec![Light {
position: Point3::new(1.0, 1.0, 1.0),
color: Vector3::new(1.0, 1.0, 1.0),
light_type: LightType::Point,
}],
sample_count,
..Default::default()
};
let mut scene = Scene::new(handler.clone(), &desc);
let mut tex_file = std::fs::File::open("examples/WoodFloor024_2K_Color.png").unwrap();
let mut bytes = Vec::new();
tex_file.read_to_end(&mut bytes).unwrap();
let texture = image::load_from_memory(&bytes).unwrap();
let texture = image2texture::image2texture(&handler, &texture);
let desc = ShapeInstanceDescriptor {
instance_state: InstanceState {
matrix: Matrix4::from_translation(Vector3::new(-0.5, -0.5, -0.5)),
material: Material {
albedo: Vector4::new(0.402, 0.262, 0.176, 1.0),
roughness: 0.9,
reflectance: 0.04,
ambient_ratio: 0.05,
},
texture: Some(std::sync::Arc::new(texture)),
backface_culling: true,
},
mesh_precision: 0.1,
};
let shape = scene
.instance_creator()
.create_shape_instance(&Self::create_cube(), &desc);
scene.add_object(&shape);
MyApp {
scene,
rotate_flag: false,
prev_cursor: None,
camera_changed: None,
light_changed: None,
}
}
fn app_title<'a>() -> Option<&'a str> { Some("textured cube") }
fn mouse_input(&mut self, state: ElementState, button: MouseButton) -> ControlFlow {
match button {
MouseButton::Left => {
self.rotate_flag = state == ElementState::Pressed;
if !self.rotate_flag {
self.prev_cursor = None;
}
}
MouseButton::Right => {
let (light, camera) = {
let desc = self.scene.descriptor_mut();
(&mut desc.lights[0], &desc.camera)
};
match light.light_type {
LightType::Point => {
light.position = camera.position();
}
LightType::Uniform => {
light.position = Point3::from_vec(camera.position().to_vec().normalize());
}
}
}
_ => {}
}
Self::default_control_flow()
}
fn mouse_wheel(&mut self, delta: MouseScrollDelta, _: TouchPhase) -> ControlFlow {
match delta {
MouseScrollDelta::LineDelta(_, y) => {
let camera = &mut self.scene.descriptor_mut().camera;
let trans_vec = camera.eye_direction() * 0.2 * y as f64;
camera.matrix = Matrix4::from_translation(trans_vec) * camera.matrix;
}
MouseScrollDelta::PixelDelta(_) => {}
};
Self::default_control_flow()
}
fn cursor_moved(&mut self, position: PhysicalPosition<f64>) -> ControlFlow {
if self.rotate_flag {
let position = Vector2::new(position.x, position.y);
if let Some(ref prev_position) = self.prev_cursor {
let matrix = &mut self.scene.descriptor_mut().camera.matrix;
let dir2d = &position - prev_position;
if dir2d.so_small() {
return Self::default_control_flow();
}
let mut axis = dir2d[1] * matrix[0].truncate();
axis += dir2d[0] * matrix[1].truncate();
axis /= axis.magnitude();
let angle = dir2d.magnitude() * 0.01;
let mat = Matrix4::from_axis_angle(axis, Rad(angle));
*matrix = mat.invert().unwrap() * *matrix;
}
self.prev_cursor = Some(position);
}
Self::default_control_flow()
}
fn keyboard_input(&mut self, input: KeyboardInput, _: bool) -> ControlFlow {
let keycode = match input.virtual_keycode {
Some(keycode) => keycode,
None => return Self::default_control_flow(),
};
match keycode {
VirtualKeyCode::P => {
if let Some(ref instant) = self.camera_changed {
let time = instant.elapsed().as_secs_f64();
if time < 0.2 {
return Self::default_control_flow();
}
}
let camera = &mut self.scene.descriptor_mut().camera;
self.camera_changed = Some(std::time::Instant::now());
*camera = match camera.projection_type() {
ProjectionType::Parallel => Camera::perspective_camera(
camera.matrix,
Rad(std::f64::consts::PI / 4.0),
0.1,
40.0,
),
ProjectionType::Perspective => {
Camera::parallel_camera(camera.matrix, 1.0, 0.1, 100.0)
}
}
}
VirtualKeyCode::L => {
if let Some(ref instant) = self.light_changed {
let time = instant.elapsed().as_secs_f64();
if time < 0.2 {
return Self::default_control_flow();
}
}
self.light_changed = Some(std::time::Instant::now());
let (light, camera) = {
let desc = self.scene.descriptor_mut();
(&mut desc.lights[0], &desc.camera)
};
*light = match light.light_type {
LightType::Point => {
let position = Point3::from_vec(camera.position().to_vec().normalize());
Light {
position,
color: Vector3::new(1.0, 1.0, 1.0),
light_type: LightType::Uniform,
}
}
LightType::Uniform => {
let position = camera.position();
Light {
position,
color: Vector3::new(1.0, 1.0, 1.0),
light_type: LightType::Point,
}
}
}
}
_ => {}
}
Self::default_control_flow()
}
fn render(&mut self, frame: &SwapChainFrame) { self.scene.render_scene(&frame.output.view); }
}
fn main() { MyApp::run(); }