This commit is contained in:
Piotr 2020-09-26 20:37:13 +02:00
parent 3c884ed3f0
commit 841cb0203b
5 changed files with 141 additions and 9 deletions

View File

@ -61,6 +61,9 @@ vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
void main() void main()
{ {
vec4 texColor = texture(material.diffuse, TexCoords);
if(texColor.a < 0.1)
discard;
// properties // properties
vec3 norm = normalize(Normal); vec3 norm = normalize(Normal);
vec3 viewDir = normalize(viewPos - FragPos); vec3 viewDir = normalize(viewPos - FragPos);
@ -77,7 +80,7 @@ void main()
for(int i = 0; i < NR_POINT_LIGHTS; i++) for(int i = 0; i < NR_POINT_LIGHTS; i++)
result += CalcPointLight(pointLights[i], norm, FragPos, viewDir); result += CalcPointLight(pointLights[i], norm, FragPos, viewDir);
// phase 3: spot light // phase 3: spot light
result += CalcSpotLight(spotLight, norm, FragPos, viewDir); //result += CalcSpotLight(spotLight, norm, FragPos, viewDir);
FragColor = vec4(result, 1.0); FragColor = vec4(result, 1.0);
} }

View File

@ -4,9 +4,11 @@ use crate::gaia::client::Client;
use crate::gaia::components::{ModelComponent, Transform}; use crate::gaia::components::{ModelComponent, Transform};
use crate::gaia::consts; use crate::gaia::consts;
use crate::gaia::engine::Engine; use crate::gaia::engine::Engine;
use crate::gaia::light::*;
use crate::gaia::sky::Sky; use crate::gaia::sky::Sky;
use crate::gaia::*; use crate::gaia::*;
use cgmath::{perspective, vec3, Deg, Matrix4, Point3}; use cgmath::prelude::*;
use cgmath::{perspective, vec3, Deg, Matrix4, Point3, Vector3};
use imgui_glfw_rs::glfw; use imgui_glfw_rs::glfw;
pub struct ExampleClient { pub struct ExampleClient {
@ -14,6 +16,7 @@ pub struct ExampleClient {
camera: Camera, camera: Camera,
shader: shader::Shader, shader: shader::Shader,
sky: Sky, sky: Sky,
point_lights: Vec<PointLight>,
show_object_info: bool, show_object_info: bool,
show_camera_info: bool, show_camera_info: bool,
object_info_id: i32, object_info_id: i32,
@ -21,9 +24,19 @@ pub struct ExampleClient {
impl ExampleClient { impl ExampleClient {
pub fn create() -> ExampleClient { pub fn create() -> ExampleClient {
let sky; let sky = unsafe { Sky::new() };
unsafe { let pointLightPositions: [Vector3<f32>; 4] = [
sky = Sky::new(); vec3(0.7, 5.0, 2.0),
vec3(2.3, 3.3, -4.0),
vec3(-4.0, 4.0, -12.0),
vec3(0.0, 2.0, -3.0),
];
let mut point_lights = vec![];
for light in pointLightPositions.iter() {
point_lights.push(PointLight {
pos: *light,
..PointLight::default()
})
} }
ExampleClient { ExampleClient {
object_info_id: 0, object_info_id: 0,
@ -40,10 +53,11 @@ impl ExampleClient {
..Camera::default() ..Camera::default()
}, },
shader: shader::Shader::from_file( shader: shader::Shader::from_file(
"resources/shaders/model_loading.vs", "resources/shaders/multiple_lights.vs",
"resources/shaders/model_loading.fs", "resources/shaders/multiple_lights.fs",
), ),
sky: sky, sky: sky,
point_lights: point_lights,
} }
} }
} }
@ -85,10 +99,15 @@ impl Client for ExampleClient {
}, },
model: cache.get_model_ext("resources/objects/tree/tree_6_c.obj", Some("tree_e.png")), model: cache.get_model_ext("resources/objects/tree/tree_6_c.obj", Some("tree_e.png")),
}; };
self.models = vec![tree, tree2, tree3, ground, robot]; let ruins = ModelComponent {
transform: Transform::default(),
model: cache.get_model("resources/objects/ruins/ruins.obj"),
};
self.models = vec![tree, tree2, tree3, ground, robot, ruins];
} }
unsafe fn draw(&mut self) { unsafe fn draw(&mut self) {
use inline_tweak::*;
self.shader.use_program(); self.shader.use_program();
// view/projection transformations // view/projection transformations
@ -101,6 +120,34 @@ impl Client for ExampleClient {
let view = self.camera.get_view_matrix(); let view = self.camera.get_view_matrix();
self.shader.set_mat4(c_str!("projection"), &projection); self.shader.set_mat4(c_str!("projection"), &projection);
self.shader.set_mat4(c_str!("view"), &view); self.shader.set_mat4(c_str!("view"), &view);
self.shader
.set_vector3(c_str!("viewPos"), &self.camera.position.to_vec());
self.shader.setFloat(c_str!("material.shininess"), 32.0);
self.shader.set_vec3(
c_str!("dirLight.direction"),
tweak!(-0.3),
tweak!(-1.0),
tweak!(-0.3),
);
self.shader.set_vec3(
c_str!("dirLight.ambient"),
tweak!(0.14),
tweak!(0.14),
tweak!(0.14),
);
self.shader.set_vec3(
c_str!("dirLight.diffuse"),
tweak!(0.4),
tweak!(0.4),
tweak!(0.4),
);
self.shader
.set_vec3(c_str!("dirLight.specular"), 0.5, 0.5, 0.5);
// point light 1
for (i, v) in self.point_lights.iter().enumerate() {
v.shader_update(i, &self.shader);
}
for model in self.models.iter() { for model in self.models.iter() {
model.draw(&self.shader); model.draw(&self.shader);

72
src/gaia/light.rs Normal file
View File

@ -0,0 +1,72 @@
use crate::gaia::shader::Shader;
use cgmath::{perspective, vec3, Deg, Matrix4, Point3, Vector3};
pub struct PointLight {
pub pos: Vector3<f32>,
pub ambient: Vector3<f32>,
pub diffuse: Vector3<f32>,
pub specular: Vector3<f32>,
pub constant: f32,
pub linear: f32,
pub quadratic: f32,
}
impl PointLight {
pub unsafe fn shader_update(&self, i: usize, shader: &Shader) {
let mut id = format!("pointLights[{}].position\0", i);
shader.set_vector3(
std::ffi::CStr::from_bytes_with_nul_unchecked(id.as_bytes()),
&self.pos,
);
id = format!("pointLights[{}].ambient\0", i);
shader.set_vec3(
std::ffi::CStr::from_bytes_with_nul_unchecked(id.as_bytes()),
self.ambient.x,
self.ambient.y,
self.ambient.z,
);
id = format!("pointLights[{}].diffuse\0", i);
shader.set_vec3(
std::ffi::CStr::from_bytes_with_nul_unchecked(id.as_bytes()),
self.diffuse.x,
self.diffuse.y,
self.diffuse.z,
);
id = format!("pointLights[{}].specular\0", i);
shader.set_vec3(
std::ffi::CStr::from_bytes_with_nul_unchecked(id.as_bytes()),
self.specular.x,
self.specular.y,
self.specular.z,
);
id = format!("pointLights[{}].constant\0", i);
shader.setFloat(
std::ffi::CStr::from_bytes_with_nul_unchecked(id.as_bytes()),
self.constant,
);
id = format!("pointLights[{}].linear\0", i);
shader.setFloat(
std::ffi::CStr::from_bytes_with_nul_unchecked(id.as_bytes()),
self.linear,
);
id = format!("pointLights[{}].quadratic\0", i);
shader.setFloat(
std::ffi::CStr::from_bytes_with_nul_unchecked(id.as_bytes()),
self.quadratic,
);
}
}
impl Default for PointLight {
fn default() -> Self {
PointLight {
pos: vec3(0.0, 0.0, 0.0),
ambient: vec3(0.05, 0.05, 0.05),
diffuse: vec3(0.8, 0.8, 0.8),
specular: vec3(1.0, 1.0, 1.0),
constant: 1.0,
linear: 0.09,
quadratic: 0.032,
}
}
}

View File

@ -62,6 +62,15 @@ pub struct Mesh {
EBO: u32, EBO: u32,
} }
impl Drop for Mesh {
fn drop(&mut self) {
unsafe {
gl::DeleteVertexArrays(1, &self.VAO);
gl::DeleteBuffers(1, &self.VBO);
}
}
}
impl Mesh { impl Mesh {
pub fn new(vertices: Vec<Vertex>, indices: Vec<u32>, textures: Vec<Texture>) -> Mesh { pub fn new(vertices: Vec<Vertex>, indices: Vec<u32>, textures: Vec<Texture>) -> Mesh {
let mut mesh = Mesh { let mut mesh = Mesh {

View File

@ -7,6 +7,7 @@ pub mod client;
pub mod components; pub mod components;
pub mod consts; pub mod consts;
pub mod engine; pub mod engine;
pub mod light;
pub mod mesh; pub mod mesh;
pub mod model; pub mod model;
pub mod shader; pub mod shader;