src/gldraw.cpp

changeset 194
cfe9ae5f1124
parent 193
ce8e25ccbaf6
child 195
7a776f6b0d2a
equal deleted inserted replaced
193:ce8e25ccbaf6 194:cfe9ae5f1124
382 glMatrixMode (GL_MODELVIEW); 382 glMatrixMode (GL_MODELVIEW);
383 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); 383 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
384 } 384 }
385 385
386 // ============================================================================= 386 // =============================================================================
387 vertex GLRenderer::coord_2to3 (const QPoint& pos2d, const bool snap) const { 387 vertex GLRenderer::coordconv2_3 (const QPoint& pos2d, bool snap) const {
388 vertex pos3d; 388 vertex pos3d;
389 const staticCameraMeta* cam = &g_staticCameras[m_camera]; 389 const staticCameraMeta* cam = &g_staticCameras[m_camera];
390 const Axis axisX = cam->axisX; 390 const Axis axisX = cam->axisX;
391 const Axis axisY = cam->axisY; 391 const Axis axisY = cam->axisY;
392 const short negXFac = cam->negX ? -1 : 1, 392 const short negXFac = cam->negX ? -1 : 1,
409 pos3d[axisY] = cy; 409 pos3d[axisY] = cy;
410 return pos3d; 410 return pos3d;
411 } 411 }
412 412
413 // ============================================================================= 413 // =============================================================================
414 QPoint GLRenderer::coord_3to2 (const vertex& pos3d) const { 414 QPoint GLRenderer::coordconv3_2 (const vertex& pos3d) const {
415 /* 415 GLfloat m[16];
416 cx = (-m_virtWidth + ((2 * pos2d.x () * m_virtWidth) / m_width) - m_panX) - (negXFac * g_objOffset[axisX]);
417
418 cx = (-vw + ((2 * x * vw) / w) - panx) - (neg * ofs)
419 cx + (neg * ofs) = (-vw + ((2 * x * vw) / w) - panx)
420 cx + (neg * ofs) = ((2 * x * vw) / w) - vw - panx
421 (cx + (neg * ofs)) + vw + panx = (2 * x * vw) / w
422 ((cx + (neg * ofs)) + vw + panx) * w = 2 * vw * x
423
424 x = (((cx + (neg * ofs)) + vw + panx) * w) / (2 * vw)
425 */
426
427 QPoint pos2d;
428 const staticCameraMeta* cam = &g_staticCameras[m_camera]; 416 const staticCameraMeta* cam = &g_staticCameras[m_camera];
429 const Axis axisX = cam->axisX; 417 const Axis axisX = cam->axisX;
430 const Axis axisY = cam->axisY; 418 const Axis axisY = cam->axisY;
431 const short negXFac = cam->negX ? -1 : 1, 419 const short negXFac = cam->negX ? -1 : 1,
432 negYFac = cam->negY ? -1 : 1; 420 negYFac = cam->negY ? -1 : 1;
433 421
434 short x1 = (((pos3d[axisX] + (negXFac * g_objOffset[axisX])) + 422 glGetFloatv (GL_MODELVIEW_MATRIX, m);
435 m_virtWidth + m_panX) * m_width) / (2 * m_virtWidth); 423
436 short y1 = -(((pos3d[axisY] + (negYFac * g_objOffset[axisY])) - 424 const double x = pos3d.x ();
437 m_virtHeight + m_panY) * m_height) / (2 * m_virtHeight); 425 const double y = pos3d.y ();
438 426 const double z = pos3d.z ();
439 x1 *= negXFac; 427
440 y1 *= negYFac; 428 vertex transformed;
441 429 transformed[X] = (m[0] * x) + (m[1] * y) + (m[2] * z) + m[3];
442 pos2d = QPoint (x1, y1); 430 transformed[Y] = (m[4] * x) + (m[5] * y) + (m[6] * z) + m[7];
443 return pos2d; 431 transformed[Z] = (m[8] * x) + (m[9] * y) + (m[10] * z) + m[11];
432
433 double rx = (((transformed[axisX] * negXFac) + (negXFac * g_objOffset[axisX])
434 + m_virtWidth + m_panX) * m_width) / (2 * m_virtWidth);
435 double ry = (((transformed[axisY] * negYFac) + (negYFac * g_objOffset[axisY])
436 - m_virtHeight + m_panY) * m_height) / (2 * m_virtHeight);
437
438 return QPoint (rx, -ry);
444 } 439 }
445 440
446 // ============================================================================= 441 // =============================================================================
447 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 442 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
448 // ============================================================================= 443 // =============================================================================
471 466
472 m_hoverpos = g_origin; 467 m_hoverpos = g_origin;
473 468
474 if (m_camera != Free) { 469 if (m_camera != Free) {
475 // Calculate 3d position of the cursor 470 // Calculate 3d position of the cursor
476 m_hoverpos = coord_2to3 (m_pos, true); 471 m_hoverpos = coordconv2_3 (m_pos, true);
477 472
478 // Paint the coordinates onto the screen. 473 // Paint the coordinates onto the screen.
479 str text; 474 str text;
480 text.format ("X: %s, Y: %s, Z: %s", ftoa (m_hoverpos[X]).chars (), 475 text.format ("X: %s, Y: %s, Z: %s", ftoa (m_hoverpos[X]).chars (),
481 ftoa (m_hoverpos[Y]).chars (), ftoa (m_hoverpos[Z]).chars ()); 476 ftoa (m_hoverpos[Y]).chars (), ftoa (m_hoverpos[Z]).chars ());
494 if (numverts > 0) { 489 if (numverts > 0) {
495 QPoint* poly = new QPoint[numverts]; 490 QPoint* poly = new QPoint[numverts];
496 491
497 uchar i = 0; 492 uchar i = 0;
498 for (vertex& vert : m_planeDrawVerts) { 493 for (vertex& vert : m_planeDrawVerts) {
499 poly[i] = coord_3to2 (vert); 494 poly[i] = coordconv3_2 (vert);
500 ++i; 495 ++i;
501 } 496 }
502 497
503 // Draw the cursor vertex as the last one in the list. 498 // Draw the cursor vertex as the last one in the list.
504 if (numverts < 5) 499 if (numverts < 5)
505 poly[i] = coord_3to2 (m_hoverpos); 500 poly[i] = coordconv3_2 (m_hoverpos);
506 else 501 else
507 numverts = 4; 502 numverts = 4;
508 503
509 paint.setPen (m_thinBorderPen); 504 paint.setPen (m_thinBorderPen);
510 paint.setBrush (QColor (128, 192, 0)); 505 paint.setBrush (QColor (128, 192, 0));
621 616
622 for (LDObject* obj : g_curfile->m_objs) 617 for (LDObject* obj : g_curfile->m_objs)
623 compileObject (obj); 618 compileObject (obj);
624 619
625 // Compile axes 620 // Compile axes
621 glDeleteLists (m_axeslist, 1);
626 m_axeslist = glGenLists (1); 622 m_axeslist = glGenLists (1);
627 glNewList (m_axeslist, GL_COMPILE); 623 glNewList (m_axeslist, GL_COMPILE);
628 glBegin (GL_LINES); 624 glBegin (GL_LINES);
629 for (const GLAxis& ax : g_GLAxes) { 625 for (const GLAxis& ax : g_GLAxes) {
630 qglColor (ax.col); 626 qglColor (ax.col);
780 } 776 }
781 777
782 // ============================================================================= 778 // =============================================================================
783 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 779 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
784 // ============================================================================= 780 // =============================================================================
785 void GLRenderer::clampAngle (double& angle) { 781 void GLRenderer::clampAngle (double& angle) const {
786 while (angle < 0) 782 while (angle < 0)
787 angle += 360.0; 783 angle += 360.0;
788 while (angle > 360.0) 784 while (angle > 360.0)
789 angle -= 360.0; 785 angle -= 360.0;
790 } 786 }
1156 1152
1157 // ============================================================================= 1153 // =============================================================================
1158 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 1154 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1159 // ============================================================================= 1155 // =============================================================================
1160 void GLRenderer::compileObject (LDObject* obj) { 1156 void GLRenderer::compileObject (LDObject* obj) {
1157 deleteLists (obj);
1158
1161 for (const GL::ListType listType : g_glListTypes) { 1159 for (const GL::ListType listType : g_glListTypes) {
1162 glDeleteLists (obj->glLists[listType], 1);
1163
1164 GLuint list = glGenLists (1); 1160 GLuint list = glGenLists (1);
1165 glNewList (list, GL_COMPILE); 1161 glNewList (list, GL_COMPILE);
1166 1162
1167 obj->glLists[listType] = list; 1163 obj->glLists[listType] = list;
1168 compileList (obj, listType); 1164 compileList (obj, listType);
1202 update (); 1198 update ();
1203 break; 1199 break;
1204 } 1200 }
1205 } 1201 }
1206 } 1202 }
1203
1204 // =============================================================================
1205 void GLRenderer::deleteLists (LDObject* obj) {
1206 for (const GL::ListType listType : g_glListTypes)
1207 glDeleteLists (obj->glLists[listType], 1);
1208 }

mercurial