-
-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
249 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ¯oquad::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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.