-
Notifications
You must be signed in to change notification settings - Fork 1
/
Track.cpp
148 lines (122 loc) · 3.71 KB
/
Track.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
#include "Track.h"
#include <math.h>
#include <cstdlib>
#include <iostream>
#include <time.h>
Track::Track() : lastRot( {0,0})
{
track.push_front(Point(0,0,0));
//track.push_front(Point(0,1000,-0.3));
SEGMENT_LENGTH = 1;
}
void Track::GenChunk(int len)
{
for (int i = 0; i < len; i++)
{
GenCurve();
}
}
void Track::GenCurve()
{
//generate a random modifier
float randP = ((float)rand()/(float)RAND_MAX * 0.1f) - 0.05f;
float randY = ((float)rand()/(float)RAND_MAX * 0.1f) - 0.05f;
for (int i = 0; i < 50; i++)
{
lastRot[0] += randP; //Pitch
lastRot[1] += randY; //Yaw
float temp = SEGMENT_LENGTH * cos(lastRot[0]);
track.push_front(Point(track.front().x + SEGMENT_LENGTH * sin(lastRot[1]),
track.front().y + SEGMENT_LENGTH * cos(lastRot[1]),
track.front().z + SEGMENT_LENGTH * sin(lastRot[0])));
//std::cout << track.front().x << " " << track.front().y << " " << track.front().z << std::endl;
}
//std::cout << "Generated: " << randP << " " << randY << std::endl;
}
void Track::ExtendTrack(Point extendTo)
{
track.push_front(extendTo);
}
std::deque<Point>::reverse_iterator Track::getStart()
{
return track.rbegin();
}
void Track::Render(float elapsedTime, float radius)
{
GLUquadricObj *quadratic=gluNewQuadric();
// LINE-BASED WORKING CODE
/*glLineWidth(5.0);
glColor3f(1, 0, 0);
glBegin(GL_LINES);
for (std::deque<Point>::iterator i = track.begin(); i+1 != track.end(); i++)
{
glVertex3f(i->x, i->y, i->z);
glVertex3f((i+1)->x, (i+1)->y, (i+1)->z);
}
glEnd();*/
//glPushMatrix(); // Save the old matrix
glBegin(GL_QUADS);
float fat = 0.5;
//glPushMatrix();
for (std::deque<Point>::iterator i = track.begin(); i+1 != track.end(); i++)
{
//glLoadIdentity();
glPushMatrix();
glTranslatef(i->x, i->y, i->z);
gluSphere(quadratic, radius,4,4);
/*glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
gluLookAt(i->x, i->y, i->z, (i+1)->x, (i+1)->y, (i+1)->z, 0,0,1);
//glPushMatrix();
//gluLookAt(i->x, i->y, i->z, 0, 0, 0, 0,1,0);
glVertex3f(-fat + i->x, -fat + i->y, 0 + i->z);
glVertex3f(-fat + i->x, fat+ i->y, 0+ i->z);
glVertex3f(-fat + (i+1)->x, -fat + (i+1)->y, SEGMENT_LENGTH + (i+1)->z);
glVertex3f(-fat + (i+1)->x, fat + (i+1)->y, SEGMENT_LENGTH + (i+1)->z);
glVertex3f(fat, -fat, 0);
glVertex3f(fat, fat, 0);
glVertex3f(fat, -fat, SEGMENT_LENGTH);
glVertex3f(fat, fat, SEGMENT_LENGTH);
glVertex3f(-fat, fat, 0);
glVertex3f(fat, fat, 0);
glVertex3f(-fat, fat, SEGMENT_LENGTH);
glVertex3f(fat, fat, SEGMENT_LENGTH);
glVertex3f(-fat, -fat, 0);
glVertex3f(fat, -fat, 0);
glVertex3f(-fat, -fat, SEGMENT_LENGTH);
glVertex3f(fat, -fat, SEGMENT_LENGTH);*/
glPopMatrix();
}
glEnd();
//glPopMatrix();
//glPopMatrix();
}
Point Track::GetAvg()
{
Point ret;
ret = Point(0, 0, 0);
for (std::deque<Point>::iterator i = track.begin(); i != track.end(); i++)
{
ret.x += i->x;
ret.y += i->y;
ret.z += i->z;
}
ret.x = ret.x/track.size();
ret.y = ret.y/track.size();
ret.z = ret.z/track.size();
return ret;
}
float Track::GetMaxDist(Point center)
{
float max = 0;
float temp;
for (std::deque<Point>::iterator i = track.begin(); i != track.end(); i++)
{
temp = sqrt(pow((center.x - i->x), 2) + pow((center.y - i->y), 2) + pow((center.z - i->z), 2));
if (temp > max)
{
max = temp;
}
}
return max;
}