/
cube.rs
180 lines (157 loc) · 5.67 KB
/
cube.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
extern crate piston_window;
extern crate vecmath;
extern crate camera_controllers;
#[macro_use]
extern crate gfx;
extern crate shader_version;
//----------------------------------------
// Cube associated data
gfx_vertex_struct!( Vertex {
a_pos: [i8; 4] = "a_pos",
a_tex_coord: [i8; 2] = "a_tex_coord",
});
impl Vertex {
fn new(pos: [i8; 3], tc: [i8; 2]) -> Vertex {
Vertex {
a_pos: [pos[0], pos[1], pos[2], 1],
a_tex_coord: tc,
}
}
}
gfx_pipeline!( pipe {
vbuf: gfx::VertexBuffer<Vertex> = (),
u_model_view_proj: gfx::Global<[[f32; 4]; 4]> = "u_model_view_proj",
t_color: gfx::TextureSampler<[f32; 4]> = "t_color",
out_color: gfx::RenderTarget<::gfx::format::Srgba8> = "o_Color",
out_depth: gfx::DepthTarget<::gfx::format::DepthStencil> =
gfx::preset::depth::LESS_EQUAL_WRITE,
});
//----------------------------------------
fn main() {
use piston_window::*;
use gfx::traits::*;
use shader_version::Shaders;
use shader_version::glsl::GLSL;
use camera_controllers::{
FirstPersonSettings,
FirstPerson,
CameraPerspective,
model_view_projection
};
let opengl = OpenGL::V3_2;
let mut window: PistonWindow =
WindowSettings::new("piston: cube", [640, 480])
.exit_on_esc(true)
.samples(4)
.graphics_api(opengl)
.build()
.unwrap();
window.set_capture_cursor(true);
let ref mut factory = window.factory.clone();
let vertex_data = vec![
//top (0, 0, 1)
Vertex::new([-1, -1, 1], [0, 0]),
Vertex::new([ 1, -1, 1], [1, 0]),
Vertex::new([ 1, 1, 1], [1, 1]),
Vertex::new([-1, 1, 1], [0, 1]),
//bottom (0, 0, -1)
Vertex::new([ 1, 1, -1], [0, 0]),
Vertex::new([-1, 1, -1], [1, 0]),
Vertex::new([-1, -1, -1], [1, 1]),
Vertex::new([ 1, -1, -1], [0, 1]),
//right (1, 0, 0)
Vertex::new([ 1, -1, -1], [0, 0]),
Vertex::new([ 1, 1, -1], [1, 0]),
Vertex::new([ 1, 1, 1], [1, 1]),
Vertex::new([ 1, -1, 1], [0, 1]),
//left (-1, 0, 0)
Vertex::new([-1, 1, 1], [0, 0]),
Vertex::new([-1, -1, 1], [1, 0]),
Vertex::new([-1, -1, -1], [1, 1]),
Vertex::new([-1, 1, -1], [0, 1]),
//front (0, 1, 0)
Vertex::new([-1, 1, -1], [0, 0]),
Vertex::new([ 1, 1, -1], [1, 0]),
Vertex::new([ 1, 1, 1], [1, 1]),
Vertex::new([-1, 1, 1], [0, 1]),
//back (0, -1, 0)
Vertex::new([ 1, -1, 1], [0, 0]),
Vertex::new([-1, -1, 1], [1, 0]),
Vertex::new([-1, -1, -1], [1, 1]),
Vertex::new([ 1, -1, -1], [0, 1]),
];
let index_data: &[u16] = &[
0, 1, 2, 2, 3, 0, // top
4, 6, 5, 6, 4, 7, // bottom
8, 9, 10, 10, 11, 8, // right
12, 14, 13, 14, 12, 15, // left
16, 18, 17, 18, 16, 19, // front
20, 21, 22, 22, 23, 20, // back
];
let (vbuf, slice) = factory.create_vertex_buffer_with_slice
(&vertex_data, index_data);
let texels = [
[0xff, 0xff, 0xff, 0x00],
[0xff, 0x00, 0x00, 0x00],
[0x00, 0xff, 0x00, 0x00],
[0x00, 0x00, 0xff, 0x00]
];
let (_, texture_view) = factory.create_texture_immutable::<gfx::format::Rgba8>(
gfx::texture::Kind::D2(2, 2, gfx::texture::AaMode::Single),
gfx::texture::Mipmap::Provided,
&[&texels]).unwrap();
let sinfo = gfx::texture::SamplerInfo::new(
gfx::texture::FilterMethod::Bilinear,
gfx::texture::WrapMode::Clamp);
let glsl = opengl.to_glsl();
let pso = factory.create_pipeline_simple(
Shaders::new()
.set(GLSL::V1_20, include_str!("../assets/cube_120.glslv"))
.set(GLSL::V1_50, include_str!("../assets/cube_150.glslv"))
.get(glsl).unwrap().as_bytes(),
Shaders::new()
.set(GLSL::V1_20, include_str!("../assets/cube_120.glslf"))
.set(GLSL::V1_50, include_str!("../assets/cube_150.glslf"))
.get(glsl).unwrap().as_bytes(),
pipe::new()
).unwrap();
let get_projection = |w: &PistonWindow| {
let draw_size = w.window.draw_size();
CameraPerspective {
fov: 90.0, near_clip: 0.1, far_clip: 1000.0,
aspect_ratio: (draw_size.width as f32) / (draw_size.height as f32)
}.projection()
};
let model = vecmath::mat4_id();
let mut projection = get_projection(&window);
let mut first_person = FirstPerson::new(
[0.5, 0.5, 4.0],
FirstPersonSettings::keyboard_wasd()
);
let mut data = pipe::Data {
vbuf: vbuf.clone(),
u_model_view_proj: [[0.0; 4]; 4],
t_color: (texture_view, factory.create_sampler(sinfo)),
out_color: window.output_color.clone(),
out_depth: window.output_stencil.clone(),
};
while let Some(e) = window.next() {
first_person.event(&e);
window.draw_3d(&e, |window| {
let args = e.render_args().unwrap();
window.encoder.clear(&window.output_color, [0.3, 0.3, 0.3, 1.0]);
window.encoder.clear_depth(&window.output_stencil, 1.0);
data.u_model_view_proj = model_view_projection(
model,
first_person.camera(args.ext_dt).orthogonal(),
projection
);
window.encoder.draw(&slice, &pso, &data);
});
if let Some(_) = e.resize_args() {
projection = get_projection(&window);
data.out_color = window.output_color.clone();
data.out_depth = window.output_stencil.clone();
}
}
}