Skip to content
Snippets Groups Projects
Select Git revision
  • e012c54405c3dddee9e71b810b9f21251a7e321a
  • master default
  • 1.1
  • 1.0
4 results

eistype.cpp

Blame
  • camera.cpp 6.69 KiB
    // file      : liblava/app/camera.cpp
    // copyright : Copyright (c) 2018-present, Lava Block OÜ and contributors
    // license   : MIT; see accompanying LICENSE file
    
    #include <liblava/app/camera.hpp>
    
    namespace lava {
    
        bool camera::create(device_ptr device) {
            update_projection();
    
            data = make_buffer();
    
            return data->create_mapped(device, &projection, size, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT);
        }
    
        void camera::destroy() {
            if (!data)
                return;
    
            data->destroy();
            data = nullptr;
        }
    
        void camera::move_first_person(delta dt) {
            v3 front;
            front.x = -cos(glm::radians(rotation.x)) * sin(glm::radians(rotation.y));
            front.y = sin(glm::radians(rotation.x));
            front.z = cos(glm::radians(rotation.x)) * cos(glm::radians(rotation.y));
    
            front = glm::normalize(front);
    
            auto speed = dt * movement_speed * 2.f;
    
            if (up) {
                if (lock_z)
                    position -= glm::normalize(glm::cross(front, v3(1.f, 0.f, 0.f))) * speed;
                else
                    position -= (front * speed);
            }
    
            if (down) {
                if (lock_z)
                    position += glm::normalize(glm::cross(front, v3(1.f, 0.f, 0.f))) * speed;
                else
                    position += (front * speed);
            }
    
            if (left)
                position += glm::normalize(glm::cross(front, v3(0.f, 1.f, 0.f))) * speed;
    
            if (right)
                position -= glm::normalize(glm::cross(front, v3(0.f, 1.f, 0.f))) * speed;
        }
    
        void camera::update_view(delta dt, mouse_position mouse_pos) {
            if (translate || rotate) {
                auto dx = mouse_pos_x - mouse_pos.x;
                auto dy = mouse_pos_y - mouse_pos.y;
    
                if (rotate && !lock_rotation) {
                    auto speed = dt * rotation_speed;
                    rotation += v3(dy * speed, -dx * speed, 0.f);
                }
    
                if (translate) {
                    auto speed = dt * movement_speed;
                    position -= v3(-dx * speed, -dy * speed, 0.f);
                }