-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathallQuadrics.c
131 lines (115 loc) · 3.6 KB
/
allQuadrics.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
127
128
129
130
131
/*
* allQuadrics.c
* Sumanta
* Draws the four different GLU quadrics.
*
* Press the shift key to change quadrics.
* Press the x, X, y, Y, z, Z keys to rotate the quadric.
*/
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
// Create a pointer to a new quadric object.
GLUquadricObj *qobj;
// Variables used to rotate the object.
static int Xangle = 0, Yangle = 0, Zangle = 0;
// Variable used to choose which quadric to draw.
static int count = 1;
void init(void)
{
// Create the new quadric.
qobj = gluNewQuadric();
// Draw the quadric in wireframe.
gluQuadricDrawStyle(qobj, GLU_LINE);
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity ();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glRotatef ((GLfloat) Zangle, 0.0, 0.0, 1.0);
glRotatef ((GLfloat) Yangle, 0.0, 1.0, 0.0);
glRotatef ((GLfloat) Xangle, 1.0, 0.0, 0.0);
// A sphere of radius 0.75 with the frame consisting of
// 15 longitudinal and 10 latitudinal slices.
if (count == 1) gluSphere(qobj, 0.75, 15, 10);
// A cylinder with base radius 0.5, top radius 0.3, height 1.0
// with the frame consisting of 15 up-down slices parallel to the axis
// and 5 slices perpendicular to the axis.
if (count == 2) gluCylinder(qobj, 0.5, 0.3, 1.0, 15, 5);
// A partial disk with inner radius 0.4, outer radius 1.0, drawn
// from 30 degrees to 300(=30+270) degrees with the frame
// consting of 10 radial slices and 5 circular slices.
if (count == 3) gluPartialDisk(qobj, 0.4, 1.0, 10, 5, 30, 270);
// An entire disk with inner radius 0.4, outer radius 1.0, drawn
// with the frame consting of 10 radial slices and 5 circular slices.
if (count == 4) gluDisk(qobj, 0.4, 1.0, 10, 5);
glFlush ();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case ' ':
if (count == 1) count = 2;
else if (count == 2) count = 3;
else if (count == 3) count = 4;
else count = 1;
glutPostRedisplay();
break;
case 'x':
Xangle = (Xangle + 5) % 360;
glutPostRedisplay();
break;
case 'X':
Xangle = (Xangle - 5) % 360;
glutPostRedisplay();
break;
case 'y':
Yangle = (Yangle + 5) % 360;
glutPostRedisplay();
break;
case 'Y':
Yangle = (Yangle - 5) % 360;
glutPostRedisplay();
break;
case 'z':
Zangle = (Zangle + 5) % 360;
glutPostRedisplay();
break;
case 'Z':
Zangle = (Zangle - 5) % 360;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
printf("Press the shift key to see different quadrics.\n Press x, X, y, Y, z, Z to rotate the quadric.\n");
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}