Skip to content

Instantly share code, notes, and snippets.

@tyoc213
Created Mar 20, 2016
Embed
What would you like to do?
Why https://open.gl/drawing#Elementbuffers give different result for this?
#[macro_use]
extern crate glium;
extern crate time;
fn main() {
// READ: http://tomaka.github.io/glium/glium/trait.Surface.html#what-does-the-gpu-do-when-you-draw
use glium::Surface;
// http://tomaka.github.io/glium/glium/trait.DisplayBuild.html
use glium::DisplayBuild;
let start_time = time::precise_time_s(); // time::precise_time_ns() and time::get_time()
// http://tomaka.github.io/glium/glium/macro.implement_vertex!.html
#[derive(Copy, Clone)]
struct Vertex {
position: [f32; 2],
color: [f32; 3]
}
implement_vertex!(Vertex, position, color);
let vertex1 = Vertex { position: [-0.5, 0.5], color: [1.0, 0.0, 0.0] };
let vertex2 = Vertex { position: [ 0.5, 0.5], color: [0.0, 1.0, 0.0] };
let vertex3 = Vertex { position: [ 0.5,-0.5], color: [0.0, 0.0, 1.0] };
let vertex4 = Vertex { position: [-0.5,-0.5], color: [1.0, 1.0, 1.0] };
let triangle = vec![vertex1, vertex2, vertex3, vertex4];
let vertex_shader_src = r#"
#version 150
in vec2 position;
in vec3 color;
out vec3 Color;
void main()
{
Color = color;
gl_Position = vec4(position, 0.0, 1.0);
}
"#;
let fragment_shader_src = r#"
#version 150
uniform vec3 triangleColor;
in vec3 Color;
out vec4 outColor;
void main()
{
outColor = vec4(Color, 1.0);
}
"#;
// http://tomaka.github.io/glium/glium/glutin/struct.WindowBuilder.html
let display = glium::glutin::WindowBuilder::new()
.with_dimensions(800, 600)
.with_title(format!("glium tut"))
.build_glium()
.unwrap();
// http://tomaka.github.io/glium/glium/struct.VertexBuffer.html
let vertex_buffer = glium::VertexBuffer::new(&display, &triangle).unwrap();
// http://tomaka.github.io/glium/glium/index/enum.PrimitiveType.html#variant.TrianglesList
// let indices = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList);
// http://tomaka.github.io/glium/glium/struct.IndexBuffer.html
let idx: [u16; 6] = [0, 1, 2, 3, 2, 0];
let indices = glium::IndexBuffer::new(&display, glium::index::PrimitiveType::TrianglesList, &idx).unwrap();
// http://tomaka.github.io/glium/glium/struct.Program.html#method.from_source also see: http://tomaka.github.io/glium/glium/program/struct.ComputeShader.html#method.from_source
let program = glium::Program::from_source(&display, vertex_shader_src, fragment_shader_src, None).unwrap();
loop {
let mut target = display.draw();
target.clear_color(0.5, 0.5, 0.5, 1.0);
let step = time::precise_time_s() - start_time;
let red_value: f32 = (4f32 * f32::sin(step as f32) + 1f32) / 2f32;
// http://tomaka.github.io/glium/glium/macro.uniform!.html
let uniforms = uniform!(triangleColor: [red_value, 0.0, 0.0]); // f32 is needed to be a float vec3 instead of a double float vec3
// http://tomaka.github.io/glium/glium/struct.Frame.html#method.draw
target.draw(&vertex_buffer, &indices, &program, &uniforms, &Default::default()).unwrap();
// http://tomaka.github.io/glium/glium/struct.Frame.html#method.finish
target.finish().unwrap();
// http://tomaka.github.io/glium/glutin/struct.Window.html#method.poll_events
for ev in display.poll_events() {
match ev {
glium::glutin::Event::Closed => return,
_ => ()
}
}
}
}
@tyoc213

This comment has been minimized.

Copy link
Owner Author

@tyoc213 tyoc213 commented Mar 28, 2016

[12:57 27] tomaka let program = program!(&display, 150 => { vertex: vertex_shader_src, fragment: fragment_shader_src, outputs_srgb: true}).unwrap();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment