-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphprim.cpp
205 lines (164 loc) · 4.85 KB
/
graphprim.cpp
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <cmath>
#define PI 3.14159265358979323846
#ifdef WIN32
#include <windows.h>
#include <SHLWAPI.H>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#define X 0.525731112119133606
#define Z 0.850650808352039932
#define SphereList 1
#define CylinderList 2
static float vdata[12][3] = {
{-X,0.0,Z}, {X,0.0,Z}, {-X,0.0,-Z}, {X,0.0,-Z},
{0.0,Z,X}, {0.0,Z,-X}, {0.0,-Z,X}, {0.0,-Z,-X},
{Z,X,0.0}, {-Z,X,0.0}, {Z,-X,0.0}, {-Z,-X,0.0}
};
static int tindices[20][3] = {
{0,4,1}, {0,9,4}, {9,5,4}, {4,5,8}, {4,8,1},
{8,10,1}, {8,3,10}, {5,3,8}, {5,2,3}, {2,7,3},
{7,10,3}, {7,6,10}, {7,11,6}, {11,0,6}, {0,1,6},
{6,1,10}, {9,0,11}, {9,11,2}, {9,2,5}, {7,2,11}
};
// normalize a vector of non-zero length
void normalize(float v[3]) {
float d = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
v[0] /= d; v[1] /= d; v[2] /= d;
}
/* recursively subdivide face `depth' times */
/* and draw the resulting triangles */
void subdivide(GLfloat v1[3], GLfloat v2[3], GLfloat v3[3], int depth)
{
GLfloat v12[3], v23[3], v31[3];
int i;
if (depth == 0) {
glBegin(GL_TRIANGLES);
glNormal3fv(v1); glVertex3fv(v1);
glNormal3fv(v2); glVertex3fv(v2);
glNormal3fv(v3); glVertex3fv(v3);
glEnd();
return;
}
/* calculate midpoints of each side */
for (i = 0; i < 3; i++) {
v12[i] = (v1[i]+v2[i])/2.0;
v23[i] = (v2[i]+v3[i])/2.0;
v31[i] = (v3[i]+v1[i])/2.0;
}
/* extrude midpoints to lie on unit sphere */
normalize(v12);
normalize(v23);
normalize(v31);
/* recursively subdivide new triangles */
subdivide(v1 , v12, v31, depth-1);
subdivide(v2 , v23, v12, depth-1);
subdivide(v3 , v31, v23, depth-1);
subdivide(v12, v23, v31, depth-1);
}
int olddepth = -1;
void CreateSphereList(int depth) {
glNewList(SphereList, GL_COMPILE);
for (int i = 0; i < 20; i++) {
subdivide(&vdata[tindices[i][0]][0],
&vdata[tindices[i][1]][0],
&vdata[tindices[i][2]][0],
depth);
}
glEndList();
}
void Sphere(GLdouble radius, GLint slices, GLint stacks ) {
int depth = slices/16;
if (depth != olddepth) {
CreateSphereList(depth);
}
glScalef(radius,radius,radius);
glCallList(SphereList);
}
/*
#define MAXsl 140
GLdouble sinez[MAXsl], cosez[MAXsl];
int SphereLastSlices = 0;
void Sphere(GLdouble radius, GLint slices, GLint stacks ) {
GLdouble x, y, z;
GLint i, j, imax;
if(slices!=SphereLastSlices) {
for (j=0;j<=slices;j++) {
sinez[j]=sin((GLdouble)j * (PI+PI) / (GLdouble) slices);
cosez[j]=cos((GLdouble)j * (PI+PI) / (GLdouble) slices);
}
SphereLastSlices=slices;
}
glBegin( GL_TRIANGLE_FAN );
glNormal3d( 0.0, 0.0, 1.0 );
glVertex3d( 0.0, 0.0, radius );
for (j=0;j<=slices;j++) {
x = -sinez[j] * sinez[1];
y = cosez[j] * sinez[1];
z = cosez[1];
glNormal3d( x, y, z );
glVertex3d( x*radius, y*radius, z*radius );
}
glEnd();
// draw intermediate stacks as quad strips
for (i=1,imax = stacks/2;i<imax;i++) {
glBegin( GL_QUAD_STRIP );
for (j=0;j<=slices;j++) {
x = -sinez[j] * sinez[i];
y = cosez[j] * sinez[i];
z = cosez[i];
glNormal3d( x, y, z );
glVertex3d( x*radius, y*radius, z*radius );
x = -sinez[j] * sinez[i+1];
y = cosez[j] * sinez[i+1];
z = cosez[i+1];
glNormal3d( x, y, z );
glVertex3d( x*radius, y*radius, z*radius );
}
glEnd();
}
glBegin( GL_TRIANGLE_FAN );
glNormal3d( 0.0, 0.0, -1.0 );
glVertex3d( 0.0, 0.0, -radius );
for (i=slices/2-1, j=slices;j>=0;j--) {
x = -sinez[j] * sinez[i];
y = cosez[j] * sinez[i];
z = cosez[i];
glNormal3d( x, y, z );
glVertex3d( x*radius, y*radius, z*radius );
}
glEnd();
}*/
int oldslices = -1;
void Cylinder(double baseRadius, double topRadius,
double height, int slices) {
if (slices < 3) slices = 3;
if (slices != oldslices) { // create new cylinder list
GLdouble x1c, y1c, x2c, y2c;
double da = 2.0*PI / slices;
double dr = (topRadius-baseRadius);
double nz = -dr;
glNewList(CylinderList, GL_COMPILE);
for (int i=0; i<slices; i++) {
x1c = -sin( i *da);
y1c = cos( i *da);
x2c = -sin((i+1)*da);
y2c = cos((i+1)*da);
double r = baseRadius;
glBegin( GL_QUAD_STRIP );
glNormal3d( x1c , nz , y1c);
glVertex3d( x1c*r, 0 , y1c*r);
glNormal3d( x2c , nz , y2c);
glVertex3d( x2c*r, 0 , y2c*r);
r += dr;
glNormal3d( x1c , nz , y1c);
glVertex3d( x1c*r, 1.0, y1c*r);
glNormal3d( x2c , nz , y2c);
glVertex3d( x2c*r, 1.0, y2c*r);
glEnd();
}
glEndList();
}
glScalef(1.0, height, 1.0);
glCallList(CylinderList);
}