put pixel

This commit is contained in:
gbrochar 2020-12-11 17:11:39 +01:00
parent 10c63aaebf
commit c47fce7e69
5 changed files with 48 additions and 20 deletions

View File

@ -9,5 +9,4 @@ edition = "2018"
[dependencies] [dependencies]
mat4 = "0.2.1" mat4 = "0.2.1"
vec3 = "0.2.1" vec3 = "0.2.1"
arrayvec = "0.5.2"
sdl2 = "0.34.3" sdl2 = "0.34.3"

View File

@ -3,26 +3,60 @@ extern crate sdl2;
use sdl2::event::Event; use sdl2::event::Event;
use sdl2::keyboard::Keycode; use sdl2::keyboard::Keycode;
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::pixels::PixelFormatEnum;
use sdl2::surface::Surface;
use std::time::Duration; use std::time::Duration;
pub fn main() {
fn put_pixel(data: &mut [u8], x: usize, y: usize, color: Color) {
data[y * 3 * 800 + x * 3] = color.r;
data[y * 3 * 800 + x * 3 + 1] = color.g;
data[y * 3 * 800 + x * 3 + 2] = color.b;
}
fn main() {
let sdl_context = sdl2::init().unwrap(); let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap(); let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem let window = video_subsystem
.window("rust-sdl2 demo", 800, 600) .window("rust-sdl2 demo", 800, 600)
.position_centered() .position_centered()
.opengl()
.build() .build()
.unwrap(); .unwrap();
// let surface = Surface::new(512, 512, PixelFormatEnum::RGB24).unwrap();
let mut data = Box::new([255u8; 1_440_000]);
// println!("{:?}", data);
let mut canvas = window.into_canvas().build().unwrap(); let mut canvas = window.into_canvas().build().unwrap();
canvas.set_draw_color(Color::RGB(0, 255, 255)); // let mut canvas = surface.into_canvas().unwrap();
canvas.clear(); //canvas.set_draw_color(Color::RGB(0, 255, 255));
canvas.present(); // let surface = Surface::from_data(&mut data, 800, 600, 3, PixelFormatEnum::RGB24).unwrap();
let texture_creator = canvas.texture_creator();
let mut event_pump = sdl_context.event_pump().unwrap(); let mut event_pump = sdl_context.event_pump().unwrap();
let mut i = 0; let mut i = 0;
'running: loop { 'running: loop {
i = (i + 1) % 255; put_pixel(
canvas.set_draw_color(Color::RGB(i, 64, 255 - i)); &mut *data,
canvas.clear(); i,
i,
Color {
r: 0,
g: 0,
b: 0,
a: 0,
},
);
if i < 599 {
i += 1;
}
// canvas.set_draw_color(Color::RGB(i, 64, 255 - i));
let surface =
Surface::from_data(&mut *data, 800, 600, 2400, PixelFormatEnum::RGB24).unwrap();
let texture = texture_creator
.create_texture_from_surface(surface)
.unwrap();
canvas.copy(&texture, None, None).unwrap();
canvas.present();
for event in event_pump.poll_iter() { for event in event_pump.poll_iter() {
match event { match event {
Event::Quit { .. } Event::Quit { .. }
@ -34,8 +68,6 @@ pub fn main() {
} }
} }
// The rest of the game loop goes here... // The rest of the game loop goes here...
// ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
canvas.present();
::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
} }
} }

View File

@ -1,7 +1,6 @@
use super::Ray; use super::Ray;
use arrayvec::ArrayVec;
pub trait Object { pub trait Object {
fn get_intersection(&self, ray: Ray) -> Option<f64>; fn get_intersection(&self, ray: Ray) -> Option<f64>;
fn get_normal(&self) -> ArrayVec<[f64; 3]>; fn get_normal(&self) -> [f64; 3];
} }

View File

@ -1,6 +1,4 @@
use arrayvec::ArrayVec;
pub struct Ray { pub struct Ray {
pub origin: ArrayVec<[f64; 3]>, pub origin: [f64; 3],
pub direction: ArrayVec<[f64; 3]>, pub direction: [f64; 3],
} }

View File

@ -1,6 +1,6 @@
extern crate vec3;
use super::Object; use super::Object;
use super::Ray; use super::Ray;
use arrayvec::ArrayVec;
struct Sphere {} struct Sphere {}
@ -31,7 +31,7 @@ impl Object for Sphere {
None None
} }
} }
fn get_normal(&self) -> ArrayVec<[f64; 3]> { fn get_normal(&self) -> [f64; 3] {
ArrayVec::<[f64; 3]>::new() vec3::new_zero()
} }
} }