-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
250 lines (221 loc) · 8.01 KB
/
main.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
//
// main.cpp
// sfml_opengl
//
// Created by Jakub Tomczyński on 27/02/2019.
// Copyright © 2019 Jakub Tomczyński. All rights reserved.
//
#define GL_SILENCE_DEPRECATION
#include <celestialbody.h>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <celestialbody.h>
#ifdef __APPLE__
#include <OpenGL/glu.h> // macOS
#else
#include <GL/glu.h> // Windows/Linux
#endif
void draw_cube(double size) {
double half_cube_size = size / 2.0;
// bottom //140 177 222
glBegin(GL_POLYGON);
glVertex3d(-half_cube_size, half_cube_size, -half_cube_size);
glVertex3d(half_cube_size, half_cube_size, -half_cube_size);
glVertex3d(half_cube_size, -half_cube_size, -half_cube_size);
glVertex3d(-half_cube_size, -half_cube_size, -half_cube_size);
glEnd();
// top
glColor3d(1.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3d(-half_cube_size, half_cube_size, half_cube_size);
glVertex3d(half_cube_size, half_cube_size, half_cube_size);
glVertex3d(half_cube_size, -half_cube_size, half_cube_size);
glVertex3d(-half_cube_size, -half_cube_size, half_cube_size);
glEnd();
// left
glColor3d(1.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3d(-half_cube_size, -half_cube_size, half_cube_size);
glVertex3d(-half_cube_size, half_cube_size, half_cube_size);
glVertex3d(-half_cube_size, half_cube_size, -half_cube_size);
glVertex3d(-half_cube_size, -half_cube_size, -half_cube_size);
glEnd();
// right
glColor3d(1.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3d(half_cube_size, -half_cube_size, half_cube_size);
glVertex3d(half_cube_size, half_cube_size, half_cube_size);
glVertex3d(half_cube_size, half_cube_size, -half_cube_size);
glVertex3d(half_cube_size, -half_cube_size, -half_cube_size);
glEnd();
// front
glColor3d(1.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3d(-half_cube_size, -half_cube_size, half_cube_size);
glVertex3d(half_cube_size, -half_cube_size, half_cube_size);
glVertex3d(half_cube_size, -half_cube_size, -half_cube_size);
glVertex3d(-half_cube_size, -half_cube_size, -half_cube_size);
glEnd();
// back
glColor3d(1.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3d(-half_cube_size, half_cube_size, half_cube_size);
glVertex3d(half_cube_size, half_cube_size, half_cube_size);
glVertex3d(half_cube_size, half_cube_size, -half_cube_size);
glVertex3d(-half_cube_size, half_cube_size, -half_cube_size);
glEnd();
}
planet pl;
bool read_file(std::string name){
std::ifstream data(name);
if(data.is_open()){
std::stringstream input_data;
std::string line;
input_data << line << "\n";
std::getline(data,line);
while(std::getline(data,line)){
input_data.str(line);
std::string name_;
std::string dis_;
std::string dia_;
std::string spi_;
std::string orb_;
std::string gra_;
std::string moo_;
std::vector<float> rgb;
std::string r_;
std::string g_;
std::string b_;
std::string end;
input_data >> name_ >> dis_ >> dia_ >> spi_ >> orb_ >> gra_ >> moo_ >> r_ >> g_ >> b_ ;
pl.name.push_back(name_);
pl.distance.push_back(stoi(dis_));
pl.diameter.push_back(stoi(dia_));
pl.spin_time.push_back(stod(spi_));
pl.orbit_time.push_back(stof(orb_));
pl.gravity.push_back(stod(gra_));
pl.moons.push_back(stoi(moo_));
rgb.push_back(stof(r_));
rgb.push_back(stof(g_));
rgb.push_back(stof(b_));
pl.rgb.push_back(rgb);
}
}else{
return 0;
}
return 1;
}
GLdouble eyez = 3;
GLdouble view_pos_x;
GLdouble view_pos_y;
void set_viewport(int width, int height) {
const float ar = (float)width / (float)height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
gluLookAt(-3, 20, 60, 0, 0, 0, 0, 0, 1);
//gluLookAt(0, 0, 0, 60, 0, 0, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//void move_cam(){
// if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
// eyez -= 0.1;
// }
// if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
// eyez += 0.1;
// }
// glMatrixMode(GL_PROJECTION);
// glLoadIdentity();
// gluLookAt(0, -5, eyez, 0, 0, 0, 2, 0, 10);
// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
//}
int main() {
//read file
if(!read_file("/home/maciek/work/Planetarium/solar/solar_system.txt")){return 1;};
std::vector<CelestialBody> CelestialBodies;
//put data to object
for(unsigned int i = 0; i < pl.name.size(); i++){
CelestialBodies.emplace_back(CelestialBody(pl.diameter[i], pl.distance[i], pl.spin_time[i], pl.orbit_time[i], pl.rgb[i]));
}
// create the window
sf::Window window(sf::VideoMode(1024, 768), "SFML OpenGL Template", sf::Style::Default, sf::ContextSettings(32));
window.setVerticalSyncEnabled(true);
// activate the window
window.setActive(true);
// set viewport according to current window size
view_pos_x = window.getSize().x;
view_pos_y = window.getSize().y;
set_viewport(view_pos_x, view_pos_y);
glClearColor(0, 0, 0, 1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_SMOOTH);
glShadeModel(GL_SMOOTH);
// setup lights
GLfloat light_position[] = { 2.0, 0.0, 2.0, 1.0 };
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
GLfloat light_specular[] = { 0.0, 0.0, 0.0, 1.0 };
glLightfv( GL_LIGHT0, GL_POSITION, light_position);
glLightfv( GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv( GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv( GL_LIGHT0, GL_SPECULAR, light_specular);
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
GLfloat global_ambient[] = {0.3, 0.3, 0.3, 0.1};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
glEnable(GL_NORMALIZE) ;
// load resources, initialize the OpenGL states, ...
// run the main loop
bool running = true;
sf::Clock clk;
while (running) {
// handle events
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
// end the program
running = false;
} else if (event.type == sf::Event::Resized) {
// adjust the viewport when the window is resized
set_viewport(event.size.width, event.size.height);
} else if (event.type == sf::Event::KeyPressed){
set_viewport(view_pos_x, view_pos_y );
//std::cerr << "x: " <<view_pos_x << "y:" << view_pos_y << "\n";
}
}
// clear the buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColorMaterial (GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE) ;
glEnable (GL_COLOR_MATERIAL);
set_viewport(view_pos_x, view_pos_y );
//float rot = clk.getElapsedTime().asSeconds() * 90;
float dir = clk.getElapsedTime().asSeconds();
// draw stuff
for( auto &CelestialBodie : CelestialBodies){
CelestialBodie.draw(dir);
}
//camera move
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
view_pos_y -= 1.5 * dir;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
view_pos_y += 1.5 * dir;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
view_pos_x -= 2.5 * dir;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
view_pos_x += 2.5 * dir;
}
// TODO
// test functions below (glTranslated, glRotated, glColor3d)
// what happens when you change their arguments?
// does their order change the result?
// end the current frame (internally swaps the front and back buffers)
window.display();
}
return 0;
}