simplification
This commit is contained in:
parent
6b9869cd37
commit
d995d62b89
|
@ -1,9 +1,13 @@
|
||||||
#version 140
|
#version 140
|
||||||
|
|
||||||
|
in vec3 v_normal;
|
||||||
out vec4 color;
|
out vec4 color;
|
||||||
|
uniform vec3 u_light;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
color = vec4(1.0, 0.0, 0.0, 1.0);
|
float brightness = dot(normalize(v_normal), normalize(u_light));
|
||||||
|
vec3 dark_color = vec3(0.6, 0.0, 0.0);
|
||||||
|
vec3 regular_color = vec3(1.0, 0.0, 0.0);
|
||||||
|
color = vec4(mix(dark_color, regular_color, brightness), 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
#version 140
|
#version 150
|
||||||
|
|
||||||
in vec3 position;
|
in vec3 position;
|
||||||
in vec3 normal;
|
in vec3 normal;
|
||||||
|
|
||||||
|
out vec3 v_normal;
|
||||||
|
|
||||||
uniform mat4 matrix;
|
uniform mat4 matrix;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
v_normal = transpose(inverse(mat3(matrix))) * normal;
|
||||||
gl_Position = matrix * vec4(position, 1.0);
|
gl_Position = matrix * vec4(position, 1.0);
|
||||||
}
|
}
|
106
src/main.rs
106
src/main.rs
|
@ -1,6 +1,6 @@
|
||||||
#![feature(const_fn_trait_bound)]
|
#![feature(const_fn_trait_bound)]
|
||||||
|
|
||||||
use std::{collections::HashMap, io::Cursor};
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate glium;
|
extern crate glium;
|
||||||
|
@ -20,60 +20,19 @@ fn main() {
|
||||||
.with_title("Voxel Engine")
|
.with_title("Voxel Engine")
|
||||||
.with_resizable(true)
|
.with_resizable(true)
|
||||||
.with_transparent(true);
|
.with_transparent(true);
|
||||||
let cb = glutin::ContextBuilder::new();
|
let cb = glutin::ContextBuilder::new().with_depth_buffer(24);
|
||||||
|
|
||||||
let display = glium::Display::new(wb, cb, &event_loop).unwrap();
|
let display = glium::Display::new(wb, cb, &event_loop).unwrap();
|
||||||
|
|
||||||
let image = image::load(
|
// let mut shaders = HashMap::new();
|
||||||
Cursor::new(&include_bytes!("../assets/GrassTop.png")),
|
|
||||||
image::ImageFormat::Png,
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
.to_rgba8();
|
|
||||||
let image_dimensions = image.dimensions();
|
|
||||||
let image =
|
|
||||||
glium::texture::RawImage2d::from_raw_rgba_reversed(&image.into_raw(), image_dimensions);
|
|
||||||
let texture = glium::texture::SrgbTexture2d::new(&display, image).unwrap();
|
|
||||||
|
|
||||||
#[derive(Copy, Clone)]
|
let vertex_shader_src = include_str!("../assets/shaders/teapot/teapot.vert");
|
||||||
struct Vertex {
|
let fragment_shader_src = include_str!("../assets/shaders/teapot/teapot.frag");
|
||||||
position: [f32; 2],
|
|
||||||
tex_coords: [f32; 2],
|
|
||||||
light_color: [f32; 4],
|
|
||||||
}
|
|
||||||
|
|
||||||
implement_vertex!(Vertex, position, tex_coords, light_color);
|
|
||||||
|
|
||||||
let vertex1 = Vertex {
|
|
||||||
position: [-0.5, -0.5],
|
|
||||||
tex_coords: [0.0, 0.0],
|
|
||||||
light_color: [1.0, 1.0, 1.0, 0.0],
|
|
||||||
};
|
|
||||||
|
|
||||||
let vertex2 = Vertex {
|
|
||||||
position: [0.0, 0.5],
|
|
||||||
tex_coords: [0.0, 1.0],
|
|
||||||
light_color: [1.0, 0.0, 0.0, 1.0],
|
|
||||||
};
|
|
||||||
|
|
||||||
let vertex3 = Vertex {
|
|
||||||
position: [0.5, -0.25],
|
|
||||||
tex_coords: [1.0, 0.0],
|
|
||||||
light_color: [0.0, 1.0, 0.0, 0.5],
|
|
||||||
};
|
|
||||||
|
|
||||||
let shape = vec![vertex1, vertex2, vertex3];
|
|
||||||
|
|
||||||
let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();
|
|
||||||
let indices = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList);
|
|
||||||
|
|
||||||
let vertex_shader_src = include_str!("../assets/shade.vert");
|
|
||||||
let fragment_shader_src = include_str!("../assets/shade.frag");
|
|
||||||
|
|
||||||
let program =
|
let program =
|
||||||
glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None)
|
glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut t: f32 = -0.5;
|
|
||||||
event_loop.run(move |event, _, control_flow| {
|
event_loop.run(move |event, _, control_flow| {
|
||||||
use glutin::event::Event::*;
|
use glutin::event::Event::*;
|
||||||
match event {
|
match event {
|
||||||
|
@ -127,45 +86,12 @@ fn main() {
|
||||||
std::time::Instant::now() + std::time::Duration::from_nanos(16_666_667);
|
std::time::Instant::now() + std::time::Duration::from_nanos(16_666_667);
|
||||||
*control_flow = glutin::event_loop::ControlFlow::WaitUntil(next_frame_time);
|
*control_flow = glutin::event_loop::ControlFlow::WaitUntil(next_frame_time);
|
||||||
|
|
||||||
// we update `t`
|
|
||||||
t += 0.0002;
|
|
||||||
if t > 0.5 {
|
|
||||||
t = -0.5;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut target = display.draw();
|
let mut target = display.draw();
|
||||||
target.clear_color(0.0, 0.0, 1.0, 1.0);
|
|
||||||
|
|
||||||
{
|
target.clear_color_and_depth((0.0, 0.0, 1.0, 1.0), 1.0);
|
||||||
// draw the triangle
|
|
||||||
let uniforms = uniform! {
|
|
||||||
matrix: [
|
|
||||||
[1.0, 0.0, 0.0, 0.0],
|
|
||||||
[0.0, 1.0, 0.0, 0.0],
|
|
||||||
[0.0, 0.0, 1.0, 0.0],
|
|
||||||
[ t , 0.0, 0.0, 1.0f32],
|
|
||||||
],
|
|
||||||
tex: &texture,
|
|
||||||
};
|
|
||||||
|
|
||||||
target
|
|
||||||
.draw(
|
|
||||||
&vertex_buffer,
|
|
||||||
&indices,
|
|
||||||
&program,
|
|
||||||
&uniforms,
|
|
||||||
&Default::default(),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
{
|
{
|
||||||
// Teapot stage
|
// Teapot stage
|
||||||
let vertex_shader_src = include_str!("../assets/shaders/teapot/teapot.vert");
|
|
||||||
let fragment_shader_src = include_str!("../assets/shaders/teapot/teapot.frag");
|
|
||||||
|
|
||||||
let program =
|
|
||||||
glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let positions = glium::VertexBuffer::new(&display, &teapot::VERTICES).unwrap();
|
let positions = glium::VertexBuffer::new(&display, &teapot::VERTICES).unwrap();
|
||||||
let normals = glium::VertexBuffer::new(&display, &teapot::NORMALS).unwrap();
|
let normals = glium::VertexBuffer::new(&display, &teapot::NORMALS).unwrap();
|
||||||
|
@ -183,17 +109,31 @@ fn main() {
|
||||||
[0.0, 0.0, 0.0, 1.0f32],
|
[0.0, 0.0, 0.0, 1.0f32],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
let light = [-1.0, 0.4, 0.9f32];
|
||||||
|
|
||||||
|
let params = glium::DrawParameters {
|
||||||
|
depth: glium::Depth {
|
||||||
|
test: glium::draw_parameters::DepthTest::IfLess,
|
||||||
|
write: true,
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
target
|
target
|
||||||
.draw(
|
.draw(
|
||||||
(&positions, &normals),
|
(&positions, &normals),
|
||||||
&indices,
|
&indices,
|
||||||
&program,
|
&program,
|
||||||
&uniform! { matrix: matrix },
|
&uniform! { matrix: matrix, u_light: light },
|
||||||
&Default::default(),
|
¶ms,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{ // Water Stage
|
||||||
|
}
|
||||||
|
|
||||||
target.finish().unwrap();
|
target.finish().unwrap();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue