-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolygon3d.cpp
393 lines (340 loc) · 10.8 KB
/
polygon3d.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#include "polygon3d.h"
Polygon3d::Polygon3d(){
drawTesselate=true;
color[0]=0;
color[1]=0;
color[2]=0;
color[3]=0;
scale = 1;
maxRadius = 0;
texture = 0;
hasTex = false;
}
Polygon3d::Polygon3d(double x,double y,double z){
this->center = Point(x,y,z);
drawTesselate=true;
color[0]=0;
color[1]=0;
color[2]=0;
color[3]=0;
scale = 1;
maxRadius = 0;
texture = 0;
hasTex = false;
}
Polygon3d::Polygon3d(std::vector<Point>& points){
this->vertexList = points;
drawTesselate=true;
color[0]=0;
color[1]=0;
color[2]=0;
color[3]=0;
scale = 1;
maxRadius = 0;
texture = 0;
hasTex = false;
}
Polygon3d::Polygon3d(Point pnt, std::vector<Point>& points){
this->center = pnt;
this->vertexList = points;
drawTesselate = true;
color[0]=0;
color[1]=0;
color[2]=0;
color[3]=0;
scale = 1;
maxRadius = 0;
texture = 0;
hasTex = false;
}
Polygon3d::Polygon3d(Triangle &tri){
drawTesselate=true;
color[0]=0;
color[1]=0;
color[2]=0;
color[3]=0;
scale = 1;
maxRadius = 0;
texture = 0;
hasTex = false;
this->vertexList.push_back(tri.p1);
this->vertexList.push_back(tri.p2);
this->vertexList.push_back(tri.p3);
this->vertexList.push_back(tri.p1);
}
Polygon3d::Polygon3d(const Polygon3d& other){
this->center = other.center;
this->rotation=other.rotation;
this->scale=other.scale;
this->velocity=other.velocity;
this->rotationSpeed=other.rotationSpeed;
this->vertexList=other.vertexList;
this->drawTesselate=other.drawTesselate;
for(int x=0;x<4;x++)this->color[x] = other.color[x];
this->maxRadius = 0;
this->texture = other.texture;
this->hasTex = other.hasTex;
this->vertexTextureList = other.vertexTextureList;
}
void Polygon3d::setRotation(Vector& rot){
this->rotation=rot;
}
Vector Polygon3d::getRotation(){
return this->rotation;
}
void Polygon3d::setScale(double sc){
this->scale = sc;
}
double Polygon3d::getScale(){
return this->scale;
}
void Polygon3d::setCenter(Point pp){
this->center = pp;
}
Point Polygon3d::getCenter(){
return this->center;
}
void Polygon3d::setColor(GLubyte red,GLubyte green, GLubyte blue){
this->color[0]=red;
this->color[1]=green;
this->color[2]=blue;
this->color[3]=255;
}
void Polygon3d::setColor(GLubyte red,GLubyte green, GLubyte blue, GLubyte alpha){
this->color[0]=red;
this->color[1]=green;
this->color[2]=blue;
this->color[3]=alpha;
}
GLubyte Polygon3d::getColor(int channel){
return this->color[channel];
}
void Polygon3d::setTesselation(bool tes){
this->drawTesselate=tes;
}
bool Polygon3d::getTesselation(){
return this->drawTesselate;
}
void Polygon3d::setVelocity(double x, double y, double z){
this->velocity = Vector(x,y,z);
}
void Polygon3d::setVelocity(Vector& x){
this->velocity = x;
}
Vector Polygon3d::getVelocity(){
return this->velocity;
}
void Polygon3d::setRotationSpeed(Vector& rot){
this->rotationSpeed = rot;
}
Vector Polygon3d::getRotationSpeed(){
return this->rotationSpeed;
}
double Polygon3d::getMaxRadius(){
if(this->maxRadius == 0){
//we are going to calculate the max distance from our center
//this lets us remove a lot of excess calculations for polygons that are just too far away
for(int x=0; x<this->numPoints(); x++){
double temp=0;
temp += pow(this->vertexList[x].x,2);
temp += pow(this->vertexList[x].y,2);
temp += pow(this->vertexList[x].z,2);
temp = sqrt(temp);
if(temp > this->maxRadius) this->maxRadius = temp;
}
}
return this->maxRadius;
}
void Polygon3d::setTexture(GLuint tex){
this->texture = tex;
this->hasTex=true;
}
GLuint Polygon3d::getTexture(){
return this->texture;
}
std::vector<Point>& Polygon3d::getTexturePoints(){
return this->vertexTextureList;
}
Polygon3d Polygon3d::getWorldPoints(){
//Herron: returns polygon in world coordinates
Polygon3d poly(this->getPoints());
poly.drawTesselate = this->drawTesselate;
poly.color[0] = this->color[0];
poly.color[1] = this->color[1];
poly.color[2] = this->color[2];
poly.color[3] = this->color[3];
poly.texture = this->texture ;
poly.hasTex = this->hasTex ;
poly.vertexTextureList = this->vertexTextureList ;
std::vector<Point> &worldCoords = poly.getPoints();
for(int i = 0; i < this->vertexList.size(); i++){
worldCoords[i] = worldCoords[i].rotatePoint(this->rotation.x, 1, 0, 0 );
worldCoords[i] = worldCoords[i].rotatePoint(this->rotation.y, 0, 1, 0 );
worldCoords[i] = worldCoords[i].rotatePoint(this->rotation.z, 0, 0, 1 );
worldCoords[i] = worldCoords[i].translatePoint(this->center[0], this->center[1], this->center[2]);
worldCoords[i] = worldCoords[i].scalePoint(this->scale, this->scale, this->scale);
}
return poly;
}
Point& Polygon3d::getTexturePos(int index){
return this->vertexTextureList[index];
}
bool Polygon3d::hasTexture(){
return this->hasTex;
}
void Polygon3d::removeTexture(){
this->hasTex=false;
}
int Polygon3d::numPoints()const{
return this->vertexList.size();
}
std::vector<Point>& Polygon3d::getPoints(){
return this->vertexList;
}
Point& Polygon3d::operator[](int x){
return this->vertexList[x];
}
Polygon3d& Polygon3d::operator=(Polygon3d other){
this->vertexList.clear();
this->vertexList.reserve(other.numPoints());
for(int x=0; x<other.numPoints(); x++){
Point temp = other[x];
this->vertexList.push_back(temp);
}
this->center = other.center;
this->rotation = other.rotation;
this->scale = other.scale;
this->drawTesselate = other.drawTesselate;
for(int x=0;x<4;x++)this->color[x] = other.color[x];
this->velocity=other.velocity;
this->rotationSpeed=other.rotationSpeed;
this->maxRadius=0; // special little optimization for collision checking
this->texture = other.texture;
this->hasTex = other.hasTex;
this->vertexTextureList = other.vertexTextureList;
return *this;
}
Polygon3d Polygon3d::getTransform(){ // get the transform of the points of the polygon to where they should be
return getWorldPoints();
}
void Polygon3d::recenter(){} // moves the center of the polygon to be at the centroid of the shape but does not change its position
Vector Polygon3d::getNormal(){
std::vector<Point> worldPoints = getTransform().getPoints();
this->normal = Vector(worldPoints.at(0),worldPoints.at(1)).cross(Vector(worldPoints.at(1),worldPoints.at(2)));
return this->normal;
}
void Polygon3d::draw(){
if(this->numPoints()<2)return;
glColor4ub(this->color[0],this->color[1],this->color[2],this->color[3]);
glPushMatrix();
//translate
glTranslated(this->center[0],this->center[1],this->center[2]);
//scale
glScaled(this->scale,this->scale,this->scale);
//rotate
glRotated(this->rotation.x,1,0,0);
glRotated(this->rotation.y,0,1,0);
glRotated(this->rotation.z,0,0,1);
//set the normal of the plane
Vector normal = Vector(vertexList[0],vertexList[1]).cross(Vector(vertexList[1],vertexList[2]));
glNormal3d(normal.x,normal.y,normal.z);
// glBegin(GL_LINES); // debug to show the normal of each plane
// glVertex3d(0,0,0);
// glVertex3d(normal.x,normal.y,normal.z);
// glEnd();
if(this->hasTexture()){
glBindTexture(GL_TEXTURE_2D,this->getTexture());
}
GLUtesselator* tessObj = gluNewTess();
if(tessObj == NULL) return; // encountered an error and so we are done
if(this->drawTesselate){
gluTessProperty(tessObj,GLU_TESS_BOUNDARY_ONLY,GL_FALSE); // dont only draw the outer edges but the whole interior
}else{
gluTessProperty(tessObj,GLU_TESS_BOUNDARY_ONLY,GL_TRUE); // only draw the outer edges
}
gluTessCallback(tessObj, GLU_TESS_BEGIN, (void(*)())glBegin);
// gluTessCallback(tessObj, GLU_TESS_VERTEX, (void(*)())glVertex3dv);
gluTessCallback(tessObj, GLU_TESS_VERTEX_DATA, (void(*)())__secretVertexDrawingFunction);
gluTessCallback(tessObj, GLU_TESS_END, (void(*)())glEnd);
//add our points in to the object to be tesselated
gluTessBeginPolygon(tessObj,this); // start tesselating and have it able to hand us a pointer to this object
gluTessBeginContour(tessObj);
for(long x=0;x<this->vertexList.size()-1;x++){
gluTessVertex(tessObj,(double*)&this->vertexList[x],(long*)x); // give the tesselator a point and us an index to the pixel
// gluTessVertex(tessObj,(double*)&this->vertexList[x],(double*)&this->vertexList[x]);
}
gluTessEndContour(tessObj);
gluTessEndPolygon(tessObj);
gluDeleteTess(tessObj);
if(this->hasTexture()){
//reset to the default empty texture
glBindTexture(GL_TEXTURE_2D, 0);
}
glPopMatrix();
}
void Polygon3d::drawLines(){
//remember what it was before
auto temp = this->drawTesselate;
this->drawTesselate=false;
this->draw();
//put it back to the way it was before
this->drawTesselate = temp;
}
void Polygon3d::draw_static(){
if(this->numPoints()<2)return;
glColor4ub(this->color[0],this->color[1],this->color[2],this->color[3]);
//set the normal of the plane
Vector normal = Vector(vertexList[0],vertexList[1]).cross(Vector(vertexList[1],vertexList[2]));
glNormal3d(normal.x,normal.y,normal.z);
// glBegin(GL_LINES); // debug to show the normal of each plane
// glVertex3d(0,0,0);
// glVertex3d(normal.x,normal.y,normal.z);
// glEnd();
if(this->hasTexture()){
glBindTexture(GL_TEXTURE_2D,this->getTexture());
}
GLUtesselator* tessObj = gluNewTess();
if(tessObj == NULL) return; // encountered an error and so we are done
if(this->drawTesselate){
gluTessProperty(tessObj,GLU_TESS_BOUNDARY_ONLY,GL_FALSE); // dont only draw the outer edges but the whole interior
}else{
gluTessProperty(tessObj,GLU_TESS_BOUNDARY_ONLY,GL_TRUE); // only draw the outer edges
}
gluTessCallback(tessObj, GLU_TESS_BEGIN, (void(*)())glBegin);
// gluTessCallback(tessObj, GLU_TESS_VERTEX, (void(*)())glVertex3dv);
gluTessCallback(tessObj, GLU_TESS_VERTEX_DATA, (void(*)())__secretVertexDrawingFunction);
gluTessCallback(tessObj, GLU_TESS_END, (void(*)())glEnd);
//add our points in to the object to be tesselated
gluTessBeginPolygon(tessObj,this); // start tesselating and have it able to hand us a pointer to this object
gluTessBeginContour(tessObj);
for(long x=0;x<this->vertexList.size()-1;x++){
gluTessVertex(tessObj,(double*)&this->vertexList[x],(long*)x); // give the tesselator a point and us an index to the pixel
// gluTessVertex(tessObj,(double*)&this->vertexList[x],(double*)&this->vertexList[x]);
}
gluTessEndContour(tessObj);
gluTessEndPolygon(tessObj);
gluDeleteTess(tessObj);
if(this->hasTexture()){
//reset to the default empty texture
glBindTexture(GL_TEXTURE_2D, 0);
}
}
void Polygon3d::__secretVertexDrawingFunction(void *data, void *polygon){
//for every vertex made by the tesselator, we get called here
//we are passed 2 things
// - data which is really just and int in disguise
// - pointer to the polygon being tesselated
//we use the int as the index to the vertex being drawn
//we use the polygon to get the position of the vertex
long index = (long)data;
Polygon3d* poly=(Polygon3d*)polygon;
if(poly->hasTexture() && poly->getTesselation()){
Point& p = poly->getTexturePos(index);
glTexCoord2d(p.x,p.y);
}
glVertex3dv((double*)&(*poly)[index]);
}
void Polygon3d::update(){
this->center += this->velocity;
this->rotation += this->rotationSpeed;
}