engine/render: ImageOrRef -> Texture

ecs
q3k 2020-07-13 23:14:12 +02:00
parent bf60f262d9
commit c541260a93
2 changed files with 20 additions and 27 deletions

View File

@ -10,7 +10,7 @@ mod util;
mod physics; mod physics;
use render::vulkan::data; use render::vulkan::data;
use render::renderable::{Object, Renderable, ResourceManager, Material, Mesh, ImageRefOrColor}; use render::renderable::{Object, Renderable, ResourceManager, Material, Mesh, Texture};
use physics::color; use physics::color;
fn main() { fn main() {
@ -67,8 +67,8 @@ fn main() {
}; };
let material_cube = rm.add_material(Material::new( let material_cube = rm.add_material(Material::new(
ImageRefOrColor::image(String::from("assets/test-128px.png")), Texture::from_image(String::from("assets/test-128px.png")),
ImageRefOrColor::color(color::LinearF32::new(1.0)), Texture::from_color(color::LinearF32::new(1.0)),
)); ));
let mut renderer = render::Renderer::initialize(); let mut renderer = render::Renderer::initialize();

View File

@ -89,8 +89,8 @@ impl<'a> ResourceManager {
} }
pub struct Material { pub struct Material {
diffuse: ImageRefOrColor<color::XYZ>, diffuse: Texture<color::XYZ>,
roughness: ImageRefOrColor<color::LinearF32>, roughness: Texture<color::LinearF32>,
id: u64, id: u64,
// vulkan cache // vulkan cache
@ -99,8 +99,8 @@ pub struct Material {
impl Material { impl Material {
pub fn new( pub fn new(
diffuse: ImageRefOrColor<color::XYZ>, diffuse: Texture<color::XYZ>,
roughness: ImageRefOrColor<color::LinearF32>, roughness: Texture<color::LinearF32>,
) -> Self { ) -> Self {
Self { Self {
diffuse, diffuse,
@ -298,36 +298,29 @@ impl ChannelLayout for color::LinearF32 {
} }
} }
pub enum ImageRefOrColor<T: ChannelLayout> { pub enum Texture<T: ChannelLayout> {
Color(T), Color(T),
ImageRef(ImageRef), ImageRef(String),
} }
impl<T: ChannelLayout> ImageRefOrColor<T> { impl<T: ChannelLayout> Texture<T> {
fn vulkan_image(&self, graphics_queue: Arc<vd::Queue>) -> Arc<vm::ImmutableImage<vf::Format>> { fn vulkan_image(&self, graphics_queue: Arc<vd::Queue>) -> Arc<vm::ImmutableImage<vf::Format>> {
match self { match self {
ImageRefOrColor::<T>::Color(c) => c.vulkan_from_value(graphics_queue), Texture::<T>::Color(c) => c.vulkan_from_value(graphics_queue),
ImageRefOrColor::<T>::ImageRef(r) => T::vulkan_from_image(r.load(), graphics_queue), Texture::<T>::ImageRef(r) => {
let path = &file::resource_path(r.clone());
let img = Arc::new(image::open(path).unwrap());
T::vulkan_from_image(img, graphics_queue)
},
} }
} }
pub fn color(color: T) -> Self { pub fn from_color(color: T) -> Self {
ImageRefOrColor::<T>::Color(color) Texture::<T>::Color(color)
} }
pub fn image(name: String) -> Self { pub fn from_image(name: String) -> Self {
ImageRefOrColor::<T>::ImageRef(ImageRef{ name }) Texture::<T>::ImageRef(name)
}
}
pub struct ImageRef {
name: String,
}
impl ImageRef {
fn load (&self) -> Arc<image::DynamicImage> {
let path = &file::resource_path(self.name.clone());
Arc::new(image::open(path).unwrap())
} }
} }