maaaath :33

This commit is contained in:
CDaut 2025-10-01 14:40:15 +02:00
parent 09bdaed3f5
commit c6290f93fd
Signed by: clara
GPG key ID: 223391B52FAD4463
4 changed files with 100 additions and 16 deletions

View file

@ -6,8 +6,8 @@ use alloc::vec::Vec;
use uefi::Error; use uefi::Error;
pub struct Buffer { pub struct Buffer {
width: usize, pub width: usize,
height: usize, pub height: usize,
pixels: Vec<BltPixel>, pixels: Vec<BltPixel>,
} }
@ -37,11 +37,7 @@ impl Buffer {
} }
/// Update only a pixel to the framebuffer. /// Update only a pixel to the framebuffer.
fn blit_pixel( fn blit_pixel(&self, gop: &mut GraphicsOutput, coords: (usize, usize)) -> Result<(), Error> {
&self,
gop: &mut GraphicsOutput,
coords: (usize, usize),
) -> Result<(), Error> {
gop.blt(BltOp::BufferToVideo { gop.blt(BltOp::BufferToVideo {
buffer: &self.pixels, buffer: &self.pixels,
src: BltRegion::SubRectangle { src: BltRegion::SubRectangle {

View file

@ -1,16 +1,17 @@
#![no_main] #![no_main]
#![no_std] #![no_std]
mod buffer; mod buffer;
mod ray;
mod renderer;
extern crate alloc; extern crate alloc;
use crate::buffer::Buffer;
use uefi::allocator::Allocator; use uefi::allocator::Allocator;
use uefi::boot::ScopedProtocol; use uefi::boot::ScopedProtocol;
use uefi::prelude::*; use uefi::prelude::*;
use uefi::proto::console::gop::GraphicsOutput; use uefi::proto::console::gop::GraphicsOutput;
use uefi::{boot, Result}; use uefi::{Result, boot};
use crate::buffer::Buffer;
#[global_allocator] #[global_allocator]
static ALLOCATOR: Allocator = uefi::allocator::Allocator; static ALLOCATOR: Allocator = uefi::allocator::Allocator;
@ -21,8 +22,6 @@ fn init_gop() -> Result<ScopedProtocol<GraphicsOutput>> {
boot::open_protocol_exclusive::<GraphicsOutput>(gop_handle) boot::open_protocol_exclusive::<GraphicsOutput>(gop_handle)
} }
fn init_buffer(gop: &GraphicsOutput) -> Buffer { fn init_buffer(gop: &GraphicsOutput) -> Buffer {
// Create a buffer to draw into. // Create a buffer to draw into.
let (width, height) = gop.current_mode_info().resolution(); let (width, height) = gop.current_mode_info().resolution();

58
raytracer/src/ray.rs Normal file
View file

@ -0,0 +1,58 @@
use alloc::vec::Vec;
use cgmath::{Matrix4, Vector3, Vector4, num_traits::Num};
pub struct Ray<T> {
pub(crate) origin: Vector3<T>,
pub(crate) direction: Vector3<T>,
}
pub fn construct_primary_rays(
(width, height): (usize, usize),
(pixel_x_coord, pixel_y_coord): (usize, usize),
cam_to_world_matrix: &Matrix4<f32>,
focal_length: f32,
rays_per_pixel: usize,
) -> Vec<Ray> {
let mut rays: Vec<Ray> = Vec::with_capacity(rays_per_pixel);
//generate all rays for this pixel and add them to the rays vector
for _ in 0..rays_per_pixel {
rays.push(generate_single_primary_ray(
(width, height),
cam_to_world_matrix,
focal_length,
pixel_x_coord,
pixel_y_coord,
//no noise
0.0f,
0.0f,
));
}
rays
}
fn generate_single_primary_ray(
(buffer_width, buffer_height): (usize, usize),
cam_to_world_transform: &Matrix4<f32>,
focal_length: f32,
u: usize,
v: usize,
u_offset: f32,
v_offset: f32,
) -> Ray {
//calculate the ray direction and translate it to world space
let direction_camera_space: Vector4<f32> = Vector4::new(
u as f32 - (buffer_width as f32 / 2.0) + u_offset,
v as f32 - (buffer_height as f32 / 2.0) + v_offset,
focal_length,
0.0,
);
let direction_world_space = cam_to_world_transform * direction_camera_space.normalize();
Ray {
origin: cam_to_world_transform.w.truncate(),
direction: direction_world_space.truncate().normalize(),
}
}

31
raytracer/src/renderer.rs Normal file
View file

@ -0,0 +1,31 @@
use alloc::vec::Vec;
use cgmath::{Matrix4, Vector4, Zero};
use crate::{
buffer::Buffer,
ray::{Ray, construct_primary_rays},
};
pub fn render(buffer: Buffer) {
let camera_matrix: Matrix4<f32> = Matrix4 {
x: Vector4::unit_x(),
y: Vector4::unit_y(),
z: Vector4::unit_z(),
w: Vector4::zero(),
};
let focal_length = 1.0f;
//this reeeeally wants to run in parallel…
(0..buffer.width).for_each(|x| {
(0..buffer.height).for_each(|y| {
let rays: Vec<Ray<f32>> = construct_primary_rays(
(buffer.width, buffer.height),
(x, y),
camera_matrix,
focal_length,
1,
);
});
});
}