CG Lab Mannual
CG Lab Mannual
Laboratory Manual
Semester: IV
Compiled By:
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
plot(x, y);
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glFlush();
}
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
}
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;
}
Sample Output:
#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();
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
}
init();
glutDisplayFunc(display);
glutTimerFunc(0, timer, 0); // Start the timer
glutMainLoop();
return 0;
}
Sample Output:
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
// Draw a triangle
void triangle(point3 a, point3 b, point3 c) {
glBegin(GL_TRIANGLES);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}
// 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);
}
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:
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
// Draw a triangle
void triangle(point3 a, point3 b, point3 c) {
glBegin(GL_TRIANGLES);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}
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();
gluPerspective(60.0, 1.0, 1.0, 20.0); // Set up perspective projection
glMatrixMode(GL_MODELVIEW);
}
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:
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
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;
}
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) {
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
}
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow("Cohen-Sutherland Line Clipping");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Sample Output:
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
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();
// 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);
}
case 4:
exitProgram();
break;
}
}
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:
#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};
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();
}
Sample Output:
#include <GL/glut.h>
#include <stdio.h>
// 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();
glFlush();
}
glutDisplayFunc(display);
glutReshapeFunc(reshape);
init();
glutMainLoop();
return 0;
}
Sample Output: