0% found this document useful (0 votes)
14 views10 pages

2021BCS0103_CSE411_LAB-9

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views10 pages

2021BCS0103_CSE411_LAB-9

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

CSE-411 COMPUTER GRAPHICS LAB-9

Name-S.Vaishnavi Reddy
Roll no-2021BCS0103
Batch-1
Q) 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:

#ifdef _WIN32
#include <windows.h>
#endif

#include <GL/glut.h>
#include <cmath>
#include <cstring>

const float radius_of_orbit[] = {0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f};
const float size_of_planet[] = {0.02f, 0.03f, 0.025f, 0.04f, 0.035f, 0.05f,
0.045f, 0.055f};
const float COLORS[][3] = {
{0.5f, 0.5f, 0.5f},
{1.0f, 0.5f, 0.0f},
{0.0f, 0.0f, 1.0f},
{1.0f, 0.0f, 0.0f},
{1.0f, 0.8f, 0.4f},
{1.0f, 1.0f, 0.0f},
{0.0f, 1.0f, 1.0f},
{0.0f, 0.0f, 1.0f}
};
const char* names_of_planet[] = {"Mercury", "Venus", "Earth", "Mars",
"Jupiter", "Saturn", "Uranus", "Neptune"};

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 func_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();
}

// Code for Drawing orbit for each planet


void func_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);
glVertex2f(x, y);
}
glEnd();
}

// Code for Drawing text at specified position


void func_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);
func_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
func_drawOrbit(radius_of_orbit[i]);

// Update planet position


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

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


func_drawCircle(x, y, size_of_planet[i], 100);

// Draw planet name at the position


glColor3f(1.0f, 1.0f, 1.0f); // White color for text
func_drawText(names_of_planet[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();
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("Implementation of Solar System Animation with Planet
Labels By Vaishnavi Reddy");

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

You might also like