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

CG IA2 Programs WithCode

Ch

Uploaded by

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

CG IA2 Programs WithCode

Ch

Uploaded by

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

1.

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 };

static GLfloat theta[] = { 0.0,0.0,0.0 };


static GLint axis = 2;
static GLdouble viewer[] = { 0.0, 0.0, 5.0 };
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(viewer[0], viewer[1], viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, list);
glFlush();
}
void mouse(int btn, int state, int x, int y)
{
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;
if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
theta[axis] += 2.0;
if (theta[axis] > 360.0) theta[axis] -= 360.0;
display();
}
void keys(unsigned char key, int x, int y)
{
if (key == 'x') viewer[0] -= 1.0;
if (key == 'X') viewer[0] += 1.0;
if (key == 'y') viewer[1] -= 1.0;
if (key == 'Y') viewer[1] += 1.0;
if (key == 'z') viewer[2] -= 1.0;
if (key == 'Z') viewer[2] += 1.0;
display();
}
void main()
{
glutInitWindowSize(700, 700);
glutCreateWindow("Colorcube Viewer");
glMatrixMode(GL_PROJECTION);
glFrustum(-2.0, 2.0, -2.0, 2.0, 2.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glutDisplayFunc(display);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, v);
glColorPointer(3, GL_FLOAT, 0, c);
glutMouseFunc(mouse);
glutKeyboardFunc(keys);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
2. Develop a program to show the different solid quadratic surface

#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;
}

void CohenSutherLand(double x0, double y0, double x1, double y1)


{
int outcode0, outcode1, outcodeOut;
int accept = 0, done = 0;
outcode0 = ComputeOutCode(x0, y0);
outcode1 = ComputeOutCode(x1, y1);
do {
if (!(outcode0 | outcode1)) {
accept = 1;
done = 1;
}
else if (outcode0 & outcode1)
done = 1;
else {
double x, y;
outcodeOut = outcode0 ? outcode0 : outcode1;
if (outcodeOut & TOP) {
x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
y = ymax;
}
else if (outcodeOut & BOTTOM) {
x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
y = ymin;
}
else if (outcodeOut & RIGHT) {
y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
x = xmax;
}
else {
y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
x = xmin;
}
if (outcodeOut == outcode0)
{
x0 = x;
y0 = y;
outcode0 = ComputeOutCode(x0, y0);
}
else
{
x1 = x;
y1 = y;
outcode1 = ComputeOutCode(x1, y1);
}
}
} while (!done);

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>

GLfloat angle = 0.0;

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);

// Front face (red)


glColor3f(1.0, 0.0, 0.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);

// Back face (green)


glColor3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);

// Top face (blue)


glColor3f(0.0, 0.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, 1.0, -1.0);

// Bottom face (yellow)


glColor3f(1.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(1.0, -1.0, 1.0);
glVertex3f(-1.0, -1.0, 1.0);
// Right face (cyan)
glColor3f(0.0, 1.0, 1.0);
glVertex3f(1.0, -1.0, -1.0);
glVertex3f(1.0, 1.0, -1.0);
glVertex3f(1.0, 1.0, 1.0);
glVertex3f(1.0, -1.0, 1.0);

// Left face (magenta)


glColor3f(1.0, 0.0, 1.0);
glVertex3f(-1.0, -1.0, -1.0);
glVertex3f(-1.0, -1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, -1.0);
glEnd();
glutSwapBuffers();
}

void spinCube() {
angle += 0.1;
if (angle > 360.0)
angle -= 360.0;
glutPostRedisplay();
}

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

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("Color Cube");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(spinCube);
init();
glutMainLoop();
return 0;
}

(No Output was pasted)


6. Design the program to display cubes , without using push, pop and glLoadIdentity.

#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

// Draw the first cube


glPushMatrix();
glTranslatef(-1.5, 0.0, 0.0);
glutSolidCube(1.0);
glPopMatrix();

// Draw the second cube


glPushMatrix();
glTranslatef(1.5, 0.0, 0.0);
glutSolidCube(1.0);
glPopMatrix();

glFlush();
}

void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
}

void reshape(int w, int h) {


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

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Cubes without Push, Pop, and LoadIdentity");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
(No output was pasted)
7. Design a opengl program to display a sphere with keyboard operation.

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

/* Called when a key is pressed */


void mykeyboard(unsigned char key, int x, int y) {
if (key == '2') // Check for the Escape key (ASCII value 27)
exit(0);
else
printf("You pressed %c\n", key);
}

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


glutInit(&argc, argv);
glutCreateWindow("Keyboard event Demo");
glutDisplayFunc(display);
glutKeyboardFunc(mykeyboard);
glutMainLoop();
return 0;
}
//Enter 2 to exit
8. Design a program to drag a square with the movement of mouse.
#include <windows.h>
#include <GL/glut.h>
int x = 0, y = 0;
float size = 20;
void drawSquare(int x, int y)
{
glColor3f(1, 1, 0);
glBegin(GL_POLYGON);
glVertex2f(x + size, y + size);
glVertex2f(x - size, y + size);
glVertex2f(x - size, y - size);
glVertex2f(x + size, y - size);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
drawSquare(x, y);
glutSwapBuffers();
}
void motion(int mx, int my)
{
x = mx;
y = glutGet(GLUT_WINDOW_HEIGHT) - my;
glutPostRedisplay();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(0, 0);
glutCreateWindow("Moving square");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutReshapeFunc(reshape);
reshape(400, 400);
glutMainLoop();
return 0;
}
9. Implement a program to display Cone and Cylinder

#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();

// Set the viewport for the right half of the window


glViewport(glutGet(GLUT_WINDOW_WIDTH) / 2, 0,
glutGet(GLUT_WINDOW_WIDTH) / 2, glutGet(GLUT_WINDOW_HEIGHT));

drawTriangle();

glFlush();
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 400);
glutCreateWindow("Split Screen Triangle");
glClearColor(1.0, 1.0, 1.0, 1.0);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
(No output was pasted)
12. Implement Sutherland-hodgman polygon clipping.

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

float x_min = 50, y_min = 50, x_max = 300, y_max = 300;

typedef struct {
float x, y;
} Point;

void drawPolygon(Point polygon[], int numVertices, float r, float g, float b) {


glColor3f(r, g, b);
glBegin(GL_POLYGON);
for (int i = 0; i < numVertices; i++) {
glVertex2f(polygon[i].x, polygon[i].y);
}
glEnd();
}

Point computeIntersection(Point p1, Point p2, float clipEdge, char edge) {


Point intersection;

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;
}

void sutherlandHodgman(Point subjectPolygon[], int numVertices) {


Point clippedPolygon[10];

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];

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');
}
}
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');
}
}

drawPolygon(clippedPolygon, outIndex, 0.0, 0.0, 1.0);


}

void display() {
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0, 0.0, 0.0);


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();
Point subjectPolygon[] = { {100, 50}, {100, 50}, {300, 100}, {300, 200},{300,100}, {150,
200} };
int numVertices = sizeof(subjectPolygon) / sizeof(subjectPolygon[0]);
drawPolygon(subjectPolygon, numVertices, 1.0, 0.0, 0.0);

sutherlandHodgman(subjectPolygon, numVertices);

glFlush();
}

void init() {
glClearColor(1.0, 1.0, 1.0, 1.0);
gluOrtho2D(0, 400, 0, 400);
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("12_Sutherland-Hodgman-Clipping");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

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);

glColor3f(1.0, 1.0, 1.0);


glutWireCube(1.0); // Example: Display a wireframe cube

glutSwapBuffers();
}

void reshape(int width, int height) {


glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)width / (float)height, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y) {


if (key == 27) {
exit(0);
}
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("Wireframe Polyhedron");

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>

GLsizei winWidth = 500, winHeight = 500; // Initial display-window

size. void init(void)

glClearColor(1.0, 1.0, 1.0, 0.0); // Set display-window color.

void wireQuadSurfs(void)

glClear(GL_COLOR_BUFFER_BIT); // Clear display window.

glColor3f(0.0, 0.0, 1.0); // Set line-color to blue.

/* Set viewing parameters with world z axis as view-up direction. */

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

/* Position and display GLUT wire-frame sphere.

*/ glPushMatrix();

glTranslatef(1.0, 1.0, 0.0);

glutWireSphere(0.75, 8, 6);

glPopMatrix();

/* Position and display GLUT wire-frame cone.

*/ glPushMatrix();

glTranslatef(1.0, -0.5, 0.5);

glutWireCone(0.7, 2.0, 7, 6);

glPopMatrix();

/* Position and display GLU wire-frame cylinder. */


GLUquadricObj* cylinder; // Set name for GLU quadric object.

glPushMatrix();

glTranslatef(0.0, 1.2, 0.8);

cylinder =

gluNewQuadric();

gluQuadricDrawStyle(cylinder, GLU_LINE);

gluCylinder(cylinder, 0.6, 0.6, 1.5, 6, 4);

glPopMatrix();

glFlush();

void winReshapeFcn(GLint newWidth, GLint newHeight)

glViewport(0, 0, newWidth,

newHeight);

glMatrixMode(GL_PROJECTION

); glOrtho(-2.0, 2.0, -2.0, 2.0, 0.0,

5.0);

glMatrixMode(GL_MODELVIEW);

glClear(GL_COLOR_BUFFER_BIT);

void main(int argc, char** argv)

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE |

GLUT_RGB); glutInitWindowPosition(100,

100); glutInitWindowSize(winWidth,
winHeight); glutCreateWindow("Wire-Frame

Quadric Surfaces"); init();

glutDisplayFunc(wireQuadSurfs);

glutReshapeFunc(winReshapeFcn);

glutMainLoop();

Output:
15. Implement a program to display squares using “display list”

#include <GL/glut.h>

GLuint squareList; // Variable to store the display list ID

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
}

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


glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set display mode
glutInitWindowSize(500, 500); // Set window size
glutInitWindowPosition(100, 100); // Set window position
glutCreateWindow("Display List Example"); // Create window with given title
init(); // Initialize OpenGL settings
glutDisplayFunc(display); // Set display callback function
glutMainLoop(); // Enter main event loop
return 0;
}

You might also like