0% found this document useful (0 votes)
40 views

CG Lab Mannual

Uploaded by

shadaburrahaman9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

CG Lab Mannual

Uploaded by

shadaburrahaman9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Computer Graphics and Visualization BCG402

PES Institute of Technology and Management

Department of Computer Science & Design

Laboratory Manual

Semester: IV

Subject: Computer Graphics and Visualization

Subject Code: BCG402

Compiled By:

Ms. Preethi S Gowda Dr. Pramod


Assistant Professor Head Of the Department

NH-206, Sagar Road, Shivamogga-577204


Ph: 08182-640733/640734 Fax: 08182-233797
www.pestrust.edu.in/pesitm

Dept. Of CSD, PESITM, Shivamogga 1


Computer Graphics and Visualization BCG402

Dept. Of CSD, PESITM, Shivamogga 2


Computer Graphics and Visualization BCG402

Dept. Of CSD, PESITM, Shivamogga 3


Computer Graphics and Visualization BCG402

1. Develop OpenGL program to draw a lineusing Bresenham’s


algorithm for all types of slopes.

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

int x1, y1, x2, y2;

void plot(int x, int y) {


glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}

void drawLine(int x1, int y1, int x2, int y2) {


int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int slope1 = dy << 1;
int slope2 = (dy - dx) << 1;
int p = slope1 - dx;
int x, y;

if (x1 > x2) {


x = x2;
y = y2;
x2 = x1;
} else {
x = x1;
y = y1;
}

plot(x, y);

while (x < x2) {


x++;
if (p < 0)
p += slope1;
else {
y++;
p += slope2;
}
plot(x, y);
}
}

void display() {

Dept. Of CSD, PESITM, Shivamogga 4


Computer Graphics and Visualization BCG402

glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);

// Draw the line


printf("Drawing line from (%d, %d) to (%d, %d)\n", x1, y1, x2, y2);
drawLine(x1, y1, x2, y2);

// Check for OpenGL errors


GLenum error = glGetError();
if (error != GL_NO_ERROR) {
printf("OpenGL error: %s\n", gluErrorString(error));
}

glFlush();
}

void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
}

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


printf("Enter two points (x1 y1 x2 y2): ");
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Bresenham's Line Drawing Algorithm");
init();
glutDisplayFunc(display);
glutMainLoop();

return 0;
}

Dept. Of CSD, PESITM, Shivamogga 5


Computer Graphics and Visualization BCG402

Sample Output:

2. Develop OpenGL program to create and rotate a triangle about


the origin and a fixed point.

#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265

int rotateOrigin = 0;
int rotateFixedPoint = 0;

void drawTriangle() {
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0); // Red color
glVertex2f(0.0, 0.5); // Top vertex
glColor3f(0.0, 1.0, 0.0); // Green color
glVertex2f(-0.5, -0.5); // Bottom left vertex
glColor3f(0.0, 0.0, 1.0); // Blue color
glVertex2f(0.5, -0.5); // Bottom right vertex
glEnd();
}

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

// Draw the triangle


drawTriangle();

// Rotate about the origin


glPushMatrix();
glRotatef(rotateOrigin, 0.0, 0.0, 1.0);
drawTriangle();
glPopMatrix();

Dept. Of CSD, PESITM, Shivamogga 6


Computer Graphics and Visualization BCG402

// Rotate about a fixed point


glPushMatrix();
glTranslatef(0.25, 0.25, 0.0); // Translate to the fixed point
glRotatef(rotateFixedPoint, 0.0, 0.0, 1.0); // Rotate
glTranslatef(-0.25, -0.25, 0.0); // Translate back
drawTriangle();
glPopMatrix();

glFlush();
}

void init() {
glClearColor(0.0, 0.0, 0.0, 1.0); // Black background
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); // 2D orthographic projection
}

void timer(int value) {


rotateOrigin += 1; // Rotate about origin
rotateFixedPoint -= 1; // Rotate about fixed point
glutPostRedisplay();
glutTimerFunc(16, timer, 0); // 60 frames per second
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Triangle Rotation");

init();
glutDisplayFunc(display);
glutTimerFunc(0, timer, 0); // Start the timer
glutMainLoop();

return 0;
}

Sample Output:

Dept. Of CSD, PESITM, Shivamogga 7


Computer Graphics and Visualization BCG402

3. Develop a OpenGL program to implement to recursively


subdivide a tetrahedron to form 3D sierpinski gasket. The number
of recursive steps is to be specified by the user.

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

typedef GLfloat point3[3];

// Initial vertices of a tetrahedron


point3 v[4] = {{0.0, 0.0, 1.0}, {0.0, 0.942809, -0.333333}, {-0.816497, -
0.471405, -0.333333}, {0.816497, -0.471405, -0.333333}};
int n;

// Draw a triangle
void triangle(point3 a, point3 b, point3 c) {
glBegin(GL_TRIANGLES);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}

// Divide a triangle recursively


void divideTriangle(point3 a, point3 b, point3 c, int m) {
point3 v1, v2, v3;
int j;
if (m > 0) {
for (j = 0; j < 3; j++) {
v1[j] = (a[j] + b[j]) / 2;
v2[j] = (a[j] + c[j]) / 2;
v3[j] = (b[j] + c[j]) / 2;
}
divideTriangle(a, v1, v2, m - 1);
divideTriangle(c, v2, v3, m - 1);

Dept. Of CSD, PESITM, Shivamogga 8


Computer Graphics and Visualization BCG402

divideTriangle(b, v3, v1, m - 1);


} else {
triangle(a, b, c);
}
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
divideTriangle(v[0], v[1], v[2], n);
divideTriangle(v[3], v[2], v[1], n);
divideTriangle(v[0], v[3], v[1], n);
divideTriangle(v[0], v[2], v[3], n);
glFlush();
}

// Initialize OpenGL
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}

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


printf("Enter the number of recursive steps: ");
scanf("%d", &n);

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("3D Sierpinski Gasket");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Sample Output:

Dept. Of CSD, PESITM, Shivamogga 9


Computer Graphics and Visualization BCG402

4. Develop a OpenGL program to Spin 3D sierpinski gasket using


OpenGL transformation matrices.

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

typedef GLfloat point3[3];

// Initial vertices of a tetrahedron


point3 v[4] = {{0.0, 0.0, 1.0}, {0.0, 0.942809, -0.333333}, {-0.816497, -
0.471405, -0.333333}, {0.816497, -0.471405, -0.333333}};
int n;
GLfloat theta = 0.0; // Angle of rotation

// Draw a triangle
void triangle(point3 a, point3 b, point3 c) {
glBegin(GL_TRIANGLES);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}

// Divide a triangle recursively


void divideTriangle(point3 a, point3 b, point3 c, int m) {
point3 v1, v2, v3;
int j;
if (m > 0) {
for (j = 0; j < 3; j++) {
v1[j] = (a[j] + b[j]) / 2;
v2[j] = (a[j] + c[j]) / 2;
v3[j] = (b[j] + c[j]) / 2;
}
divideTriangle(a, v1, v2, m - 1);
divideTriangle(c, v2, v3, m - 1);

Dept. Of CSD, PESITM, Shivamogga 10


Computer Graphics and Visualization BCG402

divideTriangle(b, v3, v1, m - 1);


} else {
triangle(a, b, c);
}
}

// Display callback function


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

// Set up viewing transformation


gluLookAt(0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

// Apply rotation transformation


glRotatef(theta, 0.0, 1.0, 0.0);

// Draw the Sierpinski gasket


divideTriangle(v[0], v[1], v[2], n);
divideTriangle(v[3], v[2], v[1], n);
divideTriangle(v[0], v[3], v[1], n);
divideTriangle(v[0], v[2], v[3], n);

glFlush();
}

// Idle callback function to continuously rotate the object


void spin() {
theta += 0.1; // Increment rotation angle
if (theta > 360.0)
theta -= 360.0; // Keep angle within 360 degrees
glutPostRedisplay(); // Request redisplay
}

// Initialize OpenGL
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, 1.0, 1.0, 20.0); // Set up perspective projection
glMatrixMode(GL_MODELVIEW);
}

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


printf("Enter the number of recursive steps: ");
scanf("%d", &n);

Dept. Of CSD, PESITM, Shivamogga 11


Computer Graphics and Visualization BCG402

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("3D Sierpinski Gasket");
init();
glutDisplayFunc(display);
glutIdleFunc(spin); // Register idle callback function
glutMainLoop();
return 0;
}
Sample Output:

5. Develop a OpenGL program to Clip 2D lines using Cohen-


Sutherland algorithm.

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

// Define region codes


#define INSIDE 0 // 0000
#define LEFT 1 // 0001
#define RIGHT 2 // 0010
#define BOTTOM 4 // 0100
#define TOP 8 // 1000

// Define window boundaries


#define X_MIN -50
#define X_MAX 50
#define Y_MIN -50
#define Y_MAX 50

// Define line clipping region codes


int computeCode(double x, double y) {
int code = INSIDE;

Dept. Of CSD, PESITM, Shivamogga 12


Computer Graphics and Visualization BCG402

if (x < X_MIN)
code |= LEFT;
else if (x > X_MAX)
code |= RIGHT;
if (y < Y_MIN)
code |= BOTTOM;
else if (y > Y_MAX)
code |= TOP;

return code;
}

// Cohen-Sutherland line clipping algorithm


void cohenSutherland(double x0, double y0, double x1, double y1) {
int code0 = computeCode(x0, y0);
int code1 = computeCode(x1, y1);
double x, y;

while (1) {
if (!(code0 | code1)) {
// Both endpoints are inside the window
glColor3f(0.0, 1.0, 0.0); // Green color for clipped line
glBegin(GL_LINES);
glVertex2d(x0, y0);
glVertex2d(x1, y1);
glEnd();
break;
} else if (code0 & code1) {
// Both endpoints are outside the window
glColor3f(1.0, 0.0, 0.0); // Red color for rejected line
glBegin(GL_LINES);
glVertex2d(x0, y0);
glVertex2d(x1, y1);
glEnd();
break;
} else {
// Line needs to be clipped
int codeOut = code0 ? code0 : code1;
if (codeOut & TOP) {
x = x0 + (x1 - x0) * (Y_MAX - y0) / (y1 - y0);
y = Y_MAX;
} else if (codeOut & BOTTOM) {
x = x0 + (x1 - x0) * (Y_MIN - y0) / (y1 - y0);
y = Y_MIN;
} else if (codeOut & RIGHT) {
y = y0 + (y1 - y0) * (X_MAX - x0) / (x1 - x0);
x = X_MAX;
} else if (codeOut & LEFT) {

Dept. Of CSD, PESITM, Shivamogga 13


Computer Graphics and Visualization BCG402

y = y0 + (y1 - y0) * (X_MIN - x0) / (x1 - x0);


x = X_MIN;
}

// Replace outside point with intersection point


if (codeOut == code0) {
x0 = x;
y0 = y;
code0 = computeCode(x0, y0);
} else {
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
}
}
}
}

// Display callback function


void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0); // Black color for window boundary
glBegin(GL_LINE_LOOP);
glVertex2f(X_MIN, Y_MIN);
glVertex2f(X_MAX, Y_MIN);
glVertex2f(X_MAX, Y_MAX);
glVertex2f(X_MIN, Y_MAX);
glEnd();

// Example lines to clip


cohenSutherland(-60, -20, 30, 70); // Line outside
cohenSutherland(10, 20, 70, 80); // Line inside
cohenSutherland(-40, 60, 60, -40); // Line crossing

glFlush();
}

// Initialize OpenGL
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0); // White background
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100); // Set up 2D orthographic projection
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

Dept. Of CSD, PESITM, Shivamogga 14


Computer Graphics and Visualization BCG402

glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Cohen-Sutherland Line Clipping");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Sample Output:

6. Develop a menu driven program to animate the polygon using


3D geometric transformations.

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

float angle = 0.0;


float scale_factor = 1.0;
float translate_x = 0.0;
float translate_y = 0.0;
float translate_z = 0.0;

void drawPolygon() {
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0); // Blue color
glVertex3f(-0.5, -0.5, 0.0);
glVertex3f(0.5, -0.5, 0.0);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(-0.5, 0.5, 0.0);
glEnd();
}

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

Dept. Of CSD, PESITM, Shivamogga 15


Computer Graphics and Visualization BCG402

// Apply transformations
glTranslatef(translate_x, translate_y, translate_z);
glRotatef(angle, 0.0, 0.0, 1.0);
glScalef(scale_factor, scale_factor, scale_factor);

drawPolygon();
glutSwapBuffers();
}

void translate() {
printf("Enter translation values (x y z): ");
scanf("%f %f %f", &translate_x, &translate_y, &translate_z);
glutPostRedisplay(); // Update display after translation
}

void rotate() {
printf("Enter rotation angle (degrees): ");
scanf("%f", &angle);
glutPostRedisplay(); // Update display after rotation
}

void scale() {
printf("Enter scale factor: ");
scanf("%f", &scale_factor);
glutPostRedisplay(); // Update display after scaling
}

void exitProgram() {
exit(0);
}

void init() {
glClearColor(1.0, 1.0, 1.0, 1.0); // White background
glEnable(GL_DEPTH_TEST);
}

void menu(int value) {


switch (value) {
case 1:
translate();
break;
case 2:
rotate();
break;
case 3:
scale();
break;

Dept. Of CSD, PESITM, Shivamogga 16


Computer Graphics and Visualization BCG402

case 4:
exitProgram();
break;
}
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Polygon Animation");

glutCreateMenu(menu);
glutAddMenuEntry("Translate", 1);
glutAddMenuEntry("Rotate", 2);
glutAddMenuEntry("Scale", 3);
glutAddMenuEntry("Exit", 4);
glutAttachMenu(GLUT_RIGHT_BUTTON);

init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Sample Output:

8.Develop a OpenGL program to draw a simple shaded scene


consisting of a tea pot on a table. Define suitably the position and
properties of the light source along with the properties of the
surfaces of the solid object used in the scene.

#include <GL/glut.h>

void init() {
GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};
GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};

Dept. Of CSD, PESITM, Shivamogga 17


Computer Graphics and Visualization BCG402

GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};


GLfloat position[] = {0.0, 5.0, 0.0, 1.0};

glClearColor(0.0, 0.0, 0.0, 1.0);


glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);


glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_POSITION, position);

glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
}

void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 6.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

// Draw table
glColor3f(0.4, 0.2, 0.0);
glPushMatrix();
glTranslatef(0.0, -1.0, 0.0);
glScalef(5.0, 0.1, 3.0);
glutSolidCube(1.0);
glPopMatrix();

// Draw teapot
glColor3f(0.8, 0.8, 0.8);
glPushMatrix();
glTranslatef(0.0, 0.5, 0.0);
glutSolidTeapot(1.0);
glPopMatrix();

glFlush();
}

void reshape(int w, int h) {


glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat) w / (GLfloat) h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}

Dept. Of CSD, PESITM, Shivamogga 18


Computer Graphics and Visualization BCG402

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Shaded Scene: Teapot on Table");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
init();
glutMainLoop();
return 0;
}

Sample Output:

9. Develop a OpenGL program to draw a simple scene containing


few 3D objects and provide day and night effect. Define suitably
the position and properties of the light source used in the scene.

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

// Variables to control day and night effect


int isDay = 1; // 1 for day, 0 for night

// Function to initialize OpenGL settings


void init() {
// Set clear color to sky blue for day and dark blue for night
if (isDay) {
glClearColor(0.529, 0.808, 0.922, 1.0); // Sky blue
} else {
glClearColor(0.0, 0.0, 0.4, 1.0); // Dark blue
}

glEnable(GL_DEPTH_TEST); // Enable depth testing


glEnable(GL_LIGHTING); // Enable lighting
glEnable(GL_LIGHT0); // Enable light source

Dept. Of CSD, PESITM, Shivamogga 19


Computer Graphics and Visualization BCG402

// Define light position and properties


GLfloat light_position[] = {0.0, 5.0, 0.0, 1.0}; // Above the scene
GLfloat ambient[] = {0.2, 0.2, 0.2, 1.0};
GLfloat diffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat specular[] = {1.0, 1.0, 1.0, 1.0};

glLightfv(GL_LIGHT0, GL_POSITION, light_position);


glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);

// Enable color tracking


glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
}

// Function to draw the scene


void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 5.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

// Draw ground
glColor3f(0.0, 0.6, 0.0); // Green color for grass
glBegin(GL_QUADS);
glVertex3f(-5.0, 0.0, -5.0);
glVertex3f(-5.0, 0.0, 5.0);
glVertex3f(5.0, 0.0, 5.0);
glVertex3f(5.0, 0.0, -5.0);
glEnd();

// Draw sun/moon
glColor3f(1.0, 1.0, 0.0); // Yellow color for sun
glPushMatrix();
glTranslatef(0.0, 5.0, 0.0);
glutSolidSphere(1.0, 20, 20);
glPopMatrix();

// Draw a tree
glColor3f(0.8, 0.4, 0.0); // Brown color for tree trunk
glPushMatrix();
glTranslatef(-3.0, 0.0, -2.0);
glScalef(0.2, 1.5, 0.2);
glutSolidCube(1.0); // Tree trunk
glPopMatrix();

glColor3f(0.0, 0.6, 0.0); // Green color for tree leaves


glPushMatrix();

Dept. Of CSD, PESITM, Shivamogga 20


Computer Graphics and Visualization BCG402

glTranslatef(-3.0, 2.0, -2.0);


glutSolidSphere(1.0, 20, 20); // Tree leaves
glPopMatrix();

glFlush();
}

// Function to handle window resizing


void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat) w / (GLfloat) h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}

// Function to switch between day and night mode


void switchDayNight() {
isDay = !isDay; // Toggle day and night
if (isDay) {
glClearColor(0.529, 0.808, 0.922, 1.0); // Sky blue
} else {
glClearColor(0.0, 0.0, 0.4, 1.0); // Dark blue
}
glutPostRedisplay(); // Redraw the scene
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Day and Night Scene");

glutDisplayFunc(display);
glutReshapeFunc(reshape);

// Add a menu to switch between day and night mode


glutCreateMenu(switchDayNight);
glutAddMenuEntry("Toggle Day/Night", 0);
glutAttachMenu(GLUT_RIGHT_BUTTON);

init();

glutMainLoop();

return 0;
}

Dept. Of CSD, PESITM, Shivamogga 21


Computer Graphics and Visualization BCG402

Sample Output:

Dept. Of CSD, PESITM, Shivamogga 22

You might also like