Working imgui

This commit is contained in:
Piotr 2020-09-22 22:03:00 +02:00
parent de84cca5b9
commit 8e66183b63
5 changed files with 196 additions and 50 deletions

3
.gitignore vendored
View File

@ -1,4 +1,5 @@
/target
**png
**jpg
**jpeg
**jpeg
imgui.ini

125
Cargo.lock generated
View File

@ -197,9 +197,9 @@ version = "0.1.0"
dependencies = [
"cgmath",
"gl",
"glfw",
"human-panic",
"image",
"imgui-glfw-rs",
"num",
"rand 0.7.3",
"tobj",
@ -250,7 +250,18 @@ version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a94edab108827d67608095e269cf862e60d920f144a5026d3dbcfd8b877fb404"
dependencies = [
"gl_generator",
"gl_generator 0.14.0",
]
[[package]]
name = "gl_generator"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a795170cbd85b5a7baa58d6d7525cae6a03e486859860c220f7ebbbdd379d0a"
dependencies = [
"khronos_api 2.2.0",
"log",
"xml-rs 0.7.0",
]
[[package]]
@ -259,20 +270,19 @@ version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a95dfc23a2b4a9a2f5ab41d194f8bfda3cabec42af4e39f08c339eb2a0c124d"
dependencies = [
"khronos_api",
"khronos_api 3.1.0",
"log",
"xml-rs",
"xml-rs 0.8.3",
]
[[package]]
name = "glfw"
version = "0.39.1"
version = "0.40.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d26b0c94d75430f0e8f0359c3f669c89b79df6c291f999596fb07e536b41c050"
checksum = "985f851b0d032911e199f308689d25c71f79d12c44625c3a31b683ad4e4f5dd9"
dependencies = [
"bitflags",
"glfw-sys",
"libc",
"log",
"objc",
"raw-window-handle",
@ -330,6 +340,47 @@ dependencies = [
"tiff",
]
[[package]]
name = "imgui"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fdb2bcc7e498e78137ce28705ae836d69e36ee2eac89d0d926cfabfcf550570"
dependencies = [
"bitflags",
"imgui-sys",
"lazy_static",
"parking_lot",
]
[[package]]
name = "imgui-glfw-rs"
version = "0.4.2"
source = "git+https://github.com/Leinnan/imgui-glfw-rs#9537c6d82f1594b22603a4e729e3943a326c54ac"
dependencies = [
"glfw",
"imgui",
"imgui-opengl-renderer",
]
[[package]]
name = "imgui-opengl-renderer"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "610bc2facbf1fe9bcf24bf6d299fada80ad1fc86283bac986b5cfc258e971e56"
dependencies = [
"gl_generator 0.9.0",
"imgui",
]
[[package]]
name = "imgui-sys"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72be9671d64dd0ed26bb708cd10060a431262ac90ae70cf7c5912feefe6849da"
dependencies = [
"cc",
]
[[package]]
name = "inflate"
version = "0.4.5"
@ -349,6 +400,12 @@ dependencies = [
"rayon",
]
[[package]]
name = "khronos_api"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "037ab472c33f67b5fbd3e9163a2645319e5356fcd355efa6d4eb7fff4bbcb554"
[[package]]
name = "khronos_api"
version = "3.1.0"
@ -367,6 +424,15 @@ version = "0.2.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2f02823cf78b754822df5f7f268fb59822e7296276d3e069d8e8cb26a14bd10"
[[package]]
name = "lock_api"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75"
dependencies = [
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.11"
@ -553,6 +619,30 @@ dependencies = [
"regex",
]
[[package]]
name = "parking_lot"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3a704eb390aafdc107b0e392f56a82b668e3a71366993b5340f5833fd62505e"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3"
dependencies = [
"cfg-if",
"cloudabi",
"libc",
"redox_syscall",
"smallvec",
"winapi",
]
[[package]]
name = "png"
version = "0.15.3"
@ -797,6 +887,12 @@ dependencies = [
"rand_core 0.3.1",
]
[[package]]
name = "redox_syscall"
version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
name = "regex"
version = "1.3.9"
@ -865,6 +961,12 @@ dependencies = [
"syn 1.0.38",
]
[[package]]
name = "smallvec"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252"
[[package]]
name = "syn"
version = "0.15.44"
@ -990,6 +1092,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "xml-rs"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c1cb601d29fe2c2ac60a2b2e5e293994d87a1f6fa9687a31a15270f909be9c2"
dependencies = [
"bitflags",
]
[[package]]
name = "xml-rs"
version = "0.8.3"

View File

@ -14,8 +14,8 @@ lto = true
[dependencies]
cgmath = "0.17.0"
gl = "0.14.0"
glfw = "0.39.1"
image = "0.22.5"
imgui-glfw-rs = { git = "https://github.com/Leinnan/imgui-glfw-rs" }
# only needed from chapter 3 on
tobj = "2.0.2"
num = "0.3.0"

View File

@ -3,8 +3,6 @@ use std::path::Path;
use std::sync::mpsc::Receiver;
use gl;
extern crate glfw;
use self::glfw::{Action, Key};
use image;
use image::DynamicImage::*;

View File

@ -1,10 +1,15 @@
extern crate gl;
extern crate glfw;
extern crate imgui_glfw_rs;
extern crate image;
// Use the reexported glfw crate to avoid version conflicts.
use imgui_glfw_rs::glfw;
// Use the reexported imgui crate to avoid version conflicts.
use imgui_glfw_rs::imgui;
use imgui_glfw_rs::ImguiGLFW;
use self::camera::*;
use self::gl::types::*;
use self::glfw::{Action, Context, Key};
use imgui_glfw_rs::glfw::{Action, Context, Key};
use cgmath::prelude::*;
use cgmath::{perspective, vec3, Deg, Matrix4, Point3, Rad, Vector3};
use human_panic::setup_panic;
@ -71,6 +76,7 @@ pub fn main() {
.expect("Failed to create GLFW window");
window.make_current();
window.set_all_polling(true);
window.set_framebuffer_size_polling(true);
window.set_cursor_pos_polling(true);
window.set_scroll_polling(true);
@ -105,10 +111,13 @@ pub fn main() {
};
let mut imgui = imgui::Context::create();
let mut imgui_glfw = ImguiGLFW::new(&mut imgui, &mut window);
// render loop
// -----------
let (r, g, b) = (0.188, 0.22, 0.235);
let (mut r,mut g,mut b) = (0.188, 0.22, 0.235);
while !window.should_close() {
// per-frame time logic
// --------------------
@ -116,16 +125,6 @@ pub fn main() {
delta_time = cur_frame - last_frame;
last_frame = cur_frame;
// events
// -----
process_events(
&events,
&mut first_mouse,
&mut last_x,
&mut last_y,
&mut camera,
);
// input
// -----
process_input(&mut window, delta_time, &mut camera);
@ -133,7 +132,7 @@ pub fn main() {
// render
// ------
unsafe {
gl::ClearColor(0.1, 0.1, 0.1, 1.0);
gl::ClearColor(r, g, b, 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
// don't forget to enable shader before setting uniforms
@ -154,46 +153,83 @@ pub fn main() {
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
let ui = imgui_glfw.frame(&mut window, &mut imgui);
{
use imgui::*;
Window::new(im_str!("Hello world"))
.size([300.0, 110.0], Condition::FirstUseEver)
.build(&ui, || {
ui.text(im_str!("Hello world!"));
ui.text(im_str!("こんにちは世界!"));
ui.text(im_str!("This...is...imgui-rs!"));
ui.separator();
ui.text(format!(
"Mouse Position: ({:.1},{:.1})",
last_x, last_y
));
imgui::Slider::new(im_str!("r"), 0.0 ..= 1.0)
.build(&ui, &mut r);
imgui::Slider::new(im_str!("g"), 0.0 ..= 1.0)
.build(&ui, &mut g);
imgui::Slider::new(im_str!("b"), 0.0 ..= 1.0)
.build(&ui, &mut b);
});
}
imgui_glfw.draw(ui, &mut window);
window.swap_buffers();
glfw.poll_events();
// events
// -----
for (_, event) in glfw::flush_messages(&events) {
imgui_glfw.handle_event(&mut imgui, &event);
process_events(
event,
&mut first_mouse,
&mut last_x,
&mut last_y,
&mut camera,
);
}
}
}
pub fn process_events(
events: &Receiver<(f64, glfw::WindowEvent)>,
event: imgui_glfw_rs::glfw::WindowEvent,
first_mouse: &mut bool,
last_x: &mut f32,
last_y: &mut f32,
camera: &mut Camera,
) {
for (_, event) in glfw::flush_messages(events) {
match event {
glfw::WindowEvent::FramebufferSize(width, height) => {
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
unsafe { gl::Viewport(0, 0, width, height) }
}
glfw::WindowEvent::CursorPos(xpos, ypos) => {
let (xpos, ypos) = (xpos as f32, ypos as f32);
if *first_mouse {
*last_x = xpos;
*last_y = ypos;
*first_mouse = false;
}
let xoffset = xpos - *last_x;
let yoffset = *last_y - ypos; // reversed since y-coordinates go from bottom to top
match event {
glfw::WindowEvent::FramebufferSize(width, height) => {
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
unsafe { gl::Viewport(0, 0, width, height) }
}
glfw::WindowEvent::CursorPos(xpos, ypos) => {
let (xpos, ypos) = (xpos as f32, ypos as f32);
if *first_mouse {
*last_x = xpos;
*last_y = ypos;
*first_mouse = false;
}
camera.process_mouse_movement(xoffset, yoffset, true);
}
glfw::WindowEvent::Scroll(_xoffset, yoffset) => {
camera.process_mouse_scroll(yoffset as f32);
}
_ => {}
let xoffset = xpos - *last_x;
let yoffset = *last_y - ypos; // reversed since y-coordinates go from bottom to top
*last_x = xpos;
*last_y = ypos;
camera.process_mouse_movement(xoffset, yoffset, true);
}
glfw::WindowEvent::Scroll(_xoffset, yoffset) => {
camera.process_mouse_scroll(yoffset as f32);
}
_ => {}
}
}