10 Program to draw hill using Bresenham Line Drawing Algorithm and fill color using Seed fill Algorithm in OpenGL

Program:

#include<GL/glut.h>

void init()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glClearColor(1,1,1,0);
	gluOrtho2D(0,500,0,500);
}

int line_cordinates[4],co_i;

void plotPixel(int x,int y,float *fill_color)
{
	glColor3f(fill_color[0],fill_color[1],fill_color[2]);
	
	glBegin(GL_POINTS);
		glVertex2i(x,y);
	glEnd();
	
	glFlush();
}

// Boundary Fill Algoritm
void boundaryfill(int x,int y,float *fill_color,float *boundary_color)
{
	float current[3];
	glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,current);
	
	if( (current[0] != boundary_color[0] || current[1] != boundary_color[1] || current[2] != boundary_color[2]) && 
	 	(current[0] != fill_color[0] || current[1] != fill_color[1] || current[2] != fill_color[2]))
	 {
	 	
	 	plotPixel(x,y,fill_color);
	 	
	 	boundaryfill(x+1,y,fill_color,boundary_color);
	 	boundaryfill(x,y+1,fill_color,boundary_color);
	 	boundaryfill(x-1,y,fill_color,boundary_color);
	 	boundaryfill(x,y-1,fill_color,boundary_color);
	 }
	
}

// DDA Line Drawing Algoritm
void ddaAlgo(int x1,int y1,int x2,int y2)
{
	float dx = x2-x1,dy = y2-y1;
	float m = dy/dx;
	float steps;
	
	if(abs(dx) > abs(dy))
		steps = abs(dx);
	else
		steps = abs(dy);
		
	float xinc = dx/steps;
	float yinc = dy/steps;
	float x=x1,y=y1;
	
	
	for(int i=1;i<=steps;i++)
	{
		glBegin(GL_POINTS);
			glVertex2f(x,y);
		glEnd();
		x  = x +xinc;
		y = y + yinc;
	}
	
	glFlush();
}

// Bresenham's Line Drawing Algorithm
void bsaAlgo(int x1,int y1,int x2,int y2)
{
	float dy = y2-y1;
	float dx = x2 -x1;
	float m = dy/dx; 
	float p;
	
	float x = x1, y = y1;
	
	int x_change = 1, y_change = 1;
	
	if(x1>x2)
		x_change = -1;
	if(y1>y2)
		y_change = -1;
		
	
	if(dx < 0)
		dx = -dx;
	if(dy < 0)
		dy = -dy;
	
	
	glBegin(GL_POINTS);

	if(dx>dy)
	{
		p = 2*dy-dx;
		for(int i=0;i<dx;i++)
		{
			if(p<0)
			{
				p = p +  2*dy;
			}
			else
			{
				
				y = y + y_change;
				p = p + 2 * (dy - dx);
			}
			x = x + x_change;
			glVertex2i(x,y);
		}
	}
	else
	{
		p = 2*dx-dy;
		for(int i=0;i<dy;i++)
		{
			if(p<0)
			{
				
				p = p + 2*dx;
			}
			else
			{
				x = x + x_change;
				
				p = p + 2*(dx - dy);
			}
			y = y + y_change;
			glVertex2i(x,y);
		}
	}
	glEnd();
	
	glFlush();
}	

void display()
{
	//glPointSize(4);
	glColor3f(1,0,0);
	
	bsaAlgo(100,100,200,100);
	bsaAlgo(300,100,400,100);
	bsaAlgo(400,100,400,400);
	bsaAlgo(400,400,100,400);
	bsaAlgo(100,400,100,100);
	bsaAlgo(200,100,160,50);
	bsaAlgo(160,50,340,50);
	bsaAlgo(340,50,300,100);
	bsaAlgo(180,200,200,140);	
	bsaAlgo(200,140,300,260);
	bsaAlgo(300,260,360,140);	
	
	        // Inside Window
	        bsaAlgo(140,140,360,140);
		bsaAlgo(360,140,360,360);
		bsaAlgo(360,360,140,360);
		bsaAlgo(140,360,140,140);
	
	bsaAlgo(140,140,180,200);
	

	float boundary_color[3],fill_color[3];
	
	// Setting boundary color as RED	
	boundary_color[0] = 1;
	boundary_color[1] = 0;
	boundary_color[2] = 0;
	
	// Fill Color to Green
	fill_color[0] = 0;
	fill_color[1] = 1;
	fill_color[2] = 0;
	
	
	boundaryfill(144,142,fill_color,boundary_color);
	boundaryfill(204,142,fill_color,boundary_color);
	
	// Fill Color to Light Blue
	fill_color[0] = 0;
	fill_color[1] = 1;
	fill_color[2] = 1;
	
	boundaryfill(102,102,fill_color,boundary_color);
	
	glFlush();
}

void mouse(int button,int state,int x,int y)
{
	if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && co_i <4)
	{
		line_cordinates[co_i] = x;
		co_i++;
		
		line_cordinates[co_i] = 500-y;
		co_i++;
	}
	
	if(co_i == 4)
	{
		ddaAlgo(line_cordinates[0],line_cordinates[1],line_cordinates[2],line_cordinates[3]);
		co_i = 0;
	}
}

int main(int argc, char **argv)
{
	glutInit(&argc,argv);
	glutInitWindowSize(500,500);
	glutInitWindowPosition(150,200);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutCreateWindow("Hill Fill");
	init();
	glutDisplayFunc(display);
	glutMouseFunc(mouse);	
	glutMainLoop();
	
	return 0;
}

Output:

Hill Program


Previous
Next Post »