-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplanetglwidget.cpp
More file actions
180 lines (143 loc) · 4.96 KB
/
Copy pathplanetglwidget.cpp
File metadata and controls
180 lines (143 loc) · 4.96 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
#include "planetglwidget.h"
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <gl/GL.h>
void
MessageCallback( GLenum source,
GLenum type,
GLuint id,
GLenum severity,
GLsizei length,
const GLchar* message,
const void* userParam )
{
(void) length;
(void) source;
(void) userParam;
(void) id;
//if (type != GL_DEBUG_TYPE_ERROR)
// return;
if (type== 0x8251 || type == 0x8250)
return;
std::fprintf( stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
( type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : "" ),
type, severity, message );
}
PlanetGLWidget::PlanetGLWidget(QWidget *parent) : QOpenGLWidget(parent)
{
std::setvbuf(stdout, NULL, _IONBF, 0);
}
void PlanetGLWidget::paintGL()
{
double simframetime = simframetimeSm;
if(mw->selectedTh) simframetime = simframetimeTH;
auto t1 = clock.now();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawBackground(textureIDbg);
glLineWidth(0.1f);
if(mw->selectedTh) drawCircle(0,0, 0.8*R / maxdistance, textureID1);
else
drawCircle(0,0, R / maxdistance, textureID1);
double dist;
if (!stopped)
for (int i = 0; i < simframetime / simticktime; i++) {
if(bodies.size() == 2 && mw->selectedTh)
bodies[0].calc(simticktime);
else
for (auto &it: bodies) {
it.calc(simticktime);
}
}
double outx, outy, outvx, outvy, outax, outay, outangle;
if(mw->selectedTh && bodies.size() == 2)
{
double x1 = bodies[0].x;
double x2 = bodies[1].x;
double y1 = bodies[0].y;
double y2 = bodies[1].y;
double xc = (x1 + x2)/2;
double yc = (y1 + y2)/2;
double x1p, x2p, y1p, y2p;
x1p = xc + amplf * (x1 - xc);
y1p = yc + amplf * (y1 - yc);
x2p = xc + amplf * (x2 - xc);
y2p = yc + amplf * (y2 - yc);
outx = x1, outy = y1, outvx = bodies[0].vx, outvy = bodies[0].vy, outax = bodies[0].ax, outay = bodies[0].ay;
drawCircle(x1p / maxdistance, y1p / maxdistance, 0.02, textureID2, bodies[0].angle);
drawCircle(x2p / maxdistance, y2p / maxdistance, 0.02, textureID2, bodies[1].angle);
line(x1p / maxdistance, y1p / maxdistance, x2p / maxdistance, y2p / maxdistance);
}
else
for (auto &it: bodies) {
drawCircle(it.x / maxdistance, it.y / maxdistance, 0.02, textureID2, it.angle);
outx = it.x, outy = it.y, outvx = it.vx, outvy = it.vy, outax = it.ax, outay = it.ay, outangle = it.angle;
}
if (bodies.size() > 0)
mw->putData(outx, outy, outvx, outvy, outax, outay);
auto t2 = clock.now();
std::cerr << "Frame time: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count()
<< "ms\r"
<< std::flush;
}
void PlanetGLWidget::mousePressEvent(QMouseEvent *event)
{
if (mw->selectedTh)
return;
createBody_x = ( event->position().x() - screensize / 2) * maxdistance / (screensize / 2);
createBody_y = (-event->position().y() + screensize / 2) * maxdistance / (screensize / 2);
}
void PlanetGLWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (mw->selectedTh)
return;
double x = ( event->position().x() - screensize / 2) * maxdistance / (screensize / 2);
double y = (-event->position().y() + screensize / 2) * maxdistance / (screensize / 2);
createSimBody(createBody_x, createBody_y, (createBody_x - x) / 1000, (createBody_y - y) / 1000, mw->selectedRk);
}
void PlanetGLWidget::createSimBody(double x, double y, double vx, double vy, bool rk)
{
bodies.push_back(Body(body_m, x, y, vx, vy, rk));
}
void PlanetGLWidget::createThBodies(double x, double y, double len, double wcm, double wp, double k, double c)
{
double dm,dM;
dm = sqrt(x*x +y*y) - len/2;
dM = sqrt(x*x +y*y) + len/2;
double alpha;
if(x == 0)
{
if(y > 0) alpha = PI/2;
else alpha = 3*PI/2;
}
else alpha = atan(y/x);
if(x < 0) alpha += PI;
double xb1,yb1, xb2,yb2, v1x,v1y, v2x,v2y;
xb1 = dm * cos(alpha);
yb1 = dm * sin(alpha);
xb2 = dM * cos(alpha);
yb2 = dM * sin(alpha);
v1x = -wp * dm * sin(alpha) + wcm * len/2 * sin(alpha);
v1y = wp * dm * cos(alpha) - wcm * len/2 * cos(alpha);
v2x = -wp * dm * sin(alpha) - wcm * len/2 * sin(alpha);
v2y = wp * dm * cos(alpha) + wcm * len/2 * cos(alpha);
Body b1(body_m, xb1, yb1, v1x, v1y, false, len, k, c);
Body b2(body_m, xb2, yb2, v2x, v2y, false, len, k, c);
bodies.push_back(b1);
bodies.push_back(b2);
bodies[bodies.size() - 1].tether = &bodies[bodies.size() - 2];
bodies[bodies.size() - 2].tether = &bodies[bodies.size() - 1];
mw->angle(alpha*180/PI);
}
void PlanetGLWidget::clear()
{
bodies.clear();
}
void PlanetGLWidget::resizeGL(int w, int h)
{
(void) w;
(void) h;
}