1
/***************************************************************************
2
* Copyright (c) 2013 Werner Mayer <wmayer[at]users.sourceforge.net> *
4
* This file is part of the FreeCAD CAx development system. *
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. *
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. *
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 *
21
***************************************************************************/
23
#include "PreCompiled.h"
29
#include "View3DInventorViewer.h"
34
TYPESYSTEM_SOURCE_ABSTRACT(Gui::GLGraphicsItem, Base::BaseClass)
40
for (int i=0; i<16; i++)
41
projectionmatrix[i] = 0.0;
44
GLPainter::~GLPainter()
49
bool GLPainter::begin(QPaintDevice * device)
54
viewer = dynamic_cast<QtGLWidget*>(device);
58
// Make current context
59
QSize view = viewer->size();
60
this->width = view.width();
61
this->height = view.height();
63
viewer->makeCurrent();
65
glMatrixMode(GL_PROJECTION);
69
glOrtho(0, this->width, 0, this->height, -1, 1);
72
glPushAttrib(GL_ALL_ATTRIB_BITS);
73
glGetFloatv(GL_DEPTH_RANGE, this->depthrange);
74
glGetDoublev(GL_PROJECTION_MATRIX, this->projectionmatrix);
76
glDepthFunc(GL_ALWAYS);
79
glEnable(GL_DEPTH_TEST);
80
glDisable(GL_LIGHTING);
81
glEnable(GL_COLOR_MATERIAL);
85
glColor4f(1.0, 1.0, 1.0, 0.0);
86
glViewport(0, 0, this->width, this->height);
99
this->logicOp = false;
100
glDisable(GL_COLOR_LOGIC_OP);
103
if (this->lineStipple) {
104
this->lineStipple = false;
105
glDisable(GL_LINE_STIPPLE);
108
// Reset original state
109
glDepthRange(this->depthrange[0], this->depthrange[1]);
110
glMatrixMode(GL_PROJECTION);
111
glLoadMatrixd(this->projectionmatrix);
120
bool GLPainter::isActive() const
122
return viewer != nullptr;
125
void GLPainter::setLineWidth(float w)
130
void GLPainter::setPointSize(float s)
135
void GLPainter::setColor(float r, float g, float b, float a)
137
glColor4f(r, g, b, a);
140
void GLPainter::setLogicOp(GLenum mode)
142
glEnable(GL_COLOR_LOGIC_OP);
144
this->logicOp = true;
147
void GLPainter::resetLogicOp()
149
glDisable(GL_COLOR_LOGIC_OP);
150
this->logicOp = false;
153
void GLPainter::setDrawBuffer(GLenum mode)
158
void GLPainter::setLineStipple(GLint factor, GLushort pattern)
160
glEnable(GL_LINE_STIPPLE);
161
glLineStipple(factor, pattern);
162
this->lineStipple = true;
165
void GLPainter::resetLineStipple()
167
glDisable(GL_LINE_STIPPLE);
168
this->lineStipple = false;
172
void GLPainter::drawRect(int x1, int y1, int x2, int y2)
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);
185
void GLPainter::drawLine(int x1, int y1, int x2, int y2)
191
glVertex3i(x1, this->height-y1, 0);
192
glVertex3i(x2, this->height-y2, 0);
196
void GLPainter::drawPoint(int x, int y)
202
glVertex3i(x, this->height-y, 0);
206
//-----------------------------------------------
208
Rubberband::Rubberband(View3DInventorViewer* v) : viewer(v)
210
x_old = y_old = x_new = y_new = 0;
220
Rubberband::Rubberband() : viewer(nullptr)
222
x_old = y_old = x_new = y_new = 0;
232
Rubberband::~Rubberband() = default;
234
void Rubberband::setWorking(bool on)
239
void Rubberband::setViewer(View3DInventorViewer* v)
244
void Rubberband::setCoords(int x1, int y1, int x2, int y2)
252
void Rubberband::setLineStipple(bool on)
257
void Rubberband::setColor(float r, float g, float b, float a)
265
void Rubberband::paintGL()
270
const SbViewportRegion vp = viewer->getSoRenderManager()->getViewportRegion();
271
SbVec2s size = vp.getViewportSizePixels();
273
glMatrixMode(GL_PROJECTION);
275
glOrtho(0, size[0], size[1], 0, 0, 100);
276
glMatrixMode(GL_MODELVIEW);
278
glDisable(GL_TEXTURE_2D);
280
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
282
glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
283
glRecti(x_old, y_old, x_new, y_new);
286
glColor4f(rgb_r, rgb_g, rgb_b, rgb_a);
288
glLineStipple(3, 0xAAAA);
289
glEnable(GL_LINE_STIPPLE);
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);
301
glDisable(GL_LINE_STIPPLE);
306
// -----------------------------------------------------------------------------------
308
Polyline::Polyline(View3DInventorViewer* v) : viewer(v)
322
Polyline::Polyline() : viewer(nullptr)
336
Polyline::~Polyline() = default;
338
void Polyline::setWorking(bool on)
343
bool Polyline::isWorking() const
348
void Polyline::setViewer(View3DInventorViewer* v)
353
void Polyline::setCoords(int x, int y)
359
void Polyline::setColor(int r, int g, int b, int a)
367
void Polyline::setClosed(bool c)
372
void Polyline::setCloseStippled(bool c)
377
void Polyline::setLineWidth(float l)
382
void Polyline::addNode(const QPoint& p)
384
_cNodeVector.push_back(p);
387
void Polyline::popNode()
389
if (!_cNodeVector.empty())
390
_cNodeVector.pop_back();
393
void Polyline::clear()
395
_cNodeVector.clear();
398
void Polyline::paintGL()
403
if (_cNodeVector.empty())
406
const SbViewportRegion vp = viewer->getSoRenderManager()->getViewportRegion();
407
SbVec2s size = vp.getViewportSizePixels();
409
glMatrixMode(GL_PROJECTION);
411
glOrtho(0, size[0], size[1], 0, 0, 100);
412
glMatrixMode(GL_MODELVIEW);
414
glDisable(GL_TEXTURE_2D);
416
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
418
glColor4f(rgb_r, rgb_g, rgb_b, rgb_a);
420
if (closed && !stippled) {
421
glBegin(GL_LINE_LOOP);
423
for (const QPoint& it : _cNodeVector) {
424
glVertex2i(it.x(), it.y());
432
QPoint start = _cNodeVector.front();
433
for (const QPoint& it : _cNodeVector) {
434
glVertex2i(start.x(), start.y());
436
glVertex2i(it.x(), it.y());
441
if (closed && stippled) {
442
glEnable(GL_LINE_STIPPLE);
443
glLineStipple(2, 0x3F3F);
445
glVertex2i(_cNodeVector.back().x(), _cNodeVector.back().y());
446
glVertex2i(_cNodeVector.front().x(), _cNodeVector.front().y());
448
glDisable(GL_LINE_STIPPLE);