0% found this document useful (0 votes)
12 views22 pages

CG PGM For Students

The document contains five different OpenGL programs demonstrating various graphics techniques. Program 1 implements Bresenham's line drawing algorithm, while Program 2 creates a Sierpinski triangle through recursive subdivision. Programs 3, 4, and 5 showcase 3D tetrahedron rendering, 2D transformations of a triangle, and a rotating cube with user-controlled transformations, respectively.

Uploaded by

Akash Chorotiya
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 views22 pages

CG PGM For Students

The document contains five different OpenGL programs demonstrating various graphics techniques. Program 1 implements Bresenham's line drawing algorithm, while Program 2 creates a Sierpinski triangle through recursive subdivision. Programs 3, 4, and 5 showcase 3D tetrahedron rendering, 2D transformations of a triangle, and a rotating cube with user-controlled transformations, respectively.

Uploaded by

Akash Chorotiya
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/ 22

PROGRAM 1

Develop a program to draw a line using Bresenham’s line drawing technique

#include<GL/glut.h>

#include<stdio.h>

int x1, y1, x2, y2;

void draw_pixel(int x, int y)

glColor3f(1.0,0.0,0.0);

glBegin(GL_POINTS);

glVertex2i(x, y);

glEnd();

void brenhams_line_draw(int x1, int y1, int x2, int y2)

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

gluOrtho2D(0.0, 500.0, 0.0, 500.0);

glMatrixMode(GL_MODELVIEW);

void display()

glClear(GL_COLOR_BUFFER_BIT);

brenhams_line_draw(x1, y1, x2, y2);

glFlush();

void main(int argc, char **argv)

printf("Enter Start Points (x1,y1)\n");

scanf("%d%d", &x1,&y1);

printf("Enter End Points (x2,y2)\n");

scanf("%d%d", &x2,&y2);

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

glutInitWindowSize(500, 500);

glutInitWindowPosition(0, 0);

glutCreateWindow("Bresenham's Line Drawing");

myInit();

glutDisplayFunc(display);

glutMainLoop();

PROGRAM 2

#include <stdio.h>
#include <GL/glut.h>
typedef float point2[2];

/* initial triangle */

point2 v[]={{-1.0, -0.58}, {1.0, -0.58}, {0.0, 1.15}};


int n;

/* display one triangle */


void triangle( point2 a, point2 b, point2 c)
{
glBegin(GL_TRIANGLES);
glVertex2fv(a);
glVertex2fv(b);
glVertex2fv(c);
glEnd();
}
void divide_triangle(point2 a, point2 b, point2 c, int m)
{
/* triangle subdivision using vertex numbers */

point2 v0, v1, v2;


int j;
if(m>0)
{
for(j=0; j<2; j++) v0[j]=(a[j]+b[j])/2;
for(j=0; j<2; j++) v1[j]=(a[j]+c[j])/2;
for(j=0; j<2; j++) v2[j]=(b[j]+c[j])/2;
divide_triangle(a, v0, v1, m-1);
divide_triangle(c, v1, v2, m-1);
divide_triangle(b, v2, v0, m-1);
}
else(triangle(a,b,c)); /* draw triangle at end of recursion */
}
void display(void)
{

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

void main(int argc, char **argv)


{
printf(" No. of Subdivisions : ");
scanf("%d",&n);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(500, 500);
glutCreateWindow("Sierpinski Gasket 2D triangle");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}

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

void myReshape(int w, int h)


{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
else
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}

void main(int argc, char **argv)


{
int i = 0;
printf("Enter value of N:");
scanf("%d", &i);
n = i;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("3D tetrahedron Gasket");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glClearColor (1.0, 1.0, 1.0, 1.0);
glutMainLoop();
}

PROGRAM 4

#include <GL/glut.h>

#include <math.h>

// Define triangle vertices

GLfloat vertices[][2] = {

{0, 1},

{-0.5, -0.5},

{0.5, -0.5}

};

// Define rotation angle

GLfloat angle = 0.0;

// Define translation offsets

GLfloat translateX = 0.0;

GLfloat translateY = 0.0;


// Define scaling factors

GLfloat scaleX = 1.0;

GLfloat scaleY = 1.0;

// Function declarations

void display();

void menu(int option);

void createMenu();

// Display function

void display() {

glClear(GL_COLOR_BUFFER_BIT);

glLoadIdentity();

// Apply transformations

glTranslatef(translateX, translateY, 0);

glRotatef(angle, 0, 0, 1);

glScalef(scaleX, scaleY, 1);

// Draw triangle
glColor3f(1, 1, 1);

glBegin(GL_TRIANGLES);

for (int i = 0; i < 3; i++) {

glVertex2fv(vertices[i]);

glEnd();

glFlush();

// Function to handle menu selection

void menu(int option) {

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;

if (angle > 360) angle -= 360;

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

// Function to create menu

void createMenu() {

glutCreateMenu(menu);

glutAddMenuEntry("Translate Left", 1);

glutAddMenuEntry("Translate Right", 2);


glutAddMenuEntry("Translate Down", 3);

glutAddMenuEntry("Translate Up", 4);

glutAddMenuEntry("Scale Up", 5);

glutAddMenuEntry("Scale Down", 6);

glutAddMenuEntry("Rotate Clockwise", 7);

glutAddMenuEntry("Rotate Anti-clockwise", 8);

glutAddMenuEntry("Reset", 9);

glutAddMenuEntry("Exit", 10);

glutAttachMenu(GLUT_RIGHT_BUTTON);

// Initialization function

void init() {

glClearColor(0, 0, 0, 1);

// Reshape function

void reshape(int w, int h) {

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();
gluOrtho2D(-1, 1, -1, 1);

glMatrixMode(GL_MODELVIEW);

// Main function

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

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

// Define cube edges


GLint edges[][2] = {
{0, 1},
{1, 2},
{2, 3},
{3, 0},
{4, 5},
{5, 6},
{6, 7},
{7, 4},
{0, 4},
{1, 5},
{2, 6},
{3, 7}
};

// Define rotation angles


GLfloat angleX = 0.0;
GLfloat angleY = 0.0;
GLfloat angleZ = 0.0;
// Define translation offsets
GLfloat translateX = 0.0;
GLfloat translateY = 0.0;
GLfloat translateZ = 0.0;

// Define scaling factors


GLfloat scaleX = 1.0;
GLfloat scaleY = 1.0;
GLfloat scaleZ = 1.0;

// Display function
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

// Set up perspective projection


gluLookAt(3, 3, 3, 0, 0, 0, 0, 1, 0);

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

// Keyboard function for scaling and translation


void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'w':
translateY += 0.1;
break;
case 's':
translateY -= 0.1;
break;
case 'a':
translateX -= 0.1;
break;
case 'd':
translateX += 0.1;
break;
case 'q':
translateZ += 0.1;
break;
case 'e':
translateZ -= 0.1;
break;
case '+':
scaleX += 0.1;
scaleY += 0.1;
scaleZ += 0.1;
break;
case '-':
scaleX -= 0.1;
scaleY -= 0.1;
scaleZ -= 0.1;
break;
case 'r':
angleX = 0.0;
angleY = 0.0;
angleZ = 0.0;
translateX = 0.0;
translateY = 0.0;
translateZ = 0.0;
scaleX = 1.0;
scaleY = 1.0;
scaleZ = 1.0;
break;
case 27: // ESC key
exit(0);
break;
}
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>

int windowWidth = 800;


int windowHeight = 600;
int squareSize = 50;
int squarePosX = 0;
float animationSpeed = 1.0;

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

void update(int value) {


// Move the square to the right
squarePosX += animationSpeed;

// If the square moves out of the screen, reset its position


if (squarePosX > windowWidth) {
squarePosX = -squareSize; // Start from the left edge again
}

// Varying animation speed


animationSpeed += 0.01; // Increase animation speed linearly

glutPostRedisplay(); // Update the display


glutTimerFunc(1000 / 60, update, 0); // 60 frames per second
}

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


glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition(100, 100);
glutCreateWindow("Animation Effects with Varying Speed");
init();
glutDisplayFunc(display);
glutTimerFunc(0, update, 0);
glutMainLoop();
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy