Move engine to separate crate

This commit is contained in:
Piotr 2020-09-29 23:13:46 +02:00
parent 31cf464ce4
commit 0070890092
34 changed files with 1810 additions and 76 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@
**jpeg
imgui.ini
log.log
/doppler/target

12
Cargo.lock generated
View File

@ -253,6 +253,18 @@ dependencies = [
"tobj",
]
[[package]]
name = "doppler_example"
version = "0.1.0"
dependencies = [
"cgmath",
"doppler",
"glutin",
"imgui",
"imgui-inspect",
"imgui-inspect-derive",
]
[[package]]
name = "downcast-rs"
version = "1.2.0"

View File

@ -1,5 +1,5 @@
[package]
name = "doppler"
name = "doppler_example"
version = "0.1.0"
authors = ["Piotr <siuszko@zoho.com>"]
edition = "2018"
@ -10,23 +10,12 @@ panic = 'abort'
lto = true
[dependencies]
gl = "0.14.0"
imgui="0.4"
imgui-winit-support = {version="0.4.0", optional=true}
imgui-opengl-renderer = {version="0.8", optional = true}
cgmath = "0.17.0"
imgui-inspect = {version="0.5.0", optional = true}
imgui-inspect-derive = {version="0.5.0", optional= true}
tobj = "2.0.2"
inline_tweak = "1.0.8"
log = "0.4.11"
simple-logging = "2.0.2"
image2 = { git = "https://github.com/Leinnan/image2-rs", branch="legacy", default-features = false, features=["io"] }
doppler = {path="doppler"}
glutin = "0.24.1"
[features]
default = ["imgui_inspect"]
imgui_inspect = ["imgui-inspect-derive","imgui-opengl-renderer", "imgui-inspect", "imgui-winit-support"]
cgmath = "0.17.0"
imgui="0.4.0"
imgui-inspect = "0.5.0"
imgui-inspect-derive = "0.5.0"
[profile.dev.package."*"]
opt-level = 2

1411
doppler/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

25
doppler/Cargo.toml Normal file
View File

@ -0,0 +1,25 @@
[package]
name = "doppler"
version = "0.1.0"
authors = ["Piotr <mev_lyshkin@protonmail.com>"]
edition = "2018"
[dependencies]
gl = "0.14.0"
imgui={version="0.4.0", optional=true}
imgui-winit-support = {version="0.4.0", optional=true}
imgui-opengl-renderer = {version="0.8", optional = true}
cgmath = "0.17.0"
imgui-inspect = {version="0.5.0", optional = true}
imgui-inspect-derive = {version="0.5.0", optional= true}
tobj = "2.0.2"
inline_tweak = "1.0.8"
log = "0.4.11"
simple-logging = "2.0.2"
image2 = { git = "https://github.com/Leinnan/image2-rs", branch="legacy", default-features = false, features=["io"] }
glutin = "0.24.1"
[features]
default = ["imgui_inspect"]
imgui_inspect = ["imgui","imgui-inspect-derive","imgui-opengl-renderer", "imgui-inspect", "imgui-winit-support"]

Binary file not shown.

View File

@ -0,0 +1,28 @@
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform float screen_width;
uniform float screen_height;
uniform sampler2D screenTexture;
uniform float pixelWidth = 3.0;
uniform float pixelHeight = 3.0;
void main()
{
vec2 uv = TexCoords.xy;
uv *= 1.0 - uv.yx;
float vig = uv.x*uv.y * 15.0;
vig = pow(vig, 0.3);
float dx = pixelWidth*(1.0/screen_width);
float dy = pixelHeight*(1.0/screen_height);
vec2 coord = vec2(dx*floor(TexCoords.x/dx), dy*floor(TexCoords.y/dy));
vec3 tc = texture(screenTexture, coord).rgb;
FragColor = vec4(tc,1.0) * vig;
}

View File

@ -0,0 +1,11 @@
#version 330 core
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec2 aTexCoords;
out vec2 TexCoords;
void main()
{
TexCoords = aTexCoords;
gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0);
}

View File

@ -0,0 +1,7 @@
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0); // set alle 4 vector values to 1.0
}

View File

@ -0,0 +1,11 @@
#version 330 core
layout (location = 0) in vec3 aPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = projection * view * model * vec4(aPos, 1.0);
}

View File

@ -0,0 +1,14 @@
#version 330 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D texture_diffuse1;
void main()
{
vec4 texColor = texture(texture_diffuse1, TexCoords);
if(texColor.a < 0.1)
discard;
FragColor = texColor;
}

View File

@ -0,0 +1,16 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
out vec2 TexCoords;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
TexCoords = aTexCoords;
gl_Position = projection * view * model * vec4(aPos, 1.0);
}

View File

@ -0,0 +1,150 @@
#version 330 core
out vec4 FragColor;
struct Material {
sampler2D diffuse;
sampler2D specular;
float shininess;
};
struct DirLight {
vec3 direction;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct PointLight {
vec3 position;
float constant;
float linear;
float quadratic;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
struct SpotLight {
vec3 position;
vec3 direction;
float cutOff;
float outerCutOff;
float constant;
float linear;
float quadratic;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
#define NR_POINT_LIGHTS 4
in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;
uniform vec3 viewPos;
uniform DirLight dirLight;
uniform PointLight pointLights[NR_POINT_LIGHTS];
uniform SpotLight spotLight;
uniform Material material;
// function prototypes
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
void main()
{
vec4 texColor = texture(material.diffuse, TexCoords);
if(texColor.a < 0.1)
discard;
// properties
vec3 norm = normalize(Normal);
vec3 viewDir = normalize(viewPos - FragPos);
// == =====================================================
// Our lighting is set up in 3 phases: directional, point lights and an optional flashlight
// For each phase, a calculate function is defined that calculates the corresponding color
// per lamp. In the main() function we take all the calculated colors and sum them up for
// this fragment's final color.
// == =====================================================
// phase 1: directional lighting
vec3 result = CalcDirLight(dirLight, norm, viewDir);
// phase 2: point lights
for(int i = 0; i < NR_POINT_LIGHTS; i++)
result += CalcPointLight(pointLights[i], norm, FragPos, viewDir);
// phase 3: spot light
//result += CalcSpotLight(spotLight, norm, FragPos, viewDir);
FragColor = vec4(result, 1.0);
}
// calculates the color when using a directional light.
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
{
vec3 lightDir = normalize(-light.direction);
// diffuse shading
float diff = max(dot(normal, lightDir), 0.0);
// specular shading
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
// combine results
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
return (ambient + diffuse + specular);
}
// calculates the color when using a point light.
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
vec3 lightDir = normalize(light.position - fragPos);
// diffuse shading
float diff = max(dot(normal, lightDir), 0.0);
// specular shading
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
// attenuation
float distance = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
// combine results
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
}
// calculates the color when using a spot light.
vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
vec3 lightDir = normalize(light.position - fragPos);
// diffuse shading
float diff = max(dot(normal, lightDir), 0.0);
// specular shading
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
// attenuation
float distance = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
// spotlight intensity
float theta = dot(lightDir, normalize(-light.direction));
float epsilon = light.cutOff - light.outerCutOff;
float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
// combine results
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
ambient *= attenuation * intensity;
diffuse *= attenuation * intensity;
specular *= attenuation * intensity;
return (ambient + diffuse + specular);
}

View File

@ -0,0 +1,30 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aTexCoords;
#define PRECISION 0.01
#define MIN_DIST 20.0
out vec3 FragPos;
out vec3 Normal;
out vec2 TexCoords;
uniform mat4 model;
uniform mat4 view;
uniform vec3 viewPos;
uniform mat4 projection;
void main()
{
FragPos = vec3(model * vec4(aPos, 1.0));
Normal = mat3(transpose(inverse(model))) * aNormal;
TexCoords = aTexCoords;
gl_Position = projection * view * vec4(FragPos, 1.0);
if(distance(viewPos, gl_Position.xyz) > MIN_DIST){
gl_Position.xyz = gl_Position.xyz - mod(gl_Position.xyz, PRECISION);
}
}

View File

@ -0,0 +1,11 @@
#version 330 core
out vec4 FragColor;
in vec3 TexCoords;
uniform samplerCube skybox;
void main()
{
FragColor = texture(skybox, TexCoords);
}

View File

@ -0,0 +1,14 @@
#version 330 core
layout (location = 0) in vec3 aPos;
out vec3 TexCoords;
uniform mat4 projection;
uniform mat4 view;
void main()
{
TexCoords = aPos;
vec4 pos = projection * view * vec4(aPos, 1.0);
gl_Position = pos.xyww;
}

View File

@ -1,6 +1,6 @@
use crate::gaia::mesh::Texture;
use crate::gaia::model::Model;
use crate::gaia::utils::load_texture_from_dir;
use crate::mesh::Texture;
use crate::model::Model;
use crate::utils::load_texture_from_dir;
use log::info;
use std::collections::HashMap;

View File

@ -1,7 +1,7 @@
#![allow(dead_code)]
#[cfg(feature = "imgui_inspect")]
use crate::gaia::imgui_helper::*;
use crate::imgui_helper::*;
use cgmath;
use cgmath::prelude::*;
use cgmath::vec3;

View File

@ -1,10 +1,9 @@
use crate::gaia::assets_cache::AssetsCache;
use crate::gaia::engine::Engine;
use crate::assets_cache::AssetsCache;
use glutin::event::{ElementState, VirtualKeyCode};
pub trait Client {
fn load_assets(&mut self, cache: &mut AssetsCache);
fn update(&mut self, engine: &Engine, delta: f32);
fn update(&mut self, delta: f32);
fn on_keyboard(&mut self, code: &VirtualKeyCode, state: &ElementState);
fn on_mouse_scroll(&mut self, yoffset: f32);
fn on_mouse_move(&mut self, x: f32, y: f32);

View File

@ -1,7 +1,7 @@
#[cfg(feature = "imgui_inspect")]
use crate::gaia::imgui_helper::*;
use crate::gaia::model::Model;
use crate::gaia::shader::Shader;
use crate::imgui_helper::*;
use crate::model::Model;
use crate::shader::Shader;
use cgmath::{vec3, Matrix4, Rad, Vector3};
#[cfg(feature = "imgui_inspect")]
use imgui;

View File

@ -1,16 +1,15 @@
use crate::example_client::ExampleClient;
use crate::gaia::assets_cache::AssetsCache;
use crate::gaia::client::Client;
use crate::gaia::consts;
use crate::gaia::framebuffer::FramebufferSystem;
use crate::assets_cache::AssetsCache;
use crate::client::Client;
use crate::consts;
use crate::framebuffer::FramebufferSystem;
use glutin::event::{Event, KeyboardInput, VirtualKeyCode, WindowEvent};
#[cfg(feature = "imgui_inspect")]
use imgui::Context;
#[cfg(feature = "imgui_inspect")]
use imgui_winit_support::{HiDpiMode, WinitPlatform};
use std::time::Instant;
use log::info;
use log::LevelFilter;
#[derive(Debug)]
pub struct TimeStep {
@ -63,7 +62,7 @@ pub struct Engine {
impl Default for Engine {
fn default() -> Self {
Engine {
Self {
title: String::from("Doppler demo"),
size: (1280, 720),
#[cfg(feature = "imgui_inspect")]
@ -73,7 +72,9 @@ impl Default for Engine {
}
impl Engine {
pub fn run(self) {
pub fn run<T: Client + Default + 'static>(self) {
let _ = simple_logging::log_to_file("log.log", LevelFilter::Info);
info!("Starting engine!");
let event_loop = glutin::event_loop::EventLoop::new();
let window = glutin::window::WindowBuilder::new()
.with_title(&self.title)
@ -99,7 +100,6 @@ impl Engine {
gl::Enable(gl::DEPTH_TEST);
}
info!("DPI: {}", gl_window.window().scale_factor());
let mut client = ExampleClient::create();
let mut framebuffer = unsafe { FramebufferSystem::generate(self.size.0, self.size.1) };
#[cfg(feature = "imgui_inspect")]
let (mut imgui, mut platform, renderer) = {
@ -161,7 +161,7 @@ impl Engine {
style.frame_rounding = style.grab_rounding;
imgui.fonts().clear();
imgui.fonts().add_font(&[FontSource::TtfData {
data: include_bytes!("../../resources/FiraSans-SemiBold.ttf"),
data: include_bytes!("../resources/FiraSans-SemiBold.ttf"),
size_pixels: 19.0,
config: None,
}]);
@ -173,7 +173,10 @@ impl Engine {
});
(imgui, platform, renderer)
};
info!("Creating AssetCache");
let mut assets_cache = AssetsCache::default();
info!("Creating client");
let mut client = Box::new(T::default());
client.load_assets(&mut assets_cache);
let mut first_mouse = true;
let mut last_x: f32 = consts::SCR_WIDTH as f32 / 2.0;
@ -200,7 +203,7 @@ impl Engine {
}
Event::MainEventsCleared => {
let delta = timestep.delta();
client.update(&self, delta);
client.update(delta);
// other application-specific logic
#[cfg(feature = "imgui_inspect")]
platform

View File

@ -1,4 +1,4 @@
use crate::gaia::shader::*;
use crate::shader::*;
use gl::types::*;
use log::{info, warn};
use std::mem;

View File

@ -1,3 +1,15 @@
pub extern crate gl;
pub extern crate glutin;
#[cfg(feature = "imgui_inspect")]
pub extern crate imgui;
pub extern crate log;
pub extern crate simple_logging;
pub use cgmath::*;
pub use glutin::*;
pub use imgui::*;
pub mod macros;
pub mod assets_cache;

View File

@ -1,6 +1,6 @@
#[cfg(feature = "imgui_inspect")]
use crate::gaia::imgui_helper::*;
use crate::gaia::shader::Shader;
use crate::imgui_helper::*;
use crate::shader::Shader;
use cgmath::{vec3, Matrix4, Vector3};
#[cfg(feature = "imgui_inspect")]
use imgui;

View File

@ -10,7 +10,7 @@ use cgmath::prelude::*;
use cgmath::{Vector2, Vector3};
use gl;
use crate::gaia::shader::Shader;
use crate::shader::Shader;
// NOTE: without repr(C) the compiler may reorder the fields or use different padding/alignment than C.
// Depending on how you pass the data to OpenGL, this may be bad. In this case it's not strictly

View File

@ -1,9 +1,9 @@
#![allow(non_snake_case)]
#![allow(dead_code)]
use crate::gaia::assets_cache::AssetsCache;
use crate::gaia::mesh::{Mesh, Texture, Vertex};
use crate::gaia::shader::Shader;
use crate::assets_cache::AssetsCache;
use crate::mesh::{Mesh, Texture, Vertex};
use crate::shader::Shader;
use cgmath::{vec2, vec3};
use log::{info, warn};
use std::path::Path;

View File

@ -8,7 +8,7 @@ use std::str;
use gl;
use gl::types::*;
use crate::gaia::consts;
use crate::consts;
use cgmath::prelude::*;
use cgmath::{Matrix, Matrix4, Vector2, Vector3};

View File

@ -1,5 +1,5 @@
use crate::gaia::shader::*;
use crate::gaia::utils::*;
use crate::shader::*;
use crate::utils::*;
use cgmath::Matrix4;
use gl::types::*;
use std::mem;

View File

@ -1,14 +1,14 @@
use crate::gaia::assets_cache::AssetsCache;
use crate::gaia::camera::*;
use crate::gaia::client::Client;
use crate::gaia::components::{ModelComponent, Transform};
use crate::gaia::consts;
use crate::gaia::engine::Engine;
use crate::gaia::light::*;
use crate::gaia::sky::Sky;
use crate::doppler::assets_cache::AssetsCache;
use crate::doppler::camera::*;
use crate::doppler::client::Client;
use crate::doppler::components::{ModelComponent, Transform};
use crate::doppler::consts;
use crate::doppler::light::*;
use crate::doppler::sky::Sky;
use cgmath::prelude::*;
use cgmath::{perspective, vec3, Deg, Matrix4, Point3};
use glutin::event::{ElementState, VirtualKeyCode};
use imgui::*;
pub struct ExampleClient {
models: Vec<ModelComponent>,
@ -23,8 +23,8 @@ pub struct ExampleClient {
delta: f32,
}
impl ExampleClient {
pub fn create() -> ExampleClient {
impl Default for ExampleClient {
fn default() -> Self {
let sky = unsafe { Sky::new() };
ExampleClient {
delta: 0.0,
@ -145,11 +145,10 @@ impl Client for ExampleClient {
}
self.sky.draw(view, projection);
}
fn update(&mut self, _engine: &Engine, delta: f32) {
fn update(&mut self, delta: f32) {
self.delta = delta;
}
#[cfg(feature = "imgui_inspect")]
fn debug_draw(&mut self, ui: &imgui::Ui) {
use imgui::*;
if let Some(menu_bar) = ui.begin_main_menu_bar() {

View File

@ -1,25 +1,16 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
extern crate gl;
extern crate glutin;
#[cfg(feature = "imgui_inspect")]
extern crate imgui;
extern crate log;
extern crate simple_logging;
#[macro_use]
mod gaia;
extern crate doppler;
mod example_client;
use crate::gaia::engine::Engine;
use log::info;
use log::LevelFilter;
use doppler::engine::Engine;
use crate::example_client::ExampleClient;
pub fn main() {
let _ = simple_logging::log_to_file("log.log", LevelFilter::Info);
info!("Starting engine!");
let engine = Engine::default();
engine.run();
engine.run::<ExampleClient>();
}