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 window camera getter #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

antonylsg
Copy link

@antonylsg antonylsg commented Mar 19, 2020

Hello,

I suggest to add a handle to the window camera.

I came across a situation where I needed to apply a global rotation to all objects of the scene. I found it useful to apply the inverse rotation to the camera instead.

Here is a minimal example how I use it:

use kiss3d::camera::Camera;
use kiss3d::light::Light;
use kiss3d::window::Window;
use nalgebra::{UnitQuaternion, Vector3};

fn main() {
    let mut window = Window::new("Kiss3d: cube");
    let mut cube = window.add_cube(1.0, 1.0, 1.0);

    cube.set_color(1.0, 0.0, 0.0);

    window.set_light(Light::StickToCamera);

    let rotation = UnitQuaternion::from_axis_angle(&Vector3::y_axis(), -0.014);

    while window.render() {
        let mut camera = window.camera().borrow_mut();

        let at = rotation * camera.at();
        let eye = rotation * camera.eye();

        camera.look_at(eye, at);
    }
}

Edit:

We could alternativly define:

use std::cell::Ref;
use std::cell::RefMut;

pub fn camera(&self) -> Ref<'_, ArcBall> {
    self.camera.borrow()
}

pub fn camera_mut(&self) -> RefMut<'_, ArcBall> {
    self.camera.borrow_mut()
}

@alvinhochun
Copy link
Collaborator

I believe the canonical way to do it is to construct your own camera or use one of kiss3d::camera::* and pass it to Window::render_with_camera instead of calling Window::render. Alternatively, you can create a type and have it implement kiss3d::window::State which allows you to return your own camera in State::cameras_and_effect_and_renderer, then use Window::render_loop to start the rendering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants