Null
Null
#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);
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();
----------------------------
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();
}
#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 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();
}
#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 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);
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
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;
}
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();
/* 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();
}
#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 x1=50,y1=50;
int r=1,g=0,b=0;
int screenwidth=800,screenheight=600;
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);
}
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 ( );
}}
#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();
}
#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 x1=50,y1=50;
int r=1,g=0,b=0;
int screenwidth=800,screenheight=600;
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 ( );
}}
#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();
}