-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.rs
102 lines (76 loc) · 2.21 KB
/
camera.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
extern crate lingo;
use lingo::{draw, gl};
include!("examples_shared.rs");
const CAMERA_VERT: &'static str = r#"#version 300 es
precision mediump float;
layout(location=0) in vec2 at_loc;
uniform mat4 un_mvp;
void main() {
gl_Position = un_mvp * vec4(at_loc, 0, 1);
}
"#;
const CAMERA_FRAG: &'static str = r#"#version 300 es
precision mediump float;
out vec4 out_color;
void main() {
out_color = vec4(1, 0, 0, 1);
}
"#;
struct Vertex(i16, i16);
fn main() {
if let Err(msg) = sample() {
eprintln!("Example error: {}", msg);
}
}
fn sample() -> Result<(), String> {
// Create environment
// Se hello_triangle.rs for description of this part
let mut win = Window::new();
let prog = draw::ProgramBuilder::new()?
.vertex_shader(CAMERA_VERT.to_string())?
.fragment_shader(CAMERA_FRAG.to_string())?
.link()?
.build();
let mut verts = draw::HwBuf::new(5, draw::Usage::Static)?;
verts.push(Vertex(0, 0));
verts.push(Vertex(1, 0));
verts.push(Vertex(0, 1));
verts.prepear_graphics();
let mut pipeline = draw::Pipeline::new(draw::PrimitiveType::Triangles)?;
let buf_id = pipeline.push_buffer(&verts, 0);
pipeline.push_attribute(buf_id, 2, draw::DataType::I16, false);
let location_mvp = prog.get_uniform_location("un_mvp");
draw::print_gl_error()?;
unsafe {
gl::ClearColor(0.3, 0.4, 0.5, 1.0);
}
loop {
unsafe {
gl::Clear(gl::COLOR_BUFFER_BIT);
}
let mut mat = draw::Matrix4x4::new();
// 2D Orthogonal mode
//mat.ortho(-4f32, -3f32, 4f32, 3f32);
// 3D camera mode
mat.camera_3d(
1.3f32,
1.3333f32,
0.1f32,
20f32,
draw::Vec3(2f32, 1f32, 1f32), // Eye
draw::Vec3(0f32, 0f32, 0f32), // At
draw::Vec3(0f32, 0f32, 1f32),
); // Center
prog.use_program();
prog.set_uniform(&location_mvp, |loc| unsafe {
gl::UniformMatrix4fv(loc, 1, gl::FALSE, mat.values.as_ptr());
});
verts.bind();
pipeline.draw(3);
draw::print_gl_error().unwrap();
if win.next() {
break;
}
}
Ok(())
}