forked from keerthan657/CGVR-lab-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog4.c
126 lines (104 loc) · 2.6 KB
/
prog4.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Design and develop C program using OpenGL libraries to create two windows: display a cylinder in one window and parallelepiped in second window
*/
#include<stdio.h>
#include<GL/glut.h>
#define WIDTH 500
#define HEIGHT 500
int wid1, wid2;
void drawPixel(int x, int y)
{
glColor3f(1,0,0);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}
void plot8points(int xc, int yc, int x, int y)
{
drawPixel(xc+x,yc+y);
drawPixel(xc-x,yc+y);
drawPixel(xc+x,yc-y);
drawPixel(xc-x,yc-y);
drawPixel(xc+y,yc+x);
drawPixel(xc+y,yc-x);
drawPixel(xc-y,yc+x);
drawPixel(xc-y,yc-x);
}
void drawCircle(int xc, int yc, int r)
{
// initialize variables
int x=0, y=r;
int incx=1, incy=-1;
// calculate initial decision parameter
int p = 3 - 2*r;
// plot the circle
plot8points(xc,yc,x,y);
while(x<y) {
if(p<0) {
p += 4*x + 6;
x += incx;
} else {
p += 4*(x-y) + 10;
x += incx;
y += incy;
}
plot8points(xc,yc,x,y);
}
// V.V.IMP - never forget to call glFlush() at the end
glFlush();
}
void drawQuad(int x, int y, int width, int height)
{
glBegin(GL_LINE_LOOP);
glVertex2i(x,y);
glVertex2i(x+width,y);
glVertex2i(x+width,y+height);
glVertex2i(x,y+height);
glEnd();
}
void display_cylinder()
{
glutSetWindow(wid1);
glColor3f(1,0,0);
int x=0,y=0,r=50;
for(int i=100; i<150; i+=5)
drawCircle(x,y+i,r);
glFlush();
}
void display_parallelopiped()
{
glutSetWindow(wid2);
glColor3f(0,1,0);
int x=-50,y=-50,w=100,h=150;
for(int i=100; i<150; i+=5)
drawQuad(x+i,y+i,w,h);
glFlush();
}
void myInit()
{
// set to PROJECTION mode
glMatrixMode(GL_PROJECTION);
// set background color
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
// set camera
gluOrtho2D(-WIDTH/2,WIDTH/2,-HEIGHT/2,HEIGHT/2);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
// window 1 parameters
glutInitWindowSize(WIDTH, HEIGHT);
wid1 = glutCreateWindow("Cylinder");
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutDisplayFunc(display_cylinder);
myInit();
// window 2 parameters
glutInitWindowSize(WIDTH, HEIGHT);
wid2 = glutCreateWindow("Parallelopiped");
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutDisplayFunc(display_parallelopiped);
myInit();
glutMainLoop();
return 0;
}