pub struct RGBSpectrum {
pub c: [Float; 3],
}
Fields§
§c: [Float; 3]
Implementations§
Source§impl RGBSpectrum
impl RGBSpectrum
Sourcepub fn new(v: Float) -> Self
pub fn new(v: Float) -> Self
Examples found in repository?
examples/parse_ass_file.rs (line 267)
244fn main() -> std::io::Result<()> {
245 let num_cores = num_cpus::get();
246 let git_describe = option_env!("GIT_DESCRIBE").unwrap_or("unknown");
247 println!(
248 "parse_ass_file version {} ({}) [Detected {} cores]",
249 VERSION, git_describe, num_cores
250 );
251 println!();
252 // handle command line options
253 let args = Args::parse();
254 let samples_per_pixel: u16 = args.samples;
255 // default values
256 let mut node_name: String = String::from(""); // no default name
257 let mut filter_name: String = String::from("box");
258 let mut filter_width: Float = 2.0;
259 let mut render_camera: String = String::from(""); // no default name
260 let mut mesh: String = String::from(""); // no default name
261 let mut fov: Float = 90.0; // read persp_camera.fov
262 let mut intensity: Float = 1.0; // read mesh_light.intensity
263 let mut cone_angle: Float = 30.0; // read spot_light.cone_angle
264 let cone_delta_angle: Float = 5.0; // TODO: read from .ass file?
265 let mut radius: Float = 0.5; // read [cylinder, disk, sphere].radius
266 let mut hole: Float = 0.0; // read disk.hole
267 let mut color: Spectrum = Spectrum::new(1.0 as Float);
268 // read standard_surface.base_color
269 let mut base_color: Spectrum = Spectrum::new(0.5 as Float);
270 // read standard_surface.specular_color
271 let mut specular_color: Spectrum = Spectrum::new(1.0 as Float);
272 let mut specular_roughness: Float = 0.01; // read standard_surface.specular_roughness
273 let mut metalness: Float = 0.0; // read standard_surface.metalness
274 let mut animated_cam_to_world: AnimatedTransform = AnimatedTransform::default();
275 let mut xres: i32 = 1280; // read options.xres
276 let mut yres: i32 = 720; // read options.yres
277 let mut max_depth: i32 = 5; // read options.GI_total_depth
278 let mut samples: i32 = 1; // read mesh_light.samples
279 let mut cur_transform: Transform = Transform::default();
280 let mut obj_to_world: Transform = Transform::default();
281 let mut world_to_obj: Transform = Transform::default();
282 let mut nsides: Vec<u32> = Vec::new();
283 let mut shidxs: Vec<u32> = Vec::new();
284 let mut shader_names: Vec<String> = Vec::new();
285 let mut p_ws: Vec<Point3f> = Vec::new();
286 let mut p_ws_len: usize = 0;
287 let mut vi: Vec<u32> = Vec::new();
288 let mut primitives: Vec<Arc<Primitive>> = Vec::new();
289 let mut lights: Vec<Arc<Light>> = Vec::new();
290 let mut named_materials: HashMap<String, Arc<Material>> = HashMap::new();
291 let mut named_primitives: HashMap<String, (Vec<String>, Vec<(u32, Arc<Primitive>)>)> =
292 HashMap::new();
293 // input (.ass) file
294 println!("FILE = {:?}", args.path);
295 let f = File::open(&args.path)?;
296 if args.path.is_relative() {
297 let cp: PathBuf = env::current_dir().unwrap();
298 let pb: PathBuf = cp.join(args.path);
299 let search_directory: &Path = pb.as_path().parent().unwrap();
300 println!("search_directory is {}", search_directory.display());
301 }
302 let mut reader = BufReader::new(f);
303 let mut str_buf: String = String::default();
304 let num_bytes = reader.read_to_string(&mut str_buf);
305 if num_bytes.is_ok() {
306 let n_bytes = num_bytes.unwrap();
307 println!("{} bytes read", n_bytes);
308 }
309 // parser
310 let pairs = AssParser::parse(Rule::ass, &str_buf).unwrap_or_else(|e| panic!("{}", e));
311 // let tokens: Vec<_> = pairs.flatten().tokens().collect();
312 // println!("{} pairs", tokens.len());
313 for pair in pairs {
314 let span = pair.clone().as_span();
315 // println!("Rule: {:?}", pair.as_rule());
316 // println!("Span: {:?}", span);
317 // println!("Text: {}", span.as_str());
318 for inner_pair in pair.into_inner() {
319 match inner_pair.as_rule() {
320 Rule::ident => {
321 let node_type = inner_pair.clone().as_span().as_str();
322 if node_type == "options"
323 || node_type == "standard_surface"
324 || node_type == "spot_light"
325 || node_type == "point_light"
326 {
327 print!("{} {{", node_type);
328 }
329 let stripped = strip_comments(span.as_str());
330 let mut iter = stripped.split_whitespace().peekable();
331 loop {
332 if let Some(next) = iter.next() {
333 if next != String::from("}") {
334 // for all nodes
335 if next == "name" {
336 if let Some(name) = iter.next() {
337 node_name = name.to_string();
338 }
339 if node_type == "standard_surface" {
340 print!(" {} {} ", next, node_name);
341 }
342 } else if next == "matrix" {
343 let mut elems: Vec<Float> = Vec::new();
344 let expected: u32 = 16;
345 for _i in 0..expected {
346 if let Some(elem_str) = iter.next() {
347 let elem: f32 = f32::from_str(elem_str).unwrap();
348 elems.push(elem as Float);
349 }
350 }
351 // print!("\n matrix ... ");
352 // print!("\n {:?}", elems);
353 let m00: Float = elems[0];
354 let m01: Float = elems[1];
355 let m02: Float = elems[2];
356 let m03: Float = elems[3];
357 let m10: Float = elems[4];
358 let m11: Float = elems[5];
359 let m12: Float = elems[6];
360 let m13: Float = elems[7];
361 let m20: Float = elems[8];
362 let m21: Float = elems[9];
363 let m22: Float = elems[10];
364 let m23: Float = elems[11];
365 let m30: Float = elems[12];
366 let m31: Float = elems[13];
367 let m32: Float = elems[14];
368 let m33: Float = elems[15];
369 cur_transform = Transform::new(
370 m00, m10, m20, m30, m01, m11, m21, m31, m02, m12, m22, m32,
371 m03, m13, m23, m33,
372 );
373 // print!("\n {:?}", cur_transform);
374 obj_to_world = Transform {
375 m: cur_transform.m,
376 m_inv: cur_transform.m_inv,
377 };
378 world_to_obj = Transform {
379 m: cur_transform.m_inv,
380 m_inv: cur_transform.m,
381 };
382 if node_type == "persp_camera" && node_name == render_camera {
383 let transform_start_time: Float = 0.0;
384 let transform_end_time: Float = 1.0;
385 let scale: Transform = Transform::scale(
386 1.0 as Float,
387 1.0 as Float,
388 -1.0 as Float,
389 );
390 cur_transform = cur_transform * scale;
391 animated_cam_to_world = AnimatedTransform::new(
392 &cur_transform,
393 transform_start_time,
394 &cur_transform,
395 transform_end_time,
396 );
397 }
398 }
399 // by node type
400 if node_type == "options" {
401 if next == "xres" {
402 if let Some(xres_str) = iter.next() {
403 xres = i32::from_str(xres_str).unwrap();
404 print!("\n xres {} ", xres);
405 }
406 } else if next == "yres" {
407 if let Some(yres_str) = iter.next() {
408 yres = i32::from_str(yres_str).unwrap();
409 print!("\n yres {} ", yres);
410 }
411 } else if next == "camera" {
412 if let Some(camera_str) = iter.next() {
413 // strip surrounding double quotes
414 let v: Vec<&str> = camera_str.split('"').collect();
415 render_camera = v[1].to_string();
416 print!("\n camera {:?} ", render_camera);
417 }
418 } else if next == "GI_total_depth" {
419 if let Some(max_depth_str) = iter.next() {
420 max_depth = i32::from_str(max_depth_str).unwrap();
421 print!("\n GI_total_depth {} ", max_depth);
422 }
423 }
424 } else if node_type == "persp_camera" && node_name == render_camera
425 {
426 // camera_name = String::from("perspective");
427 if next == "fov" {
428 if let Some(fov_str) = iter.next() {
429 fov = f32::from_str(fov_str).unwrap();
430 // print!("\n fov {} ", fov);
431 }
432 }
433 } else if node_type == "gaussian_filter" {
434 filter_name = String::from("gaussian");
435 if next == "width" {
436 if let Some(filter_width_str) = iter.next() {
437 filter_width = f32::from_str(filter_width_str).unwrap();
438 // print!("\n filter_width {} ", filter_width);
439 }
440 }
441 } else if node_type == "mesh_light" {
442 if next == "intensity" {
443 if let Some(intensity_str) = iter.next() {
444 intensity = f32::from_str(intensity_str).unwrap();
445 // print!("\n intensity {} ", intensity);
446 }
447 } else if next == "color" {
448 let mut color_r: Float = 0.0;
449 let mut color_g: Float = 0.0;
450 let mut color_b: Float = 0.0;
451 if let Some(color_str) = iter.next() {
452 color_r = f32::from_str(color_str).unwrap();
453 }
454 if let Some(color_str) = iter.next() {
455 color_g = f32::from_str(color_str).unwrap();
456 }
457 if let Some(color_str) = iter.next() {
458 color_b = f32::from_str(color_str).unwrap();
459 }
460 color = Spectrum::rgb(color_r, color_g, color_b);
461 // print!(
462 // "\n color {} {} {} ",
463 // color_r, color_g, color_b
464 // );
465 } else if next == "samples" {
466 if let Some(samples_str) = iter.next() {
467 samples = i32::from_str(samples_str).unwrap();
468 // print!("\n samples {} ", samples);
469 }
470 } else if next == "mesh" {
471 if let Some(mesh_str) = iter.next() {
472 // strip surrounding double quotes
473 let v: Vec<&str> = mesh_str.split('"').collect();
474 mesh = v[1].to_string();
475 // print!("\n mesh {:?} ", mesh);
476 }
477 }
478 } else if node_type == "point_light" {
479 if next == "intensity" {
480 if let Some(intensity_str) = iter.next() {
481 intensity = f32::from_str(intensity_str).unwrap();
482 print!("\n intensity {} ", intensity);
483 }
484 } else if next == "color" {
485 let mut color_r: Float = 0.0;
486 let mut color_g: Float = 0.0;
487 let mut color_b: Float = 0.0;
488 if let Some(color_str) = iter.next() {
489 color_r = f32::from_str(color_str).unwrap();
490 }
491 if let Some(color_str) = iter.next() {
492 color_g = f32::from_str(color_str).unwrap();
493 }
494 if let Some(color_str) = iter.next() {
495 color_b = f32::from_str(color_str).unwrap();
496 }
497 color = Spectrum::rgb(color_r, color_g, color_b);
498 print!("\n color {} {} {} ", color_r, color_g, color_b);
499 }
500 } else if node_type == "spot_light" {
501 if next == "intensity" {
502 if let Some(intensity_str) = iter.next() {
503 intensity = f32::from_str(intensity_str).unwrap();
504 print!("\n intensity {} ", intensity);
505 }
506 } else if next == "color" {
507 let mut color_r: Float = 0.0;
508 let mut color_g: Float = 0.0;
509 let mut color_b: Float = 0.0;
510 if let Some(color_str) = iter.next() {
511 color_r = f32::from_str(color_str).unwrap();
512 }
513 if let Some(color_str) = iter.next() {
514 color_g = f32::from_str(color_str).unwrap();
515 }
516 if let Some(color_str) = iter.next() {
517 color_b = f32::from_str(color_str).unwrap();
518 }
519 color = Spectrum::rgb(color_r, color_g, color_b);
520 print!("\n color {} {} {} ", color_r, color_g, color_b);
521 } else if next == "cone_angle" {
522 if let Some(cone_angle_str) = iter.next() {
523 cone_angle = f32::from_str(cone_angle_str).unwrap();
524 print!("\n cone_angle {} ", cone_angle);
525 }
526 }
527 } else if node_type == "polymesh" {
528 if next == "vlist" {
529 // parameter_name: vlist
530 // <num_elements>
531 // <num_motionblur_keys>
532 // <data_type>: VECTOR
533 // <elem1> <elem2>
534 // <elem3> <elem4>
535 // ...
536 let num_elements: u32;
537 let num_motionblur_keys: u32;
538 let data_type: String = String::from("VECTOR");
539 let mut elems: Vec<Float> = Vec::new();
540 if let Some(num_elements_str) = iter.next() {
541 num_elements = u32::from_str(num_elements_str).unwrap();
542 if let Some(num_motionblur_keys_str) = iter.next() {
543 num_motionblur_keys =
544 u32::from_str(num_motionblur_keys_str).unwrap();
545 if let Some(data_type_str) = iter.next() {
546 if data_type_str != data_type {
547 panic!("ERROR: {} expected ...", data_type);
548 } else {
549 let expected: u32 =
550 num_elements * num_motionblur_keys * 3;
551 for _i in 0..expected {
552 if let Some(elem_str) = iter.next() {
553 let elem: f32 =
554 f32::from_str(elem_str)
555 .unwrap();
556 elems.push(elem as Float);
557 }
558 }
559 }
560 }
561 }
562 }
563 // print!(
564 // "\n vlist {} {} VECTOR ... ",
565 // num_elements, num_motionblur_keys
566 // );
567 // print!("\n {:?}", elems);
568 // TriangleMesh
569 let mut x: Float = 0.0;
570 let mut y: Float = 0.0;
571 let mut z;
572 let mut p: Vec<Point3f> = Vec::new();
573 for i in 0..elems.len() {
574 if i % 3 == 0 {
575 x = elems[i];
576 } else if i % 3 == 1 {
577 y = elems[i];
578 } else {
579 // i % 3 == 2
580 z = elems[i];
581 // store as Point3f
582 p.push(Point3f { x, y, z });
583 }
584 }
585 // transform mesh vertices to world space
586 p_ws = Vec::new();
587 let n_vertices: usize = p.len();
588 for i in 0..n_vertices {
589 p_ws.push(obj_to_world.transform_point(&p[i]));
590 }
591 p_ws_len = p_ws.len();
592 // print info
593 // println!("");
594 // for point in p {
595 // println!(" {:?}", point);
596 // }
597 } else if next == "nsides" {
598 nsides = Vec::new();
599 loop {
600 let mut is_int: bool = false;
601 // check if next string can be converted to u32
602 if let Some(ref check_for_int_str) = iter.peek() {
603 if u32::from_str(check_for_int_str).is_ok() {
604 is_int = true;
605 } else {
606 // if not ... break the loop
607 break;
608 }
609 }
610 // if we can convert use next()
611 if is_int {
612 if let Some(nside_str) = iter.next() {
613 let nside: u32 =
614 u32::from_str(nside_str).unwrap();
615 nsides.push(nside);
616 }
617 }
618 }
619 let mut followed_by_uint: bool = false;
620 // check if next string is 'UINT' (or not)
621 if let Some(check_for_uint_str) = iter.peek() {
622 if *check_for_uint_str == "UINT" {
623 followed_by_uint = true;
624 }
625 }
626 if followed_by_uint {
627 // skip next (we checked already)
628 iter.next();
629 let num_elements = nsides[0];
630 let num_motionblur_keys = nsides[1];
631 // print!(
632 // "\n nsides {} {} UINT ... ",
633 // num_elements, num_motionblur_keys
634 // );
635 let expected: u32 = num_elements * num_motionblur_keys;
636 nsides = Vec::new();
637 for _i in 0..expected {
638 if let Some(nside_str) = iter.next() {
639 let nside: u32 =
640 u32::from_str(nside_str).unwrap();
641 nsides.push(nside);
642 }
643 }
644 } else {
645 // print!("\n nsides ... ");
646 }
647 // print!("\n {:?} ", nsides);
648 } else if next == "vidxs" {
649 // parameter_name: vidxs
650 // <num_elements>
651 // <num_motionblur_keys>
652 // <data_type>: UINT
653 // <elem1> <elem2>
654 // <elem3> <elem4>
655 // ...
656 let num_elements: u32;
657 let num_motionblur_keys: u32;
658 let data_type: String = String::from("UINT");
659 vi = Vec::new();
660 if let Some(num_elements_str) = iter.next() {
661 num_elements = u32::from_str(num_elements_str).unwrap();
662 if let Some(num_motionblur_keys_str) = iter.next() {
663 num_motionblur_keys =
664 u32::from_str(num_motionblur_keys_str).unwrap();
665 if let Some(data_type_str) = iter.next() {
666 if data_type_str != data_type {
667 panic!("ERROR: {} expected ...", data_type);
668 } else {
669 let expected: u32 =
670 num_elements * num_motionblur_keys;
671 for _i in 0..expected {
672 if let Some(elem_str) = iter.next() {
673 let elem: u32 =
674 u32::from_str(elem_str)
675 .unwrap();
676 vi.push(elem);
677 }
678 }
679 }
680 }
681 }
682 }
683 // print!(
684 // "\n vidxs {} {} UINT ... ",
685 // num_elements, num_motionblur_keys
686 // );
687 // print!("\n {:?} ", vi);
688 } else if next == "shidxs" {
689 shidxs = Vec::new();
690 loop {
691 let mut is_int: bool = false;
692 // check if next string can be converted to u32
693 if let Some(ref check_for_int_str) = iter.peek() {
694 if u32::from_str(check_for_int_str).is_ok() {
695 is_int = true;
696 } else {
697 // if not ... break the loop
698 break;
699 }
700 }
701 // if we can convert use next()
702 if is_int {
703 if let Some(shidx_str) = iter.next() {
704 let shidx: u32 =
705 u32::from_str(shidx_str).unwrap();
706 shidxs.push(shidx);
707 }
708 }
709 }
710 let mut followed_by_byte: bool = false;
711 // check if next string is 'BYTE' (or not)
712 if let Some(check_for_uint_str) = iter.peek() {
713 if *check_for_uint_str == "BYTE" {
714 followed_by_byte = true;
715 }
716 }
717 if followed_by_byte {
718 // skip next (we checked already)
719 iter.next();
720 let num_elements = shidxs[0];
721 let num_motionblur_keys = shidxs[1];
722 // print!(
723 // "\n shidxs {} {} BYTE ... ",
724 // num_elements, num_motionblur_keys
725 // );
726 let expected: u32 = num_elements * num_motionblur_keys;
727 shidxs = Vec::new();
728 for _i in 0..expected {
729 if let Some(shidx_str) = iter.next() {
730 let shidx: u32 =
731 u32::from_str(shidx_str).unwrap();
732 shidxs.push(shidx);
733 }
734 }
735 } else {
736 // print!("\n shidxs ... ");
737 }
738 // print!("\n {:?} ", shidxs);
739 } else if next == "shader" {
740 shader_names = get_shader_names(&mut iter);
741 // print!("\n {:?} ", shader_names);
742 }
743 } else if node_type == "disk" {
744 if next == "radius" {
745 if let Some(radius_str) = iter.next() {
746 radius = f32::from_str(radius_str).unwrap();
747 // print!("\n radius {} ", radius);
748 }
749 } else if next == "hole" {
750 if let Some(hole_str) = iter.next() {
751 hole = f32::from_str(hole_str).unwrap();
752 // print!("\n hole {} ", hole);
753 }
754 } else if next == "shader" {
755 shader_names = get_shader_names(&mut iter);
756 // print!("\n {:?} ", shader_names);
757 }
758 } else if node_type == "sphere" {
759 if next == "radius" {
760 if let Some(radius_str) = iter.next() {
761 radius = f32::from_str(radius_str).unwrap();
762 // print!("\n radius {} ", radius);
763 }
764 } else if next == "shader" {
765 shader_names = get_shader_names(&mut iter);
766 // print!("\n {:?} ", shader_names);
767 }
768 } else if node_type == "cylinder" {
769 if next == "radius" {
770 if let Some(radius_str) = iter.next() {
771 radius = f32::from_str(radius_str).unwrap();
772 // print!("\n radius {} ", radius);
773 }
774 } else if next == "shader" {
775 shader_names = get_shader_names(&mut iter);
776 // print!("\n {:?} ", shader_names);
777 }
778 } else if node_type == "standard_surface" {
779 if next == "base_color" {
780 let mut color_r: Float = 0.0;
781 let mut color_g: Float = 0.0;
782 let mut color_b: Float = 0.0;
783 if let Some(color_str) = iter.next() {
784 color_r = f32::from_str(color_str).unwrap();
785 }
786 if let Some(color_str) = iter.next() {
787 color_g = f32::from_str(color_str).unwrap();
788 }
789 if let Some(color_str) = iter.next() {
790 color_b = f32::from_str(color_str).unwrap();
791 }
792 base_color = Spectrum::rgb(color_r, color_g, color_b);
793 print!(
794 "\n base_color {} {} {} ",
795 color_r, color_g, color_b
796 );
797 } else if next == "specular_color" {
798 let mut color_r: Float = 0.0;
799 let mut color_g: Float = 0.0;
800 let mut color_b: Float = 0.0;
801 if let Some(color_str) = iter.next() {
802 color_r = f32::from_str(color_str).unwrap();
803 }
804 if let Some(color_str) = iter.next() {
805 color_g = f32::from_str(color_str).unwrap();
806 }
807 if let Some(color_str) = iter.next() {
808 color_b = f32::from_str(color_str).unwrap();
809 }
810 specular_color = Spectrum::rgb(color_r, color_g, color_b);
811 print!(
812 "\n specular_color {} {} {} ",
813 color_r, color_g, color_b
814 );
815 } else if next == "specular_roughness" {
816 if let Some(specular_roughness_str) = iter.next() {
817 specular_roughness =
818 f32::from_str(specular_roughness_str).unwrap();
819 print!("\n specular_roughness {} ", specular_roughness);
820 }
821 } else if next == "metalness" {
822 if let Some(metalness_str) = iter.next() {
823 metalness = f32::from_str(metalness_str).unwrap();
824 print!("\n metalness {} ", metalness);
825 }
826 }
827 }
828 } else {
829 // by node type
830 if node_type == "options" {
831 println!("}}");
832 } else if node_type == "persp_camera" && node_name == render_camera
833 {
834 // println!("}}");
835 } else if node_type == "gaussian_filter" {
836 // println!("}}");
837 } else if node_type == "mesh_light" {
838 match named_primitives.get_mut(mesh.as_str()) {
839 Some((_shader_names, prims_vec)) => {
840 // for i in 0..prims.len() {
841 // let mut prim = &mut prims[i];
842 for (_shader_idx, prim) in prims_vec.iter_mut() {
843 let prim_opt = Arc::get_mut(prim);
844 if prim_opt.is_some() {
845 let prim = prim_opt.unwrap();
846 match prim {
847 Primitive::Geometric(primitive) => {
848 let shape = primitive.shape.clone();
849 let mi: MediumInterface =
850 MediumInterface::default();
851 let l_emit: Spectrum =
852 color * intensity;
853 let two_sided: bool = false;
854 let area_light: Arc<Light> = Arc::new(
855 Light::DiffuseArea(Box::new(
856 DiffuseAreaLight::new(
857 &cur_transform,
858 &mi,
859 &l_emit,
860 samples,
861 shape,
862 two_sided,
863 ),
864 )),
865 );
866 lights.push(area_light.clone());
867 primitive.area_light =
868 Some(area_light.clone());
869 }
870 _ => {}
871 }
872 } else {
873 println!("WARNING: no pointer from primitive to area light");
874 }
875 }
876 }
877 None => {
878 panic!("ERROR: mesh_light({:?}) without mesh", mesh);
879 }
880 }
881 // println!("}}");
882 } else if node_type == "point_light" {
883 let mi: MediumInterface = MediumInterface::default();
884 let point_light = Arc::new(Light::Point(Box::new(
885 PointLight::new(&cur_transform, &mi, &(color * intensity)),
886 )));
887 lights.push(point_light);
888 println!("}}");
889 } else if node_type == "spot_light" {
890 let mi: MediumInterface = MediumInterface::default();
891 let spot_light =
892 Arc::new(Light::Spot(Box::new(SpotLight::new(
893 &cur_transform,
894 &mi,
895 &(color * intensity),
896 cone_angle,
897 cone_angle - cone_delta_angle,
898 ))));
899 lights.push(spot_light);
900 println!("}}");
901 } else if node_type == "polymesh" {
902 // make sure there are no out of-bounds vertex indices
903 for i in 0..vi.len() {
904 if vi[i] as usize >= p_ws_len {
905 panic!(
906 "trianglemesh has out of-bounds vertex index {} ({} \"P\" values were given)",
907 vi[i],
908 p_ws_len
909 );
910 }
911 }
912 // convert quads to triangles
913 let mut vi_tri: Vec<u32> = Vec::new();
914 let mut shidxs_tri: Vec<u32> = Vec::new();
915 let mut count_vi: usize = 0;
916 let mut count_shidxs: usize = 0;
917 for i in 0..nsides.len() {
918 let nside = nsides[i];
919 if nside == 3 {
920 // triangle
921 vi_tri.push(vi[count_vi]);
922 count_vi += 1;
923 vi_tri.push(vi[count_vi]);
924 count_vi += 1;
925 vi_tri.push(vi[count_vi]);
926 count_vi += 1;
927 shidxs_tri.push(shidxs[count_shidxs]);
928 count_shidxs += 1;
929 } else if nside == 4 {
930 // quad gets split into 2 triangles
931 vi_tri.push(vi[count_vi]);
932 vi_tri.push(vi[count_vi + 1]);
933 vi_tri.push(vi[count_vi + 2]);
934 vi_tri.push(vi[count_vi]);
935 vi_tri.push(vi[count_vi + 2]);
936 vi_tri.push(vi[count_vi + 3]);
937 count_vi += 4;
938 shidxs_tri.push(shidxs[count_shidxs]);
939 shidxs_tri.push(shidxs[count_shidxs]);
940 count_shidxs += 1;
941 } else {
942 panic!("{}-sided poygons are not supported", nside);
943 }
944 }
945 let n_triangles: usize = vi_tri.len() / 3;
946 assert!(shidxs_tri.len() == n_triangles);
947 // TriangleMesh
948 let mut shapes: Vec<Arc<Shape>> = Vec::new();
949 let s_ws: Vec<Vector3f> = Vec::new();
950 let n_ws: Vec<Normal3f> = Vec::new();
951 let uvs: Vec<Point2f> = Vec::new();
952 // vertex indices are expected as usize, not u32
953 let mut vertex_indices: Vec<u32> = Vec::new();
954 for i in 0..vi_tri.len() {
955 vertex_indices.push(vi_tri[i] as u32);
956 }
957 let mesh = Arc::new(TriangleMesh::new(
958 obj_to_world,
959 world_to_obj,
960 false, // reverse_orientation,
961 n_triangles.try_into().unwrap(),
962 vertex_indices,
963 p_ws_len as u32,
964 p_ws.clone(), // in world space
965 s_ws, // in world space
966 n_ws, // in world space
967 uvs,
968 None,
969 None,
970 ));
971 for id in 0..mesh.n_triangles {
972 let triangle =
973 Arc::new(Shape::Trngl(Triangle::new(mesh.clone(), id)));
974 shapes.push(triangle.clone());
975 }
976 let mi: MediumInterface = MediumInterface::default();
977 let mut prims: Vec<(u32, Arc<Primitive>)> = Vec::new();
978 assert!(shidxs_tri.len() == shapes.len());
979 for i in 0..shapes.len() {
980 let shape = &shapes[i];
981 let shidx = shidxs_tri[i];
982 let geo_prim = Arc::new(Primitive::Geometric(Box::new(
983 GeometricPrimitive::new(
984 shape.clone(),
985 None,
986 None,
987 Some(Arc::new(mi.clone())),
988 ),
989 )));
990 prims.push((shidx, geo_prim.clone()));
991 }
992 named_primitives
993 .insert(node_name.clone(), (shader_names.clone(), prims));
994 // println!("}}");
995 } else if node_type == "disk" {
996 let mut shapes: Vec<Arc<Shape>> = Vec::new();
997 let disk = Arc::new(Shape::Dsk(Disk::new(
998 obj_to_world,
999 world_to_obj,
1000 false,
1001 0.0 as Float, // height
1002 radius,
1003 hole,
1004 360.0 as Float, // phi_max
1005 )));
1006 shapes.push(disk.clone());
1007 let mi: MediumInterface = MediumInterface::default();
1008 let mut prims: Vec<(u32, Arc<Primitive>)> = Vec::new();
1009 let shidx: u32 = 0;
1010 for i in 0..shapes.len() {
1011 let shape = &shapes[i];
1012 let geo_prim = Arc::new(Primitive::Geometric(Box::new(
1013 GeometricPrimitive::new(
1014 shape.clone(),
1015 None,
1016 None,
1017 Some(Arc::new(mi.clone())),
1018 ),
1019 )));
1020 prims.push((shidx, geo_prim.clone()));
1021 }
1022 named_primitives
1023 .insert(node_name.clone(), (shader_names.clone(), prims));
1024 // println!("}}");
1025 } else if node_type == "sphere" {
1026 let mut shapes: Vec<Arc<Shape>> = Vec::new();
1027 let sphere = Arc::new(Shape::Sphr(Sphere::new(
1028 obj_to_world,
1029 world_to_obj,
1030 false,
1031 radius,
1032 -radius, // z_min
1033 radius, // z_max
1034 360.0 as Float, // phi_max
1035 )));
1036 shapes.push(sphere.clone());
1037 let mi: MediumInterface = MediumInterface::default();
1038 let mut prims: Vec<(u32, Arc<Primitive>)> = Vec::new();
1039 let shidx: u32 = 0;
1040 for i in 0..shapes.len() {
1041 let shape = &shapes[i];
1042 let geo_prim = Arc::new(Primitive::Geometric(Box::new(
1043 GeometricPrimitive::new(
1044 shape.clone(),
1045 None,
1046 None,
1047 Some(Arc::new(mi.clone())),
1048 ),
1049 )));
1050 prims.push((shidx, geo_prim.clone()));
1051 }
1052 named_primitives
1053 .insert(node_name.clone(), (shader_names.clone(), prims));
1054 // println!("}}");
1055 } else if node_type == "cylinder" {
1056 let mut shapes: Vec<Arc<Shape>> = Vec::new();
1057 // TODO: assumption about z_min and z_max
1058 let cylinder = Arc::new(Shape::Clndr(Cylinder::new(
1059 obj_to_world,
1060 world_to_obj,
1061 false,
1062 radius,
1063 0.0 as Float, // z_min
1064 radius, // z_max
1065 360.0 as Float, // phi_max
1066 )));
1067 shapes.push(cylinder.clone());
1068 let mi: MediumInterface = MediumInterface::default();
1069 let mut prims: Vec<(u32, Arc<Primitive>)> = Vec::new();
1070 let shidx: u32 = 0;
1071 for i in 0..shapes.len() {
1072 let shape = &shapes[i];
1073 let geo_prim = Arc::new(Primitive::Geometric(Box::new(
1074 GeometricPrimitive::new(
1075 shape.clone(),
1076 None,
1077 None,
1078 Some(Arc::new(mi.clone())),
1079 ),
1080 )));
1081 prims.push((shidx, geo_prim.clone()));
1082 }
1083 named_primitives
1084 .insert(node_name.clone(), (shader_names.clone(), prims));
1085 // println!("}}");
1086 } else if node_type == "standard_surface" {
1087 if metalness > 0.0 as Float {
1088 if metalness == 1.0 as Float {
1089 let kr = Arc::new(ConstantTexture::new(specular_color));
1090 let mirror = Arc::new(Material::Mirror(Box::new(
1091 MirrorMaterial::new(kr, None),
1092 )));
1093 named_materials.insert(node_name.clone(), mirror);
1094 } else {
1095 let copper_n: Spectrum = Spectrum::from_sampled(
1096 &COPPER_WAVELENGTHS,
1097 &COPPER_N,
1098 COPPER_SAMPLES as i32,
1099 );
1100 let eta: Arc<dyn Texture<Spectrum> + Send + Sync> =
1101 Arc::new(ConstantTexture::new(copper_n));
1102 let copper_k: Spectrum = Spectrum::from_sampled(
1103 &COPPER_WAVELENGTHS,
1104 &COPPER_K,
1105 COPPER_SAMPLES as i32,
1106 );
1107 let k: Arc<dyn Texture<Spectrum> + Send + Sync> =
1108 Arc::new(ConstantTexture::new(copper_k));
1109 let roughness = Arc::new(ConstantTexture::new(
1110 specular_roughness as Float,
1111 ));
1112 let remap_roughness: bool = true;
1113 let metal = Arc::new(Material::Metal(Box::new(
1114 MetalMaterial::new(
1115 eta,
1116 k,
1117 roughness,
1118 None,
1119 None,
1120 None,
1121 remap_roughness,
1122 ),
1123 )));
1124 named_materials.insert(node_name.clone(), metal);
1125 }
1126 } else {
1127 // TODO: create a matte material for now
1128 let kd = Arc::new(ConstantTexture::new(base_color));
1129 let sigma = Arc::new(ConstantTexture::new(0.0 as Float));
1130 let matte = Arc::new(Material::Matte(Box::new(
1131 MatteMaterial::new(kd, sigma, None),
1132 )));
1133 named_materials.insert(node_name.clone(), matte);
1134 }
1135 // reset
1136 base_color = Spectrum::new(0.5 as Float);
1137 specular_color = Spectrum::new(1.0 as Float);
1138 specular_roughness = 0.01 as Float;
1139 metalness = 0.0 as Float;
1140 println!("}}");
1141 }
1142 }
1143 } else {
1144 break;
1145 }
1146 }
1147 }
1148 _ => println!("TODO: {:?}", inner_pair.as_rule()),
1149 }
1150 }
1151 }
1152 println!("render_camera = {:?} ", render_camera);
1153 println!("fov = {:?} ", fov);
1154 println!("filter_name = {:?}", filter_name);
1155 println!("filter_width = {:?}", filter_width);
1156 println!("max_depth = {:?}", max_depth);
1157 for value in named_primitives.values_mut() {
1158 let (shader_names, tuple_vec) = value;
1159 // let mut count: usize = 0;
1160 for (shader_idx, prim) in tuple_vec.iter_mut() {
1161 if shader_names.len() > 0 as usize {
1162 let shader_name: String = shader_names[*shader_idx as usize].clone();
1163 if let Some(named_material) = named_materials.get(&shader_name) {
1164 // println!("#{}: {} -> {:?}", count, shader_idx, shader_name);
1165 let prim_opt = Arc::get_mut(prim);
1166 if prim_opt.is_some() {
1167 let prim = prim_opt.unwrap();
1168 match prim {
1169 Primitive::Geometric(primitive) => {
1170 primitive.material = Some(named_material.clone());
1171 }
1172 _ => {}
1173 }
1174 } else {
1175 println!("WARNING: Can't replace GeometricPrimitive.material");
1176 }
1177 }
1178 } else {
1179 println!("WARNING: No shader names");
1180 }
1181 primitives.push(prim.clone());
1182 // count += 1;
1183 }
1184 }
1185 println!("samples_per_pixel = {:?}", samples_per_pixel);
1186 println!("number of lights = {:?}", lights.len());
1187 println!("number of primitives = {:?}", primitives.len());
1188 let some_integrator: Option<Box<Integrator>> = make_path_integrator(
1189 filter_width,
1190 xres,
1191 yres,
1192 fov,
1193 animated_cam_to_world,
1194 max_depth,
1195 samples_per_pixel as i32,
1196 );
1197 if let Some(mut integrator) = some_integrator {
1198 let scene = make_scene(&primitives, lights);
1199 let num_threads: u8 = num_cpus::get() as u8;
1200 integrator.render(&scene, num_threads);
1201 } else {
1202 panic!("Unable to create integrator.");
1203 }
1204 Ok(())
1205}
Sourcepub fn rgb(r: Float, g: Float, b: Float) -> RGBSpectrum
pub fn rgb(r: Float, g: Float, b: Float) -> RGBSpectrum
Examples found in repository?
examples/parse_ass_file.rs (line 460)
244fn main() -> std::io::Result<()> {
245 let num_cores = num_cpus::get();
246 let git_describe = option_env!("GIT_DESCRIBE").unwrap_or("unknown");
247 println!(
248 "parse_ass_file version {} ({}) [Detected {} cores]",
249 VERSION, git_describe, num_cores
250 );
251 println!();
252 // handle command line options
253 let args = Args::parse();
254 let samples_per_pixel: u16 = args.samples;
255 // default values
256 let mut node_name: String = String::from(""); // no default name
257 let mut filter_name: String = String::from("box");
258 let mut filter_width: Float = 2.0;
259 let mut render_camera: String = String::from(""); // no default name
260 let mut mesh: String = String::from(""); // no default name
261 let mut fov: Float = 90.0; // read persp_camera.fov
262 let mut intensity: Float = 1.0; // read mesh_light.intensity
263 let mut cone_angle: Float = 30.0; // read spot_light.cone_angle
264 let cone_delta_angle: Float = 5.0; // TODO: read from .ass file?
265 let mut radius: Float = 0.5; // read [cylinder, disk, sphere].radius
266 let mut hole: Float = 0.0; // read disk.hole
267 let mut color: Spectrum = Spectrum::new(1.0 as Float);
268 // read standard_surface.base_color
269 let mut base_color: Spectrum = Spectrum::new(0.5 as Float);
270 // read standard_surface.specular_color
271 let mut specular_color: Spectrum = Spectrum::new(1.0 as Float);
272 let mut specular_roughness: Float = 0.01; // read standard_surface.specular_roughness
273 let mut metalness: Float = 0.0; // read standard_surface.metalness
274 let mut animated_cam_to_world: AnimatedTransform = AnimatedTransform::default();
275 let mut xres: i32 = 1280; // read options.xres
276 let mut yres: i32 = 720; // read options.yres
277 let mut max_depth: i32 = 5; // read options.GI_total_depth
278 let mut samples: i32 = 1; // read mesh_light.samples
279 let mut cur_transform: Transform = Transform::default();
280 let mut obj_to_world: Transform = Transform::default();
281 let mut world_to_obj: Transform = Transform::default();
282 let mut nsides: Vec<u32> = Vec::new();
283 let mut shidxs: Vec<u32> = Vec::new();
284 let mut shader_names: Vec<String> = Vec::new();
285 let mut p_ws: Vec<Point3f> = Vec::new();
286 let mut p_ws_len: usize = 0;
287 let mut vi: Vec<u32> = Vec::new();
288 let mut primitives: Vec<Arc<Primitive>> = Vec::new();
289 let mut lights: Vec<Arc<Light>> = Vec::new();
290 let mut named_materials: HashMap<String, Arc<Material>> = HashMap::new();
291 let mut named_primitives: HashMap<String, (Vec<String>, Vec<(u32, Arc<Primitive>)>)> =
292 HashMap::new();
293 // input (.ass) file
294 println!("FILE = {:?}", args.path);
295 let f = File::open(&args.path)?;
296 if args.path.is_relative() {
297 let cp: PathBuf = env::current_dir().unwrap();
298 let pb: PathBuf = cp.join(args.path);
299 let search_directory: &Path = pb.as_path().parent().unwrap();
300 println!("search_directory is {}", search_directory.display());
301 }
302 let mut reader = BufReader::new(f);
303 let mut str_buf: String = String::default();
304 let num_bytes = reader.read_to_string(&mut str_buf);
305 if num_bytes.is_ok() {
306 let n_bytes = num_bytes.unwrap();
307 println!("{} bytes read", n_bytes);
308 }
309 // parser
310 let pairs = AssParser::parse(Rule::ass, &str_buf).unwrap_or_else(|e| panic!("{}", e));
311 // let tokens: Vec<_> = pairs.flatten().tokens().collect();
312 // println!("{} pairs", tokens.len());
313 for pair in pairs {
314 let span = pair.clone().as_span();
315 // println!("Rule: {:?}", pair.as_rule());
316 // println!("Span: {:?}", span);
317 // println!("Text: {}", span.as_str());
318 for inner_pair in pair.into_inner() {
319 match inner_pair.as_rule() {
320 Rule::ident => {
321 let node_type = inner_pair.clone().as_span().as_str();
322 if node_type == "options"
323 || node_type == "standard_surface"
324 || node_type == "spot_light"
325 || node_type == "point_light"
326 {
327 print!("{} {{", node_type);
328 }
329 let stripped = strip_comments(span.as_str());
330 let mut iter = stripped.split_whitespace().peekable();
331 loop {
332 if let Some(next) = iter.next() {
333 if next != String::from("}") {
334 // for all nodes
335 if next == "name" {
336 if let Some(name) = iter.next() {
337 node_name = name.to_string();
338 }
339 if node_type == "standard_surface" {
340 print!(" {} {} ", next, node_name);
341 }
342 } else if next == "matrix" {
343 let mut elems: Vec<Float> = Vec::new();
344 let expected: u32 = 16;
345 for _i in 0..expected {
346 if let Some(elem_str) = iter.next() {
347 let elem: f32 = f32::from_str(elem_str).unwrap();
348 elems.push(elem as Float);
349 }
350 }
351 // print!("\n matrix ... ");
352 // print!("\n {:?}", elems);
353 let m00: Float = elems[0];
354 let m01: Float = elems[1];
355 let m02: Float = elems[2];
356 let m03: Float = elems[3];
357 let m10: Float = elems[4];
358 let m11: Float = elems[5];
359 let m12: Float = elems[6];
360 let m13: Float = elems[7];
361 let m20: Float = elems[8];
362 let m21: Float = elems[9];
363 let m22: Float = elems[10];
364 let m23: Float = elems[11];
365 let m30: Float = elems[12];
366 let m31: Float = elems[13];
367 let m32: Float = elems[14];
368 let m33: Float = elems[15];
369 cur_transform = Transform::new(
370 m00, m10, m20, m30, m01, m11, m21, m31, m02, m12, m22, m32,
371 m03, m13, m23, m33,
372 );
373 // print!("\n {:?}", cur_transform);
374 obj_to_world = Transform {
375 m: cur_transform.m,
376 m_inv: cur_transform.m_inv,
377 };
378 world_to_obj = Transform {
379 m: cur_transform.m_inv,
380 m_inv: cur_transform.m,
381 };
382 if node_type == "persp_camera" && node_name == render_camera {
383 let transform_start_time: Float = 0.0;
384 let transform_end_time: Float = 1.0;
385 let scale: Transform = Transform::scale(
386 1.0 as Float,
387 1.0 as Float,
388 -1.0 as Float,
389 );
390 cur_transform = cur_transform * scale;
391 animated_cam_to_world = AnimatedTransform::new(
392 &cur_transform,
393 transform_start_time,
394 &cur_transform,
395 transform_end_time,
396 );
397 }
398 }
399 // by node type
400 if node_type == "options" {
401 if next == "xres" {
402 if let Some(xres_str) = iter.next() {
403 xres = i32::from_str(xres_str).unwrap();
404 print!("\n xres {} ", xres);
405 }
406 } else if next == "yres" {
407 if let Some(yres_str) = iter.next() {
408 yres = i32::from_str(yres_str).unwrap();
409 print!("\n yres {} ", yres);
410 }
411 } else if next == "camera" {
412 if let Some(camera_str) = iter.next() {
413 // strip surrounding double quotes
414 let v: Vec<&str> = camera_str.split('"').collect();
415 render_camera = v[1].to_string();
416 print!("\n camera {:?} ", render_camera);
417 }
418 } else if next == "GI_total_depth" {
419 if let Some(max_depth_str) = iter.next() {
420 max_depth = i32::from_str(max_depth_str).unwrap();
421 print!("\n GI_total_depth {} ", max_depth);
422 }
423 }
424 } else if node_type == "persp_camera" && node_name == render_camera
425 {
426 // camera_name = String::from("perspective");
427 if next == "fov" {
428 if let Some(fov_str) = iter.next() {
429 fov = f32::from_str(fov_str).unwrap();
430 // print!("\n fov {} ", fov);
431 }
432 }
433 } else if node_type == "gaussian_filter" {
434 filter_name = String::from("gaussian");
435 if next == "width" {
436 if let Some(filter_width_str) = iter.next() {
437 filter_width = f32::from_str(filter_width_str).unwrap();
438 // print!("\n filter_width {} ", filter_width);
439 }
440 }
441 } else if node_type == "mesh_light" {
442 if next == "intensity" {
443 if let Some(intensity_str) = iter.next() {
444 intensity = f32::from_str(intensity_str).unwrap();
445 // print!("\n intensity {} ", intensity);
446 }
447 } else if next == "color" {
448 let mut color_r: Float = 0.0;
449 let mut color_g: Float = 0.0;
450 let mut color_b: Float = 0.0;
451 if let Some(color_str) = iter.next() {
452 color_r = f32::from_str(color_str).unwrap();
453 }
454 if let Some(color_str) = iter.next() {
455 color_g = f32::from_str(color_str).unwrap();
456 }
457 if let Some(color_str) = iter.next() {
458 color_b = f32::from_str(color_str).unwrap();
459 }
460 color = Spectrum::rgb(color_r, color_g, color_b);
461 // print!(
462 // "\n color {} {} {} ",
463 // color_r, color_g, color_b
464 // );
465 } else if next == "samples" {
466 if let Some(samples_str) = iter.next() {
467 samples = i32::from_str(samples_str).unwrap();
468 // print!("\n samples {} ", samples);
469 }
470 } else if next == "mesh" {
471 if let Some(mesh_str) = iter.next() {
472 // strip surrounding double quotes
473 let v: Vec<&str> = mesh_str.split('"').collect();
474 mesh = v[1].to_string();
475 // print!("\n mesh {:?} ", mesh);
476 }
477 }
478 } else if node_type == "point_light" {
479 if next == "intensity" {
480 if let Some(intensity_str) = iter.next() {
481 intensity = f32::from_str(intensity_str).unwrap();
482 print!("\n intensity {} ", intensity);
483 }
484 } else if next == "color" {
485 let mut color_r: Float = 0.0;
486 let mut color_g: Float = 0.0;
487 let mut color_b: Float = 0.0;
488 if let Some(color_str) = iter.next() {
489 color_r = f32::from_str(color_str).unwrap();
490 }
491 if let Some(color_str) = iter.next() {
492 color_g = f32::from_str(color_str).unwrap();
493 }
494 if let Some(color_str) = iter.next() {
495 color_b = f32::from_str(color_str).unwrap();
496 }
497 color = Spectrum::rgb(color_r, color_g, color_b);
498 print!("\n color {} {} {} ", color_r, color_g, color_b);
499 }
500 } else if node_type == "spot_light" {
501 if next == "intensity" {
502 if let Some(intensity_str) = iter.next() {
503 intensity = f32::from_str(intensity_str).unwrap();
504 print!("\n intensity {} ", intensity);
505 }
506 } else if next == "color" {
507 let mut color_r: Float = 0.0;
508 let mut color_g: Float = 0.0;
509 let mut color_b: Float = 0.0;
510 if let Some(color_str) = iter.next() {
511 color_r = f32::from_str(color_str).unwrap();
512 }
513 if let Some(color_str) = iter.next() {
514 color_g = f32::from_str(color_str).unwrap();
515 }
516 if let Some(color_str) = iter.next() {
517 color_b = f32::from_str(color_str).unwrap();
518 }
519 color = Spectrum::rgb(color_r, color_g, color_b);
520 print!("\n color {} {} {} ", color_r, color_g, color_b);
521 } else if next == "cone_angle" {
522 if let Some(cone_angle_str) = iter.next() {
523 cone_angle = f32::from_str(cone_angle_str).unwrap();
524 print!("\n cone_angle {} ", cone_angle);
525 }
526 }
527 } else if node_type == "polymesh" {
528 if next == "vlist" {
529 // parameter_name: vlist
530 // <num_elements>
531 // <num_motionblur_keys>
532 // <data_type>: VECTOR
533 // <elem1> <elem2>
534 // <elem3> <elem4>
535 // ...
536 let num_elements: u32;
537 let num_motionblur_keys: u32;
538 let data_type: String = String::from("VECTOR");
539 let mut elems: Vec<Float> = Vec::new();
540 if let Some(num_elements_str) = iter.next() {
541 num_elements = u32::from_str(num_elements_str).unwrap();
542 if let Some(num_motionblur_keys_str) = iter.next() {
543 num_motionblur_keys =
544 u32::from_str(num_motionblur_keys_str).unwrap();
545 if let Some(data_type_str) = iter.next() {
546 if data_type_str != data_type {
547 panic!("ERROR: {} expected ...", data_type);
548 } else {
549 let expected: u32 =
550 num_elements * num_motionblur_keys * 3;
551 for _i in 0..expected {
552 if let Some(elem_str) = iter.next() {
553 let elem: f32 =
554 f32::from_str(elem_str)
555 .unwrap();
556 elems.push(elem as Float);
557 }
558 }
559 }
560 }
561 }
562 }
563 // print!(
564 // "\n vlist {} {} VECTOR ... ",
565 // num_elements, num_motionblur_keys
566 // );
567 // print!("\n {:?}", elems);
568 // TriangleMesh
569 let mut x: Float = 0.0;
570 let mut y: Float = 0.0;
571 let mut z;
572 let mut p: Vec<Point3f> = Vec::new();
573 for i in 0..elems.len() {
574 if i % 3 == 0 {
575 x = elems[i];
576 } else if i % 3 == 1 {
577 y = elems[i];
578 } else {
579 // i % 3 == 2
580 z = elems[i];
581 // store as Point3f
582 p.push(Point3f { x, y, z });
583 }
584 }
585 // transform mesh vertices to world space
586 p_ws = Vec::new();
587 let n_vertices: usize = p.len();
588 for i in 0..n_vertices {
589 p_ws.push(obj_to_world.transform_point(&p[i]));
590 }
591 p_ws_len = p_ws.len();
592 // print info
593 // println!("");
594 // for point in p {
595 // println!(" {:?}", point);
596 // }
597 } else if next == "nsides" {
598 nsides = Vec::new();
599 loop {
600 let mut is_int: bool = false;
601 // check if next string can be converted to u32
602 if let Some(ref check_for_int_str) = iter.peek() {
603 if u32::from_str(check_for_int_str).is_ok() {
604 is_int = true;
605 } else {
606 // if not ... break the loop
607 break;
608 }
609 }
610 // if we can convert use next()
611 if is_int {
612 if let Some(nside_str) = iter.next() {
613 let nside: u32 =
614 u32::from_str(nside_str).unwrap();
615 nsides.push(nside);
616 }
617 }
618 }
619 let mut followed_by_uint: bool = false;
620 // check if next string is 'UINT' (or not)
621 if let Some(check_for_uint_str) = iter.peek() {
622 if *check_for_uint_str == "UINT" {
623 followed_by_uint = true;
624 }
625 }
626 if followed_by_uint {
627 // skip next (we checked already)
628 iter.next();
629 let num_elements = nsides[0];
630 let num_motionblur_keys = nsides[1];
631 // print!(
632 // "\n nsides {} {} UINT ... ",
633 // num_elements, num_motionblur_keys
634 // );
635 let expected: u32 = num_elements * num_motionblur_keys;
636 nsides = Vec::new();
637 for _i in 0..expected {
638 if let Some(nside_str) = iter.next() {
639 let nside: u32 =
640 u32::from_str(nside_str).unwrap();
641 nsides.push(nside);
642 }
643 }
644 } else {
645 // print!("\n nsides ... ");
646 }
647 // print!("\n {:?} ", nsides);
648 } else if next == "vidxs" {
649 // parameter_name: vidxs
650 // <num_elements>
651 // <num_motionblur_keys>
652 // <data_type>: UINT
653 // <elem1> <elem2>
654 // <elem3> <elem4>
655 // ...
656 let num_elements: u32;
657 let num_motionblur_keys: u32;
658 let data_type: String = String::from("UINT");
659 vi = Vec::new();
660 if let Some(num_elements_str) = iter.next() {
661 num_elements = u32::from_str(num_elements_str).unwrap();
662 if let Some(num_motionblur_keys_str) = iter.next() {
663 num_motionblur_keys =
664 u32::from_str(num_motionblur_keys_str).unwrap();
665 if let Some(data_type_str) = iter.next() {
666 if data_type_str != data_type {
667 panic!("ERROR: {} expected ...", data_type);
668 } else {
669 let expected: u32 =
670 num_elements * num_motionblur_keys;
671 for _i in 0..expected {
672 if let Some(elem_str) = iter.next() {
673 let elem: u32 =
674 u32::from_str(elem_str)
675 .unwrap();
676 vi.push(elem);
677 }
678 }
679 }
680 }
681 }
682 }
683 // print!(
684 // "\n vidxs {} {} UINT ... ",
685 // num_elements, num_motionblur_keys
686 // );
687 // print!("\n {:?} ", vi);
688 } else if next == "shidxs" {
689 shidxs = Vec::new();
690 loop {
691 let mut is_int: bool = false;
692 // check if next string can be converted to u32
693 if let Some(ref check_for_int_str) = iter.peek() {
694 if u32::from_str(check_for_int_str).is_ok() {
695 is_int = true;
696 } else {
697 // if not ... break the loop
698 break;
699 }
700 }
701 // if we can convert use next()
702 if is_int {
703 if let Some(shidx_str) = iter.next() {
704 let shidx: u32 =
705 u32::from_str(shidx_str).unwrap();
706 shidxs.push(shidx);
707 }
708 }
709 }
710 let mut followed_by_byte: bool = false;
711 // check if next string is 'BYTE' (or not)
712 if let Some(check_for_uint_str) = iter.peek() {
713 if *check_for_uint_str == "BYTE" {
714 followed_by_byte = true;
715 }
716 }
717 if followed_by_byte {
718 // skip next (we checked already)
719 iter.next();
720 let num_elements = shidxs[0];
721 let num_motionblur_keys = shidxs[1];
722 // print!(
723 // "\n shidxs {} {} BYTE ... ",
724 // num_elements, num_motionblur_keys
725 // );
726 let expected: u32 = num_elements * num_motionblur_keys;
727 shidxs = Vec::new();
728 for _i in 0..expected {
729 if let Some(shidx_str) = iter.next() {
730 let shidx: u32 =
731 u32::from_str(shidx_str).unwrap();
732 shidxs.push(shidx);
733 }
734 }
735 } else {
736 // print!("\n shidxs ... ");
737 }
738 // print!("\n {:?} ", shidxs);
739 } else if next == "shader" {
740 shader_names = get_shader_names(&mut iter);
741 // print!("\n {:?} ", shader_names);
742 }
743 } else if node_type == "disk" {
744 if next == "radius" {
745 if let Some(radius_str) = iter.next() {
746 radius = f32::from_str(radius_str).unwrap();
747 // print!("\n radius {} ", radius);
748 }
749 } else if next == "hole" {
750 if let Some(hole_str) = iter.next() {
751 hole = f32::from_str(hole_str).unwrap();
752 // print!("\n hole {} ", hole);
753 }
754 } else if next == "shader" {
755 shader_names = get_shader_names(&mut iter);
756 // print!("\n {:?} ", shader_names);
757 }
758 } else if node_type == "sphere" {
759 if next == "radius" {
760 if let Some(radius_str) = iter.next() {
761 radius = f32::from_str(radius_str).unwrap();
762 // print!("\n radius {} ", radius);
763 }
764 } else if next == "shader" {
765 shader_names = get_shader_names(&mut iter);
766 // print!("\n {:?} ", shader_names);
767 }
768 } else if node_type == "cylinder" {
769 if next == "radius" {
770 if let Some(radius_str) = iter.next() {
771 radius = f32::from_str(radius_str).unwrap();
772 // print!("\n radius {} ", radius);
773 }
774 } else if next == "shader" {
775 shader_names = get_shader_names(&mut iter);
776 // print!("\n {:?} ", shader_names);
777 }
778 } else if node_type == "standard_surface" {
779 if next == "base_color" {
780 let mut color_r: Float = 0.0;
781 let mut color_g: Float = 0.0;
782 let mut color_b: Float = 0.0;
783 if let Some(color_str) = iter.next() {
784 color_r = f32::from_str(color_str).unwrap();
785 }
786 if let Some(color_str) = iter.next() {
787 color_g = f32::from_str(color_str).unwrap();
788 }
789 if let Some(color_str) = iter.next() {
790 color_b = f32::from_str(color_str).unwrap();
791 }
792 base_color = Spectrum::rgb(color_r, color_g, color_b);
793 print!(
794 "\n base_color {} {} {} ",
795 color_r, color_g, color_b
796 );
797 } else if next == "specular_color" {
798 let mut color_r: Float = 0.0;
799 let mut color_g: Float = 0.0;
800 let mut color_b: Float = 0.0;
801 if let Some(color_str) = iter.next() {
802 color_r = f32::from_str(color_str).unwrap();
803 }
804 if let Some(color_str) = iter.next() {
805 color_g = f32::from_str(color_str).unwrap();
806 }
807 if let Some(color_str) = iter.next() {
808 color_b = f32::from_str(color_str).unwrap();
809 }
810 specular_color = Spectrum::rgb(color_r, color_g, color_b);
811 print!(
812 "\n specular_color {} {} {} ",
813 color_r, color_g, color_b
814 );
815 } else if next == "specular_roughness" {
816 if let Some(specular_roughness_str) = iter.next() {
817 specular_roughness =
818 f32::from_str(specular_roughness_str).unwrap();
819 print!("\n specular_roughness {} ", specular_roughness);
820 }
821 } else if next == "metalness" {
822 if let Some(metalness_str) = iter.next() {
823 metalness = f32::from_str(metalness_str).unwrap();
824 print!("\n metalness {} ", metalness);
825 }
826 }
827 }
828 } else {
829 // by node type
830 if node_type == "options" {
831 println!("}}");
832 } else if node_type == "persp_camera" && node_name == render_camera
833 {
834 // println!("}}");
835 } else if node_type == "gaussian_filter" {
836 // println!("}}");
837 } else if node_type == "mesh_light" {
838 match named_primitives.get_mut(mesh.as_str()) {
839 Some((_shader_names, prims_vec)) => {
840 // for i in 0..prims.len() {
841 // let mut prim = &mut prims[i];
842 for (_shader_idx, prim) in prims_vec.iter_mut() {
843 let prim_opt = Arc::get_mut(prim);
844 if prim_opt.is_some() {
845 let prim = prim_opt.unwrap();
846 match prim {
847 Primitive::Geometric(primitive) => {
848 let shape = primitive.shape.clone();
849 let mi: MediumInterface =
850 MediumInterface::default();
851 let l_emit: Spectrum =
852 color * intensity;
853 let two_sided: bool = false;
854 let area_light: Arc<Light> = Arc::new(
855 Light::DiffuseArea(Box::new(
856 DiffuseAreaLight::new(
857 &cur_transform,
858 &mi,
859 &l_emit,
860 samples,
861 shape,
862 two_sided,
863 ),
864 )),
865 );
866 lights.push(area_light.clone());
867 primitive.area_light =
868 Some(area_light.clone());
869 }
870 _ => {}
871 }
872 } else {
873 println!("WARNING: no pointer from primitive to area light");
874 }
875 }
876 }
877 None => {
878 panic!("ERROR: mesh_light({:?}) without mesh", mesh);
879 }
880 }
881 // println!("}}");
882 } else if node_type == "point_light" {
883 let mi: MediumInterface = MediumInterface::default();
884 let point_light = Arc::new(Light::Point(Box::new(
885 PointLight::new(&cur_transform, &mi, &(color * intensity)),
886 )));
887 lights.push(point_light);
888 println!("}}");
889 } else if node_type == "spot_light" {
890 let mi: MediumInterface = MediumInterface::default();
891 let spot_light =
892 Arc::new(Light::Spot(Box::new(SpotLight::new(
893 &cur_transform,
894 &mi,
895 &(color * intensity),
896 cone_angle,
897 cone_angle - cone_delta_angle,
898 ))));
899 lights.push(spot_light);
900 println!("}}");
901 } else if node_type == "polymesh" {
902 // make sure there are no out of-bounds vertex indices
903 for i in 0..vi.len() {
904 if vi[i] as usize >= p_ws_len {
905 panic!(
906 "trianglemesh has out of-bounds vertex index {} ({} \"P\" values were given)",
907 vi[i],
908 p_ws_len
909 );
910 }
911 }
912 // convert quads to triangles
913 let mut vi_tri: Vec<u32> = Vec::new();
914 let mut shidxs_tri: Vec<u32> = Vec::new();
915 let mut count_vi: usize = 0;
916 let mut count_shidxs: usize = 0;
917 for i in 0..nsides.len() {
918 let nside = nsides[i];
919 if nside == 3 {
920 // triangle
921 vi_tri.push(vi[count_vi]);
922 count_vi += 1;
923 vi_tri.push(vi[count_vi]);
924 count_vi += 1;
925 vi_tri.push(vi[count_vi]);
926 count_vi += 1;
927 shidxs_tri.push(shidxs[count_shidxs]);
928 count_shidxs += 1;
929 } else if nside == 4 {
930 // quad gets split into 2 triangles
931 vi_tri.push(vi[count_vi]);
932 vi_tri.push(vi[count_vi + 1]);
933 vi_tri.push(vi[count_vi + 2]);
934 vi_tri.push(vi[count_vi]);
935 vi_tri.push(vi[count_vi + 2]);
936 vi_tri.push(vi[count_vi + 3]);
937 count_vi += 4;
938 shidxs_tri.push(shidxs[count_shidxs]);
939 shidxs_tri.push(shidxs[count_shidxs]);
940 count_shidxs += 1;
941 } else {
942 panic!("{}-sided poygons are not supported", nside);
943 }
944 }
945 let n_triangles: usize = vi_tri.len() / 3;
946 assert!(shidxs_tri.len() == n_triangles);
947 // TriangleMesh
948 let mut shapes: Vec<Arc<Shape>> = Vec::new();
949 let s_ws: Vec<Vector3f> = Vec::new();
950 let n_ws: Vec<Normal3f> = Vec::new();
951 let uvs: Vec<Point2f> = Vec::new();
952 // vertex indices are expected as usize, not u32
953 let mut vertex_indices: Vec<u32> = Vec::new();
954 for i in 0..vi_tri.len() {
955 vertex_indices.push(vi_tri[i] as u32);
956 }
957 let mesh = Arc::new(TriangleMesh::new(
958 obj_to_world,
959 world_to_obj,
960 false, // reverse_orientation,
961 n_triangles.try_into().unwrap(),
962 vertex_indices,
963 p_ws_len as u32,
964 p_ws.clone(), // in world space
965 s_ws, // in world space
966 n_ws, // in world space
967 uvs,
968 None,
969 None,
970 ));
971 for id in 0..mesh.n_triangles {
972 let triangle =
973 Arc::new(Shape::Trngl(Triangle::new(mesh.clone(), id)));
974 shapes.push(triangle.clone());
975 }
976 let mi: MediumInterface = MediumInterface::default();
977 let mut prims: Vec<(u32, Arc<Primitive>)> = Vec::new();
978 assert!(shidxs_tri.len() == shapes.len());
979 for i in 0..shapes.len() {
980 let shape = &shapes[i];
981 let shidx = shidxs_tri[i];
982 let geo_prim = Arc::new(Primitive::Geometric(Box::new(
983 GeometricPrimitive::new(
984 shape.clone(),
985 None,
986 None,
987 Some(Arc::new(mi.clone())),
988 ),
989 )));
990 prims.push((shidx, geo_prim.clone()));
991 }
992 named_primitives
993 .insert(node_name.clone(), (shader_names.clone(), prims));
994 // println!("}}");
995 } else if node_type == "disk" {
996 let mut shapes: Vec<Arc<Shape>> = Vec::new();
997 let disk = Arc::new(Shape::Dsk(Disk::new(
998 obj_to_world,
999 world_to_obj,
1000 false,
1001 0.0 as Float, // height
1002 radius,
1003 hole,
1004 360.0 as Float, // phi_max
1005 )));
1006 shapes.push(disk.clone());
1007 let mi: MediumInterface = MediumInterface::default();
1008 let mut prims: Vec<(u32, Arc<Primitive>)> = Vec::new();
1009 let shidx: u32 = 0;
1010 for i in 0..shapes.len() {
1011 let shape = &shapes[i];
1012 let geo_prim = Arc::new(Primitive::Geometric(Box::new(
1013 GeometricPrimitive::new(
1014 shape.clone(),
1015 None,
1016 None,
1017 Some(Arc::new(mi.clone())),
1018 ),
1019 )));
1020 prims.push((shidx, geo_prim.clone()));
1021 }
1022 named_primitives
1023 .insert(node_name.clone(), (shader_names.clone(), prims));
1024 // println!("}}");
1025 } else if node_type == "sphere" {
1026 let mut shapes: Vec<Arc<Shape>> = Vec::new();
1027 let sphere = Arc::new(Shape::Sphr(Sphere::new(
1028 obj_to_world,
1029 world_to_obj,
1030 false,
1031 radius,
1032 -radius, // z_min
1033 radius, // z_max
1034 360.0 as Float, // phi_max
1035 )));
1036 shapes.push(sphere.clone());
1037 let mi: MediumInterface = MediumInterface::default();
1038 let mut prims: Vec<(u32, Arc<Primitive>)> = Vec::new();
1039 let shidx: u32 = 0;
1040 for i in 0..shapes.len() {
1041 let shape = &shapes[i];
1042 let geo_prim = Arc::new(Primitive::Geometric(Box::new(
1043 GeometricPrimitive::new(
1044 shape.clone(),
1045 None,
1046 None,
1047 Some(Arc::new(mi.clone())),
1048 ),
1049 )));
1050 prims.push((shidx, geo_prim.clone()));
1051 }
1052 named_primitives
1053 .insert(node_name.clone(), (shader_names.clone(), prims));
1054 // println!("}}");
1055 } else if node_type == "cylinder" {
1056 let mut shapes: Vec<Arc<Shape>> = Vec::new();
1057 // TODO: assumption about z_min and z_max
1058 let cylinder = Arc::new(Shape::Clndr(Cylinder::new(
1059 obj_to_world,
1060 world_to_obj,
1061 false,
1062 radius,
1063 0.0 as Float, // z_min
1064 radius, // z_max
1065 360.0 as Float, // phi_max
1066 )));
1067 shapes.push(cylinder.clone());
1068 let mi: MediumInterface = MediumInterface::default();
1069 let mut prims: Vec<(u32, Arc<Primitive>)> = Vec::new();
1070 let shidx: u32 = 0;
1071 for i in 0..shapes.len() {
1072 let shape = &shapes[i];
1073 let geo_prim = Arc::new(Primitive::Geometric(Box::new(
1074 GeometricPrimitive::new(
1075 shape.clone(),
1076 None,
1077 None,
1078 Some(Arc::new(mi.clone())),
1079 ),
1080 )));
1081 prims.push((shidx, geo_prim.clone()));
1082 }
1083 named_primitives
1084 .insert(node_name.clone(), (shader_names.clone(), prims));
1085 // println!("}}");
1086 } else if node_type == "standard_surface" {
1087 if metalness > 0.0 as Float {
1088 if metalness == 1.0 as Float {
1089 let kr = Arc::new(ConstantTexture::new(specular_color));
1090 let mirror = Arc::new(Material::Mirror(Box::new(
1091 MirrorMaterial::new(kr, None),
1092 )));
1093 named_materials.insert(node_name.clone(), mirror);
1094 } else {
1095 let copper_n: Spectrum = Spectrum::from_sampled(
1096 &COPPER_WAVELENGTHS,
1097 &COPPER_N,
1098 COPPER_SAMPLES as i32,
1099 );
1100 let eta: Arc<dyn Texture<Spectrum> + Send + Sync> =
1101 Arc::new(ConstantTexture::new(copper_n));
1102 let copper_k: Spectrum = Spectrum::from_sampled(
1103 &COPPER_WAVELENGTHS,
1104 &COPPER_K,
1105 COPPER_SAMPLES as i32,
1106 );
1107 let k: Arc<dyn Texture<Spectrum> + Send + Sync> =
1108 Arc::new(ConstantTexture::new(copper_k));
1109 let roughness = Arc::new(ConstantTexture::new(
1110 specular_roughness as Float,
1111 ));
1112 let remap_roughness: bool = true;
1113 let metal = Arc::new(Material::Metal(Box::new(
1114 MetalMaterial::new(
1115 eta,
1116 k,
1117 roughness,
1118 None,
1119 None,
1120 None,
1121 remap_roughness,
1122 ),
1123 )));
1124 named_materials.insert(node_name.clone(), metal);
1125 }
1126 } else {
1127 // TODO: create a matte material for now
1128 let kd = Arc::new(ConstantTexture::new(base_color));
1129 let sigma = Arc::new(ConstantTexture::new(0.0 as Float));
1130 let matte = Arc::new(Material::Matte(Box::new(
1131 MatteMaterial::new(kd, sigma, None),
1132 )));
1133 named_materials.insert(node_name.clone(), matte);
1134 }
1135 // reset
1136 base_color = Spectrum::new(0.5 as Float);
1137 specular_color = Spectrum::new(1.0 as Float);
1138 specular_roughness = 0.01 as Float;
1139 metalness = 0.0 as Float;
1140 println!("}}");
1141 }
1142 }
1143 } else {
1144 break;
1145 }
1146 }
1147 }
1148 _ => println!("TODO: {:?}", inner_pair.as_rule()),
1149 }
1150 }
1151 }
1152 println!("render_camera = {:?} ", render_camera);
1153 println!("fov = {:?} ", fov);
1154 println!("filter_name = {:?}", filter_name);
1155 println!("filter_width = {:?}", filter_width);
1156 println!("max_depth = {:?}", max_depth);
1157 for value in named_primitives.values_mut() {
1158 let (shader_names, tuple_vec) = value;
1159 // let mut count: usize = 0;
1160 for (shader_idx, prim) in tuple_vec.iter_mut() {
1161 if shader_names.len() > 0 as usize {
1162 let shader_name: String = shader_names[*shader_idx as usize].clone();
1163 if let Some(named_material) = named_materials.get(&shader_name) {
1164 // println!("#{}: {} -> {:?}", count, shader_idx, shader_name);
1165 let prim_opt = Arc::get_mut(prim);
1166 if prim_opt.is_some() {
1167 let prim = prim_opt.unwrap();
1168 match prim {
1169 Primitive::Geometric(primitive) => {
1170 primitive.material = Some(named_material.clone());
1171 }
1172 _ => {}
1173 }
1174 } else {
1175 println!("WARNING: Can't replace GeometricPrimitive.material");
1176 }
1177 }
1178 } else {
1179 println!("WARNING: No shader names");
1180 }
1181 primitives.push(prim.clone());
1182 // count += 1;
1183 }
1184 }
1185 println!("samples_per_pixel = {:?}", samples_per_pixel);
1186 println!("number of lights = {:?}", lights.len());
1187 println!("number of primitives = {:?}", primitives.len());
1188 let some_integrator: Option<Box<Integrator>> = make_path_integrator(
1189 filter_width,
1190 xres,
1191 yres,
1192 fov,
1193 animated_cam_to_world,
1194 max_depth,
1195 samples_per_pixel as i32,
1196 );
1197 if let Some(mut integrator) = some_integrator {
1198 let scene = make_scene(&primitives, lights);
1199 let num_threads: u8 = num_cpus::get() as u8;
1200 integrator.render(&scene, num_threads);
1201 } else {
1202 panic!("Unable to create integrator.");
1203 }
1204 Ok(())
1205}
pub fn from_srgb(rgb: [u8; 3]) -> RGBSpectrum
pub fn inverse_gamma_correct(&self) -> RGBSpectrum
pub fn from_rgb(rgb: &[Float; 3]) -> RGBSpectrum
pub fn to_rgb(&self, rgb: &mut [Float; 3])
pub fn to_xyz(&self, xyz: &mut [Float; 3])
pub fn from_xyz(xyz: &[Float; 3], _spectrum_type: SpectrumType) -> RGBSpectrum
pub fn y(&self) -> Float
Sourcepub fn from_sampled(lambda: &[Float], v: &[Float], n: i32) -> RGBSpectrum
pub fn from_sampled(lambda: &[Float], v: &[Float], n: i32) -> RGBSpectrum
Examples found in repository?
examples/parse_ass_file.rs (lines 1095-1099)
244fn main() -> std::io::Result<()> {
245 let num_cores = num_cpus::get();
246 let git_describe = option_env!("GIT_DESCRIBE").unwrap_or("unknown");
247 println!(
248 "parse_ass_file version {} ({}) [Detected {} cores]",
249 VERSION, git_describe, num_cores
250 );
251 println!();
252 // handle command line options
253 let args = Args::parse();
254 let samples_per_pixel: u16 = args.samples;
255 // default values
256 let mut node_name: String = String::from(""); // no default name
257 let mut filter_name: String = String::from("box");
258 let mut filter_width: Float = 2.0;
259 let mut render_camera: String = String::from(""); // no default name
260 let mut mesh: String = String::from(""); // no default name
261 let mut fov: Float = 90.0; // read persp_camera.fov
262 let mut intensity: Float = 1.0; // read mesh_light.intensity
263 let mut cone_angle: Float = 30.0; // read spot_light.cone_angle
264 let cone_delta_angle: Float = 5.0; // TODO: read from .ass file?
265 let mut radius: Float = 0.5; // read [cylinder, disk, sphere].radius
266 let mut hole: Float = 0.0; // read disk.hole
267 let mut color: Spectrum = Spectrum::new(1.0 as Float);
268 // read standard_surface.base_color
269 let mut base_color: Spectrum = Spectrum::new(0.5 as Float);
270 // read standard_surface.specular_color
271 let mut specular_color: Spectrum = Spectrum::new(1.0 as Float);
272 let mut specular_roughness: Float = 0.01; // read standard_surface.specular_roughness
273 let mut metalness: Float = 0.0; // read standard_surface.metalness
274 let mut animated_cam_to_world: AnimatedTransform = AnimatedTransform::default();
275 let mut xres: i32 = 1280; // read options.xres
276 let mut yres: i32 = 720; // read options.yres
277 let mut max_depth: i32 = 5; // read options.GI_total_depth
278 let mut samples: i32 = 1; // read mesh_light.samples
279 let mut cur_transform: Transform = Transform::default();
280 let mut obj_to_world: Transform = Transform::default();
281 let mut world_to_obj: Transform = Transform::default();
282 let mut nsides: Vec<u32> = Vec::new();
283 let mut shidxs: Vec<u32> = Vec::new();
284 let mut shader_names: Vec<String> = Vec::new();
285 let mut p_ws: Vec<Point3f> = Vec::new();
286 let mut p_ws_len: usize = 0;
287 let mut vi: Vec<u32> = Vec::new();
288 let mut primitives: Vec<Arc<Primitive>> = Vec::new();
289 let mut lights: Vec<Arc<Light>> = Vec::new();
290 let mut named_materials: HashMap<String, Arc<Material>> = HashMap::new();
291 let mut named_primitives: HashMap<String, (Vec<String>, Vec<(u32, Arc<Primitive>)>)> =
292 HashMap::new();
293 // input (.ass) file
294 println!("FILE = {:?}", args.path);
295 let f = File::open(&args.path)?;
296 if args.path.is_relative() {
297 let cp: PathBuf = env::current_dir().unwrap();
298 let pb: PathBuf = cp.join(args.path);
299 let search_directory: &Path = pb.as_path().parent().unwrap();
300 println!("search_directory is {}", search_directory.display());
301 }
302 let mut reader = BufReader::new(f);
303 let mut str_buf: String = String::default();
304 let num_bytes = reader.read_to_string(&mut str_buf);
305 if num_bytes.is_ok() {
306 let n_bytes = num_bytes.unwrap();
307 println!("{} bytes read", n_bytes);
308 }
309 // parser
310 let pairs = AssParser::parse(Rule::ass, &str_buf).unwrap_or_else(|e| panic!("{}", e));
311 // let tokens: Vec<_> = pairs.flatten().tokens().collect();
312 // println!("{} pairs", tokens.len());
313 for pair in pairs {
314 let span = pair.clone().as_span();
315 // println!("Rule: {:?}", pair.as_rule());
316 // println!("Span: {:?}", span);
317 // println!("Text: {}", span.as_str());
318 for inner_pair in pair.into_inner() {
319 match inner_pair.as_rule() {
320 Rule::ident => {
321 let node_type = inner_pair.clone().as_span().as_str();
322 if node_type == "options"
323 || node_type == "standard_surface"
324 || node_type == "spot_light"
325 || node_type == "point_light"
326 {
327 print!("{} {{", node_type);
328 }
329 let stripped = strip_comments(span.as_str());
330 let mut iter = stripped.split_whitespace().peekable();
331 loop {
332 if let Some(next) = iter.next() {
333 if next != String::from("}") {
334 // for all nodes
335 if next == "name" {
336 if let Some(name) = iter.next() {
337 node_name = name.to_string();
338 }
339 if node_type == "standard_surface" {
340 print!(" {} {} ", next, node_name);
341 }
342 } else if next == "matrix" {
343 let mut elems: Vec<Float> = Vec::new();
344 let expected: u32 = 16;
345 for _i in 0..expected {
346 if let Some(elem_str) = iter.next() {
347 let elem: f32 = f32::from_str(elem_str).unwrap();
348 elems.push(elem as Float);
349 }
350 }
351 // print!("\n matrix ... ");
352 // print!("\n {:?}", elems);
353 let m00: Float = elems[0];
354 let m01: Float = elems[1];
355 let m02: Float = elems[2];
356 let m03: Float = elems[3];
357 let m10: Float = elems[4];
358 let m11: Float = elems[5];
359 let m12: Float = elems[6];
360 let m13: Float = elems[7];
361 let m20: Float = elems[8];
362 let m21: Float = elems[9];
363 let m22: Float = elems[10];
364 let m23: Float = elems[11];
365 let m30: Float = elems[12];
366 let m31: Float = elems[13];
367 let m32: Float = elems[14];
368 let m33: Float = elems[15];
369 cur_transform = Transform::new(
370 m00, m10, m20, m30, m01, m11, m21, m31, m02, m12, m22, m32,
371 m03, m13, m23, m33,
372 );
373 // print!("\n {:?}", cur_transform);
374 obj_to_world = Transform {
375 m: cur_transform.m,
376 m_inv: cur_transform.m_inv,
377 };
378 world_to_obj = Transform {
379 m: cur_transform.m_inv,
380 m_inv: cur_transform.m,
381 };
382 if node_type == "persp_camera" && node_name == render_camera {
383 let transform_start_time: Float = 0.0;
384 let transform_end_time: Float = 1.0;
385 let scale: Transform = Transform::scale(
386 1.0 as Float,
387 1.0 as Float,
388 -1.0 as Float,
389 );
390 cur_transform = cur_transform * scale;
391 animated_cam_to_world = AnimatedTransform::new(
392 &cur_transform,
393 transform_start_time,
394 &cur_transform,
395 transform_end_time,
396 );
397 }
398 }
399 // by node type
400 if node_type == "options" {
401 if next == "xres" {
402 if let Some(xres_str) = iter.next() {
403 xres = i32::from_str(xres_str).unwrap();
404 print!("\n xres {} ", xres);
405 }
406 } else if next == "yres" {
407 if let Some(yres_str) = iter.next() {
408 yres = i32::from_str(yres_str).unwrap();
409 print!("\n yres {} ", yres);
410 }
411 } else if next == "camera" {
412 if let Some(camera_str) = iter.next() {
413 // strip surrounding double quotes
414 let v: Vec<&str> = camera_str.split('"').collect();
415 render_camera = v[1].to_string();
416 print!("\n camera {:?} ", render_camera);
417 }
418 } else if next == "GI_total_depth" {
419 if let Some(max_depth_str) = iter.next() {
420 max_depth = i32::from_str(max_depth_str).unwrap();
421 print!("\n GI_total_depth {} ", max_depth);
422 }
423 }
424 } else if node_type == "persp_camera" && node_name == render_camera
425 {
426 // camera_name = String::from("perspective");
427 if next == "fov" {
428 if let Some(fov_str) = iter.next() {
429 fov = f32::from_str(fov_str).unwrap();
430 // print!("\n fov {} ", fov);
431 }
432 }
433 } else if node_type == "gaussian_filter" {
434 filter_name = String::from("gaussian");
435 if next == "width" {
436 if let Some(filter_width_str) = iter.next() {
437 filter_width = f32::from_str(filter_width_str).unwrap();
438 // print!("\n filter_width {} ", filter_width);
439 }
440 }
441 } else if node_type == "mesh_light" {
442 if next == "intensity" {
443 if let Some(intensity_str) = iter.next() {
444 intensity = f32::from_str(intensity_str).unwrap();
445 // print!("\n intensity {} ", intensity);
446 }
447 } else if next == "color" {
448 let mut color_r: Float = 0.0;
449 let mut color_g: Float = 0.0;
450 let mut color_b: Float = 0.0;
451 if let Some(color_str) = iter.next() {
452 color_r = f32::from_str(color_str).unwrap();
453 }
454 if let Some(color_str) = iter.next() {
455 color_g = f32::from_str(color_str).unwrap();
456 }
457 if let Some(color_str) = iter.next() {
458 color_b = f32::from_str(color_str).unwrap();
459 }
460 color = Spectrum::rgb(color_r, color_g, color_b);
461 // print!(
462 // "\n color {} {} {} ",
463 // color_r, color_g, color_b
464 // );
465 } else if next == "samples" {
466 if let Some(samples_str) = iter.next() {
467 samples = i32::from_str(samples_str).unwrap();
468 // print!("\n samples {} ", samples);
469 }
470 } else if next == "mesh" {
471 if let Some(mesh_str) = iter.next() {
472 // strip surrounding double quotes
473 let v: Vec<&str> = mesh_str.split('"').collect();
474 mesh = v[1].to_string();
475 // print!("\n mesh {:?} ", mesh);
476 }
477 }
478 } else if node_type == "point_light" {
479 if next == "intensity" {
480 if let Some(intensity_str) = iter.next() {
481 intensity = f32::from_str(intensity_str).unwrap();
482 print!("\n intensity {} ", intensity);
483 }
484 } else if next == "color" {
485 let mut color_r: Float = 0.0;
486 let mut color_g: Float = 0.0;
487 let mut color_b: Float = 0.0;
488 if let Some(color_str) = iter.next() {
489 color_r = f32::from_str(color_str).unwrap();
490 }
491 if let Some(color_str) = iter.next() {
492 color_g = f32::from_str(color_str).unwrap();
493 }
494 if let Some(color_str) = iter.next() {
495 color_b = f32::from_str(color_str).unwrap();
496 }
497 color = Spectrum::rgb(color_r, color_g, color_b);
498 print!("\n color {} {} {} ", color_r, color_g, color_b);
499 }
500 } else if node_type == "spot_light" {
501 if next == "intensity" {
502 if let Some(intensity_str) = iter.next() {
503 intensity = f32::from_str(intensity_str).unwrap();
504 print!("\n intensity {} ", intensity);
505 }
506 } else if next == "color" {
507 let mut color_r: Float = 0.0;
508 let mut color_g: Float = 0.0;
509 let mut color_b: Float = 0.0;
510 if let Some(color_str) = iter.next() {
511 color_r = f32::from_str(color_str).unwrap();
512 }
513 if let Some(color_str) = iter.next() {
514 color_g = f32::from_str(color_str).unwrap();
515 }
516 if let Some(color_str) = iter.next() {
517 color_b = f32::from_str(color_str).unwrap();
518 }
519 color = Spectrum::rgb(color_r, color_g, color_b);
520 print!("\n color {} {} {} ", color_r, color_g, color_b);
521 } else if next == "cone_angle" {
522 if let Some(cone_angle_str) = iter.next() {
523 cone_angle = f32::from_str(cone_angle_str).unwrap();
524 print!("\n cone_angle {} ", cone_angle);
525 }
526 }
527 } else if node_type == "polymesh" {
528 if next == "vlist" {
529 // parameter_name: vlist
530 // <num_elements>
531 // <num_motionblur_keys>
532 // <data_type>: VECTOR
533 // <elem1> <elem2>
534 // <elem3> <elem4>
535 // ...
536 let num_elements: u32;
537 let num_motionblur_keys: u32;
538 let data_type: String = String::from("VECTOR");
539 let mut elems: Vec<Float> = Vec::new();
540 if let Some(num_elements_str) = iter.next() {
541 num_elements = u32::from_str(num_elements_str).unwrap();
542 if let Some(num_motionblur_keys_str) = iter.next() {
543 num_motionblur_keys =
544 u32::from_str(num_motionblur_keys_str).unwrap();
545 if let Some(data_type_str) = iter.next() {
546 if data_type_str != data_type {
547 panic!("ERROR: {} expected ...", data_type);
548 } else {
549 let expected: u32 =
550 num_elements * num_motionblur_keys * 3;
551 for _i in 0..expected {
552 if let Some(elem_str) = iter.next() {
553 let elem: f32 =
554 f32::from_str(elem_str)
555 .unwrap();
556 elems.push(elem as Float);
557 }
558 }
559 }
560 }
561 }
562 }
563 // print!(
564 // "\n vlist {} {} VECTOR ... ",
565 // num_elements, num_motionblur_keys
566 // );
567 // print!("\n {:?}", elems);
568 // TriangleMesh
569 let mut x: Float = 0.0;
570 let mut y: Float = 0.0;
571 let mut z;
572 let mut p: Vec<Point3f> = Vec::new();
573 for i in 0..elems.len() {
574 if i % 3 == 0 {
575 x = elems[i];
576 } else if i % 3 == 1 {
577 y = elems[i];
578 } else {
579 // i % 3 == 2
580 z = elems[i];
581 // store as Point3f
582 p.push(Point3f { x, y, z });
583 }
584 }
585 // transform mesh vertices to world space
586 p_ws = Vec::new();
587 let n_vertices: usize = p.len();
588 for i in 0..n_vertices {
589 p_ws.push(obj_to_world.transform_point(&p[i]));
590 }
591 p_ws_len = p_ws.len();
592 // print info
593 // println!("");
594 // for point in p {
595 // println!(" {:?}", point);
596 // }
597 } else if next == "nsides" {
598 nsides = Vec::new();
599 loop {
600 let mut is_int: bool = false;
601 // check if next string can be converted to u32
602 if let Some(ref check_for_int_str) = iter.peek() {
603 if u32::from_str(check_for_int_str).is_ok() {
604 is_int = true;
605 } else {
606 // if not ... break the loop
607 break;
608 }
609 }
610 // if we can convert use next()
611 if is_int {
612 if let Some(nside_str) = iter.next() {
613 let nside: u32 =
614 u32::from_str(nside_str).unwrap();
615 nsides.push(nside);
616 }
617 }
618 }
619 let mut followed_by_uint: bool = false;
620 // check if next string is 'UINT' (or not)
621 if let Some(check_for_uint_str) = iter.peek() {
622 if *check_for_uint_str == "UINT" {
623 followed_by_uint = true;
624 }
625 }
626 if followed_by_uint {
627 // skip next (we checked already)
628 iter.next();
629 let num_elements = nsides[0];
630 let num_motionblur_keys = nsides[1];
631 // print!(
632 // "\n nsides {} {} UINT ... ",
633 // num_elements, num_motionblur_keys
634 // );
635 let expected: u32 = num_elements * num_motionblur_keys;
636 nsides = Vec::new();
637 for _i in 0..expected {
638 if let Some(nside_str) = iter.next() {
639 let nside: u32 =
640 u32::from_str(nside_str).unwrap();
641 nsides.push(nside);
642 }
643 }
644 } else {
645 // print!("\n nsides ... ");
646 }
647 // print!("\n {:?} ", nsides);
648 } else if next == "vidxs" {
649 // parameter_name: vidxs
650 // <num_elements>
651 // <num_motionblur_keys>
652 // <data_type>: UINT
653 // <elem1> <elem2>
654 // <elem3> <elem4>
655 // ...
656 let num_elements: u32;
657 let num_motionblur_keys: u32;
658 let data_type: String = String::from("UINT");
659 vi = Vec::new();
660 if let Some(num_elements_str) = iter.next() {
661 num_elements = u32::from_str(num_elements_str).unwrap();
662 if let Some(num_motionblur_keys_str) = iter.next() {
663 num_motionblur_keys =
664 u32::from_str(num_motionblur_keys_str).unwrap();
665 if let Some(data_type_str) = iter.next() {
666 if data_type_str != data_type {
667 panic!("ERROR: {} expected ...", data_type);
668 } else {
669 let expected: u32 =
670 num_elements * num_motionblur_keys;
671 for _i in 0..expected {
672 if let Some(elem_str) = iter.next() {
673 let elem: u32 =
674 u32::from_str(elem_str)
675 .unwrap();
676 vi.push(elem);
677 }
678 }
679 }
680 }
681 }
682 }
683 // print!(
684 // "\n vidxs {} {} UINT ... ",
685 // num_elements, num_motionblur_keys
686 // );
687 // print!("\n {:?} ", vi);
688 } else if next == "shidxs" {
689 shidxs = Vec::new();
690 loop {
691 let mut is_int: bool = false;
692 // check if next string can be converted to u32
693 if let Some(ref check_for_int_str) = iter.peek() {
694 if u32::from_str(check_for_int_str).is_ok() {
695 is_int = true;
696 } else {
697 // if not ... break the loop
698 break;
699 }
700 }
701 // if we can convert use next()
702 if is_int {
703 if let Some(shidx_str) = iter.next() {
704 let shidx: u32 =
705 u32::from_str(shidx_str).unwrap();
706 shidxs.push(shidx);
707 }
708 }
709 }
710 let mut followed_by_byte: bool = false;
711 // check if next string is 'BYTE' (or not)
712 if let Some(check_for_uint_str) = iter.peek() {
713 if *check_for_uint_str == "BYTE" {
714 followed_by_byte = true;
715 }
716 }
717 if followed_by_byte {
718 // skip next (we checked already)
719 iter.next();
720 let num_elements = shidxs[0];
721 let num_motionblur_keys = shidxs[1];
722 // print!(
723 // "\n shidxs {} {} BYTE ... ",
724 // num_elements, num_motionblur_keys
725 // );
726 let expected: u32 = num_elements * num_motionblur_keys;
727 shidxs = Vec::new();
728 for _i in 0..expected {
729 if let Some(shidx_str) = iter.next() {
730 let shidx: u32 =
731 u32::from_str(shidx_str).unwrap();
732 shidxs.push(shidx);
733 }
734 }
735 } else {
736 // print!("\n shidxs ... ");
737 }
738 // print!("\n {:?} ", shidxs);
739 } else if next == "shader" {
740 shader_names = get_shader_names(&mut iter);
741 // print!("\n {:?} ", shader_names);
742 }
743 } else if node_type == "disk" {
744 if next == "radius" {
745 if let Some(radius_str) = iter.next() {
746 radius = f32::from_str(radius_str).unwrap();
747 // print!("\n radius {} ", radius);
748 }
749 } else if next == "hole" {
750 if let Some(hole_str) = iter.next() {
751 hole = f32::from_str(hole_str).unwrap();
752 // print!("\n hole {} ", hole);
753 }
754 } else if next == "shader" {
755 shader_names = get_shader_names(&mut iter);
756 // print!("\n {:?} ", shader_names);
757 }
758 } else if node_type == "sphere" {
759 if next == "radius" {
760 if let Some(radius_str) = iter.next() {
761 radius = f32::from_str(radius_str).unwrap();
762 // print!("\n radius {} ", radius);
763 }
764 } else if next == "shader" {
765 shader_names = get_shader_names(&mut iter);
766 // print!("\n {:?} ", shader_names);
767 }
768 } else if node_type == "cylinder" {
769 if next == "radius" {
770 if let Some(radius_str) = iter.next() {
771 radius = f32::from_str(radius_str).unwrap();
772 // print!("\n radius {} ", radius);
773 }
774 } else if next == "shader" {
775 shader_names = get_shader_names(&mut iter);
776 // print!("\n {:?} ", shader_names);
777 }
778 } else if node_type == "standard_surface" {
779 if next == "base_color" {
780 let mut color_r: Float = 0.0;
781 let mut color_g: Float = 0.0;
782 let mut color_b: Float = 0.0;
783 if let Some(color_str) = iter.next() {
784 color_r = f32::from_str(color_str).unwrap();
785 }
786 if let Some(color_str) = iter.next() {
787 color_g = f32::from_str(color_str).unwrap();
788 }
789 if let Some(color_str) = iter.next() {
790 color_b = f32::from_str(color_str).unwrap();
791 }
792 base_color = Spectrum::rgb(color_r, color_g, color_b);
793 print!(
794 "\n base_color {} {} {} ",
795 color_r, color_g, color_b
796 );
797 } else if next == "specular_color" {
798 let mut color_r: Float = 0.0;
799 let mut color_g: Float = 0.0;
800 let mut color_b: Float = 0.0;
801 if let Some(color_str) = iter.next() {
802 color_r = f32::from_str(color_str).unwrap();
803 }
804 if let Some(color_str) = iter.next() {
805 color_g = f32::from_str(color_str).unwrap();
806 }
807 if let Some(color_str) = iter.next() {
808 color_b = f32::from_str(color_str).unwrap();
809 }
810 specular_color = Spectrum::rgb(color_r, color_g, color_b);
811 print!(
812 "\n specular_color {} {} {} ",
813 color_r, color_g, color_b
814 );
815 } else if next == "specular_roughness" {
816 if let Some(specular_roughness_str) = iter.next() {
817 specular_roughness =
818 f32::from_str(specular_roughness_str).unwrap();
819 print!("\n specular_roughness {} ", specular_roughness);
820 }
821 } else if next == "metalness" {
822 if let Some(metalness_str) = iter.next() {
823 metalness = f32::from_str(metalness_str).unwrap();
824 print!("\n metalness {} ", metalness);
825 }
826 }
827 }
828 } else {
829 // by node type
830 if node_type == "options" {
831 println!("}}");
832 } else if node_type == "persp_camera" && node_name == render_camera
833 {
834 // println!("}}");
835 } else if node_type == "gaussian_filter" {
836 // println!("}}");
837 } else if node_type == "mesh_light" {
838 match named_primitives.get_mut(mesh.as_str()) {
839 Some((_shader_names, prims_vec)) => {
840 // for i in 0..prims.len() {
841 // let mut prim = &mut prims[i];
842 for (_shader_idx, prim) in prims_vec.iter_mut() {
843 let prim_opt = Arc::get_mut(prim);
844 if prim_opt.is_some() {
845 let prim = prim_opt.unwrap();
846 match prim {
847 Primitive::Geometric(primitive) => {
848 let shape = primitive.shape.clone();
849 let mi: MediumInterface =
850 MediumInterface::default();
851 let l_emit: Spectrum =
852 color * intensity;
853 let two_sided: bool = false;
854 let area_light: Arc<Light> = Arc::new(
855 Light::DiffuseArea(Box::new(
856 DiffuseAreaLight::new(
857 &cur_transform,
858 &mi,
859 &l_emit,
860 samples,
861 shape,
862 two_sided,
863 ),
864 )),
865 );
866 lights.push(area_light.clone());
867 primitive.area_light =
868 Some(area_light.clone());
869 }
870 _ => {}
871 }
872 } else {
873 println!("WARNING: no pointer from primitive to area light");
874 }
875 }
876 }
877 None => {
878 panic!("ERROR: mesh_light({:?}) without mesh", mesh);
879 }
880 }
881 // println!("}}");
882 } else if node_type == "point_light" {
883 let mi: MediumInterface = MediumInterface::default();
884 let point_light = Arc::new(Light::Point(Box::new(
885 PointLight::new(&cur_transform, &mi, &(color * intensity)),
886 )));
887 lights.push(point_light);
888 println!("}}");
889 } else if node_type == "spot_light" {
890 let mi: MediumInterface = MediumInterface::default();
891 let spot_light =
892 Arc::new(Light::Spot(Box::new(SpotLight::new(
893 &cur_transform,
894 &mi,
895 &(color * intensity),
896 cone_angle,
897 cone_angle - cone_delta_angle,
898 ))));
899 lights.push(spot_light);
900 println!("}}");
901 } else if node_type == "polymesh" {
902 // make sure there are no out of-bounds vertex indices
903 for i in 0..vi.len() {
904 if vi[i] as usize >= p_ws_len {
905 panic!(
906 "trianglemesh has out of-bounds vertex index {} ({} \"P\" values were given)",
907 vi[i],
908 p_ws_len
909 );
910 }
911 }
912 // convert quads to triangles
913 let mut vi_tri: Vec<u32> = Vec::new();
914 let mut shidxs_tri: Vec<u32> = Vec::new();
915 let mut count_vi: usize = 0;
916 let mut count_shidxs: usize = 0;
917 for i in 0..nsides.len() {
918 let nside = nsides[i];
919 if nside == 3 {
920 // triangle
921 vi_tri.push(vi[count_vi]);
922 count_vi += 1;
923 vi_tri.push(vi[count_vi]);
924 count_vi += 1;
925 vi_tri.push(vi[count_vi]);
926 count_vi += 1;
927 shidxs_tri.push(shidxs[count_shidxs]);
928 count_shidxs += 1;
929 } else if nside == 4 {
930 // quad gets split into 2 triangles
931 vi_tri.push(vi[count_vi]);
932 vi_tri.push(vi[count_vi + 1]);
933 vi_tri.push(vi[count_vi + 2]);
934 vi_tri.push(vi[count_vi]);
935 vi_tri.push(vi[count_vi + 2]);
936 vi_tri.push(vi[count_vi + 3]);
937 count_vi += 4;
938 shidxs_tri.push(shidxs[count_shidxs]);
939 shidxs_tri.push(shidxs[count_shidxs]);
940 count_shidxs += 1;
941 } else {
942 panic!("{}-sided poygons are not supported", nside);
943 }
944 }
945 let n_triangles: usize = vi_tri.len() / 3;
946 assert!(shidxs_tri.len() == n_triangles);
947 // TriangleMesh
948 let mut shapes: Vec<Arc<Shape>> = Vec::new();
949 let s_ws: Vec<Vector3f> = Vec::new();
950 let n_ws: Vec<Normal3f> = Vec::new();
951 let uvs: Vec<Point2f> = Vec::new();
952 // vertex indices are expected as usize, not u32
953 let mut vertex_indices: Vec<u32> = Vec::new();
954 for i in 0..vi_tri.len() {
955 vertex_indices.push(vi_tri[i] as u32);
956 }
957 let mesh = Arc::new(TriangleMesh::new(
958 obj_to_world,
959 world_to_obj,
960 false, // reverse_orientation,
961 n_triangles.try_into().unwrap(),
962 vertex_indices,
963 p_ws_len as u32,
964 p_ws.clone(), // in world space
965 s_ws, // in world space
966 n_ws, // in world space
967 uvs,
968 None,
969 None,
970 ));
971 for id in 0..mesh.n_triangles {
972 let triangle =
973 Arc::new(Shape::Trngl(Triangle::new(mesh.clone(), id)));
974 shapes.push(triangle.clone());
975 }
976 let mi: MediumInterface = MediumInterface::default();
977 let mut prims: Vec<(u32, Arc<Primitive>)> = Vec::new();
978 assert!(shidxs_tri.len() == shapes.len());
979 for i in 0..shapes.len() {
980 let shape = &shapes[i];
981 let shidx = shidxs_tri[i];
982 let geo_prim = Arc::new(Primitive::Geometric(Box::new(
983 GeometricPrimitive::new(
984 shape.clone(),
985 None,
986 None,
987 Some(Arc::new(mi.clone())),
988 ),
989 )));
990 prims.push((shidx, geo_prim.clone()));
991 }
992 named_primitives
993 .insert(node_name.clone(), (shader_names.clone(), prims));
994 // println!("}}");
995 } else if node_type == "disk" {
996 let mut shapes: Vec<Arc<Shape>> = Vec::new();
997 let disk = Arc::new(Shape::Dsk(Disk::new(
998 obj_to_world,
999 world_to_obj,
1000 false,
1001 0.0 as Float, // height
1002 radius,
1003 hole,
1004 360.0 as Float, // phi_max
1005 )));
1006 shapes.push(disk.clone());
1007 let mi: MediumInterface = MediumInterface::default();
1008 let mut prims: Vec<(u32, Arc<Primitive>)> = Vec::new();
1009 let shidx: u32 = 0;
1010 for i in 0..shapes.len() {
1011 let shape = &shapes[i];
1012 let geo_prim = Arc::new(Primitive::Geometric(Box::new(
1013 GeometricPrimitive::new(
1014 shape.clone(),
1015 None,
1016 None,
1017 Some(Arc::new(mi.clone())),
1018 ),
1019 )));
1020 prims.push((shidx, geo_prim.clone()));
1021 }
1022 named_primitives
1023 .insert(node_name.clone(), (shader_names.clone(), prims));
1024 // println!("}}");
1025 } else if node_type == "sphere" {
1026 let mut shapes: Vec<Arc<Shape>> = Vec::new();
1027 let sphere = Arc::new(Shape::Sphr(Sphere::new(
1028 obj_to_world,
1029 world_to_obj,
1030 false,
1031 radius,
1032 -radius, // z_min
1033 radius, // z_max
1034 360.0 as Float, // phi_max
1035 )));
1036 shapes.push(sphere.clone());
1037 let mi: MediumInterface = MediumInterface::default();
1038 let mut prims: Vec<(u32, Arc<Primitive>)> = Vec::new();
1039 let shidx: u32 = 0;
1040 for i in 0..shapes.len() {
1041 let shape = &shapes[i];
1042 let geo_prim = Arc::new(Primitive::Geometric(Box::new(
1043 GeometricPrimitive::new(
1044 shape.clone(),
1045 None,
1046 None,
1047 Some(Arc::new(mi.clone())),
1048 ),
1049 )));
1050 prims.push((shidx, geo_prim.clone()));
1051 }
1052 named_primitives
1053 .insert(node_name.clone(), (shader_names.clone(), prims));
1054 // println!("}}");
1055 } else if node_type == "cylinder" {
1056 let mut shapes: Vec<Arc<Shape>> = Vec::new();
1057 // TODO: assumption about z_min and z_max
1058 let cylinder = Arc::new(Shape::Clndr(Cylinder::new(
1059 obj_to_world,
1060 world_to_obj,
1061 false,
1062 radius,
1063 0.0 as Float, // z_min
1064 radius, // z_max
1065 360.0 as Float, // phi_max
1066 )));
1067 shapes.push(cylinder.clone());
1068 let mi: MediumInterface = MediumInterface::default();
1069 let mut prims: Vec<(u32, Arc<Primitive>)> = Vec::new();
1070 let shidx: u32 = 0;
1071 for i in 0..shapes.len() {
1072 let shape = &shapes[i];
1073 let geo_prim = Arc::new(Primitive::Geometric(Box::new(
1074 GeometricPrimitive::new(
1075 shape.clone(),
1076 None,
1077 None,
1078 Some(Arc::new(mi.clone())),
1079 ),
1080 )));
1081 prims.push((shidx, geo_prim.clone()));
1082 }
1083 named_primitives
1084 .insert(node_name.clone(), (shader_names.clone(), prims));
1085 // println!("}}");
1086 } else if node_type == "standard_surface" {
1087 if metalness > 0.0 as Float {
1088 if metalness == 1.0 as Float {
1089 let kr = Arc::new(ConstantTexture::new(specular_color));
1090 let mirror = Arc::new(Material::Mirror(Box::new(
1091 MirrorMaterial::new(kr, None),
1092 )));
1093 named_materials.insert(node_name.clone(), mirror);
1094 } else {
1095 let copper_n: Spectrum = Spectrum::from_sampled(
1096 &COPPER_WAVELENGTHS,
1097 &COPPER_N,
1098 COPPER_SAMPLES as i32,
1099 );
1100 let eta: Arc<dyn Texture<Spectrum> + Send + Sync> =
1101 Arc::new(ConstantTexture::new(copper_n));
1102 let copper_k: Spectrum = Spectrum::from_sampled(
1103 &COPPER_WAVELENGTHS,
1104 &COPPER_K,
1105 COPPER_SAMPLES as i32,
1106 );
1107 let k: Arc<dyn Texture<Spectrum> + Send + Sync> =
1108 Arc::new(ConstantTexture::new(copper_k));
1109 let roughness = Arc::new(ConstantTexture::new(
1110 specular_roughness as Float,
1111 ));
1112 let remap_roughness: bool = true;
1113 let metal = Arc::new(Material::Metal(Box::new(
1114 MetalMaterial::new(
1115 eta,
1116 k,
1117 roughness,
1118 None,
1119 None,
1120 None,
1121 remap_roughness,
1122 ),
1123 )));
1124 named_materials.insert(node_name.clone(), metal);
1125 }
1126 } else {
1127 // TODO: create a matte material for now
1128 let kd = Arc::new(ConstantTexture::new(base_color));
1129 let sigma = Arc::new(ConstantTexture::new(0.0 as Float));
1130 let matte = Arc::new(Material::Matte(Box::new(
1131 MatteMaterial::new(kd, sigma, None),
1132 )));
1133 named_materials.insert(node_name.clone(), matte);
1134 }
1135 // reset
1136 base_color = Spectrum::new(0.5 as Float);
1137 specular_color = Spectrum::new(1.0 as Float);
1138 specular_roughness = 0.01 as Float;
1139 metalness = 0.0 as Float;
1140 println!("}}");
1141 }
1142 }
1143 } else {
1144 break;
1145 }
1146 }
1147 }
1148 _ => println!("TODO: {:?}", inner_pair.as_rule()),
1149 }
1150 }
1151 }
1152 println!("render_camera = {:?} ", render_camera);
1153 println!("fov = {:?} ", fov);
1154 println!("filter_name = {:?}", filter_name);
1155 println!("filter_width = {:?}", filter_width);
1156 println!("max_depth = {:?}", max_depth);
1157 for value in named_primitives.values_mut() {
1158 let (shader_names, tuple_vec) = value;
1159 // let mut count: usize = 0;
1160 for (shader_idx, prim) in tuple_vec.iter_mut() {
1161 if shader_names.len() > 0 as usize {
1162 let shader_name: String = shader_names[*shader_idx as usize].clone();
1163 if let Some(named_material) = named_materials.get(&shader_name) {
1164 // println!("#{}: {} -> {:?}", count, shader_idx, shader_name);
1165 let prim_opt = Arc::get_mut(prim);
1166 if prim_opt.is_some() {
1167 let prim = prim_opt.unwrap();
1168 match prim {
1169 Primitive::Geometric(primitive) => {
1170 primitive.material = Some(named_material.clone());
1171 }
1172 _ => {}
1173 }
1174 } else {
1175 println!("WARNING: Can't replace GeometricPrimitive.material");
1176 }
1177 }
1178 } else {
1179 println!("WARNING: No shader names");
1180 }
1181 primitives.push(prim.clone());
1182 // count += 1;
1183 }
1184 }
1185 println!("samples_per_pixel = {:?}", samples_per_pixel);
1186 println!("number of lights = {:?}", lights.len());
1187 println!("number of primitives = {:?}", primitives.len());
1188 let some_integrator: Option<Box<Integrator>> = make_path_integrator(
1189 filter_width,
1190 xres,
1191 yres,
1192 fov,
1193 animated_cam_to_world,
1194 max_depth,
1195 samples_per_pixel as i32,
1196 );
1197 if let Some(mut integrator) = some_integrator {
1198 let scene = make_scene(&primitives, lights);
1199 let num_threads: u8 = num_cpus::get() as u8;
1200 integrator.render(&scene, num_threads);
1201 } else {
1202 panic!("Unable to create integrator.");
1203 }
1204 Ok(())
1205}
pub fn is_black(&self) -> bool
pub fn sqrt(&self) -> RGBSpectrum
pub fn exp(&self) -> RGBSpectrum
Sourcepub fn clamp(&self, low: Float, high: Float) -> RGBSpectrum
pub fn clamp(&self, low: Float, high: Float) -> RGBSpectrum
Clamp spectrum to lie between the values low and high. Use (0.0 as Float, std::f32::INFINITY as Float) if there are no specific values.
pub fn max_component_value(&self) -> Float
pub fn has_nans(&self) -> bool
Trait Implementations§
Source§impl Add for RGBSpectrum
impl Add for RGBSpectrum
Source§type Output = RGBSpectrum
type Output = RGBSpectrum
The resulting type after applying the
+
operator.Source§fn add(self, rhs: RGBSpectrum) -> RGBSpectrum
fn add(self, rhs: RGBSpectrum) -> RGBSpectrum
Performs the
+
operation. Read moreSource§impl AddAssign for RGBSpectrum
impl AddAssign for RGBSpectrum
Source§fn add_assign(&mut self, rhs: RGBSpectrum)
fn add_assign(&mut self, rhs: RGBSpectrum)
Performs the
+=
operation. Read moreSource§impl Clone for RGBSpectrum
impl Clone for RGBSpectrum
Source§fn clone(&self) -> RGBSpectrum
fn clone(&self) -> RGBSpectrum
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for RGBSpectrum
impl Debug for RGBSpectrum
Source§impl Default for RGBSpectrum
impl Default for RGBSpectrum
Source§fn default() -> RGBSpectrum
fn default() -> RGBSpectrum
Returns the “default value” for a type. Read more
Source§impl Div<f32> for RGBSpectrum
impl Div<f32> for RGBSpectrum
Source§type Output = RGBSpectrum
type Output = RGBSpectrum
The resulting type after applying the
/
operator.Source§impl Div for RGBSpectrum
impl Div for RGBSpectrum
Source§type Output = RGBSpectrum
type Output = RGBSpectrum
The resulting type after applying the
/
operator.Source§fn div(self, rhs: RGBSpectrum) -> RGBSpectrum
fn div(self, rhs: RGBSpectrum) -> RGBSpectrum
Performs the
/
operation. Read moreSource§impl DivAssign<f32> for RGBSpectrum
impl DivAssign<f32> for RGBSpectrum
Source§fn div_assign(&mut self, rhs: Float)
fn div_assign(&mut self, rhs: Float)
Performs the
/=
operation. Read moreSource§impl From<f32> for RGBSpectrum
impl From<f32> for RGBSpectrum
Source§impl ImageTextureConvert<RGBSpectrum> for ImageTexture<Spectrum>
impl ImageTextureConvert<RGBSpectrum> for ImageTexture<Spectrum>
fn convert_out(from: &Spectrum, to: &mut Spectrum)
Source§impl Index<RGBEnum> for RGBSpectrum
impl Index<RGBEnum> for RGBSpectrum
Source§impl IndexMut<RGBEnum> for RGBSpectrum
impl IndexMut<RGBEnum> for RGBSpectrum
Source§impl Mul<RGBSpectrum> for Float
impl Mul<RGBSpectrum> for Float
Source§type Output = RGBSpectrum
type Output = RGBSpectrum
The resulting type after applying the
*
operator.Source§fn mul(self, rhs: RGBSpectrum) -> RGBSpectrum
fn mul(self, rhs: RGBSpectrum) -> RGBSpectrum
Performs the
*
operation. Read moreSource§impl Mul<f32> for RGBSpectrum
impl Mul<f32> for RGBSpectrum
Source§type Output = RGBSpectrum
type Output = RGBSpectrum
The resulting type after applying the
*
operator.Source§impl Mul for RGBSpectrum
impl Mul for RGBSpectrum
Source§type Output = RGBSpectrum
type Output = RGBSpectrum
The resulting type after applying the
*
operator.Source§fn mul(self, rhs: RGBSpectrum) -> RGBSpectrum
fn mul(self, rhs: RGBSpectrum) -> RGBSpectrum
Performs the
*
operation. Read moreSource§impl MulAssign for RGBSpectrum
impl MulAssign for RGBSpectrum
Source§fn mul_assign(&mut self, rhs: RGBSpectrum)
fn mul_assign(&mut self, rhs: RGBSpectrum)
Performs the
*=
operation. Read moreSource§impl Neg for RGBSpectrum
impl Neg for RGBSpectrum
Source§type Output = RGBSpectrum
type Output = RGBSpectrum
The resulting type after applying the
-
operator.Source§fn neg(self) -> RGBSpectrum
fn neg(self) -> RGBSpectrum
Performs the unary
-
operation. Read moreSource§impl PartialEq for RGBSpectrum
impl PartialEq for RGBSpectrum
Source§impl Sub for RGBSpectrum
impl Sub for RGBSpectrum
Source§type Output = RGBSpectrum
type Output = RGBSpectrum
The resulting type after applying the
-
operator.Source§fn sub(self, rhs: RGBSpectrum) -> RGBSpectrum
fn sub(self, rhs: RGBSpectrum) -> RGBSpectrum
Performs the
-
operation. Read moreSource§impl Texture<RGBSpectrum> for ImageTexture<Spectrum>
impl Texture<RGBSpectrum> for ImageTexture<Spectrum>
fn evaluate(&self, si: &SurfaceInteraction<'_>) -> Spectrum
Source§impl Texture<RGBSpectrum> for MarbleTexture
impl Texture<RGBSpectrum> for MarbleTexture
fn evaluate(&self, si: &SurfaceInteraction<'_>) -> Spectrum
Source§impl Zero for RGBSpectrum
impl Zero for RGBSpectrum
impl Copy for RGBSpectrum
Auto Trait Implementations§
impl Freeze for RGBSpectrum
impl RefUnwindSafe for RGBSpectrum
impl Send for RGBSpectrum
impl Sync for RGBSpectrum
impl Unpin for RGBSpectrum
impl UnwindSafe for RGBSpectrum
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
Read this value from the supplied reader. Same as
ReadEndian::read_from_little_endian()
.