Professional Documents
Culture Documents
LAB FILE
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
Steps:
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
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
Plot (x,y)if(P
< 0)
Update x = x+1 ;
P += r 2 [2xy+ 3 ]
Else
Update x = x + 1
y= y - 1
If (P2 > 0)
P2 = P2 -2 y r 2 + rx2 x
else
x = x+1y =
y-1
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)
else{
y=y-1;
x = x + 1;
glVertex2f(x, y);
glVertex2f(x,-y);
glVertex2f(-x, -y);
glVertex2f(-x, y);
while(y!=0)
{
if(d2<0) {
20
x=x+1;
else
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)
Syntax:
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");
scanf("%d %d",&x_final,&y_final);
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;
}
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(){
getch();
closegraph();
return 0;
}
#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);
}
}