Skip to content

Commit

Permalink
Gizmos API
Browse files Browse the repository at this point in the history
  • Loading branch information
not-fl3 committed Jul 9, 2024
1 parent 1e3f31b commit 0b0cffa
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 120 deletions.
12 changes: 2 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ all-features = true
miniquad = { version = "0.4", features = ["log-impl"] }
quad-rand = "0.2.1"
glam = {version = "0.24", features = ["scalar-math","mint"] }
fontdue = { version = "0.8", default-features=false, features = ["std"] }
fontdue = { git = "https://github.com/not-fl3/fontdue.git", default-features=false, features = ["std"] }
backtrace = { version = "0.3.60", optional = true, default-features = false, features = [ "std", "libbacktrace" ] }
log = { version = "0.4", optional = true }
quad-snd = { version = "0.2", optional = true }
Expand All @@ -43,13 +43,6 @@ quad-gl = { git = "https://github.com/not-fl3/quad-gl", branch = "v0.4" }

[dev-dependencies]
dolly = "*"
#macroquad-particles = { path = "./particles" }
#macroquad-tiled = { path = "./tiled" }
#macroquad-profiler = { path = "./profiler" }
#macroquad-platformer = { path = "./physics-platformer" }

# workaround to fix the examples
# this allows to use macroquad-particles in examples without two versions of

#[patch."https://github.com/not-fl3/quad-gl"]
#quad-gl = { path = '../quad-gl' }
Expand All @@ -62,8 +55,7 @@ macroquad = { path = './' }

#quad-snd = {path = '../quad-snd'}
#miniquad = { path = '../miniquad' }
fontdue = { git = 'https://github.com/not-fl3/fontdue.git' }
miniquad = { git = 'https://github.com/not-fl3/miniquad.git' }
#miniquad = { git = 'https://github.com/not-fl3/miniquad.git' }
#fontdue = { path = '../fontdue' }
#quad-gl = {path = '../quad-gl'}
#zune-jpeg = { path = "../zune-image/zune-jpeg" }
Expand Down
2 changes: 1 addition & 1 deletion examples/basic_shapes_compat.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use macroquad::compat::*;

async fn game(ctx: macroquad::Context) {
init_compat_mode(ctx);
init_compat_mode(&ctx);

loop {
clear_background(LIGHTGRAY);
Expand Down
68 changes: 68 additions & 0 deletions examples/gizmos/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use macroquad::{
file::load_file,
gizmos::{draw_gizmos, gizmos_add_line, init_gizmos},
math::{vec2, vec3},
quad_gl::{camera::Environment, color},
time::get_time,
window::next_frame,
};

mod orbit_camera;

async fn game(ctx: macroquad::Context) {
unsafe {
macroquad::miniquad::gl::glEnable(macroquad::miniquad::gl::GL_TEXTURE_CUBE_MAP_SEAMLESS)
};

init_gizmos(&ctx);

let mut scene = ctx.new_scene();

let heightmap = load_file("examples/ferris.png").await.unwrap();
let heightmap = quad_gl::image::decode(&heightmap).unwrap();

let indices = vec![0u16, 1, 2, 0, 2, 3];

let vertices = vec![
vec3(-0.5, 0., -0.5),
vec3(-0.5, 0., 0.5),
vec3(0.5, 0., 0.5),
vec3(0.5, 0., -0.5),
];

for v in &vertices {
gizmos_add_line(true, *v, *v + vec3(0.0, 0.1, 0.0));
}

let uvs = vec![vec2(0., 1.), vec2(0., 0.), vec2(1., 0.), vec2(1., 1.)];
let normals = vec![vec3(0., 1., 0.); 4];
let mesh = quad_gl::models::CpuMesh(vertices, uvs, normals, indices);
let mesh = ctx.mesh(
mesh,
Some(ctx.new_texture_from_rgba8(
heightmap.width as _,
heightmap.height as _,
&heightmap.data,
)),
);
let _mesh = scene.add_model(&mesh);
let mut orbit = orbit_camera::OrbitCamera::new();

loop {
let t = get_time();
let mut p = vec3(t.sin() as f32, 0.0, t.cos() as f32);

gizmos_add_line(true, p, p * 1.2);
gizmos_add_line(false, vec3(0.0, 0.0, 0.0), p);

ctx.clear_screen(color::WHITE);
orbit.orbit(&ctx);
scene.draw(&orbit.camera);
draw_gizmos(&orbit.camera);
next_frame().await
}
}

fn main() {
macroquad::start(Default::default(), |ctx| game(ctx));
}
65 changes: 65 additions & 0 deletions examples/gizmos/orbit_camera.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use dolly::prelude::*;
use macroquad::{
input::MouseButton,
math::{vec3, Vec3},
quad_gl::{
camera::{Camera, Environment, Projection},
color,
},
};

pub struct OrbitCamera {
dolly_rig: CameraRig,
pub camera: Camera,
zoom: f32,
}

impl OrbitCamera {
pub fn new() -> OrbitCamera {
let dolly_rig: CameraRig = CameraRig::builder()
.with(YawPitch::new().yaw_degrees(45.0).pitch_degrees(-10.0))
.with(Smooth::new_rotation(0.7))
.with(Arm::new(Vec3::Z * 4.0))
.build();

let camera = Camera {
environment: Environment::SolidColor(color::WHITE),
depth_enabled: true,
projection: Projection::Perspective,
position: vec3(0., 1.5, 4.),
up: vec3(0., 1., 0.),
target: vec3(0., 0., 0.),
z_near: 0.1,
z_far: 1500.0,
..Default::default()
};

OrbitCamera {
dolly_rig,
camera,
zoom: 4.0,
}
}

pub fn orbit(&mut self, ctx: &macroquad::Context) {
if !ctx.root_ui().is_mouse_over(ctx.mouse_position())
&& ctx.is_mouse_button_down(MouseButton::Left)
{
self.dolly_rig
.driver_mut::<YawPitch>()
.rotate_yaw_pitch(ctx.mouse_delta().x * 100., ctx.mouse_delta().y * 100.);
}
if ctx.mouse_wheel().1 != 0.0 {
self.zoom -= ctx.mouse_wheel().1 * 0.4;
self.zoom = self.zoom.clamp(1.8, 10.0);
self.dolly_rig.driver_mut::<Arm>().offset = (Vec3::Z * self.zoom).into();
}
let delta = 0.1;
let dolly_transform = self.dolly_rig.update(delta);
self.camera.position = dolly_transform.position.into();
self.camera.up = dolly_transform.up();
let p: Vec3 = dolly_transform.position.into();
let f: Vec3 = dolly_transform.forward::<Vec3>().into();
self.camera.target = p + f;
}
}
15 changes: 9 additions & 6 deletions src/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
pub use crate::window::next_frame;
pub use quad_gl::color::*;

use std::cell::RefCell;
use std::{
cell::RefCell,
sync::{Arc, Mutex},
};

pub struct CompatContext {
ctx: crate::Context,
quad_ctx: Arc<Mutex<Box<miniquad::Context>>>,
canvas: quad_gl::sprite_batcher::SpriteBatcher,
}

Expand All @@ -17,11 +20,12 @@ thread_local! {
fn with_ctx<F: Fn(&mut CompatContext)>(f: F) {
CTX.with_borrow_mut(|v| f(v.as_mut().unwrap()));
}
pub fn init_compat_mode(ctx: crate::Context) {
pub fn init_compat_mode(ctx: &crate::Context) {
let canvas = ctx.new_canvas();
let quad_ctx = ctx.quad_ctx.clone();

CTX.with_borrow_mut(|v| {
*v = Some(CompatContext { ctx, canvas });
*v = Some(CompatContext { quad_ctx, canvas });
});
}

Expand All @@ -35,8 +39,7 @@ pub(crate) fn end_frame() {
}
pub fn clear_background(color: Color) {
with_ctx(|ctx| {
ctx.ctx
.quad_ctx
ctx.quad_ctx
.lock()
.unwrap()
.clear(Some((1., 1., 1., 1.)), None, None)
Expand Down
79 changes: 79 additions & 0 deletions src/gizmos.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
pub use crate::{math::Vec3, window::next_frame};
pub use quad_gl::{
color::*,
draw_calls_batcher::{DrawCallsBatcher, DrawMode},
};

use std::{
cell::RefCell,
sync::{Arc, Mutex},
};

struct Line {
persist: bool,
p0: Vec3,
p1: Vec3,
}

pub struct Gizmos {
quad_ctx: Arc<Mutex<Box<miniquad::Context>>>,
canvas: quad_gl::sprite_batcher::SpriteBatcher,
lines: Vec<Line>,
}

thread_local! {
pub static CTX: RefCell<Option<Gizmos>> = { RefCell::new(None) };
}

fn with_ctx<F: Fn(&mut Gizmos)>(f: F) {
CTX.with_borrow_mut(|v| f(v.as_mut().unwrap()));
}
pub fn init_gizmos(ctx: &crate::Context) {
let canvas = ctx.new_canvas();
let quad_ctx = ctx.quad_ctx.clone();

CTX.with_borrow_mut(|v| {
*v = Some(Gizmos {
quad_ctx,
canvas,
lines: vec![],
});
});
}

fn draw_line(gl: &mut DrawCallsBatcher, p0: Vec3, p1: Vec3) {
let uv = [0., 0.];
let color: [f32; 4] = [0.0, 0.0, 1.0, 1.0];
let indices = [0, 1];

let line = [
([p0.x, p0.y, p0.z], uv, color),
([p1.x, p1.y, p1.z], uv, color),
];
gl.texture(None);
gl.draw_mode(DrawMode::Lines);
gl.geometry(&line[..], &indices);
}

pub fn draw_gizmos(camera: &quad_gl::camera::Camera) {
if CTX.with_borrow(|ctx| ctx.is_some()) {
with_ctx(|ctx| {
let mut gl = ctx.canvas.gl();
gl.depth_test(true);
for line in &mut ctx.lines {
draw_line(gl, line.p0, line.p1);
}

ctx.canvas.draw2(camera);
ctx.canvas.reset();

ctx.lines.retain(|line| line.persist);
});
}
}

pub fn gizmos_add_line(persist: bool, p0: Vec3, p1: Vec3) {
with_ctx(|ctx| {
ctx.lines.push(Line { persist, p0, p1 });
});
}
25 changes: 14 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,14 @@ pub use ::log as logging;
pub use miniquad;

pub mod compat;
pub mod gizmos;
pub mod resources;

pub use quad_gl;
pub use quad_gl::math;

use quad_gl::{models::CpuMesh, scene::Model, texture::Image, texture::Texture2D};

use glam::{vec2, Mat4, Vec2};
use std::sync::{Arc, Mutex, Weak};

Expand Down Expand Up @@ -400,23 +403,23 @@ impl Context {
quad_gl.new_scene()
}

pub fn new_texture_from_image(
&self,
image: &quad_gl::texture::Image,
) -> quad_gl::texture::Texture2D {
pub fn new_texture_from_image(&self, image: &Image) -> Texture2D {
self.quad_gl.lock().unwrap().from_image(&image)
}

pub fn new_texture_from_rgba8(&self, width: u16, height: u16, data: &[u8]) -> Texture2D {
self.quad_gl
.lock()
.unwrap()
.from_rgba8(width, height, &data)
}
pub fn mesh(&self, mesh: CpuMesh, texture: Option<Texture2D>) -> Model {
self.quad_gl.lock().unwrap().mesh(mesh, texture)
}

pub fn root_ui<'a>(&'a self) -> impl std::ops::DerefMut<Target = quad_gl::ui::Ui> + 'a {
self.ui.lock().unwrap()
}
// pub fn new_scene(&self) -> scene::Scene {
// scene::Scene::new(self.quad_ctx.clone(), self.fonts_storage.clone())
// }

// pub fn new_canvas(&self) -> sprite_layer::SpriteLayer {
// //sprite_layer::SpriteLayer::new(self.quad_ctx.clone(), self.fonts_storage.clone())
// }
}

pub fn start<F: Fn(Context) -> Fut + 'static, Fut: Future<Output = ()> + 'static>(
Expand Down
Loading

0 comments on commit 0b0cffa

Please sign in to comment.