-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·316 lines (265 loc) · 7.05 KB
/
main.cpp
File metadata and controls
executable file
·316 lines (265 loc) · 7.05 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
//#include <iostream>
#import <OpenGL/OpenGL.h>
#import <GLUT/GLUT.h>
#import "Planet.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <filesystem>
using std::string;
using std::ifstream;
using std::istringstream;
using std::vector;
namespace fs = std::filesystem;
vector<Planet *> solarSystem;
float xRot = 50.0f;
float timeInc = 10.0f;
double timeValue = 0.0;
float zPos = -2000.0f;
void setupScene(void) {
// for reading in CSV from file
fs::path pwd = fs::current_path();
string dataFilePath = pwd.string() + "/PlanetInfo.dat";
std::cout << dataFilePath;
ifstream inFile (dataFilePath, ifstream::in);
string line;
int linenum = 0;
while (inFile.good())
{
getline(inFile, line);
Planet *newPlanet = new Planet();
solarSystem.push_back(newPlanet);
//cout << "\nLine #" << linenum << ":" << endl;
istringstream linestream(line);
string item;
int itemnum = 0;
while (getline(linestream, item, ','))
{
//cout << "Item #" << itemnum << ": " << item << endl;
switch (itemnum) {
case 0:
solarSystem[linenum]->setName(item);
break;
case 1:
{
double temp = 0.0;
sscanf(item.c_str(), "%lf", &temp);
solarSystem[linenum]->getOrbit()->setAphelion(temp);
}
break;
case 2:
{
double temp = 0.0;
sscanf(item.c_str(), "%lf", &temp);
solarSystem[linenum]->getOrbit()->setPerihelion(temp);
}
break;
case 3:
{
float temp = 0.0;
sscanf(item.c_str(), "%f", &temp);
solarSystem[linenum]->getOrbit()->setPeriod(temp);
}
break;
case 4:
{
float temp = 0.0;
sscanf(item.c_str(), "%f", &temp);
solarSystem[linenum]->getOrbit()->setInclination(temp);
}
break;
case 5:
{
float temp = 0.0;
sscanf(item.c_str(), "%f", &temp);
solarSystem[linenum]->setRadius(temp);
}
break;
case 6:
{
float temp = 0.0;
sscanf(item.c_str(), "%f", &temp);
solarSystem[linenum]->setPeriod(temp);
}
break;
case 7:
solarSystem[linenum]->getColour()->setRedComponent((unsigned short)atoi(item.c_str()));
break;
case 8:
solarSystem[linenum]->getColour()->setGreenComponent((unsigned short)atoi(item.c_str()));
break;
case 9:
solarSystem[linenum]->getColour()->setBlueComponent((unsigned short)atoi(item.c_str()));
break;
case 10:
{
float temp = 0.0;
sscanf(item.c_str(), "%f", &temp);
solarSystem[linenum]->setAxialTilt(temp);
}
break;
case 11:
solarSystem[linenum]->setNumSatellites(atoi(item.c_str()));
break;
default:
//too many line params so do nothing
break;
}
itemnum++;
}
linenum++;
}
return;
}
void RenderScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
//translate the whole scene out into view
glTranslatef(0.0f, 0.0f, zPos);
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
//set material colour to yellow (sun)
glColor3ub(255, 255, 0);
glDisable(GL_LIGHTING);
glutSolidSphere(15.0f, 15, 15);
glEnable(GL_LIGHTING);
//position the light after we draw the sun!
GLfloat lightPos[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat amb_dif[] = {1.0f, 1.0f, 1.0, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glLightfv(GL_LIGHT0, GL_AMBIENT_AND_DIFFUSE, amb_dif);
glEnable(GL_LIGHT0);
//start drawing
//std::clog << solarSystem.size();
for (int i = 0 ; i < solarSystem.size() ; i++) {
//save sun position
glPushMatrix();
//rotate the coord system
glRotatef(1.0 / solarSystem[i]->getOrbit()->getPeriod() * timeValue, 0.0f, 1.0f, 0.0f);
//translate distance to planet
glTranslatef(solarSystem[i]->getOrbit()->getAverageDistance() * 50, 0.0f, 0.0f);
//draw planet
glColor3f(solarSystem[i]->getColour()->getRedComponent(),
solarSystem[i]->getColour()->getGreenComponent(),
solarSystem[i]->getColour()->getBlueComponent());
glutSolidSphere(solarSystem[i]->getRadius()/1000, 15, 15);
//if moon
//save planet position
//rotate to moon angle
//translate moon distance
//draw moon
//go back to planet position
//go back to sun position
glPopMatrix();
}
/*
//rotate coord system
glRotatef(timeValue / 12.0, 0.0f, 1.0f, 0.0f);
//draw the earth (blue)
glColor3ub(0, 0, 255);
glTranslatef(105.0f, 0.0f, 0.0f);
glutSolidSphere(15.0f, 15, 15);
//rotate earth-based coords and draw moon (grey)
glColor3ub(200, 200, 200);
glRotatef(timeValue, 0.0f, 1.0f, 0.0f);
glTranslatef(30.0f, 0.0f, 0.0f);
//fMoonRot += 15.0f;
//if (fMoonRot > 360.0f) {
// fMoonRot = 0.0f;
//}
glutSolidSphere(6.0f, 15, 15);
*/
//stop drawing
//restore the matrix state
glPopMatrix();
//step the time variable
timeValue += timeInc;
if (timeValue > 360000000.0) {
timeValue = 0.0f;
}
//flush and swap
glutSwapBuffers();
}
void SetupRC(void) {
//setup up a clear window colour of opaque black
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
}
void ChangeSize(GLsizei w, GLsizei h) {
GLfloat aspectRatio = (GLfloat)w / (GLfloat)h;
//prevent a div by 0
if (h == 0) {
h = 1;
}
//set the viewport
glViewport(0, 0, w, h);
//reset the coord system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//produce a perspective projection - FOV 45 deg, near 1 and far 425
gluPerspective(45.0f, aspectRatio, 1.0, 10000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void InputKeys(unsigned char key, int mouseX, int mouseY){
std::cerr << zPos;
switch (key) {
case '=':
zPos += 100.0f;
zPos > -500.0f ? zPos = -500.0 : zPos;
break;
case '-':
zPos -= 100.0f;
zPos < -2000.0f ? zPos = -2000.0 : zPos;
;
break;
default:
break;
}
std::cerr << "\nInputKeys\n";
}
void SpecialKeys(int key, int mouseX, int mouseY) {
switch (key) {
case GLUT_KEY_UP:
xRot += 10.0f;
xRot > 90.0 ? xRot = 90.0f : 0;
break;
case GLUT_KEY_DOWN:
xRot -= 10.0f;
xRot < 0.0f ? xRot = 0.0f : 0;
break;
case GLUT_KEY_LEFT:
timeInc -= 10.0f;
timeInc < 10.0f ? timeInc = 10.0f : 0;
break;
case GLUT_KEY_RIGHT:
timeInc += 10.0f;
break;
default:
break;
}
std::cerr << "\nSpecialKeys: " << key << "\n";
}
void TimerFunc(int value) {
glutPostRedisplay();
glutTimerFunc(100, TimerFunc, value);
}
int main (int argc, char * argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Solar");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
glutKeyboardFunc(InputKeys);
glutSpecialFunc(SpecialKeys);
glutTimerFunc(300, TimerFunc, 1);
setupScene();
SetupRC();
glutMainLoop();
return 0;
}