FreeCAD

Форк
0
/
GLPainter.cpp 
453 строки · 9.5 Кб
1
/***************************************************************************
2
 *   Copyright (c) 2013 Werner Mayer <wmayer[at]users.sourceforge.net>     *
3
 *                                                                         *
4
 *   This file is part of the FreeCAD CAx development system.              *
5
 *                                                                         *
6
 *   This library is free software; you can redistribute it and/or         *
7
 *   modify it under the terms of the GNU Library General Public           *
8
 *   License as published by the Free Software Foundation; either          *
9
 *   version 2 of the License, or (at your option) any later version.      *
10
 *                                                                         *
11
 *   This library  is distributed in the hope that it will be useful,      *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU Library General Public License for more details.                  *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU Library General Public     *
17
 *   License along with this library; see the file COPYING.LIB. If not,    *
18
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
19
 *   Suite 330, Boston, MA  02111-1307, USA                                *
20
 *                                                                         *
21
 ***************************************************************************/
22

23
#include "PreCompiled.h"
24

25
#ifndef _PreComp_
26
#endif
27

28
#include "GLPainter.h"
29
#include "View3DInventorViewer.h"
30

31

32
using namespace Gui;
33

34
TYPESYSTEM_SOURCE_ABSTRACT(Gui::GLGraphicsItem, Base::BaseClass)
35

36
GLPainter::GLPainter()
37
{
38
    depthrange[0] = 0;
39
    depthrange[1] = 0;
40
    for (int i=0; i<16; i++)
41
        projectionmatrix[i] = 0.0;
42
}
43

44
GLPainter::~GLPainter()
45
{
46
    end();
47
}
48

49
bool GLPainter::begin(QPaintDevice * device)
50
{
51
    if (viewer)
52
        return false;
53

54
    viewer = dynamic_cast<QtGLWidget*>(device);
55
    if (!viewer)
56
        return false;
57

58
    // Make current context
59
    QSize view = viewer->size();
60
    this->width = view.width();
61
    this->height = view.height();
62

63
    viewer->makeCurrent();
64

65
    glMatrixMode(GL_PROJECTION);
66
    glPushMatrix();
67

68
    glLoadIdentity();
69
    glOrtho(0, this->width, 0, this->height, -1, 1);
70

71
    // Store GL state
72
    glPushAttrib(GL_ALL_ATTRIB_BITS);
73
    glGetFloatv(GL_DEPTH_RANGE, this->depthrange);
74
    glGetDoublev(GL_PROJECTION_MATRIX, this->projectionmatrix);
75

76
    glDepthFunc(GL_ALWAYS);
77
    glDepthMask(GL_TRUE);
78
    glDepthRange(0,0);
79
    glEnable(GL_DEPTH_TEST);
80
    glDisable(GL_LIGHTING);
81
    glEnable(GL_COLOR_MATERIAL);
82
    glDisable(GL_BLEND);
83

84
    glLineWidth(1.0f);
85
    glColor4f(1.0, 1.0, 1.0, 0.0);
86
    glViewport(0, 0, this->width, this->height);
87

88
    return true;
89
}
90

91
bool GLPainter::end()
92
{
93
    if (!viewer)
94
        return false;
95

96
    glFlush();
97

98
    if (this->logicOp) {
99
        this->logicOp = false;
100
        glDisable(GL_COLOR_LOGIC_OP);
101
    }
102

103
    if (this->lineStipple) {
104
        this->lineStipple = false;
105
        glDisable(GL_LINE_STIPPLE);
106
    }
107

108
    // Reset original state
109
    glDepthRange(this->depthrange[0], this->depthrange[1]);
110
    glMatrixMode(GL_PROJECTION);
111
    glLoadMatrixd(this->projectionmatrix);
112

113
    glPopAttrib();
114
    glPopMatrix();
115

116
    viewer = nullptr;
117
    return true;
118
}
119

120
bool GLPainter::isActive() const
121
{
122
    return viewer != nullptr;
123
}
124

125
void GLPainter::setLineWidth(float w)
126
{
127
    glLineWidth(w);
128
}
129

130
void GLPainter::setPointSize(float s)
131
{
132
    glPointSize(s);
133
}
134

135
void GLPainter::setColor(float r, float g, float b, float a)
136
{
137
    glColor4f(r, g, b, a);
138
}
139

140
void GLPainter::setLogicOp(GLenum mode)
141
{
142
    glEnable(GL_COLOR_LOGIC_OP);
143
    glLogicOp(mode);
144
    this->logicOp = true;
145
}
146

147
void GLPainter::resetLogicOp()
148
{
149
    glDisable(GL_COLOR_LOGIC_OP);
150
    this->logicOp = false;
151
}
152

153
void GLPainter::setDrawBuffer(GLenum mode)
154
{
155
    glDrawBuffer(mode);
156
}
157

158
void GLPainter::setLineStipple(GLint factor, GLushort pattern)
159
{
160
    glEnable(GL_LINE_STIPPLE);
161
    glLineStipple(factor, pattern);
162
    this->lineStipple = true;
163
}
164

165
void GLPainter::resetLineStipple()
166
{
167
    glDisable(GL_LINE_STIPPLE);
168
    this->lineStipple = false;
169
}
170

171
// Draw routines
172
void GLPainter::drawRect(int x1, int y1, int x2, int y2)
173
{
174
    if (!viewer)
175
        return;
176

177
    glBegin(GL_LINE_LOOP);
178
        glVertex3i(x1, this->height-y1, 0);
179
        glVertex3i(x2, this->height-y1, 0);
180
        glVertex3i(x2, this->height-y2, 0);
181
        glVertex3i(x1, this->height-y2, 0);
182
    glEnd();
183
}
184

185
void GLPainter::drawLine(int x1, int y1, int x2, int y2)
186
{
187
    if (!viewer)
188
        return;
189

190
    glBegin(GL_LINES);
191
        glVertex3i(x1, this->height-y1, 0);
192
        glVertex3i(x2, this->height-y2, 0);
193
    glEnd();
194
}
195

196
void GLPainter::drawPoint(int x, int y)
197
{
198
    if (!viewer)
199
        return;
200

201
    glBegin(GL_POINTS);
202
        glVertex3i(x, this->height-y, 0);
203
    glEnd();
204
}
205

206
//-----------------------------------------------
207

208
Rubberband::Rubberband(View3DInventorViewer* v) : viewer(v)
209
{
210
    x_old = y_old = x_new = y_new = 0;
211
    working = false;
212
    stipple = true;
213

214
    rgb_r = 1.0f;
215
    rgb_g = 1.0f;
216
    rgb_b = 1.0f;
217
    rgb_a = 1.0f;
218
}
219

220
Rubberband::Rubberband() : viewer(nullptr)
221
{
222
    x_old = y_old = x_new = y_new = 0;
223
    working = false;
224
    stipple = true;
225

226
    rgb_r = 1.0f;
227
    rgb_g = 1.0f;
228
    rgb_b = 1.0f;
229
    rgb_a = 1.0f;
230
}
231

232
Rubberband::~Rubberband() = default;
233

234
void Rubberband::setWorking(bool on)
235
{
236
    working = on;
237
}
238

239
void Rubberband::setViewer(View3DInventorViewer* v)
240
{
241
    viewer = v;
242
}
243

244
void Rubberband::setCoords(int x1, int y1, int x2, int y2)
245
{
246
    x_old = x1;
247
    y_old = y1;
248
    x_new = x2;
249
    y_new = y2;
250
}
251

252
void Rubberband::setLineStipple(bool on)
253
{
254
    stipple = on;
255
}
256

257
void Rubberband::setColor(float r, float g, float b, float a)
258
{
259
    rgb_a = a;
260
    rgb_b = b;
261
    rgb_g = g;
262
    rgb_r = r;
263
}
264

265
void Rubberband::paintGL()
266
{
267
    if (!working)
268
        return;
269

270
    const SbViewportRegion vp = viewer->getSoRenderManager()->getViewportRegion();
271
    SbVec2s size = vp.getViewportSizePixels();
272

273
    glMatrixMode(GL_PROJECTION);
274
    glLoadIdentity();
275
    glOrtho(0, size[0], size[1], 0, 0, 100);
276
    glMatrixMode(GL_MODELVIEW);
277
    glLoadIdentity();
278
    glDisable(GL_TEXTURE_2D);
279
    glEnable(GL_BLEND);
280
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
281
    glLineWidth(4.0);
282
    glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
283
    glRecti(x_old, y_old, x_new, y_new);
284

285
    glLineWidth(4.0);
286
    glColor4f(rgb_r, rgb_g, rgb_b, rgb_a);
287
    if (stipple) {
288
        glLineStipple(3, 0xAAAA);
289
        glEnable(GL_LINE_STIPPLE);
290
    }
291
    glBegin(GL_LINE_LOOP);
292
    glVertex2i(x_old, y_old);
293
    glVertex2i(x_old, y_new);
294
    glVertex2i(x_new, y_new);
295
    glVertex2i(x_new, y_old);
296
    glEnd();
297

298
    glLineWidth(1.0);
299

300
    if (stipple)
301
        glDisable(GL_LINE_STIPPLE);
302

303
    glDisable(GL_BLEND);
304
}
305

306
// -----------------------------------------------------------------------------------
307

308
Polyline::Polyline(View3DInventorViewer* v) : viewer(v)
309
{
310
    x_new = y_new = 0;
311
    working = false;
312
    closed = true;
313
    stippled = false;
314
    line = 2.0;
315

316
    rgb_r = 1.0f;
317
    rgb_g = 1.0f;
318
    rgb_b = 1.0f;
319
    rgb_a = 1.0f;
320
}
321

322
Polyline::Polyline() : viewer(nullptr)
323
{
324
    x_new = y_new = 0;
325
    working = false;
326
    closed = true;
327
    stippled = false;
328
    line = 2.0;
329

330
    rgb_r = 1.0f;
331
    rgb_g = 1.0f;
332
    rgb_b = 1.0f;
333
    rgb_a = 1.0f;
334
}
335

336
Polyline::~Polyline() = default;
337

338
void Polyline::setWorking(bool on)
339
{
340
    working = on;
341
}
342

343
bool Polyline::isWorking() const
344
{
345
    return working;
346
}
347

348
void Polyline::setViewer(View3DInventorViewer* v)
349
{
350
    viewer = v;
351
}
352

353
void Polyline::setCoords(int x, int y)
354
{
355
    x_new = x;
356
    y_new = y;
357
}
358

359
void Polyline::setColor(int r, int g, int b, int a)
360
{
361
    rgb_r = r;
362
    rgb_g = g;
363
    rgb_b = b;
364
    rgb_a = a;
365
}
366

367
void Polyline::setClosed(bool c)
368
{
369
    closed = c;
370
}
371

372
void Polyline::setCloseStippled(bool c)
373
{
374
    stippled = c;
375
}
376

377
void Polyline::setLineWidth(float l)
378
{
379
    line = l;
380
}
381

382
void Polyline::addNode(const QPoint& p)
383
{
384
    _cNodeVector.push_back(p);
385
}
386

387
void Polyline::popNode()
388
{
389
    if (!_cNodeVector.empty())
390
        _cNodeVector.pop_back();
391
}
392

393
void Polyline::clear()
394
{
395
    _cNodeVector.clear();
396
}
397

398
void Polyline::paintGL()
399
{
400
    if (!working)
401
        return;
402

403
    if (_cNodeVector.empty())
404
        return;
405

406
    const SbViewportRegion vp = viewer->getSoRenderManager()->getViewportRegion();
407
    SbVec2s size = vp.getViewportSizePixels();
408

409
    glMatrixMode(GL_PROJECTION);
410
    glLoadIdentity();
411
    glOrtho(0, size[0], size[1], 0, 0, 100);
412
    glMatrixMode(GL_MODELVIEW);
413
    glLoadIdentity();
414
    glDisable(GL_TEXTURE_2D);
415
    glEnable(GL_BLEND);
416
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
417
    glLineWidth(line);
418
    glColor4f(rgb_r, rgb_g, rgb_b, rgb_a);
419

420
    if (closed && !stippled) {
421
        glBegin(GL_LINE_LOOP);
422

423
        for (const QPoint& it : _cNodeVector) {
424
            glVertex2i(it.x(), it.y());
425
        }
426

427
        glEnd();
428
    }
429
    else {
430
        glBegin(GL_LINES);
431

432
        QPoint start = _cNodeVector.front();
433
        for (const QPoint& it : _cNodeVector) {
434
            glVertex2i(start.x(), start.y());
435
            start = it;
436
            glVertex2i(it.x(), it.y());
437
        }
438

439
        glEnd();
440

441
        if (closed && stippled) {
442
            glEnable(GL_LINE_STIPPLE);
443
            glLineStipple(2, 0x3F3F);
444
            glBegin(GL_LINES);
445
                glVertex2i(_cNodeVector.back().x(), _cNodeVector.back().y());
446
                glVertex2i(_cNodeVector.front().x(), _cNodeVector.front().y());
447
            glEnd();
448
            glDisable(GL_LINE_STIPPLE);
449
        }
450
    }
451

452
    glDisable(GL_BLEND);
453
}
454

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.