Create a gist now

Instantly share code, notes, and snippets.

@phaazon /main.rs
Last active Sep 14, 2017

What would you like to do?
lumitest
extern crate luminance;
extern crate luminance_glfw;
use luminance::framebuffer::Framebuffer;
use luminance::pipeline::{entry, pipeline};
use luminance::shader::program::{Program, ProgramError, Uniform, UniformBuilder, UniformInterface, UniformWarning};
use luminance::tess::{Mode, Tess, TessVertices};
use luminance_glfw::{Action, Device, Key, WindowDim, WindowOpt, WindowEvent};
use std::time::Instant;
const SCREEN_WIDTH: u32 = 960;
const SCREEN_HEIGHT: u32 = 540;
fn main() {
let rdev = Device::new(WindowDim::Windowed(SCREEN_WIDTH, SCREEN_HEIGHT), "lumitest", WindowOpt::default());
match rdev {
Err(e) => {
eprintln!("{:#?}", e);
::std::process::exit(1);
}
Ok(mut dev) => {
println!("let’s go!");
let triangle = Tess::new(Mode::Triangle, TessVertices::Fill(&TRIANGLE_VERTS), None);
let screen = Framebuffer::default([SCREEN_WIDTH, SCREEN_HEIGHT]);
let (shader, warnings) = Program::<Vertex, (), TimeUniform>::from_strings(None, SHADER_VS, None, SHADER_FS).unwrap();
for warning in &warnings {
eprintln!("{:#?}", warning);
}
let t_start = Instant::now();
'app: loop {
for (_, ev) in dev.events() { // the pair is an interface mistake; it’ll be removed
match ev {
WindowEvent::Close | WindowEvent::Key(Key::Escape, _, Action::Release, _) => break 'app,
_ => ()
}
}
let t_dur = t_start.elapsed();
let t = (t_dur.as_secs() as f64 + t_dur.subsec_nanos() as f64 * 1e-9) as f32;
println!("{}", t);
dev.draw(|| {
entry(|_| {
pipeline(&screen, [0., 0., 0., 1.], |shd_gate| {
shd_gate.shade(&shader, |rdr_gate, uniforms| {
// update the time
uniforms.0.update(t);
rdr_gate.render(None, true, |tess_gate| {
let t = &triangle;
tess_gate.render(t.into());
});
});
});
});
});
}
}
}
}
type Position = [f32; 2];
type RGB = [f32; 3];
type Vertex = (Position, RGB);
const TRIANGLE_VERTS: [Vertex; 3] = [
([-0.5, -0.5], [0.8, 0.5, 0.5]), // red bottom leftmost
([-0., 0.5], [0.5, 0.8, 0.5]), // green top
([0.5, -0.5], [0.5, 0.5, 0.8]) // blue bottom rightmost
];
const SHADER_VS: &str = include_str!("../data/vs.glsl");
const SHADER_FS: &str = include_str!("../data/fs.glsl");
struct TimeUniform(Uniform<f32>);
impl UniformInterface for TimeUniform {
fn uniform_interface(builder: UniformBuilder) -> Result<(Self, Vec<UniformWarning>), ProgramError> {
// this will fail if the "t" variable is not used in the shader
//let t = builder.ask("t").map_err(ProgramError::UniformWarning)?;
// I rather like this one: we just forward up the warning and use the special unbound uniform
match builder.ask("t") {
Ok(t) => Ok((TimeUniform(t), Vec::new())),
Err(e) => Ok((TimeUniform(builder.unbound()), vec![e]))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment