First Commit
This commit is contained in:
commit
0135974d08
7 changed files with 4979 additions and 0 deletions
127
src/main.rs
Normal file
127
src/main.rs
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
#[macro_use]
|
||||
extern crate glium;
|
||||
use glium::Surface;
|
||||
mod teapot;
|
||||
|
||||
fn main() {
|
||||
let event_loop = glium::winit::event_loop::EventLoop::builder()
|
||||
.build()
|
||||
.expect("event loop building");
|
||||
let (window, display) = glium::backend::glutin::SimpleWindowBuilder::new()
|
||||
.with_title("Glium tutorial #3")
|
||||
.build(&event_loop);
|
||||
|
||||
|
||||
let positions = glium::VertexBuffer::new(&display, &teapot::VERTICES).unwrap();
|
||||
let normals = glium::VertexBuffer::new(&display, &teapot::NORMALS).unwrap();
|
||||
let indices = glium::IndexBuffer::new(&display, glium::index::PrimitiveType::TrianglesList,
|
||||
&teapot::INDICES).unwrap();
|
||||
|
||||
let vertex_shader_src = r#"
|
||||
#version 140
|
||||
|
||||
in vec3 position;
|
||||
in vec3 normal;
|
||||
|
||||
out vec3 v_normal;
|
||||
|
||||
uniform mat4 matrix;
|
||||
uniform mat4 perspective;
|
||||
|
||||
void main() {
|
||||
v_normal = transpose(inverse(mat3(matrix))) * normal;
|
||||
gl_Position = perspective * matrix * vec4(position, 1.0);
|
||||
}
|
||||
"#;
|
||||
let fragment_shader_src = r#"
|
||||
#version 140
|
||||
|
||||
in vec3 v_normal;
|
||||
out vec4 color;
|
||||
uniform vec3 u_light;
|
||||
|
||||
void main() {
|
||||
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);
|
||||
}
|
||||
"#;
|
||||
let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap();
|
||||
|
||||
let light = [-1.0, 0.4, 0.9f32];
|
||||
let mut t: f32 = 0.0;
|
||||
#[allow(deprecated)]
|
||||
event_loop.run(move |ev, window_target| {
|
||||
match ev {
|
||||
glium::winit::event::Event::WindowEvent { event, .. } => match event {
|
||||
glium::winit::event::WindowEvent::CloseRequested => {
|
||||
window_target.exit();
|
||||
},
|
||||
// We now need to render everyting in response to a RedrawRequested event due to the animation
|
||||
glium::winit::event::WindowEvent::RedrawRequested => {
|
||||
let mut target = display.draw();
|
||||
target.clear_color_and_depth((0.0, 0.0, 0.0, 1.0), 1.0);
|
||||
t += 0.01;
|
||||
let x = t.sin() * 0.5;
|
||||
let y = t.cos() * 0.5;
|
||||
|
||||
let perspective = {
|
||||
let (width, height) = target.get_dimensions();
|
||||
let aspect_ratio = height as f32 / width as f32;
|
||||
|
||||
let fov: f32 = 3.141592 / 3.0;
|
||||
let zfar = 1024.0;
|
||||
let znear = 0.1;
|
||||
|
||||
let f = 1.0 / (fov / 2.0).tan();
|
||||
|
||||
[
|
||||
[f * aspect_ratio , 0.0, 0.0 , 0.0],
|
||||
[ 0.0 , f , 0.0 , 0.0],
|
||||
[ 0.0 , 0.0, (zfar+znear)/(zfar-znear) , 1.0],
|
||||
[ 0.0 , 0.0, -(2.0*zfar*znear)/(zfar-znear), 0.0],
|
||||
]
|
||||
};
|
||||
|
||||
let uniforms = uniform! {
|
||||
matrix: [
|
||||
[0.01, 0.0, 0.0, 0.0],
|
||||
[0.0, 0.01, 0.0, 0.0],
|
||||
[0.0, 0.0, 0.01, 0.0],
|
||||
[x, y, 2.0, 1.0f32 ],
|
||||
],
|
||||
u_light: light,
|
||||
perspective: perspective
|
||||
};
|
||||
|
||||
let params = glium::DrawParameters {
|
||||
depth: glium::Depth {
|
||||
test: glium::draw_parameters::DepthTest::IfLess,
|
||||
write: true,
|
||||
.. Default::default()
|
||||
},
|
||||
.. Default::default()
|
||||
};
|
||||
|
||||
target.draw((&positions, &normals), &indices, &program, &uniforms,
|
||||
¶ms).unwrap();
|
||||
target.finish().unwrap();
|
||||
},
|
||||
// Because glium doesn't know about windows we need to resize the display
|
||||
// when the window's size has changed.
|
||||
glium::winit::event::WindowEvent::Resized(window_size) => {
|
||||
display.resize(window_size.into());
|
||||
},
|
||||
_ => (),
|
||||
},
|
||||
// By requesting a redraw in response to a RedrawEventsCleared event we get continuous rendering.
|
||||
// For applications that only change due to user input you could remove this handler.
|
||||
glium::winit::event::Event::AboutToWait => {
|
||||
window.request_redraw();
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue