/
main.rs
60 lines (48 loc) · 1.88 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use consts::*;
use model;
use raytracer;
use std::*;
use std::io::{Writer, WriterUtil};
use extra;
fn write_ppm( fname: &str, width: uint, height: uint, pixels: &[raytracer::Color] ){
let writer = io::buffered_file_writer( &Path(fname) ).unwrap();
writer.write_str(fmt!("P6\n%u %u\n255\n", width, height));
for pixel in pixels.iter() {
writer.write([pixel.r, pixel.g, pixel.b]);
};
}
#[main]
fn main()
{
// Get command line args
let args = os::args();
if args.len() != 2u {
io::println("Usage: rustray OBJ");
io::println("");
io::println("For example:");
io::println(" $ wget http://groups.csail.mit.edu/graphics/classes/6.837/F03/models/cow-nonormals.obj");
io::println(" $ ./rustray cow-nonormals.obj");
io::println(" $ gimp oput.ppm");
io::println("");
fail!();
}
let start = extra::time::precise_time_s();
io::println(~"Reading \"" + args[1] + "\"...");
let model = model::read_mesh( args[1] );
let (depth,count) = model::count_kd_tree_nodes( &model.kd_tree );
io::println(fmt!("Done.\nLoaded model.\n\tVerts: %u, Tris: %u\n\tKD-tree depth: %u, #nodes: %u",
model.polys.vertices.len(),
model.polys.indices.len()/3u,
depth, count));
io::print("Tracing rays... ");
let start_tracing = extra::time::precise_time_s();
let pixels = raytracer::generate_raytraced_image(model, FOV, WIDTH, HEIGHT, SAMPLE_GRID_SIZE);
io::println("Done!");
let end_tracing = extra::time::precise_time_s();
let outputfile = "./oput.ppm";
io::print(~"Writing \"" + outputfile + "\"...");
write_ppm( outputfile, WIDTH, HEIGHT, pixels );
io::println("Done!");
let end = extra::time::precise_time_s();
io::print(fmt!("Total time: %3.3fs, of which tracing: %3.3f\n", end - start, end_tracing - start_tracing) );
}