daily pastebin goal
16%
SHARE
TWEET

Test3D-windows-impl

johan_solo Oct 18th, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Window.h"
  2.  
  3. #include <QtCore/QDebug>
  4.  
  5. #include <QtGui/QKeyEvent>
  6. #include <QtGui/QMouseEvent>
  7. #include <QtGui/QScreen>
  8. #include <QtGui/QWheelEvent>
  9.  
  10. #include <QtWidgets/QWidget>
  11. #include <QtWidgets/QHBoxLayout>
  12.  
  13. #include <Qt3DCore/QAspectEngine>
  14. #include <Qt3DCore/QCamera>
  15. #include <Qt3DCore/QComponent>
  16. #include <Qt3DCore/QEntity>
  17. #include <Qt3DCore/QTransform>
  18.  
  19. #include <Qt3DInput/QInputAspect>
  20.  
  21. #include <Qt3DRender/QForwardRenderer>
  22. #include <Qt3DRender/QFrameGraph>
  23. #include <Qt3DRender/QPhongMaterial>
  24. #include <Qt3DRender/QRenderAspect>
  25. #include <Qt3DRender/QSphereMesh>
  26. Window::Window()
  27.     : Qt3DRender::QWindow()
  28.     , _Engine( new Qt3DCore::QAspectEngine( this ) )
  29.     , _Input( new Qt3DInput::QInputAspect() )
  30.     , _Camera( 0 )
  31.     , _RootEntity( new Qt3DCore::QEntity() )
  32.     , _SphereEntities()
  33.     , _Widget( new QWidget() )
  34.     , _PanEngaged( false )
  35.     , _OriginPan()
  36. {
  37.     _RootEntity->setObjectName( "Root entity" );
  38. }
  39.  
  40. Window::~Window()
  41. {}
  42.  
  43. bool Window::initialise()
  44. {
  45.     _Widget = QWidget::createWindowContainer( this );
  46.     _Widget->setMinimumSize( QSize( 200, 200 ) );
  47.     QSize screenSize( screen()->size() );
  48.     _Widget->setMaximumSize( screenSize );
  49.  
  50.     _Engine->registerAspect( new Qt3DRender::QRenderAspect() );
  51.  
  52.     _Engine->registerAspect( _Input );
  53.  
  54.     qDebug() << "Aspects registered";
  55.  
  56.     QVariantMap data;
  57.     data.insert( QStringLiteral( "surface" ),
  58.                  QVariant::fromValue( static_cast< QSurface* >( this ) ) );
  59.     data.insert( QStringLiteral( "eventSource" ), QVariant::fromValue( this ) );
  60.     _Engine->setData( data );
  61.  
  62.     qDebug() << "Engine data set";
  63.  
  64.     // Camera
  65.     Qt3DCore::QCamera* cameraEntity( new Qt3DCore::QCamera( _RootEntity ) );
  66.     _Camera = cameraEntity;
  67.     cameraEntity->setObjectName( QStringLiteral( "cameraEntity" ) );
  68.  
  69.     cameraEntity->lens()->setPerspectiveProjection( 45.0f, 16.0f / 9.0f, 0.1f,
  70.                                                     1000.0f );
  71.     cameraEntity->setPosition( QVector3D( 0, 0, 40.0f ) );
  72.     cameraEntity->setUpVector( QVector3D( 0, 1, 0 ) );
  73.     cameraEntity->setViewCenter( QVector3D( 0, 0, 0 ) );
  74.     _Input->setCamera( cameraEntity );
  75.  
  76.     qDebug() << "Camera set";
  77.  
  78.     // FrameGraph
  79.     Qt3DRender::QFrameGraph* frameGraph( new Qt3DRender::QFrameGraph() );
  80.     Qt3DRender::QForwardRenderer* forwardRenderer(
  81.         new Qt3DRender::QForwardRenderer() );
  82.  
  83.     forwardRenderer->setCamera( cameraEntity );
  84.     forwardRenderer->setClearColor( QColor( QRgb( 0x4d4d4f ) ) );
  85.     frameGraph->setActiveFrameGraph( forwardRenderer );
  86.  
  87.     qDebug() << "FrameGraph initialised";
  88.  
  89.     // Setting the FrameGraph
  90.     _RootEntity->addComponent( frameGraph );
  91.  
  92.     qDebug() << "FrameGraph added";
  93.  
  94.  
  95.     const unsigned vertexNbr( 11u );
  96.  
  97.     // Sphere object
  98.     Qt3DRender::QSphereMesh* sphereMesh( new Qt3DRender::QSphereMesh() );
  99.     sphereMesh->setRings( 4 );
  100.     sphereMesh->setSlices( 4 );
  101.     sphereMesh->setRadius( 0.5 );
  102.  
  103.     // Material
  104.     Qt3DRender::QPhongMaterial* sphereMaterial(
  105.         new Qt3DRender::QPhongMaterial() );
  106.     sphereMaterial->setDiffuse( QColor( QRgb( 0xa69929 ) ) );
  107.  
  108.     for ( unsigned i( 0u ); i < vertexNbr*vertexNbr*vertexNbr; ++i )
  109.     {
  110.         // Sphere mesh transform
  111.         Qt3DCore::QTransform* sphereTransform( new Qt3DCore::QTransform() );
  112.  
  113.         sphereTransform->setScale( 1.f );
  114.  
  115.         sphereTransform->setTranslation( QVector3D( ( i % vertexNbr ) * 10.f - 10.f * ( vertexNbr / 2u ),
  116.                                                     ( ( i / vertexNbr ) % vertexNbr ) * 10.f - 10.f * ( vertexNbr / 2u ),
  117.                                                     ( i / ( vertexNbr * vertexNbr ) ) * 10.f  - 10.f * ( vertexNbr / 2u ) ) );
  118.  
  119.         qDebug() << "Sphere transformation initialised to"
  120.                  << ( i % vertexNbr ) * 10.f - 10.f * vertexNbr / 2u
  121.                  << ( ( i / vertexNbr ) % vertexNbr ) * 10.f - 10.f * vertexNbr / 2u
  122.                  << ( i / vertexNbr * vertexNbr ) * 10.f  - 10.f * vertexNbr / 2u;
  123.  
  124.         // Spheres
  125.         _SphereEntities.append( new Qt3DCore::QEntity( _RootEntity ) );
  126.         _SphereEntities.back()->addComponent( sphereMesh );
  127.         _SphereEntities.back()->addComponent( sphereMaterial );
  128.         _SphereEntities.back()->addComponent( sphereTransform );
  129.     }
  130.  
  131.     // Set root object of the scene
  132.     _Engine->setRootEntity( _RootEntity );
  133.     qDebug() << "Root entity set";
  134.  
  135.     QSize widgetSize( _Widget->size() );
  136.     qDebug() << "Size of containing widget" << widgetSize;
  137.  
  138.     float aspectRatio = float( widgetSize.width() ) /
  139.                         float( widgetSize.height() );
  140.     cameraEntity->lens()->setPerspectiveProjection( 45.0f, aspectRatio, 0.1f,
  141.                                                     1000.0f );
  142.  
  143.     return true;
  144. }
  145.  
  146. QWidget* Window::widget()
  147. {
  148.     return _Widget;
  149. }
  150.  
  151. void Window::wheelEvent( QWheelEvent* event )
  152. {
  153.     QPoint numDegrees( event->angleDelta() / 8 );
  154.     if ( ! numDegrees.isNull() )
  155.     {
  156.         float value( float( numDegrees.y() ) / 30.f );
  157.  
  158.         poorMansZoom( value );
  159.     }
  160. }
  161.  
  162. void Window::poorMansZoom( float factor )
  163. {
  164.     QVector3D pos( _Camera->position() );
  165.     QVector3D centre( _Camera->viewCenter() );
  166.  
  167.     QVector3D vec( pos - centre );
  168.     vec /= vec.length();
  169.  
  170.     _Camera->setPosition( pos + factor * vec * 5.f );
  171. }
  172.  
  173. void Window::pan( const QPoint& pt )
  174. {
  175.     QVector3D vec( pt.x(), 0.f, pt.y() );
  176.  
  177.     qDebug() << "pos" << _Camera->position();
  178.     qDebug() << "up" << _Camera->upVector();
  179.     qDebug() << "trans vec" << vec;
  180.  
  181.     _Camera->translateWorld( vec );
  182.     // _Camera->setPosition( pos );
  183.     // _Camera->setViewCenter( centre );
  184.  
  185. }
RAW Paste Data
Top