2021BCS0137 Cse-411 Lab-9

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

OCTOBER 29, 2024

CSE-411
COMPUTER GRAPHICS

LAB EXERCISE-9

MOTURI VENKATA SATYA KARTHIK


2021BCS0137
Develop a visual simulation of a simplified solar system that includes
Sun and the Planets (with unique size and color in a circular orbit
around the sun ). Include at least three planets. Implement smooth
animation to show the planets rotating around the Sun and spinning on
their axes.
Code:
#include <GL/glut.h>
#include <cmath>
#include <cstring>

// Constants for the orbital radii and colors of planets


const float ORBIT_RADIUS[] = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f,
0.8f};
const float PLANET_SIZE[] = {0.02f, 0.025f, 0.03f, 0.035f, 0.05f, 0.045f,
0.04f, 0.03f};
const float COLORS[][3] = {
{0.5f, 0.5f, 0.5f}, // Mercury (Gray)
{1.0f, 0.5f, 0.0f}, // Venus (Orange)
{0.0f, 0.0f, 1.0f}, // Earth (Blue)
{1.0f, 0.0f, 0.0f}, // Mars (Red)
{1.0f, 0.8f, 0.4f}, // Jupiter (Beige)
{1.0f, 1.0f, 0.0f}, // Saturn (Yellow)
{0.0f, 1.0f, 1.0f}, // Uranus (Cyan)
{0.0f, 0.0f, 1.0f} // Neptune (Dark Blue)
};
const char* PLANET_NAMES[] = {"Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};

// Variables for animating planets' rotations


float angle[] = {0.0f, 45.0f, 90.0f, 135.0f, 180.0f, 225.0f, 270.0f,
315.0f};
const float SPEED[] = {1.0f, 0.8f, 0.6f, 0.5f, 0.3f, 0.2f, 0.1f, 0.05f};

// Draw a filled circle


void drawCircle(float cx, float cy, float r, int num_segments) {
glBegin(GL_POLYGON);
for (int i = 0; i < num_segments; i++) {
float theta = 2.0f * 3.1415926f * float(i) / float(num_segments);
float x = r * cosf(theta);
float y = r * sinf(theta);
glVertex2f(x + cx, y + cy);
}
glEnd();
}

// Draw orbit for each planet


void drawOrbit(float radius) {
glBegin(GL_LINE_LOOP);
for (int i = 0; i < 100; i++) {
float theta = 2.0f * 3.1415926f * float(i) / float(100);
float x = radius * cosf(theta);
float y = radius * sinf(theta);

2|Page
glVertex2f(x, y);
}
glEnd();
}

// Draw text at specified position


void drawText(const char* text, float x, float y) {
glRasterPos2f(x, y);
for (const char* c = text; *c != '\0'; c++) {
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *c);
}
}

// Display callback for GLUT


void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

// Draw Sun
glColor3f(1.0f, 1.0f, 0.0f);
drawCircle(0.0f, 0.0f, 0.07f, 100);

// Draw orbits and planets


for (int i = 0; i < 8; i++) {
glColor3f(1.0f, 1.0f, 1.0f); // White color for orbits
drawOrbit(ORBIT_RADIUS[i]);

// Update planet position


float x = ORBIT_RADIUS[i] * cos(angle[i] * 3.14159 / 180);
float y = ORBIT_RADIUS[i] * sin(angle[i] * 3.14159 / 180);

glColor3f(COLORS[i][0], COLORS[i][1], COLORS[i][2]);


drawCircle(x, y, PLANET_SIZE[i], 100);

// Draw planet name at the position


glColor3f(1.0f, 1.0f, 1.0f); // White color for text
drawText(PLANET_NAMES[i], x + 0.03f, y + 0.03f); // Offset text
for visibility

// Increment the angle for animation


angle[i] += SPEED[i];
if (angle[i] > 360.0f)
angle[i] -= 360.0f;
}

glutSwapBuffers();
}

// Timer function for continuous animation


void timer(int) {
glutPostRedisplay();
glutTimerFunc(16, timer, 0);
}

// Initialize OpenGL settings


void init() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

3|Page
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv) {


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 800);
glutCreateWindow("Solar System By MVS Karthik");

init();
glutDisplayFunc(display);
glutTimerFunc(0, timer, 0);
glutMainLoop();
return 0;
}

Output:

4|Page
5|Page
6|Page

You might also like