From 311eb0227c4317aef29fd7d7837e49c25bd53734 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Wed, 6 Apr 2022 17:16:24 -0500 Subject: [PATCH] Draw some pretty colors --- src/main.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index f10d89a..caea7ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,14 @@ +#[macro_use] extern crate glium; +#[derive(Copy, Clone)] +struct Vertex { + position: [f32; 2], + color: [f32; 3], +} + +implement_vertex!(Vertex, position, color); + fn main() { use glium::{glutin, Surface}; @@ -10,12 +19,48 @@ fn main() { let display = glium::Display::new(wb, cb, &events_loop).unwrap(); + let vertex1 = Vertex { position: [-0.5, -0.5], color: [1.0, 0.0, 0.0] }; + let vertex2 = Vertex { position: [ 0.0, 0.5], color: [0.0, 1.0, 0.0] }; + let vertex3 = Vertex { position: [ 0.5, -0.25], color: [0.0, 0.0, 1.0]}; + 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 = r#" + #version 330 + + in vec2 position; + in vec3 color; + + out vec3 DIFFUSE; + + void main() { + gl_Position = vec4(position, 0.0, 1.0); + DIFFUSE = color; + } +"#; + let fragment_shader_src = r#" + #version 330 + + in vec3 DIFFUSE; + out vec4 color; + + void main() { + color = vec4(DIFFUSE, 1.0); + } +"#; + let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap(); + events_loop.run(move |ev, _, control_flow| { let mut target = display.draw(); - target.clear_color(0.0, 0.0, 1.0, 1.0); + target.clear_color(0.0, 0.0, 0.0, 1.0); + target.draw(&vertex_buffer, &indices, &program, &glium::uniforms::EmptyUniforms, + &Default::default()).unwrap(); target.finish().unwrap(); + let next_frame_time = std::time::Instant::now() + std::time::Duration::from_nanos(16_666_667); *control_flow = glutin::event_loop::ControlFlow::WaitUntil(next_frame_time);