feat(core): added the Object type

This commit is contained in:
antifallobst 2023-11-30 19:10:36 +01:00
parent eddb0693e4
commit 6815077035
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
5 changed files with 38 additions and 8 deletions

1
Cargo.lock generated
View File

@ -265,6 +265,7 @@ dependencies = [
"anyhow", "anyhow",
"image", "image",
"lithium-loader", "lithium-loader",
"lithium-utils",
"slab", "slab",
"thiserror", "thiserror",
"tokio", "tokio",

View File

@ -9,6 +9,7 @@ authors = ["antifallobst"]
[dependencies] [dependencies]
lithium-loader = { path = "lithium-loader" } lithium-loader = { path = "lithium-loader" }
lithium-utils = { path = "lithium-utils" }
anyhow = "1.0.75" anyhow = "1.0.75"
thiserror = "1.0.50" thiserror = "1.0.50"

View File

@ -1,13 +1,16 @@
pub mod render;
pub mod error; pub mod error;
pub mod render;
use crate::render::Renderer; use crate::render::Renderer;
use anyhow::Result; use anyhow::Result;
use lithium_loader::Model;
use lithium_utils::Vector3;
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
let mut renderer = Renderer::new(); let mut renderer = Renderer::new();
let camera = renderer.add_camera(512, 512); let camera = renderer.add_camera(512, 512);
let test = renderer.add_object(Model::test(), Vector3::default());
let image = renderer.render(camera).await?; let image = renderer.render(camera).await?;
image.save("image.png")?; image.save("image.png")?;

View File

@ -1,29 +1,41 @@
pub mod camera; pub mod camera;
pub mod object;
use crate::{error::LithiumError, render::camera::Camera}; use crate::{
error::LithiumError,
render::{camera::Camera, object::Object},
};
use anyhow::Result; use anyhow::Result;
use image::RgbImage; use image::RgbImage;
use lithium_loader::Model; use lithium_loader::Model;
use lithium_utils::Vector3;
use slab::Slab; use slab::Slab;
pub type CameraHandle = usize;
pub type ObjectHandle = usize;
pub struct Renderer { pub struct Renderer {
cameras: Slab<Camera>, cameras: Slab<Camera>,
models: Slab<Model>, objects: Slab<Object>,
} }
impl Renderer { impl Renderer {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
cameras: Slab::new(), cameras: Slab::new(),
models: Slab::new(), objects: Slab::new(),
} }
} }
pub fn add_camera(&mut self, width: u32, height: u32) -> usize { pub fn add_camera(&mut self, width: u32, height: u32) -> CameraHandle {
self.cameras.insert(Camera::new(width, height)) self.cameras.insert(Camera::new(width, height))
} }
pub async fn render(&self, camera: usize) -> Result<image::RgbImage, LithiumError> { pub fn add_object(&mut self, model: Model, position: Vector3) -> ObjectHandle {
self.objects.insert(Object::new(model, position))
}
pub async fn render(&self, camera: CameraHandle) -> Result<RgbImage, LithiumError> {
let camera = self let camera = self
.cameras .cameras
.get(camera) .get(camera)

13
src/render/object.rs Normal file
View File

@ -0,0 +1,13 @@
use lithium_loader::Model;
use lithium_utils::Vector3;
pub struct Object {
model: Model,
position: Vector3,
}
impl Object {
pub fn new(model: Model, position: Vector3) -> Self {
Self { model, position }
}
}