Replaced RGB888 with BGR888 since in sdl2-rs they seem to be having a reversed effect

This commit is contained in:
Ishan Jain 2019-03-08 22:59:21 +05:30
parent 7cba8998c5
commit b368aad313
6 changed files with 20 additions and 27 deletions

View File

@ -17,7 +17,7 @@ pub trait Demo {
.expect("error in writing file header"); .expect("error in writing file header");
for i in buf.chunks(4) { for i in buf.chunks(4) {
match file.write(&format!("{} {} {}\n", i[0], i[1], i[2]).as_bytes()) { match file.write(format!("{} {} {}\n", i[0], i[1], i[2]).as_bytes()) {
Ok(_) => (), Ok(_) => (),
Err(e) => panic!("couldn't write to {}: {}", self.name(), e.description()), Err(e) => panic!("couldn't write to {}: {}", self.name(), e.description()),
} }

View File

@ -16,7 +16,7 @@ impl crate::Demo for LinearInterpolationY {
let origin = Vec3::new(0.0, 0.0, 0.0); let origin = Vec3::new(0.0, 0.0, 0.0);
let mut offset = 0; let mut offset = 0;
for j in (0..h) { for j in 0..h {
for i in 0..w { for i in 0..w {
// relative offsets // relative offsets
// current position to total width/length // current position to total width/length
@ -32,7 +32,6 @@ impl crate::Demo for LinearInterpolationY {
buf[offset] = ir; buf[offset] = ir;
buf[offset + 1] = ig; buf[offset + 1] = ig;
buf[offset + 2] = ib; buf[offset + 2] = ib;
buf[offset + 3] = 0;
offset += 4; offset += 4;
} }
} }

View File

@ -0,0 +1,7 @@
mod linear_interpolation_y;
mod simple_sphere;
mod ppm_example;
pub use linear_interpolation_y::LinearInterpolationY;
pub use simple_sphere::SimpleSphere;
pub use ppm_example::PpmExample;

View File

@ -8,7 +8,7 @@ impl crate::Demo for PpmExample {
fn render(&self, buf: &mut Vec<u8>, w: usize, h: usize) { fn render(&self, buf: &mut Vec<u8>, w: usize, h: usize) {
let mut offset = 0; let mut offset = 0;
for j in (0..h) { for j in 0..h {
for i in 0..w { for i in 0..w {
let color = Vec3::new((i as f32) / (w as f32), (j as f32) / (h as f32), 0.2); let color = Vec3::new((i as f32) / (w as f32), (j as f32) / (h as f32), 0.2);
@ -19,7 +19,6 @@ impl crate::Demo for PpmExample {
buf[offset] = ir; buf[offset] = ir;
buf[offset + 1] = ig; buf[offset + 1] = ig;
buf[offset + 2] = ib; buf[offset + 2] = ib;
buf[offset + 3] = 255;
offset += 4; offset += 4;
} }

View File

@ -31,7 +31,6 @@ impl crate::Demo for SimpleSphere {
buf[offset] = ir; buf[offset] = ir;
buf[offset + 1] = ig; buf[offset + 1] = ig;
buf[offset + 2] = ib; buf[offset + 2] = ib;
buf[offset + 3] = 0;
offset += 4; offset += 4;
} }
} }

View File

@ -1,13 +1,10 @@
mod demo; mod demo;
mod linear_interpolation_y; mod demos;
mod ppm_example;
mod ray; mod ray;
mod simple_sphere;
mod vec3; mod vec3;
use demo::Demo; use demo::Demo;
use linear_interpolation_y::LinearInterpolationY; use demos::{LinearInterpolationY, PpmExample, SimpleSphere};
use ppm_example::PpmExample;
use sdl2::{ use sdl2::{
event::{Event, WindowEvent}, event::{Event, WindowEvent},
keyboard::Keycode, keyboard::Keycode,
@ -17,8 +14,6 @@ use sdl2::{
video::Window, video::Window,
EventPump, Sdl, EventPump, Sdl,
}; };
use simple_sphere::SimpleSphere;
use vec3::Vec3;
fn main() -> Result<(), String> { fn main() -> Result<(), String> {
let sdl_ctx = sdl2::init()?; let sdl_ctx = sdl2::init()?;
@ -45,11 +40,13 @@ fn main() -> Result<(), String> {
let texture_creator = canvas.texture_creator(); let texture_creator = canvas.texture_creator();
let mut texture = texture_creator let mut texture = texture_creator
.create_texture_static(PixelFormatEnum::RGB888, width as u32, height as u32) .create_texture_static(PixelFormatEnum::BGR888, width as u32, height as u32)
.map_err(|e| e.to_string())?; .map_err(|e| e.to_string())?;
let mut active_demo: Box<Demo> = Box::new(LinearInterpolationY); let mut active_demo: Box<Demo> = Box::new(LinearInterpolationY);
//println!("{:?} {:?} {:?}", texture.query(), texture.color_mod(), texture.alpha_mod());
loop { loop {
for event in event_pump.poll_iter() { for event in event_pump.poll_iter() {
match event { match event {
@ -61,27 +58,19 @@ fn main() -> Result<(), String> {
Event::KeyUp { Event::KeyUp {
keycode: Some(Keycode::Num1), keycode: Some(Keycode::Num1),
.. ..
} => { } => active_demo = Box::new(PpmExample),
active_demo = Box::new(PpmExample);
}
Event::KeyUp { Event::KeyUp {
keycode: Some(Keycode::Num2), keycode: Some(Keycode::Num2),
.. ..
} => { } => active_demo = Box::new(LinearInterpolationY),
active_demo = Box::new(LinearInterpolationY);
}
Event::KeyUp { Event::KeyUp {
keycode: Some(Keycode::Num3), keycode: Some(Keycode::Num3),
.. ..
} => { } => active_demo = Box::new(SimpleSphere),
active_demo = Box::new(SimpleSphere);
}
Event::KeyUp { Event::KeyUp {
keycode: Some(Keycode::S), keycode: Some(Keycode::S),
.. ..
} => { } => active_demo.save_as_ppm(&buffer, width, height),
active_demo.save_as_ppm(&buffer, width, height);
}
Event::Window { Event::Window {
win_event: WindowEvent::Resized(w, h), win_event: WindowEvent::Resized(w, h),
.. ..
@ -90,7 +79,7 @@ fn main() -> Result<(), String> {
height = h as usize; height = h as usize;
buffer.resize(width * height * 4, 0); buffer.resize(width * height * 4, 0);
texture = texture_creator texture = texture_creator
.create_texture_static(PixelFormatEnum::RGB888, width as u32, height as u32) .create_texture_static(PixelFormatEnum::BGR888, width as u32, height as u32)
.expect("error in resizing texture"); .expect("error in resizing texture");
} }
_ => {} _ => {}