Simulating Gravity And Black Holes In C++ (Amazing)

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include <vector>
#include <cmath>

const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);

// Orbital params
struct Planet {
    float distance;  // from sun
    float size;
    float speed;     // angular speed
    glm::vec3 color;
};

// Planets
std::vector<Planet> planets = {
    { 0.4f, 0.05f, 4.7f, glm::vec3(0.5f, 0.5f, 0.5f) }, // Mercury
    { 0.7f, 0.08f, 3.5f, glm::vec3(0.8f, 0.6f, 0.2f) }, // Venus
    { 1.0f, 0.09f, 2.9f, glm::vec3(0.2f, 0.5f, 1.0f) }, // Earth
    { 1.5f, 0.06f, 2.4f, glm::vec3(1.0f, 0.2f, 0.2f) }, // Mars
    { 2.5f, 0.15f, 1.3f, glm::vec3(1.0f, 0.8f, 0.2f) }, // Jupiter (scaled down)
};

int main() {
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Solar System 3D", NULL, NULL);
    if (!window) {
        std::cerr << "Failed to create window.n";
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
        std::cerr << "Failed to initialize GLADn";
        return -1;
    }

    glEnable(GL_DEPTH_TEST);

    // Simplified rendering loop
    float time = 0.0f;

    while (!glfwWindowShouldClose(window)) {
        processInput(window);
        time += 0.01f;

        glClearColor(0.0f, 0.0f, 0.05f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Projection and view
        glm::mat4 projection = glm::perspective(glm::radians(45.0f),
                                (float)SCR_WIDTH / SCR_HEIGHT, 0.1f, 100.0f);
        glm::mat4 view = glm::lookAt(glm::vec3(0, 5, 10),
                                     glm::vec3(0, 0, 0),
                                     glm::vec3(0, 1, 0));

        // Draw Sun
        // Draw planet positions
        for (auto& planet : planets) {
            float angle = time * planet.speed;
            float x = cos(angle) * planet.distance;
            float z = sin(angle) * planet.distance;

            // Replace this with your drawSphere(x, 0, z, planet.size)
        }

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

// Resize window
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
    glViewport(0, 0, width, height);
}

// Close on Esc
void processInput(GLFWwindow *window) {
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}