You are on page 1of 26

COMPUTER GRAPHICS

LAB FILE

NAME: MIMANSA PATHANIA


ENROLLMENT NO: R2142210490
SAP ID: 500091628
BATCH: 2 (AIML NON-HONOURS)
SEMESTER: IV
INDEX
S.NO. EXPERIMENT SIGNATURE
EXPERIMENT – 1
Introduction to OpenGL: [Virtual Lab Environment Setup]
 OpenGL
OpenGL is a cross-language, cross-platform application programming interface for
rendering 2D and 3D vector graphics. The API is typically used to interact with a
graphics processing unit, to achieve hardware-accelerated rendering.

It is commonly used to make UI animations more responsive or to handle embedded video or


to draw vector graphics – really any visual element you put on the screen is fair game for
OpenGL. OpenGL is becoming increasingly ubiquitous and understanding howto leverage its
incredible power is a must for developers.

 GLU AND GLUT


The OpenGL Utility Library (GLU) contains 43 functions, the prefix of the function name is
glu.OpenGL provides powerful but few drawing commands, all the more complicated
drawing must start from the point. Line and surface start. In order to reducethe heavy
programming work, Glu encapsulates OpenGL functions.
Glu functions provide developers with relatively simple usage and implement some more
complicated operations by calling functions in the core library. This function is interpreted
and executed by glu.dll. The core library and utility library in OpenGL can run on all OpenGL
platforms. Mainly include the following.
Auxiliary texture mapping functions include gluScaleImage(), gluBuild1Dmipmaps(),
gluBuild2Dmipmaps().
The OpenGL Utility Toolkit contains about 30 functions, the function name prefix is glut.Glut
is an OpenGL toolkit that does not depend on the window platform, written by MarkKLilgrad
in SGI (now in Nvidia), with the purpose of hiding the complexity of different window
platform APIs. The functions start with glut. They serve as a more powerful substitute for the
aux library and provide more complex drawing functions.

This function is interpreted and executed by glut.dll. Since the window management
function in glut does not depend on the operating environment, the tool library in
OpenGL can run under X-Window, Windows NT, OS/2 and other systems, which is
especially suitable for developing OpenGL sample programs that do not require complex
interfaces.

 OpenGL architecture
The architecture of OpenGL is based on a client-server model. An application program written
to use the OpenGL API is the "client" and runs on the CPU. The implementation of the
OpenGL graphics engine (including the GLSL shader programs you will write) is the "server"
and runs on the GPU. Geometry and many other types of attributes are storedin buffers called
Vertx Buffer Objects (or VBOs). These buffers are allocated on the GPU and filled by your
CPU program.
 Setting up the environment

 First OpenGL Program: This initializes a window of Green color.


#include <windows.h>
#include <GL/glut.h> void display() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS); glColor3f(0.0f,
1.0f, 0.0f);//Green glVertex2f(-0.5f, -
0.5f); // x, y
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush(); // Render now
}

/* Main function: GLUT runs as a console application starting at main() */int


main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
glutInitWindowSize(320, 320); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the event-processing loop
return 0;
}
 Difference between c and OpenGL
C graphics library provides various tools as well as programming capabilities for the user
to implement various graphics in a C program. On the other hand, OpenGL is anAPI
which allows the rendering of 2D as well as 3D graphics in the program.
EXPERIMENT – 2
Drawing A Line
1. Draw a simple line
Code:
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
void display() {
glClearColor(1, 0, 0, 0);
glColor3f(1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
float x1 = -0.3, y1 = -0.5;
float x2 = 0.2, y2 = 0.5;
float m, c, y;
m = (y2 - y1) / (x2 - x1);
c = y1 - m * x1;
float x5=10, y5= 0;
float x6 = -10 , y6= 0;
float m2=(y5- y6)/(x5-x6);
float c2 = y6 - m2 * x6;
float x3=0, y3=0;
float x4 = 3 , y4= 1;
float x7 = 1 , y7= 7;
float m4=(y7- y3)/(x7-x3);
float c4 = y7 - m4* x7;
float m3=(y4- y3)/(x4-x3);
float c3 = y4 - m3* x4;
glBegin(GL_POINTS);
for (float x = x1; x <= x2; x = x + 0.002) {
y = m * x + c;
glVertex2f(x, y);
}
for (float x = x6; x <= x5; x = x + 0.002) {
y = m2 * x + c2;
glVertex2f(x, y);
}
for (float x = x3; x <= x4; x = x + 0.002) {
y = m3 * x + c3;
glVertex2f(x, y);
}
for (float x = x3; x <= x7; x = x + 0.002) {
y = m4* x + c4;
glVertex2f(x, y);
}
glEnd();
glColor3f(1.0,1.0,1.0); // green y
glBegin(GL_LINES);
glVertex3f(0.0, -4.0f, 0.0f);
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(1.0, 3.0f, 0.0f);
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(-1.0, 3.0f, 0.0f);
glEnd();
printf("m=%f\n", m);
glFlush();
}

2. Draw a line using DDA algorithm


Code:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
main(){
int gd = DETECT ,gm, i;
float x, y,dx,dy,steps;
int x0, x1, y0, y1;
initgraph(&gd, &gm, NULL);
setbkcolor(WHITE);
x0 = 100 , y0 = 200, x1 = 500, y1 = 300;
dx = (float)(x1 - x0);
dy = (float)(y1 - y0);
if(dx>=dy){
steps = dx;
}
else{
steps = dy;
}
dx = dx/steps;
dy = dy/steps;
x = x0;
y = y0;
i = 1;
while(i<= steps)
{
putpixel(x, y, YELLOW);
x += dx;
y += dy;
i=i+1;
}
getch();
closegraph(); }
3. Draw a line using bresenhams’s line algorithm
Code:
#include<graphics.h>
#include<stdio.h>
main(){
int x,y,x1,y1,delx,dely,m,grtr_d,smlr_d,d;
int gm,gd=DETECT;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("*** BRESENHAM'S LINE DRAWING ALGORITHM ***\n\n");
printf("enter initial coordinate = ");
scanf("%d %d",&x,&y);
printf("enter final coordinate = ");
scanf("%d %d",&x1,&y1);
delx=x1-x;dely=y1-y;
grtr_d=2*dely-2*delx;// when d > 0
smlr_d=2*dely; // when d< 0
d=(2*dely)-delx;
do{ putpixel(x,y,1);
if(d<0) {
d=smlr_d+d;
}
else{
d=grtr_d+d;
y=y+1;
}
x=x+1;
}while(x<x1);
getch();
EXPERIMENT – 3
Drawing A Circle
1. WAP to draw simple circle
Code:
#include<GL/glut.h>
#include<stdio.h>
#include<math.h>
#include <stdlib.h>
float x,y,r;
void Init(){
gluOrtho2D(-10,10,-10,10);
}
void circle1(GLfloat x, GLfloat y, GLfloat radius){
float angle;
glBegin(GL_POLYGON);
for(int i=0;i<360;i++){
glVertex2f((x+(cos(i * 2 * (3.14/360)) * radius)),(y+(sin(i* 2 * (3.14/360))*radius)));
}
glEnd();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,1,0);
circle1(x,y,r);
glFlush();
}

2. WAP to draw circle using Bresenham circle drawing algo.


Bresenham’s Circle Drawing Algorithm is a circle drawing algorithm that selects thenearest
pixel position to complete the arc. The unique part of this algorithm is that is usesonly integer
arithmetic which makes it, significantly, faster than other algorithms using floating point
arithmetic in classical processors.

Steps:

1. Set initial value of (Xc,Yc) and (X,Y)


2. Set decision parameter d to d=3-(2*r)
3. Call drawcircle(int Xc ,int Yc ,int X,int Y) function
4. Repeat step 5 to 8 until X<=Y
5. Increment value of x
6. If d<0,set d=dt(4*X)+6
7. Else, d=d+4*(X-Y)+10 and decrement Y by 1.
8. Call drawcircle(int Xc ,int Yc ,int X,int Y) function

Code:
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
void plotPoints(int cx, int cy, int x, int y) {
putpixel(cx+x, cy+y, YELLOW);
putpixel(cx-x, cy+y, YELLOW);
putpixel(cx+x, cy-y, YELLOW);
putpixel(cx-x, cy-y, YELLOW);
putpixel(cx+y, cy+x, YELLOW);
putpixel(cx-y, cy+x, YELLOW);
putpixel(cx+y, cy-x, YELLOW);
putpixel(cx-y, cy-x, YELLOW);
}
int main() {
int cx, cy, x = 0, y, r, p;int
gd = DETECT, gm;
printf("Enter the coordinates of centre of the circle: ");
scanf("%d %d", &cx, &cy);
printf("Enter radius of : ");
scanf("%d", &r);
y = r;
p = 3 - 2 * r;
initgraph(&gd, &gm, "");
cleardevice();
while (x < y) {
plotPoints(cx, cy, x, y);
x++;
if (p < 0)
p = p + 4 * x + 6;
else {
y--;
p = p + 4 * (x - y) + 10;
}
plotPoints(cx, cy, x, y);
delay(200);
}
getch();
}
3. WAP to Draw the circle with the help of mid-point method.
The mid-point circle drawing algorithm is an algorithm used to determine the pointsneeded
for rasterizing a circle.
We use the mid-point algorithm to calculate all the perimeter points of the circle in the
first octant and then print them along with their mirror points in the other octants.This will
work because a circle is symmetric about its center.
Steps:
Step1: Put x =0, y =r in equation 2We
have p=1-r
Step2: Repeat steps while x ≤ yPlot
(x, y)
If (p<0)
Then set p = p + 2x + 3Else
p = p + 2(x-y)+5y =y
- 1 (end if)
x =x+1 (end loop)
Step3: End

Code:
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
main()
{
int x,y,x_mid,y_mid,radius,dp;
int g_mode,g_driver=DETECT;
initgraph(&g_driver,&g_mode,"C:\\TC\\BGI");
printf("**** MID POINT Circle drawing algorithm ***\n\n");
printf("\n Enter the coordinates= ");
scanf("%d %d",&x_mid,&y_mid);
printf("\n now enter the radius =");
scanf("%d",&radius);
x=0;
y=radius;
dp=1-radius;do
{
putpixel(x_mid+x,y_mid+y,YELLOW);
putpixel(x_mid+y,y_mid+x,YELLOW);
putpixel(x_mid-y,y_mid+x,YELLOW);
putpixel(x_mid-x,y_mid+y,YELLOW);
putpixel(x_mid-x,y_mid-y,YELLOW);
putpixel(x_mid-y,y_mid-x,YELLOW);
putpixel(x_mid+y,y_mid-x,YELLOW);
putpixel(x_mid+x,y_mid-y,YELLOW);
if(dp<0) {
dp+=(2*x)+1;
}
else{ y=y-
1;
dp+=(2*x)-(2*y)+1;
}
x=x+1;
}while(y>x);
getch();
}
EXPERIMENT – 4
Drawing An Ellipse
This is an incremental method for scan converting an ellipse that is centered at the origin in standard
position i.e., with the major and minor axis parallel to coordinate system axis. It is verysimilar to the
midpoint circle algorithm. Because of the four-way symmetry property we need toconsider the entire
elliptical curve in the first quadrant.
Algorithm Step

1: Start

Step 2: Declare rx , ry , x , y , m , dx , dy , P , P2.

Step 3: Initialize initial point of region1 asx=0 ,

y = ry

Step 4: Calculate P= r 2 + r y2 / 4 x- r r 2
y x

dx = 2 r 2 x y

dy = 2 r 2 y x

Step 5: Update values of dx and dy after each iteration.Step

6: Repeat steps while (dx < dy):

Plot (x,y)if(P

< 0)

Update x = x+1 ;

P += r 2 [2xy+ 3 ]

Else

Update x = x + 1
y= y - 1

Step 7: When dx ≥ dy, plot region 2:

Step 8: Calculate P2 = r 2 ( x+1 2 2


y / 2) + r (y -1)
x
2
- r 2r 2
x y

Step 9: Repeat till (y > 0)

If (P2 > 0)

Update y = y-1 (x will remain same)

P2 = P2 -2 y r 2 + rx2 x
else

x = x+1y =

y-1

P2= P2+ 2 r 2 [2x]y -2 y r 2 + rx2 x

Step 10: End

CODE:

#include <GL/glut.h>

#include <math.h>

#include <GL/gl.h>

#include<stdio.h>

float a,rx,b,ry,d1,d2,x,y;

void display(){

a=rx;

b=ry;

x=0;

y=b;

d1=(b*b)+(a*a)*(0.25-b);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_POINTS);

while((x*b*b)<=(y*a*a)){

if(d1<0)

d1= d1+ (b*b) * (2*x+3);

else{

d1= d1+ (b*b) * (2*x+3) - (a*a) * (2*y-2);

y=y-1;

x = x + 1;

glVertex2f(x, y);

glVertex2f(x,-y);
glVertex2f(-x, -y);

glVertex2f(-x, y);

d2=(b*b)*((x+0.5)*(x+0.5)) + (a*a) * ((y-1)*(y-1)) - (a*a*b*b);

while(y!=0)

{
if(d2<0) {

d2=d2+ 2* (b*b) * (x+1) - (a*a) * (2*y-3);

20

x=x+1;

else

d2=d2+ (a*a) * (3-2*y);y=y-

y=y-1;

glVertex2f(x, y);

glVertex2f(x,-y);

glVertex2f(-x, -y);

glVertex2f(-x, y);

glEnd();

glFlush();

}
EXPERIMENT – 5
Inbuilt Functions
Some basic inbuilt functions (graphics.h)

1. Inbuilt function to draw a line


There is a predefined function named line which is used to draw a line on the output
screen. It takes 4 arguments, first two parameters represent an initial point and the lasttwo
arguments are for the final points of the line.
line() is a library function used to draw a line using given coordinates. It comes under
Graphic.h header file. It uses two coordinate points (x,y) as initial point and (x1,y1) asend
point to draw a line on output screen.

Syntax:

void Line(int x, int y, int x1, int y2);

The code given below is a simple graphic program that draws a line:

#include<conio.h>

#include<graphics.h>

#include<stdio.h>

void main(){

int gd=DETECT,gm;

int x_initial,y_initial,x_final,y_final;

clrscr();

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");

printf("\n Please enter an initial coordinate of the line = ");

scanf("%d %d", &x_initial,&y_initial);


printf("\n Now, \n enter final coordinate of the line = ");

scanf("%d %d",&x_final,&y_final);

printf("\n***** LINE ******");

line(x_initial,y_initial,x_final,y_final);

getch();

closegraph();

}
2. Inbuilt function to draw rectangle
rectangle function draws a rectangle on screen. It takes the coordinates of top left and bottom
right corners. Left specifies the X-coordinate of top left corner, top specifies theY-
coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom
specifies the Y-coordinate of right bottom corner.
Syntax:
Rectangle(int left,int top,int right,int bottom)

CODE:-
#include<graphics.h>
int main(int argc,char *,argv[]){
int gd=DETECT,gm;
init graph(&gd,&gm,” “);
setcolor(s);
rectangle(150,350,450,100); //in arc just use arc(350,350,45,180,100);
getch();
closegraph();
return 0;
}

3. Inbuilt function to draw a circle


The header file graphics.h contains circle() function which draws a circle with center at (x, y)and
given radius.

Syntax :

circle(x, y, radius);
where,
(x, y) is center of the circle. 'radius' is
the Radius of the circle.
gm is Graphics mode which is a computer display mode that generates image using pixels.
DETECT is a macro defined in "graphics.h" header file
initgraph initializes the graphics system by loading a graphics driver from disk
closegraph function closes the graphics mode and deallocates all memory allocated by graphics system .
CODE:
#include <graphics.h>

int main(){

int gd = DETECT, gm;


initgraph(&gd, &gm, "");
circle(250, 200, 50);

getch();
closegraph();
return 0;
}

4. Inbuilt function to draw arc


The header file graphics.h contains circle() function which draws a circle with center at (x, y)and
given radius.

#include <GL/glut.h> // include the GLUT library


#include <math.h> // include the math library
void display(){
glClear(GL_COLOR_BUFFER_BIT); // clear the color buffer
glColor3f(1.0, 1.0, 1.0); // set the color to white
glMatrixMode(GL_PROJECTION); // switch to projection matrix
glLoadIdentity(); // reset projection matrix
gluOrtho2D(-1.0, 1.0, -1.0, 1.0); // set up an orthographic view
const float PI = 3.14159265;
float angle1 = 45.0f; // start angle of arc in degrees
float angle2 = 135.0f; // end angle of arc in degrees
float radius = 0.5f; // radius of arc
glBegin(GL_LINE_STRIP); // start drawing line strip
for (float angle = angle1; angle <= angle2; angle += 0.1f) {
float x = radius * cos(angle * PI / 180.0f);
float y = radius * sin(angle * PI / 180.0f);
glVertex2f(x, y); // add vertex to line strip
}
glEnd(); // end drawing line strip
glFlush(); // flush drawing commands
}
int main(int argc, char **argv)
{
glutInit(&argc, argv); // initialize GLUT
glutCreateWindow("Arc"); // create a window
glutDisplayFunc(display); // register display callback function
glutMainLoop(); // enter main loop
return 0;
}
EXPERIMENT – 6
Filling Algorithms
1. Boundary fill
Boundary Fill Algorithm starts at a pixel inside the polygon to be filled and paints the interior
proceeding outwards towards the boundary. This algorithm works only if the color with which
the region has to be filled and the color of the boundary of the region are different. If the
boundary is of one single color, this approach proceeds outwards pixel by pixel until it hits the
boundary of the region.
Boundary Fill Algorithm is recursive in nature.

CODE for 4 connected approach:

#include <stdio.h>
#include <graphics.h>
Void boundary_fill_4(int x,int y,int fillcolor,int boundarycolor){
if (getpixel(x,y)!=boundarycolor && getpixel((x,y)!=fillcolor){
Putpixel(x,y,fillcolor);
Boundary_fill_4(x+1,fillcolor,boundarycolor);
Boundary_fill_4(x,y+1,fillcolor,boundarycolor);
Boundary_fill_4(x-1,y,fillcolor,boundarycolor);
Boundary_fill_4(x,y-1,fillcolor,boundarycolor);
}}
int main(){
int gd=DETECT,gm;
initgraph(&gd,&gm,” “);
Rectangle(80,320,240,430);
Delay(180);
Boundary_fill_4(85,385,1,15);
Getch();
Closegraph();
}
CODE for 8 connected approach:
#include <GL/glut.h>
#include <stdlib.h>
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
// Set the fill color
GLfloat fillColor[3] = {0.0, 0.0, 1.0}; // Blue
// Set the boundary color
GLfloat borderColor[3] = {1.0, 1.0, 1.0}; // Black
// Get the color of a pixel
void getPixelColor(int x, int y, GLfloat color[3]) {
glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, color);
}
// Set the color of a pixel
void setPixelColor(int x, int y, GLfloat color[3]) {
glColor3fv(color);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
// Recursive function to fill the region
void boundaryFill8(int x, int y) {
GLfloat interiorColor[3];
getPixelColor(x, y, interiorColor);
if ((interiorColor[0] != borderColor[0]) &&
(interiorColor[1] != borderColor[1]) &&
(interiorColor[2] != borderColor[2]) &&
(interiorColor[0] != fillColor[0]) &&
(interiorColor[1] != fillColor[1]) &&
(interiorColor[2] != fillColor[2])) {
setPixelColor(x, y, fillColor);
boundaryFill8(x+1, y);
boundaryFill8(x-1, y);
boundaryFill8(x, y+1);
boundaryFill8(x, y-1);
boundaryFill8(x+1, y+1);
boundaryFill8(x+1, y-1);
boundaryFill8(x-1, y+1);
boundaryFill8(x-1, y-1);
}
}
// Display the window
void display() {
glClearColor(0.0, 0.0, 0.0, 0.0); // Set the background color to white
glClear(GL_COLOR_BUFFER_BIT);
// Draw the border of the region
glColor3fv(borderColor);
glRecti(100, 100, 300, 300);
// Call the boundary fill algorithm
boundaryFill8(150, 150);
glFlush();
}
// Initialize the window
void init() {
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT);
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Boundary Fill 8 Connected Algorithm");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;}
2. Flood fill
In this method, a point or seed which is inside region is selected. This point is called a seed
point. Then four connected approaches or eight connected approaches is used to fill with
specified color. The flood fill algorithm has many characters similar to boundary fill. But this
method is more suitable for filling multiple colors boundary. When boundary is of many
colors and interior is to be filled with one color we use this algorithm.
CODE for 4 connected approach:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>

void flood(int,int,int,int);
void main(){
intgd=DETECT,gm;
initgraph(&gd,&gm," ");
rectangle(50,50,250,250);
flood(55,55,10,0);

getch();
}
void flood(intx,inty,intfillColor, intdefaultColor)
{
if(getpixel(x,y)==defaultColor){
delay(1);
putpixel(x,y,fillColor);

flood(x+1,y,fillColor,defaultColor)
flood(x-,y,fillColor,defaultColor);
flood(x,y+1,fillColor,defaultColor)
flood(x,y-,fillColor,defaultColor);

}
}

CODE for 8 connected approach:


#include <GL/glut.h>
#include <stdlib.h>
#define WINDOW_WIDTH 640
#define WINDOW_HEIGHT 480
// Set the fill color
GLfloat fillColor[3] = {0.0, 0.0, 1.0}; // Blue
// Set the boundary color
GLfloat borderColor[3] = {0.0, 0.0, 0.0}; // Black
// Set the color of a pixel
void setPixelColor(int x, int y, GLfloat color[3]) {
glColor3fv(color);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
// Recursive function to fill the region
void floodFill8(int x, int y) {
GLfloat interiorColor[3];
getPixelColor(x, y, interiorColor);
if ((interiorColor[0] != borderColor[0]) &&
(interiorColor[1] != borderColor[1]) &&
(interiorColor[2] != borderColor[2]) &&
(interiorColor[0] != fillColor[0]) &&
(interiorColor[1] != fillColor[1]) &&
(interiorColor[2] != fillColor[2])) {
setPixelColor(x, y, fillColor);
floodFill8(x+1, y);
floodFill8(x-1, y);
floodFill8(x, y+1);
floodFill8(x, y-1);
floodFill8(x+1, y+1);
floodFill8(x+1, y-1);
floodFill8(x-1, y+1);
floodFill8(x-1, y-1);
}
}
// Display the window
void display() {
glClearColor(1.0, 1.0, 1.0, 1.0); // Set the background color to white
glClear(GL_COLOR_BUFFER_BIT);

// Draw the border of the region


glColor3fv(borderColor);
glRecti(100, 100, 300, 300);
// Call the flood fill algorithm
floodFill8(150, 150);
glFlush();
}
// Initialize the window
void init() {
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT);
}
// Get the color of a pixel
void getPixelColor(int x, int y, GLfloat color[3]) {
glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, color);
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow("Flood Fill 8 Connected Algorithm");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0;
}

You might also like