Debian Codegraph @

src/Gui/GLPainter.cpp

raw
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 <QOpenGLWidget>
24
25 #include "GLPainter.h"
26 #include "View3DInventorViewer.h"
27
28
29 using namespace Gui;
30
31 TYPESYSTEM_SOURCE_ABSTRACT(Gui::GLGraphicsItem, Base::BaseClass)
32
33 GLPainter::GLPainter()
34 {
35 depthrange[0] = 0;
36 depthrange[1] = 0;
37 for (int i = 0; i < 16; i++) {
38 projectionmatrix[i] = 0.0;
39 }
40 }
41
42 GLPainter::~GLPainter()
43 {
44 end();
45 }
46
47 bool GLPainter::begin(QPaintDevice* device)
48 {
49 if (viewer) {
50 return false;
51 }
52
53 viewer = dynamic_cast<QOpenGLWidget*>(device);
54 if (!viewer) {
55 return false;
56 }
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
97 glFlush();
98
99 if (this->logicOp) {
100 this->logicOp = false;
101 glDisable(GL_COLOR_LOGIC_OP);
102 }
103
104 if (this->lineStipple) {
105 this->lineStipple = false;
106 glDisable(GL_LINE_STIPPLE);
107 }
108
109 // Reset original state
110 glDepthRange(this->depthrange[0], this->depthrange[1]);
111 glMatrixMode(GL_PROJECTION);
112 glLoadMatrixd(this->projectionmatrix);
113
114 glPopAttrib();
115 glPopMatrix();
116
117 viewer = nullptr;
118 return true;
119 }
120
121 bool GLPainter::isActive() const
122 {
123 return viewer != nullptr;
124 }
125
126 void GLPainter::setLineWidth(float w)
127 {
128 glLineWidth(w);
129 }
130
131 void GLPainter::setPointSize(float s)
132 {
133 glPointSize(s);
134 }
135
136 void GLPainter::setColor(float r, float g, float b, float a)
137 {
138 glColor4f(r, g, b, a);
139 }
140
141 void GLPainter::setLogicOp(GLenum mode)
142 {
143 glEnable(GL_COLOR_LOGIC_OP);
144 glLogicOp(mode);
145 this->logicOp = true;
146 }
147
148 void GLPainter::resetLogicOp()
149 {
150 glDisable(GL_COLOR_LOGIC_OP);
151 this->logicOp = false;
152 }
153
154 void GLPainter::setDrawBuffer(GLenum mode)
155 {
156 glDrawBuffer(mode);
157 }
158
159 void GLPainter::setLineStipple(GLint factor, GLushort pattern)
160 {
161 glEnable(GL_LINE_STIPPLE);
162 glLineStipple(factor, pattern);
163 this->lineStipple = true;
164 }
165
166 void GLPainter::resetLineStipple()
167 {
168 glDisable(GL_LINE_STIPPLE);
169 this->lineStipple = false;
170 }
171
172 // Draw routines
173 void GLPainter::drawRect(int x1, int y1, int x2, int y2)
174 {
175 if (!viewer) {
176 return;
177 }
178
179 glBegin(GL_LINE_LOOP);
180 glVertex3i(x1, this->height - y1, 0);
181 glVertex3i(x2, this->height - y1, 0);
182 glVertex3i(x2, this->height - y2, 0);
183 glVertex3i(x1, this->height - y2, 0);
184 glEnd();
185 }
186
187 void GLPainter::drawLine(int x1, int y1, int x2, int y2)
188 {
189 if (!viewer) {
190 return;
191 }
192
193 glBegin(GL_LINES);
194 glVertex3i(x1, this->height - y1, 0);
195 glVertex3i(x2, this->height - y2, 0);
196 glEnd();
197 }
198
199 void GLPainter::drawPoint(int x, int y)
200 {
201 if (!viewer) {
202 return;
203 }
204
205 glBegin(GL_POINTS);
206 glVertex3i(x, this->height - y, 0);
207 glEnd();
208 }
209
210 //-----------------------------------------------
211
212 Rubberband::Rubberband(View3DInventorViewer* v)
213 : viewer(v)
214 {
215 x_old = y_old = x_new = y_new = 0;
216 working = false;
217 stipple = true;
218
219 rgb_r = 1.0f;
220 rgb_g = 1.0f;
221 rgb_b = 1.0f;
222 rgb_a = 1.0f;
223 }
224
225 Rubberband::Rubberband()
226 : viewer(nullptr)
227 {
228 x_old = y_old = x_new = y_new = 0;
229 working = false;
230 stipple = true;
231
232 rgb_r = 0.27f;
233 rgb_g = 0.4f;
234 rgb_b = 1.0f;
235 rgb_a = 1.0f;
236 }
237
238 Rubberband::~Rubberband() = default;
239
240 void Rubberband::setWorking(bool on)
241 {
242 working = on;
243 }
244
245 void Rubberband::setViewer(View3DInventorViewer* v)
246 {
247 viewer = v;
248 }
249
250 void Rubberband::setCoords(int x1, int y1, int x2, int y2)
251 {
252 x_old = x1;
253 y_old = y1;
254 x_new = x2;
255 y_new = y2;
256 }
257
258 void Rubberband::setLineStipple(bool on)
259 {
260 stipple = on;
261 }
262
263 void Rubberband::setColor(float r, float g, float b, float a)
264 {
265 rgb_a = a;
266 rgb_b = b;
267 rgb_g = g;
268 rgb_r = r;
269 }
270
271 void Rubberband::paintGL()
272 {
273 if (!working) {
274 return;
275 }
276
277 const SbViewportRegion vp = viewer->getSoRenderManager()->getViewportRegion();
278 SbVec2s size = vp.getViewportSizePixels();
279
280 glMatrixMode(GL_PROJECTION);
281 glLoadIdentity();
282 glOrtho(0, size[0], size[1], 0, 0, 100);
283 glMatrixMode(GL_MODELVIEW);
284 glLoadIdentity();
285 glDisable(GL_TEXTURE_2D);
286 glEnable(GL_BLEND);
287 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
288 glLineWidth(4.0);
289 glColor4f(1.0f, 1.0f, 1.0f, 0.5f);
290 glRecti(x_old, y_old, x_new, y_new);
291
292 glLineWidth(4.0);
293 glColor4f(rgb_r, rgb_g, rgb_b, rgb_a);
294 if (stipple) {
295 glLineStipple(3, 0xAAAA);
296 glEnable(GL_LINE_STIPPLE);
297 }
298 glBegin(GL_LINE_LOOP);
299 glVertex2i(x_old, y_old);
300 glVertex2i(x_old, y_new);
301 glVertex2i(x_new, y_new);
302 glVertex2i(x_new, y_old);
303 glEnd();
304
305 glLineWidth(1.0);
306
307 if (stipple) {
308 glDisable(GL_LINE_STIPPLE);
309 }
310
311 glDisable(GL_BLEND);
312 }
313
314 // -----------------------------------------------------------------------------------
315
316 Polyline::Polyline(View3DInventorViewer* v)
317 : viewer(v)
318 {
319 x_new = y_new = 0;
320 working = false;
321 closed = true;
322 stippled = false;
323 line = 2.0;
324
325 rgb_r = 1.0f;
326 rgb_g = 1.0f;
327 rgb_b = 1.0f;
328 rgb_a = 1.0f;
329 }
330
331 Polyline::Polyline()
332 : viewer(nullptr)
333 {
334 x_new = y_new = 0;
335 working = false;
336 closed = true;
337 stippled = false;
338 line = 2.0;
339
340 rgb_r = 1.0f;
341 rgb_g = 1.0f;
342 rgb_b = 1.0f;
343 rgb_a = 1.0f;
344 }
345
346 Polyline::~Polyline() = default;
347
348 void Polyline::setWorking(bool on)
349 {
350 working = on;
351 }
352
353 bool Polyline::isWorking() const
354 {
355 return working;
356 }
357
358 void Polyline::setViewer(View3DInventorViewer* v)
359 {
360 viewer = v;
361 }
362
363 void Polyline::setCoords(int x, int y)
364 {
365 x_new = x;
366 y_new = y;
367 }
368
369 void Polyline::setColor(int r, int g, int b, int a)
370 {
371 rgb_r = r;
372 rgb_g = g;
373 rgb_b = b;
374 rgb_a = a;
375 }
376
377 void Polyline::setClosed(bool c)
378 {
379 closed = c;
380 }
381
382 void Polyline::setCloseStippled(bool c)
383 {
384 stippled = c;
385 }
386
387 void Polyline::setLineWidth(float l)
388 {
389 line = l;
390 }
391
392 void Polyline::addNode(const QPoint& p)
393 {
394 _cNodeVector.push_back(p);
395 }
396
397 void Polyline::popNode()
398 {
399 if (!_cNodeVector.empty()) {
400 _cNodeVector.pop_back();
401 }
402 }
403
404 void Polyline::clear()
405 {
406 _cNodeVector.clear();
407 }
408
409 void Polyline::paintGL()
410 {
411 if (!working) {
412 return;
413 }
414
415 if (_cNodeVector.empty()) {
416 return;
417 }
418
419 const SbViewportRegion vp = viewer->getSoRenderManager()->getViewportRegion();
420 SbVec2s size = vp.getViewportSizePixels();
421
422 glMatrixMode(GL_PROJECTION);
423 glLoadIdentity();
424 glOrtho(0, size[0], size[1], 0, 0, 100);
425 glMatrixMode(GL_MODELVIEW);
426 glLoadIdentity();
427 glDisable(GL_TEXTURE_2D);
428 glEnable(GL_BLEND);
429 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
430 glLineWidth(line);
431 glColor4f(rgb_r, rgb_g, rgb_b, rgb_a);
432
433 if (closed && !stippled) {
434 glBegin(GL_LINE_LOOP);
435
436 for (const QPoint& it : _cNodeVector) {
437 glVertex2i(it.x(), it.y());
438 }
439
440 glEnd();
441 }
442 else {
443 glBegin(GL_LINES);
444
445 QPoint start = _cNodeVector.front();
446 for (const QPoint& it : _cNodeVector) {
447 glVertex2i(start.x(), start.y());
448 start = it;
449 glVertex2i(it.x(), it.y());
450 }
451
452 glEnd();
453
454 if (closed && stippled) {
455 glEnable(GL_LINE_STIPPLE);
456 glLineStipple(2, 0x3F3F);
457 glBegin(GL_LINES);
458 glVertex2i(_cNodeVector.back().x(), _cNodeVector.back().y());
459 glVertex2i(_cNodeVector.front().x(), _cNodeVector.front().y());
460 glEnd();
461 glDisable(GL_LINE_STIPPLE);
462 }
463 }
464
465 glDisable(GL_BLEND);
466 }