gldraw.cpp

changeset 182
9374fea8f77f
parent 170
2247a32e63a8
equal deleted inserted replaced
181:a61f07b7965c 182:9374fea8f77f
28 #include "gui.h" 28 #include "gui.h"
29 #include "misc.h" 29 #include "misc.h"
30 #include "history.h" 30 #include "history.h"
31 31
32 static double g_objOffset[3]; 32 static double g_objOffset[3];
33 static double g_storedBBoxSize;
34 33
35 static short g_pulseTick = 0; 34 static short g_pulseTick = 0;
36 static const short g_numPulseTicks = 8; 35 static const short g_numPulseTicks = 8;
37 static const short g_pulseInterval = 65; 36 static const short g_pulseInterval = 65;
38 37
150 // ============================================================================= 149 // =============================================================================
151 void GLRenderer::resetAngles () { 150 void GLRenderer::resetAngles () {
152 m_rotX = 30.0f; 151 m_rotX = 30.0f;
153 m_rotY = 325.f; 152 m_rotY = 325.f;
154 m_panX = m_panY = m_rotZ = 0.0f; 153 m_panX = m_panY = m_rotZ = 0.0f;
155 m_zoom = 5.0f; 154
155 // Set the default zoom based on the bounding box
156 m_zoom = g_BBox.size () * 6;
156 } 157 }
157 158
158 // ============================================================================= 159 // =============================================================================
159 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 160 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
160 // ============================================================================= 161 // =============================================================================
346 calcCameraIconRects (); 347 calcCameraIconRects ();
347 348
348 glViewport (0, 0, w, h); 349 glViewport (0, 0, w, h);
349 glMatrixMode (GL_PROJECTION); 350 glMatrixMode (GL_PROJECTION);
350 glLoadIdentity (); 351 glLoadIdentity ();
351 gluPerspective (45.0f, (double)w / (double)h, 1.0f, 100.0f); 352 gluPerspective (45.0f, (double)w / (double)h, 1.0f, 10000.0f);
352 glMatrixMode (GL_MODELVIEW); 353 glMatrixMode (GL_MODELVIEW);
353 } 354 }
354 355
355 void GLRenderer::drawGLScene () const { 356 void GLRenderer::drawGLScene () const {
356 if (g_curfile == null) 357 if (g_curfile == null)
409 const Axis axisY = cam->axisY; 410 const Axis axisY = cam->axisY;
410 const short negXFac = cam->negX ? -1 : 1, 411 const short negXFac = cam->negX ? -1 : 1,
411 negYFac = cam->negY ? -1 : 1; 412 negYFac = cam->negY ? -1 : 1;
412 413
413 // Calculate cx and cy - these are the LDraw unit coords the cursor is at. 414 // Calculate cx and cy - these are the LDraw unit coords the cursor is at.
414 double cx = (-m_virtWidth + ((2 * pos2d.x () * m_virtWidth) / m_width) - m_panX) * 415 double cx = (-m_virtWidth + ((2 * pos2d.x () * m_virtWidth) / m_width) - m_panX) - (negXFac * g_objOffset[axisX]);
415 g_storedBBoxSize - (negXFac * g_objOffset[axisX]); 416 double cy = (m_virtHeight - ((2 * pos2d.y () * m_virtHeight) / m_height) - m_panY) - (negYFac * g_objOffset[axisY]);
416 double cy = (m_virtHeight - ((2 * pos2d.y () * m_virtHeight) / m_height) - m_panY) *
417 g_storedBBoxSize - (negYFac * g_objOffset[axisY]);
418 417
419 if (snap) { 418 if (snap) {
420 cx = Grid::snap (cx, (Grid::Config) axisX); 419 cx = Grid::snap (cx, (Grid::Config) axisX);
421 cy = Grid::snap (cy, (Grid::Config) axisY); 420 cy = Grid::snap (cy, (Grid::Config) axisY);
422 } 421 }
431 } 430 }
432 431
433 // ============================================================================= 432 // =============================================================================
434 QPoint GLRenderer::coord_3to2 (const vertex& pos3d) const { 433 QPoint GLRenderer::coord_3to2 (const vertex& pos3d) const {
435 /* 434 /*
436 cx = (-m_virtWidth + ((2 * pos2d.x () * m_virtWidth) / m_width) - m_panX) * g_storedBBoxSize - (negXFac * g_objOffset[axisX]); 435 cx = (-m_virtWidth + ((2 * pos2d.x () * m_virtWidth) / m_width) - m_panX) - (negXFac * g_objOffset[axisX]);
437 436
438 cx = (-vw + ((2 * x * vw) / w) - panx) * size - (neg * ofs) 437 cx = (-vw + ((2 * x * vw) / w) - panx) - (neg * ofs)
439 cx + (neg * ofs) = (-vw + ((2 * x * vw) / w) - panx) * size 438 cx + (neg * ofs) = (-vw + ((2 * x * vw) / w) - panx)
440 (cx + (neg * ofs)) / size = ((2 * x * vw) / w) - vw - panx 439 cx + (neg * ofs) = ((2 * x * vw) / w) - vw - panx
441 ((cx + (neg * ofs)) / size) + vw + panx = (2 * x * vw) / w 440 (cx + (neg * ofs)) + vw + panx = (2 * x * vw) / w
442 (((cx + (neg * ofs)) / size) + vw + panx) * w = 2 * vw * x 441 ((cx + (neg * ofs)) + vw + panx) * w = 2 * vw * x
443 442
444 x = ((((cx + (neg * ofs)) / size) + vw + panx) * w) / (2 * vw) 443 x = (((cx + (neg * ofs)) + vw + panx) * w) / (2 * vw)
445 */ 444 */
446 445
447 QPoint pos2d; 446 QPoint pos2d;
448 const staticCameraMeta* cam = &g_staticCameras[m_camera]; 447 const staticCameraMeta* cam = &g_staticCameras[m_camera];
449 const Axis axisX = cam->axisX; 448 const Axis axisX = cam->axisX;
450 const Axis axisY = cam->axisY; 449 const Axis axisY = cam->axisY;
451 const short negXFac = cam->negX ? -1 : 1, 450 const short negXFac = cam->negX ? -1 : 1,
452 negYFac = cam->negY ? -1 : 1; 451 negYFac = cam->negY ? -1 : 1;
453 452
454 short x1 = ((((pos3d[axisX] + (negXFac * g_objOffset[axisX])) / g_storedBBoxSize) + 453 short x1 = (((pos3d[axisX] + (negXFac * g_objOffset[axisX])) +
455 m_virtWidth + m_panX) * m_width) / (2 * m_virtWidth); 454 m_virtWidth + m_panX) * m_width) / (2 * m_virtWidth);
456 short y1 = -((((pos3d[axisY] + (negYFac * g_objOffset[axisY])) / g_storedBBoxSize) - 455 short y1 = -(((pos3d[axisY] + (negYFac * g_objOffset[axisY])) -
457 m_virtHeight + m_panY) * m_height) / (2 * m_virtHeight); 456 m_virtHeight + m_panY) * m_height) / (2 * m_virtHeight);
458 457
459 x1 *= negXFac; 458 x1 *= negXFac;
460 y1 *= negYFac; 459 y1 *= negYFac;
461 460
614 void GLRenderer::compileObjects () { 613 void GLRenderer::compileObjects () {
615 if (g_BBox.empty () == false) { 614 if (g_BBox.empty () == false) {
616 g_objOffset[X] = -(g_BBox.v0 ()[X] + g_BBox.v1 ()[X]) / 2; 615 g_objOffset[X] = -(g_BBox.v0 ()[X] + g_BBox.v1 ()[X]) / 2;
617 g_objOffset[Y] = -(g_BBox.v0 ()[Y] + g_BBox.v1 ()[Y]) / 2; 616 g_objOffset[Y] = -(g_BBox.v0 ()[Y] + g_BBox.v1 ()[Y]) / 2;
618 g_objOffset[Z] = -(g_BBox.v0 ()[Z] + g_BBox.v1 ()[Z]) / 2; 617 g_objOffset[Z] = -(g_BBox.v0 ()[Z] + g_BBox.v1 ()[Z]) / 2;
619 g_storedBBoxSize = g_BBox.size ();
620 } else { 618 } else {
621 // use a default bbox if we need 619 // use a default bbox if we need
622 g_objOffset[X] = g_objOffset[Y] = g_objOffset[Z] = 0; 620 g_objOffset[X] = g_objOffset[Y] = g_objOffset[Z] = 0;
623 g_storedBBoxSize = 5.0f; 621 }
624 }
625 printf ("size: %.3f\n", g_storedBBoxSize);
626 622
627 if (!g_curfile) { 623 if (!g_curfile) {
628 printf ("renderer: no files loaded, cannot compile anything\n"); 624 printf ("renderer: no files loaded, cannot compile anything\n");
629 return; 625 return;
630 } 626 }
769 // ============================================================================= 765 // =============================================================================
770 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 766 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
771 // ============================================================================= 767 // =============================================================================
772 void GLRenderer::compileVertex (const vertex& vrt) { 768 void GLRenderer::compileVertex (const vertex& vrt) {
773 glVertex3d ( 769 glVertex3d (
774 (vrt[X] + g_objOffset[0]) / g_storedBBoxSize, 770 (vrt[X] + g_objOffset[0]),
775 -(vrt[Y] + g_objOffset[1]) / g_storedBBoxSize, 771 -(vrt[Y] + g_objOffset[1]),
776 -(vrt[Z] + g_objOffset[2]) / g_storedBBoxSize); 772 -(vrt[Z] + g_objOffset[2]));
777 } 773 }
778 774
779 // ============================================================================= 775 // =============================================================================
780 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 776 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
781 // ============================================================================= 777 // =============================================================================
896 } 892 }
897 893
898 // ============================================================================= 894 // =============================================================================
899 void GLRenderer::wheelEvent (QWheelEvent* ev) { 895 void GLRenderer::wheelEvent (QWheelEvent* ev) {
900 m_zoom *= (ev->delta () < 0) ? 1.2f : (1.0f / 1.2f); 896 m_zoom *= (ev->delta () < 0) ? 1.2f : (1.0f / 1.2f);
901 m_zoom = clamp (m_zoom, 0.01, 100.0); 897 m_zoom = clamp (m_zoom, 0.01, 10000.0);
902 898
903 update (); 899 update ();
904 ev->accept (); 900 ev->accept ();
905 } 901 }
906 902

mercurial