Skip to content

fix typo and import PI#22

Merged
armansito merged 1 commit intoRayTracing:devfrom
c0deaddict:dev
Mar 16, 2025
Merged

fix typo and import PI#22
armansito merged 1 commit intoRayTracing:devfrom
c0deaddict:dev

Conversation

@c0deaddict
Copy link
Contributor

Hello,

Thanks for your amazing work on this book. I've just completed the last available chapter of the book, and it was a very fun experience.

I encountered two small typos and one missing import, which are fixed in this PR.

Another thing that I encountered was that DeviceEvent::MouseWheel and DeviceEvent::Button would not trigger on my PC, instead i've used WindowEvent::MouseWheel and WindowEvent::MouseInput that did trigger. My guess it has something to do with the environment i'm running the program in (sway on NixOS unstable). For reference here is my event loop run:

    event_loop.run(|event, control_handle| {
        control_handle.set_control_flow(ControlFlow::Poll);
        match event {
            Event::WindowEvent { event, .. } => match event {
                WindowEvent::CloseRequested => control_handle.exit(),
                WindowEvent::RedrawRequested => {
                    // Wait for the next available frame buffer.
                    let frame: wgpu::SurfaceTexture = surface
                        .get_current_texture()
                        .expect("failed to get current texture");

                    let render_target = frame
                        .texture
                        .create_view(&wgpu::TextureViewDescriptor::default());

                    renderer.render_frame(&camera, &render_target);

                    frame.present();
                    window.request_redraw();
                },
                WindowEvent::MouseWheel { delta, .. } =>  {
                    let delta = match delta {
                        MouseScrollDelta::PixelDelta(delta) => 0.001 * delta.y as f32,
                        MouseScrollDelta::LineDelta(_, y) => y * 0.05,
                    };
                    camera.zoom(delta);
                    renderer.reset_samples();
                },
                WindowEvent::MouseInput { button, state, .. } => {
                    let pressed = state == ElementState::Pressed;
                    match button {
                        winit::event::MouseButton::Left => left_mouse_button_pressed = pressed,
                        winit::event::MouseButton::Right => right_mouse_button_pressed = pressed,
                        _ => (),
                    }
                }
                _ => (),
            },
            Event::DeviceEvent { event, .. } =>  match event {
                DeviceEvent::MouseMotion { delta: (dx, dy) } => {
                    let dx = dx as f32 *  0.01;
                    let dy = dy as f32 * -0.01;
                    if left_mouse_button_pressed {
                        camera.orbit(dx, dy);
                        renderer.reset_samples();
                    }
                    if right_mouse_button_pressed {
                        camera.pan(dx, dy);
                        renderer.reset_samples();
                    }
                },
                _ => (),
            },
            _ => (),
        }
    })?;

Copy link
Collaborator

@armansito armansito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the kind words and corrections!

@armansito
Copy link
Collaborator

Another thing that I encountered was that DeviceEvent::MouseWheel and DeviceEvent::Button would not trigger on my PC, instead i've used WindowEvent::MouseWheel and WindowEvent::MouseInput that did trigger. My guess it has something to do with the environment i'm running the program in (sway on NixOS unstable). For reference here is my event loop run:

    event_loop.run(|event, control_handle| {
        control_handle.set_control_flow(ControlFlow::Poll);
        match event {
            Event::WindowEvent { event, .. } => match event {
                WindowEvent::CloseRequested => control_handle.exit(),
                WindowEvent::RedrawRequested => {
                    // Wait for the next available frame buffer.
                    let frame: wgpu::SurfaceTexture = surface
                        .get_current_texture()
                        .expect("failed to get current texture");

                    let render_target = frame
                        .texture
                        .create_view(&wgpu::TextureViewDescriptor::default());

                    renderer.render_frame(&camera, &render_target);

                    frame.present();
                    window.request_redraw();
                },
                WindowEvent::MouseWheel { delta, .. } =>  {
                    let delta = match delta {
                        MouseScrollDelta::PixelDelta(delta) => 0.001 * delta.y as f32,
                        MouseScrollDelta::LineDelta(_, y) => y * 0.05,
                    };
                    camera.zoom(delta);
                    renderer.reset_samples();
                },
                WindowEvent::MouseInput { button, state, .. } => {
                    let pressed = state == ElementState::Pressed;
                    match button {
                        winit::event::MouseButton::Left => left_mouse_button_pressed = pressed,
                        winit::event::MouseButton::Right => right_mouse_button_pressed = pressed,
                        _ => (),
                    }
                }
                _ => (),
            },
            Event::DeviceEvent { event, .. } =>  match event {
                DeviceEvent::MouseMotion { delta: (dx, dy) } => {
                    let dx = dx as f32 *  0.01;
                    let dy = dy as f32 * -0.01;
                    if left_mouse_button_pressed {
                        camera.orbit(dx, dy);
                        renderer.reset_samples();
                    }
                    if right_mouse_button_pressed {
                        camera.pan(dx, dy);
                        renderer.reset_samples();
                    }
                },
                _ => (),
            },
            _ => (),
        }
    })?;

Thank you for the report. I ran into a different issue related to mouse handling that also got resolved with the WindowEvent equivalent, so I think it makes sense to change the code (I filed #23 to track that). PRs are always welcome :)

@armansito armansito merged commit 88c9cce into RayTracing:dev Mar 16, 2025
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