Você está na página 1de 8

Q u e s: 1

Write a program in C/C++ using OpenGL to draw a circle of red colour inside of a
triangle of blue colour on a background of yellow colour.

Progam:
#include <gl.h>
#include <glu.h>
#include <glut.h>
void initGL()
{
glClearColor(1.0f, 1.0f, 0.0f);// Set background clearing color to yellow

void display()
{
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer with current
clearing color

glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex2f(0.1f, -0.6f);
glVertex2f(0.7f, -0.6f);
glVertex2f(0.4f, -0.1f);

glColor3f(1.0f, 1.0f, 0.0f); // Yellow


glVertex2f(0.3f, -0.4f);
glVertex2f(0.9f, -0.4f);
glVertex2f(0.6f, -0.9f);
glEnd();

glBegin(GL_POLYGON); // The vertices form one closed polygon


glColor3f(0.0f, 1.0f, 1.0f); // Cyan
glVertex2f(0.4f, 0.2f);
glVertex2f(0.6f, 0.2f);
glVertex2f(0.7f, 0.4f);
glVertex2f(0.6f, 0.6f);
glVertex2f(0.4f, 0.6f);
glVertex2f(0.3f, 0.4f);
glEnd();

glFlush(); // Render now


}

// Main function: GLUT runs as a Console Application


int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitWindowSize(320, 320); // Set the initial Window's width and height
glutInitWindowPosition(50, 50); // Position the initial Window's top-left
corner
glutCreateWindow("2D Shapes"); // Create window with the given title
glutDisplayFunc(display); // Register callback handler for window re-
paint event
initGL(); // Our own OpenGL initialization
glutMainLoop(); // Enter infinitely event-processing loop
return 0;
Q u e s:2
Q2: Write a program in C or C++ to Cohen-Sutherland line clipping algorithm

Program
*/- Line clipping using cohen- sutherland algo -*/
#include<iostream.h>
#include<graphics.h>
#include<conio.h>

typedef unsigned int outcode;


enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };

//prototype declaration for the functions


int calcode (float,float,float,float,float,float);
void lineclipping(float, float, float, float, float ,float ,float ,float );

void main()
{
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd,gm;
detectgraph (&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
cout<<"\n\n\t Enter the co-ordinates of Line :";
cout"\n\n\tX1 Y1 : ";
cin>>x1>>y1;
cout<<"\n\n\tX2 Y2 :";
cin>>x2>>y2;
cout<<"\n\tEnter the co_ordinates of window :\n ";
cout<<"\n\txwmin , ywmin : ";
cin>> xwmin>>ywmin ;
cout<<"\n\txwmax , ywmax : ";
cin>>xwmax>>ywmax ;
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
cleardevice();
lineclipping(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax ); //calling clipping function
getch();
closegraph();
}

//creating line clipping function

void lineclipping(float x0,float y0,float x1,float y1,float xwmin,float ywmin,float xwmax,float


ywmax )
{
int gd, gm;
outcode code0,code1,codeout;
int accept = 0, done=0;
code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
do
{
if(!(code0 || code1))
{
accept =1 ;
done =1;
}
else if(code0 & code1)
done = 1;
else
{
float x,y;
codeout = code0 ? code0 : code1;
if(codeout & TOP)
{
x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);
y = ywmax;
}
else if( codeout & BOTTOM)
{
x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);
y = ywmin;
}
else if ( codeout & RIGHT)
{
y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);
x = xwmax;
}
else
{
y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);
x = xwmin;
}
}
if( codeout == code0)
{
x0 = x; y0 = y;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
}
else
{
x1 = x; y1 = y;
code1 = calcode(x1, y1,xwmin,ywmin,xwmax,ywmax);
}

} while( done == 0);


if(accept)
{ line(x0,y0,x1,y1);
}
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
} // end of line clipping function

//defining calcode function


int calcode (float x,float y,float xwmin,float ywmin,float xwmax,float ywmax)
{
int code =0;
if(y> ywmax)
code|=TOP;
else if( y<ywmin)
code = BOTTOM;
else if(x > xwmax)
code = RIGHT;
else if ( x< xwmin)
code = LEFT;
return(code);
}
Ques:3
Write a program in C/C++ using OpenGL to draw a hard wire diagram as shown in
figure given below. Use basic primitives of openGL.

Solution
#include <gl.h>
#include <glu.h>
#include <glut.h>

Int main (int argc , char ** argv )


{
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitWindowSize(320, 320); // Set the initial Window's width and height
glutInitWindowPosition(50, 50); // Position the initial Window's top-left corner
glutCreateWindow("2D Shapes"); // Create window with the given title
glutDisplayFunc(display); // Register callback handler for window re-paint event

init GL( );
glvertex 3f (-1. 0 f , - 0.5 f , - 4.0f);
glvertex 3f (1. 0 f , - 0.5 f , - 4.0f);
glvertex 3f (0. 0 f , - 0.5 f , - 4.0f);
glvertex 3f (0. 0 f , - 0.9 f , - 4.0f);
glvertex 3f (1. 0 f, - 0.0f, 0.0f);
glvertex 2f (-1. 8 f , - 0.1f );
glvertex 2f (-1. 2 f , - 0.1f );
glvertex 2f (-1. 2 f , - 0.7f );
glvertex 2f (-1. 8 f , - 0.7f );
glvertex 2f (-3.0 f , - 2.9f );
glEnd ( );
}
Q u e s: 4
Write a program in C/C++ using OpenGL to perform a 3-Dimensional transformation,
such as translation ,rotation and reflection, on a given rectangle.

Program:

#include <iostream.h>
#include <stdlib.h>
#include<graphics.h>
#include<conio.h>
void draw3d(int ,int [ ] ,int[ ],int ,int ,int );
void draw3d(int fs,int x[20],int y[20],int tx , int ty, int d)
{
int i,j,k=0;
for(j=0;j<2;j++)
{
for(i=0;i<fs;i++)
{
if(i!=fs-1)
line(x[i]+tx+k,y[i]+ty-k,x[i+1]+tx+k,y[i+1]+ty-k);
else
line(x[i]+tx+k,y[i]+ty-k,x[0]+tx+k,y[0]+ty-k);
}
k=d;
}
for(i=0;i<fs;i++)
{
line(x[i]+tx,y[i]+ty,x[i]+tx+d,y[i]+ty-d);
}
}

void main()
{
int gd=DETECT,gm;
int x[20],y[20],tx=0,ty=0,i,fs,d;
initgraph(&gd,&gm,"");
cout<<"no of sides (front view only) : ";
cin>>fs;
cout<<"co-ordinates : ";
for(i=0;i<fs;i++)
{
cout<<i<<i;
cin>>x[i]>>y[i];
}
cout<<"Depth :";
cin>>d;
draw3d(fs,x,y,tx,ty,d);
cout<<"translation (x,y)";
cin>>tx>>ty;
draw3d(fs,x,y,tx,ty,d);
getch();
}

Você também pode gostar