hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/src/engine/engine.h
blob: 6f90efb9219f0f5ac1e252e16b383aa8ad6a3ee7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#pragma once

#include <vector>
#include <iostream>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <fstream>
#include <sstream>
#include <chrono>
#include <cmath>

#include "core/application.h"
#include "core/camera.h"
#include "rendering/renderer.h"
#include "rendering/shader.h"
#include "rendering/texture.h"
#include "rendering/vertex_array.h"
#include "rendering/uniform_buffer.h"

namespace Donut
{
    const double c = 299792458.0;
    const double G = 6.67430e-11;

    struct BlackHole 
    {
        glm::vec3 m_position;
        double m_mass;
        double m_radius;
        double m_rs;

        BlackHole(glm::vec3 pos, float mass) 
            : m_position(pos),
              m_mass(mass) 
        {
            m_rs = 2.0 * G * m_mass / (c * c);
        }

        bool intercept(float px, float py, float pz) const 
        {
            double dx = double(px) - double(m_position.x);
            double dy = double(py) - double(m_position.y);
            double dz = double(pz) - double(m_position.z);
            double dist2 = dx * dx + dy * dy + dz * dz;
            return dist2 < m_rs * m_rs;
        }
    };

    struct ObjectData 
    {
        glm::vec4 m_pos_radius;
        glm::vec4 m_color;
        float m_mass;
        glm::vec3 m_velocity = glm::vec3(0.0f, 0.0f, 0.0f);
    };

    class Engine
    {
    public:
        Engine();
        ~Engine() = default;

        void generate_grid(const std::vector<ObjectData>& objects);
        void draw_grid(const glm::mat4& view_proj);
        void draw_full_screen_quad();
        void dispatch_compute(const Camera& cam);
        void upload_camera_ubo(const Camera& cam);
        void upload_objects_ubo(const std::vector<ObjectData>& objs);
        void upload_disk_ubo();
        void render_scene();
        void update_physics(float delta_time);
        void update_window_dimensions();

              Camera& get_camera()       { return m_camera; }
        const Camera& get_camera() const { return m_camera; }
        
        const std::vector<ObjectData>& get_objects() const { return m_objects; }
              std::vector<ObjectData>& get_objects()       { return m_objects; }
        
        bool get_gravity() const       { return m_gravity;    }
        void set_gravity(bool gravity) { m_gravity = gravity; }
        
        int get_width()         const { return m_width;         }
        int get_height()        const { return m_height;        }
        int get_compute_width()  const { return m_compute_width;  }
        int get_compute_height() const { return m_compute_height; }

    private:
        std::string create_basic_vertex_shader();
        std::string create_basic_fragment_shader();
        std::string load_compute_shader(const std::string& path);
    private:
        Ref<Shader>        m_grid_shader;
        Ref<Shader>        m_quad_shader;
        Ref<Shader>        m_compute_shader;
        Ref<Texture2D>     m_texture;
        Ref<VertexArray>   m_quad_vao;
        Ref<VertexArray>   m_grid_vao;
        Ref<UniformBuffer> m_camera_ubo;
        Ref<UniformBuffer> m_disk_ubo;
        Ref<UniformBuffer> m_objects_ubo;
        
        int   m_grid_index_count = 0;
        int   m_width;
        int   m_height;
        int   m_compute_width;
        int   m_compute_height;
        float m_width_scale = 100000000000.0f;
        float m_height_scale = 75000000000.0f;

        std::vector<ObjectData> m_objects;
        BlackHole               m_sag_a;
        Camera                  m_camera;
        bool                    m_gravity = false;
    };
}