Init work on 2d

This commit is contained in:
Piotr Siuszko 2021-09-14 22:16:49 +02:00
parent 9320ed97b6
commit 102eadf5c9
12 changed files with 276 additions and 67 deletions

72
examples/2d.rs Normal file
View File

@ -0,0 +1,72 @@
use doppler::assets_cache::AssetsCache;
use doppler::camera::*;
use doppler::client::Client;
use doppler::glutin::event::{ElementState, VirtualKeyCode};
use doppler::shader::Shader;
use doppler::map::*;
use doppler::components::*;
pub struct Client2D{
delta: f32,
models_2d: ModelComponent,
shader: Shader
}
impl Default for Client2D {
fn default() -> Self {
Client2D {
delta: 0.0,
models_2d: ModelComponent::default(),
shader: Shader::from_file(
"resources/shaders/multiple_lights.vs",
"resources/shaders/multiple_lights.fs",
)
}
}
}
impl Client for Client2D {
fn on_keyboard(&mut self, code: &VirtualKeyCode, state: &ElementState) {
match (code, state) {
(VirtualKeyCode::W, ElementState::Pressed) => (),
(VirtualKeyCode::S, ElementState::Pressed) => (),
(VirtualKeyCode::A, ElementState::Pressed) => (),
(VirtualKeyCode::D, ElementState::Pressed) => (),
(VirtualKeyCode::LControl, _) => (),
(_, _) => (),
}
}
fn load_assets(&mut self, cache: &mut AssetsCache) {
self.models_2d.model = cache.load_2d("resources/objects/ddd.jpg");
}
unsafe fn draw(&mut self) {
use doppler::math::prelude::*;
self.models_2d.draw(&self.shader);
// let projection: Matrix4<f32> = perspective(
// Deg(self.camera.zoom),
// consts::SCR_WIDTH as f32 / consts::SCR_HEIGHT as f32,
// 0.1,
// 1000.0,
// );
}
fn update(&mut self, delta: f32) {
self.delta = delta;
}
fn debug_draw(&mut self, ui: &doppler::imgui::Ui) {
}
fn on_mouse_scroll(&mut self, yoffset: f32) {
}
fn on_mouse_move(&mut self, x: f32, y: f32) {
}
}
pub fn main() {
doppler::engine::Engine::default().run::<Client2D>();
}

View File

@ -12,6 +12,7 @@ pub struct ExampleClient {
delta: f32,
show_object_info: bool,
show_camera_info: bool,
imgui_grabs_input: bool,
object_info_id: i32,
show_light_info: bool,
light_info_id: i32,
@ -26,6 +27,7 @@ impl Default for ExampleClient {
show_camera_info: false,
show_object_info: false,
show_light_info: false,
imgui_grabs_input: false,
light_info_id: 0,
}
}
@ -189,12 +191,21 @@ impl Client for ExampleClient {
self.show_light_info = show_window;
}
}
self.imgui_grabs_input = ui.is_any_item_hovered() || ui.is_any_item_focused();
}
fn on_mouse_scroll(&mut self, yoffset: f32) {
if self.imgui_grabs_input {
println!("DD");
return;
}
self.map.camera.process_mouse_scroll(yoffset as f32);
}
fn on_mouse_move(&mut self, x: f32, y: f32) {
if self.imgui_grabs_input {
println!("DD");
return;
}
self.map.camera.process_mouse_movement(x, y, true);
}
}

View File

@ -0,0 +1,10 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl None
Ns 500
Ka 0.8 0.8 0.8
Kd 0.8 0.8 0.8
Ks 0.8 0.8 0.8
d 1
illum 2

View File

@ -0,0 +1,16 @@
# Blender v2.93.4 OBJ File: ''
# www.blender.org
mtllib plane.mtl
o Plane
v -1.000000 0.000000 1.000000
v 1.000000 0.000000 1.000000
v -1.000000 0.000000 -1.000000
v 1.000000 0.000000 -1.000000
vt 0.999900 0.999900
vt 0.000100 0.999900
vt 0.000100 0.000100
vt 0.999900 0.000100
vn 0.0000 1.0000 0.0000
usemtl None
s off
f 1/1/1 2/2/1 4/3/1 3/4/1

View File

@ -54,28 +54,6 @@ objects:
y: 0.0
z: 0.0
scale: 1.0
- model_hash: 4360533331876312021
transform:
position:
x: 0.0
y: 0.0
z: 0.0
rotation:
x: 0.0
y: 0.0
z: 0.0
scale: 1.0
- model_hash: 6024833582843300776
transform:
position:
x: 8.3
y: 0.0
z: 3.0
rotation:
x: 0.0
y: 120.0
z: 0.0
scale: 0.025
- model_hash: 12474845581084103280
transform:
position:
@ -87,6 +65,61 @@ objects:
y: 0.0
z: 0.0
scale: 1.0
- model_hash: 7463349984896526650
transform:
position:
x: 2.0
y: 0.0
z: 0.0
rotation:
x: 0.0
y: 0.0
z: 0.0
scale: 1.0
- model_hash: 13800530402659272349
transform:
position:
x: 44.0
y: 0.0
z: 33.0
rotation:
x: 0.0
y: 128.0
z: 0.0
scale: 3.0
- model_hash: 10609347989321310959
transform:
position:
x: 55.0
y: 0.0
z: -28.0
rotation:
x: 0.0
y: -67.0
z: 0.0
scale: 2.0
- model_hash: 14496278726830200225
transform:
position:
x: 39.0
y: 0.0
z: -39.0
rotation:
x: -12.0
y: 70.0
z: 30.0
scale: 1.5
- model_hash: 448611579693571817
transform:
position:
x: 11.0
y: 0.0
z: -40.0
rotation:
x: 0.0
y: 0.0
z: 0.0
scale: 2.0
camera:
position:
x: 0.0

View File

@ -1,8 +1,11 @@
resources/objects/ground/ground.obj
resources/objects/robot/robot.obj
resources/objects/grass/grass.obj foliage.png
resources/objects/gaz_tank/gaz_tank.obj
resources/objects/tree/tree_6_d.obj tree_e.png
resources/objects/tree/tree_6_c.obj tree_e.png
resources/objects/tree/tree_6_c.obj tree_e.png
resources/objects/ruins/ruins.obj
resources/objects/citypack/policeman.obj
resources/objects/sclavinia/wapienne_skaly.obj wapno.png
resources/objects/sclavinia/wapienne_skaly_2.obj wapno.png
resources/objects/sclavinia/wapienne_skaly_3.obj wapno.png
resources/objects/sclavinia/chata_zniszczona_1.obj palev2.png

View File

@ -5,6 +5,7 @@ use log::{error, info};
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::path::Path;
#[derive(Default)]
pub struct AssetsCache {
@ -13,6 +14,23 @@ pub struct AssetsCache {
}
impl AssetsCache {
pub fn load_2d(&mut self, path: &str) -> Model {
let fullpath = Path::new(&path);
let dir = fullpath
.parent()
.unwrap()
.to_str()
.unwrap()
.to_string();
let filename = fullpath.file_name().unwrap().to_str().unwrap();
let texture = self.get_material_texture(&dir, &filename, "texture_diffuse");
let mut model = Model::new_2d( self);
for ele in &mut model.meshes {
ele.textures.push(texture.clone())
}
model
}
pub fn load_all_from_file(&mut self, path: &str) {
let path = std::path::Path::new(path);
let meta = std::fs::metadata(path);
@ -70,7 +88,7 @@ impl AssetsCache {
fn load_model_ext(&mut self, path: &str, diff_texture: Option<&str>) {
let hash = Self::path_hash(path);
info!("Loading model: {}({})", path, hash);
let model = Model::new_ext(path, diff_texture, self);
let model = Model::new_ext(path, diff_texture, self, false);
self.models.insert(hash, model);
}

View File

@ -1,3 +1,5 @@
use std::default;
#[cfg(feature = "imgui_inspect")]
use crate::imgui_helper::*;
use crate::model::Model;
@ -47,6 +49,16 @@ pub struct ModelComponent {
pub transform: Transform,
}
impl Default for ModelComponent {
fn default() -> Self {
ModelComponent {
model: Model::default(),
hash: u64::default(),
transform: Transform::default(),
}
}
}
impl ModelComponent {
pub unsafe fn draw(&self, shader: &Shader) {
let matrix = self.transform.get_matrix();

View File

@ -101,7 +101,7 @@ impl Default for DirectionalLight {
DirectionalLight {
direction: vec3(-0.3, -1.0, -0.3),
ambient: vec3(0.05, 0.05, 0.05),
diffuse: vec3(0.4, 0.4, 0.4),
diffuse: vec3(0.9, 0.9, 0.8),
specular: vec3(0.5, 0.5, 0.5),
}
}

View File

@ -10,6 +10,7 @@ use cgmath::prelude::*;
use cgmath::{Vector2, Vector3};
use gl;
use crate::assets_cache::AssetsCache;
use crate::shader::Shader;
// NOTE: without repr(C) the compiler may reorder the fields or use different padding/alignment than C.
@ -90,7 +91,6 @@ impl Mesh {
VBO: 0,
EBO: 0,
};
// now that we have all the required data, set the vertex buffers and its attribute pointers.
unsafe { mesh.setupMesh() }
mesh

View File

@ -28,8 +28,17 @@ impl Default for Model {
}
impl Model {
pub fn new_2d(cache: &mut AssetsCache) -> Model {
Self::new_ext("resources/defaults/plane.obj", None, cache, true)
}
/// constructor, expects a filepath to a 3D model.
pub fn new_ext(path: &str, diff_texture: Option<&str>, cache: &mut AssetsCache) -> Model {
pub fn new_ext(
path: &str,
diff_texture: Option<&str>,
cache: &mut AssetsCache,
skip_textures: bool,
) -> Model {
let pathObj = Path::new(path);
let mut model = Model {
meshes: Vec::<Mesh>::new(),
@ -43,7 +52,7 @@ impl Model {
};
if pathObj.exists() {
model.load_model(path, diff_texture, cache);
model.load_model(path, diff_texture, cache, skip_textures);
} else {
warn!("{} does not exist, returning empty model", path);
}
@ -52,7 +61,7 @@ impl Model {
}
pub fn new(path: &str, cache: &mut AssetsCache) -> Model {
Model::new_ext(path, None, cache)
Model::new_ext(path, None, cache, false)
}
pub fn Draw(&self, shader: &Shader) {
@ -64,7 +73,13 @@ impl Model {
}
// loads a model from file and stores the resulting meshes in the meshes vector.
fn load_model(&mut self, path: &str, diffuse_path: Option<&str>, cache: &mut AssetsCache) {
fn load_model(
&mut self,
path: &str,
diffuse_path: Option<&str>,
cache: &mut AssetsCache,
skip_textures: bool,
) {
let path = Path::new(path);
// println!("Started loading model from path: {}", path.display());
@ -98,6 +113,8 @@ impl Model {
// process material
let mut textures = Vec::new();
if !skip_textures {
if let Some(material_id) = mesh.material_id {
let material = &materials[material_id];
@ -130,7 +147,18 @@ impl Model {
}
// NOTE: no height maps
} else if diffuse_path.is_some() {
println!("Loading {}", &diffuse_path.unwrap());
let path = diffuse_path.unwrap();
println!("Loading {}", path);
let dir: String = if path.contains("/") {
Path::new(&path)
.parent()
.unwrap()
.to_str()
.unwrap()
.to_string()
} else {
self.directory.to_string()
};
let texture = cache.get_material_texture(
&self.directory,
&diffuse_path.unwrap(),
@ -140,6 +168,7 @@ impl Model {
} else {
warn!("There are no materials for: {}", path.display());
}
}
self.meshes.push(Mesh::new(vertices, indices, textures));
}

View File

@ -64,8 +64,13 @@ pub unsafe fn load_texture(path: &str, file_format: &str) -> u32 {
pub unsafe fn load_texture_from_dir(filename: &str, directory: &str) -> u32 {
let fullpath = format!("{}/{}", directory, filename);
let dot = filename.find(".").unwrap_or_default() + 1usize;
let (_, format) = filename.split_at(dot);
load_texture_from_fullpath(&fullpath)
}
pub unsafe fn load_texture_from_fullpath(fullpath: &str) -> u32 {
let dot = fullpath.find(".").unwrap_or_default() + 1usize;
let (_, format) = fullpath.split_at(dot);
load_texture(&fullpath, format)
}