-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglspace.cpp
276 lines (224 loc) · 4.97 KB
/
glspace.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
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
#include "glspace.h"
#include "ui_glspace.h"
#include <QMouseEvent>
#include <QWheelEvent>
#if (_MSC_VER >= 1500 && _MSC_VER <= 1600)
#include <Windows.h>
#else
#endif
//#include <GL/gl.h>
//#include <GL/glu.h>
GLSpace::GLSpace(QWidget *parent) :
QGLWidget(parent),
ui(new Ui::GLSpace)
, m_is_draw_plane(true)
, m_count_plane_line(200)
, m_plane_width(500)
{
ui->setupUi(this);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(on_timeout()));
m_timer.start(20);
m_backround = QColor(0, 0, 0, 0);
m_translate.setZ(-2);
}
GLSpace::~GLSpace()
{
delete ui;
disconnect(&m_timer);
m_timer.stop();
foreach (VirtGLObject* obj, m_objects) {
delete obj;
}
}
void GLSpace::add_object(VirtGLObject *obj)
{
if(!m_objects.contains(obj)){
obj->setParent(this);
m_objects.push_back(obj);
}
}
VirtGLObject *GLSpace::item(int code_enum)
{
foreach (VirtGLObject* obj, m_objects) {
if(obj->type() == code_enum)
return obj;
}
return 0;
}
VirtGLObject *GLSpace::object(int index)
{
return m_objects[index];
}
int GLSpace::count_objects() const
{
return m_objects.size();
}
void GLSpace::setTimeout(int timeout)
{
m_timer.setInterval(timeout);
}
int GLSpace::timeout() const
{
return m_timer.interval();
}
QColor GLSpace::background() const
{
return m_backround;
}
void GLSpace::setBackground(const QColor &color)
{
m_backround = color;
}
bool GLSpace::is_draw_plane() const
{
return m_is_draw_plane;
}
void GLSpace::set_is_draw_plane(bool value)
{
m_is_draw_plane = value;
}
void GLSpace::set_count_lines_plane(int value)
{
m_count_plane_line = value;
}
int GLSpace::count_lines_plane() const
{
return m_count_plane_line;
}
void GLSpace::set_width_plane(double value)
{
m_plane_width = value;
}
double GLSpace::width_plane() const
{
return m_plane_width;
}
void GLSpace::calc_mouse_move(const QPointF &pos)
{
if(!m_mouse_down)
return;
QPointF delta = pos - m_mouse_move;
m_rotate += delta * 0.5;
}
void GLSpace::draw_plane()
{
glColor3f(1, 1, 1);
const int count = m_count_plane_line;
const double width = m_plane_width;
glBegin(GL_LINES);
for(int i = 0; i < count; i++){
glVertex3d(-width/2.0,
-width/2.0 + width * i/count,
0);
glVertex3d(-width/2.0 + width,
-width/2.0 + width * i/count,
0);
}
glEnd();
glBegin(GL_LINES);
for(int i = 0; i < count; i++){
glVertex3d(-width/2.0 + width* i/count,
-width/2.0,
0);
glVertex3d(-width/2.0 + width* i/count,
-width/2.0 + width,
0);
}
glLineWidth(5);
glBegin(GL_LINES);
glVertex3f(-width, 0, 0);
glVertex3f(width, 0, 0);
glVertex3f(0, -width, 0);
glVertex3f(0, width, 0);
glEnd();
glLineWidth(1);
glEnd();
}
bool GLSpace::event(QEvent *ev)
{
QMouseEvent* mev;
QWheelEvent* wev;
double z;
switch (ev->type()) {
case QEvent::MouseButtonPress:
mev = dynamic_cast<QMouseEvent*>(ev);
m_mouse_down = true;
m_mouse_move = mev->globalPos();
break;
case QEvent::MouseButtonRelease:
mev = dynamic_cast<QMouseEvent*>(ev);
m_mouse_down = false;
m_mouse_move = mev->globalPos();
break;
case QEvent::MouseMove:
mev = dynamic_cast<QMouseEvent*>(ev);
calc_mouse_move(mev->globalPos());
m_mouse_move = mev->globalPos();
break;
case QEvent::Wheel:
wev = dynamic_cast< QWheelEvent* >(ev);
z = m_translate.z();
m_translate.setZ(z + wev->delta() * 0.01);
break;
default:
break;
}
return QGLWidget::event(ev);
}
void GLSpace::updateGL()
{
QGLWidget::updateGL();
}
void GLSpace::on_timeout()
{
foreach (VirtGLObject* obj, m_objects) {
obj->tick();
}
updateGL();
}
void GLSpace::initializeGL()
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
foreach (VirtGLObject* obj, m_objects) {
obj->init();
}
}
void GLSpace::resizeGL(int w, int h)
{
double ar = 1.0 * w/h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-0.5 * ar, 0.5 * ar, -0.5, 0.5, 1, 1000);
glMatrixMode(GL_MODELVIEW);
}
void GLSpace::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ACCUM_BUFFER_BIT);
glClearColor(m_backround.redF(), m_backround.greenF(), m_backround.blueF(), m_backround.alphaF());
glLoadIdentity();
glTranslatef(m_translate.x(), m_translate.y(), m_translate.z());
glRotated(m_rotate.x(), 0, 1, 0);
glRotated(m_rotate.y(), 1, 0, 0);
/// watch for select object
foreach (VirtGLObject* obj, m_objects) {
if(!obj->is_enable())
continue;
if(obj->is_watchXY()){
glTranslated(-obj->position().x(), -obj->position().y(), 0);
break;
}
if(obj->is_watch()){
glTranslated(-obj->position().x(), -obj->position().y(), -obj->position().z());
break;
}
}
if(m_is_draw_plane)
draw_plane();
foreach (VirtGLObject* obj, m_objects) {
if(!obj->is_enable())
continue;
obj->draw();
}
}