145:ddf24c380be6 | 146:2ab24976acaa |
---|---|
56 // ============================================================================= | 56 // ============================================================================= |
57 void GLRenderer::resetAngles () { | 57 void GLRenderer::resetAngles () { |
58 rotX = 30.0f; | 58 rotX = 30.0f; |
59 rotY = 325.f; | 59 rotY = 325.f; |
60 panX = panY = rotZ = 0.0f; | 60 panX = panY = rotZ = 0.0f; |
61 zoom = 1.0f; | 61 zoom = 5.0f; |
62 } | 62 } |
63 | 63 |
64 // ============================================================================= | 64 // ============================================================================= |
65 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | 65 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
66 // ============================================================================= | 66 // ============================================================================= |
83 glEnable (GL_LINE_SMOOTH); | 83 glEnable (GL_LINE_SMOOTH); |
84 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST); | 84 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST); |
85 | 85 |
86 glLineWidth (gl_linethickness); | 86 glLineWidth (gl_linethickness); |
87 | 87 |
88 setAutoFillBackground (false); | |
88 setMouseTracking (true); | 89 setMouseTracking (true); |
89 setFocusPolicy (Qt::WheelFocus); | 90 setFocusPolicy (Qt::WheelFocus); |
90 compileObjects (); | 91 compileObjects (); |
91 } | 92 } |
92 | 93 |
221 | 222 |
222 // ============================================================================= | 223 // ============================================================================= |
223 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | 224 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
224 // ============================================================================= | 225 // ============================================================================= |
225 void GLRenderer::refresh () { | 226 void GLRenderer::refresh () { |
226 paintGL (); | 227 update (); |
227 swapBuffers (); | 228 swapBuffers (); |
228 } | 229 } |
229 | 230 |
230 // ============================================================================= | 231 // ============================================================================= |
231 void GLRenderer::hardRefresh () { | 232 void GLRenderer::hardRefresh () { |
243 height = h; | 244 height = h; |
244 | 245 |
245 glViewport (0, 0, w, h); | 246 glViewport (0, 0, w, h); |
246 glMatrixMode (GL_PROJECTION); | 247 glMatrixMode (GL_PROJECTION); |
247 glLoadIdentity (); | 248 glLoadIdentity (); |
248 gluPerspective (45.0f, (double)w / (double)h, 0.1f, 100.0f); | 249 gluPerspective (45.0f, (double)w / (double)h, 1.0f, 100.0f); |
249 glMatrixMode (GL_MODELVIEW); | 250 glMatrixMode (GL_MODELVIEW); |
250 } | 251 } |
251 | 252 |
252 // ============================================================================= | 253 char g_staticCameras[6][3] = { |
253 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | 254 { 0, 0, 1 }, |
254 // ============================================================================= | 255 { 0, 0, -1 }, |
255 void GLRenderer::paintGL () { | 256 { 1, 0, 0 }, |
257 { -1, 0, 0 }, | |
258 { 0, 1, 0 }, | |
259 { 0, -1, 0 }, | |
260 }; | |
261 | |
262 GLRenderer::Camera cam = GLRenderer::Front; | |
263 | |
264 void GLRenderer::drawGLScene () { | |
256 if (g_CurrentFile == null) | 265 if (g_CurrentFile == null) |
257 return; | 266 return; |
258 | 267 |
259 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 268 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
260 | 269 glEnable (GL_DEPTH_TEST); |
261 glMatrixMode (GL_PROJECTION); | 270 |
262 | 271 if (cam != GLRenderer::Free) { |
263 glPushMatrix (); | 272 glMatrixMode (GL_PROJECTION); |
273 glPushMatrix (); | |
274 | |
264 glLoadIdentity (); | 275 glLoadIdentity (); |
265 | 276 glOrtho (-vw, vw, -vh, vh, -100.0, 100.0); |
266 double x = zoom; | 277 glTranslatef (panX, panY, 0.0f); |
267 double y = (height * x) / width; | 278 glRotatef (90.f, g_staticCameras[cam][0], g_staticCameras[cam][1], g_staticCameras[cam][2]); |
268 | 279 } else { |
269 glOrtho (-x, x, -y, y, -100.0, 100.0); | 280 glMatrixMode (GL_MODELVIEW); |
270 | 281 glPushMatrix (); |
271 glTranslatef (panX, panY, -5.0f); | 282 glLoadIdentity (); |
272 glRotatef (90.f, 0.0f, 1.0f, 0.0f); | 283 |
273 | 284 glTranslatef (0.0f, 0.0f, -5.0f); |
285 glTranslatef (panX, panY, -zoom); | |
286 glRotatef (rotX, 1.0f, 0.0f, 0.0f); | |
287 glRotatef (rotY, 0.0f, 1.0f, 0.0f); | |
288 glRotatef (rotZ, 0.0f, 0.0f, 1.0f); | |
289 } | |
290 | |
274 for (LDObject* obj : g_CurrentFile->objects) | 291 for (LDObject* obj : g_CurrentFile->objects) |
275 glCallList ((picking == false) ? obj->uGLList : obj->uGLPickList); | 292 glCallList ((picking == false) ? obj->uGLList : obj->uGLPickList); |
276 glPopMatrix (); | 293 glPopMatrix (); |
277 glMatrixMode (GL_MODELVIEW); | 294 glMatrixMode (GL_MODELVIEW); |
278 | 295 } |
279 #if 0 | 296 |
280 glMatrixMode (GL_MODELVIEW); | 297 // ============================================================================= |
281 | 298 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
282 glPushMatrix (); | 299 // ============================================================================= |
283 glLoadIdentity (); | 300 void GLRenderer::paintEvent (QPaintEvent* ev) { |
284 | 301 drawGLScene (); |
285 glTranslatef (0.0f, 0.0f, -5.0f); | 302 |
286 glTranslatef (panX, panY, -zoom); | 303 vw = zoom; |
287 | 304 vh = (height * vw) / width; |
288 glRotatef (rotX, 1.0f, 0.0f, 0.0f); | 305 const double cx = Grid::snap ((-vw + ((2 * mouseX * vw) / width) - panX) * g_StoredBBoxSize - (g_faObjectOffset[0]), Grid::X); |
289 glRotatef (rotY, 0.0f, 1.0f, 0.0f); | 306 const double cy = Grid::snap ((vh - ((2 * mouseY * vh) / height) - panY) * g_StoredBBoxSize - (g_faObjectOffset[2]), Grid::Z); |
290 glRotatef (rotZ, 0.0f, 0.0f, 1.0f); | 307 |
291 | 308 str text; |
292 for (LDObject* obj : g_CurrentFile->objects) | 309 text.format ("(%.3f, %.3f)", cx, cy); |
293 glCallList ((picking == false) ? obj->uGLList : obj->uGLPickList); | 310 QFontMetrics metrics = QFontMetrics (font ()); |
294 glPopMatrix (); | 311 QRect textSize = metrics.boundingRect (0, 0, width, height, Qt::AlignCenter, text); |
295 #endif | 312 |
313 QPainter paint (this); | |
314 paint.setRenderHint (QPainter::Antialiasing); | |
315 paint.drawText (width - textSize.width (), height - 16, textSize.width (), | |
316 textSize.height (), Qt::AlignCenter, text); | |
296 | 317 |
297 // If we're range-picking, draw a rectangle encompassing the selection area. | 318 // If we're range-picking, draw a rectangle encompassing the selection area. |
298 if (rangepick && !picking) { | 319 if (rangepick && !picking) { |
299 const short x0 = rangeStart.x (), | 320 const short x0 = rangeStart.x (), |
300 y0 = rangeStart.y (), | 321 y0 = rangeStart.y (), |
301 x1 = pos.x (), | 322 x1 = pos.x (), |
302 y1 = pos.y (); | 323 y1 = pos.y (); |
303 | 324 |
325 QRect rect (x0, y0, x1 - x0, y1 - y0); | |
326 QColor fillColor = (addpick ? "#80FF00" : "#00CCFF"); | |
327 QColor borderColor = Qt::black; | |
328 fillColor.setAlphaF (0.2f); | |
329 borderColor.setAlphaF (0.8f); | |
330 QPen borderPen (borderColor, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin); | |
331 | |
332 paint.setPen (borderPen); | |
333 paint.setBrush (QBrush (fillColor)); | |
334 paint.drawRect (rect); | |
335 | |
336 #if 0 | |
304 glMatrixMode (GL_PROJECTION); | 337 glMatrixMode (GL_PROJECTION); |
305 | 338 |
306 glPushMatrix (); | 339 glPushMatrix (); |
307 glLoadIdentity (); | 340 glLoadIdentity (); |
308 glOrtho (.0, width, height, .0, -1.0, 1.0); | 341 glOrtho (.0, width, height, .0, -1.0, 1.0); |
329 glEnd (); | 362 glEnd (); |
330 } | 363 } |
331 glPopMatrix (); | 364 glPopMatrix (); |
332 | 365 |
333 glMatrixMode (GL_MODELVIEW); | 366 glMatrixMode (GL_MODELVIEW); |
367 #endif | |
334 } | 368 } |
335 } | 369 } |
336 | 370 |
337 // ============================================================================= | 371 // ============================================================================= |
338 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | 372 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
513 totalmove = 0; | 547 totalmove = 0; |
514 } | 548 } |
515 } | 549 } |
516 | 550 |
517 // ============================================================================= | 551 // ============================================================================= |
552 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | |
553 // ============================================================================= | |
518 void GLRenderer::mousePressEvent (QMouseEvent* ev) { | 554 void GLRenderer::mousePressEvent (QMouseEvent* ev) { |
519 if (ev->buttons () & Qt::LeftButton) | 555 if (ev->buttons () & Qt::LeftButton) |
520 totalmove = 0; | 556 totalmove = 0; |
521 | 557 |
522 if (ev->modifiers () & Qt::ShiftModifier) { | 558 if (ev->modifiers () & Qt::ShiftModifier) { |
553 panX += 0.03f * dx * (zoom / 7.5f); | 589 panX += 0.03f * dx * (zoom / 7.5f); |
554 panY -= 0.03f * dy * (zoom / 7.5f); | 590 panY -= 0.03f * dy * (zoom / 7.5f); |
555 } | 591 } |
556 | 592 |
557 pos = ev->pos (); | 593 pos = ev->pos (); |
558 updateGL (); | 594 update (); |
559 } | 595 } |
560 | 596 |
561 // ============================================================================= | 597 // ============================================================================= |
562 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | 598 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
563 // ============================================================================= | 599 // ============================================================================= |
569 keymods = ev->modifiers (); | 605 keymods = ev->modifiers (); |
570 } | 606 } |
571 | 607 |
572 // ============================================================================= | 608 // ============================================================================= |
573 void GLRenderer::wheelEvent (QWheelEvent* ev) { | 609 void GLRenderer::wheelEvent (QWheelEvent* ev) { |
574 printf ("%.5f -> ", zoom); | |
575 // zoom += (-ev->delta () / 100.0); | |
576 zoom *= (ev->delta () < 0) ? 1.2f : (1.0f / 1.2f); | 610 zoom *= (ev->delta () < 0) ? 1.2f : (1.0f / 1.2f); |
577 printf ("%.5f\n", zoom); | |
578 zoom = clamp (zoom, 0.01, 100.0); | 611 zoom = clamp (zoom, 0.01, 100.0); |
612 | |
613 update (); | |
579 ev->accept (); | 614 ev->accept (); |
580 updateGL (); | |
581 } | 615 } |
582 | 616 |
583 // ============================================================================= | 617 // ============================================================================= |
584 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * | 618 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
585 // ============================================================================= | 619 // ============================================================================= |
589 g_dPulseTick = 0; | 623 g_dPulseTick = 0; |
590 } else | 624 } else |
591 pulseTimer->stop (); | 625 pulseTimer->stop (); |
592 } | 626 } |
593 | 627 |
594 // ========================================================================= // | 628 // ============================================================================= |
595 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // | 629 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
596 // ========================================================================= // | 630 // ============================================================================= |
597 void GLRenderer::pick (uint mouseX, uint mouseY) { | 631 void GLRenderer::pick (uint mouseX, uint mouseY) { |
598 GLint viewport[4]; | 632 GLint viewport[4]; |
599 LDObject* removedObject = null; | 633 LDObject* removedObject = null; |
600 | 634 |
601 // Clear the selection if we do not wish to add to it. | 635 // Clear the selection if we do not wish to add to it. |
611 picking = true; | 645 picking = true; |
612 | 646 |
613 // Paint the picking scene | 647 // Paint the picking scene |
614 glDisable (GL_DITHER); | 648 glDisable (GL_DITHER); |
615 glClearColor (1.0f, 1.0f, 1.0f, 1.0f); | 649 glClearColor (1.0f, 1.0f, 1.0f, 1.0f); |
616 paintGL (); | 650 drawGLScene (); |
617 | 651 |
618 glGetIntegerv (GL_VIEWPORT, viewport); | 652 glGetIntegerv (GL_VIEWPORT, viewport); |
619 | 653 |
620 short x0 = mouseX, | 654 short x0 = mouseX, |
621 y0 = mouseY; | 655 y0 = mouseY; |
713 recompileObject (obj); | 747 recompileObject (obj); |
714 | 748 |
715 if (removedObject != null) | 749 if (removedObject != null) |
716 recompileObject (removedObject); | 750 recompileObject (removedObject); |
717 | 751 |
718 paintGL (); | 752 drawGLScene (); |
719 swapBuffers (); | 753 swapBuffers (); |
754 update (); | |
720 } | 755 } |
721 | 756 |
722 // ========================================================================= // | 757 // ========================================================================= // |
723 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // | 758 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // |
724 // ========================================================================= // | 759 // ========================================================================= // |
743 ++g_dPulseTick %= g_dNumPulseTicks; | 778 ++g_dPulseTick %= g_dNumPulseTicks; |
744 | 779 |
745 for (LDObject* obj : g_ForgeWindow->sel) | 780 for (LDObject* obj : g_ForgeWindow->sel) |
746 recompileObject (obj); | 781 recompileObject (obj); |
747 | 782 |
748 paintGL (); | 783 update (); |
749 swapBuffers (); | |
750 } | 784 } |
751 | 785 |
752 // ============================================================================= | 786 // ============================================================================= |
753 uchar* GLRenderer::screencap (ushort& w, ushort& h) { | 787 uchar* GLRenderer::screencap (ushort& w, ushort& h) { |
754 w = width; | 788 w = width; |
755 h = height; | 789 h = height; |
756 uchar* cap = new uchar[4 * w * h]; | 790 uchar* cap = new uchar[4 * w * h]; |
757 paintGL (); | 791 update (); |
758 | 792 |
759 // Capture the pixels | 793 // Capture the pixels |
760 glReadPixels (0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, cap); | 794 glReadPixels (0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, cap); |
761 | 795 |
762 // Restore the background | 796 // Restore the background |