kdeglwidget.cpp
上传用户:center1979
上传日期:2022-07-26
资源大小:50633k
文件大小:15k
源码类别:

OpenGL

开发平台:

Visual C++

  1. /***************************************************************************
  2.                           kdeglwidget.cpp  -  description
  3.                              -------------------
  4.     begin                : Tue Jul 16 2002
  5.     copyright            : (C) 2002 by Christophe Teyssier
  6.     email                : chris@teyssier.org
  7.  ***************************************************************************/
  8. /***************************************************************************
  9.  *                                                                         *
  10.  *   This program is free software; you can redistribute it and/or modify  *
  11.  *   it under the terms of the GNU General Public License as published by  *
  12.  *   the Free Software Foundation; either version 2 of the License, or     *
  13.  *   (at your option) any later version.                                   *
  14.  *                                                                         *
  15.  ***************************************************************************/
  16. #include "kdeglwidget.h"
  17. #include <kaccel.h>
  18. #include <unistd.h>
  19. #include <celengine/gl.h>
  20. //#include <celengine/glext.h>
  21. #include <celengine/celestia.h>
  22. #include <celengine/starbrowser.h>
  23. #include <kmainwindow.h>
  24. #include <kconfig.h>
  25. #include <qcursor.h>
  26. #include <qpaintdevicemetrics.h>
  27. #ifndef DEBUG
  28. #  define G_DISABLE_ASSERT
  29. #endif
  30. #include "celmath/vecmath.h"
  31. #include "celmath/quaternion.h"
  32. #include "celmath/mathlib.h"
  33. #include "celengine/astro.h"
  34. #include "celutil/util.h"
  35. #include "celutil/filetype.h"
  36. #include "celutil/debug.h"
  37. #include "imagecapture.h"
  38. #include "celestiacore.h"
  39. #include "celengine/simulation.h"
  40. #include "celengine/glcontext.h"
  41. #include "kdeapp.h"
  42. #include <math.h>
  43. #include <vector>
  44. KdeGlWidget::KdeGlWidget(  QWidget* parent, const char* name, CelestiaCore* core)
  45.     : QGLWidget( parent, name )
  46. {
  47.     actionColl = ((KdeApp*)parent)->actionCollection();
  48.     setFocusPolicy(QWidget::ClickFocus);
  49.     appCore = core;
  50.     appRenderer=appCore->getRenderer();
  51.     appSim = appCore->getSimulation();
  52.     setCursor(QCursor(Qt::CrossCursor));
  53.     currentCursor = CelestiaCore::CrossCursor;
  54.     setMouseTracking(true);
  55.     appCore->setCursorHandler(this);
  56.     lastX = lastY = 0;
  57. }
  58. /*!
  59.   Release allocated resources
  60. */
  61. KdeGlWidget::~KdeGlWidget()
  62. {
  63. }
  64. /*!
  65.   Paint the box. The actual openGL commands for drawing the box are
  66.   performed here.
  67. */
  68. void KdeGlWidget::paintGL()
  69. {
  70.     appCore->draw();
  71. }
  72. /*!
  73.   Set up the OpenGL rendering state, and define display list
  74. */
  75. void KdeGlWidget::initializeGL()
  76. {
  77.     if (!appCore->initRenderer())
  78.     {
  79. //        cerr << "Failed to initialize renderer.n";
  80.         exit(1);
  81.     }
  82.     time_t curtime=time(NULL);
  83.     appCore->start(astro::UTCtoTDB((double) curtime / 86400.0 + (double) astro::Date(1970, 1, 1)));
  84.     localtime(&curtime); /* Only doing this to set timezone as a side effect*/
  85.     appCore->setTimeZoneBias(-timezone+3600*daylight);
  86.     appCore->setTimeZoneName(tzname[daylight?0:1]);
  87.     appCore->tick();
  88.     KGlobal::config()->setGroup("Preferences");
  89.     if (KGlobal::config()->hasKey("RendererFlags"))
  90.         appRenderer->setRenderFlags(KGlobal::config()->readNumEntry("RendererFlags"));
  91.     if (KGlobal::config()->hasKey("OrbitMask"))
  92.         appRenderer->setOrbitMask(KGlobal::config()->readNumEntry("OrbitMask"));
  93.     if (KGlobal::config()->hasKey("LabelMode"))
  94.         appRenderer->setLabelMode(KGlobal::config()->readNumEntry("LabelMode"));
  95.     if (KGlobal::config()->hasKey("AmbientLightLevel"))
  96.         appRenderer->setAmbientLightLevel(KGlobal::config()->readDoubleNumEntry("AmbientLightLevel"));
  97.     if (KGlobal::config()->hasKey("FaintestVisible"))
  98.         appCore->setFaintest(KGlobal::config()->readDoubleNumEntry("FaintestVisible"));
  99.     if (KGlobal::config()->hasKey("HudDetail"))
  100.         appCore->setHudDetail(KGlobal::config()->readNumEntry("HudDetail"));
  101.     if (KGlobal::config()->hasKey("TimeZoneBias"))
  102.         appCore->setTimeZoneBias(KGlobal::config()->readNumEntry("TimeZoneBias"));
  103.     if (KGlobal::config()->hasKey("MinFeatureSize"))
  104.         appRenderer->setMinimumFeatureSize(KGlobal::config()->readNumEntry("MinFeatureSize"));
  105.     if (!appCore->getRenderer()->getGLContext()->renderPathSupported(GLContext::GLPath_Basic))
  106.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathBasic")))->setEnabled(false);
  107.     if (!appCore->getRenderer()->getGLContext()->renderPathSupported(GLContext::GLPath_Multitexture))
  108.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathMultitexture")))->setEnabled(false);
  109.     if (!appCore->getRenderer()->getGLContext()->renderPathSupported(GLContext::GLPath_NvCombiner))
  110.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathNvCombiner")))->setEnabled(false);
  111.     if (!appCore->getRenderer()->getGLContext()->renderPathSupported(GLContext::GLPath_DOT3_ARBVP))
  112.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathDOT3ARBVP")))->setEnabled(false);
  113.     if (!appCore->getRenderer()->getGLContext()->renderPathSupported(GLContext::GLPath_NvCombiner_NvVP))
  114.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathNvCombinerNvVP")))->setEnabled(false);
  115.     if (!appCore->getRenderer()->getGLContext()->renderPathSupported(GLContext::GLPath_NvCombiner_ARBVP))
  116.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathNvCombinerARBVP")))->setEnabled(false);
  117.     if (!appCore->getRenderer()->getGLContext()->renderPathSupported(GLContext::GLPath_ARBFP_ARBVP))
  118.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathARBFPARBVP")))->setEnabled(false);
  119.     if (!appCore->getRenderer()->getGLContext()->renderPathSupported(GLContext::GLPath_NV30))
  120.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathNV30")))->setEnabled(false);
  121.     if (!appCore->getRenderer()->getGLContext()->renderPathSupported(GLContext::GLPath_GLSL))
  122.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathGLSL")))->setEnabled(false);
  123.     if (KGlobal::config()->hasKey("RenderPath")) {
  124.        GLContext::GLRenderPath path = (GLContext::GLRenderPath)KGlobal::config()->readNumEntry("RenderPath");
  125.        if (appCore->getRenderer()->getGLContext()->renderPathSupported(path)) {
  126.             appCore->getRenderer()->getGLContext()->setRenderPath(path);
  127.        }
  128.     }
  129.     switch (appCore->getRenderer()->getGLContext()->getRenderPath()) {
  130.     case GLContext::GLPath_Basic:
  131.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathBasic")))->setChecked(true);
  132.         break;
  133.     case GLContext::GLPath_Multitexture:
  134.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathMultitexture")))->setChecked(true);
  135.         break;
  136.     case GLContext::GLPath_NvCombiner:
  137.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathNvCombiner")))->setChecked(true);
  138.         break;
  139.     case GLContext::GLPath_DOT3_ARBVP:
  140.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathDOT3ARBVP")))->setChecked(true);
  141.         break;
  142.     case GLContext::GLPath_NvCombiner_NvVP:
  143.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathNvCombinerNvVP")))->setChecked(true);
  144.         break;
  145.     case GLContext::GLPath_NvCombiner_ARBVP:
  146.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathNvCombinerARBVP")))->setChecked(true);
  147.         break;
  148.     case GLContext::GLPath_ARBFP_ARBVP:
  149.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathARBFPARBVP")))->setChecked(true);
  150.         break;
  151.     case GLContext::GLPath_NV30:
  152.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathNV30")))->setChecked(true);
  153.         break;
  154.     case GLContext::GLPath_GLSL:
  155.         ((KToggleAction*)(((KdeApp*)parentWidget())->action("renderPathGLSL")))->setChecked(true);
  156.         break;
  157.     }
  158.     KGlobal::config()->setGroup(0);
  159.     QPaintDeviceMetrics pdm(this);
  160.     appCore->setScreenDpi(pdm.logicalDpiY());
  161. }
  162. void KdeGlWidget::resizeGL( int w, int h )
  163. {
  164.     appCore->resize(w, h);
  165. }
  166. void KdeGlWidget::mouseMoveEvent( QMouseEvent* m )
  167. {
  168.     int x = (int) m->x();
  169.     int y = (int) m->y();
  170.     int buttons = 0;
  171.     if (m->state() & LeftButton)
  172.         buttons |= CelestiaCore::LeftButton;
  173.     if (m->state() & MidButton)
  174.         buttons |= CelestiaCore::MiddleButton;
  175.     if (m->state() & RightButton)
  176.         buttons |= CelestiaCore::RightButton;
  177.     if (m->state() & ShiftButton)
  178.         buttons |= CelestiaCore::ShiftKey;
  179.     if (m->state() & ControlButton)
  180.         buttons |= CelestiaCore::ControlKey;
  181.     if (buttons != 0)
  182.         appCore->mouseMove(x - lastX, y - lastY, buttons);
  183.     else
  184.         appCore->mouseMove(x, y);
  185.     lastX = x;
  186.     lastY = y;
  187. }
  188. void KdeGlWidget::mousePressEvent( QMouseEvent* m )
  189. {
  190.     lastX = (int) m->x();
  191.     lastY = (int) m->y();
  192.     if (m->button() == LeftButton)
  193.         appCore->mouseButtonDown(m->x(), m->y(), CelestiaCore::LeftButton);
  194.     else if (m->button() == MidButton)
  195.         appCore->mouseButtonDown(m->x(), m->y(), CelestiaCore::MiddleButton);
  196.     else if (m->button() == RightButton)
  197.         appCore->mouseButtonDown(m->x(), m->y(), CelestiaCore::RightButton);
  198. }
  199. void KdeGlWidget::mouseReleaseEvent( QMouseEvent* m )
  200. {
  201.     lastX = (int) m->x();
  202.     lastY = (int) m->y();
  203.     if (m->button() == LeftButton)
  204.         appCore->mouseButtonUp(m->x(), m->y(), CelestiaCore::LeftButton);
  205.     else if (m->button() == MidButton)
  206.         appCore->mouseButtonUp(m->x(), m->y(), CelestiaCore::MiddleButton);
  207.     else if (m->button() == RightButton)
  208.         appCore->mouseButtonUp(m->x(), m->y(), CelestiaCore::RightButton);
  209. }
  210. void KdeGlWidget::wheelEvent( QWheelEvent* w )
  211. {
  212.     if (w->delta() > 0 )
  213.     {
  214.         appCore->mouseWheel(-1.0f, 0);
  215.     }
  216.     else if (w->delta() < 0)
  217.     {
  218.         appCore->mouseWheel(1.0f, 0);
  219.     }
  220. }
  221. bool KdeGlWidget::handleSpecialKey(QKeyEvent* e, bool down)
  222. {
  223.     int k = -1;
  224.     switch (e->key())
  225.     {
  226.     case Key_Up:
  227.         k = CelestiaCore::Key_Up;
  228.         break;
  229.     case Key_Down:
  230.         k = CelestiaCore::Key_Down;
  231.         break;
  232.     case Key_Left:
  233.         k = CelestiaCore::Key_Left;
  234.         break;
  235.     case Key_Right:
  236.         k = CelestiaCore::Key_Right;
  237.         break;
  238.     case Key_Home:
  239.         k = CelestiaCore::Key_Home;
  240.         break;
  241.     case Key_End:
  242.         k = CelestiaCore::Key_End;
  243.         break;
  244.     case Key_F1:
  245.         k = CelestiaCore::Key_F1;
  246.         break;
  247.     case Key_F2:
  248.         k = CelestiaCore::Key_F2;
  249.         break;
  250.     case Key_F3:
  251.         k = CelestiaCore::Key_F3;
  252.         break;
  253.     case Key_F4:
  254.         k = CelestiaCore::Key_F4;
  255.         break;
  256.     case Key_F5:
  257.         k = CelestiaCore::Key_F5;
  258.         break;
  259.     case Key_F6:
  260.         k = CelestiaCore::Key_F6;
  261.         break;
  262.     case Key_F7:
  263.         k = CelestiaCore::Key_F7;
  264.         break;
  265.     case Key_F11:
  266.         k = CelestiaCore::Key_F11;
  267.         break;
  268.     case Key_F12:
  269.         k = CelestiaCore::Key_F12;
  270.         break;
  271.     case Key_PageDown:
  272.         k = CelestiaCore::Key_PageDown;
  273.         break;
  274.     case Key_PageUp:
  275.         k = CelestiaCore::Key_PageUp;
  276.         break;
  277. /*    case Key_F10:
  278.         if (down)
  279.             menuCaptureImage();
  280.         break;     */
  281.     case Key_0:
  282.         if (e->state() & Qt::Keypad)
  283.             k = CelestiaCore::Key_NumPad0;
  284.         break;
  285.     case Key_1:
  286.         if (e->state() & Qt::Keypad)
  287.             k = CelestiaCore::Key_NumPad1;
  288.         break;
  289.     case Key_2:
  290.         if (e->state() & Qt::Keypad)
  291.             k = CelestiaCore::Key_NumPad2;
  292.         break;
  293.     case Key_3:
  294.         if (e->state() & Qt::Keypad)
  295.             k = CelestiaCore::Key_NumPad3;
  296.         break;
  297.     case Key_4:
  298.         if (e->state() & Qt::Keypad)
  299.             k = CelestiaCore::Key_NumPad4;
  300.         break;
  301.     case Key_5:
  302.         if (e->state() & Qt::Keypad)
  303.             k = CelestiaCore::Key_NumPad5;
  304.         break;
  305.     case Key_6:
  306.         if (e->state() & Qt::Keypad)
  307.             k = CelestiaCore::Key_NumPad6;
  308.         break;
  309.     case Key_7:
  310.         if (e->state() & Qt::Keypad)
  311.             k = CelestiaCore::Key_NumPad7;
  312.         break;
  313.     case Key_8:
  314.         if (e->state() & Qt::Keypad)
  315.             k = CelestiaCore::Key_NumPad8;
  316.         break;
  317.     case Key_9:
  318.         if (e->state() & Qt::Keypad)
  319.             k = CelestiaCore::Key_NumPad9;
  320.         break;
  321.     case Key_A:
  322.         k = 'A';
  323.         break;
  324.     case Key_Z:
  325.         k = 'Z';
  326.         break;
  327.     }
  328.     if (k >= 0)
  329.     {
  330.         int buttons = 0;
  331.         if (e->state() & ShiftButton)
  332.             buttons |= CelestiaCore::ShiftKey;
  333.         if (down)
  334.             appCore->keyDown(k, buttons);
  335.         else
  336.             appCore->keyUp(k);
  337.         return (k < 'A' || k > 'Z');
  338.     }
  339.     else
  340.     {
  341.         return false;
  342.     }
  343. }
  344. void KdeGlWidget::keyPressEvent( QKeyEvent* e )
  345. {
  346.     switch (e->key())
  347.     {
  348.     case Key_Escape:
  349.         appCore->charEntered('33');
  350.         break;
  351.     case Key_BackTab:
  352.         appCore->charEntered(CelestiaCore::Key_BackTab);
  353.         break;
  354.     default:
  355.         if (!handleSpecialKey(e, true))
  356.         {
  357.             if ((e->text() != 0) && (e->text() != ""))
  358.             {
  359.                 appCore->charEntered(e->text().utf8().data());
  360.             }
  361.         }
  362.     }
  363. }
  364. void KdeGlWidget::keyReleaseEvent( QKeyEvent* e )
  365. {
  366.     handleSpecialKey(e, false);
  367. }
  368. void KdeGlWidget::setCursorShape(CelestiaCore::CursorShape shape)
  369. {
  370.     int cursor;
  371.     if (currentCursor != shape)
  372.     {
  373.         switch(shape)
  374.         {
  375.         case CelestiaCore::ArrowCursor:
  376.             cursor = Qt::ArrowCursor;
  377.             break;
  378.         case CelestiaCore::UpArrowCursor:
  379.             cursor = Qt::UpArrowCursor;
  380.             break;
  381.         case CelestiaCore::CrossCursor:
  382.             cursor = Qt::CrossCursor;
  383.             break;
  384.         case CelestiaCore::InvertedCrossCursor:
  385.             cursor = Qt::CrossCursor;
  386.             break;
  387.         case CelestiaCore::WaitCursor:
  388.             cursor = Qt::WaitCursor;
  389.             break;
  390.         case CelestiaCore::BusyCursor:
  391.             cursor = Qt::WaitCursor;
  392.             break;
  393.         case CelestiaCore::IbeamCursor:
  394.             cursor = Qt::IbeamCursor;
  395.             break;
  396.         case CelestiaCore::SizeVerCursor:
  397.             cursor = Qt::SizeVerCursor;
  398.             break;
  399.         case CelestiaCore::SizeHorCursor:
  400.             cursor = Qt::SizeHorCursor;
  401.             break;
  402.         case CelestiaCore::SizeBDiagCursor:
  403.             cursor = Qt::SizeBDiagCursor;
  404.             break;
  405.         case CelestiaCore::SizeFDiagCursor:
  406.             cursor = Qt::SizeFDiagCursor;
  407.             break;
  408.         case CelestiaCore::SizeAllCursor:
  409.             cursor = Qt::SizeAllCursor;
  410.             break;
  411.         case CelestiaCore::SplitVCursor:
  412.             cursor = Qt::SplitVCursor;
  413.             break;
  414.         case CelestiaCore::SplitHCursor:
  415.             cursor = Qt::SplitHCursor;
  416.             break;
  417.         case CelestiaCore::PointingHandCursor:
  418.             cursor = Qt::PointingHandCursor;
  419.             break;
  420.         case CelestiaCore::ForbiddenCursor:
  421.             cursor = Qt::ForbiddenCursor;
  422.             break;
  423.         case CelestiaCore::WhatsThisCursor:
  424.             cursor = Qt::WhatsThisCursor;
  425.             break;
  426.         default:
  427.             cursor = Qt::CrossCursor;
  428.             break;
  429.         }
  430.         setCursor(QCursor(cursor));
  431.         currentCursor = shape;
  432.     }
  433. }
  434. CelestiaCore::CursorShape KdeGlWidget::getCursorShape() const
  435. {
  436.     return currentCursor;
  437. }