0% found this document useful (0 votes)
11 views7 pages

Practical 4

The document contains a C++ OpenGL program that allows users to draw a polygon and fill it with color using mouse interactions. It includes functions for setting pixel colors, flood filling, and drawing lines, along with keyboard and mouse event handling. The program initializes a window and sets up the necessary OpenGL configurations for rendering graphics.

Uploaded by

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

Practical 4

The document contains a C++ OpenGL program that allows users to draw a polygon and fill it with color using mouse interactions. It includes functions for setting pixel colors, flood filling, and drawing lines, along with keyboard and mouse event handling. The program initializes a window and sets up the necessary OpenGL configurations for rendering graphics.

Uploaded by

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

PRACTICAL NO :-04

#include<string.h>

#include<iostream>

#include<stdio.h>

#include<GL/gl.h>

#include<GL/glu.h>

#include<GL/glut.h>

using namespace std;

# define ROUND(x)((int)(x+0.5))

struct Point

GLint x;

GLint y;

};

struct Color

GLfloat r;

GLfloat g;

GLfloat b;

};

Color getPixelColor(GLint x,GLint y)

Color color;

glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,&color);

return color;

void setPixelColor(GLint x,GLint y,Color color)

glColor3f(color.r,color.g,color.b);

glBegin(GL_POINTS);

1
PRACTICAL NO :-04
glVertex2i(x,y);

glEnd();

glFlush();

void floodFill(GLint x,GLint y,Color oldColor,Color newColor)

Color color;

color=getPixelColor(x,y);

if(color.r==oldColor.r && color.g==oldColor.g && color.b ==oldColor.b)

setPixelColor(x,y,newColor);

floodFill(x+1,y,oldColor,newColor);

floodFill(x,y+1,oldColor,newColor);

floodFill(x-1,y,oldColor,newColor);

floodFill(x,y-1,oldColor,newColor);

int Height=650,Width=650;

int startX,startY;

static Point vertex[1];

static int pt=0;

Color fillcolor;

void myMouse(int button,int state,int x,int y);

void drawline(double X1,double Y1,double X2,double Y2)

float x,y,dx,dy,length;

int i;

dx=abs(X2-X1);

dy=abs(Y2-Y1);

if(dx>=dy)

2
PRACTICAL NO :-04
length=dx;

else

length=dy;

dx=(X2-X1)/length;

dy=(Y2-Y1)/length;

x=X1;

y=Y1;

i=1;

while(i<=length)

glColor3f(1.0,1.0,0.0);

glBegin(GL_POINTS);

glVertex2i(ROUND(x),ROUND(y));

glEnd();

glFlush();

x=x+dx;

y=y+dy;

i=i+1;

glFlush();

void display(void)

char string[]="Step1:Draw a polygon";

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0,1.0,1.0);

glRasterPos2f(10,600);

int len,i;

len=(int)strlen(string);

for(i=0;i<len;i++)

3
PRACTICAL NO :-04
{

glutBitmapCharacter(GLUT_BITMAP_9_BY_15,string[i]);

glColor3f(1.0,0.0,0.0);

glRecti(10,30,60,10);

glColor3f(0.0,1.0,0.0);

glRecti(90,30,140,10);

glColor3f(0.0,0.0,1.0);

glRecti(170,30,220,10);

glFlush();

void myinit()

glClearColor(0.0,0.0,0.0,1.0);

glColor3f(1.0,1.0,0.0);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0,650.0,0.0,650.0);

void myKeyboard(unsigned char key,int mouseX,int mouseY)

char string[]="Step 2:Pick color by clicking on the desire color rectangle";

switch(key)

case 13:

glColor3f(1.0,1.0,1.0);

glRasterPos2f(10,580);

int len,i;

4
PRACTICAL NO :-04
len=(int)strlen(string);

for(i=0;i<len;i++)

glutBitmapCharacter(GLUT_BITMAP_9_BY_15,string[i]);

drawline(vertex[0].x,vertex[0].y,startX,startY);

pt=2;

break;

case 27:

exit(0);

void myMouse(int button,int state,int x,int y)

if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)

if(pt==0)

vertex[pt].x=x;

vertex[pt].y=Height-y;

startX=x;

startY=Height-y;

pt++;

else if(pt==1)

drawline(vertex[0].x,vertex[0].y,x,Height-y);

vertex[0].x=x;

5
PRACTICAL NO :-04
vertex[0].y=Height-y;

else if(pt==2)

fillcolor=getPixelColor(x,Height-y);

char string[]="Step 3:Click inside polygon to fill color";

glColor3f(1.0,1.0,1.0);

glRasterPos2f(10,560);

int len,i;

len=(int)strlen(string);

for(i=0;i<len;i++)

glutBitmapCharacter(GLUT_BITMAP_9_BY_15,string[i]);

pt=3;

else if(pt==3)

Color newColor={fillcolor.r,fillcolor.g,fillcolor.b};

Color oldColor={0.0f,0.0f,0.0f};

floodFill(x,Height-y,oldColor,newColor);

pt=4;

glFlush();

int main(int argc,char **argv)

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

6
PRACTICAL NO :-04
glutInitWindowPosition(0,0);

glutInitWindowSize(650,650);

glutCreateWindow("Draw a polygon using openGL");

glutDisplayFunc(display);

glutKeyboardFunc(myKeyboard);

glutMouseFunc(myMouse);

myinit();

glutMainLoop();

return 0;

You might also like