-
Notifications
You must be signed in to change notification settings - Fork 0
/
stride.rs
86 lines (63 loc) · 1.77 KB
/
stride.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
extern crate lingo;
use lingo::{draw, gl};
use std::mem;
include!("examples_shared.rs");
const STRIDE_VERT: &'static str = r#"#version 300 es
precision mediump float;
layout(location=0) in vec2 at_loc;
layout(location=1) in vec4 at_color;
out vec4 va_color;
void main() {
va_color = at_color;
gl_Position = vec4(at_loc, 0, 1);
}
"#;
const STRIDE_FRAG: &'static str = r#"#version 300 es
precision mediump float;
in vec4 va_color;
out vec4 out_color;
void main() {
out_color = va_color;
}
"#;
#[repr(C, packed)]
struct Vertex(f32, f32, u8, u8, u8, u8);
fn main() {
if let Err(msg) = sample() {
eprintln!("Example error: {}", msg);
}
}
fn sample() -> Result<(), String> {
let mut win = Window::new();
let prog = draw::ProgramBuilder::new()?
.vertex_shader(STRIDE_VERT.to_string())?
.fragment_shader(STRIDE_FRAG.to_string())?
.link()?
.build();
let mut verts = draw::HwBuf::new(3, draw::Usage::Static)?;
verts.push(Vertex(0.0, 0.0, 1, 0, 0, 1));
verts.push(Vertex(1.0, 0.0, 0, 1, 0, 1));
verts.push(Vertex(0.0, 1.0, 0, 0, 1, 1));
verts.prepear_graphics();
let mut pipeline = draw::Pipeline::new(draw::PrimitiveType::Triangles)?;
let buf_id = pipeline.push_buffer(&verts, mem::size_of::<Vertex>());
pipeline.push_attribute(buf_id, 2, draw::DataType::F32, false);
pipeline.push_attribute(buf_id, 4, draw::DataType::U8, false);
draw::print_gl_error()?;
unsafe {
gl::ClearColor(0.3, 0.4, 0.5, 1.0);
}
loop {
unsafe {
gl::Clear(gl::COLOR_BUFFER_BIT);
}
prog.use_program();
verts.bind();
pipeline.draw(3);
draw::print_gl_error().unwrap();
if win.next() {
break;
}
}
Ok(())
}