-
Notifications
You must be signed in to change notification settings - Fork 70
/
render_to_egui_image.rs
280 lines (233 loc) · 8.71 KB
/
render_to_egui_image.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use egui::load::SizedTexture;
use glam::{vec3, EulerRot, Mat4};
use {egui_miniquad as egui_mq, miniquad as mq};
struct Stage {
egui_mq: egui_mq::EguiMq,
offscreen_pipeline: mq::Pipeline,
offscreen_bind: mq::Bindings,
offscreen_pass: mq::RenderPass,
rx: f32,
ry: f32,
mq_ctx: Box<dyn mq::RenderingBackend>,
}
impl Stage {
pub fn new() -> Stage {
let mut mq_ctx = mq::window::new_rendering_backend();
let color_img = mq_ctx.new_render_texture(mq::TextureParams {
width: 256,
height: 256,
format: mq::TextureFormat::RGBA8,
..Default::default()
});
let depth_img = mq_ctx.new_render_texture(mq::TextureParams {
width: 256,
height: 256,
format: mq::TextureFormat::Depth,
..Default::default()
});
let offscreen_pass = mq_ctx.new_render_pass(color_img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.0, 0.5, 1.0, 1.0, 0.0,
1.0, 1.0, 1.0, 1.0, 0.0, 0.5, 1.0, 1.0, 1.0,
1.0, 1.0, -1.0, 1.0, 0.0, 0.5, 1.0, 0.0, 1.0
];
let vertex_buffer = mq_ctx.new_buffer(
mq::BufferType::VertexBuffer,
mq::BufferUsage::Immutable,
mq::BufferSource::slice(vertices),
);
#[rustfmt::skip]
let indices: &[u16] = &[
0, 1, 2, 0, 2, 3,
6, 5, 4, 7, 6, 4,
8, 9, 10, 8, 10, 11,
14, 13, 12, 15, 14, 12,
16, 17, 18, 16, 18, 19,
22, 21, 20, 23, 22, 20
];
let index_buffer = mq_ctx.new_buffer(
mq::BufferType::IndexBuffer,
mq::BufferUsage::Immutable,
mq::BufferSource::slice(indices),
);
let offscreen_bind = mq::Bindings {
vertex_buffers: vec![vertex_buffer],
index_buffer,
images: vec![],
};
let offscreen_shader = mq_ctx
.new_shader(
mq::ShaderSource::Glsl {
vertex: offscreen_shader::VERTEX,
fragment: offscreen_shader::FRAGMENT,
},
offscreen_shader::meta(),
)
.unwrap();
let offscreen_pipeline = mq_ctx.new_pipeline(
&[mq::BufferLayout {
stride: 36,
..Default::default()
}],
&[
mq::VertexAttribute::new("pos", mq::VertexFormat::Float3),
mq::VertexAttribute::new("color0", mq::VertexFormat::Float4),
],
offscreen_shader,
mq::PipelineParams {
depth_test: mq::Comparison::LessOrEqual,
depth_write: true,
..Default::default()
},
);
Stage {
egui_mq: egui_mq::EguiMq::new(&mut *mq_ctx),
offscreen_pipeline,
offscreen_bind,
offscreen_pass,
rx: 0.,
ry: 0.,
mq_ctx,
}
}
}
impl mq::EventHandler for Stage {
fn update(&mut self) {}
fn draw(&mut self) {
let (width, height) = mq::window::screen_size();
let proj = Mat4::perspective_rh_gl(60.0f32.to_radians(), width / height, 0.01, 10.0);
let view = Mat4::look_at_rh(
vec3(0.0, 1.5, 3.0),
vec3(0.0, 0.0, 0.0),
vec3(0.0, 1.0, 0.0),
);
let view_proj = proj * view;
self.rx += 0.01;
self.ry += 0.03;
let model = Mat4::from_euler(EulerRot::YXZ, self.rx, self.ry, 0.);
let vs_params = offscreen_shader::Uniforms {
mvp: view_proj * model,
};
// the offscreen pass, rendering an rotating, untextured cube into a render target image
self.mq_ctx.begin_pass(
Some(self.offscreen_pass),
mq::PassAction::clear_color(1.0, 1.0, 1.0, 1.),
);
self.mq_ctx.apply_pipeline(&self.offscreen_pipeline);
self.mq_ctx.apply_bindings(&self.offscreen_bind);
self.mq_ctx
.apply_uniforms(mq::UniformsSource::table(&vs_params));
self.mq_ctx.draw(0, 36, 1);
self.mq_ctx.end_render_pass();
// Extract texture from offscreen render pass
let mq_texture = self.mq_ctx.render_pass_texture(self.offscreen_pass);
// create egui TextureId from Miniquad GL texture Id
let raw_id = match unsafe { self.mq_ctx.texture_raw_id(mq_texture) } {
mq::RawId::OpenGl(id) => id as u64,
};
let egui_texture_id = egui::TextureId::User(raw_id);
self.mq_ctx
.begin_default_pass(mq::PassAction::clear_color(0.0, 0.0, 0.0, 1.0));
self.mq_ctx.end_render_pass();
// Run the UI code:
self.egui_mq.run(&mut *self.mq_ctx, |_mq_ctx, egui_ctx| {
egui::Window::new("egui ❤ miniquad").show(egui_ctx, |ui| {
let img =
egui::Image::from_texture(SizedTexture::new(egui_texture_id, [140.0, 140.0]));
ui.add(img);
#[cfg(not(target_arch = "wasm32"))]
{
if ui.button("Quit").clicked() {
std::process::exit(0);
}
}
});
});
// Draw things behind egui here
self.egui_mq.draw(&mut *self.mq_ctx);
// Draw things in front of egui here
self.mq_ctx.commit_frame();
}
fn mouse_motion_event(&mut self, x: f32, y: f32) {
self.egui_mq.mouse_motion_event(x, y);
}
fn mouse_wheel_event(&mut self, dx: f32, dy: f32) {
self.egui_mq.mouse_wheel_event(dx, dy);
}
fn mouse_button_down_event(&mut self, mb: mq::MouseButton, x: f32, y: f32) {
self.egui_mq.mouse_button_down_event(mb, x, y);
}
fn mouse_button_up_event(&mut self, mb: mq::MouseButton, x: f32, y: f32) {
self.egui_mq.mouse_button_up_event(mb, x, y);
}
fn char_event(&mut self, character: char, _keymods: mq::KeyMods, _repeat: bool) {
self.egui_mq.char_event(character);
}
fn key_down_event(&mut self, keycode: mq::KeyCode, keymods: mq::KeyMods, _repeat: bool) {
self.egui_mq.key_down_event(keycode, keymods);
}
fn key_up_event(&mut self, keycode: mq::KeyCode, keymods: mq::KeyMods) {
self.egui_mq.key_up_event(keycode, keymods);
}
}
fn main() {
let conf = mq::conf::Conf {
high_dpi: true,
..Default::default()
};
mq::start(conf, || Box::new(Stage::new()));
}
mod offscreen_shader {
use miniquad as mq;
pub const VERTEX: &str = r#"#version 100
attribute vec4 pos;
attribute vec4 color0;
varying lowp vec4 color;
uniform mat4 mvp;
void main() {
gl_Position = mvp * pos;
color = color0;
}
"#;
pub const FRAGMENT: &str = r#"#version 100
varying lowp vec4 color;
void main() {
gl_FragColor = color;
}
"#;
pub fn meta() -> mq::ShaderMeta {
mq::ShaderMeta {
images: vec![],
uniforms: mq::UniformBlockLayout {
uniforms: vec![mq::UniformDesc::new("mvp", mq::UniformType::Mat4)],
},
}
}
#[repr(C)]
pub struct Uniforms {
pub mvp: glam::Mat4,
}
}