-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBezierPatch4.cpp
321 lines (280 loc) · 8.25 KB
/
BezierPatch4.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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "BezierPatch4.h"
// default constructor
BezierPatch4::BezierPatch4()
{
amplitude = 0;
// convert control points into array of floats
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
Vector3 currentVec(i,0,j);
ctrlpoints[i][j][0] = currentVec.get(0);
ctrlpoints[i][j][1] = currentVec.get(1);
ctrlpoints[i][j][2] = currentVec.get(2);
// set initial color to white
colors[i][j][0] = 0;
colors[i][j][1] = 0;
colors[i][j][2] = 0;
colors[i][j][3] = 0;
}
}
// glEnable(GL_COLOR_MATERIAL);
glEnable(GL_MAP2_VERTEX_3);
// glEnable(GL_MAP2_COLOR_4);
glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
glEnable(GL_AUTO_NORMAL);
}
// constructor that takes the corners and makes a flat patch (Y is always 0)
BezierPatch4::BezierPatch4(Vector3 topLeft, Vector3 topRight, Vector3 lowerBound)
{
// amplitude is the middle (average of the 3 points' y)
amplitude = (topLeft.get(1)+topRight.get(1)+lowerBound.get(1))/3.0;
// interpolates the points to create the surface
Vector3 deltaX(topRight - topLeft); // how much the x direction changes
deltaX.scale(1.0 / 3);
Vector3 deltaZ(lowerBound - topLeft); // how much the z direction changes
deltaZ.scale(1.0 / 3);
// convert control points into array of floats
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
// find current interpolated point
Vector3 currX(deltaX);
currX.scale(i);
Vector3 currZ(deltaZ);
currZ.scale(j);
Vector3 currentVec = topLeft + currX + currZ;
// set control point accordingly
ctrlpoints[i][j][0] = currentVec.get(0);
ctrlpoints[i][j][1] = currentVec.get(1);
ctrlpoints[i][j][2] = currentVec.get(2);
// set initial color to white
colors[i][j][0] = 0;
colors[i][j][1] = 0;
colors[i][j][2] = 0;
colors[i][j][3] = 0;
}
}
// glEnable(GL_COLOR_MATERIAL);
glEnable(GL_MAP2_VERTEX_3);
// glEnable(GL_MAP2_COLOR_4);
glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
}
// Bezier patch constructor
BezierPatch4::BezierPatch4(Vector3 * controlPoints)
{
// convert control points into array of floats
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
Vector3 currentVec = controlPoints[i*4 + j];
ctrlpoints[i][j][0] = currentVec.get(0);
ctrlpoints[i][j][1] = currentVec.get(1);
ctrlpoints[i][j][2] = currentVec.get(2);
// set initial color to white
colors[i][j][0] = 0;
colors[i][j][1] = 0;
colors[i][j][2] = 0;
colors[i][j][3] = 0;
// add to amplitude
amplitude += currentVec.get(1);
}
}
// amplitude is an average of all the points
amplitude /= NUM_CONTROL_POINTS;
glEnable(GL_MAP2_VERTEX_3);
// glEnable(GL_MAP2_COLOR_4);
glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
}
BezierPatch4::~BezierPatch4()
{
}
// takes array to 16 Vector3s and changes the control points
void BezierPatch4::setControlPoints(Vector3* controlPoints)
{
}
// changes the value of a single control point
void BezierPatch4::setControlPoint(int m, int n, Vector3 vec)
{
ctrlpoints[m][n][0] = vec.get(0);
ctrlpoints[m][n][1] = vec.get(1);
ctrlpoints[m][n][2] = vec.get(2);
}
// changes the value of a single control point
Vector3 BezierPatch4::getControlPoint(int m, int n)
{
return Vector3(ctrlpoints[m][n][0], ctrlpoints[m][n][1], ctrlpoints[m][n][2]);
}
// takes a color map and sets each points color based on it's amp
void BezierPatch4::setColors(ColorGradient* colorMap, double yScale)
{
// loop through every control point
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
// set the color based on the yscale
Vector4 newColor = colorMap->getColor(ctrlpoints[i][j][1] / yScale);
/* colors[i][j][0] = newColor[0];
colors[i][j][1] = newColor[1];
colors[i][j][2] = newColor[2];
colors[i][j][3] = newColor[3]; */
colors[i][j][0] = 0;
colors[i][j][1] = 1;
colors[i][j][2] = 1;
colors[i][j][3] = 1;
}
}
}
// changes the color of a single control point
void BezierPatch4::setColor(int m, int n, Vector4 vec)
{
colors[m][n][0] = vec.get(0);
colors[m][n][1] = vec.get(1);
colors[m][n][2] = vec.get(2);
colors[m][n][3] = vec.get(3);
}
// gets the color of a single control point
Vector4 BezierPatch4::getColor(int m, int n)
{
return Vector4(colors[m][n][0], colors[m][n][1], colors[m][n][2], colors[m][n][3]);
}
/*
* copyColor
* copies the color and the respective y values of
* the given patch to the current patch.
*/
void BezierPatch4::copyColor(BezierPatch4 other)
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
colors[i][j][0] = other.getColor(i, j)[0];
colors[i][j][1] = other.getColor(i, j)[1];
colors[i][j][2] = other.getColor(i, j)[2];
colors[i][j][3] = other.getColor(i, j)[3];
}
}
}
// draws the bezier curve
void BezierPatch4::draw(Matrix4 C, Frustum F, bool checkCulling)
{
// load control points
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,
0, 1, 12, 4, &ctrlpoints[0][0][0]);
/* glMap2f(GL_MAP2_COLOR_4, 0, 1, 4, 4,
0, 1, 16, 4, &colors[0][0][0]);*/
// draw curve
glLoadMatrixd(C.getPointer());
glEvalMesh2(GL_FILL, 0, 20, 0, 20);
glPopMatrix();
}
// TODO: finish writing this
void BezierPatch4::computeBoundingSphere(Matrix4 C)
{
}
/*
* copyAmplitude
* copies the amplitude and the respective y values of
* the given patch to the current patch.
*/
void BezierPatch4::copyAmplitude(BezierPatch4 other)
{
setAmplitude(other.getAmplitude());
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
ctrlpoints[i][j][1] = other.getControlPoint(i, j).get(1);
}
}
}
/*
* setAmplitude
*
* Sets the amplitude and all the y values of the patch to
* the given amplitude
*/
void BezierPatch4::setAmplitude(double amp)
{
amplitude = amp;
// set all of the y values of the current patch to amp
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
ctrlpoints[i][j][1] = amp;
}
double BezierPatch4::getAmplitude()
{
return amplitude;
}
/*
* join
*
* Connects the current patch to three other patches assuming that the calling
* object is the lower left corner
*/
void BezierPatch4::join(BezierPatch4* top, BezierPatch4* right, double edgeAmp)
{
// join top edge
for (int i = 0; i < 4; i++)
{
// bottom of the top patch
Vector3 topPoint = top->getControlPoint(i, 3);
// connect current patch to bottom of top
setControlPoint(i, 0, topPoint);
}
// join right edge
if (right)
{
for (int i = 1; i <= 3; i++)
{
// left of the right patch
Vector3 rightPoint = right->getControlPoint(0, i);
// right of current
Vector3 currPoint = getControlPoint(3,i);
// set both points y coord to the midpoint
// right->setControlPoint(0, i, Vector3(rightPoint.get(0), currPoint.get(1), rightPoint.get(2)));
setControlPoint(3, i, Vector3(currPoint.get(0), rightPoint.get(1), currPoint.get(2)));
// C1 version
// left of the right patch
rightPoint = right->getControlPoint(1, i);
// right of left patch
currPoint = getControlPoint(2, i);
// float midpoint = (rightPoint.get(1) + currPoint.get(1))/2
// right->setControlPoint(1, i, Vector3(rightPoint.get(0), currPoint.get(1), rightPoint.get(2)));
setControlPoint(2, i, Vector3(currPoint.get(0), rightPoint.get(1), currPoint.get(2)));
}
}
else{
// set the far right edge amplitude
for (int i = 1; i <= 3; i++)
{
Vector3 currPoint = getControlPoint(3, i);
// set both points y coord to the midpoint
setControlPoint(3, i, Vector3(currPoint.get(0), edgeAmp, currPoint.get(2)));
}
}
}
// connects the current patch to the adjacent one with C1 continuity
void BezierPatch4::joinCont(BezierPatch4* top, BezierPatch4* left)
{
// join top edge
for (int i = 0; i < 4; i++)
{
// bottom of the top patch
Vector3 topPoint = top->getControlPoint(i, 2);
// right of left patch
Vector3 currPoint = getControlPoint(i,1);
setControlPoint(i,1, Vector3(currPoint.get(0), topPoint.get(1), currPoint.get(2)));
}
}