Compare commits
48 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a98eda1616 | |||
| 13fb710dc7 | |||
| ca460bc0d9 | |||
| 059e98c3e3 | |||
| 0065a45e7d | |||
| 653788d456 | |||
| 68b4041c88 | |||
| 98f3c44ab3 | |||
| 59bc5b640f | |||
| 41579bbb5d | |||
| 722e5d4b6e | |||
| 295037edc6 | |||
| a8f72096d8 | |||
| c40ff3e324 | |||
| 5a6b10965d | |||
| 77cab94a6e | |||
| 4b138deb34 | |||
| a51f46a038 | |||
| fb8ea92ce8 | |||
| 707bb6f66e | |||
| 96e5c78a59 | |||
| e80e1d09fe | |||
| 5a69a3a1cb | |||
| 61f19c85f4 | |||
| a7746deec5 | |||
| 93c8652116 | |||
| 91473f08ae | |||
| 713fd25c9c | |||
| cbe0839cd6 | |||
| 2e6589e01b | |||
| 6ba9add6ea | |||
| 12bc173fb2 | |||
| 64925f9640 | |||
| 198dc06d02 | |||
| bb8373fad5 | |||
| aa56768655 | |||
| cd0c264c6e | |||
| cf65929b99 | |||
| 8bed95f872 | |||
| 413839ccb8 | |||
| 7b22551bee | |||
| 16ea643f90 | |||
| be2df57702 | |||
| d92d75a4d1 | |||
| 4e4c4493f9 | |||
| 10d74f1318 | |||
| e12f4c77aa | |||
| 2575dd570c |
9
.clang-format
Normal file
9
.clang-format
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
BasedOnStyle: LLVM
|
||||||
|
UseTab: Always
|
||||||
|
TabWidth: 4
|
||||||
|
IndentWidth: 4
|
||||||
|
AccessModifierOffset: -4
|
||||||
|
IndentCaseLabels: false
|
||||||
|
ColumnLimit: 0
|
||||||
|
PointerAlignment: Left
|
||||||
|
PackConstructorInitializers: Never
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1 +1,2 @@
|
||||||
/target
|
/target
|
||||||
|
/build
|
||||||
|
|
|
||||||
14
CMakeLists.txt
Normal file
14
CMakeLists.txt
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
cmake_minimum_required(VERSION 3.18)
|
||||||
|
|
||||||
|
project(photon_light VERSION 1.0.0 LANGUAGES CXX)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
|
find_package(Qt6 REQUIRED COMPONENTS Gui Widgets)
|
||||||
|
find_program(CARGO cargo REQUIRED)
|
||||||
|
set(CARGO_TARGET_DIR "${CMAKE_BINARY_DIR}/cargo")
|
||||||
|
|
||||||
|
qt_standard_project_setup()
|
||||||
|
|
||||||
|
add_subdirectory(ui)
|
||||||
11
Cargo.lock
generated
11
Cargo.lock
generated
|
|
@ -1165,6 +1165,17 @@ dependencies = [
|
||||||
"winit",
|
"winit",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "photon-light-impl"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"glam",
|
||||||
|
"photon-light",
|
||||||
|
"pollster",
|
||||||
|
"raw-window-handle",
|
||||||
|
"wgpu",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pin-project"
|
name = "pin-project"
|
||||||
version = "1.1.10"
|
version = "1.1.10"
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,6 @@
|
||||||
|
[workspace]
|
||||||
|
members = ["ui"]
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "photon-light"
|
name = "photon-light"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,12 @@ impl OrbitalCamera {
|
||||||
self.distance * vec3(xy * x, xy * y, z)
|
self.distance * vec3(xy * x, xy * y, z)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn direction(&self) -> Vec3 {
|
||||||
|
let (y, x) = self.position_yaw.sin_cos();
|
||||||
|
let (z, xy) = self.position_pitch.sin_cos();
|
||||||
|
-vec3(xy * x, xy * y, z)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn transform(&self) -> Mat4 {
|
pub fn transform(&self) -> Mat4 {
|
||||||
// for yaw=0, pitch=0:
|
// for yaw=0, pitch=0:
|
||||||
// X -> -Z
|
// X -> -Z
|
||||||
|
|
|
||||||
524
src/lib.rs
Normal file
524
src/lib.rs
Normal file
|
|
@ -0,0 +1,524 @@
|
||||||
|
#![feature(gen_blocks)]
|
||||||
|
|
||||||
|
use std::{convert::identity, error::Error, f32::consts::PI};
|
||||||
|
|
||||||
|
use glam::{Mat4, UVec2, Vec2, Vec3, vec3};
|
||||||
|
use rand_distr::Distribution as _;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
camera::OrbitalCamera,
|
||||||
|
ray::Ray,
|
||||||
|
render::{
|
||||||
|
DEPTH_FORMAT, OUTPUT_FORMAT,
|
||||||
|
lines::{LookParams, Mesh, Pipeline, Vertex},
|
||||||
|
},
|
||||||
|
trace::{Hit, Lambertian, Reflector, Scene, Source, Sphere},
|
||||||
|
};
|
||||||
|
|
||||||
|
mod camera;
|
||||||
|
mod ray;
|
||||||
|
mod render;
|
||||||
|
mod shape;
|
||||||
|
mod trace;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct SphericalPosition {
|
||||||
|
pub yaw: f32,
|
||||||
|
pub pitch: f32,
|
||||||
|
pub distance: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
#[repr(u8)]
|
||||||
|
pub enum UseNormal {
|
||||||
|
Light,
|
||||||
|
Camera,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct RedrawArgs {
|
||||||
|
pub camera_position: SphericalPosition,
|
||||||
|
pub light_position: SphericalPosition,
|
||||||
|
pub light_radius: f32,
|
||||||
|
pub light_spread: f32,
|
||||||
|
pub accum_sigma: f32,
|
||||||
|
pub accum_scale: f32,
|
||||||
|
pub reflections: u8,
|
||||||
|
pub use_normal: UseNormal,
|
||||||
|
pub show_axes: bool,
|
||||||
|
pub show_shapes: bool,
|
||||||
|
pub show_hit_emission: bool,
|
||||||
|
pub show_miss_emission: bool,
|
||||||
|
pub show_direct_hit: bool,
|
||||||
|
pub show_indirect_hit: bool,
|
||||||
|
pub show_light: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Gpu {
|
||||||
|
device: wgpu::Device,
|
||||||
|
queue: wgpu::Queue,
|
||||||
|
surface: wgpu::Surface<'static>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Core {
|
||||||
|
device: wgpu::Device,
|
||||||
|
queue: wgpu::Queue,
|
||||||
|
surface: wgpu::Surface<'static>,
|
||||||
|
|
||||||
|
depth: wgpu::Texture,
|
||||||
|
|
||||||
|
pipeline: Pipeline,
|
||||||
|
mesh_pipe: render::faces::Pipeline,
|
||||||
|
tripod: Mesh,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_tripod(device: &wgpu::Device) -> Mesh {
|
||||||
|
Mesh::new(
|
||||||
|
device,
|
||||||
|
&[
|
||||||
|
Vertex::new(vec3(0., 0., 0.), vec3(1., 0., 0.)),
|
||||||
|
Vertex::new(vec3(1., 0., 0.), vec3(1., 0., 0.)),
|
||||||
|
Vertex::new(vec3(0., 0., 0.), vec3(0., 1., 0.)),
|
||||||
|
Vertex::new(vec3(0., 1., 0.), vec3(0., 1., 0.)),
|
||||||
|
Vertex::new(vec3(0., 0., 0.), vec3(0., 0., 1.)),
|
||||||
|
Vertex::new(vec3(0., 0., 1.), vec3(0., 0., 1.)),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn loop_list<T: Clone>(iter: impl IntoIterator<Item = T>) -> impl Iterator<Item = T> {
|
||||||
|
loop_list_ex(iter, identity, identity)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn loop_list_ex<T: Clone, U>(
|
||||||
|
iter: impl IntoIterator<Item = T>,
|
||||||
|
mut fa: impl FnMut(T) -> U,
|
||||||
|
mut fb: impl FnMut(T) -> U,
|
||||||
|
) -> impl Iterator<Item = U> {
|
||||||
|
gen move {
|
||||||
|
let mut iter = iter.into_iter();
|
||||||
|
let Some(first) = iter.next() else { return };
|
||||||
|
yield fa(first.clone());
|
||||||
|
for item in iter {
|
||||||
|
yield fb(item.clone());
|
||||||
|
yield fa(item);
|
||||||
|
}
|
||||||
|
yield fb(first);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl OrbitalCamera {
|
||||||
|
fn make_ray(&self, rng: &mut impl rand::Rng) -> Ray {
|
||||||
|
let off: f32 = rand_distr::StandardUniform.sample(rng);
|
||||||
|
let side: Vec2 = rand_distr::UnitCircle.sample(rng).into();
|
||||||
|
|
||||||
|
let m = self.transform().inverse();
|
||||||
|
let fwd = 1. - 0.1 * off;
|
||||||
|
let side_scale = (1. - fwd.powi(2)).sqrt();
|
||||||
|
let dir = Vec3::from((side_scale * side, fwd));
|
||||||
|
Ray {
|
||||||
|
base: self.position(),
|
||||||
|
dir: m.transform_vector3(dir),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Core {
|
||||||
|
pub fn new(gpu: Gpu, pixel_size: UVec2) -> Self {
|
||||||
|
let Gpu {
|
||||||
|
device,
|
||||||
|
queue,
|
||||||
|
surface,
|
||||||
|
} = gpu;
|
||||||
|
|
||||||
|
let pipeline = Pipeline::new(&device);
|
||||||
|
let mesh_pipe = render::faces::Pipeline::new(&device);
|
||||||
|
let tripod = new_tripod(&device);
|
||||||
|
let depth = Self::create_depth_buffer(&device, pixel_size);
|
||||||
|
queue.submit([]); // flush buffer updates
|
||||||
|
|
||||||
|
Self::configure_surface(&surface, &device, pixel_size);
|
||||||
|
|
||||||
|
Self {
|
||||||
|
device,
|
||||||
|
queue,
|
||||||
|
surface,
|
||||||
|
depth,
|
||||||
|
pipeline,
|
||||||
|
tripod,
|
||||||
|
mesh_pipe,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render(&self, output: &wgpu::Texture, args: &RedrawArgs) {
|
||||||
|
let camera = OrbitalCamera {
|
||||||
|
position_yaw: args.camera_position.yaw,
|
||||||
|
position_pitch: args.camera_position.pitch,
|
||||||
|
distance: args.camera_position.distance,
|
||||||
|
};
|
||||||
|
let aspect = {
|
||||||
|
let size = output.size();
|
||||||
|
let w = size.width as f32;
|
||||||
|
let h = size.height as f32;
|
||||||
|
w / h
|
||||||
|
};
|
||||||
|
let perspective = Mat4::perspective_lh(PI / 3., aspect, 1e-2, 1e2);
|
||||||
|
self.pipeline.set_look(
|
||||||
|
&self.queue,
|
||||||
|
LookParams {
|
||||||
|
m: perspective * camera.transform(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
self.mesh_pipe.set_look(
|
||||||
|
&self.queue,
|
||||||
|
render::faces::LookParams {
|
||||||
|
m: perspective * camera.transform(),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
self.queue.submit([]); // flush buffer updates
|
||||||
|
|
||||||
|
let view = output.create_view(&wgpu::TextureViewDescriptor::default());
|
||||||
|
let depth_view = self
|
||||||
|
.depth
|
||||||
|
.create_view(&wgpu::TextureViewDescriptor::default());
|
||||||
|
let mut encoder = self
|
||||||
|
.device
|
||||||
|
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
|
||||||
|
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
||||||
|
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
||||||
|
view: &view,
|
||||||
|
depth_slice: None,
|
||||||
|
resolve_target: None,
|
||||||
|
ops: wgpu::Operations {
|
||||||
|
load: wgpu::LoadOp::Clear(wgpu::Color::BLACK),
|
||||||
|
store: wgpu::StoreOp::Store,
|
||||||
|
},
|
||||||
|
})],
|
||||||
|
depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
|
||||||
|
view: &depth_view,
|
||||||
|
depth_ops: Some(wgpu::Operations {
|
||||||
|
load: wgpu::LoadOp::Clear(1.),
|
||||||
|
store: wgpu::StoreOp::Discard,
|
||||||
|
}),
|
||||||
|
stencil_ops: None,
|
||||||
|
}),
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
if args.show_axes {
|
||||||
|
self.pipeline.render(&mut pass, [&self.tripod]);
|
||||||
|
}
|
||||||
|
|
||||||
|
let source = Source {
|
||||||
|
position_yaw: args.light_position.yaw,
|
||||||
|
position_pitch: args.light_position.pitch,
|
||||||
|
distance: args.light_position.distance,
|
||||||
|
radius: args.light_radius,
|
||||||
|
spread: args.light_spread,
|
||||||
|
};
|
||||||
|
|
||||||
|
if args.show_shapes {
|
||||||
|
let contour: Vec<Vertex> = loop_list(source.contour(17))
|
||||||
|
.map(|pos| Vertex {
|
||||||
|
pos,
|
||||||
|
color: vec3(1., 1., 1.),
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
self.pipeline
|
||||||
|
.render(&mut pass, [&Mesh::new(&self.device, &contour)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
const BASE_R: f32 = 2.;
|
||||||
|
const BASE_POS: Vec3 = vec3(0., 0., -BASE_R);
|
||||||
|
const BASE: Sphere = Sphere {
|
||||||
|
position: vec3(0., 0., -BASE_R),
|
||||||
|
radius: BASE_R,
|
||||||
|
color: vec3(0.01, 0.03, 0.30),
|
||||||
|
};
|
||||||
|
fn sphere(pos: Vec3, color: Vec3) -> Sphere {
|
||||||
|
Sphere {
|
||||||
|
position: pos,
|
||||||
|
radius: BASE_POS.distance(pos) - BASE_R,
|
||||||
|
color,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let scene = Scene {
|
||||||
|
objects: vec![
|
||||||
|
BASE,
|
||||||
|
sphere(vec3(0., 0., 0.1), vec3(1., 1., 1.)),
|
||||||
|
sphere(vec3(0.3, 0., 0.1), vec3(1., 1., 1.)),
|
||||||
|
sphere(vec3(0.1, 0.3, 0.1), vec3(0.5, 0.03, 0.01)),
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
struct LightHit<'a> {
|
||||||
|
incident: Ray,
|
||||||
|
normal: Vec3,
|
||||||
|
light: Vec3,
|
||||||
|
object: &'a Sphere,
|
||||||
|
}
|
||||||
|
let mut prng = rand_pcg::Pcg64::new(42, 0);
|
||||||
|
let source_rays: Vec<Ray> = (0..10240).map(|_| source.make_ray(&mut prng)).collect();
|
||||||
|
let camera_rays: Vec<Ray> = (0..10240).map(|_| camera.make_ray(&mut prng)).collect();
|
||||||
|
let mut source_ray_display: Vec<Vertex> = Vec::with_capacity(source_rays.len());
|
||||||
|
let mut hits: Vec<LightHit> = Vec::with_capacity(source_rays.len());
|
||||||
|
for ray in source_rays {
|
||||||
|
let light = Vec3::splat(1.);
|
||||||
|
if let Some(hit) = scene.trace_ray(ray) {
|
||||||
|
hits.push(LightHit {
|
||||||
|
incident: hit.incident,
|
||||||
|
normal: hit.normal,
|
||||||
|
light,
|
||||||
|
object: hit.object,
|
||||||
|
});
|
||||||
|
if args.show_hit_emission {
|
||||||
|
source_ray_display.extend([
|
||||||
|
Vertex {
|
||||||
|
pos: ray.base,
|
||||||
|
color: vec3(1., 1., 1.),
|
||||||
|
},
|
||||||
|
Vertex {
|
||||||
|
pos: ray.base + 0.1 * ray.dir,
|
||||||
|
color: vec3(0., 1., 0.),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
if args.show_direct_hit {
|
||||||
|
source_ray_display.extend([
|
||||||
|
Vertex {
|
||||||
|
pos: hit.incident.base - 0.02 * hit.incident.dir,
|
||||||
|
color: light,
|
||||||
|
},
|
||||||
|
Vertex {
|
||||||
|
pos: hit.incident.base,
|
||||||
|
color: vec3(1., 1., 1.),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if args.show_miss_emission {
|
||||||
|
source_ray_display.extend([
|
||||||
|
Vertex {
|
||||||
|
pos: ray.base,
|
||||||
|
color: vec3(1., 1., 1.),
|
||||||
|
},
|
||||||
|
Vertex {
|
||||||
|
pos: ray.base + 0.1 * ray.dir,
|
||||||
|
color: vec3(1., 0., 0.),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if args.reflections > 0 {
|
||||||
|
let mut hits1 = hits.clone();
|
||||||
|
for _ in 0..args.reflections {
|
||||||
|
let mut hits2: Vec<LightHit> = Vec::with_capacity(hits1.len());
|
||||||
|
for hit in &hits1 {
|
||||||
|
let reflector = Lambertian;
|
||||||
|
let reflected = reflector.reflect(&mut prng, hit.normal, hit.incident.dir);
|
||||||
|
let light = hit.light * hit.object.color;
|
||||||
|
let ray = Ray::new(hit.incident.base, reflected);
|
||||||
|
let Some(hit2) = scene.trace_ray(ray) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
hits2.push(LightHit {
|
||||||
|
incident: hit2.incident,
|
||||||
|
normal: hit2.normal,
|
||||||
|
light: light,
|
||||||
|
object: hit2.object,
|
||||||
|
});
|
||||||
|
if args.show_indirect_hit {
|
||||||
|
source_ray_display.extend([
|
||||||
|
Vertex {
|
||||||
|
pos: hit2.incident.base - 0.02 * hit2.incident.dir,
|
||||||
|
color: light,
|
||||||
|
},
|
||||||
|
Vertex {
|
||||||
|
pos: hit2.incident.base,
|
||||||
|
color: vec3(1., 1., 1.),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hits.extend(&hits2);
|
||||||
|
hits1 = hits2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let light_at = {
|
||||||
|
let sigma2 = args.accum_sigma.powi(2);
|
||||||
|
let accum_normalizator = (2. * PI * sigma2).recip();
|
||||||
|
let hits = &hits;
|
||||||
|
move |hit: Hit| -> Vec3 {
|
||||||
|
let mut total_cd = Vec3::splat(0.);
|
||||||
|
for light_hit in hits {
|
||||||
|
let d2 = hit.incident.base.distance_squared(light_hit.incident.base);
|
||||||
|
if d2 > 9. * sigma2 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let normal = match args.use_normal {
|
||||||
|
UseNormal::Light => light_hit.normal,
|
||||||
|
UseNormal::Camera => hit.normal,
|
||||||
|
};
|
||||||
|
assert!(normal.is_normalized());
|
||||||
|
assert!(hit.incident.dir.is_normalized());
|
||||||
|
let reflector = Lambertian;
|
||||||
|
let in_lm = light_hit.light;
|
||||||
|
let out_cd = in_lm
|
||||||
|
* reflector.brdf(normal, light_hit.incident.dir, -hit.incident.dir)
|
||||||
|
* hit.object.color;
|
||||||
|
let weight = accum_normalizator * (-0.5 * d2 / sigma2).exp();
|
||||||
|
total_cd += weight * out_cd;
|
||||||
|
}
|
||||||
|
total_cd * args.accum_scale
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let colormap = |light: Vec3| {
|
||||||
|
let light = light.dot(Vec3::splat(1. / 3.));
|
||||||
|
let brightness = 3. * (1. - (1. + light).recip());
|
||||||
|
vec3(brightness, brightness - 1., brightness - 2.)
|
||||||
|
.clamp(Vec3::splat(0.), Vec3::splat(1.))
|
||||||
|
};
|
||||||
|
let mut camera_ray_display: Vec<Vertex> = Vec::with_capacity(camera_rays.len());
|
||||||
|
if args.show_light {
|
||||||
|
let r = args.accum_sigma;
|
||||||
|
for ray in camera_rays {
|
||||||
|
let Some(hit) = scene.trace_ray(ray) else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
|
let light = light_at(hit);
|
||||||
|
let color = colormap(light);
|
||||||
|
let vertex = |off: Vec3| Vertex {
|
||||||
|
pos: hit.incident.base + r * off,
|
||||||
|
color,
|
||||||
|
};
|
||||||
|
camera_ray_display.extend([
|
||||||
|
vertex(-Vec3::X),
|
||||||
|
vertex(Vec3::X),
|
||||||
|
vertex(-Vec3::Y),
|
||||||
|
vertex(Vec3::Y),
|
||||||
|
vertex(-Vec3::Z),
|
||||||
|
vertex(Vec3::Z),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if args.show_shapes {
|
||||||
|
let mut meshes = Vec::new();
|
||||||
|
for obj in &scene.objects {
|
||||||
|
let mesh = shape::octo_sphere((obj.radius * 45.) as usize + 7);
|
||||||
|
let obj_mesh = render::faces::Mesh::new(
|
||||||
|
&self.device,
|
||||||
|
&mesh
|
||||||
|
.vertices
|
||||||
|
.iter()
|
||||||
|
.map(|&v| {
|
||||||
|
let pos = obj.position + obj.radius * v;
|
||||||
|
let normal = v;
|
||||||
|
let mut dir = (pos - camera.position()).normalize();
|
||||||
|
if !dir.is_finite() {
|
||||||
|
dir = camera.direction();
|
||||||
|
}
|
||||||
|
let color = light_at(Hit {
|
||||||
|
incident: Ray::new(pos, dir),
|
||||||
|
normal,
|
||||||
|
object: obj,
|
||||||
|
});
|
||||||
|
render::faces::Vertex { pos, color }
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
bytemuck::cast_slice(&mesh.indices),
|
||||||
|
);
|
||||||
|
meshes.push(obj_mesh);
|
||||||
|
}
|
||||||
|
self.mesh_pipe.render(&mut pass, &meshes);
|
||||||
|
}
|
||||||
|
if !source_ray_display.is_empty() {
|
||||||
|
self.pipeline
|
||||||
|
.render(&mut pass, [&Mesh::new(&self.device, &source_ray_display)]);
|
||||||
|
}
|
||||||
|
if !camera_ray_display.is_empty() {
|
||||||
|
self.pipeline
|
||||||
|
.render(&mut pass, [&Mesh::new(&self.device, &camera_ray_display)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
drop(pass);
|
||||||
|
self.queue.submit(std::iter::once(encoder.finish()));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_depth_buffer(device: &wgpu::Device, pixel_size: UVec2) -> wgpu::Texture {
|
||||||
|
device.create_texture(&wgpu::TextureDescriptor {
|
||||||
|
label: Some("depth buffer"),
|
||||||
|
size: wgpu::Extent3d {
|
||||||
|
width: pixel_size.x,
|
||||||
|
height: pixel_size.y,
|
||||||
|
depth_or_array_layers: 1,
|
||||||
|
},
|
||||||
|
mip_level_count: 1,
|
||||||
|
sample_count: 1,
|
||||||
|
dimension: wgpu::TextureDimension::D2,
|
||||||
|
format: DEPTH_FORMAT,
|
||||||
|
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
|
||||||
|
view_formats: &[],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn configure_surface(surface: &wgpu::Surface, device: &wgpu::Device, pixel_size: UVec2) {
|
||||||
|
surface.configure(
|
||||||
|
device,
|
||||||
|
&wgpu::SurfaceConfiguration {
|
||||||
|
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::COPY_DST,
|
||||||
|
format: OUTPUT_FORMAT,
|
||||||
|
width: pixel_size.x,
|
||||||
|
height: pixel_size.y,
|
||||||
|
present_mode: wgpu::PresentMode::Fifo,
|
||||||
|
alpha_mode: wgpu::CompositeAlphaMode::Auto,
|
||||||
|
view_formats: vec![],
|
||||||
|
desired_maximum_frame_latency: 2,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Configures the renderer for a given target size.
|
||||||
|
pub fn configure(&mut self, pixel_size: UVec2) {
|
||||||
|
Self::configure_surface(&self.surface, &self.device, pixel_size);
|
||||||
|
self.depth = Self::create_depth_buffer(&self.device, pixel_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Redraws the entire surface.
|
||||||
|
///
|
||||||
|
/// [`Self::configure`] must be called at least once before this.
|
||||||
|
pub fn redraw(&mut self, args: &RedrawArgs) {
|
||||||
|
let output = self.surface.get_current_texture().unwrap();
|
||||||
|
self.render(&output.texture, args);
|
||||||
|
output.present();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn init_gpu_inner<E: Error + 'static>(
|
||||||
|
make_surface: impl FnOnce(&wgpu::Instance) -> Result<wgpu::Surface<'static>, E>,
|
||||||
|
) -> Result<Gpu, Box<dyn Error>> {
|
||||||
|
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
|
||||||
|
backends: wgpu::Backends::PRIMARY,
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
let surface = make_surface(&instance)?;
|
||||||
|
let adapter = instance
|
||||||
|
.request_adapter(&wgpu::RequestAdapterOptions {
|
||||||
|
power_preference: wgpu::PowerPreference::default(),
|
||||||
|
compatible_surface: Some(&surface),
|
||||||
|
force_fallback_adapter: false,
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let (device, queue) = adapter
|
||||||
|
.request_device(&wgpu::DeviceDescriptor::default())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
Ok(Gpu {
|
||||||
|
device,
|
||||||
|
queue,
|
||||||
|
surface,
|
||||||
|
})
|
||||||
|
}
|
||||||
284
src/main.rs
284
src/main.rs
|
|
@ -1,8 +1,7 @@
|
||||||
#![feature(gen_blocks)]
|
use std::{f32::consts::PI, sync::Arc};
|
||||||
|
|
||||||
use std::{convert::identity, error::Error, f32::consts::PI, sync::Arc};
|
use glam::uvec2;
|
||||||
|
use photon_light::{Core, RedrawArgs, SphericalPosition, UseNormal, init_gpu_inner};
|
||||||
use glam::{Mat4, Vec3, vec3};
|
|
||||||
use winit::{
|
use winit::{
|
||||||
application::ApplicationHandler,
|
application::ApplicationHandler,
|
||||||
event::WindowEvent,
|
event::WindowEvent,
|
||||||
|
|
@ -10,236 +9,58 @@ use winit::{
|
||||||
window::Window,
|
window::Window,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
|
||||||
camera::OrbitalCamera,
|
|
||||||
render::lines::{LookParams, Mesh, Pipeline, Vertex},
|
|
||||||
trace::{Scene, Source, Sphere},
|
|
||||||
};
|
|
||||||
|
|
||||||
mod camera;
|
|
||||||
mod ray;
|
|
||||||
mod render;
|
|
||||||
mod trace;
|
|
||||||
|
|
||||||
const TITLE: &str = "WGPU example";
|
const TITLE: &str = "WGPU example";
|
||||||
const OUTPUT_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Bgra8UnormSrgb;
|
|
||||||
|
|
||||||
struct MainWindow {
|
struct MainWindow {
|
||||||
handle: Arc<Window>,
|
window: Arc<Window>,
|
||||||
device: wgpu::Device,
|
core: Core,
|
||||||
queue: wgpu::Queue,
|
|
||||||
surface: wgpu::Surface<'static>,
|
|
||||||
surface_configured: bool,
|
|
||||||
|
|
||||||
pipeline: Pipeline,
|
|
||||||
tripod: Mesh,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn new_tripod(device: &wgpu::Device) -> Mesh {
|
|
||||||
Mesh::new(
|
|
||||||
device,
|
|
||||||
&[
|
|
||||||
Vertex::new(vec3(0., 0., 0.), vec3(1., 0., 0.)),
|
|
||||||
Vertex::new(vec3(1., 0., 0.), vec3(1., 0., 0.)),
|
|
||||||
Vertex::new(vec3(0., 0., 0.), vec3(0., 1., 0.)),
|
|
||||||
Vertex::new(vec3(0., 1., 0.), vec3(0., 1., 0.)),
|
|
||||||
Vertex::new(vec3(0., 0., 0.), vec3(0., 0., 1.)),
|
|
||||||
Vertex::new(vec3(0., 0., 1.), vec3(0., 0., 1.)),
|
|
||||||
],
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn loop_list<T: Clone>(iter: impl IntoIterator<Item = T>) -> impl Iterator<Item = T> {
|
|
||||||
loop_list_ex(iter, identity, identity)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn loop_list_ex<T: Clone, U>(
|
|
||||||
iter: impl IntoIterator<Item = T>,
|
|
||||||
mut fa: impl FnMut(T) -> U,
|
|
||||||
mut fb: impl FnMut(T) -> U,
|
|
||||||
) -> impl Iterator<Item = U> {
|
|
||||||
gen move {
|
|
||||||
let mut iter = iter.into_iter();
|
|
||||||
let Some(first) = iter.next() else { return };
|
|
||||||
yield fa(first.clone());
|
|
||||||
for item in iter {
|
|
||||||
yield fb(item.clone());
|
|
||||||
yield fa(item);
|
|
||||||
}
|
|
||||||
yield fb(first);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MainWindow {
|
impl MainWindow {
|
||||||
fn new(event_loop: &ActiveEventLoop) -> Self {
|
fn new(event_loop: &ActiveEventLoop) -> Self {
|
||||||
let handle = event_loop
|
let window = event_loop
|
||||||
.create_window(Window::default_attributes().with_title(TITLE))
|
.create_window(Window::default_attributes().with_title(TITLE))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let handle = Arc::new(handle);
|
let window = Arc::new(window);
|
||||||
|
let gpu = pollster::block_on(init_gpu_inner(|instance| {
|
||||||
let (device, queue, surface) = pollster::block_on(init_gpu(Arc::clone(&handle))).unwrap();
|
instance.create_surface(Arc::clone(&window))
|
||||||
let pipeline = Pipeline::new(&device, OUTPUT_FORMAT);
|
}))
|
||||||
let tripod = new_tripod(&device);
|
.unwrap();
|
||||||
queue.submit([]); // flush buffer updates
|
let core = Core::new(gpu, uvec2(1, 1));
|
||||||
|
Self { window, core }
|
||||||
Self {
|
|
||||||
handle,
|
|
||||||
device,
|
|
||||||
queue,
|
|
||||||
surface,
|
|
||||||
surface_configured: false,
|
|
||||||
pipeline,
|
|
||||||
tripod,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render(&self, output: &wgpu::Texture) {
|
|
||||||
let camera = OrbitalCamera {
|
|
||||||
position_yaw: PI / 4.,
|
|
||||||
position_pitch: 0.5f32.sqrt().atan(),
|
|
||||||
distance: 3.0,
|
|
||||||
};
|
|
||||||
let aspect = {
|
|
||||||
let size = output.size();
|
|
||||||
let w = size.width as f32;
|
|
||||||
let h = size.height as f32;
|
|
||||||
w / h
|
|
||||||
};
|
|
||||||
let perspective = Mat4::perspective_lh(PI / 3., aspect, 1e-2, 1e2);
|
|
||||||
self.pipeline.set_look(
|
|
||||||
&self.queue,
|
|
||||||
LookParams {
|
|
||||||
m: perspective * camera.transform(),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
self.queue.submit([]); // flush buffer updates
|
|
||||||
|
|
||||||
let view = output.create_view(&wgpu::TextureViewDescriptor::default());
|
|
||||||
let mut encoder = self
|
|
||||||
.device
|
|
||||||
.create_command_encoder(&wgpu::CommandEncoderDescriptor::default());
|
|
||||||
let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor {
|
|
||||||
color_attachments: &[Some(wgpu::RenderPassColorAttachment {
|
|
||||||
view: &view,
|
|
||||||
depth_slice: None,
|
|
||||||
resolve_target: None,
|
|
||||||
ops: wgpu::Operations {
|
|
||||||
load: wgpu::LoadOp::Clear(wgpu::Color {
|
|
||||||
r: 0.1,
|
|
||||||
g: 0.2,
|
|
||||||
b: 0.8,
|
|
||||||
a: 1.0,
|
|
||||||
}),
|
|
||||||
store: wgpu::StoreOp::Store,
|
|
||||||
},
|
|
||||||
})],
|
|
||||||
depth_stencil_attachment: None,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
self.pipeline.render(&mut pass, [&self.tripod]);
|
|
||||||
|
|
||||||
let source = Source {
|
|
||||||
position_yaw: 0.0,
|
|
||||||
position_pitch: PI / 3.,
|
|
||||||
distance: 1.0,
|
|
||||||
radius: 0.125,
|
|
||||||
spread: 0.125,
|
|
||||||
};
|
|
||||||
|
|
||||||
let contour: Vec<Vertex> = loop_list(source.contour(17))
|
|
||||||
.map(|pos| Vertex {
|
|
||||||
pos,
|
|
||||||
color: vec3(1., 1., 1.),
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
self.pipeline
|
|
||||||
.render(&mut pass, [&Mesh::new(&self.device, &contour)]);
|
|
||||||
|
|
||||||
const BASE_R: f32 = 2.;
|
|
||||||
const BASE_POS: Vec3 = vec3(0., 0., -BASE_R);
|
|
||||||
const BASE: Sphere = Sphere {
|
|
||||||
position: vec3(0., 0., -BASE_R),
|
|
||||||
radius: BASE_R,
|
|
||||||
};
|
|
||||||
fn sphere(pos: Vec3) -> Sphere {
|
|
||||||
Sphere {
|
|
||||||
position: pos,
|
|
||||||
radius: BASE_POS.distance(pos) - BASE_R,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let scene = Scene {
|
|
||||||
objects: vec![
|
|
||||||
BASE,
|
|
||||||
sphere(vec3(0., 0., 0.1)),
|
|
||||||
sphere(vec3(0.3, 0., 0.1)),
|
|
||||||
sphere(vec3(0.1, 0.3, 0.1)),
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut prng = rand_pcg::Pcg64::new(42, 0);
|
|
||||||
let rays: Vec<Vertex> = (0..10000)
|
|
||||||
.flat_map(|_| {
|
|
||||||
let ray = source.make_ray(&mut prng);
|
|
||||||
if let Some(ray) = scene.trace_ray(ray) {
|
|
||||||
[
|
|
||||||
Vertex {
|
|
||||||
pos: ray.base - 0.02 * ray.dir,
|
|
||||||
color: vec3(1., 1., 1.),
|
|
||||||
},
|
|
||||||
Vertex {
|
|
||||||
pos: ray.base,
|
|
||||||
color: vec3(0., 1., 0.),
|
|
||||||
},
|
|
||||||
]
|
|
||||||
} else {
|
|
||||||
[
|
|
||||||
Vertex {
|
|
||||||
pos: ray.base,
|
|
||||||
color: vec3(1., 1., 1.),
|
|
||||||
},
|
|
||||||
Vertex {
|
|
||||||
pos: ray.base + 0.1 * ray.dir,
|
|
||||||
color: vec3(1., 0., 0.),
|
|
||||||
},
|
|
||||||
]
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
self.pipeline
|
|
||||||
.render(&mut pass, [&Mesh::new(&self.device, &rays)]);
|
|
||||||
|
|
||||||
drop(pass);
|
|
||||||
self.queue.submit(std::iter::once(encoder.finish()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn event(&mut self, event_loop: &ActiveEventLoop, event: WindowEvent) {
|
fn event(&mut self, event_loop: &ActiveEventLoop, event: WindowEvent) {
|
||||||
match event {
|
match event {
|
||||||
WindowEvent::CloseRequested => event_loop.exit(),
|
WindowEvent::CloseRequested => event_loop.exit(),
|
||||||
WindowEvent::Resized(physical_size) => {
|
WindowEvent::Resized(physical_size) => self
|
||||||
self.surface.configure(
|
.core
|
||||||
&self.device,
|
.configure(uvec2(physical_size.width, physical_size.height)),
|
||||||
&wgpu::SurfaceConfiguration {
|
WindowEvent::RedrawRequested => self.core.redraw(&RedrawArgs {
|
||||||
usage: wgpu::TextureUsages::RENDER_ATTACHMENT
|
camera_position: SphericalPosition {
|
||||||
| wgpu::TextureUsages::COPY_DST,
|
yaw: PI / 4.,
|
||||||
format: OUTPUT_FORMAT,
|
pitch: 0.5f32.sqrt().atan(),
|
||||||
width: physical_size.width,
|
distance: 3.0,
|
||||||
height: physical_size.height,
|
|
||||||
present_mode: wgpu::PresentMode::Fifo,
|
|
||||||
alpha_mode: wgpu::CompositeAlphaMode::Auto,
|
|
||||||
view_formats: vec![],
|
|
||||||
desired_maximum_frame_latency: 2,
|
|
||||||
},
|
},
|
||||||
);
|
light_position: SphericalPosition {
|
||||||
self.surface_configured = true;
|
yaw: 0.0,
|
||||||
}
|
pitch: PI / 3.,
|
||||||
WindowEvent::RedrawRequested => {
|
distance: 1.0,
|
||||||
if !self.surface_configured {
|
},
|
||||||
return;
|
light_radius: 0.125,
|
||||||
}
|
light_spread: 0.125,
|
||||||
let output = self.surface.get_current_texture().unwrap();
|
accum_sigma: 0.025,
|
||||||
self.render(&output.texture);
|
accum_scale: 0.01,
|
||||||
output.present();
|
reflections: 2,
|
||||||
}
|
use_normal: UseNormal::Light,
|
||||||
|
show_axes: true,
|
||||||
|
show_shapes: true,
|
||||||
|
show_hit_emission: false,
|
||||||
|
show_miss_emission: false,
|
||||||
|
show_direct_hit: false,
|
||||||
|
show_indirect_hit: false,
|
||||||
|
show_light: true,
|
||||||
|
}),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -270,34 +91,11 @@ impl ApplicationHandler for Application {
|
||||||
.main_window
|
.main_window
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.expect("window must exist to recieve events");
|
.expect("window must exist to recieve events");
|
||||||
assert_eq!(window.handle.id(), window_id);
|
assert_eq!(window.window.id(), window_id);
|
||||||
window.event(event_loop, event);
|
window.event(event_loop, event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn init_gpu<'window>(
|
|
||||||
wnd: impl wgpu::WindowHandle + 'window,
|
|
||||||
) -> Result<(wgpu::Device, wgpu::Queue, wgpu::Surface<'window>), Box<dyn Error>> {
|
|
||||||
let instance = wgpu::Instance::new(&wgpu::InstanceDescriptor {
|
|
||||||
backends: wgpu::Backends::PRIMARY,
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
let surface = instance.create_surface(wnd)?;
|
|
||||||
let adapter = instance
|
|
||||||
.request_adapter(&wgpu::RequestAdapterOptions {
|
|
||||||
power_preference: wgpu::PowerPreference::default(),
|
|
||||||
compatible_surface: Some(&surface),
|
|
||||||
force_fallback_adapter: false,
|
|
||||||
})
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
let (device, queue) = adapter
|
|
||||||
.request_device(&wgpu::DeviceDescriptor::default())
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
Ok((device, queue, surface))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let event_loop = EventLoop::new().unwrap();
|
let event_loop = EventLoop::new().unwrap();
|
||||||
event_loop.set_control_flow(ControlFlow::Wait);
|
event_loop.set_control_flow(ControlFlow::Wait);
|
||||||
|
|
|
||||||
33
src/render/colormap.wgsl
Normal file
33
src/render/colormap.wgsl
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
struct LookParams {
|
||||||
|
m: mat4x4f,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Vertex {
|
||||||
|
@location(0) pos: vec3f,
|
||||||
|
@location(1) color: vec3f,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Varying {
|
||||||
|
@builtin(position) screen: vec4f,
|
||||||
|
@location(0) color: vec4f,
|
||||||
|
}
|
||||||
|
|
||||||
|
@group(0) @binding(0) var<uniform> look: LookParams;
|
||||||
|
|
||||||
|
fn colormap(light: vec3f) -> vec3f {
|
||||||
|
let avg = dot(light, vec3f(0.2126, 0.7152, 0.0722));
|
||||||
|
let scale = 1. / (1. + avg);
|
||||||
|
return scale * light;
|
||||||
|
}
|
||||||
|
|
||||||
|
@vertex
|
||||||
|
fn on_vertex(in: Vertex) -> Varying {
|
||||||
|
let pos = look.m * vec4f(in.pos, 1.0);
|
||||||
|
let color = vec4f(in.color, 1.0);
|
||||||
|
return Varying(pos, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
@fragment
|
||||||
|
fn on_fragment(in: Varying) -> @location(0) vec4f {
|
||||||
|
return vec4f(colormap(in.color.rgb), in.color.a);
|
||||||
|
}
|
||||||
169
src/render/faces.rs
Normal file
169
src/render/faces.rs
Normal file
|
|
@ -0,0 +1,169 @@
|
||||||
|
use std::mem::offset_of;
|
||||||
|
|
||||||
|
use bytemuck::{Pod, Zeroable, bytes_of, cast_slice};
|
||||||
|
use glam::{Mat4, Vec3};
|
||||||
|
use wgpu::util::DeviceExt as _;
|
||||||
|
|
||||||
|
use crate::render::{DEPTH_FORMAT, OUTPUT_FORMAT};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct LookParams {
|
||||||
|
pub m: Mat4,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct Vertex {
|
||||||
|
pub pos: Vec3,
|
||||||
|
pub color: Vec3,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Vertex {
|
||||||
|
pub fn new(pos: Vec3, color: Vec3) -> Self {
|
||||||
|
Self { pos, color }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Mesh {
|
||||||
|
vertex_buffer: wgpu::Buffer,
|
||||||
|
index_buffer: wgpu::Buffer,
|
||||||
|
index_count: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Mesh {
|
||||||
|
pub fn new(device: &wgpu::Device, vertices: &[Vertex], indices: &[u16]) -> Self {
|
||||||
|
if vertices.len() >= 1 << 16 {
|
||||||
|
panic!("too many vertices");
|
||||||
|
}
|
||||||
|
for index in indices {
|
||||||
|
if *index as usize >= vertices.len() {
|
||||||
|
panic!(
|
||||||
|
"vertex index out of bounds: {index} out of {}",
|
||||||
|
vertices.len()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
|
label: None,
|
||||||
|
usage: wgpu::BufferUsages::VERTEX,
|
||||||
|
contents: cast_slice(vertices),
|
||||||
|
});
|
||||||
|
let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
|
||||||
|
label: None,
|
||||||
|
usage: wgpu::BufferUsages::INDEX,
|
||||||
|
contents: cast_slice(indices),
|
||||||
|
});
|
||||||
|
Self {
|
||||||
|
vertex_buffer,
|
||||||
|
index_buffer,
|
||||||
|
index_count: indices.len().try_into().expect("too many indices"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Pipeline {
|
||||||
|
look_buf: wgpu::Buffer,
|
||||||
|
bindings: wgpu::BindGroup,
|
||||||
|
pipeline: wgpu::RenderPipeline,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Pipeline {
|
||||||
|
pub fn new(device: &wgpu::Device) -> Self {
|
||||||
|
let look_buf = device.create_buffer(&wgpu::BufferDescriptor {
|
||||||
|
label: None,
|
||||||
|
size: size_of::<LookParams>() as u64,
|
||||||
|
usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST,
|
||||||
|
mapped_at_creation: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||||
|
label: None,
|
||||||
|
source: wgpu::ShaderSource::Wgsl(super::COLORMAP_SHADER.into()),
|
||||||
|
});
|
||||||
|
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||||
|
label: None,
|
||||||
|
layout: None,
|
||||||
|
vertex: wgpu::VertexState {
|
||||||
|
module: &shader,
|
||||||
|
entry_point: None,
|
||||||
|
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||||
|
buffers: &[wgpu::VertexBufferLayout {
|
||||||
|
array_stride: size_of::<Vertex>() as u64,
|
||||||
|
step_mode: wgpu::VertexStepMode::Vertex,
|
||||||
|
attributes: &[
|
||||||
|
wgpu::VertexAttribute {
|
||||||
|
shader_location: 0,
|
||||||
|
offset: offset_of!(Vertex, pos) as u64,
|
||||||
|
format: wgpu::VertexFormat::Float32x3,
|
||||||
|
},
|
||||||
|
wgpu::VertexAttribute {
|
||||||
|
shader_location: 1,
|
||||||
|
offset: offset_of!(Vertex, color) as u64,
|
||||||
|
format: wgpu::VertexFormat::Float32x3,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}],
|
||||||
|
},
|
||||||
|
primitive: wgpu::PrimitiveState {
|
||||||
|
topology: wgpu::PrimitiveTopology::TriangleList,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
depth_stencil: Some(wgpu::DepthStencilState {
|
||||||
|
format: DEPTH_FORMAT,
|
||||||
|
depth_write_enabled: true,
|
||||||
|
depth_compare: wgpu::CompareFunction::LessEqual,
|
||||||
|
stencil: wgpu::StencilState::default(),
|
||||||
|
bias: wgpu::DepthBiasState::default(),
|
||||||
|
}),
|
||||||
|
multisample: wgpu::MultisampleState {
|
||||||
|
count: 1,
|
||||||
|
mask: !0,
|
||||||
|
alpha_to_coverage_enabled: false,
|
||||||
|
},
|
||||||
|
fragment: Some(wgpu::FragmentState {
|
||||||
|
module: &shader,
|
||||||
|
entry_point: None,
|
||||||
|
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||||
|
targets: &[Some(wgpu::ColorTargetState {
|
||||||
|
format: OUTPUT_FORMAT,
|
||||||
|
blend: Some(wgpu::BlendState::REPLACE),
|
||||||
|
write_mask: wgpu::ColorWrites::ALL,
|
||||||
|
})],
|
||||||
|
}),
|
||||||
|
multiview: None,
|
||||||
|
cache: None,
|
||||||
|
});
|
||||||
|
let bindings = device.create_bind_group(&wgpu::BindGroupDescriptor {
|
||||||
|
label: None,
|
||||||
|
layout: &pipeline.get_bind_group_layout(0),
|
||||||
|
entries: &[wgpu::BindGroupEntry {
|
||||||
|
binding: 0,
|
||||||
|
resource: look_buf.as_entire_binding(),
|
||||||
|
}],
|
||||||
|
});
|
||||||
|
Self {
|
||||||
|
look_buf,
|
||||||
|
bindings,
|
||||||
|
pipeline,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_look(&self, queue: &wgpu::Queue, look: LookParams) {
|
||||||
|
queue.write_buffer(&self.look_buf, 0, bytes_of(&look));
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn render<'a>(
|
||||||
|
&self,
|
||||||
|
pass: &mut wgpu::RenderPass,
|
||||||
|
meshes: impl IntoIterator<Item = &'a Mesh>,
|
||||||
|
) {
|
||||||
|
pass.set_pipeline(&self.pipeline);
|
||||||
|
pass.set_bind_group(0, &self.bindings, &[]);
|
||||||
|
for mesh in meshes {
|
||||||
|
pass.set_vertex_buffer(0, mesh.vertex_buffer.slice(..));
|
||||||
|
pass.set_index_buffer(mesh.index_buffer.slice(..), wgpu::IndexFormat::Uint16);
|
||||||
|
pass.draw_indexed(0..mesh.index_count, 0, 0..1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,8 @@ use bytemuck::{Pod, Zeroable, bytes_of, cast_slice};
|
||||||
use glam::{Mat4, Vec3};
|
use glam::{Mat4, Vec3};
|
||||||
use wgpu::util::DeviceExt as _;
|
use wgpu::util::DeviceExt as _;
|
||||||
|
|
||||||
|
use crate::render::{DEPTH_FORMAT, OUTPUT_FORMAT};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
#[derive(Debug, Clone, Copy, Pod, Zeroable)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct LookParams {
|
pub struct LookParams {
|
||||||
|
|
@ -49,7 +51,7 @@ pub struct Pipeline {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pipeline {
|
impl Pipeline {
|
||||||
pub fn new(device: &wgpu::Device, format: wgpu::TextureFormat) -> Self {
|
pub fn new(device: &wgpu::Device) -> Self {
|
||||||
let look_buf = device.create_buffer(&wgpu::BufferDescriptor {
|
let look_buf = device.create_buffer(&wgpu::BufferDescriptor {
|
||||||
label: None,
|
label: None,
|
||||||
size: size_of::<LookParams>() as u64,
|
size: size_of::<LookParams>() as u64,
|
||||||
|
|
@ -57,10 +59,9 @@ impl Pipeline {
|
||||||
mapped_at_creation: false,
|
mapped_at_creation: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
let shader = std::fs::read_to_string("shaders/line.wgsl").unwrap();
|
|
||||||
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
|
||||||
label: None,
|
label: None,
|
||||||
source: wgpu::ShaderSource::Wgsl(shader.into()),
|
source: wgpu::ShaderSource::Wgsl(super::SIMPLE_SHADER.into()),
|
||||||
});
|
});
|
||||||
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
let pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
|
||||||
label: None,
|
label: None,
|
||||||
|
|
@ -90,7 +91,17 @@ impl Pipeline {
|
||||||
topology: wgpu::PrimitiveTopology::LineList,
|
topology: wgpu::PrimitiveTopology::LineList,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
depth_stencil: None,
|
depth_stencil: Some(wgpu::DepthStencilState {
|
||||||
|
format: DEPTH_FORMAT,
|
||||||
|
depth_write_enabled: true,
|
||||||
|
depth_compare: wgpu::CompareFunction::LessEqual,
|
||||||
|
stencil: wgpu::StencilState::default(),
|
||||||
|
bias: wgpu::DepthBiasState {
|
||||||
|
constant: -2,
|
||||||
|
slope_scale: 0.,
|
||||||
|
clamp: 0.,
|
||||||
|
},
|
||||||
|
}),
|
||||||
multisample: wgpu::MultisampleState {
|
multisample: wgpu::MultisampleState {
|
||||||
count: 1,
|
count: 1,
|
||||||
mask: !0,
|
mask: !0,
|
||||||
|
|
@ -101,7 +112,7 @@ impl Pipeline {
|
||||||
entry_point: None,
|
entry_point: None,
|
||||||
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
compilation_options: wgpu::PipelineCompilationOptions::default(),
|
||||||
targets: &[Some(wgpu::ColorTargetState {
|
targets: &[Some(wgpu::ColorTargetState {
|
||||||
format,
|
format: OUTPUT_FORMAT,
|
||||||
blend: Some(wgpu::BlendState::REPLACE),
|
blend: Some(wgpu::BlendState::REPLACE),
|
||||||
write_mask: wgpu::ColorWrites::ALL,
|
write_mask: wgpu::ColorWrites::ALL,
|
||||||
})],
|
})],
|
||||||
|
|
|
||||||
|
|
@ -1 +1,8 @@
|
||||||
|
pub mod faces;
|
||||||
pub mod lines;
|
pub mod lines;
|
||||||
|
|
||||||
|
static SIMPLE_SHADER: &str = include_str!("simple.wgsl");
|
||||||
|
static COLORMAP_SHADER: &str = include_str!("colormap.wgsl");
|
||||||
|
|
||||||
|
pub const OUTPUT_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Bgra8UnormSrgb;
|
||||||
|
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth24Plus;
|
||||||
|
|
|
||||||
348
src/shape.rs
Normal file
348
src/shape.rs
Normal file
|
|
@ -0,0 +1,348 @@
|
||||||
|
use std::f32::consts::PI;
|
||||||
|
|
||||||
|
use glam::{Vec3, vec3};
|
||||||
|
|
||||||
|
pub type Face = [u16; 3];
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Mesh {
|
||||||
|
pub vertices: Vec<Vec3>,
|
||||||
|
pub indices: Vec<Face>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn sphere(subdiv: usize) -> Mesh {
|
||||||
|
assert!(subdiv >= 2);
|
||||||
|
assert!(2 * subdiv * (subdiv - 1) + 2 <= 1 << 16);
|
||||||
|
|
||||||
|
let mut vertices = Vec::new();
|
||||||
|
vertices.push(Vec3::Z);
|
||||||
|
for j in 1..subdiv {
|
||||||
|
let v = j as f32 / subdiv as f32;
|
||||||
|
let theta = PI * v;
|
||||||
|
let (xy, z) = theta.sin_cos();
|
||||||
|
for i in 0..2 * subdiv {
|
||||||
|
let u = i as f32 / (2 * subdiv) as f32;
|
||||||
|
let phi = 2. * PI * u;
|
||||||
|
let (y, x) = phi.sin_cos();
|
||||||
|
vertices.push(vec3(xy * x, xy * y, z));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vertices.push(-Vec3::Z);
|
||||||
|
assert_eq!(vertices.len(), 2 * subdiv * (subdiv - 1) + 2);
|
||||||
|
|
||||||
|
let mut indices = Vec::new();
|
||||||
|
let subdiv = subdiv as u16;
|
||||||
|
for i in 0..2 * subdiv - 1 {
|
||||||
|
indices.push([0, i + 1, i + 2]);
|
||||||
|
}
|
||||||
|
indices.push([0, 2 * subdiv, 1]);
|
||||||
|
for j in 0..subdiv - 2 {
|
||||||
|
let k1 = j * 2 * subdiv + 1;
|
||||||
|
let k2 = k1 + 2 * subdiv;
|
||||||
|
for i in 0..2 * subdiv - 1 {
|
||||||
|
let a = k1 + i;
|
||||||
|
let b = k2 + i;
|
||||||
|
indices.push([a, b, b + 1]);
|
||||||
|
indices.push([a, b + 1, a + 1]);
|
||||||
|
}
|
||||||
|
let a = k1 + 2 * subdiv - 1;
|
||||||
|
let b = k2 + 2 * subdiv - 1;
|
||||||
|
indices.push([a, b, k2]);
|
||||||
|
indices.push([a, k2, k1]);
|
||||||
|
}
|
||||||
|
let k1 = 2 * subdiv * (subdiv - 2) + 1;
|
||||||
|
let k2 = 2 * subdiv * (subdiv - 1) + 1;
|
||||||
|
for i in 0..2 * subdiv - 1 {
|
||||||
|
indices.push([k1 + i, k2, k1 + i + 1]);
|
||||||
|
}
|
||||||
|
indices.push([k1 + 2 * subdiv - 1, k2, k1]);
|
||||||
|
|
||||||
|
Mesh { vertices, indices }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn octo_sphere(subdiv: usize) -> Mesh {
|
||||||
|
assert!(subdiv >= 2);
|
||||||
|
|
||||||
|
let mut vertices = Vec::new();
|
||||||
|
let theta_step = PI / subdiv as f32;
|
||||||
|
let v_belt = |j: usize, n: usize| {
|
||||||
|
let phi_step = 2. * PI / n as f32;
|
||||||
|
let theta = j as f32 * theta_step;
|
||||||
|
let (xy, z) = theta.sin_cos();
|
||||||
|
(0..n).map(move |i| {
|
||||||
|
let phi = i as f32 * phi_step;
|
||||||
|
let (y, x) = phi.sin_cos();
|
||||||
|
vec3(xy * x, xy * y, z)
|
||||||
|
})
|
||||||
|
};
|
||||||
|
vertices.push(Vec3::Z);
|
||||||
|
for j in 1..subdiv / 2 {
|
||||||
|
vertices.extend(v_belt(j, 4 * j));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let j = subdiv / 2;
|
||||||
|
vertices.extend(v_belt(j, 4 * j));
|
||||||
|
}
|
||||||
|
for j in (subdiv + 2) / 2..subdiv {
|
||||||
|
vertices.extend(v_belt(j, 4 * (subdiv - j)));
|
||||||
|
}
|
||||||
|
vertices.push(-Vec3::Z);
|
||||||
|
|
||||||
|
fn i_cap(top: usize, start: usize) -> impl Iterator<Item = Face> {
|
||||||
|
gen move {
|
||||||
|
yield [top, start, start + 1];
|
||||||
|
yield [top, start + 1, start + 2];
|
||||||
|
yield [top, start + 2, start + 3];
|
||||||
|
yield [top, start + 3, start];
|
||||||
|
}
|
||||||
|
.map(face)
|
||||||
|
}
|
||||||
|
fn i_skew_belt(
|
||||||
|
short_start: usize,
|
||||||
|
long_start: usize,
|
||||||
|
seg_short_face: usize,
|
||||||
|
) -> impl Iterator<Item = Face> {
|
||||||
|
gen move {
|
||||||
|
let seg_long_face = seg_short_face + 1;
|
||||||
|
let n_short_total = 4 * seg_short_face;
|
||||||
|
let n_long_total = 4 * seg_long_face;
|
||||||
|
for face in 0..4 {
|
||||||
|
let short = move |k| short_start + (face * seg_short_face + k) % n_short_total;
|
||||||
|
let long = move |k| long_start + (face * seg_long_face + k) % n_long_total;
|
||||||
|
yield [short(0), long(0), long(1)];
|
||||||
|
for k in 0..seg_short_face {
|
||||||
|
yield [short(k), long(k + 1), short(k + 1)];
|
||||||
|
yield [short(k + 1), long(k + 1), long(k + 2)];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.map(face)
|
||||||
|
}
|
||||||
|
fn i_belt(start1: usize, start2: usize, seg_per_face: usize) -> impl Iterator<Item = Face> {
|
||||||
|
gen move {
|
||||||
|
let n = 4 * seg_per_face;
|
||||||
|
for k in 0..n {
|
||||||
|
let k2 = (k + 1) % n;
|
||||||
|
let a = start1 + k;
|
||||||
|
let b = start2 + k;
|
||||||
|
let c = start1 + k2;
|
||||||
|
let d = start2 + k2;
|
||||||
|
yield [a, b, c];
|
||||||
|
yield [c, b, d];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.map(face)
|
||||||
|
}
|
||||||
|
fn face(f: [usize; 3]) -> Face {
|
||||||
|
f.map(|i| i as u16)
|
||||||
|
}
|
||||||
|
fn flip(f: Face) -> Face {
|
||||||
|
[f[1], f[0], f[2]]
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut indices = Vec::new();
|
||||||
|
indices.extend(i_cap(0, 1));
|
||||||
|
let mut k = 1;
|
||||||
|
for j in 1..subdiv - 1 {
|
||||||
|
let j2 = (subdiv - 1) - j;
|
||||||
|
match j.cmp(&j2) {
|
||||||
|
std::cmp::Ordering::Less => {
|
||||||
|
let n = 4 * j;
|
||||||
|
indices.extend(i_skew_belt(k, k + n, j));
|
||||||
|
k += n;
|
||||||
|
}
|
||||||
|
std::cmp::Ordering::Equal => {
|
||||||
|
let n = 4 * j;
|
||||||
|
indices.extend(i_belt(k, k + n, j));
|
||||||
|
k += n;
|
||||||
|
}
|
||||||
|
std::cmp::Ordering::Greater => {
|
||||||
|
let n = 4 * (j2 + 1);
|
||||||
|
indices.extend(i_skew_belt(k + n, k, j2).map(flip));
|
||||||
|
k += n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
indices.extend(i_cap(k + 4, k).map(flip));
|
||||||
|
|
||||||
|
Mesh { vertices, indices }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use approx::abs_diff_eq;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sphere_2_topology() {
|
||||||
|
assert_eq!(
|
||||||
|
sphere(2).indices,
|
||||||
|
vec![
|
||||||
|
[0, 1, 2],
|
||||||
|
[0, 2, 3],
|
||||||
|
[0, 3, 4],
|
||||||
|
[0, 4, 1],
|
||||||
|
[1, 5, 2],
|
||||||
|
[2, 5, 3],
|
||||||
|
[3, 5, 4],
|
||||||
|
[4, 5, 1],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn are_equal_eps(left: &[Vec3], right: &[Vec3], eps: f32) -> bool {
|
||||||
|
if left.len() != right.len() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
left.iter()
|
||||||
|
.zip(right.iter())
|
||||||
|
.all(|(a, b)| abs_diff_eq!(a, b, epsilon = eps))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sphere_2_geometry() {
|
||||||
|
let left = sphere(2).vertices;
|
||||||
|
let right = [Vec3::Z, Vec3::X, Vec3::Y, -Vec3::X, -Vec3::Y, -Vec3::Z];
|
||||||
|
assert!(
|
||||||
|
are_equal_eps(&left, &right, 1e-6),
|
||||||
|
"assertion `left == right` failed\n left: {left:?}\n right: {right:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_sphere_3_topology() {
|
||||||
|
assert_eq!(
|
||||||
|
sphere(3).indices,
|
||||||
|
vec![
|
||||||
|
// top
|
||||||
|
[0, 1, 2],
|
||||||
|
[0, 2, 3],
|
||||||
|
[0, 3, 4],
|
||||||
|
[0, 4, 5],
|
||||||
|
[0, 5, 6],
|
||||||
|
[0, 6, 1],
|
||||||
|
// belt
|
||||||
|
[1, 7, 8],
|
||||||
|
[1, 8, 2],
|
||||||
|
[2, 8, 9],
|
||||||
|
[2, 9, 3],
|
||||||
|
[3, 9, 10],
|
||||||
|
[3, 10, 4],
|
||||||
|
[4, 10, 11],
|
||||||
|
[4, 11, 5],
|
||||||
|
[5, 11, 12],
|
||||||
|
[5, 12, 6],
|
||||||
|
[6, 12, 7],
|
||||||
|
[6, 7, 1],
|
||||||
|
// bottom
|
||||||
|
[7, 13, 8],
|
||||||
|
[8, 13, 9],
|
||||||
|
[9, 13, 10],
|
||||||
|
[10, 13, 11],
|
||||||
|
[11, 13, 12],
|
||||||
|
[12, 13, 7],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_octo_sphere_2_topology() {
|
||||||
|
assert_eq!(
|
||||||
|
octo_sphere(2).indices,
|
||||||
|
vec![
|
||||||
|
[0, 1, 2],
|
||||||
|
[0, 2, 3],
|
||||||
|
[0, 3, 4],
|
||||||
|
[0, 4, 1],
|
||||||
|
[1, 5, 2],
|
||||||
|
[2, 5, 3],
|
||||||
|
[3, 5, 4],
|
||||||
|
[4, 5, 1],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_octo_sphere_2_geometry() {
|
||||||
|
let left = octo_sphere(2).vertices;
|
||||||
|
let right = [Vec3::Z, Vec3::X, Vec3::Y, -Vec3::X, -Vec3::Y, -Vec3::Z];
|
||||||
|
assert!(
|
||||||
|
are_equal_eps(&left, &right, 1e-6),
|
||||||
|
"assertion `left == right` failed\n left: {left:?}\n right: {right:?}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_octo_sphere_3_topology() {
|
||||||
|
assert_eq!(
|
||||||
|
octo_sphere(3).indices,
|
||||||
|
vec![
|
||||||
|
// top
|
||||||
|
[0, 1, 2],
|
||||||
|
[0, 2, 3],
|
||||||
|
[0, 3, 4],
|
||||||
|
[0, 4, 1],
|
||||||
|
// belt
|
||||||
|
[1, 5, 2],
|
||||||
|
[2, 5, 6],
|
||||||
|
[2, 6, 3],
|
||||||
|
[3, 6, 7],
|
||||||
|
[3, 7, 4],
|
||||||
|
[4, 7, 8],
|
||||||
|
[4, 8, 1],
|
||||||
|
[1, 8, 5],
|
||||||
|
// bottom
|
||||||
|
[5, 9, 6],
|
||||||
|
[6, 9, 7],
|
||||||
|
[7, 9, 8],
|
||||||
|
[8, 9, 5],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_octo_sphere_4_topology() {
|
||||||
|
assert_eq!(
|
||||||
|
octo_sphere(4).indices,
|
||||||
|
vec![
|
||||||
|
// top
|
||||||
|
[0, 1, 2],
|
||||||
|
[0, 2, 3],
|
||||||
|
[0, 3, 4],
|
||||||
|
[0, 4, 1],
|
||||||
|
// belt 1
|
||||||
|
[1, 5, 6],
|
||||||
|
[1, 6, 2],
|
||||||
|
[2, 6, 7],
|
||||||
|
[2, 7, 8],
|
||||||
|
[2, 8, 3],
|
||||||
|
[3, 8, 9],
|
||||||
|
[3, 9, 10],
|
||||||
|
[3, 10, 4],
|
||||||
|
[4, 10, 11],
|
||||||
|
[4, 11, 12],
|
||||||
|
[4, 12, 1],
|
||||||
|
[1, 12, 5],
|
||||||
|
// belt 2
|
||||||
|
[5, 13, 6],
|
||||||
|
[6, 13, 14],
|
||||||
|
[6, 14, 7],
|
||||||
|
[7, 14, 8],
|
||||||
|
[8, 14, 15],
|
||||||
|
[8, 15, 9],
|
||||||
|
[9, 15, 10],
|
||||||
|
[10, 15, 16],
|
||||||
|
[10, 16, 11],
|
||||||
|
[11, 16, 12],
|
||||||
|
[12, 16, 13],
|
||||||
|
[12, 13, 5],
|
||||||
|
// bottom
|
||||||
|
[13, 17, 14],
|
||||||
|
[14, 17, 15],
|
||||||
|
[15, 17, 16],
|
||||||
|
[16, 17, 13],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
66
src/trace.rs
66
src/trace.rs
|
|
@ -1,7 +1,7 @@
|
||||||
use std::f32::consts::PI;
|
use std::f32::consts::PI;
|
||||||
|
|
||||||
use glam::{Mat4, Vec2, Vec3, vec3};
|
use glam::{Mat4, Vec2, Vec3, vec3};
|
||||||
use rand_distr::Distribution;
|
use rand_distr::{Distribution, UnitSphere};
|
||||||
|
|
||||||
use crate::{camera::OrbitalCamera, ray::Ray};
|
use crate::{camera::OrbitalCamera, ray::Ray};
|
||||||
|
|
||||||
|
|
@ -76,10 +76,17 @@ impl Source {
|
||||||
pub struct Sphere {
|
pub struct Sphere {
|
||||||
pub position: Vec3,
|
pub position: Vec3,
|
||||||
pub radius: f32,
|
pub radius: f32,
|
||||||
|
pub color: Vec3,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Hit1 {
|
||||||
|
pos: Vec3,
|
||||||
|
dist: f32,
|
||||||
|
normal: Vec3,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sphere {
|
impl Sphere {
|
||||||
fn trace_ray(&self, ray: Ray) -> Option<f32> {
|
fn trace_ray(&self, ray: Ray) -> Option<Hit1> {
|
||||||
// let t: f32;
|
// let t: f32;
|
||||||
// let hit = ray.base + t * ray.dir;
|
// let hit = ray.base + t * ray.dir;
|
||||||
// (hit - self.position).length() == self.radius;
|
// (hit - self.position).length() == self.radius;
|
||||||
|
|
@ -94,7 +101,10 @@ impl Sphere {
|
||||||
if d4 < 0. {
|
if d4 < 0. {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
Some((-b2 - d4.sqrt()) / a)
|
let dist = (-b2 - d4.sqrt()) / a;
|
||||||
|
let pos = ray.advance(dist).base;
|
||||||
|
let normal = (pos - self.position).normalize();
|
||||||
|
Some(Hit1 { pos, dist, normal })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -103,13 +113,53 @@ pub struct Scene {
|
||||||
pub objects: Vec<Sphere>,
|
pub objects: Vec<Sphere>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct Hit<'a> {
|
||||||
|
pub incident: Ray,
|
||||||
|
pub normal: Vec3,
|
||||||
|
pub object: &'a Sphere,
|
||||||
|
}
|
||||||
|
|
||||||
impl Scene {
|
impl Scene {
|
||||||
pub fn trace_ray(&self, ray: Ray) -> Option<Ray> {
|
pub fn trace_ray(&self, ray: Ray) -> Option<Hit<'_>> {
|
||||||
let dist = self
|
const EPS: f32 = -1e-3;
|
||||||
|
let hit = self
|
||||||
.objects
|
.objects
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|obj| obj.trace_ray(ray))
|
.filter_map(|obj| obj.trace_ray(ray).map(|hit| (obj, hit)))
|
||||||
.min_by(f32::total_cmp);
|
.filter(|(_obj, h)| h.dist >= EPS)
|
||||||
Some(ray.advance(dist?))
|
.min_by(|a, b| f32::total_cmp(&a.1.dist, &b.1.dist))?;
|
||||||
|
Some(Hit {
|
||||||
|
incident: Ray {
|
||||||
|
base: hit.1.pos,
|
||||||
|
dir: ray.dir,
|
||||||
|
},
|
||||||
|
normal: hit.1.normal,
|
||||||
|
object: hit.0,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Reflector {
|
||||||
|
fn brdf(&self, normal: Vec3, incident: Vec3, reflected: Vec3) -> f32 /* 1/sr */;
|
||||||
|
fn reflect(&self, rgen: &mut impl rand::Rng, normal: Vec3, incident: Vec3) -> Vec3;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Lambertian;
|
||||||
|
|
||||||
|
impl Reflector for Lambertian {
|
||||||
|
fn brdf(&self, _normal: Vec3, _incident: Vec3, _reflected: Vec3) -> f32 {
|
||||||
|
1. / PI
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reflect(&self, rgen: &mut impl rand::Rng, normal: Vec3, _incident: Vec3) -> Vec3 {
|
||||||
|
let sphere: Vec3 = UnitSphere.sample(rgen).into();
|
||||||
|
let sphere_n = normal.dot(sphere); // uniform on [-1, 1]!
|
||||||
|
let sphere_t = sphere - sphere_n * normal;
|
||||||
|
|
||||||
|
let out_n_len2 = sphere_n.abs();
|
||||||
|
let out_t = (1. + out_n_len2).recip().sqrt() * sphere_t;
|
||||||
|
let out_n = out_n_len2.sqrt() * normal;
|
||||||
|
out_t + out_n
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
13
ui/CMakeLists.txt
Normal file
13
ui/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
include(impl.cmake)
|
||||||
|
|
||||||
|
qt_add_executable(photon_light
|
||||||
|
src/api.cxx
|
||||||
|
src/main.cxx
|
||||||
|
src/main_window.cxx
|
||||||
|
src/main_window.ui
|
||||||
|
src/viewport.cxx
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(photon_light PRIVATE Qt6::Gui Qt6::Widgets)
|
||||||
|
target_link_libraries(photon_light PRIVATE photon_light_impl)
|
||||||
|
target_include_directories(photon_light PRIVATE src)
|
||||||
15
ui/Cargo.toml
Normal file
15
ui/Cargo.toml
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
[package]
|
||||||
|
name = "photon-light-impl"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
crate-type = ["staticlib"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
photon-light = {path = "../"}
|
||||||
|
|
||||||
|
glam = { version = "0.30" }
|
||||||
|
pollster = "0.4.0"
|
||||||
|
raw-window-handle = "0.6.2"
|
||||||
|
wgpu = "27.0.1"
|
||||||
22
ui/impl.cmake
Normal file
22
ui/impl.cmake
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
set(impl_basename "${CARGO_TARGET_DIR}/release/libphoton_light_impl")
|
||||||
|
|
||||||
|
add_custom_command(
|
||||||
|
OUTPUT ${impl_basename}.a
|
||||||
|
COMMAND env CARGO_TARGET_DIR=${CARGO_TARGET_DIR} ${CARGO} build --release --package photon-light-impl
|
||||||
|
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
||||||
|
DEPFILE ${impl_basename}.d
|
||||||
|
USES_TERMINAL
|
||||||
|
JOB_SERVER_AWARE
|
||||||
|
DEPENDS_EXPLICIT_ONLY
|
||||||
|
)
|
||||||
|
|
||||||
|
# HACK ensure CMake *actually adds* the command above
|
||||||
|
add_custom_target(build_impl
|
||||||
|
DEPENDS ${impl_basename}.a
|
||||||
|
)
|
||||||
|
|
||||||
|
add_library(photon_light_impl STATIC IMPORTED)
|
||||||
|
|
||||||
|
set_target_properties(photon_light_impl PROPERTIES
|
||||||
|
IMPORTED_LOCATION ${impl_basename}.a
|
||||||
|
)
|
||||||
57
ui/src/api.cxx
Normal file
57
ui/src/api.cxx
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
#include "api.hxx"
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace ffi {
|
||||||
|
extern "C" Core* rt4_viewport_create(xcb_connection_t* connection, std::uint32_t window, std::uint32_t width, std::uint32_t height);
|
||||||
|
extern "C" void rt4_viewport_destroy(Core* viewport);
|
||||||
|
extern "C" void rt4_viewport_configure(Core* viewport, std::uint32_t width, std::uint32_t height);
|
||||||
|
extern "C" void rt4_viewport_redraw(Core* viewport, const RedrawArgs* args);
|
||||||
|
} // namespace ffi
|
||||||
|
|
||||||
|
BoxCore::BoxCore(BoxCore&& b)
|
||||||
|
: ptr(std::exchange(b.ptr, {})) {
|
||||||
|
}
|
||||||
|
|
||||||
|
BoxCore::~BoxCore() {
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
BoxCore& BoxCore::operator=(BoxCore&& b) {
|
||||||
|
if (&b == this)
|
||||||
|
return *this;
|
||||||
|
std::swap(ptr, b.ptr);
|
||||||
|
b.reset();
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
BoxCore BoxCore::from_xcb(xcb_connection_t* connection, std::uint32_t window, std::uint32_t width, std::uint32_t height) {
|
||||||
|
if (!connection)
|
||||||
|
throw std::logic_error("attempt to use a null connection");
|
||||||
|
if (!window)
|
||||||
|
throw std::logic_error("attempt to use a null window");
|
||||||
|
BoxCore out;
|
||||||
|
out.ptr.ptr = ffi::rt4_viewport_create(connection, window, width, height);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BoxCore::reset() {
|
||||||
|
auto viewport = std::exchange(ptr, {});
|
||||||
|
if (viewport)
|
||||||
|
ffi::rt4_viewport_destroy(viewport.use());
|
||||||
|
}
|
||||||
|
|
||||||
|
ffi::Core* MutCore::use() const {
|
||||||
|
if (!ptr)
|
||||||
|
throw std::logic_error("attempt to use a null Core");
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MutCore::configure(std::uint32_t width, std::uint32_t height) const {
|
||||||
|
rt4_viewport_configure(use(), width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MutCore::redraw(const RedrawArgs& args) const {
|
||||||
|
rt4_viewport_redraw(use(), &args);
|
||||||
|
}
|
||||||
76
ui/src/api.hxx
Normal file
76
ui/src/api.hxx
Normal file
|
|
@ -0,0 +1,76 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
struct xcb_connection_t;
|
||||||
|
|
||||||
|
namespace ffi {
|
||||||
|
struct Core;
|
||||||
|
|
||||||
|
struct SphericalPosition {
|
||||||
|
float yaw = 0.;
|
||||||
|
float pitch = 0.;
|
||||||
|
float distance = 1.;
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class UseNormal: std::uint8_t {
|
||||||
|
Light,
|
||||||
|
Camera,
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RedrawArgs {
|
||||||
|
SphericalPosition camera_position;
|
||||||
|
SphericalPosition light_position;
|
||||||
|
float light_radius = 1.;
|
||||||
|
float light_spread = 0.;
|
||||||
|
float accum_sigma = 1.;
|
||||||
|
float accum_scale = 1.;
|
||||||
|
std::uint8_t reflections = 0;
|
||||||
|
UseNormal use_normal = UseNormal::Light;
|
||||||
|
bool show_axes = true;
|
||||||
|
bool show_shapes = true;
|
||||||
|
bool show_hit_emission = true;
|
||||||
|
bool show_miss_emission = true;
|
||||||
|
bool show_direct_hit = true;
|
||||||
|
bool show_indirect_hit = true;
|
||||||
|
bool show_light = true;
|
||||||
|
};
|
||||||
|
} // namespace ffi
|
||||||
|
|
||||||
|
using ffi::UseNormal;
|
||||||
|
using ffi::RedrawArgs;
|
||||||
|
using ffi::SphericalPosition;
|
||||||
|
|
||||||
|
class MutCore {
|
||||||
|
friend class BoxCore;
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit operator bool() const { return ptr; }
|
||||||
|
|
||||||
|
void configure(std::uint32_t width, std::uint32_t height) const;
|
||||||
|
void redraw(const RedrawArgs& args) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
ffi::Core* ptr = nullptr;
|
||||||
|
ffi::Core* use() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
class BoxCore {
|
||||||
|
public:
|
||||||
|
BoxCore() = default;
|
||||||
|
BoxCore(const BoxCore&) = delete;
|
||||||
|
BoxCore(BoxCore&&);
|
||||||
|
BoxCore& operator=(const BoxCore&) = delete;
|
||||||
|
BoxCore& operator=(BoxCore&&);
|
||||||
|
~BoxCore();
|
||||||
|
|
||||||
|
explicit operator bool() const { return !!ptr; }
|
||||||
|
const MutCore* operator->() const { return &ptr; }
|
||||||
|
|
||||||
|
void reset();
|
||||||
|
|
||||||
|
static BoxCore from_xcb(xcb_connection_t* connection, std::uint32_t window, std::uint32_t width, std::uint32_t height);
|
||||||
|
|
||||||
|
private:
|
||||||
|
MutCore ptr;
|
||||||
|
};
|
||||||
48
ui/src/lib.rs
Normal file
48
ui/src/lib.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
use std::{ffi::c_void, num::NonZero, ptr::NonNull};
|
||||||
|
|
||||||
|
use glam::{UVec2, uvec2};
|
||||||
|
use photon_light::{Core, RedrawArgs, init_gpu_inner};
|
||||||
|
use raw_window_handle::{RawDisplayHandle, RawWindowHandle, XcbDisplayHandle, XcbWindowHandle};
|
||||||
|
|
||||||
|
unsafe fn create_viewport(
|
||||||
|
display: impl Into<RawDisplayHandle>,
|
||||||
|
window: impl Into<RawWindowHandle>,
|
||||||
|
size: UVec2,
|
||||||
|
) -> Box<Core> {
|
||||||
|
let target = wgpu::SurfaceTargetUnsafe::RawHandle {
|
||||||
|
raw_display_handle: display.into(),
|
||||||
|
raw_window_handle: window.into(),
|
||||||
|
};
|
||||||
|
let gpu = pollster::block_on(init_gpu_inner(|instance| unsafe {
|
||||||
|
instance.create_surface_unsafe(target)
|
||||||
|
}))
|
||||||
|
.unwrap();
|
||||||
|
Box::new(Core::new(gpu, size))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[unsafe(no_mangle)]
|
||||||
|
unsafe extern "C" fn rt4_viewport_create(
|
||||||
|
connection: NonNull<c_void>,
|
||||||
|
window: NonZero<u32>,
|
||||||
|
width: u32,
|
||||||
|
height: u32,
|
||||||
|
) -> Box<Core> {
|
||||||
|
let display = XcbDisplayHandle::new(Some(connection), 0);
|
||||||
|
let window = XcbWindowHandle::new(window);
|
||||||
|
unsafe { create_viewport(display, window, uvec2(width, height)) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[unsafe(no_mangle)]
|
||||||
|
unsafe extern "C" fn rt4_viewport_destroy(viewport: Box<Core>) {
|
||||||
|
drop(viewport);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[unsafe(no_mangle)]
|
||||||
|
unsafe extern "C" fn rt4_viewport_configure(viewport: &mut Core, width: u32, height: u32) {
|
||||||
|
viewport.configure(uvec2(width, height));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[unsafe(no_mangle)]
|
||||||
|
unsafe extern "C" fn rt4_viewport_redraw(viewport: &mut Core, args: &RedrawArgs) {
|
||||||
|
viewport.redraw(args);
|
||||||
|
}
|
||||||
12
ui/src/main.cxx
Normal file
12
ui/src/main.cxx
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
#include "main_window.hxx"
|
||||||
|
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
QApplication app(argc, argv);
|
||||||
|
|
||||||
|
auto w = new PhotonLight;
|
||||||
|
w->show();
|
||||||
|
|
||||||
|
return app.exec();
|
||||||
|
}
|
||||||
65
ui/src/main_window.cxx
Normal file
65
ui/src/main_window.cxx
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
#include "main_window.hxx"
|
||||||
|
|
||||||
|
#include "ui_main_window.h"
|
||||||
|
|
||||||
|
PhotonLight::PhotonLight(QWidget* parent)
|
||||||
|
: QMainWindow(parent),
|
||||||
|
m_ui(new Ui::MainWindow) {
|
||||||
|
m_ui->setupUi(this);
|
||||||
|
updateView();
|
||||||
|
}
|
||||||
|
|
||||||
|
PhotonLight::~PhotonLight() = default;
|
||||||
|
|
||||||
|
float deg_to_rad(float val) {
|
||||||
|
return val * float(M_PI / 180);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhotonLight::updateView() {
|
||||||
|
UseNormal use_normal = {};
|
||||||
|
if (m_ui->normalFromLight->isChecked())
|
||||||
|
use_normal = UseNormal::Light;
|
||||||
|
else if (m_ui->normalFromCamera->isChecked())
|
||||||
|
use_normal = UseNormal::Camera;
|
||||||
|
RedrawArgs args{
|
||||||
|
.camera_position = SphericalPosition{
|
||||||
|
.yaw = deg_to_rad(m_ui->cameraYaw->value()),
|
||||||
|
.pitch = deg_to_rad(m_ui->cameraPitch->value()),
|
||||||
|
.distance = m_ui->cameraDistance->value() / 10.0f,
|
||||||
|
},
|
||||||
|
.light_position = SphericalPosition{
|
||||||
|
.yaw = deg_to_rad(m_ui->lightYaw->value()),
|
||||||
|
.pitch = deg_to_rad(m_ui->lightPitch->value()),
|
||||||
|
.distance = m_ui->lightDistance->value() / 10.0f,
|
||||||
|
},
|
||||||
|
.light_radius = 0.125,
|
||||||
|
.light_spread = 0.125,
|
||||||
|
.accum_sigma = exp10f(m_ui->accumSigma->value() / 25.0),
|
||||||
|
.accum_scale = exp10f(m_ui->accumScale->value() / 25.0),
|
||||||
|
.reflections = std::uint8_t(m_ui->reflections->value()),
|
||||||
|
.use_normal = use_normal,
|
||||||
|
.show_axes = m_ui->displayAxes->isChecked(),
|
||||||
|
.show_shapes = m_ui->displayShapes->isChecked(),
|
||||||
|
.show_hit_emission = m_ui->displayEmitted->isChecked(),
|
||||||
|
.show_miss_emission = m_ui->displayEmitted->isChecked(),
|
||||||
|
.show_direct_hit = m_ui->displayDirectHits->isChecked(),
|
||||||
|
.show_indirect_hit = m_ui->displayIndirectHits->isChecked(),
|
||||||
|
.show_light = m_ui->displayResult->isChecked(),
|
||||||
|
};
|
||||||
|
m_ui->cameraYawLabel->setText(tr("Yaw: %1 deg").arg(QString::number(qRadiansToDegrees(args.camera_position.yaw))));
|
||||||
|
m_ui->cameraPitchLabel->setText(tr("Pitch: %1 deg").arg(QString::number(qRadiansToDegrees(args.camera_position.pitch))));
|
||||||
|
m_ui->cameraDistanceLabel->setText(tr("Distance: %1").arg(QString::number(args.camera_position.distance)));
|
||||||
|
m_ui->lightYawLabel->setText(tr("Yaw: %1 deg").arg(QString::number(qRadiansToDegrees(args.light_position.yaw))));
|
||||||
|
m_ui->lightPitchLabel->setText(tr("Pitch: %1 deg").arg(QString::number(qRadiansToDegrees(args.light_position.pitch))));
|
||||||
|
m_ui->lightDistanceLabel->setText(tr("Distance: %1").arg(QString::number(args.light_position.distance)));
|
||||||
|
m_ui->accumSigmaLabel->setText(tr("Averaging radius: %1").arg(QString::number(args.accum_sigma, 'f', 5)));
|
||||||
|
m_ui->accumScaleLabel->setText(tr("Brightness: %1").arg(QString::number(args.accum_scale, 'f', 5)));
|
||||||
|
m_ui->viewport->setView(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhotonLight::updateViewIf(bool update) {
|
||||||
|
if (update)
|
||||||
|
updateView();
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "moc_main_window.cpp"
|
||||||
23
ui/src/main_window.hxx
Normal file
23
ui/src/main_window.hxx
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
#pragma once
|
||||||
|
#include <QMainWindow>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace Ui {
|
||||||
|
class MainWindow;
|
||||||
|
}
|
||||||
|
|
||||||
|
class PhotonLight : public QMainWindow {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit PhotonLight(QWidget* parent = nullptr);
|
||||||
|
~PhotonLight() override;
|
||||||
|
|
||||||
|
public slots:
|
||||||
|
void updateView();
|
||||||
|
void updateViewIf(bool update); // for radio buttons
|
||||||
|
|
||||||
|
private:
|
||||||
|
const std::unique_ptr<Ui::MainWindow> m_ui;
|
||||||
|
};
|
||||||
701
ui/src/main_window.ui
Normal file
701
ui/src/main_window.ui
Normal file
|
|
@ -0,0 +1,701 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ui version="4.0">
|
||||||
|
<class>MainWindow</class>
|
||||||
|
<widget class="QMainWindow" name="MainWindow">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>1600</width>
|
||||||
|
<height>1200</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>MainWindow</string>
|
||||||
|
</property>
|
||||||
|
<widget class="QWidget" name="centralwidget">
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||||
|
<item>
|
||||||
|
<widget class="Viewport" name="viewport" native="true"/>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
<widget class="QMenuBar" name="menubar">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>1600</width>
|
||||||
|
<height>38</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<widget class="QStatusBar" name="statusbar"/>
|
||||||
|
<widget class="QDockWidget" name="dockWidget">
|
||||||
|
<property name="features">
|
||||||
|
<set>QDockWidget::DockWidgetFloatable|QDockWidget::DockWidgetMovable</set>
|
||||||
|
</property>
|
||||||
|
<attribute name="dockWidgetArea">
|
||||||
|
<number>2</number>
|
||||||
|
</attribute>
|
||||||
|
<widget class="QWidget" name="dockWidgetContents">
|
||||||
|
<layout class="QVBoxLayout" name="verticalLayout_2">
|
||||||
|
<item>
|
||||||
|
<widget class="QGroupBox" name="groupBox">
|
||||||
|
<property name="title">
|
||||||
|
<string>Camera</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QVBoxLayout" name="verticalLayout">
|
||||||
|
<item>
|
||||||
|
<widget class="QLabel" name="cameraYawLabel">
|
||||||
|
<property name="text">
|
||||||
|
<string>Yaw</string>
|
||||||
|
</property>
|
||||||
|
<property name="buddy">
|
||||||
|
<cstring>cameraYaw</cstring>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QSlider" name="cameraYaw">
|
||||||
|
<property name="minimum">
|
||||||
|
<number>-180</number>
|
||||||
|
</property>
|
||||||
|
<property name="maximum">
|
||||||
|
<number>180</number>
|
||||||
|
</property>
|
||||||
|
<property name="pageStep">
|
||||||
|
<number>15</number>
|
||||||
|
</property>
|
||||||
|
<property name="value">
|
||||||
|
<number>45</number>
|
||||||
|
</property>
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QLabel" name="cameraPitchLabel">
|
||||||
|
<property name="text">
|
||||||
|
<string>Pitch</string>
|
||||||
|
</property>
|
||||||
|
<property name="buddy">
|
||||||
|
<cstring>cameraPitch</cstring>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QSlider" name="cameraPitch">
|
||||||
|
<property name="minimum">
|
||||||
|
<number>-90</number>
|
||||||
|
</property>
|
||||||
|
<property name="maximum">
|
||||||
|
<number>90</number>
|
||||||
|
</property>
|
||||||
|
<property name="pageStep">
|
||||||
|
<number>15</number>
|
||||||
|
</property>
|
||||||
|
<property name="value">
|
||||||
|
<number>35</number>
|
||||||
|
</property>
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QLabel" name="cameraDistanceLabel">
|
||||||
|
<property name="text">
|
||||||
|
<string>Distance</string>
|
||||||
|
</property>
|
||||||
|
<property name="buddy">
|
||||||
|
<cstring>cameraDistance</cstring>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QSlider" name="cameraDistance">
|
||||||
|
<property name="maximum">
|
||||||
|
<number>50</number>
|
||||||
|
</property>
|
||||||
|
<property name="value">
|
||||||
|
<number>30</number>
|
||||||
|
</property>
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QGroupBox" name="groupBox_2">
|
||||||
|
<property name="title">
|
||||||
|
<string>Light</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QVBoxLayout" name="verticalLayout_3">
|
||||||
|
<item>
|
||||||
|
<widget class="QLabel" name="lightYawLabel">
|
||||||
|
<property name="text">
|
||||||
|
<string>Yaw</string>
|
||||||
|
</property>
|
||||||
|
<property name="buddy">
|
||||||
|
<cstring>lightYaw</cstring>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QSlider" name="lightYaw">
|
||||||
|
<property name="minimum">
|
||||||
|
<number>-180</number>
|
||||||
|
</property>
|
||||||
|
<property name="maximum">
|
||||||
|
<number>180</number>
|
||||||
|
</property>
|
||||||
|
<property name="pageStep">
|
||||||
|
<number>15</number>
|
||||||
|
</property>
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QLabel" name="lightPitchLabel">
|
||||||
|
<property name="text">
|
||||||
|
<string>Pitch</string>
|
||||||
|
</property>
|
||||||
|
<property name="buddy">
|
||||||
|
<cstring>lightPitch</cstring>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QSlider" name="lightPitch">
|
||||||
|
<property name="minimum">
|
||||||
|
<number>-90</number>
|
||||||
|
</property>
|
||||||
|
<property name="maximum">
|
||||||
|
<number>90</number>
|
||||||
|
</property>
|
||||||
|
<property name="pageStep">
|
||||||
|
<number>15</number>
|
||||||
|
</property>
|
||||||
|
<property name="value">
|
||||||
|
<number>60</number>
|
||||||
|
</property>
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
<property name="invertedAppearance">
|
||||||
|
<bool>false</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QLabel" name="lightDistanceLabel">
|
||||||
|
<property name="text">
|
||||||
|
<string>Distance</string>
|
||||||
|
</property>
|
||||||
|
<property name="buddy">
|
||||||
|
<cstring>lightDistance</cstring>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QSlider" name="lightDistance">
|
||||||
|
<property name="maximum">
|
||||||
|
<number>50</number>
|
||||||
|
</property>
|
||||||
|
<property name="value">
|
||||||
|
<number>10</number>
|
||||||
|
</property>
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QGroupBox" name="groupBox_3">
|
||||||
|
<property name="title">
|
||||||
|
<string>Lighting</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QVBoxLayout" name="verticalLayout_4">
|
||||||
|
<item>
|
||||||
|
<widget class="QLabel" name="accumSigmaLabel">
|
||||||
|
<property name="text">
|
||||||
|
<string>Averaging radius</string>
|
||||||
|
</property>
|
||||||
|
<property name="buddy">
|
||||||
|
<cstring>accumSigma</cstring>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QSlider" name="accumSigma">
|
||||||
|
<property name="minimum">
|
||||||
|
<number>-100</number>
|
||||||
|
</property>
|
||||||
|
<property name="maximum">
|
||||||
|
<number>0</number>
|
||||||
|
</property>
|
||||||
|
<property name="value">
|
||||||
|
<number>-45</number>
|
||||||
|
</property>
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QLabel" name="accumScaleLabel">
|
||||||
|
<property name="text">
|
||||||
|
<string>Brightness</string>
|
||||||
|
</property>
|
||||||
|
<property name="buddy">
|
||||||
|
<cstring>accumScale</cstring>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QSlider" name="accumScale">
|
||||||
|
<property name="minimum">
|
||||||
|
<number>-100</number>
|
||||||
|
</property>
|
||||||
|
<property name="maximum">
|
||||||
|
<number>0</number>
|
||||||
|
</property>
|
||||||
|
<property name="value">
|
||||||
|
<number>-85</number>
|
||||||
|
</property>
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QLabel" name="label">
|
||||||
|
<property name="text">
|
||||||
|
<string>Reflections</string>
|
||||||
|
</property>
|
||||||
|
<property name="buddy">
|
||||||
|
<cstring>reflections</cstring>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QSpinBox" name="reflections">
|
||||||
|
<property name="maximum">
|
||||||
|
<number>20</number>
|
||||||
|
</property>
|
||||||
|
<property name="value">
|
||||||
|
<number>2</number>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QLabel" name="label_2">
|
||||||
|
<property name="text">
|
||||||
|
<string>Use normal at</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QRadioButton" name="normalFromLight">
|
||||||
|
<property name="text">
|
||||||
|
<string>Hit position</string>
|
||||||
|
</property>
|
||||||
|
<property name="checked">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QRadioButton" name="normalFromCamera">
|
||||||
|
<property name="text">
|
||||||
|
<string>Show position</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QGroupBox" name="groupBox_4">
|
||||||
|
<property name="title">
|
||||||
|
<string>Show</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QVBoxLayout" name="verticalLayout_5">
|
||||||
|
<item>
|
||||||
|
<widget class="QCheckBox" name="displayAxes">
|
||||||
|
<property name="text">
|
||||||
|
<string>Axes</string>
|
||||||
|
</property>
|
||||||
|
<property name="checked">
|
||||||
|
<bool>false</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QCheckBox" name="displayShapes">
|
||||||
|
<property name="text">
|
||||||
|
<string>Shapes</string>
|
||||||
|
</property>
|
||||||
|
<property name="checked">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QCheckBox" name="displayEmitted">
|
||||||
|
<property name="text">
|
||||||
|
<string>Emitted rays</string>
|
||||||
|
</property>
|
||||||
|
<property name="checked">
|
||||||
|
<bool>false</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QCheckBox" name="displayDirectHits">
|
||||||
|
<property name="text">
|
||||||
|
<string>Direct incident rays</string>
|
||||||
|
</property>
|
||||||
|
<property name="checked">
|
||||||
|
<bool>false</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QCheckBox" name="displayIndirectHits">
|
||||||
|
<property name="text">
|
||||||
|
<string>Indirect incident rays</string>
|
||||||
|
</property>
|
||||||
|
<property name="checked">
|
||||||
|
<bool>false</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QCheckBox" name="displayResult">
|
||||||
|
<property name="text">
|
||||||
|
<string>Average light</string>
|
||||||
|
</property>
|
||||||
|
<property name="checked">
|
||||||
|
<bool>false</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<spacer name="verticalSpacer">
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Vertical</enum>
|
||||||
|
</property>
|
||||||
|
<property name="sizeHint" stdset="0">
|
||||||
|
<size>
|
||||||
|
<width>20</width>
|
||||||
|
<height>385</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
</spacer>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</widget>
|
||||||
|
</widget>
|
||||||
|
<customwidgets>
|
||||||
|
<customwidget>
|
||||||
|
<class>Viewport</class>
|
||||||
|
<extends>QWidget</extends>
|
||||||
|
<header>viewport.hxx</header>
|
||||||
|
<container>1</container>
|
||||||
|
</customwidget>
|
||||||
|
</customwidgets>
|
||||||
|
<resources/>
|
||||||
|
<connections>
|
||||||
|
<connection>
|
||||||
|
<sender>cameraYaw</sender>
|
||||||
|
<signal>valueChanged(int)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateView()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1585</x>
|
||||||
|
<y>169</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>403</x>
|
||||||
|
<y>362</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>cameraPitch</sender>
|
||||||
|
<signal>valueChanged(int)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateView()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1585</x>
|
||||||
|
<y>225</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>403</x>
|
||||||
|
<y>362</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>lightYaw</sender>
|
||||||
|
<signal>valueChanged(int)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateView()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1585</x>
|
||||||
|
<y>385</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>403</x>
|
||||||
|
<y>362</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>lightPitch</sender>
|
||||||
|
<signal>valueChanged(int)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateView()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1585</x>
|
||||||
|
<y>441</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>403</x>
|
||||||
|
<y>362</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>accumSigma</sender>
|
||||||
|
<signal>valueChanged(int)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateView()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1585</x>
|
||||||
|
<y>601</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>399</x>
|
||||||
|
<y>299</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>accumScale</sender>
|
||||||
|
<signal>valueChanged(int)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateView()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1585</x>
|
||||||
|
<y>657</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>399</x>
|
||||||
|
<y>299</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>cameraDistance</sender>
|
||||||
|
<signal>valueChanged(int)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateView()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1489</x>
|
||||||
|
<y>271</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>799</x>
|
||||||
|
<y>599</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>lightDistance</sender>
|
||||||
|
<signal>valueChanged(int)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateView()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1489</x>
|
||||||
|
<y>487</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>799</x>
|
||||||
|
<y>599</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>displayAxes</sender>
|
||||||
|
<signal>stateChanged(int)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateView()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1489</x>
|
||||||
|
<y>799</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>799</x>
|
||||||
|
<y>599</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>displayDirectHits</sender>
|
||||||
|
<signal>stateChanged(int)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateView()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1489</x>
|
||||||
|
<y>901</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>799</x>
|
||||||
|
<y>599</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>displayEmitted</sender>
|
||||||
|
<signal>stateChanged(int)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateView()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1489</x>
|
||||||
|
<y>867</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>799</x>
|
||||||
|
<y>599</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>displayIndirectHits</sender>
|
||||||
|
<signal>stateChanged(int)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateView()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1489</x>
|
||||||
|
<y>935</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>799</x>
|
||||||
|
<y>599</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>displayResult</sender>
|
||||||
|
<signal>stateChanged(int)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateView()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1489</x>
|
||||||
|
<y>969</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>799</x>
|
||||||
|
<y>599</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>displayShapes</sender>
|
||||||
|
<signal>stateChanged(int)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateView()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1489</x>
|
||||||
|
<y>833</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>799</x>
|
||||||
|
<y>599</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>reflections</sender>
|
||||||
|
<signal>valueChanged(int)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateView()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1489</x>
|
||||||
|
<y>712</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>799</x>
|
||||||
|
<y>599</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>normalFromLight</sender>
|
||||||
|
<signal>toggled(bool)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateViewIf(bool)</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1429</x>
|
||||||
|
<y>787</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>1381</x>
|
||||||
|
<y>783</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
<connection>
|
||||||
|
<sender>normalFromCamera</sender>
|
||||||
|
<signal>toggled(bool)</signal>
|
||||||
|
<receiver>MainWindow</receiver>
|
||||||
|
<slot>updateViewIf(bool)</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>1932</x>
|
||||||
|
<y>816</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>1600</x>
|
||||||
|
<y>874</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
</connections>
|
||||||
|
<slots>
|
||||||
|
<slot>updateView()</slot>
|
||||||
|
<slot>updateViewIf(bool)</slot>
|
||||||
|
</slots>
|
||||||
|
</ui>
|
||||||
74
ui/src/viewport.cxx
Normal file
74
ui/src/viewport.cxx
Normal file
|
|
@ -0,0 +1,74 @@
|
||||||
|
#include "viewport.hxx"
|
||||||
|
|
||||||
|
#include <QApplication>
|
||||||
|
#include <QEvent>
|
||||||
|
#include <QGuiApplication>
|
||||||
|
|
||||||
|
Viewport::Viewport(QWidget* parent, Qt::WindowFlags f)
|
||||||
|
: QWidget(parent, f) {
|
||||||
|
setAttribute(Qt::WA_DontCreateNativeAncestors);
|
||||||
|
setAttribute(Qt::WA_NativeWindow);
|
||||||
|
setAttribute(Qt::WA_PaintOnScreen);
|
||||||
|
setAttribute(Qt::WA_NoSystemBackground);
|
||||||
|
}
|
||||||
|
|
||||||
|
Viewport::~Viewport() = default;
|
||||||
|
|
||||||
|
void Viewport::setView(RedrawArgs new_args) {
|
||||||
|
args = new_args;
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
QPaintEngine* Viewport::paintEngine() const {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Viewport::event(QEvent* event) {
|
||||||
|
switch (event->type()) {
|
||||||
|
case QEvent::Type::WinIdChange:
|
||||||
|
recreate();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return QWidget::event(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Viewport::paintEvent(QPaintEvent* event) {
|
||||||
|
if (!core)
|
||||||
|
recreate();
|
||||||
|
core->redraw(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Viewport::resizeEvent(QResizeEvent* event) {
|
||||||
|
if (!core)
|
||||||
|
return;
|
||||||
|
updateSize();
|
||||||
|
QWidget::resizeEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Viewport::recreate() try {
|
||||||
|
auto* app = qobject_cast<QGuiApplication*>(QApplication::instance());
|
||||||
|
if (!app)
|
||||||
|
throw std::runtime_error("not a GUI application (WTF?)");
|
||||||
|
|
||||||
|
auto* native = app->nativeInterface<QNativeInterface::QX11Application>();
|
||||||
|
if (!native)
|
||||||
|
throw std::runtime_error("X11 interface is not available");
|
||||||
|
|
||||||
|
auto* xcb_connection = native->connection();
|
||||||
|
std::uint32_t x11_window = winId();
|
||||||
|
|
||||||
|
fprintf(stderr, "connection %p, window %#08x\n", xcb_connection, x11_window);
|
||||||
|
|
||||||
|
const QSize device_size = size() * devicePixelRatio();
|
||||||
|
core.reset();
|
||||||
|
core = BoxCore::from_xcb(xcb_connection, x11_window, device_size.width(), device_size.height());
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
fprintf(stderr, "failed to recreate the viewport: %s", e.what());
|
||||||
|
}
|
||||||
|
|
||||||
|
void Viewport::updateSize() {
|
||||||
|
const QSize device_size = size() * devicePixelRatio();
|
||||||
|
core->configure(device_size.width(), device_size.height());
|
||||||
|
}
|
||||||
28
ui/src/viewport.hxx
Normal file
28
ui/src/viewport.hxx
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QWidget>
|
||||||
|
|
||||||
|
#include "api.hxx"
|
||||||
|
|
||||||
|
class Viewport : public QWidget {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit Viewport(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags());
|
||||||
|
~Viewport() override;
|
||||||
|
QPaintEngine* paintEngine() const override;
|
||||||
|
|
||||||
|
void setView(RedrawArgs new_args);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool event(QEvent* event) override;
|
||||||
|
void paintEvent(QPaintEvent* event) override;
|
||||||
|
void resizeEvent(QResizeEvent* event) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
BoxCore core;
|
||||||
|
RedrawArgs args = {};
|
||||||
|
|
||||||
|
void recreate();
|
||||||
|
void updateSize();
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user