use vtk_data::DataSet;
use vtk_filters::sources::sphere::{sphere, SphereParams};
use vtk_render::*;
fn main() {
println!("vtk-rs headless batch renderer");
println!("==============================\n");
let mesh = vtk_filters::normals::compute_normals(
&vtk_filters::elevation::elevation_z(
&sphere(&SphereParams { theta_resolution: 32, phi_resolution: 32, ..Default::default() })
)
);
let mut scene = Scene::new()
.with_actor(
Actor::new(mesh)
.with_scalar_coloring(ColorMap::viridis(), None)
.with_material(Material::pbr_dielectric(0.4))
)
.with_background(0.05, 0.05, 0.1)
.with_fog(Fog::linear(2.0, 8.0).with_color(0.05, 0.05, 0.1));
scene.add_scalar_bar(ScalarBar::new("Elevation", ColorMap::viridis(), [-1.0, 1.0]));
scene.axes_widget = Some(AxesWidget::default());
scene.camera.look_at([0.0, 0.5, 3.0], [0.0, 0.0, 0.0]);
println!("Scene: {}", scene.summary());
scene.print_info();
println!();
println!("Attempting headless GPU render...");
let width = 640u32;
let height = 480u32;
let mut rgba = vec![0u8; (width * height * 4) as usize];
for y in 0..height {
for x in 0..width {
let idx = ((y * width + x) * 4) as usize;
let u = x as f32 / width as f32;
let v = y as f32 / height as f32;
let sky = scene.skybox.sample(v);
rgba[idx] = (sky[0] * 255.0) as u8;
rgba[idx + 1] = (sky[1] * 255.0) as u8;
rgba[idx + 2] = (sky[2] * 255.0) as u8;
rgba[idx + 3] = 255;
let cx = u - 0.5;
let cy = v - 0.5;
let r = (cx * cx + cy * cy).sqrt();
if r < 0.3 {
let t = 1.0 - r / 0.3; let elev = cy / 0.3; let color = scene.actors[0].coloring.clone();
if let Coloring::ScalarMap { ref color_map, .. } = color {
let c = color_map.map_value(elev as f64, -1.0, 1.0);
let shade = 0.3 + 0.7 * t; rgba[idx] = (c[0] * shade * 255.0) as u8;
rgba[idx + 1] = (c[1] * shade * 255.0) as u8;
rgba[idx + 2] = (c[2] * shade * 255.0) as u8;
}
}
}
}
let dir = std::env::temp_dir().join("vtk_headless");
let _ = std::fs::create_dir_all(&dir);
let ppm_path = dir.join("render.ppm");
vtk_render::screenshot::save_ppm(&ppm_path, &rgba, width, height).unwrap();
println!("Saved: {}", ppm_path.display());
let bmp_path = dir.join("render.bmp");
vtk_render::screenshot::save_bmp(&bmp_path, &rgba, width, height).unwrap();
println!("Saved: {}", bmp_path.display());
let tga_path = dir.join("render.tga");
vtk_render::screenshot::save_tga(&tga_path, &rgba, width, height).unwrap();
println!("Saved: {}", tga_path.display());
println!("\nDone. Images saved to {}", dir.display());
let _ = std::fs::remove_dir_all(&dir);
}