#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
const int numPlanets = 8;
const float distanceScale = 0.1f;
const float rotationSpeed = 0.01f;
GLFWwindow* window;
float aspectRatio = 16.0f / 9.0f;
float deltaTime = 0.0f;
float lastFrame = 0.0f;
struct Planet {
float distance;
float angle;
float radius;
glm::vec3 color;
};
Planet planets[numPlanets] = {
{0.0f, 0.0f, 1.0f, glm::vec3(1.0f, 1.0f, 0.0f)}, // Sun
{2.0f, 0.0f, 0.2f, glm::vec3(0.5f, 0.5f, 1.0f)}, // Mercury
{3.0f, 0.0f, 0.4f, glm::vec3(0.7f, 0.2f, 0.2f)}, // Venus
{5.0f, 0.0f, 0.5f, glm::vec3(0.2f, 0.5f, 0.8f)}, // Earth
{8.0f, 0.0f, 0.3f, glm::vec3(0.8f, 0.8f, 0.8f)}, // Mars
{12.0f, 0.0f, 1.0f, glm::vec3(0.9f, 0.7f, 0.5f)}, // Jupiter
{20.0f, 0.0f, 0.8f, glm::vec3(0.5f, 0.7f, 0.9f)}, // Saturn
{30.0f, 0.0f, 0.6f, glm::vec3(0.6f, 0.6f, 0.6f)} // Uranus
// Add more planets as needed
};
void initGL() {
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW\n";
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
window = glfwCreateWindow(800, 600, "Solar System Simulation", NULL, NULL);
if (!window) {
std::cerr << "Failed to open GLFW window\n";
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cerr << "Failed to initialize GLEW\n";
exit(EXIT_FAILURE);
}
glViewport(0, 0, 800, 600);
glEnable(GL_DEPTH_TEST);
}
void update() {
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
for (int i = 0; i < numPlanets; ++i) {
planets[i].angle += rotationSpeed * deltaTime;
if (planets[i].angle > 360.0f) {
planets[i].angle -= 360.0f;
}
}
}
void render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set up projection and view matrices
glm::mat4 projection = glm::perspective(glm::radians(45.0f), aspectRatio, 0.1f, 100.0f);
glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 15.0f), glm::vec3(0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
for (int i = 0; i < numPlanets; ++i) {
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(planets[i].distance * distanceScale, 0.0f, 0.0f));
model = glm::rotate(model, glm::radians(planets[i].angle), glm::vec3(0.0f, 1.0f, 0.0f));
model = glm::translate(model, glm::vec3(planets[i].radius, 0.0f, 0.0f));
glm::mat4 mvp = projection * view * model;
// Draw the planet
// Replace this with your rendering code (e.g., a sphere)
// Example: drawSphere(planets[i].radius, planets[i].color, mvp);
}
glfwSwapBuffers(window);
}
int main() {
initGL();
while (!glfwWindowShouldClose(window)) {
update();
render();
glfwPollEvents();
}
glfwTerminate();
return 0;
} Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++ and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}