Results 1 to 8 of 8

Thread: Ogre+qt mouse event (add object with mouse problem)

  1. #1
    Join Date
    Apr 2012
    Posts
    101
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Ogre+qt mouse event (add object with mouse problem)

    Hi
    i integrate ogre with qt
    i have now this code
    Qt Code:
    1. #include <QtDebug>
    2.  
    3. #include "ogrewidget.h"
    4.  
    5. OgreWidget::OgreWidget(QWidget *parent)
    6. :QWidget(parent),
    7. ogreRoot(0), ogreSceneManager(0), ogreRenderWindow(0), ogreViewport(0),
    8. ogreCamera(0)
    9. {
    10. setAttribute(Qt::WA_OpaquePaintEvent);
    11. setAttribute(Qt::WA_PaintOnScreen);
    12. setMinimumSize(240,240);
    13. setFocusPolicy(Qt::ClickFocus);
    14. }
    15.  
    16. OgreWidget::~OgreWidget()
    17. {
    18. if(ogreRenderWindow)
    19. {
    20. ogreRenderWindow->removeAllViewports();
    21. }
    22.  
    23. if(ogreRoot)
    24. {
    25. ogreRoot->detachRenderTarget(ogreRenderWindow);
    26.  
    27. if(ogreSceneManager)
    28. {
    29. ogreRoot->destroySceneManager(ogreSceneManager);
    30. }
    31. }
    32.  
    33. delete ogreRoot;
    34. }
    35.  
    36. void OgreWidget::setBackgroundColor(QColor c)
    37. {
    38. if(ogreViewport)
    39. {
    40. Ogre::ColourValue ogreColour;
    41. ogreColour.setAsARGB(c.rgba());
    42. ogreViewport->setBackgroundColour(ogreColour);
    43. }
    44. }
    45.  
    46. void OgreWidget::setCameraPosition(const Ogre::Vector3 &pos)
    47. {
    48. ogreCamera->setPosition(pos);
    49. ogreCamera->lookAt(0,50,0);
    50. update();
    51. emit cameraPositionChanged(pos);
    52. }
    53.  
    54. void OgreWidget::keyPressEvent(QKeyEvent *e)
    55. {
    56. static QMap<int, Ogre::Vector3> keyCoordModificationMapping;
    57. static bool mappingInitialised = false;
    58.  
    59. if(!mappingInitialised)
    60. {
    61. keyCoordModificationMapping[Qt::Key_Z] = Ogre::Vector3(0, 0, -5);
    62. keyCoordModificationMapping[Qt::Key_S] = Ogre::Vector3(0, 0, 5);
    63. keyCoordModificationMapping[Qt::Key_Q] = Ogre::Vector3(-5, 0, 0);
    64. keyCoordModificationMapping[Qt::Key_D] = Ogre::Vector3( 5, 0, 0);
    65. keyCoordModificationMapping[Qt::Key_PageUp] = Ogre::Vector3(0, 5, 0);
    66. keyCoordModificationMapping[Qt::Key_PageDown] = Ogre::Vector3(0, -5, 0);
    67.  
    68. mappingInitialised = true;
    69. }
    70.  
    71. QMap<int, Ogre::Vector3>::iterator keyPressed =
    72. keyCoordModificationMapping.find(e->key());
    73. if(keyPressed != keyCoordModificationMapping.end() && ogreCamera)
    74. {
    75. const Ogre::Vector3 &actualCamPos = ogreCamera->getPosition();
    76. setCameraPosition(actualCamPos + keyPressed.value());
    77.  
    78. e->accept();
    79. }
    80. else
    81. {
    82. QWidget::keyPressEvent(e);
    83. }
    84. }
    85.  
    86. void OgreWidget::moveEvent(QMoveEvent *e)
    87. {
    88. QWidget::moveEvent(e);
    89.  
    90. if(e->isAccepted() && ogreRenderWindow)
    91. {
    92. ogreRenderWindow->windowMovedOrResized();
    93. update();
    94. }
    95. }
    96.  
    97. void OgreWidget::paintEvent(QPaintEvent *e)
    98. {
    99. ogreRoot->_fireFrameStarted();
    100. ogreRenderWindow->update();
    101. ogreRoot->_fireFrameEnded();
    102.  
    103. e->accept();
    104. }
    105.  
    106. void OgreWidget::resizeEvent(QResizeEvent *e)
    107. {
    108. QWidget::resizeEvent(e);
    109.  
    110. if(e->isAccepted())
    111. {
    112. const QSize &newSize = e->size();
    113. if(ogreRenderWindow)
    114. {
    115. ogreRenderWindow->resize(newSize.width(), newSize.height());
    116. ogreRenderWindow->windowMovedOrResized();
    117. }
    118. if(ogreCamera)
    119. {
    120. Ogre::Real aspectRatio = Ogre::Real(newSize.width()) / Ogre::Real(newSize.height());
    121. ogreCamera->setAspectRatio(aspectRatio);
    122. }
    123. }
    124. }
    125.  
    126. void OgreWidget::showEvent(QShowEvent *e)
    127. {
    128. if(!ogreRoot)
    129. {
    130. initOgreSystem();
    131. }
    132.  
    133. QWidget::showEvent(e);
    134. }
    135. /////////////////////////////////////////////////////////////////////////////////////
    136. ////////////////////////////////////////////////////////////
    137. void OgreWidget::initOgreSystem()
    138. {
    139. //ogreRoot = new Ogre::Root();
    140.  
    141. /* Ogre::Root**/ ogreRoot=new Ogre::Root("plugins_d.cfg");
    142.  
    143. //Ogre::RenderSystem *renderSystem = ogreRoot->getRenderSystemByName("OpenGL Rendering Subsystem");
    144. // Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
    145.  
    146.  
    147. ogreRoot->loadPlugin("RenderSystem_GL_d");
    148. Ogre::RenderSystem* rs = ogreRoot->getRenderSystemByName("OpenGL Rendering Subsystem");
    149. if(!(rs->getName() == "OpenGL Rendering Subsystem"))
    150. {
    151. printf("no susystem"); //No RenderSystem found
    152. }
    153. //Ogre::RenderSystem *renderSystem = ogreRoot->getRenderSystemByName("ogre_pluginsRenderSystem_GL_d");
    154. ogreRoot->setRenderSystem(/*renderSystem*/rs);
    155. ogreRoot->initialise(false);
    156.  
    157. ogreSceneManager = ogreRoot->createSceneManager(Ogre::ST_GENERIC);
    158.  
    159. Ogre::NameValuePairList viewConfig;
    160. Ogre::String widgetHandle;
    161. #ifdef Q_WS_WIN
    162. widgetHandle = Ogre::StringConverter::toString((size_t)((HWND)winId()));
    163. #else
    164. QWidget *q_parent = dynamic_cast <QWidget *> (parent());
    165. QX11Info xInfo = x11Info();
    166.  
    167. widgetHandle = Ogre::StringConverter::toString ((unsigned long)xInfo.display()) +
    168. ":" + Ogre::StringConverter::toString ((unsigned int)xInfo.screen()) +
    169. ":" + Ogre::StringConverter::toString ((unsigned long)q_parent->winId());
    170. #endif
    171. viewConfig["externalWindowHandle"] = widgetHandle;
    172. ogreRenderWindow = ogreRoot->createRenderWindow("Ogre rendering window",
    173. width(), height(), false, &viewConfig);
    174.  
    175. ogreCamera = ogreSceneManager->createCamera("myCamera");
    176. Ogre::Vector3 camPos(0, 50,150);
    177. ogreCamera->setPosition(camPos);
    178. ogreCamera->lookAt(0,50,0);
    179. emit cameraPositionChanged(camPos);
    180.  
    181. ogreViewport = ogreRenderWindow->addViewport(ogreCamera);
    182. ogreViewport->setBackgroundColour(Ogre::ColourValue(0,0,0));
    183. ogreCamera->setAspectRatio(Ogre::Real(width()) / Ogre::Real(height()));
    184.  
    185. setupNLoadResources();
    186. createScene();
    187. }
    188.  
    189.  
    190. void OgreWidget::setupNLoadResources()
    191. {
    192. // Load resource paths from config file
    193. // Load resource paths from config file
    194. Ogre::ConfigFile cf;
    195. cf.load("resources_d.cfg");
    196.  
    197. // Go through all sections & settings in the file
    198. Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
    199.  
    200. Ogre::String secName, typeName, archName;
    201. while (seci.hasMoreElements())
    202. {
    203. secName = seci.peekNextKey();
    204. Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
    205. Ogre::ConfigFile::SettingsMultiMap::iterator i;
    206. for (i = settings->begin(); i != settings->end(); ++i)
    207. {
    208. typeName = i->first;
    209. archName = i->second;
    210. Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
    211. archName, typeName, secName);
    212. }
    213. }
    214. // Initialise, parse scripts etc
    215. Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
    216. }
    217.  
    218. void OgreWidget::createScene()
    219. {
    220. ogreSceneManager->setAmbientLight(Ogre::ColourValue(1,1,1));
    221.  
    222. Ogre::Entity *robotEntity = ogreSceneManager->createEntity("Robot","robot.mesh");
    223. Ogre::SceneNode *robotNode = ogreSceneManager->getRootSceneNode()->createChildSceneNode("RobotNode");
    224. robotNode->attachObject(robotEntity);
    225. robotNode->yaw(Ogre::Radian(Ogre::Degree(-90)));
    226. }
    To copy to clipboard, switch view to plain text mode 
    my problem now is i want add object with mouse in the windows i do not know how i do that

    when i was work only with ogre i make it like that

    Qt Code:
    1. bool BaseApplication::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
    2. {
    3. MarkerNode= mSceneMgr->getRootSceneNode()->createChildSceneNode();
    4. if (mTrayMgr->injectMouseDown(arg, id)) return true;
    5. //mCameraMan->injectMouseDown(arg, id);
    6. switch(id) {
    7. case OIS::MB_Left:
    8. /*Ogre::Entity**/ ent_marker = mSceneMgr->createEntity( Ogre::SceneManager:: PT_SPHERE);
    9.  
    10. //createNode(ent_marker);
    11. MarkerNode->attachObject(ent_marker);
    12. MarkerNode->setScale(0.1f, 0.1f, 0.1f);
    13. Ogre::Real screenWidth = Ogre::Root::getSingleton().getAutoCreatedWindow()->getWidth();
    14. Ogre::Real screenHeight = Ogre::Root::getSingleton().getAutoCreatedWindow()->getHeight();
    15.  
    16. // convert to 0-1 offset
    17. Ogre::Real offsetX = arg.state.X.abs / screenWidth;
    18. Ogre::Real offsetY = arg.state.Y.abs / screenHeight;
    19.  
    20. // set up the ray
    21. Ogre::Ray mouseRay = mCamera1->getCameraToViewportRay(offsetX, offsetY);
    22.  
    23. // check if the ray intersects our cube
    24. // intersects() will return whether it intersects or not (the bool value) and
    25. // what distance (the Real value) along the ray the intersection is
    26. std::pair<bool, Ogre::Real> result = mouseRay.intersects(CubeNode->getAttachedObject (0) ->getBoundingBox());
    27.  
    28.  
    29. if(result.first) {
    30. // if the ray intersect the plane, we have a distance value
    31. // telling us how far from the ray origin the intersection occurred.
    32. // the last thing we need is the point of the intersection.
    33. // Ray provides us getPoint() function which returns a point
    34. // along the ray, supplying it with a distance value.
    35.  
    36. // get the point where the intersection is
    37. Ogre:: Vector3 point = mouseRay.getPoint(result.second);
    38.  
    39. // position our sphere
    40. /* Ogre::Entity**/ //ent_marker->setPosition(point);
    41. //MarkerNode->setPosition(point);
    42.  
    43.  
    44. MarkerNode->setPosition(point);
    45.  
    46.  
    47. //break;
    48. }
    49. }
    To copy to clipboard, switch view to plain text mode 
    and i initialyse OIS like that
    Qt Code:
    1. Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
    2. OIS::ParamList pl;
    3. size_t windowHnd = 0;
    4. std::ostringstream windowHndStr;
    5.  
    6. mWindow->getCustomAttribute("WINDOW", &windowHnd);
    7. windowHndStr << windowHnd;
    8. pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
    9.  
    10. mInputManager = OIS::InputManager::createInputSystem( pl );
    11.  
    12. mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, true ));
    13. mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject( OIS::OISMouse, true ));*/
    14.  
    15. mRoot->addFrameListener(this);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2012
    Posts
    101
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Ogre+qt mouse event (add object with mouse problem)

    i can now add oject ut if i clic another time i can't add (it add one object )
    Qt Code:
    1. void OgreWidget::mousePressEvent(QMouseEvent *e)
    2. {
    3. if(e->button() == Qt::LeftButton)
    4. {
    5.  
    6. MarkerNode= ogreSceneManager->getRootSceneNode()->createChildSceneNode();
    7.  
    8.  
    9. ent_marker = ogreSceneManager->createEntity( Ogre::SceneManager:: PT_SPHERE);
    10.  
    11. //createNode(ent_marker);
    12. Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create("green", "General");
    13. material->getTechnique( 0 )->getPass( 0 )->setAmbient(1, 0, 0);
    14. ent_marker->setMaterial(material);
    15. MarkerNode->attachObject(ent_marker);
    16. MarkerNode->setScale(0.1f, 0.1f, 0.1f);
    17.  
    18. // convert to 0-1 offset
    19.  
    20. Ogre::Real offsetX =(double)e->pos().x()/(double)width();
    21. Ogre::Real offsetY=(double) e->pos().y()/(double)height();
    22.  
    23. // set up the ray
    24. Ogre::Ray mouseRay = ogreCamera->getCameraToViewportRay(offsetX, offsetY);
    25.  
    26. // check if the ray intersects our cube
    27. // intersects() will return whether it intersects or not (the bool value) and
    28. // what distance (the Real value) along the ray the intersection is
    29. std::pair<bool, Ogre::Real> result1 = mouseRay.intersects(CubeNode->getAttachedObject (0) ->getBoundingBox());
    30.  
    31. mRayScnQuery->setRay(mouseRay);
    32. // Execute query
    33.  
    34. Ogre::RaySceneQueryResult &result =mRayScnQuery->execute();
    35. Ogre::RaySceneQueryResult::iterator itr;
    36. for(itr = result.begin() ;itr != result.end();++itr)
    37. {
    38. if(itr->movable)
    39. {
    40.  
    41. MarkerNode->_setDerivedPosition(mouseRay.getPoint(itr->distance));
    42.  
    43. MarkerNode->setScale(0.1f, 0.1f, 0.1f);
    44. }
    45. }
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 
    it dos not make update??

  3. #3
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Ogre+qt mouse event (add object with mouse problem)

    Theoretically, this should work:
    Qt Code:
    1. bool OgreWidget::mousePressEvent( QMouseEvent* e )
    2. {
    3. MarkerNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
    4. //if (mTrayMgr->injectMouseDown(arg, id)) return true; //do you need that for something???
    5. if( e->button() == Qt::LeftButton )
    6. {
    7. ent_marker = mSceneMgr->createEntity( Ogre::SceneManager:: PT_SPHERE);
    8. MarkerNode->attachObject(ent_marker);
    9. MarkerNode->setScale(0.1f, 0.1f, 0.1f);
    10. Ogre::Real screenWidth = Ogre::Root::getSingleton().getAutoCreatedWindow()->getWidth();
    11. Ogre::Real screenHeight = Ogre::Root::getSingleton().getAutoCreatedWindow()->getHeight();
    12.  
    13. Ogre::Real offsetX = e->posF().x() / this->width();
    14. Ogre::Real offsetY = e->posF().y() / this->height();
    15. Ogre::Ray mouseRay = mCamera1->getCameraToViewportRay(offsetX, offsetY);
    16. std::pair<bool, Ogre::Real> result = mouseRay.intersects(CubeNode->getAttachedObject (0) ->getBoundingBox());
    17.  
    18. if(result.first)
    19. {
    20. Ogre:: Vector3 point = mouseRay.getPoint(result.second);
    21. MarkerNode->setPosition(point);
    22. }
    23. break;
    24. }
    25.  
    26. QWidget::mousePressEvent( e );
    27. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Apr 2012
    Posts
    101
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Ogre+qt mouse event (add object with mouse problem)

    No it dos not work

    when i clic another time i do not see another oject ut if i resize the xindow i see it , if i clic another time i can't see the oject ut if i resize the windows i see it
    that mean it make update just when i resize the window

  5. #5
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Ogre+qt mouse event (add object with mouse problem)

    then call this->update() at the end of the mousePressEvent(), this will trigger repaint of the whole widget and you should see new content.

  6. #6
    Join Date
    Apr 2012
    Posts
    101
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Ogre+qt mouse event (add object with mouse problem)

    it dos not work
    when i clic the cube hide but when i resize the window i see the cube and the object how is adde

  7. #7
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Ogre+qt mouse event (add object with mouse problem)

    Ok, I'm not too familiar with OGRE, but you should see where I'm going with it.

    Resizing a window triggers repaint of the scene. If repainting of the widget doesn't help, try updating the ogre scene somehow, maybe mSceneMgr->redraw() or some_ogre_view->repaint().

  8. #8
    Join Date
    Apr 2012
    Posts
    101
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Ogre+qt mouse event (add object with mouse problem)

    Ah ok i will do that
    thanks

Similar Threads

  1. Replies: 3
    Last Post: 7th January 2012, 09:38
  2. Mouse event problem
    By cheyanne in forum Newbie
    Replies: 8
    Last Post: 27th December 2011, 07:13
  3. Replies: 3
    Last Post: 12th May 2010, 14:11
  4. problem in mouse event
    By bhogasena in forum Qt Programming
    Replies: 0
    Last Post: 28th January 2009, 13:58
  5. Replies: 2
    Last Post: 4th August 2007, 05:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.