-
Notifications
You must be signed in to change notification settings - Fork 1
/
triangle.rs
201 lines (182 loc) · 5.51 KB
/
triangle.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! Usage example of drawing a triangle.
//!
//! Look for `old_school_gfx_glutin_ext` to see what this crate is doing:
//! * Initialise window & gfx.
//! * Resize gfx views.
#[macro_use]
extern crate gfx;
use gfx::{traits::FactoryExt, Device};
use glutin::{
context::PossiblyCurrentContext,
surface::{GlSurface, Surface, WindowSurface},
};
use std::{error::Error, num::NonZeroU32};
use winit::{
dpi::PhysicalSize,
event::{KeyEvent, WindowEvent},
event_loop::{ActiveEventLoop, ControlFlow, EventLoop},
keyboard::{Key, NamedKey},
window::Window,
};
type ColorFormat = gfx::format::Srgba8;
type DepthFormat = gfx::format::DepthStencil;
gfx_defines! {
vertex Vertex {
pos: [f32; 2] = "pos",
color: [f32; 3] = "color",
}
pipeline pipe {
vbuf: gfx::VertexBuffer<Vertex> = (),
out: gfx::RenderTarget<ColorFormat> = "target",
}
}
impl Eq for pipe::Meta {}
const TRIANGLE: [Vertex; 3] = [
Vertex {
pos: [-0.5, -0.5],
color: [1.0, 0.0, 0.0],
},
Vertex {
pos: [0.5, -0.5],
color: [0.0, 1.0, 0.0],
},
Vertex {
pos: [0.0, 0.5],
color: [0.0, 0.0, 1.0],
},
];
const CLEAR_COLOR: [f32; 4] = [0.1, 0.11, 0.12, 1.0];
pub fn main() -> Result<(), Box<dyn Error>> {
Ok(EventLoop::new()?.run_app(&mut WinitApp::None)?)
}
enum WinitApp {
None,
Resumed(App),
}
impl winit::application::ApplicationHandler for WinitApp {
fn resumed(&mut self, events: &ActiveEventLoop) {
events.set_control_flow(ControlFlow::Poll);
*self = Self::Resumed(App::new(events).unwrap());
}
fn window_event(
&mut self,
events: &ActiveEventLoop,
_: winit::window::WindowId,
event: WindowEvent,
) {
if let Self::Resumed(app) = self {
app.window_event(events, event);
}
}
fn about_to_wait(&mut self, _events: &ActiveEventLoop) {
if let Self::Resumed(App { window, .. }) = self {
window.request_redraw();
};
}
}
struct App {
window: Window,
gl_surface: Surface<WindowSurface>,
gl_context: PossiblyCurrentContext,
device: gfx_device_gl::Device,
encoder: gfx::Encoder<gfx_device_gl::Resources, gfx_device_gl::CommandBuffer>,
dimensions: PhysicalSize<u32>,
data: pipe::Data<gfx_device_gl::Resources>,
slice: gfx::Slice<gfx_device_gl::Resources>,
pso: gfx::PipelineState<gfx_device_gl::Resources, pipe::Meta>,
depth_view: gfx::handle::DepthStencilView<gfx_device_gl::Resources, DepthFormat>,
}
impl App {
fn new(events: &ActiveEventLoop) -> Result<Self, Box<dyn Error>> {
let old_school_gfx_glutin_ext::Init {
window,
gl_surface,
gl_context,
device,
mut factory,
color_view,
depth_view,
..
} = old_school_gfx_glutin_ext::window_builder(
events,
Window::default_attributes()
.with_title("Triangle")
.with_inner_size(winit::dpi::PhysicalSize::new(1024, 768)),
)
.build::<ColorFormat, DepthFormat>()?;
let encoder = gfx::Encoder::from(factory.create_command_buffer());
let pso = factory.create_pipeline_simple(
include_bytes!("triangle.vs.glsl"),
include_bytes!("triangle.fs.glsl"),
pipe::new(),
)?;
let (vertex_buffer, slice) = factory.create_vertex_buffer_with_slice(&TRIANGLE, ());
let data = pipe::Data {
vbuf: vertex_buffer,
out: color_view,
};
let dimensions = window.inner_size();
Ok(Self {
window,
gl_surface,
gl_context,
device,
encoder,
dimensions,
data,
slice,
pso,
depth_view,
})
}
fn window_event(&mut self, elwt: &ActiveEventLoop, event: WindowEvent) {
let Self {
window,
gl_surface,
gl_context,
device,
encoder,
dimensions,
data,
slice,
pso,
depth_view,
} = self;
match event {
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Named(NamedKey::Escape),
..
},
..
} => elwt.exit(),
WindowEvent::RedrawRequested => {
// handle resizes
let window_size = window.inner_size();
if *dimensions != window_size {
if let (Some(w), Some(h)) = (
NonZeroU32::new(window_size.width),
NonZeroU32::new(window_size.height),
) {
gl_surface.resize(gl_context, w, h);
old_school_gfx_glutin_ext::resize_views(
window_size,
&mut data.out,
depth_view,
);
}
*dimensions = window_size;
}
// draw a frame
encoder.clear(&data.out, CLEAR_COLOR);
encoder.draw(slice, pso, data);
encoder.flush(device);
gl_surface.swap_buffers(gl_context).unwrap();
device.cleanup();
}
_ => (),
}
}
}