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