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

Null

The document contains multiple OpenGL programs that demonstrate various graphical outputs, including displaying a national flag, creating animations, rendering a chess game, implementing the DDA line drawing algorithm, and drawing a house. Each program initializes OpenGL settings, defines drawing functions, and uses GLUT for window management and rendering. The code snippets illustrate different graphical techniques and algorithms in OpenGL.

Uploaded by

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

Null

The document contains multiple OpenGL programs that demonstrate various graphical outputs, including displaying a national flag, creating animations, rendering a chess game, implementing the DDA line drawing algorithm, and drawing a house. Each program initializes OpenGL settings, defines drawing functions, and uses GLUT for window management and rendering. The code snippets illustrate different graphical techniques and algorithms in OpenGL.

Uploaded by

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

1: Write a program in opengl that display flag of our country

#include <GL/glut.h>
void init()
{
glClearColor(1.0,1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,800,0,600);
}
void QUAD_DRAW(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4)
{
glBegin(GL_QUADS);
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glVertex2i(x3,y3);
glVertex2i(x4,y4);
glEnd();
}

void Line_Strip_DRAW(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4)
{
glBegin(GL_LINE_STRIP);
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glVertex2i(x3,y3);
glVertex2i(x4,y4);
glEnd();
}
void displayMe(void)
{
int x;
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(8.0);

//Code for Green_Area


glColor3f(0.0,0.6,0.1);
QUAD_DRAW(150,250,420,250,420,500,150,500);
//Code for Green_Area_Border
glColor3f(0,0,0);
Line_Strip_DRAW(150,250,420,250,420,500,150,500);
//Code for Star Of Flag
glBegin(GL_TRIANGLES);
glColor3f(1,1,1);
glVertex2i(300,440);
glVertex2i(290,400);
glVertex2i(325,400);

glVertex2i(280,400);
glVertex2i(250,400);
glVertex2i(280,370);

glVertex2i(280,370);
glVertex2i(260,330);
glVertex2i(300,350);

glVertex2i(300,350);
glVertex2i(325,370);
glVertex2i(340,330);
glVertex2i(325,400);
glVertex2i(325,370);
glVertex2i(360,395);
glEnd();

glBegin(GL_POLYGON);
glVertex2i(280,400);
glVertex2i(325,400);
glVertex2i(280,370);
glVertex2i(300,350);
glVertex2i(325,370);
glVertex2i(325,400);
glEnd();

//Code for MOON using POLYGON


/*/code for Moon
glColor3f(0,0,0);
glPointSize(10);
glBegin(GL_LINE_STRIP);
glVertex2i(300,460);
//glVertex2i(260,430);
glVertex2i(245,400);
//glVertex2i(240,400);
//glVertex2i(245,390);
//glVertex2i(240,380);
glVertex2i(300,310);
//glVertex2i(210,400);
glVertex2i(210,400);
// glVertex2i(300,460);
glEnd();
*/
//Code for White_Area
glColor3f(1,1,1);
QUAD_DRAW(60,250,150,250,150,500,60,500);
//Code for White_Area_Border
glColor3f(0,0,0);
Line_Strip_DRAW(60,250,150,250,150,500,60,500);

// Code for Flag_Stand


glColor3f(0.5,0.3,0.1);
QUAD_DRAW(50,100,50,503,60,503,60,100);
glFlush();
}

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(10, 10);
glutCreateWindow("Pakistan's Flag");
init();
glutDisplayFunc(displayMe);
glutMainLoop();
return 0;
}

----------------------------
2: Write a program that display animation

#include<GL/freeglut.h>
#include<stdlib.h>
void init()
{
glClearColor(1.0,1.0,1.0,0.0);
glPointSize(8.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,800,0,600);
glClear(GL_COLOR_BUFFER_BIT);
}
void Draw()
{
int r, g, b;
r= rand()%2;
g= rand()%2;
b= rand()%2;
glColor3f(r,g,b);
int x= rand()%800;
int y= rand()%600;
int x1= rand()%800;
int y1= rand()%600;
int x2= rand()%800;
int y2= rand()%600;
int x3= rand()%800;
int y3= rand()%600;
int x4= rand()%800;
int y4= rand()%600;
glBegin(GL_TRIANGLES);
glVertex2i(x,y);
glVertex2i(x+50,y);
glVertex2i(x+25,y+50);
glEnd();
/*glRecti(x,y,x+50,y+25);*/
glFlush();
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(20, 20);
glutInitWindowSize(900, 700);
glutCreateWindow("My First");
init();
glutDisplayFunc(Draw);
glutIdleFunc(Draw);
glutMainLoop();
return 0;
}
----------------------------------
3: Write a program that show chess game

#include <GL\glut.h>
void init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 800, 0, 600);
}
void Draw()
{
int x,A=0;

glClear(GL_COLOR_BUFFER_BIT);
glPointSize(8.0);
int x1=60,x2=100,y1=60,y2=100;
glColor3f(0, 0, 0);
int y,z=0,sign=1,sign1=-1;
int a=2,b,c;
for(x=0;x<10;x++)
{
if(a==z)
{
z=z+(sign)*1;
sign=sign*(-1);
}
for(y=0;y<10;y++)
{
if(y==0)
{
a=z;
}

glColor3f(z, z, z);
glRecti(x1,y1,x2,y2);
z=z+(sign)*1;
sign=sign*(-1);
x1=x2;
x2+=40;
}
b=x2;
c=y2;
y1=y2;
y2+=40;
x1=60;
x2=100;
}
glColor3f(0,0,0);
glBegin(GL_LINE_STRIP);
glVertex2i(60,60);
glVertex2i(b-40,60);
glVertex2i(b-40,c);
glVertex2i(60,c);
glVertex2i(60,60);
glEnd();
glFlush();
}

int main(int argc, char**argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(20, 20);
glutInitWindowSize(800, 600);
glutCreateWindow("My First OpenGL Program");
init();
glutDisplayFunc(Draw);
glutMainLoop();
}
-------------------------------------

4: Write program that implemet and display DDA algorithm

#include <GL\glut.h>
int Number;
void init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 800, 0, 600);
}

void Set_Pixel(int x, int y)


{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
int Round_Func(double x)
{
return (int)(x + 0.5);
}

void DDA_LINE_DRAW(int x0, int y0, int x1, int y1)


{
int dx = x1 - x0;
int dy = y1 - y0;
int steps;
if (abs(dx)>abs(dy))
steps = abs(dx);
else
steps = abs(dy);
double Xinc = (double)dx / (double)steps;
double Yinc = (double)dy / (double)steps;
double x = x0;
double y = y0;
Set_Pixel(Round_Func(x), Round_Func(y));
for (int i = 0; i<steps; i++)
{
x += Xinc;
y += Yinc;
Set_Pixel(Round_Func(x), Round_Func(y));
}
}

void Draw()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 1.0, 0.0);
glPointSize(8.0);
DDA_LINE_DRAW(120, 150, 700, 340);
glColor3f(0.8, 1, 0.2);
DDA_LINE_DRAW(300, 100, 500, 500);
glEnd();
glFlush();
}

int main(int argc, char**argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(20, 20);
glutInitWindowSize(800, 600);
glutCreateWindow("DDA Algorithm");
init();
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}
---------------------------------
5: Write program in opengl that display house

#include<GL/freeglut.h>
#include<stdlib.h>
void init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glPointSize(8.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 800, 0, 600);

void Set_Pixel(int x, int y)


{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}

void midptellipse(int rx, int ry, int xc, int yc)


{
glClear(GL_COLOR_BUFFER_BIT);
float dx, dy, d1, d2, x, y;
x = 0;
y = ry;
// Initial decision parameter of region 1
d1 = (ry * ry) - (rx * rx * ry) +
(0.25 * rx * rx);
dx = 2 * ry * ry * x;
dy = 2 * rx * rx * y;
// For region 1
while (dx < dy)
{

// Print points based on 4-way symmetry


Set_Pixel(int(x + xc), (int)(y + yc));
Set_Pixel((int)(-x + xc), (int)(y + yc));
Set_Pixel((int)(x + xc), (int)(-y + yc));
Set_Pixel((int)(-x + xc), (int)(-y + yc));
// Checking and updating value of
// decision parameter based on algorithm
if (d1 < 0)
{
x++;
dx = dx + (2 * ry * ry);
d1 = d1 + dx + (ry * ry);
}
else
{
x++;
y--;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d1 = d1 + dx - dy + (ry * ry);
}
}

// Decision parameter of region 2


d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) +
((rx * rx) * ((y - 1) * (y - 1))) -
(rx * rx * ry * ry);

// Plotting points of region 2


while (y >= 0)
{

// Print points based on 4-way symmetry


Set_Pixel(int(x + xc), (int)(y + yc));
Set_Pixel((int)(-x + xc), (int)(y + yc));
Set_Pixel((int)(x + xc), (int)(-y + yc));
Set_Pixel((int)(-x + xc), (int)(-y + yc));

// Checking and updating parameter


// value based on algorithm
if (d2 > 0)
{
y--;
dy = dy - (2 * rx * rx);
d2 = d2 + (rx * rx) - dy;
}
else
{
y--;
x++;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d2 = d2 + dx - dy + (rx * rx);
}
}
}

void Draw()
{

glColor3f(0, 0, 0);
midptellipse(50, 100, 150, 200);
glFlush();
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(20, 20);
glutInitWindowSize(900, 700);
glutCreateWindow("Mid Point Ellipse Drawing Algorithm");
init();
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}
-----------------------------
6: write a program in opengl that display house

#include<GL\glut.h>
void init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 800, 0, 600);

}
void Draw()
{
glPointSize(3.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINES);

glVertex2i(220, 370);
glVertex2i(150, 240);

glVertex2i(220, 370);
glVertex2i(300, 240);

glVertex2i(160, 235);
glVertex2i(160, 150);

glVertex2i(290, 235);
glVertex2i(290, 150);

glVertex2i(160, 150);
glVertex2i(290, 150);

glVertex2i(160, 235);
glVertex2i(150, 240);

glVertex2i(290, 235);
glVertex2i(300, 240);

glBegin(GL_LINES);
glColor3f(1.0, 1.0, 0);
glVertex2i(200, 200); glVertex2i(240, 200);
glVertex2i(200, 200); glVertex2i(200, 150);
glVertex2i(240, 200); glVertex2i(240, 150);

glVertex2i(240, 200); glVertex2i(210, 190);


glVertex2i(210, 190); glVertex2i(210, 150);
glVertex2i(200, 220); glVertex2i(220, 220);
glVertex2i(200, 250); glVertex2i(200, 220);
glVertex2i(220, 220); glVertex2i(220, 250);
glVertex2i(200, 250); glVertex2i(220, 250);

glVertex2i(210, 250); glVertex2i(210, 220);


glVertex2i(200, 235); glVertex2i(220, 235);

glVertex2i(220, 350); glVertex2i(160, 235);


glVertex2i(220, 350); glVertex2i(290, 235);

glEnd();
glFlush();
}
void main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(20, 20);
glutInitWindowSize(800, 600);
glutCreateWindow("Mt First openGl program");
init();
glutDisplayFunc(Draw);
glutMainLoop();
}
--------------------------
7: Write a program in opengl that display line clipping

#include<windows.h>
#include<GL/glut.h>
int x1 = -80, x2 = 0, y3 = -80, y2 = 0;
float u1 = 0, u2 = 1;
int xmin = -50, ymin = -50, xmax = 50, ymax = 50;
double p[4], q[4]; // changed from int to double thats it
void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-320, 320, -240, 240);
}
void clip(int x1, int y1, int x2, int y2)
{
int dx = x2 - x1, dy = y2 - y1, i;
double t;
p[0] = -dx; q[0] = x1 - xmin;
p[1] = dx; q[1] = xmax - x1;
p[2] = -dy; q[2] = y1 - ymin;
p[3] = dy; q[3] = ymax - y1;
for (i = 0; i<4; i++)
{
if (p[i] == 0 && q[i]<0)
return;
if (p[i]<0)
{
t = (q[i]) / (p[i]); // This calculation was returning a zero
because both q and p were int
if (t>u1 && t<u2)
{
u1 = t;
}
}
else if (p[i]>0)
{
t = (q[i]) / (p[i]); // This calculation was returning a zero
because both q and p were int
if (t>u1 && t<u2)
{
u2 = t;
}
}
}
if (u1<u2)
{
x1 = x1 + u1*(x2 - x1);
y1 = y1 + u1*(y2 - y1);
x2 = x1 + u2*(x2 - x1);
y2 = y1 + u2*(y2 - y1);
glBegin(GL_LINES);
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glEnd();
glFlush();
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.8, 0.7, 1.0);
glBegin(GL_LINES);
glVertex2i(x1, y3);
glVertex2i(x2, y2);
glEnd();
glFlush();
}
void myKey(unsigned char key, int x, int y)
{
glClear(GL_COLOR_BUFFER_BIT);
if (key == 'c' || key == 'C')
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_LINE_STRIP);
glVertex2i(-50, -50);
glVertex2i(-50, 50);
glVertex2i(50, 50);
glVertex2i(50, -50);
glVertex2i(-50, -50);
glEnd();
glFlush();
clip(x1, y3, x2, y2);
}
}
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0, 0);
glutCreateWindow("Liang Barsky Line Clipping Algorithm");
glutDisplayFunc(display);
glutKeyboardFunc(myKey);
init();
glutMainLoop();
return 0;
}
-------------------------
8: write a program in opengl that display movement of rectangle with keyboard

#include <GL/glut.h> // (or others, depending on the system in use)


#include <stdlib.h>
#include <iostream>

using namespace std;


int x = 100, y = 100;

void init(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0); // Set display-window color to white.
glPointSize(3.0);
glMatrixMode(GL_PROJECTION); // Set projection parameters.
gluOrtho2D(0.0, 800.0, 0.0, 600.0);

void Draw()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.2);
glRecti(x, y, x + 100, y + 100);
glFlush(); // Process all OpenGL routines as quickly as possible.

}
void keyboard(unsigned char key, int mousex, int mousey)
{
switch (key)
{
case 'w': // UP
case 'W':
y++;
break;

case 's': // DOWN


case'S':
y--;
break;
case 'D': // RIGHT
case'd':
x++;
break;

case 'a': // left


case'A':
x--;
break;
}
glutPostRedisplay();

}
void SPkeyboard(int key, int mousex, int mousey)
{
switch (key)
{
case GLUT_KEY_UP:

y += 50;
break;

case GLUT_KEY_DOWN:

y -= 50;
break;
case GLUT_KEY_RIGHT:

x += 50;
break;

case GLUT_KEY_LEFT:

x -= 50;
break;
}

glutPostRedisplay();

int main(int argc, char** argv)


{
glutInit(&argc, argv); // Initialize GLUT.
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition(50, 100); // Set top-left display-window position.
glutInitWindowSize(800, 600); // Set display-window width and height.
glutCreateWindow("An Example OpenGL Program"); // Create display window.

init(); // Execute initialization procedure.


glutDisplayFunc(Draw); // Send graphics to display window.
glutKeyboardFunc(keyboard);
glutSpecialFunc(SPkeyboard);
glutMainLoop(); // Display everything and wait.
return 0;
}
--------------------
9: Write a program in opengl that display chair

/* srand example */
#include<iostream>
#include <GL\glut.h>
void init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 800, 0, 600);
}

void Draw_Square(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4)
{
glBegin(GL_QUADS);
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glVertex2i(x3,y3);
glVertex2i(x4,y4);
glEnd();

void Draw()
{
int x;

int y,z;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
glPointSize(8.0);
glColor3f(1,0.8,0.1);
Draw_Square(250,450,400,470,400,320,250,250);
Draw_Square(400,320,250,250,380,190,500,275);
Draw_Square(260,250,250,255,250,100,260,100);
Draw_Square(375,195,385,195,385,60,375,60);
Draw_Square(490,100,500,100,500,275,490,275);
Draw_Square(400,290,410,290,410,130,400,130);
glFlush();
}

int main(int argc, char**argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(20, 20);
glutInitWindowSize(800, 600);
glutCreateWindow("My First OpenGL Program");
init();
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}
-----------------------------
10: write a program in opengl that draw points

#include <GL\glut.h>
void init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0, 800, 0, 600);
}

void Draw()
{
int x;
int y,z;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
glPointSize(8.0);
glBegin(GL_POINTS);
glVertex2i(320, 320);
glVertex2i(420, 320);
glVertex2i(300, 260);
glVertex2i(365, 260);
glVertex2i(430, 260);
glVertex2i(320, 200);
glVertex2i(420, 200);

glEnd();
glFlush();
}

int main(int argc, char**argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(20, 20);
glutInitWindowSize(800, 600);
glutCreateWindow("My First OpenGL Program");
init();
glutDisplayFunc(Draw);
glutMainLoop();
return 0;
}
---------------------
11: Write a program in opengl that draw point with mouse and menuhandler

#include <GL/glut.h> // (or others, depending on the system in use)


#include<stdlib.h>
# define RED 1 //preprocessor directives they are include to the program
# define GREEN 2
# define BLUE 3
#define BLACK 4

int x1=50,y1=50;
int r=1,g=0,b=0;

int screenwidth=800,screenheight=600;

void init (void)


{
glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white.
glPointSize(5.0);
glMatrixMode (GL_PROJECTION); // Set projection parameters.
gluOrtho2D (0.0, screenwidth, 0.0, screenheight);
glClear(GL_COLOR_BUFFER_BIT);
}
void menuhandler(int option)
{
switch(option)
{
case RED:
r=1.0;
g=0.0;
b=0.0;
break;

case BLUE:
r=0.0;
g=0.0;
b=1.0;
break;
case GREEN:
r=0.0;
g=1.0;
b=0.0;
break;
case BLACK:
r=0.0;
g=0.0;
b=0.0;
break;
}
}

void Draw(void)
{

// glColor3f(r,g,b);

glFlush ( ); // Process all OpenGL routines as quickly as possible.

}
void Mousehandler(int button ,int state,int mousex,int mousey)
{
if(state==GLUT_DOWN) //input ,output is used thats why we r using glut.gl
is used for drawing.
if(button==GLUT_LEFT_BUTTON)
{
x1=mousex;
y1=screenheight-mousey;
glColor3f(r,g,b);
glPointSize(8);
glBegin(GL_POINTS);

glVertex2i(x1,y1);

glEnd ( );
glFlush ( );
}}

int main (int argc, char** argv)


{
glutInit (&argc, argv); // Initialize GLUT.
glutInitDisplayMode(GLUT_SINGLE| GLUT_RGB); // Set display mode.
glutInitWindowPosition(50, 100); // Set top-left display-window position.
glutInitWindowSize(screenwidth,screenheight); // Set display-window width
and height.
glutCreateWindow ("mouse Drawing"); // Create display window.

init ( ); // Execute initialization procedure.


glutDisplayFunc(Draw); // Send graphics to display window.
glutMouseFunc(Mousehandler);
glutCreateMenu(menuhandler);
glutAddMenuEntry("red",RED);
glutAddMenuEntry("green",GREEN);
glutAddMenuEntry("blue",BLUE);
glutAddMenuEntry("black",BLACK);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop( );
// Display everything and wait.
return 0;
}
-----------------------------
12: Write a program in opengl that draw rectangle with same x1 and y1

#include <GL/glut.h>
void init()
{
glClearColor(1.0,1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,1366,0,768);

}
void displayMe(void)
{
int x=10;
int x1=650,x2=800;
int y1=350,y2=450;
int x_gap=(x2-x1)/x-2;
int y_gap=(y2-y1)/x-2;
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(8.0);
float a=1.0,b=0.0,c=1.0;
for(int i=0;i<x;i++)
{
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE_STRIP);
glVertex2i(x1,y1);
glVertex2i(x1,y2);
glVertex2i(x2,y2);
glVertex2i(x2,y1);
glVertex2i(x1,y1);
glEnd();
//x1=x1-x_gap;
x2=x2+x_gap;
// y1=y1-y_gap;
y2=y2+y_gap;
}
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(1366, 768);
glutInitWindowPosition(10, 10);
glutCreateWindow("Rectangles_inside_Triangle");
init();
glutDisplayFunc(displayMe);
glutMainLoop();
return 0;
}
-------------------
13: Write a program in opengl that display rectangle groups

#include <GL/glut.h>
void init()
{
glClearColor(1.0,1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,1366,0,768);

}
void displayMe(void)
{
int x=8;
int x1=650,x2=800;
int y1=350,y2=450;
int x_gap=(x2-x1)/x+3;
int y_gap=(y2-y1)/x+3;
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(8.0);
float a=1.0,b=0.0,c=1.0;
for(int i=0;i<x;i++)
{
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE_STRIP);
glVertex2i(x1,y1);
glVertex2i(x1,y2);
glVertex2i(x2,y2);
glVertex2i(x2,y1);
glVertex2i(x1,y1);
glEnd();
x1=x1-x_gap;
x2=x2+x_gap;
y1=y1-y_gap;
y2=y2+y_gap;
}
glFlush();
}

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(1366, 768);
glutInitWindowPosition(10, 10);
glutCreateWindow("Rectangles_inside_Triangle");
init();
glutDisplayFunc(displayMe);
glutMainLoop();
return 0;
}
-------------------
14: Write a program in opengl that display rectangle with same y1

#include <GL/glut.h>
void init()
{
glClearColor(1.0,1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,1366,0,768);

}
void displayMe(void)
{
int x=6;
int x1=650,x2=800;
int y1=350,y2=450;
int x_gap=(x2-x1)/x+3;
int y_gap=(y2-y1)/x+3;
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(8.0);
float a=1.0,b=0.0,c=1.0;
for(int i=0;i<x;i++)
{
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE_STRIP);
glVertex2i(x1,y1);
glVertex2i(x1,y2);
glVertex2i(x2,y2);
glVertex2i(x2,y1);
glVertex2i(x1,y1);
glEnd();
x1=x1-x_gap;
x2=x2+x_gap;
y2=y2+y_gap;
}
glFlush();
}

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(1366, 768);
glutInitWindowPosition(10, 10);
glutCreateWindow("Rectangles_inside_Triangle_with_Same(Y1)");
init();
glutDisplayFunc(displayMe);
glutMainLoop();
return 0;
}
---------------------
15: Write a program in opengl that draw recti with mouse and menuhandler
#include <GL/glut.h> // (or others, depending on the system in use)
#include<stdlib.h>
# define RED 1 //preprocessor directives they are include to the program
# define GREEN 2
# define BLUE 3
#define BLACK 4

int x1=50,y1=50;
int r=1,g=0,b=0;

int screenwidth=800,screenheight=600;

void init (void)


{
glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white.
glPointSize(5.0);
glMatrixMode (GL_PROJECTION); // Set projection parameters.
gluOrtho2D (0.0, screenwidth, 0.0, screenheight);
glClear(GL_COLOR_BUFFER_BIT);
}
void menuhandler(int option)
{
switch(option)
{
case RED:
r=1.0;
g=0.0;
b=0.0;
break;

case BLUE:
r=0.0;
g=0.0;
b=1.0;
break;
case GREEN:
r=0.0;
g=1.0;
b=0.0;
break;
case BLACK:
r=0.0;
g=0.0;
b=0.0;
break;
}
}

void Draw(void)
{

// glColor3f(r,g,b);
glFlush ( ); // Process all OpenGL routines as quickly as possible.

}
void Mousehandler(int button ,int state,int mousex,int mousey)
{
if(state==GLUT_DOWN) //input ,output is used thats why we r using glut.gl
is used for drawing.
if(button==GLUT_LEFT_BUTTON)
{
x1=mousex;
y1=screenheight-mousey;
glColor3f(r,g,b);
glPointSize(8);
glRecti(x1,y1,x1+100,y1+100);
glFlush ( );
}}

int main (int argc, char** argv)


{
glutInit (&argc, argv); // Initialize GLUT.
glutInitDisplayMode(GLUT_SINGLE| GLUT_RGB); // Set display mode.
glutInitWindowPosition(50, 100); // Set top-left display-window position.
glutInitWindowSize(screenwidth,screenheight); // Set display-window width
and height.
glutCreateWindow ("mouse Drawing"); // Create display window.

init ( ); // Execute initialization procedure.


glutDisplayFunc(Draw); // Send graphics to display window.
glutMouseFunc(Mousehandler);
glutCreateMenu(menuhandler);
glutAddMenuEntry("red",RED);
glutAddMenuEntry("green",GREEN);
glutAddMenuEntry("blue",BLUE);
glutAddMenuEntry("black",BLACK);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop( );
// Display everything and wait.
return 0;
}
----------------------------
16: Write a program in opengl that shows triangle group

#include <GL/glut.h>
void init()
{
glClearColor(1.0,1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,1366,0,768);

}
void displayMe(void)
{
int x=8;
int x1=650,x2=750,x3=700;
int y1=350,y2=350,y3=450;
int x_gap=(x2-x1)/x+3;
int y_gap=(y3-y1)/x+3;
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(8.0);
float a=1.0,b=0.0,c=1.0;
for(int i=0;i<x;i++)
{
glColor3f(0.0,0.0,0.0);
glBegin(GL_LINE_STRIP);
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glVertex2i(x3,y3);
glVertex2i(x1,y1);

glEnd();
x1=x1-x_gap;
x2=x2+x_gap;
y1=y1-y_gap;
y2=y2-y_gap;
y3=y3+y_gap+1;
}
glFlush();
}

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(1366, 768);
glutInitWindowPosition(10, 10);
glutCreateWindow("Triangles_inside_Triangle");
init();
glutDisplayFunc(displayMe);
glutMainLoop();
return 0;
}
-------------------

You might also like