0% found this document useful (0 votes)
66 views21 pages

101403023

The document contains C++ code for OpenGL programs that draw various basic 2D shapes and lines. There are programs to: 1) Create an empty window in different colors 2) Draw a green point at coordinates (50,40) 3) Draw a green line from (10,10) to (50,50) 4) Draw a triangle on a black background 5) Draw a rectangle on a black background 6) Draw a line using the DDA algorithm 7) Draw a line using Bresenham's line algorithm 8) Draw a circle using Bresenham's midpoint circle algorithm

Uploaded by

Akash Gupta
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)
66 views21 pages

101403023

The document contains C++ code for OpenGL programs that draw various basic 2D shapes and lines. There are programs to: 1) Create an empty window in different colors 2) Draw a green point at coordinates (50,40) 3) Draw a green line from (10,10) to (50,50) 4) Draw a triangle on a black background 5) Draw a rectangle on a black background 6) Draw a line using the DDA algorithm 7) Draw a line using Bresenham's line algorithm 8) Draw a circle using Bresenham's midpoint circle algorithm

Uploaded by

Akash Gupta
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/ 21

GRAPHICS AND VISUAL

COMPUTING
ASSIGNMENT-1

SUBMITTED TO

SUBMITTED BY

Dr. Raman Singh

Akash GUPTA

Assistant Professor

101403023
COE-1

Write a program to create empty window (Black, White


and different Colors)
#include<GL/glut.h>
void draw(void)
{
glClearColor(1,1,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50,25);
glutInitWindowSize(500,250);
glutCreateWindow("Green window");
glutDisplayFunc(draw);
glutMainLoop();
return 0;
}

Write a program to draw a point of width 10


pixel.
#include<GL/glut.h>
void Displaydot ( void ) {
glClear ( GL_COLOR_BUFFER_BIT ); //clear pixel buffer
glBegin(GL_POINTS); // render with points
glVertex2i(50,40); //display a point
glEnd();
glFlush();}
void MyInit ( void ) {
glClearColor ( 1.0, 1.0, 1.0, 0.0 ); //white background
glColor3f(0.0f, 1.0f,0.0f); // green drawing colour
glPointSize(10.0); // 10 pixel dot!
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
gluOrtho2D ( 0.0, (GLdouble)100, 0.0, (GLdouble)100 ); //Display area
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50,25);
glutInitWindowSize(500,250);
glutCreateWindow("Green window");
MyInit ( );
glutDisplayFunc ( Displaydot );
glutMainLoop();
return 0;}

Write a program to draw a green color line from (10,10)


to (50,50)
#include<GL/glut.h>
void DisplayLine ( void ) {
glClear ( GL_COLOR_BUFFER_BIT ); //clear pixel buffer
glBegin(GL_LINES);
glVertex2f(10, 10);
glVertex2f(20, 20);
glEnd();
glFlush();
}
void MyInit ( void ) {

glClearColor ( 1.0, 1.0, 1.0, 0.0 ); //white background


glColor3f(0.0f, 1.0f,0.0f); // green drawing colour
glPointSize(10.0); // 10 pixel dot!
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
gluOrtho2D ( 0.0, (GLdouble)100, 0.0, (GLdouble)100 ); //Display area
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50,25);
glutInitWindowSize(500,250);

glutCreateWindow("New Green Line");


MyInit ( );
glutDisplayFunc ( DisplayLine );
glutMainLoop();
return 0;
}

Write a program to draw a triangle on black


background.
#include <GL/gl.h>
#include <GL/glut.h>
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f( 0.25f, 0.0f, 0.0f); // lower left vertex
glVertex3f( 0.75f, 0.0f, 0.0f); // lower right vertex
glVertex3f( 0.5f, 0.75f, 0.0f); // upper vertex
glEnd();
glFlush ();
}
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (350, 350);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Write a program to draw a rectangle on black


background
#include <GL/gl.h>
#include <GL/glut.h>
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();
glFlush ();
}
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Write a program to draw a line using DDA algorithm.


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

double X1, Y1, X2, Y2;


float round_value(float v)
{
return floor(v + 0.5);
}
void LineDDA(void)
{
double dx=(X2-X1);
double dy=(Y2-Y1);
double steps;
float xInc,yInc,x=X1,y=Y1;
steps=(abs(dx))>(abs(dy))?(abs(dx)):(abs(dy));
xInc=dx/(float)steps;
yInc=dy/(float)steps;

glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_POINTS);
/* Plot the first point */
glVertex2d(x,y);
int k;

/* For every step, find an intermediate vertex */


for(k=0;k<steps;k++)
{
x+=xInc;
y+=yInc;
/* printf("%0.6lf %0.6lf\n",floor(x), floor(y)); */
glVertex2d(round_value(x), round_value(y));
}
glEnd();

glFlush();
}
void Init()
{
glClearColor(1.0,1.0,1.0,0);
glColor3f(0.0,0.0,0.0);
gluOrtho2D(0 , 640 , 0 , 480);
}
int main(int argc, char **argv)
{
printf("Enter two end points \n");
printf("\nEnter Point1( X1 , Y1):\n");
scanf("%lf%lf",&X1,&Y1);
printf("\nEnter Point1( X2 , Y2):\n");
scanf("%lf%lf",&X2,&Y2);

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

glutInitWindowSize(640,480);
glutCreateWindow("DDA_Line");
Init();
glutDisplayFunc(LineDDA);
glutMainLoop();
return 0;
}

Write a program to draw a line using Bresenham's


algorithm
#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,400.0,0.0,400.0);
}
void setPixel(GLint x,GLint y)
{
//glColor3f(255,255,255);
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
}
void Line()
{
int x0=50,y0=50,xn=200,yn=400,x,y;
int dx,dy;
int pk;
int k;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,0,0);

setPixel(x0,y0);
dx=xn-x0;
dy=yn-y0;
pk=2*dy-dx;
x=x0;
y=y0;
for(k=0;k<dx-1;++k)
{
if(pk<0)
{
pk=pk+2*dy;
}
else
{
pk=pk+2*dy-2*dx;
++y;
}
++x;
setPixel(x,y);
}
glFlush();
}

int main (int argc,char **argv)


{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50,25);
glutInitWindowSize(500,250);

glutCreateWindow("Bresenham Line");

init();
glutDisplayFunc(Line);
glutMainLoop();
return 0;
}

Write a program to draw a circle using Bresenham's


Mid Point algorithm.
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>

int xc = 320, yc = 240;

void plot_point(int x, int y)


{
glBegin(GL_POINTS);
glVertex2i(xc+x, yc+y);
glVertex2i(xc+x, yc-y);
glVertex2i(xc+y, yc+x);
glVertex2i(xc+y, yc-x);
glVertex2i(xc-x, yc-y);
glVertex2i(xc-y, yc-x);
glVertex2i(xc-x, yc+y);
glVertex2i(xc-y, yc+x);
glEnd();
}

void bresenham_circle(int r)
{
int x=0,y=r;
float pk=(5.0/4.0)-r;

plot_point(x,y);
int k;
/* Find all vertices till x=y */
while(x < y)
{
x = x + 1;
if(pk < 0)
pk = pk + 2*x+1;
else
{
y = y - 1;
pk = pk + 2*(x - y) + 1;
}
plot_point(x,y);
}
glFlush();
}

void concentric_circles(void)
{

glClear(GL_COLOR_BUFFER_BIT);

int radius = 100;


bresenham_circle(radius);

void Init()
{
glClearColor(1.0,1.0,1.0,0);
glColor3f(0.0,0.0,0.0);
gluOrtho2D(0 , 640 , 0 , 480);
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
glutCreateWindow("bresenham_circle");
Init();
glutDisplayFunc(concentric_circles);
glutMainLoop();
return 0;
}

You might also like