2020-07-23 22:53:33 +00:00
|
|
|
// Copyright 2020 Sergiusz 'q3k' Bazanski <q3k@q3k.org>
|
|
|
|
//
|
|
|
|
// This file is part of Abrasion.
|
|
|
|
//
|
|
|
|
// Abrasion is free software: you can redistribute it and/or modify it under
|
|
|
|
// the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// Abrasion is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
// details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along with
|
|
|
|
// Abrasion. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2019-05-05 14:52:27 +00:00
|
|
|
use log;
|
|
|
|
use env_logger;
|
2020-03-15 15:43:15 +00:00
|
|
|
use std::sync::Arc;
|
2020-03-15 23:24:28 +00:00
|
|
|
use std::time;
|
2020-01-26 00:57:15 +00:00
|
|
|
|
|
|
|
use cgmath as cgm;
|
2019-05-05 14:52:27 +00:00
|
|
|
|
2021-04-04 15:46:29 +00:00
|
|
|
pub mod input;
|
2019-05-05 14:52:27 +00:00
|
|
|
mod render;
|
2020-01-20 01:00:14 +00:00
|
|
|
mod util;
|
2020-07-13 20:58:24 +00:00
|
|
|
mod physics;
|
2019-05-05 14:52:27 +00:00
|
|
|
|
2021-01-13 23:10:58 +00:00
|
|
|
use ecs::{Component, World, Processor};
|
2021-03-22 00:20:35 +00:00
|
|
|
use ecs_macros::Access;
|
2020-01-26 00:57:15 +00:00
|
|
|
use render::vulkan::data;
|
2020-07-20 19:16:29 +00:00
|
|
|
use render::material::{Texture, PBRMaterialBuilder};
|
2021-01-13 23:10:58 +00:00
|
|
|
use render::{Light, Material, Mesh, Transform, Renderable};
|
2020-07-13 20:58:24 +00:00
|
|
|
use physics::color;
|
2020-01-26 00:57:15 +00:00
|
|
|
|
2021-03-21 18:43:36 +00:00
|
|
|
struct Time {
|
|
|
|
start: time::Instant,
|
|
|
|
now: time::Instant,
|
2020-08-22 14:41:36 +00:00
|
|
|
}
|
2021-03-21 18:43:36 +00:00
|
|
|
impl ecs::Global for Time {}
|
2020-08-22 14:41:36 +00:00
|
|
|
|
2021-03-21 18:43:36 +00:00
|
|
|
impl Time {
|
|
|
|
pub fn instant(&self) -> f32 {
|
|
|
|
let instant_ns = self.now.duration_since(self.start).as_nanos() as u64;
|
|
|
|
let instant = ((instant_ns/1000) as f32) / 1_000_000.0;
|
|
|
|
instant
|
2021-01-13 23:10:58 +00:00
|
|
|
}
|
2021-03-21 18:43:36 +00:00
|
|
|
}
|
2020-08-22 14:41:36 +00:00
|
|
|
|
2021-03-21 18:43:36 +00:00
|
|
|
|
|
|
|
struct Main {
|
|
|
|
light1: ecs::EntityID,
|
|
|
|
light2: ecs::EntityID,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Main {
|
|
|
|
pub fn new(world: &mut World, renderer: &mut render::Renderer) -> Self {
|
2021-01-13 23:10:58 +00:00
|
|
|
let mesh = {
|
|
|
|
let vertices = Arc::new(vec![
|
|
|
|
data::Vertex::new([-0.5, -0.5, 0.5], [ 0.0, 0.0, 1.0], [1.0, 0.0]),
|
|
|
|
data::Vertex::new([ 0.5, -0.5, 0.5], [ 0.0, 0.0, 1.0], [0.0, 0.0]),
|
|
|
|
data::Vertex::new([ 0.5, 0.5, 0.5], [ 0.0, 0.0, 1.0], [0.0, 1.0]),
|
|
|
|
data::Vertex::new([-0.5, 0.5, 0.5], [ 0.0, 0.0, 1.0], [1.0, 1.0]),
|
|
|
|
|
|
|
|
data::Vertex::new([ 0.5, -0.5, -0.5], [ 1.0, 0.0, 0.0], [0.0, 1.0]),
|
|
|
|
data::Vertex::new([ 0.5, 0.5, -0.5], [ 1.0, 0.0, 0.0], [1.0, 1.0]),
|
|
|
|
data::Vertex::new([ 0.5, 0.5, 0.5], [ 1.0, 0.0, 0.0], [1.0, 0.0]),
|
|
|
|
data::Vertex::new([ 0.5, -0.5, 0.5], [ 1.0, 0.0, 0.0], [0.0, 0.0]),
|
|
|
|
|
|
|
|
data::Vertex::new([-0.5, -0.5, -0.5], [-1.0, 0.0, 0.0], [1.0, 1.0]),
|
|
|
|
data::Vertex::new([-0.5, 0.5, -0.5], [-1.0, 0.0, 0.0], [0.0, 1.0]),
|
|
|
|
data::Vertex::new([-0.5, 0.5, 0.5], [-1.0, 0.0, 0.0], [0.0, 0.0]),
|
|
|
|
data::Vertex::new([-0.5, -0.5, 0.5], [-1.0, 0.0, 0.0], [1.0, 0.0]),
|
|
|
|
|
|
|
|
data::Vertex::new([-0.5, -0.5, -0.5], [ 0.0, -1.0, 0.0], [0.0, 1.0]),
|
|
|
|
data::Vertex::new([ 0.5, -0.5, -0.5], [ 0.0, -1.0, 0.0], [1.0, 1.0]),
|
|
|
|
data::Vertex::new([ 0.5, -0.5, 0.5], [ 0.0, -1.0, 0.0], [1.0, 0.0]),
|
|
|
|
data::Vertex::new([-0.5, -0.5, 0.5], [ 0.0, -1.0, 0.0], [0.0, 0.0]),
|
|
|
|
|
|
|
|
data::Vertex::new([-0.5, 0.5, -0.5], [ 0.0, 1.0, 0.0], [1.0, 1.0]),
|
|
|
|
data::Vertex::new([ 0.5, 0.5, -0.5], [ 0.0, 1.0, 0.0], [0.0, 1.0]),
|
|
|
|
data::Vertex::new([ 0.5, 0.5, 0.5], [ 0.0, 1.0, 0.0], [0.0, 0.0]),
|
|
|
|
data::Vertex::new([-0.5, 0.5, 0.5], [ 0.0, 1.0, 0.0], [1.0, 0.0]),
|
|
|
|
|
|
|
|
data::Vertex::new([-0.5, -0.5, -0.5], [ 0.0, 0.0, -1.0], [0.0, 0.0]),
|
|
|
|
data::Vertex::new([ 0.5, -0.5, -0.5], [ 0.0, 0.0, -1.0], [1.0, 0.0]),
|
|
|
|
data::Vertex::new([ 0.5, 0.5, -0.5], [ 0.0, 0.0, -1.0], [1.0, 1.0]),
|
|
|
|
data::Vertex::new([-0.5, 0.5, -0.5], [ 0.0, 0.0, -1.0], [0.0, 1.0]),
|
|
|
|
]);
|
|
|
|
let indices = Arc::new(vec![
|
|
|
|
0, 1, 2, 2, 3, 0,
|
|
|
|
|
|
|
|
4, 5, 6, 6, 7, 4,
|
|
|
|
8, 10, 9, 10, 8, 11,
|
|
|
|
|
|
|
|
12, 13, 14, 14, 15, 12,
|
|
|
|
16, 18, 17, 18, 16, 19,
|
|
|
|
|
|
|
|
20, 22, 21, 22, 20, 23,
|
|
|
|
|
|
|
|
]);
|
2021-03-21 18:43:36 +00:00
|
|
|
renderer.add_resource(Mesh::new(vertices, indices))
|
2021-01-13 23:10:58 +00:00
|
|
|
};
|
|
|
|
|
2021-03-21 18:43:36 +00:00
|
|
|
let material = renderer.add_resource(PBRMaterialBuilder {
|
2021-01-13 23:10:58 +00:00
|
|
|
diffuse: Texture::from_image(String::from("assets/test-128px.png")),
|
2021-04-03 14:05:18 +00:00
|
|
|
roughness: Texture::from_image(String::from("assets/test-128px-roughness.png")),
|
2021-01-13 23:10:58 +00:00
|
|
|
}.build());
|
|
|
|
|
|
|
|
for x in -20..20 {
|
|
|
|
for y in -20..20 {
|
|
|
|
for z in -20..20 {
|
2021-03-21 18:43:36 +00:00
|
|
|
world.new_entity()
|
2021-01-13 23:10:58 +00:00
|
|
|
.with(Transform::at((x as f32)*4.0, (y as f32)*4.0, (z as f32)*4.0))
|
|
|
|
.with(Renderable::Mesh(mesh, material))
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-22 14:41:36 +00:00
|
|
|
|
2021-03-21 18:43:36 +00:00
|
|
|
let light = renderer.add_resource(Light::omni_test());
|
2021-01-13 23:10:58 +00:00
|
|
|
|
|
|
|
// The Sun (Sol) is 1AU from the Earth. We ignore the diameter of the Sun and the Earth, as
|
|
|
|
// these are negligible at this scale.
|
|
|
|
let sun_distance: f32 = 149_597_870_700.0;
|
|
|
|
// Solar constant: solar radiant power per square meter of earth's area [w/m^2].
|
|
|
|
let solar_constant: f32 = 1366.0;
|
|
|
|
// Solar luminous emittance (assuming 93 luminous efficacy) [lm/m^2].
|
|
|
|
let sun_luminous_emittance: f32 = solar_constant * 93.0;
|
|
|
|
// Solar luminour power (integrating over a sphere of radius == sun_distance) [lm].
|
|
|
|
let sun_lumen: f32 = sun_luminous_emittance * (4.0 * 3.14159 * sun_distance * sun_distance);
|
|
|
|
|
|
|
|
let sun_color = color::XYZ::new(sun_lumen/3.0, sun_lumen/3.0, sun_lumen/3.0);
|
2021-03-21 18:43:36 +00:00
|
|
|
let sun = renderer.add_resource(Light::omni_with_color(sun_color));
|
2021-01-13 23:10:58 +00:00
|
|
|
|
|
|
|
// In our scene, the sun at a 30 degree zenith.
|
|
|
|
let sun_angle: f32 = (3.14159 * 2.0) / (360.0 / 30.0);
|
|
|
|
|
2021-03-21 18:43:36 +00:00
|
|
|
let light1 = world.new_entity()
|
2021-01-13 23:10:58 +00:00
|
|
|
.with(Transform::at(-10.0, -10.0, -5.0))
|
|
|
|
.with(Renderable::Light(light))
|
|
|
|
.build();
|
2021-03-21 18:43:36 +00:00
|
|
|
let light2 = world.new_entity()
|
2021-01-13 23:10:58 +00:00
|
|
|
.with(Transform::at(-10.0, -10.0, -5.0))
|
|
|
|
.with(Renderable::Light(light))
|
|
|
|
.build();
|
2021-03-21 18:43:36 +00:00
|
|
|
world.new_entity()
|
2021-01-13 23:10:58 +00:00
|
|
|
.with(Transform::at(0.0, sun_angle.sin() * sun_distance, sun_angle.cos() * sun_distance))
|
|
|
|
.with(Renderable::Light(sun))
|
|
|
|
.build();
|
2020-08-22 14:41:36 +00:00
|
|
|
|
2021-03-21 18:43:36 +00:00
|
|
|
Self {
|
|
|
|
light1, light2
|
|
|
|
}
|
2021-01-13 23:10:58 +00:00
|
|
|
}
|
2021-03-21 18:43:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 00:20:35 +00:00
|
|
|
#[derive(Access)]
|
|
|
|
struct MainData<'a> {
|
|
|
|
scene_info: ecs::ReadWriteGlobal<'a, render::SceneInfo>,
|
|
|
|
time: ecs::ReadGlobal<'a, Time>,
|
2021-04-04 15:46:29 +00:00
|
|
|
input: ecs::ReadGlobal<'a, input::Input>,
|
2021-03-22 00:20:35 +00:00
|
|
|
transforms: ecs::ReadWriteComponent<'a, Transform>,
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:43:36 +00:00
|
|
|
impl<'a> ecs::System <'a> for Main {
|
2021-03-22 00:20:35 +00:00
|
|
|
type SystemData = MainData<'a>;
|
|
|
|
fn run(&mut self, sd: Self::SystemData) {
|
2021-04-04 15:46:29 +00:00
|
|
|
let position: f32 = match sd.input.get().mouse_cursor() {
|
|
|
|
Some(cursor) => cursor.x,
|
|
|
|
_ => (sd.time.get().instant() / 10.0) * 3.14 * 2.0,
|
|
|
|
};
|
2021-03-21 18:43:36 +00:00
|
|
|
|
|
|
|
let camera = cgm::Point3::new(
|
|
|
|
7.0 + (position / 4.0).sin(),
|
|
|
|
12.0 + (position / 4.0).cos(),
|
|
|
|
3.0
|
|
|
|
);
|
|
|
|
|
|
|
|
let view = cgm::Matrix4::look_at(
|
|
|
|
camera.clone(),
|
|
|
|
cgm::Point3::new(0.0, 0.0, 0.0),
|
|
|
|
cgm::Vector3::new(0.0, 0.0, 1.0)
|
|
|
|
);
|
|
|
|
|
2021-03-22 00:20:35 +00:00
|
|
|
sd.scene_info.get().camera = camera;
|
|
|
|
sd.scene_info.get().view = view;
|
2021-03-21 18:43:36 +00:00
|
|
|
|
2021-03-22 00:20:35 +00:00
|
|
|
*sd.transforms.get_mut(self.light1).unwrap() = Transform::at(
|
2021-03-21 20:35:16 +00:00
|
|
|
-0.0 + (position*3.0).sin() * 4.0,
|
|
|
|
-0.0 + (position*4.0).cos() * 4.0,
|
|
|
|
-0.0 + (position*2.0).sin() * 3.0,
|
|
|
|
);
|
|
|
|
|
2021-03-22 00:20:35 +00:00
|
|
|
*sd.transforms.get_mut(self.light2).unwrap() = Transform::at(
|
2021-03-21 20:35:16 +00:00
|
|
|
-0.0 + (position*3.0).cos() * 4.0,
|
|
|
|
-0.0 + (position*4.0).sin() * 4.0,
|
|
|
|
-0.0 + (position*2.0).cos() * 3.0,
|
|
|
|
);
|
2020-01-26 00:57:15 +00:00
|
|
|
|
2020-03-14 15:55:03 +00:00
|
|
|
}
|
2021-01-13 23:10:58 +00:00
|
|
|
}
|
2020-01-26 00:57:15 +00:00
|
|
|
|
2021-01-13 23:10:58 +00:00
|
|
|
fn main() {
|
|
|
|
env_logger::init();
|
2021-03-21 18:43:36 +00:00
|
|
|
let mut world = World::new();
|
|
|
|
let mut renderer = render::Renderer::initialize(&mut world);
|
|
|
|
let main = Main::new(&mut world, &mut renderer);
|
|
|
|
|
2021-01-13 23:10:58 +00:00
|
|
|
log::info!("Starting...");
|
2021-03-21 18:43:36 +00:00
|
|
|
|
|
|
|
let mut p = Processor::new(&world);
|
|
|
|
p.add_system(main);
|
2021-03-21 20:35:16 +00:00
|
|
|
p.add_system(renderer);
|
2021-03-21 18:43:36 +00:00
|
|
|
|
|
|
|
let start = time::Instant::now();
|
2021-04-04 15:46:29 +00:00
|
|
|
world.set_global(Time {
|
2021-03-21 18:43:36 +00:00
|
|
|
start,
|
|
|
|
now: start,
|
|
|
|
});
|
2021-04-04 15:46:29 +00:00
|
|
|
world.set_global(input::Input::new());
|
2021-03-21 18:43:36 +00:00
|
|
|
loop {
|
|
|
|
world.global_mut::<Time>().get().now = time::Instant::now();
|
|
|
|
|
|
|
|
p.run();
|
|
|
|
let status = world.global::<render::Status>().get();
|
|
|
|
if status.closed {
|
|
|
|
log::info!("Renderer closed, exiting.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-05-05 14:52:27 +00:00
|
|
|
}
|