Compare commits

...

2 Commits

Author SHA1 Message Date
68b4041c88 fix normalization 2025-11-25 12:59:28 +03:00
98f3c44ab3 add super-duper complicated sphere mesh generator 2025-11-25 12:40:26 +03:00
4 changed files with 214 additions and 6 deletions

View File

@ -328,7 +328,7 @@ impl Core {
} }
let light_at = { let light_at = {
let sigma2 = args.accum_sigma.powi(2); let sigma2 = args.accum_sigma.powi(2);
let accum_normalizator = (2. * PI * sigma2).sqrt().recip(); let accum_normalizator = (2. * PI * sigma2).recip();
let hits = &hits; let hits = &hits;
move |hit: Hit| -> f32 { move |hit: Hit| -> f32 {
let mut total_cd = 0.0f32; let mut total_cd = 0.0f32;
@ -384,7 +384,7 @@ impl Core {
if args.show_shapes { if args.show_shapes {
let mut meshes = Vec::new(); let mut meshes = Vec::new();
for obj in &scene.objects { for obj in &scene.objects {
let mesh = shape::sphere((obj.radius * 45.) as usize + 7); let mesh = shape::octo_sphere((obj.radius * 45.) as usize + 7);
let obj_mesh = render::faces::Mesh::new( let obj_mesh = render::faces::Mesh::new(
&self.device, &self.device,
&mesh &mesh

View File

@ -60,6 +60,114 @@ pub fn sphere(subdiv: usize) -> Mesh {
Mesh { vertices, indices } 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)] #[cfg(test)]
mod tests { mod tests {
use approx::abs_diff_eq; use approx::abs_diff_eq;
@ -137,4 +245,104 @@ mod tests {
] ]
); );
} }
#[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],
]
);
}
} }

View File

@ -52,8 +52,8 @@ void PhotonLight::updateView() {
m_ui->lightYawLabel->setText(tr("Yaw: %1 deg").arg(QString::number(qRadiansToDegrees(args.light_position.yaw)))); 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->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->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', 3))); 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', 3))); m_ui->accumScaleLabel->setText(tr("Brightness: %1").arg(QString::number(args.accum_scale, 'f', 5)));
m_ui->viewport->setView(args); m_ui->viewport->setView(args);
} }

View File

@ -245,7 +245,7 @@
<number>0</number> <number>0</number>
</property> </property>
<property name="value"> <property name="value">
<number>-40</number> <number>-45</number>
</property> </property>
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
@ -271,7 +271,7 @@
<number>0</number> <number>0</number>
</property> </property>
<property name="value"> <property name="value">
<number>-50</number> <number>-85</number>
</property> </property>
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>