-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnvironment.cpp
More file actions
369 lines (346 loc) · 9.54 KB
/
Environment.cpp
File metadata and controls
369 lines (346 loc) · 9.54 KB
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
#include "Environment.h"
/*
* Constructor
*/
Environment::Environment(){}
/*
* Used to initially generate the invironment into the environmentDL variable
*/
void Environment::generateEnvironmentDL(std::ifstream& inFile) {
environmentDL = glGenLists( 1 );
// Tell openGL to begin displaying lists
glNewList( environmentDL, GL_COMPILE );
// Draw the figures
glPushMatrix(); {
placeObjectsInEnvironment(inFile);
drawCurve();
drawSurface();
trackBox();
trackRoad();
}; glPopMatrix();
// Tell openGL to end displayiung lists
glEndList();
}
/*
* Places the tree and hut in environement according to the the input file
*/
void Environment::placeObjectsInEnvironment(std::ifstream& inFile){
if(inFile.is_open() == false){
std::cout << "Couldn't read in File" << std::endl;
exit(1);
}
int numObjects = 0;
int objType = 0;
float objectX = 0, objectY = 0, objectZ = 0;
float orientX = 0, orientY = 0, orientZ = 0;
float objSize = 0;
//char comments;
inFile >> numObjects;
for(int i = 0; i < numObjects ; i++){
std::string inValue;
getline(inFile, inValue, ',');
objType = atoi(inValue.c_str());
getline(inFile, inValue, ',');
objectX = atof(inValue.c_str());
getline(inFile, inValue, ',');
objectY = atof(inValue.c_str());
getline(inFile, inValue, ',');
objectZ = atof(inValue.c_str());
getline(inFile, inValue, ',');
orientX = atof(inValue.c_str());
getline(inFile, inValue, ',');
orientY = atof(inValue.c_str());
getline(inFile, inValue, ',');
orientZ = atof(inValue.c_str());
getline(inFile,inValue, '\n');
objSize = atof(inValue.c_str());
glPushMatrix();
{
float uVector = (0.01)* objectX + 1;
float vVector = (-0.01) * objectZ + 1;
int bezierListIndex = 2*((int)floor(vVector)) + (int)floor(uVector);
Point surfPos = surf[bezierListIndex].evaluateSurface(uVector - floor(uVector) ,vVector - floor(vVector));
Point axis=surf[bezierListIndex].rotationAxis(uVector - floor(uVector) ,vVector - floor(vVector));
float surfAngle=surf[bezierListIndex].rotationAngle(uVector - floor(uVector) ,vVector - floor(vVector));
glTranslatef(objectX,( surfPos.getY()-1.95)*100.0/12, objectZ);
glRotatef(surfAngle,axis.getX(), 0, axis.getZ());
glScalef(objSize, objSize, objSize);
switch(objType){
case 0:
drawTree();
break;
case 1:
drawHut();
break;
default:
drawTree();
break;
}
}
glPopMatrix();
}
}
/*
* Draws a flat grid. Not used in this project
*/
void Environment::drawGrid() {
/*
* We will get to why we need to do this when we talk about lighting,
* but for now whenever we want to draw something with an OpenGL
* Primitive - like a line, quad, point - we need to disable lighting
* and then reenable it for use with the GLUT 3D Primitives.
*/
glDisable( GL_LIGHTING );
glColor3f(1,1,1);
glLineWidth(1);
glBegin(GL_LINES);
for(int i = -100; i <= 100; i+=5){
glVertex3f( i, 0, -100);
glVertex3f( i, 0, 100);
glVertex3f(100, 0, i);
glVertex3f(-100, 0, i);
}
glEnd();
/*
* As noted above, we are done drawing with OpenGL Primitives, so we
* must turn lighting back on.
*/
glEnable( GL_LIGHTING );
}
/*
* Draws a flat ground. Not used in this project
*/
void Environment::drawGround(){
glDisable( GL_LIGHTING );
glColor3f(1,1,1);
glPushMatrix();
{
glBegin(GL_QUADS);
glVertex3f(-100,0.1,-100);
glVertex3f(-100,0.1,100);
glVertex3f(100, 0.1, 100);
glVertex3f(100, 0.1, -100);
glEnd();
}
glPopMatrix();
glEnable( GL_LIGHTING);
}
/*
* Draws a tree object
*/
void Environment::drawTree(){
glPushMatrix();
{
glPushMatrix();
glColor3f( 0.647059 , 0.164706 , 0.164706);
glRotatef(-90,1,0,0);
GLUquadricObj *trunk;
trunk = gluNewQuadric();
gluQuadricDrawStyle(trunk, GLU_FILL);
gluCylinder(trunk, 1,1,10,50,50);
gluDeleteQuadric(trunk);
glPopMatrix();
glPushMatrix();
glColor3f(0,1,0);
glRotatef(-90,1,0,0);
glTranslatef(0, 0, 3);
glutSolidCone(5,20, 30,10);
glPopMatrix();
}
glPopMatrix();
}
/*
* Draws a hut object
*/
void Environment::drawHut() {
glPushMatrix();
glRotatef(-90, 1, 0, 0);
glColor3f(0.55, 0.55, 0.57);
GLUquadricObj *quadratic;
quadratic = gluNewQuadric();
gluCylinder(quadratic, 2, 2, 4, 32, 32);
glPopMatrix();
glPushMatrix();
glRotatef(90, 1, 0, 0);
glTranslatef(0, 0, -7);
glColor3f(0.5, 0.5, 0);
gluCylinder(quadratic, 0, 3, 4, 32, 32);
glPopMatrix();
}
/*
* Adds the bezier surface
*/
void Environment::addSurface(vector<BezierSurface> surf){
for(unsigned int i=0; i<surf.size(); i++)
this->surf.push_back(surf[i]);
}
/*
* Draws the bezier surface
*/
void Environment::drawSurface(){
for(unsigned int i=0; i<surf.size(); i++){
glPushMatrix();
float scale=100.0/12;
glScalef(scale,scale,scale);
glTranslatef(0,-1.95,0);
surf[i].renderGrid();
surf[i].renderSurface();
glPopMatrix();
}
}
/*
* Adds the track
*/
void Environment::addCurve(Bezier curve){
this->track=curve;
}
/*
* Draw Curve
*/
void Environment::drawCurve(){
glPushMatrix();
glTranslatef(0,10,0);
Point surfPos;
float uVector, vVector;
glDisable(GL_LIGHTING);
glColor3f(0,0,1.0f);
glLineWidth(3.0f);
glBegin(GL_LINE_STRIP);
for(unsigned int j=0; j<track.pSize(); j++){ // goes through each bezier curve if multiple joined
for(int i=0; i<=track.resolution(); i++){
Point temp=track.evaluateCurve(4*j,(float)(i)/track.resolution());
uVector = (0.01)* temp.getX() + 1;
vVector = (-0.01) * temp.getZ() + 1;
int bezierListIndex = 2*((int)floor(vVector)) + (int)floor(uVector);
surfPos = surf[bezierListIndex].evaluateSurface(uVector - floor(uVector) ,vVector - floor(vVector));
//glVertex3f(surfPos.getX()*100.0/12,(surfPos.getY()-2)*50.0,surfPos.getZ()*100.0/12);
glVertex3f(surfPos.getX()*100.0/12,(surfPos.getY())*10.0,surfPos.getZ()*100.0/12);
}
}
glEnd();
glEnable(GL_LIGHTING);
glPopMatrix();
}
/*
* Draws a box to be placed on track
*/
void Environment::drawBox(){
glColor3ub(181,166,66); // Brass color
// BOTTOM SQUARE
glPushMatrix();
glTranslatef(0,0,-5);
glScalef(10,1,1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(-5,0,0);
glScalef(1,1,10);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0,5);
glScalef(10,1,1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(5,0,0);
glScalef(1,1,10);
glutSolidCube(1);
glPopMatrix();
// TOP SQUARE
glPushMatrix();
glTranslatef(0,10,-5);
glScalef(10,1,1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(-5,10,0);
glScalef(1,1,10);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(0,10,5);
glScalef(10,1,1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(5,10,0);
glScalef(1,1,10);
glutSolidCube(1);
glPopMatrix();
// PILLARS
glPushMatrix();
glTranslatef(5,5,-5);
glScalef(1,11,1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(-5,5,-5);
glScalef(1,11,1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(5,5,5);
glScalef(1,11,1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(-5,5,5);
glScalef(1,11,1);
glutSolidCube(1);
glPopMatrix();
}
/*
* Adds the boxes to the track
*/
void Environment::trackBox(){
Point tmpC, tmpD, surfPos;
float uVector, vVector, theta;
for(int arc=0; arc<track.resSize(); arc+=30){
tmpC=track.arcLengthCurve(arc);
uVector = (0.01)* tmpC.getX() + 1;
vVector = (-0.01) * tmpC.getZ() + 1;
int bezierListIndex = 2*((int)floor(vVector)) + (int)floor(uVector);
surfPos = surf[bezierListIndex].evaluateSurface(uVector - floor(uVector) ,vVector - floor(vVector));
//Point tmp(surfPos.getX()*100.0/12,(surfPos.getY()-2)*20.0,surfPos.getZ()*100.0/12);
Point tmp(surfPos.getX()*100.0/12,(surfPos.getY())*10.0,surfPos.getZ()*100.0/12);
tmpD=track.arcDerivative(arc);
theta=atan2(tmpD.getX(),tmpD.getZ())*180/3.1415;
glPushMatrix();
glTranslatef(0,10,0);
glTranslatef(tmp.getX(),(tmp.getY()-2),tmp.getZ());
glRotatef(theta,0,1,0);
glScalef(.4,.4,.4);
drawBox();
glPopMatrix();
}
}
/*
* Draws crappy rainbow road
*/
void Environment::trackRoad(){
Point tmpC, tmpD, surfPos;
float uVector, vVector, theta;
for(int arc=0; arc<track.resSize(); arc++){
tmpC=track.arcLengthCurve(arc);
uVector = (0.01)* tmpC.getX() + 1;
vVector = (-0.01) * tmpC.getZ() + 1;
int bezierListIndex = 2*((int)floor(vVector)) + (int)floor(uVector);
surfPos = surf[bezierListIndex].evaluateSurface(uVector - floor(uVector) ,vVector - floor(vVector));
//Point tmp(surfPos.getX()*100.0/12,(surfPos.getY()-2)*20.0,surfPos.getZ()*100.0/12);
Point tmp(surfPos.getX()*100.0/12,(surfPos.getY())*10.0,surfPos.getZ()*100.0/12);
Point axis=surf[bezierListIndex].rotationAxis(uVector - floor(uVector) ,vVector - floor(vVector));
float surfAngle=surf[bezierListIndex].rotationAngle(uVector - floor(uVector) ,vVector - floor(vVector));
tmpD=track.arcDerivative(arc);
theta=atan2(tmpD.getX(),tmpD.getZ())*180/3.1415;
glPushMatrix();
glColor3f(rand()%10/100.0,rand()%10/100.0,rand()%10/100.0);
glTranslatef(0,8,0);
glTranslatef(tmp.getX(),(tmp.getY()-2),tmp.getZ());
glRotatef(surfAngle,axis.getX(),axis.getY(),axis.getZ());
glRotatef(90+theta,0,1,0);
glScalef(1,.1,5);
glutSolidCube(1);
glPopMatrix();
}
}