From 8bed95f872d6276b80d5d070a348d295ba486c48 Mon Sep 17 00:00:00 2001 From: numzero Date: Sun, 16 Nov 2025 14:43:31 +0300 Subject: [PATCH] use mutable vectors, to collect into several at once --- src/lib.rs | 68 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index df375e9..926df41 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ use glam::{Mat4, UVec2, Vec3, vec3}; use crate::{ camera::OrbitalCamera, + ray::Ray, render::lines::{LookParams, Mesh, Pipeline, Vertex}, trace::{Scene, Source, Sphere}, }; @@ -189,36 +190,45 @@ impl Core { }; let mut prng = rand_pcg::Pcg64::new(42, 0); - let rays: Vec = (0..10000) - .flat_map(|_| { - let ray = source.make_ray(&mut prng); - if let Some(ray) = scene.trace_ray(ray) { - [ - Vertex { - pos: ray.base - 0.02 * ray.dir, - color: vec3(1., 1., 1.), - }, - Vertex { - pos: ray.base, - color: vec3(0., 1., 0.), - }, - ] - } else { - [ - Vertex { - pos: ray.base, - color: vec3(1., 1., 1.), - }, - Vertex { - pos: ray.base + 0.1 * ray.dir, - color: vec3(1., 0., 0.), - }, - ] - } - }) - .collect(); + let source_rays: Vec = (0..1024).map(|_| source.make_ray(&mut prng)).collect(); + let mut source_ray_display: Vec = Vec::with_capacity(source_rays.len()); + let mut hits: Vec = Vec::with_capacity(source_rays.len()); + for ray in source_rays { + if let Some(hit) = scene.trace_ray(ray) { + hits.push(hit); + source_ray_display.extend([ + Vertex { + pos: ray.base, + color: vec3(1., 1., 1.), + }, + Vertex { + pos: ray.base + 0.1 * ray.dir, + color: vec3(0., 1., 0.), + }, + Vertex { + pos: hit.base - 0.02 * hit.dir, + color: vec3(0., 0., 1.), + }, + Vertex { + pos: hit.base, + color: vec3(1., 1., 1.), + }, + ]); + } else { + source_ray_display.extend([ + Vertex { + pos: ray.base, + color: vec3(1., 1., 1.), + }, + Vertex { + pos: ray.base + 0.1 * ray.dir, + color: vec3(1., 0., 0.), + }, + ]); + } + } self.pipeline - .render(&mut pass, [&Mesh::new(&self.device, &rays)]); + .render(&mut pass, [&Mesh::new(&self.device, &source_ray_display)]); drop(pass); self.queue.submit(std::iter::once(encoder.finish()));