Pregunta

I'm trying to re-implement the business card ray tracer in rust for fun and profit. As far as I can tell all of the logic is in place, but for the life of me I can't figure out how correctly convert from my floating point values to the RGB bytes that the ppm file format expects. In the original code (C++), it looks like this:

printf("%c%c%c",(int)p.x,(int)p.y,(int)p.z);

where p is a 3-d vector of floats representing an RGB value. I didn't have much luck with print!, so after much tweaking I wrote the equivalent expression as:

let mut out = io::stdout();
out.write(&[(p.x as u8),(p.x as u8),(p.x as u8)]);

However, the output is way off (I can post it if you really want, but suffice it to say that the correct output has plenty of non-printable characters, while my version is quite printable). I honestly don't understand the nuances of type conversion for either language. Anyone care to lend a hand?

Edit: full source (note that I use floats as vector attributes)

¿Fue útil?

Solución

Your code seems basically correct (assuming you didn't mean p.x three times). Here's a small program showing printing an RGB struct containing u32s as bytes, which I verified works as expected (prints ABC\n)

struct RGB {
  x: u32,
  y: u32,
  z: u32
}

fn main() {
  let  p = RGB{ x:65, y:66, z:67};
  let mut out = std::io::stdout();
  out.write([p.x as u8, p.y as u8, p.z as u8, 10]);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top