Graphics assignment

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

Samrat Ashok Technological Institute (S. A. T. I.

),
Vidisha (M. P.), India
Practical- 1
Name : Samyak Jain Enrolment No : 0108CS221113
Subject/ Code: Practical Lab-1 (CS-506)
Branch: 5th Sem. BTech (CSE-B) Session: 2024-2025 Date of Execution: 24/09/24

2. Write a program to draw a line using DDA algorithm.

#include <GL/glut.h>
#include <stdio.h>
#include <math.h>

// Function to set a pixel on the window


void setPixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y); // Set the pixel at (x, y)
glEnd();
}

// Function to implement DDA algorithm


void drawLine(int x1, int y1, int x2, int y2) {
float dx = x2 - x1;
float dy = y2 - y1;

// Find the number of steps required


int steps = (fabs(dx) > fabs(dy)) ? fabs(dx) : fabs(dy);

// Calculate the increment for each step


float xIncrement = dx / steps;
float yIncrement = dy / steps;

float x = x1;
float y = y1;

// Draw the line by setting points


for (int i = 0; i <= steps; i++) {
setPixel(round(x), round(y));
x += xIncrement;
y += yIncrement;

1
}
glFlush(); // Render the drawing on the window
}

// Display function to render the line


void display() {
// Clear the window (white background)
glClear(GL_COLOR_BUFFER_BIT);

// Draw a black line using DDA algorithm from (50, 100) to (400, 300)
glColor3f(0.0, 0.0, 0.0); // Set the line color to black
drawLine(50, 100, 400, 300);
}

// Initialization function to set up OpenGL without gluOrtho2D


void init() {
// Set the background color (white)
glClearColor(1.0, 1.0, 1.0, 0.0); // White background

// Set the point size


glPointSize(2.0);

// Setup the projection matrix (manual orthographic projection)


glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Define the coordinate system manually (similar to gluOrtho2D)


glOrtho(0, 500, 0, 500, -1, 1); // Orthographic projection with x: 0-500, y: 0-500

// Switch to the modelview matrix


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

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


// Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500); // Set the window size
glutInitWindowPosition(100, 100); // Set the window position

// Create the window with title


glutCreateWindow("Samyak Jain”);

2
// Call initialization routines
init();

// Register the display callback function


glutDisplayFunc(display);

// Enter the GLUT event processing loop


glutMainLoop();

return 0;
}

5. Write a program to draw a circle using Bresenham‘s algorithm.

#include <GL/glut.h>
#include <stdio.h>

// Function to set a pixel on the window


void setPixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}

3
// Function to plot all the symmetrical points in a circle
void plotCirclePoints(int xc, int yc, int x, int y) {
setPixel(xc + x, yc + y);
setPixel(xc - x, yc + y);
setPixel(xc + x, yc - y);
setPixel(xc - x, yc - y);
setPixel(xc + y, yc + x);
setPixel(xc - y, yc + x);
setPixel(xc + y, yc - x);
setPixel(xc - y, yc - x);
}

// Function to implement Bresenham's circle drawing algorithm


void drawCircle(int xc, int yc, int r) {
int x = 0;
int y = r;
int d = 3 - 2 * r; // Initial decision parameter

plotCirclePoints(xc, yc, x, y); // Plot the initial points

while (y >= x) {
x++;

// Update decision parameter based on the value of d


if (d > 0) {
y--;
d = d + 4 * (x - y) + 10;
} else {
d = d + 4 * x + 6;
}

plotCirclePoints(xc, yc, x, y); // Plot all symmetrical points


}

glFlush(); // Render the drawing on the window


}

// Display function to render the circle


void display() {
// Clear the window with white background
glClear(GL_COLOR_BUFFER_BIT);

// Draw a circle using Bresenham's algorithm with center (250, 250) and radius
100

4
glColor3f(0.0, 0.0, 0.0); // Set the color to black
drawCircle(250, 250, 100);
}

// Initialization function to set up OpenGL without gluOrtho2D


void init() {
// Set the background color (white)
glClearColor(1.0, 1.0, 1.0, 0.0); // White background

// Set the point size


glPointSize(2.0);

// Setup the projection matrix (manual orthographic projection)


glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Define the coordinate system manually (similar to gluOrtho2D)


glOrtho(0, 500, 0, 500, -1, 1); // Orthographic projection with x: 0-500, y: 0-500

// Switch to the modelview matrix


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

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


// Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500); // Set the window size
glutInitWindowPosition(100, 100); // Set the window position

// Create the window with title


glutCreateWindow("Samyak Jain");

// Call initialization routines


init();

// Register the display callback function


glutDisplayFunc(display);

// Enter the GLUT event processing loop


glutMainLoop();

return 0;

5
}

7. Write a program to perform 2D Transformation on a line.

#include <GL/glut.h>
#include <stdio.h>
#include <math.h>

// Initial coordinates of the line


float line_x1 = 50, line_y1 = 100, line_x2 = 400, line_y2 = 300;

// Function to set a pixel on the window


void setPixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}

// Function to draw a line using the DDA algorithm


void drawLine(float x1, float y1, float x2, float y2) {
float dx = x2 - x1;
float dy = y2 - y1;

// Find the number of steps required


int steps = (fabs(dx) > fabs(dy)) ? fabs(dx) : fabs(dy);

6
// Calculate the increment for each step
float xIncrement = dx / steps;
float yIncrement = dy / steps;

float x = x1;
float y = y1;

// Draw the line by setting points


for (int i = 0; i <= steps; i++) {
setPixel(round(x), round(y));
x += xIncrement;
y += yIncrement;
}
}

// Function to apply translation


void translate(float tx, float ty) {
glTranslatef(tx, ty, 0); // Translate the line
}

// Function to apply scaling


void scale(float sx, float sy) {
glScalef(sx, sy, 1); // Scale the line
}

// Function to apply rotation


void rotate(float angle) {
glRotatef(angle, 0.0, 0.0, 1.0); // Rotate the line
}

// Display function to render the line


void display() {
// Clear the window with white background
glClear(GL_COLOR_BUFFER_BIT);

// Set the color to black


glColor3f(0.0, 0.0, 0.0);

// Draw the original line


drawLine(line_x1, line_y1, line_x2, line_y2);

// Apply transformations and draw the transformed line


glPushMatrix(); // Save the current matrix
translate(50, 50); // Translate the line by (50, 50)

7
rotate(45); // Rotate the line by 45 degrees
scale(1.5, 1.5); // Scale the line by 1.5 in both x and y directions
drawLine(line_x1, line_y1, line_x2, line_y2); // Draw the transformed line
glPopMatrix(); // Restore the original matrix

glFlush(); // Render the drawing on the window


}

// Initialization function to set up OpenGL without gluOrtho2D


void init() {
// Set the background color (white)
glClearColor(1.0, 1.0, 1.0, 0.0); // White background

// Set the point size


glPointSize(2.0);

// Setup the projection matrix (manual orthographic projection)


glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Define the coordinate system manually (similar to gluOrtho2D)


glOrtho(0, 500, 0, 500, -1, 1); // Orthographic projection with x: 0-500, y: 0-500

// Switch to the modelview matrix


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

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


// Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500); // Set the window size
glutInitWindowPosition(100, 100); // Set the window position

// Create the window with title


glutCreateWindow("Samyak Jain");

// Call initialization routines


init();

// Register the display callback function


glutDisplayFunc(display);

8
// Enter the GLUT event processing loop
glutMainLoop();

return 0;
}

10. Write a program to draw a car using in build graphics function


and translate it from bottom left
corner to right bottom corner of screen.

#include <GL/glut.h>
#include <stdio.h>
#include <math.h>

// Initial position of the car


float carX = -100.0f; // Start from outside the left edge
float carSpeed = 0.5f; // Speed of translation

// Function to draw the car


void drawCar(float x, float y) {
// Draw the body of the car
glBegin(GL_QUADS); // Start drawing a quadrilateral
glColor3f(0.0f, 0.0f, 1.0f); // Blue color
glVertex2f(x, y); // Bottom left
glVertex2f(x + 100, y); // Bottom right
glVertex2f(x + 100, y + 40); // Top right

9
glVertex2f(x, y + 40); // Top left
glEnd();

// Draw the roof of the car


glBegin(GL_QUADS);
glColor3f(0.0f, 0.0f, 0.5f); // Dark blue color
glVertex2f(x + 20, y + 40); // Bottom left of roof
glVertex2f(x + 80, y + 40); // Bottom right of roof
glVertex2f(x + 70, y + 60); // Top right of roof
glVertex2f(x + 30, y + 60); // Top left of roof
glEnd();

// Draw the wheels of the car


glBegin(GL_TRIANGLE_FAN); // Draw circles for wheels
glColor3f(0.0f, 0.0f, 0.0f); // Black color for wheels
for (int i = 0; i < 360; i++) {
float angle = i * 3.14159 / 180; // Convert degrees to radians
float wheelX = x + 20 + 20 * cos(angle);
float wheelY = y - 10 + 20 * sin(angle);
glVertex2f(wheelX, wheelY);
}
glEnd();

glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i < 360; i++) {
float angle = i * 3.14159 / 180;
float wheelX = x + 70 + 20 * cos(angle);
float wheelY = y - 10 + 20 * sin(angle);
glVertex2f(wheelX, wheelY);
}
glEnd();
}

// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
glLoadIdentity(); // Load identity matrix

// Draw the car at its current position


drawCar(carX, 100);

glFlush(); // Render
}

10
// Update function for animation
void update(int value) {
// Update the position of the car
carX += carSpeed;

// Reset position if the car goes off-screen


if (carX > 500) {
carX = -100; // Start from left again
}

glutPostRedisplay(); // Request a new display


glutTimerFunc(16, update, 0); // Call update function every 16 ms (approximately
60 FPS)
}

// Initialization function
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white
glColor3f(0.0f, 0.0f, 0.0f); // Set drawing color to black
glPointSize(1.0); // Set point size
glLineWidth(1.0); // Set line width

// Set the view port and projection matrix manually


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 500, 0, 500, -1, 1); // Define the orthographic projection with zNear and
zFar
glMatrixMode(GL_MODELVIEW);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500); // Set the window size
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow(“Samyak Jain”); // Create the window

init(); // Call initialization routines


glutDisplayFunc(display); // Register display callback function
glutTimerFunc(0, update, 0); // Set up timer for animation
glutMainLoop(); // Enter the GLUT event processing loop

return 0;
}

11
11. Write a program to draw balloons using in build graphics
function and translate it from bottom
left corner to right top corner of screen.

#include <GL/glut.h>
#include <math.h>

// Initial position of the balloons


float balloonX = -50.0f; // Start from outside the left edge
float balloonY = -50.0f; // Start from outside the bottom edge
float balloonSpeedX = 0.1f; // Horizontal speed
float balloonSpeedY = 0.1f; // Vertical speed

// Function to draw a balloon


void drawBalloon(float x, float y, float radius) {
// Draw the body of the balloon (a circle)
glBegin(GL_TRIANGLE_FAN); // Start drawing the balloon's circular body
glColor3f(1.0f, 0.0f, 0.0f); // Red color for the balloon
glVertex2f(x, y); // Center of the circle
for (int i = 0; i <= 360; i++) {
float angle = i * 3.14159 / 180; // Convert degrees to radians
float balloonX = x + radius * cos(angle);
float balloonY = y + radius * sin(angle);
glVertex2f(balloonX, balloonY);

12
}
glEnd();

// Draw the balloon string (a line)


glColor3f(0.0f, 0.0f, 0.0f); // Black color for the string
glBegin(GL_LINES);
glVertex2f(x, y - radius); // Bottom of the balloon
glVertex2f(x, y - radius - 30); // End of the string
glEnd();
}

// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
glLoadIdentity(); // Load identity matrix

// Draw three balloons at different positions


drawBalloon(balloonX, balloonY, 30.0f);
drawBalloon(balloonX + 50, balloonY + 20, 30.0f);
drawBalloon(balloonX + 100, balloonY + 40, 30.0f);

glFlush(); // Render the scene


}

// Update function for animation


void update(int value) {
// Update the position of the balloons
balloonX += balloonSpeedX;
balloonY += balloonSpeedY;

// Reset position if the balloons go off-screen


if (balloonX > 500 || balloonY > 500) {
balloonX = -50; // Reset to bottom-left corner
balloonY = -50;
}

glutPostRedisplay(); // Request a new display


glutTimerFunc(16, update, 0); // Call update function every 16 ms (approximately
60 FPS)
}

// Initialization function
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white

13
glColor3f(0.0f, 0.0f, 0.0f); // Set drawing color to black
glPointSize(1.0); // Set point size
glLineWidth(1.0); // Set line width

// Set the view port and projection matrix manually


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 500, 0, 500, -1, 1); // Define the orthographic projection manually
glMatrixMode(GL_MODELVIEW);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500); // Set the window size
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("Samyak Jain"); // Create the window

init(); // Call initialization routines


glutDisplayFunc(display); // Register display callback function
glutTimerFunc(0, update, 0); // Set up timer for animation
glutMainLoop(); // Enter the GLUT event processing loop

return 0;
}

14
12. Write a program to draw a cube using in build library function
and perform 3D transformations
i) Translations in x, y, z directions
ii) Rotation by angle 450 about z axis, rotation by 600 about y-axis
in succession.
iii) Scaling in x-direction by a factor of 2, scaling in y- direction by a
factor of 3.

#include <GL/glut.h>

// Initial position of the cube


float transX = 0.0f, transY = 0.0f, transZ = -5.0f;

// Rotation angles
float rotAngleZ = 0.0f;
float rotAngleY = 0.0f;

// Function to draw a simple square (to debug visibility)


void drawSquare() {
glBegin(GL_QUADS); // Begin quadrilateral drawing
glColor3f(1.0f, 0.0f, 0.0f); // Red color
glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom left
glVertex3f(1.0f, -1.0f, 0.0f); // Bottom right
glVertex3f(1.0f, 1.0f, 0.0f); // Top right
glVertex3f(-1.0f, 1.0f, 0.0f); // Top left
glEnd();
}

// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen
and depth buffer
glLoadIdentity(); // Reset the matrix

// Translate and rotate


glTranslatef(transX, transY, transZ);
glRotatef(rotAngleZ, 0.0f, 0.0f, 1.0f);
glRotatef(rotAngleY, 0.0f, 1.0f, 0.0f);

// Draw a square instead of a cube (just for testing)


drawSquare();

15
glutSwapBuffers(); // Swap buffers for double buffering
}

// Idle function for rotation


void idle() {
rotAngleZ += 0.2f; // Rotate Z
rotAngleY += 0.2f; // Rotate Y
glutPostRedisplay(); // Redraw
}

// Initialization function
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // White background
glEnable(GL_DEPTH_TEST); // Enable depth testing

// Set up perspective
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 1.0, 100.0); // Perspective with a viewing frustum
glMatrixMode(GL_MODELVIEW); // Back to model view matrix
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 600); // Set the window size
glutCreateWindow("Samyak Jain"); // Create the window

init(); // Initialize
glutDisplayFunc(display); // Display callback
glutIdleFunc(idle); // Idle callback to animate
glutMainLoop(); // Enter the main loop

return 0;
}

16
13. Write a
program for making Bezier curve.

#include <GL/glut.h>
#include <math.h>

// Define control points


float ctrlPoints[4][2] = {
{ -0.8, -0.8 }, // P0
{ -0.4, 0.8 }, // P1
{ 0.4, -0.8 }, // P2
{ 0.8, 0.8 } // P3
};

// Function to calculate the point on the Bezier curve at parameter t


void bezierCurve(float t, float* x, float* y) {
float u = 1 - t;
*x = u * u * u * ctrlPoints[0][0] + 3 * u * u * t * ctrlPoints[1][0] + 3 * u * t * t *
ctrlPoints[2][0] + t * t * t * ctrlPoints[3][0];
*y = u * u * u * ctrlPoints[0][1] + 3 * u * u * t * ctrlPoints[1][1] + 3 * u * t * t *
ctrlPoints[2][1] + t * t * t * ctrlPoints[3][1];
}

// Function to draw a Bezier curve


void drawBezierCurve() {
glBegin(GL_LINE_STRIP); // Draw continuous line segments

17
for (float t = 0; t <= 1.0; t += 0.01) {
float x, y;
bezierCurve(t, &x, &y);
glVertex2f(x, y); // Plot the calculated point on the curve
}
glEnd();
}

// Function to draw control points


void drawControlPoints() {
glPointSize(5.0);
glBegin(GL_POINTS);
for (int i = 0; i < 4; i++) {
glVertex2f(ctrlPoints[i][0], ctrlPoints[i][1]); // Plot control points
}
glEnd();
}

// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen

// Draw the control points


glColor3f(1.0, 0.0, 0.0); // Red color for control points
drawControlPoints();

// Draw the Bezier curve


glColor3f(0.0, 0.0, 1.0); // Blue color for curve
drawBezierCurve();

glFlush(); // Render now


}

// Initialization function
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background to white
glColor3f(0.0f, 0.0f, 0.0f); // Set drawing color to black
glPointSize(1.0); // Set point size
}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

18
glutInitWindowSize(500, 500); // Set the window size
glutCreateWindow("Samyak Jain"); // Create window with title

init(); // Initialize settings


glutDisplayFunc(display); // Register display callback
glutMainLoop(); // Enter the main loop

return 0;
}

14. Write a program to study various in build functions for 2D


drawing in MAYA software.

import maya.cmds as cmds

def draw_shapes():
# Clear existing shapes
cmds.delete(cmds.ls('myShapes*'))

# Draw a circle
circle = cmds.circle(name='myCircle', radius=1, ch=False)
cmds.setAttr(circle[0] + ".overrideEnabled", 1)
cmds.setAttr(circle[0] + ".overrideColor", 17) # Blue color

# Draw a square (using a polygon)

19
square = cmds.polyCube(name='mySquare', width=2, height=0.1, depth=2,
ch=False)
cmds.move(3, 0, 0) # Move square to the right
cmds.setAttr(square[0] + ".overrideEnabled", 1)
cmds.setAttr(square[0] + ".overrideColor", 6) # Yellow color

# Draw a triangle (using a polygon)


triangle = cmds.polyPlane(name='myTriangle', width=2, height=2, ch=False)
cmds.move(-3, 0, 0) # Move triangle to the left
cmds.setAttr(triangle[0] + ".overrideEnabled", 1)
cmds.setAttr(triangle[0] + ".overrideColor", 13) # Green color

# Draw lines (using curves)


line1 = cmds.curve(name='myLine1', d=1, p=[-4, 0, 0, 0, 4, 0])
cmds.setAttr(line1 + ".overrideEnabled", 1)
cmds.setAttr(line1 + ".overrideColor", 1) # Red color

line2 = cmds.curve(name='myLine2', d=1, p=[0, -4, 0, 0, 4, 0])


cmds.setAttr(line2 + ".overrideEnabled", 1)
cmds.setAttr(line2 + ".overrideColor", 3) # Light Blue color

print("Shapes drawn successfully!")

# Run the function to draw shapes


draw_shapes()

20
15. Write a program to show animation of a ball moving in a helical
path.

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

// Parameters for the ball's movement


float angle = 0.0f; // Angle for helical path
float radius = 1.0f; // Radius of the helix
float height = 0.05f; // Vertical distance per complete rotation
float speed = 0.02f; // Speed of the ball

// Function to draw the ball


void drawBall(float x, float y, float z) {
glColor3f(1.0f, 0.0f, 0.0f); // Red color for the ball
glPushMatrix();
glTranslatef(x, y, z); // Translate to the position of the ball
glutSolidSphere(0.05, 20, 20); // Draw the ball
glPopMatrix();
}

// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the
screen and depth buffer
glLoadIdentity(); // Load the identity matrix

// Calculate the position of the ball on the helical path


float x = radius * cos(angle);
float y = radius * sin(angle);
float z = height * angle / (2 * M_PI); // Vertical height increases with angle

// Draw the ball


drawBall(x, y, z);

glFlush(); // Render the scene


glutSwapBuffers(); // Swap the buffers for double buffering
}

// Update function for animation


void update(int value) {
angle += speed; // Update the angle for helical motion

21
if (angle > 2 * M_PI) {
angle -= 2 * M_PI; // Reset angle after a full circle
}

glutPostRedisplay(); // Request a new display


glutTimerFunc(16, update, 0); // Call update function every 16 ms (approx. 60
FPS)
}

// Initialization function
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white with full opacity

glEnable(GL_DEPTH_TEST); // Enable depth testing for 3D effects


}

// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); // Enable
double buffering and depth buffer
glutInitWindowSize(800, 600); // Set the window size
glutCreateWindow("Samyak Jain"); // Create the window

init(); // Call initialization routines


glutDisplayFunc(display); // Register display callback function
glutTimerFunc(0, update, 0); // Set up timer for animation
glutMainLoop(); // Enter the GLUT event processing loop

return 0;
}

22
16. Write a program to show animation of solar system.

#include <GL/glut.h>
#include <math.h>

// Define the radius of the sun and planet


const float sunRadius = 0.2f;
const float planetRadius = 0.05f;
const float orbitRadius = 0.5f;
const float orbitSpeed = 0.01f;

float angle = 0.0f; // Angle for planet orbiting

// Function to draw the sun


void drawSun() {
glColor3f(1.0f, 1.0f, 0.0f); // Yellow color for the sun
glBegin(GL_TRIANGLE_FAN);
glVertex2f(0.0f, 0.0f); // Center of the sun
for (int i = 0; i <= 360; i++) {
float theta = i * 3.14159 / 180; // Convert degrees to radians
float x = sunRadius * cos(theta);
float y = sunRadius * sin(theta);
glVertex2f(x, y);
}
glEnd();

23
}

// Function to draw the planet


void drawPlanet(float distance, float angle) {
float x = distance * cos(angle);
float y = distance * sin(angle);

glPushMatrix(); // Save the current matrix


glTranslatef(x, y, 0.0f); // Move to the planet's position

glColor3f(0.0f, 0.0f, 1.0f); // Blue color for the planet


glBegin(GL_TRIANGLE_FAN);
glVertex2f(0.0f, 0.0f); // Center of the planet
for (int i = 0; i <= 360; i++) {
float theta = i * 3.14159 / 180; // Convert degrees to radians
float px = planetRadius * cos(theta);
float py = planetRadius * sin(theta);
glVertex2f(px, py);
}
glEnd();

glPopMatrix(); // Restore the previous matrix


}

// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
glLoadIdentity(); // Load the identity matrix

drawSun(); // Draw the sun

// Draw the planet in orbit


drawPlanet(orbitRadius, angle);

glFlush(); // Render
}

// Update function for animation


void update(int value) {
angle += orbitSpeed; // Update the angle for the planet's orbit
if (angle > 2 * M_PI) {
angle -= 2 * M_PI; // Keep the angle in the range [0, 2π]
}

24
glutPostRedisplay(); // Request a new display
glutTimerFunc(16, update, 0); // Call update function every 16 ms (approximately
60 FPS)
}

// Initialization function
void init() {
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // Set background color to white
glColor3f(0.0f, 0.0f, 0.0f); // Set drawing color to black
glPointSize(1.0); // Set point size
glLineWidth(1.0); // Set line width

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.5, 1.5, -1.5, 1.5, -1.0, 1.0); // Define the orthographic projection
manually
glMatrixMode(GL_MODELVIEW);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500); // Set the window size
glutInitWindowPosition(100, 100); // Set the window position
glutCreateWindow("Samyak Jain"); // Create the window

init(); // Call initialization routines


glutDisplayFunc(display); // Register display callback function
glutTimerFunc(0, update, 0); // Set up timer for animation
glutMainLoop(); // Enter the GLUT event processing loop

return 0;
}

25
26

You might also like