aboutsummaryrefslogtreecommitdiff
path: root/src/Engine/Engine.h
blob: 2368bb80dbc9dc69fb0d363ccc2a6792ed03c51c (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/VertexArray.h"
#include "Rendering/UniformBuffer.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_PosRadius;
        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 GenerateGrid(const std::vector<ObjectData>& objects);
        void DrawGrid(const glm::mat4& viewProj);
        void DrawFullScreenQuad();
        void DispatchCompute(const Camera& cam);
        void UploadCameraUBO(const Camera& cam);
        void UploadObjectsUBO(const std::vector<ObjectData>& objs);
        void UploadDiskUBO();
        void RenderScene();
        void UpdatePhysics(float deltaTime);
        void UpdateWindowDimensions();

              Camera& GetCamera()       { return m_Camera; }
        const Camera& GetCamera() const { return m_Camera; }
        
        const std::vector<ObjectData>& GetObjects() const { return m_Objects; }
              std::vector<ObjectData>& GetObjects()       { return m_Objects; }
        
        bool GetGravity() const       { return m_Gravity;    }
        void SetGravity(bool gravity) { m_Gravity = gravity; }
        
        int GetWidth()         const { return m_Width;         }
        int GetHeight()        const { return m_Height;        }
        int GetComputeWidth()  const { return m_ComputeWidth;  }
        int GetComputeHeight() const { return m_ComputeHeight; }

    private:
        std::string CreateBasicVertexShader();
        std::string CreateBasicFragmentShader();
        std::string LoadComputeShader(const std::string& path);
    private:
        Ref<Shader>        m_GridShader;
        Ref<Shader>        m_QuadShader;
        Ref<Shader>        m_ComputeShader;
        Ref<Texture2D>     m_Texture;
        Ref<VertexArray>   m_QuadVAO;
        Ref<VertexArray>   m_GridVAO;
        Ref<UniformBuffer> m_CameraUBO;
        Ref<UniformBuffer> m_DiskUBO;
        Ref<UniformBuffer> m_ObjectsUBO;
        
        int   m_GridIndexCount = 0;
        int   m_Width;
        int   m_Height;
        int   m_ComputeWidth;
        int   m_ComputeHeight;
        float m_WidthScale = 100000000000.0f;
        float m_HeightScale = 75000000000.0f;

        std::vector<ObjectData> m_Objects;
        BlackHole               m_SagA;
        Camera                  m_Camera;
        bool                    m_Gravity = false;
    };
}