CG IA2 Programs WithCode
CG IA2 Programs WithCode
Draw a color cube and all the user to move the camera suitably to experiment with the
perspective viewing
#include<stdio.h>
#include <GL/glut.h>
#include<math.h>
#include<stdio.h>
float v[] = { -1,-1,-1, -1,1,-1, 1,1,-1, 1,-1,-1, -1,-1,1, -1,1,1, 1,1,1, 1,-1,1 };
float c[] = { 0,0,0, 1,0,0, 1,1,0, 0,1,0, 0,0,1, 1,0,1, 1,1,1, 0,1,1, };
GLubyte list[] = { 0,1,2,3, 2,3,7,6, 4,5,6,7, 4,5,1,0, 5,6,2,1, 0,3,7,4 };
#include <GL/glut.h>
#include <stdio.h>
void init(void) {
GLfloat light_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.2 };
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(20.0, 1.0, 0.0, 0.0);
//CONE
glPushMatrix();
glTranslatef(-0.7, 0.5, 0.0);
glRotatef(270.0, 1.0, 0.0, 0.0);
glutSolidCone(0.8, 2.1, 15, 15);
glPopMatrix();
//CUBE
glPushMatrix();
glTranslatef(-0.75, -0.5, 0.0);
glRotatef(85, 1.0, 0.0, 0.0);
glRotatef(-20, 0.0, 0.0, 1.0);
glutSolidCube(1.7);
glPopMatrix();
//SPHERE
glPushMatrix();
glTranslatef(0.5, 0.0, 3.0);
glutSolidSphere(1.0, 30, 30);
glPopMatrix();
//CYLINDER
GLUquadricObj* qobj;
qobj = gluNewQuadric();
glPushMatrix();
gluQuadricDrawStyle(qobj, GLU_FILL);
gluQuadricNormals(qobj, GLU_SMOOTH);
glTranslatef(0.7, 0.0, -4.5);
glRotatef(90, 1.0, 0.0, 0.0);
glRotatef(30, 0.0, 0.0, 1.0);
gluCylinder(qobj, 0.85, 0.85, 3.0, 6, 6);
glPopMatrix();
glFlush();
}
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.5, 2.5, -2.5 * (GLfloat)h / (GLfloat)w, 2.5 * (GLfloat)h / (GLfloat)w, -10.0,
10.0);
else
glOrtho(-2.5 * (GLfloat)w / (GLfloat)h, 2.5 * (GLfloat)w / (GLfloat)h, -2.5, 2.5, -10.0,
10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Solid objects");
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
//glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
3. To show a simple shaded scene consisting og a tea pot on a table. Define suitably the
position and properties of the light source along with the properties of the surface of
the solid object used in the scene
#include<GL/glut.h>
void obj(double tx, doublety, doubletz, doublesx, doublesy, doublesz)
{ glRotated(50,0,1,0);
glRotated(10,-1,0,0);
glRotated(11.7,0,0,-1);
glTranslated(tx,ty,tz);
glScaled(sx,sy,sz);
glutSolidCube(1);
glLoadIdentity();
}
void display()
{ glViewport(0,0,700,700);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
obj(0,0,0.5,1,1,0.04); // three walls
obj(0,-0.5,0,1,0.04,1);
obj(-0.5,0,0,0.04,1,1);
obj(0,-0.3,0,0.02,0.2,0.02); // four table legs
obj(0,-0.3,-0.4,0.02,0.2,0.02);
obj(0.4,-0.3,0,0.02,0.2,0.02);
obj(0.4,-0.3,-0.4,0.02,0.2,0.02);
obj(0.2,-0.18,-0.2,0.6,0.02,0.6); // table top
glRotated(50,0,1,0);
glRotated(10,-1,0,0);
glRotated(11.7,0,0,-1);
glTranslated(0.3,-0.1,-0.3);
glutSolidTeapot(0.09);
glFlush();
glLoadIdentity();
}
void main()
{ float ambient[]={1,1,1,1};
float light_pos[]={27,80,2,3};
glutInitWindowSize(700,700);
glutCreateWindow("scene");
glutDisplayFunc(display);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glMaterialfv(GL_FRONT,GL_AMBIENT,ambient);
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
OUTPUT
4. Clip a line using Cohen-Sutherland algorithm
#include<stdio.h>
#include<GL/glut.h>
double xmin = 50, ymin = 50, xmax = 100, ymax = 100;
double xvmin = 200, yvmin = 200, xvmax = 300, yvmax = 300;
const int RIGHT = 8;
const int LEFT = 2;
const int TOP = 4;
const int BOTTOM = 1;
int ComputeOutCode(double x, double y)
{
int code = 0;
if (y > ymax)
code |= TOP;
else if (y < ymin)
code |= BOTTOM;
if (x > xmax)
code |= RIGHT;
else if (x < xmin)
code |= LEFT;
return code;
}
if (accept) {
double sx = (xvmax - xvmin) / (xmax - xmin);
double sy = (yvmax - yvmin) / (ymax - ymin);
double vx0 = xvmin + (x0 - xmin) * sx;
double vy0 = yvmin + (y0 - ymin) * sy;
double vx1 = xvmin + (x1 - xmin) * sx;
double vy1 = yvmin + (y1 - ymin) * sy;
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin, yvmin);
glVertex2f(xvmax, yvmin);
glVertex2f(xvmax, yvmax);
glVertex2f(xvmin, yvmax);
glEnd();
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex2d(vx0, vy0);
glVertex2d(vx1, vy1);
glEnd();
}
}
void display()
{
double x0 = 60, y0 = 20, x1 = 80, y1 = 120;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINES);
glVertex2d(x0, y0);
glVertex2d(x1, y1);
glEnd();
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
CohenSutherLand(x0, y0, x1, y1);
glFlush();
}
void myinit()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 500.0, 0.0, 500.0);
glMatrixMode(GL_MODELVIEW);
}
void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("COHEN");
myinit();
glutDisplayFunc(display);
glutMainLoop();
}
5. Draw a colour cube and spin it using OpenGL transformation matrices.
#include <GL/glut.h>
#include <stdlib.h>
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
glRotatef(angle, 1.0, 1.0, 1.0);
glBegin(GL_QUADS);
void spinCube() {
angle += 0.1;
if (angle > 360.0)
angle -= 360.0;
glutPostRedisplay();
}
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0, 0.0, -5.0); // Translate to a proper viewing position
glFlush();
}
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
}
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h> // Include stdlib.h for exit()
void display(void) {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0, 0, 1);
glutSolidSphere(0.4, 16, 16);
glFlush();
}
#include <GL/glut.h>
#include <math.h>
#define PI 3.14159265
void drawCylinder(float radius, float height, int slices) {
glBegin(GL_QUAD_STRIP);
for (int i = 0; i <= slices; ++i) {
float theta = (2.0f * PI / slices) * i;
float x = radius * cos(theta);
float z = radius * sin(theta); glNormal3f(x / radius, 0.0f, z / radius);
glVertex3f(x, height / 2.0f, z);
glVertex3f(x, -height / 2.0f, z);
}
glEnd();
}
void drawCone(float radius, float height, int slices) {
glBegin(GL_TRIANGLE_FAN);
glNormal3f(0.0f, -1.0f, 0.0f);
glVertex3f(0.0f, -height / 2.0f, 0.0f);
for (int i = 0; i <= slices; ++i) {
float theta = (2.0f * PI / slices) * i;
float x = radius * cos(theta);
float z = radius * sin(theta);
glNormal3f(x / radius, radius / height, z / radius);
glVertex3f(x, height / 2.0f, z);
}
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-2.0f, 0.0f, -7.0f);
glColor3f(1.0f, 0.0f, 0.0f); // Red color for cylinder
drawCylinder(1.0f, 2.0f, 20);
glLoadIdentity();
glTranslatef(2.0f, 0.0f, -7.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue color for cone
drawCone(1.0f, 2.0f, 20);
glutSwapBuffers();
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat)w / (GLfloat)h, 0.1f, 180.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Cylinder and Cone");
glEnable(GL_DEPTH_TEST);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
OUTPUT:
10. Implement a program to spin torus and teapot using three dimensional
transformations.
#include <GL/glut.h>
GLfloat angle_torus = 0.0; // Angle for torus rotation
GLfloat angle_teapot = 0.0; // Angle for teapot rotation
void init(void) {
glClearColor(1.0, 1.0, 1.0, 1.0); // Set background color to white
glEnable(GL_DEPTH_TEST); // Enable depth testing for 3D rendering
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Set up the view matrix
gluLookAt(0.0, 0.0, 5.0, // eye position
0.0, 0.0, 0.0, // look-at position
0.0, 1.0, 0.0); // up direction
// Draw torus
glPushMatrix();
glRotatef(angle_torus, 0.0, 1.0, 0.0); // Rotate torus around y-axis
glColor3f(1.0, 0.0, 0.0); // Red color
glutWireTorus(0.5, 1.0, 20, 20); // Draw wireframe torus
glPopMatrix();
// Draw teapot
glPushMatrix();
glRotatef(angle_teapot, 1.0, 0.0, 0.0); // Rotate teapot around x-axis
glColor3f(0.0, 0.0, 1.0); // Blue color
glutWireTeapot(1.0); // Draw wireframe teapot
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 1.0, 100.0); // Set perspective projection
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void timer(int value) {
angle_torus += 1.0; // Increment torus rotation angle
angle_teapot += 1.5; // Increment teapot rotation angle
glutPostRedisplay(); // Request redisplay
glutTimerFunc(16, timer, 0); // Set timer to call itself after 16 milliseconds (about 60
frames per second)
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Spinning Torus and Teapot");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0, timer, 0); // Start timer immediately
glutMainLoop();
return 0;
}
Output:
11. Use opengl viewport function, to split screen effect to show two views of a triangle.
#include <GL/glut.h>
void drawTriangle() {
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0); // Red color
glVertex2f(-0.5, -0.5);
glColor3f(0.0, 1.0, 0.0); // Green color
glVertex2f(0.5, -0.5);
glColor3f(0.0, 0.0, 1.0); // Blue color
glVertex2f(0.0, 0.5);
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, glutGet(GLUT_WINDOW_WIDTH) / 2,
glutGet(GLUT_WINDOW_HEIGHT));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawTriangle();
drawTriangle();
glFlush();
}
#include <GL/glut.h>
#include <math.h>
typedef struct {
float x, y;
} Point;
if (edge == 'L') {
intersection.x = x_min;
intersection.y = p1.y + (clipEdge - p1.x) * (p2.y - p1.y) / (p2.x - p1.x);
}
else if (edge == 'R') {
intersection.x = x_max;
intersection.y = p1.y + (clipEdge - p1.x) * (p2.y - p1.y) / (p2.x - p1.x);
}
else if (edge == 'B') {
intersection.x = p1.x + (clipEdge - p1.y) * (p2.x - p1.x) / (p2.y - p1.y);
intersection.y = y_min;
}
else if (edge == 'T') {
intersection.x = p1.x + (clipEdge - p1.y) * (p2.x - p1.x) / (p2.y - p1.y);
intersection.y = y_max;
}
return intersection;
}
int outIndex = 0;
for (int i = 0; i < numVertices; i++) {
int next = (i + 1) % numVertices;
if (subjectPolygon[i].x >= x_min && subjectPolygon[i].x <= x_max &&
subjectPolygon[i].y >= y_min && subjectPolygon[i].y <= y_max) {
clippedPolygon[outIndex++] = subjectPolygon[i];
clippedPolygon[outIndex++] = computeIntersection(subjectPolygon[i],
subjectPolygon[next], x_min, 'L');
clippedPolygon[outIndex++] = computeIntersection(subjectPolygon[i],
subjectPolygon[next], x_max, 'R');
clippedPolygon[outIndex++] = computeIntersection(subjectPolygon[i],
subjectPolygon[next], y_min, 'B');
clippedPolygon[outIndex++] = computeIntersection(subjectPolygon[i],
subjectPolygon[next], y_max, 'T');
}
}
else if (subjectPolygon[next].x >= x_min && subjectPolygon[next].x <= x_max &&
subjectPolygon[next].y >= y_min && subjectPolygon[next].y <= y_max) {
clippedPolygon[outIndex++] = computeIntersection(subjectPolygon[i],
subjectPolygon[next], x_min, 'L');
clippedPolygon[outIndex++] = computeIntersection(subjectPolygon[i],
subjectPolygon[next], x_max, 'R');
clippedPolygon[outIndex++] = computeIntersection(subjectPolygon[i],
subjectPolygon[next], y_min, 'B');
clippedPolygon[outIndex++] = computeIntersection(subjectPolygon[i],
subjectPolygon[next], y_max, 'T');
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
sutherlandHodgman(subjectPolygon, numVertices);
glFlush();
}
void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(0, 400, 0, 400);
}
Output: 2 examples
13. Implement wireframe prespective display of polyhedran
#include <GL/glut.h>
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(5, 5, 5, 0, 0, 0, 0, 1, 0);
glutSwapBuffers();
}
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
14. Implement wireframe model of 3 quadratic surface objects (sphere, cone and cylinder)
at different positions, set view up direction as positive z axis and also in single display
window
#include <GL/glut.h>
void wireQuadSurfs(void)
*/ glPushMatrix();
glutWireSphere(0.75, 8, 6);
glPopMatrix();
*/ glPushMatrix();
glPopMatrix();
glPushMatrix();
cylinder =
gluNewQuadric();
gluQuadricDrawStyle(cylinder, GLU_LINE);
glPopMatrix();
glFlush();
glViewport(0, 0, newWidth,
newHeight);
glMatrixMode(GL_PROJECTION
5.0);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE |
GLUT_RGB); glutInitWindowPosition(100,
100); glutInitWindowSize(winWidth,
winHeight); glutCreateWindow("Wire-Frame
glutDisplayFunc(wireQuadSurfs);
glutReshapeFunc(winReshapeFcn);
glutMainLoop();
Output:
15. Implement a program to display squares using “display list”
#include <GL/glut.h>
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0); // Set clear color to white
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-250.0, 250.0, -250.0, 250.0); // Set the orthographic projection
squareList = glGenLists(1); // Generate one display list
glNewList(squareList, GL_COMPILE); // Start compiling the display list
glBegin(GL_QUADS); // Draw a square
glVertex2f(-50.0, -50.0);
glVertex2f(50.0, -50.0);
glVertex2f(50.0, 50.0);
glVertex2f(-50.0, 50.0);
glEnd();
glEndList(); // End compiling the display list
}
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
glColor3f(0.0, 0.0, 0.0); // Set drawing color to black
glCallList(squareList); // Call the display list to draw the square
glFlush(); // Flush OpenGL buffer
}