Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a nannou_core crate. Switch from cgmath to glam. Remove geom::Graph. Remove generic Scalar param from Draw API. #754

Merged
merged 10 commits into from
Jun 20, 2021
Merged
30 changes: 30 additions & 0 deletions .github/workflows/nannou.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,31 @@ jobs:
command: test
args: --doc --all-features --verbose

cargo-test-core-no-std:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Update apt
run: sudo apt update
- name: Install libxcb dev tools
run: sudo apt-get install libxcb-composite0-dev
- name: Install stable
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- name: Text no_std
uses: actions-rs/cargo@v1
with:
command: test
args: -p nannou_core --no-default-features --features "libm" --verbose
- name: Test no_std serde
uses: actions-rs/cargo@v1
with:
command: test
args: -p nannou_core --no-default-features --features "libm serde" --verbose

cargo-check-examples:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -113,6 +138,11 @@ jobs:
profile: minimal
toolchain: stable
override: true
- name: Cargo publish nannou_core
continue-on-error: true
run: cargo publish --token $CRATESIO_TOKEN --manifest-path nannou_core/Cargo.toml
- name: Wait for crates.io
run: sleep 30
- name: Cargo publish nannou
continue-on-error: true
run: cargo publish --token $CRATESIO_TOKEN --manifest-path nannou/Cargo.toml
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"guide/book_tests",
"nannou",
"nannou_audio",
"nannou_core",
"nannou_isf",
"nannou_laser",
"nannou_new",
Expand Down
4 changes: 2 additions & 2 deletions examples/audio/hrtf-noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn model(app: &App) -> Model {
// Initialise the state that we want to live on the audio thread.
let source_position = [0.0; 3].into();
let audio_model = Audio {
rng: SmallRng::from_seed([0; 16]),
rng: SmallRng::seed_from_u64(0),
hrtf_data: HrtfData::new(),
hrtf_processor,
source_position,
Expand Down Expand Up @@ -201,7 +201,7 @@ fn view(app: &App, model: &Model, frame: Frame) {

// Simple function for determining a gain based on the distance from the listener.
fn dist_gain(p: &Point3) -> f32 {
let m = p.magnitude();
let m = p.length();
if m == 0.0 {
1.0
} else if m > 1.0 {
Expand Down
4 changes: 2 additions & 2 deletions examples/draw/draw_arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ fn view(app: &App, frame: Frame) {
let side = r.w().min(r.h());
let start = r.xy();
let start_to_mouse = app.mouse.position() - start;
let target_mag = start_to_mouse.magnitude().min(side * 0.5);
let end = start + start_to_mouse.with_magnitude(target_mag);
let target_mag = start_to_mouse.length().min(side * 0.5);
let end = start + start_to_mouse.normalize() * target_mag;
draw.arrow().weight(5.0).points(start, end);
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/draw/draw_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn view(app: &App, frame: Frame) {
tri.map_vertices(|v| {
let y_fract = map_range(v.y.abs(), 0.0, win.top(), 0.0, 1.0);
let color = srgba(y_fract, 1.0 - y_fract, 1.0 - y_fract, 1.0);
(v, color)
(v.extend(0.0), color)
})
});

Expand Down
3 changes: 2 additions & 1 deletion examples/draw/draw_textured_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ fn view(app: &App, model: &Model, frame: Frame) {
.map(|point| {
// Tex coords should be in range (0.0, 0.0) to (1.0, 1.0);
// This will have the logo show on the front and back faces.
let tex_coords = [point.x + 0.5, 1.0 - (point.y + 0.5)];
let [x, y, _] = point;
let tex_coords = [x + 0.5, 1.0 - (y + 0.5)];
(point, tex_coords)
});

Expand Down
7 changes: 4 additions & 3 deletions examples/draw/draw_textured_polygon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,23 @@ fn view(app: &App, model: &Model, frame: Frame) {
let draw = app.draw();

// We'll make a wave from an ellipse with a wave added onto its circumference.
let resolution = win_rect.right() as usize;
let resolution = win_rect.right().floor();
let rect = geom::Rect::from_wh(vec2(1.0, 1.0));
let ellipse = geom::Ellipse::new(rect, resolution).circumference();

// The wave's frequency and amplitude are derived from the mouse position.
let freq = map_range(app.mouse.x, win_rect.left(), win_rect.right(), 1.0, 20.0);
let amp = map_range(app.mouse.y, win_rect.bottom(), win_rect.top(), 0.0, 0.5);
let wave = (0..resolution).map(|i| {
let phase = i as f32 / resolution as f32;
let wave = (0..resolution as usize).map(|i| {
let phase = i as f32 / resolution;
(phase * freq * PI * 2.0).sin() * amp
});

// Combine the ellipse with the wave.
let points = ellipse.zip(wave).map(|(point, wave)| {
// Base the tex coords on the non-wavey points.
// This will make the texture look wavey.
let point = Point2::from(point);
let tex_coords = [point.x + 0.5, 1.0 - (point.y + 0.5)];
// Apply the wave to the points.
let point = point + point * wave;
Expand Down
4 changes: 2 additions & 2 deletions examples/laser/laser_frame_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ fn laser(laser: &mut Laser, frame: &mut laser::Frame) {
TestPattern::Circle => {
let n_points = frame.points_per_frame() as usize / 4;
let rect = Rect::from_w_h(1.0, 1.0);
let ellipse: Vec<_> = geom::ellipse::Circumference::new(rect, n_points)
.map(|p| lit_p([p.x, p.y]))
let ellipse: Vec<_> = geom::ellipse::Circumference::new(rect, n_points as f32)
.map(|[x, y]| lit_p([x, y]))
.collect();
frame.add_lines(&ellipse);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/laser/laser_frame_stream_gui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,8 @@ fn laser(laser: &mut Laser, frame: &mut laser::Frame) {
TestPattern::Circle => {
let n_points = frame.points_per_frame() as usize / 4;
let rect = Rect::from_w_h(2.0, 2.0);
let ellipse: Vec<_> = geom::ellipse::Circumference::new(rect, n_points)
.map(|p| lit_p([p.x, p.y]))
let ellipse: Vec<_> = geom::ellipse::Circumference::new(rect, n_points as f32)
.map(|[x, y]| lit_p([x, y]))
.collect();
add_points(&ellipse, laser.draw_mode, laser.scale, frame);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/nannou_basics/all_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ fn touchpad_pressure(_app: &App, _model: &mut Model, _pressure: TouchpadPressure

fn window_moved(_app: &App, _model: &mut Model, _pos: Point2) {}

fn window_resized(_app: &App, _model: &mut Model, _dim: Vector2) {}
fn window_resized(_app: &App, _model: &mut Model, _dim: Vec2) {}

fn window_focused(_app: &App, _model: &mut Model) {}

Expand Down
2 changes: 1 addition & 1 deletion examples/offline/quadtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct QuadTree {
}

pub trait WithPos {
fn get_pos(&self) -> Vector2;
fn get_pos(&self) -> Vec2;
}

impl QuadTree {
Expand Down
10 changes: 5 additions & 5 deletions examples/offline/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct Model {

// a Thing will be our main object, it'll try to grow outward
struct Thing {
position: Vector2,
position: Vec2,
size: f32,
energy: f32,
frac: f32,
Expand All @@ -39,7 +39,7 @@ impl PartialEq for Thing {
}
// define what to use for the quadtree
impl quadtree::WithPos for Thing {
fn get_pos(&self) -> Vector2 {
fn get_pos(&self) -> Vec2 {
self.position
}
}
Expand Down Expand Up @@ -275,23 +275,23 @@ fn view(app: &App, model: &Model, frame: Frame) {

//draw in three steps
draw.ellipse()
.resolution(20)
.resolution(20.0)
.xy(model.things[k].position * scale)
.w_h(
model.things[k].size * 1.3 * scale,
model.things[k].size * 1.3 * scale,
)
.color(c);
draw.ellipse()
.resolution(20)
.resolution(20.0)
.xy(model.things[k].position * scale)
.w_h(
model.things[k].size * 1.2 * scale,
model.things[k].size * 1.2 * scale,
)
.color(c2);
draw.ellipse()
.resolution(20)
.resolution(20.0)
.xy(model.things[k].position * scale)
.w_h(
model.things[k].size * 0.5 * scale,
Expand Down
8 changes: 4 additions & 4 deletions examples/ui/simple_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {
struct Model {
ui: Ui,
ids: Ids,
resolution: usize,
resolution: f32,
scale: f32,
rotation: f32,
color: Rgb,
Expand Down Expand Up @@ -36,7 +36,7 @@ fn model(app: &App) -> Model {
let ids = Ids::new(ui.widget_id_generator());

// Init our variables
let resolution = 6;
let resolution = 6.0;
let scale = 200.0;
let rotation = 0.0;
let position = pt2(0.0, 0.0);
Expand Down Expand Up @@ -66,12 +66,12 @@ fn update(_app: &App, model: &mut Model, _update: Update) {
.border(0.0)
}

for value in slider(model.resolution as f32, 3.0, 15.0)
for value in slider(model.resolution, 3.0, 15.0)
.top_left_with_margin(20.0)
.label("Resolution")
.set(model.ids.resolution, ui)
{
model.resolution = value as usize;
model.resolution = value.round();
}

for value in slider(model.scale, 10.0, 500.0)
Expand Down
78 changes: 40 additions & 38 deletions examples/wgpu/wgpu_instancing/wgpu_instancing.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use nannou::math::cgmath::{self, Matrix3, Matrix4, Point3, Rad, Vector3};
use nannou::prelude::*;
use std::cell::RefCell;

Expand All @@ -9,26 +8,26 @@ mod data;
* regularly than it would be with for instance polar coordinates (which is the case for UV map).
* See https://en.wikipedia.org/wiki/Geodesic_polyhedron
*/
fn make_geodesic_isocahedron(subdivisions: usize) -> Vec<Vector3<f32>> {
fn make_geodesic_isocahedron(subdivisions: usize) -> Vec<Vec3> {
let sqrt5 = 5f32.sqrt();
let phi = (1f32 + sqrt5) * 0.5f32;
let ratio = (10f32 + (2f32 * sqrt5)).sqrt() / (4f32 * phi);
let a = (1f32 / ratio) * 0.5f32;
let b = (1f32 / ratio) / (2f32 * phi);

let mut points = vec![
Vector3::new(0f32, b, -a),
Vector3::new(b, a, 0f32),
Vector3::new(-b, a, 0f32),
Vector3::new(0f32, b, a),
Vector3::new(0f32, -b, a),
Vector3::new(-a, 0f32, b),
Vector3::new(0f32, -b, -a),
Vector3::new(a, 0f32, -b),
Vector3::new(a, 0f32, b),
Vector3::new(-a, 0f32, -b),
Vector3::new(b, -a, 0f32),
Vector3::new(-b, -a, 0f32),
vec3(0f32, b, -a),
vec3(b, a, 0f32),
vec3(-b, a, 0f32),
vec3(0f32, b, a),
vec3(0f32, -b, a),
vec3(-a, 0f32, b),
vec3(0f32, -b, -a),
vec3(a, 0f32, -b),
vec3(a, 0f32, b),
vec3(-a, 0f32, -b),
vec3(b, -a, 0f32),
vec3(-b, -a, 0f32),
];

let triangles = vec![
Expand Down Expand Up @@ -103,7 +102,7 @@ fn make_geodesic_isocahedron(subdivisions: usize) -> Vec<Vector3<f32>> {

struct Model {
graphics: RefCell<Graphics>,
sphere: Vec<Vector3<f32>>,
sphere: Vec<Vec3>,
}

struct Graphics {
Expand Down Expand Up @@ -133,15 +132,15 @@ pub struct Normal {
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Uniforms {
world: Matrix4<f32>,
view: Matrix4<f32>,
proj: Matrix4<f32>,
world: Mat4,
view: Mat4,
proj: Mat4,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct Instance {
transformation: Matrix4<f32>,
transformation: Mat4,
color: [f32; 3],
}

Expand Down Expand Up @@ -233,27 +232,30 @@ fn model(app: &App) -> Model {
}

fn make_instance(
orientation: Vector3<f32>,
orientation: Vec3,
offset: f32,
local_rotation: f32,
scale: f32,
color: [f32; 3],
) -> Instance {
let scale_m = Matrix4::from_scale(scale);
let local_rotation_m = Matrix4::from(Matrix3::from_angle_y(Rad(local_rotation)));
let scale_m = Mat4::from_scale(Vec3::splat(scale));
let local_rotation_m = Mat4::from_rotation_y(local_rotation);
let orientation_m = {
let up = Vector3::new(0f32, 1f32, 0f32);
let up = Vec3::Y;
let cosine = orientation.dot(up);
if cosine > 0.999 {
Matrix4::identity()
Mat4::IDENTITY
} else if cosine < -0.999 {
Matrix4::from_axis_angle(Vector3::new(1f32, 0f32, 0f32), Rad(std::f32::consts::PI))
Mat4::from_axis_angle(Vec3::X, std::f32::consts::PI)
} else {
Matrix4::from_axis_angle(up.cross(orientation).normalize(), up.angle(orientation))
Mat4::from_axis_angle(
up.cross(orientation).normalize(),
up.angle_between(orientation),
)
}
};

let translation_m = Matrix4::from_translation(offset * orientation);
let translation_m = Mat4::from_translation(offset * orientation);

Instance {
transformation: translation_m * orientation_m * local_rotation_m * scale_m,
Expand Down Expand Up @@ -356,19 +358,19 @@ fn view(app: &App, model: &Model, frame: Frame) {
}

fn create_uniforms(world_rotation: f32, [w, h]: [u32; 2]) -> Uniforms {
let world_rotation = Matrix3::from_angle_y(Rad(world_rotation as f32));
let world_rotation = Mat4::from_rotation_y(world_rotation);
let aspect_ratio = w as f32 / h as f32;
let proj = cgmath::perspective(Rad(std::f32::consts::FRAC_PI_2), aspect_ratio, 0.01, 100.0);
let view = Matrix4::look_at(
Point3::new(0.3, 0.3, 1.0),
Point3::new(0.0, 0.0, 0.0),
Vector3::new(0.0, 1.0, 0.0),
);

let world_scale = Matrix4::from_scale(0.015);

let fov_y = std::f32::consts::FRAC_PI_2;
let near = 0.01;
let far = 100.0;
let proj = Mat4::perspective_rh_gl(fov_y, aspect_ratio, near, far);
let eye = pt3(0.3, 0.3, 1.0);
let target = Point3::ZERO;
let up = Vec3::Y;
let view = Mat4::look_at_rh(eye, target, up);
let world_scale = Mat4::from_scale(Vec3::splat(0.015));
Uniforms {
world: Matrix4::from(world_rotation).into(),
world: world_rotation,
view: (view * world_scale).into(),
proj: proj.into(),
}
Expand Down
Loading