-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurve.h
275 lines (233 loc) · 6.62 KB
/
curve.h
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const double PI = 3.14159265389;
/* GLUT callback Handlers */
int anglex= 0, angley = 0, anglez = 0; //rotation angles
int window;
int wired=0;
int shcpt=1;
int animat = 0;
const int L=34;
const int dgre=3;
int ncpt=L+1;
int clikd=0;
//const int nt = 40; //number of slices along x-direction
//const int ntheta = 20;
GLfloat ctrlpoints[L+1][3] =
{
{3.975,0.025,0},
{3.85,0.3,0},
{3.475,0.325,0},
{3.325,0.175,0},
{3.3,-0.05,0},
{3.4,-0.35,0},
{3.65,-0.45,0},
{3.85,-0.425,0},
{3.975,-0.175,0},
{4,0.1,0},
{3.95,0.275,0},
{3.75,0.375,0},
{3.4,0.375,0},
{3.275,0.15,0},
{3.275,-0.15,0},
{3.425,-0.45,0},
{3.75,-0.475,0},
{3.95,-0.35,0},
{4.025,-0.1,0},
{4.025,0.325,0},
{3.65,0.35,0}, //ufo
{-4.71111,40.6982,0},
{-4.62222,40.3491,0},
{-4.51556,39.7091,0},
{-4.51556,38.6909,0},
{-4.71111,38.4,0},
{-5.06667,38.1091,0},
{-5.31556,38.0509,0},
{-5.49333,38.0509,0}, //ring
{-5.93778,38.7782,0},
{-5.70667,38.1964,0},
{-5.08444,37.7891,0},
{-4.33778,37.7018,0},
{-3.87556,38.3418,0},
{-3.50222,39.1564,0}, //rock
};
float wcsClkDn[3],wcsClkUp[3];
///////////////////////////////
class point1
{
public:
point1()
{
x=0;
y=0;
}
int x;
int y;
} clkpt[2];
int curve_flag=0;
GLint viewport[4]; //var to hold the viewport info
GLdouble modelview[16]; //var to hold the modelview info
GLdouble projection[16]; //var to hold the projection matrix info
//
////////////////////////////
//void scsToWcs(float sx,float sy, float wcsv[3] );
//void processMouse(int button, int state, int x, int y);
//void matColor(float kdr, float kdg, float kdb, float shiny, int frnt_Back=0, float ambFactor=1.0, float specFactor=1.0);
/////////////////////////////
void scsToWcs(float sx,float sy, float wcsv[3] )
{
GLfloat winX, winY, winZ; //variables to hold screen x,y,z coordinates
GLdouble worldX, worldY, worldZ; //variables to hold world x,y,z coordinates
glGetDoublev( GL_MODELVIEW_MATRIX, modelview ); //get the modelview info
glGetDoublev( GL_PROJECTION_MATRIX, projection ); //get the projection matrix info
glGetIntegerv( GL_VIEWPORT, viewport ); //get the viewport info
winX = sx;
winY = (float)viewport[3] - (float)sy;
winZ = 0;
//get the world coordinates from the screen coordinates
gluUnProject( winX, winY, winZ, modelview, projection, viewport, &worldX, &worldY, &worldZ);
wcsv[0]=worldX;
wcsv[1]=worldY;
wcsv[2]=worldZ;
}
void processMouse(int button, int state, int x, int y)
{
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
{
if(curve_flag!=1)
{
curve_flag=1;
clkpt[0].x=x;
clkpt[0].y=y;
}
scsToWcs(clkpt[0].x,clkpt[0].y,wcsClkDn);
//cout<<"\nD: "<<x<<" "<<y<<" wcs: "<<wcsClkDn[0]<<" "<<wcsClkDn[1];
cout<<"{"<<wcsClkDn[0]<<","<<wcsClkDn[1]<<",0},"<<endl;
}
else if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
{
if (curve_flag==1)
{
clkpt[1].x=x;
clkpt[1].y=y;
curve_flag=0;
}
float wcs[3];
scsToWcs(clkpt[1].x,clkpt[1].y,wcsClkUp);
//cout<<"\nU: "<<x<<" "<<y<<" wcs: "<<wcsClkUp[0]<<" "<<wcsClkUp[1];
clikd=!clikd;
}
}
//control points
long long nCr(int n, int r)
{
if(r > n / 2) r = n - r; // because C(n, r) == C(n, n - r)
long long ans = 1;
int i;
for(i = 1; i <= r; i++)
{
ans *= n - r + i;
ans /= i;
}
return ans;
}
//polynomial interpretation for N points
void BezierCurve ( double t, float xy[2], int first, int last)
{
double y=0;
double x=0;
t=t>1.0?1.0:t;
for(int i=0, n=first; i<=(last-first), n<=last; i++, n++)
{
int ncr=nCr((last-first),i);
double oneMinusTpow=pow(1-t,double((last-first)-i));
double tPow=pow(t,double(i));
double coef=oneMinusTpow*tPow*ncr;
x+=coef*ctrlpoints[n][0];
y+=coef*ctrlpoints[n][1];
}
xy[0] = float(x);
xy[1] = float(y);
//return y;
}
///////////////////////
void setNormal(GLfloat x1, GLfloat y1,GLfloat z1, GLfloat x2, GLfloat y2,GLfloat z2, GLfloat x3, GLfloat y3,GLfloat z3)
{
GLfloat Ux, Uy, Uz, Vx, Vy, Vz, Nx, Ny, Nz;
Ux = x2-x1;
Uy = y2-y1;
Uz = z2-z1;
Vx = x3-x1;
Vy = y3-y1;
Vz = z3-z1;
Nx = Uy*Vz - Uz*Vy;
Ny = Uz*Vx - Ux*Vz;
Nz = Ux*Vy - Uy*Vx;
glNormal3f(-Nx,-Ny,-Nz);
}
void bottleBezier(int first, int last, int nt, int ntheta)
{
int i, j;
float x, y, z, r; //current coordinates
float x1, y1, z1, r1; //next coordinates
float theta;
const float startx = 0, endx = ctrlpoints[L][0];
//number of angular slices
const float dx = (endx - startx) / nt; //x step size
const float dtheta = 2*PI / ntheta; //angular step size
float t=0;
float dt=1.0/nt;
float xy[2];
BezierCurve( t, xy, first, last);
x = xy[0];
r = xy[1];
//rotate about z-axis
float p1x,p1y,p1z,p2x,p2y,p2z;
for ( i = 0; i < nt; ++i ) //step through x
{
theta = 0;
t+=dt;
BezierCurve( t, xy, first, last);
x1 = xy[0];
r1 = xy[1];
//draw the surface composed of quadrilaterals by sweeping theta
glBegin( GL_QUAD_STRIP );
for ( j = 0; j <= ntheta; ++j )
{
theta += dtheta;
double cosa = cos( theta );
double sina = sin ( theta );
y = r * cosa;
y1 = r1 * cosa; //current and next y
z = r * sina;
z1 = r1 * sina; //current and next z
//edge from point at x to point at next x
glVertex3f (x, y, z);
if(j>0)
{
setNormal(p1x,p1y,p1z,p2x,p2y,p2z,x, y, z);
}
else
{
p1x=x;
p1y=y;
p1z=z;
p2x=x1;
p2y=y1;
p2z=z1;
}
glVertex3f (x1, y1, z1);
//forms quad with next pair of points with incremented theta value
}
glEnd();
x = x1;
r = r1;
} //for i
}
void showControlPoints()
{
glPointSize(5.0);
glColor3f(1.0, 0.0, 1.0);
glBegin(GL_POINTS);
for (int i = 0; i <=L; i++)
glVertex3fv(&ctrlpoints[i][0]);
glEnd();
}