CG PGM For Students
CG PGM For Students
#include<GL/glut.h>
#include<stdio.h>
glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
int dx=x2-x1,dy=y2-y1;
int p=2*dy*dx;
int twoDy=2*dy;
int twoDyMinusDx=2*(dy-dx);
int x=x1,y=y1;
if(dx<0)
{
x=x2;
y=y2;
x2=x1;
draw_pixel(x, y);
while(x<x2)
x++;
if(p<0)
p+=twoDy;
else
y++;
p+=twoDyMinusDx;
draw_pixel(x, y);
void myInit()
{
glClearColor(0.0,0.0,0.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
void display()
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
scanf("%d%d", &x1,&y1);
scanf("%d%d", &x2,&y2);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
myInit();
glutDisplayFunc(display);
glutMainLoop();
PROGRAM 2
#include <stdio.h>
#include <GL/glut.h>
typedef float point2[2];
/* initial triangle */
glClear(GL_COLOR_BUFFER_BIT);
divide_triangle(v[0], v[1], v[2], n);
glFlush();
}
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-2.0, 2.0, -2.0, 2.0);
glMatrixMode(GL_MODELVIEW);
glClearColor (1.0, 1.0, 1.0, 1.0);
glColor3f(0.0,0.0,0.0);
}
PROGRAM 3
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
typedef float point[3];
/* initial tetrahedron */
point v[]={{0.0, 0.0, 1.0}, {0.0, 0.942809, -0.33333},
{-0.816497, -0.471405, -0.333333}, {0.816497, -0.471405, -0.333333}};
static GLfloat theta[] = {0.0,0.0,0.0};
int n;
void triangle( point a, point b, point c)
/* display one triangle using a line loop for wire frame, a single
normal for constant shading, or three normals for interpolative shading */
{
glBegin(GL_POLYGON);
glNormal3fv(a);
glVertex3fv(a);
glVertex3fv(b);
glVertex3fv(c);
glEnd();
}
/* triangle subdivision using vertex numbers
righthand rule applied to create outward pointing faces */
void divide_triangle(point a, point b, point c, int m)
{
point v1, v2, v3;
int j;
if(m>0)
{
for(j=0; j<3; j++) v1[j]=(a[j]+b[j])/2;
for(j=0; j<3; j++) v2[j]=(a[j]+c[j])/2;
for(j=0; j<3; j++) v3[j]=(b[j]+c[j])/2;
divide_triangle(a, v1, v2, m-1);
divide_triangle(c, v2, v3, m-1);
divide_triangle(b, v3, v1, m-1);
}
else(triangle(a,b,c)); /* draw triangle at end of recursion */
}
/* Apply triangle subdivision to faces of tetrahedron */
void tetrahedron( int m)
{
glColor3f(1.0,0.0,0.0);
divide_triangle(v[0], v[1], v[2], m);
glColor3f(0.0,1.0,0.0);
divide_triangle(v[3], v[2], v[1], m);
glColor3f(0.0,0.0,1.0);
divide_triangle(v[0], v[3], v[1], m);
glColor3f(0.0,0.0,0.0);
divide_triangle(v[0], v[2], v[3], m);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
tetrahedron(3);
glFlush();
}
PROGRAM 4
#include <GL/glut.h>
#include <math.h>
GLfloat vertices[][2] = {
{0, 1},
{-0.5, -0.5},
{0.5, -0.5}
};
// Function declarations
void display();
void createMenu();
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// Apply transformations
glRotatef(angle, 0, 0, 1);
// Draw triangle
glColor3f(1, 1, 1);
glBegin(GL_TRIANGLES);
glVertex2fv(vertices[i]);
glEnd();
glFlush();
switch (option) {
case 1:
translateX -= 0.1;
break;
case 2:
translateX += 0.1;
break;
case 3:
translateY -= 0.1;
break;
case 4:
translateY += 0.1;
break;
case 5:
scaleX += 0.1;
scaleY += 0.1;
break;
case 6:
scaleX -= 0.1;
scaleY -= 0.1;
break;
case 7:
angle += 10.0;
break;
case 8:
angle -= 10.0;
if (angle < 0) angle += 360;
break;
case 9:
angle = 0.0;
translateX = 0.0;
translateY = 0.0;
scaleX = 1.0;
scaleY = 1.0;
break;
case 10:
exit(0);
break;
glutPostRedisplay();
void createMenu() {
glutCreateMenu(menu);
glutAddMenuEntry("Reset", 9);
glutAddMenuEntry("Exit", 10);
glutAttachMenu(GLUT_RIGHT_BUTTON);
// Initialization function
void init() {
glClearColor(0, 0, 0, 1);
// Reshape function
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1, 1, -1, 1);
glMatrixMode(GL_MODELVIEW);
// Main function
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("2D Transformations");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
createMenu();
init();
glutMainLoop();
return 0;
PROGRAM 5
#include <stdlib.h>
#include <GL/glut.h>
// Define cube vertices
GLfloat vertices[][3] = {
{-1, -1, -1},
{1, -1, -1},
{1, 1, -1},
{-1, 1, -1},
{-1, -1, 1},
{1, -1, 1},
{1, 1, 1},
{-1, 1, 1}
};
// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Apply transformations
glTranslatef(translateX, translateY, translateZ);
glRotatef(angleX, 1, 0, 0);
glRotatef(angleY, 0, 1, 0);
glRotatef(angleZ, 0, 0, 1);
glScalef(scaleX, scaleY, scaleZ);
// Draw cube
glColor3f(1, 1, 1);
glBegin(GL_LINES);
for (int i = 0; i < 12; i++) {
glVertex3fv(vertices[edges[i][0]]);
glVertex3fv(vertices[edges[i][1]]);
}
glEnd();
glutSwapBuffers();
}
// Idle function
void idle() {
angleX += 0.5;
if (angleX > 360) angleX -= 360;
angleY += 0.5;
if (angleY > 360) angleY -= 360;
angleZ += 0.5;
if (angleZ > 360) angleZ -= 360;
glutPostRedisplay();
}
// Initialization function
void init() {
glClearColor(0, 0, 0, 1);
glEnable(GL_DEPTH_TEST);
}
// Reshape function
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (float)w / h, 1, 100);
glMatrixMode(GL_MODELVIEW);
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("3D Transformations");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutKeyboardFunc(keyboard);
init();
glutMainLoop();
return 0;
}
PROGRAM 6
#include <GL/glut.h>
#include <math.h>
void init() {
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, windowWidth, 0.0, windowHeight);
}
void drawSquare() {
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
glVertex2i(squarePosX, windowHeight / 2);
glVertex2i(squarePosX + squareSize, windowHeight / 2);
glVertex2i(squarePosX + squareSize, windowHeight / 2 + squareSize);
glVertex2i(squarePosX, windowHeight / 2 + squareSize);
glEnd();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
drawSquare();
glutSwapBuffers();
}