Advertisement
czaffik

Qml custom mesh Tetrahedron

May 5th, 2019
2,559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QML 5.25 KB | None | 0 0
  1. // main.cpp:
  2. #include <QGuiApplication>
  3. #include <QQmlApplicationEngine>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7.     QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  8.  
  9.     QGuiApplication app(argc, argv);
  10.  
  11.     QQmlApplicationEngine engine;
  12.     engine.load(QUrl(QStringLiteral("main.qml")));
  13.     if (engine.rootObjects().isEmpty())
  14.         return -1;
  15.  
  16.     return app.exec();
  17. }
  18.  
  19. // main.qml:
  20. import QtQuick 2.9
  21. import QtQuick.Window 2.2
  22. import QtQuick.Scene3D 2.0
  23.  
  24. Window {
  25.     visible: true
  26.     width: 640
  27.     height: 480
  28.     title: qsTr("Czworościan")
  29.  
  30.     id: mainwindow
  31.  
  32.     Scene3D {
  33.         id: scene
  34.         anchors.fill: parent
  35.         aspects: ["render", "logic", "input"]
  36.         focus: true
  37.  
  38.         Scene {
  39.             id: sceneRoot
  40.         }
  41.     }
  42. }
  43.  
  44. // Scene.qml:
  45. import QtQuick 2.0
  46. import Qt3D.Core 2.0
  47. import Qt3D.Render 2.0
  48. import Qt3D.Input 2.1
  49. import Qt3D.Extras 2.0
  50.  
  51. Entity {
  52.     id: sceneRoot
  53.  
  54.     components: [
  55.         RenderSettings {
  56.             activeFrameGraph: ForwardRenderer {
  57.                 clearColor: Qt.rgba(0.5, 0.5, 0.5, 1.0)
  58.                 camera: camera
  59.             }
  60.         },
  61.  
  62.         InputSettings { }
  63.     ]
  64.  
  65.     Camera {
  66.         id: camera
  67.         projectionType: CameraLens.PerspectiveProjection
  68.         fieldOfView: 45
  69.         aspectRatio: 16/9
  70.         nearPlane: 0.1
  71.         farPlane: 1000.0
  72.         position: Qt.vector3d(0.0, 0.0, 10.0)
  73.         upVector: Qt.vector3d(0.0, 1.0, 0.0)
  74.         viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
  75.     }
  76.  
  77.     OrbitCameraController {
  78.         camera: camera
  79.     }
  80.  
  81.     Entity {
  82.         components: [
  83.             DirectionalLight {
  84.                 color: "white"
  85.                 intensity: 1.0
  86.                 worldDirection: camera.viewCenter.minus(camera.position)
  87.             }
  88.  
  89.         ]
  90.     }
  91.  
  92.     Tetrahedron {
  93.     }
  94. }
  95.  
  96. // Tetrahedron.qml:
  97. import QtQuick 2.0
  98. import Qt3D.Core 2.0
  99. import Qt3D.Render 2.0
  100. import Qt3D.Extras 2.0
  101.  
  102. Entity {
  103.     id: root
  104.  
  105.     property vector3d position: Qt.vector3d(0.0, -1.0, 0.0)
  106.     property real angle: 0.0
  107.  
  108.     components: [ mesh, material, transform ]
  109.  
  110.     PhongMaterial {
  111.         id: material
  112.         ambient: "black"
  113.         diffuse: "#002300"
  114.     }
  115.  
  116.     Transform {
  117.         id: transform
  118.         matrix: {
  119.             var m = Qt.matrix4x4();
  120.             m.rotate(angle, Qt.vector3d(0, 1, 0));
  121.             m.translate(position);
  122.             return m;
  123.         }
  124.     }
  125.  
  126.     GeometryRenderer {
  127.         id: mesh
  128.         instanceCount: 1
  129.         indexOffset: 0
  130.         firstInstance: 0
  131.         primitiveType: GeometryRenderer.Triangles
  132.  
  133.         Buffer {
  134.             id: vertexBuffer
  135.             type: Buffer.VertexBuffer
  136.             data: new Float32Array([
  137.                                        2.0,  0.0,  0.0,              0.0,  0.0,  1.0,
  138.                                       -2.0,  0.0,  0.0,              0.0,  0.0,  1.0,
  139.                                        0.0,  3.46410161513,  0.0,    0.0,  0.0,  1.0,
  140.  
  141.                                        2.0,  0.0,  0.0,              0.0, -1.0,  0.0,
  142.                                       -2.0,  0.0,  0.0,              0.0, -1.0,  0.0,
  143.                                        0.0, 0.0, -3.46410161513,     0.0, -1.0,  0.0,
  144.  
  145.                                        2.0,  0.0,  0.0,              1.0,  1.0, -1.0,
  146.                                        0.0, 0.0, -3.46410161513,     1.0,  1.0, -1.0,
  147.                                        0.0,  3.46410161513,  0.0,    1.0,  1.0, -1.0,
  148.  
  149.                                       -2.0,  0.0,  0.0,             -1.0,  1.0, -1.0,
  150.                                        0.0,  3.46410161513,  0.0,   -1.0,  1.0, -1.0,
  151.                                        0.0, 0.0, -3.46410161513,    -1.0,  1.0, -1.0
  152.             ])
  153.         }
  154.  
  155.         Buffer {
  156.             id: indexBuffer
  157.             type: Buffer.IndexBuffer
  158.             data: new Uint16Array([
  159.                                       2, 1, 0,
  160.                                       4, 5, 3,
  161.                                       6, 7, 8,
  162.                                       9, 10, 11
  163.             ])
  164.         }
  165.  
  166.         geometry: Geometry {
  167.             attributes: [
  168.                 Attribute {
  169.                     attributeType: Attribute.VertexAttribute
  170.                     vertexBaseType: Attribute.Float
  171.                     vertexSize: 3
  172.                     byteOffset: 0
  173.                     byteStride: 6*4
  174.                     count: 12
  175.                     name: defaultPositionAttributeName
  176.                     buffer: vertexBuffer
  177.                 },
  178.  
  179.                 Attribute {
  180.                     attributeType: Attribute.VertexAttribute
  181.                     vertexBaseType: Attribute.Float
  182.                     vertexSize: 3
  183.                     byteOffset: 3 * 4
  184.                     byteStride: 6*4
  185.                     count: 12
  186.                     name: defaultNormalAttributeName
  187.                     buffer: vertexBuffer
  188.                 },
  189.  
  190.                 Attribute {
  191.                     attributeType: Attribute.IndexAttribute
  192.                     vertexBaseType: Attribute.UnsignedShort
  193.                     count: 3*4
  194.                     buffer: indexBuffer
  195.                 }
  196.  
  197.             ]
  198.         }
  199.     }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement