1/****************************************************************************
2**
3** Copyright (C) 2015 Klaralvdalens Datakonsult AB (KDAB).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include <QGuiApplication>
52
53#include <Qt3DCore/QEntity>
54#include <Qt3DRender/QCamera>
55#include <Qt3DRender/QCameraLens>
56#include <Qt3DCore/QTransform>
57#include <Qt3DCore/QAspectEngine>
58
59#include <Qt3DInput/QInputAspect>
60
61#include <Qt3DRender/QRenderStateSet>
62#include <Qt3DRender/QRenderAspect>
63#include <Qt3DExtras/QForwardRenderer>
64#include <Qt3DExtras/QPerVertexColorMaterial>
65
66#include <Qt3DRender/QGeometryRenderer>
67#include <Qt3DRender/QGeometry>
68#include <Qt3DRender/QAttribute>
69#include <Qt3DRender/QBuffer>
70
71#include <QPropertyAnimation>
72#include <Qt3DExtras/qt3dwindow.h>
73#include <Qt3DExtras/qorbitcameracontroller.h>
74
75int main(int argc, char* argv[])
76{
77 QGuiApplication app(argc, argv);
78 Qt3DExtras::Qt3DWindow view;
79 view.defaultFrameGraph()->setClearColor(QColor::fromRgbF(r: 0.0, g: 0.5, b: 1.0, a: 1.0));
80
81 // Root entity
82 Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity();
83
84 // Camera
85 Qt3DRender::QCamera *cameraEntity = view.camera();
86
87 cameraEntity->lens()->setPerspectiveProjection(fieldOfView: 45.0f, aspect: 16.0f/9.0f, nearPlane: 0.1f, farPlane: 1000.0f);
88 cameraEntity->setPosition(QVector3D(0, 0, 40.0f));
89 cameraEntity->setUpVector(QVector3D(0, 1, 0));
90 cameraEntity->setViewCenter(QVector3D(0, 0, 0));
91
92 // For camera controls
93 Qt3DExtras::QOrbitCameraController *camController = new Qt3DExtras::QOrbitCameraController(rootEntity);
94 camController->setCamera(cameraEntity);
95
96 // Material
97 Qt3DRender::QMaterial *material = new Qt3DExtras::QPerVertexColorMaterial(rootEntity);
98
99 // Torus
100 Qt3DCore::QEntity *customMeshEntity = new Qt3DCore::QEntity(rootEntity);
101
102 // Transform
103 Qt3DCore::QTransform *transform = new Qt3DCore::QTransform;
104 transform->setScale(8.0f);
105
106 // Custom Mesh (TetraHedron)
107 Qt3DRender::QGeometryRenderer *customMeshRenderer = new Qt3DRender::QGeometryRenderer;
108 Qt3DRender::QGeometry *customGeometry = new Qt3DRender::QGeometry(customMeshRenderer);
109
110 Qt3DRender::QBuffer *vertexDataBuffer = new Qt3DRender::QBuffer(customGeometry);
111 Qt3DRender::QBuffer *indexDataBuffer = new Qt3DRender::QBuffer(customGeometry);
112
113 // vec3 for position
114 // vec3 for colors
115 // vec3 for normals
116
117 /* 2
118 /|\
119 / | \
120 / /3\ \
121 0/___\ 1
122 */
123
124 // 4 distinct vertices
125 QByteArray vertexBufferData;
126 vertexBufferData.resize(size: 4 * (3 + 3 + 3) * sizeof(float));
127
128 // Vertices
129 QVector3D v0(-1.0f, 0.0f, -1.0f);
130 QVector3D v1(1.0f, 0.0f, -1.0f);
131 QVector3D v2(0.0f, 1.0f, 0.0f);
132 QVector3D v3(0.0f, 0.0f, 1.0f);
133
134 // Faces Normals
135 QVector3D n023 = QVector3D::normal(v1: v0, v2, v3);
136 QVector3D n012 = QVector3D::normal(v1: v0, v2: v1, v3: v2);
137 QVector3D n310 = QVector3D::normal(v1: v3, v2: v1, v3: v0);
138 QVector3D n132 = QVector3D::normal(v1, v2: v3, v3: v2);
139
140 // Vector Normals
141 QVector3D n0 = QVector3D(n023 + n012 + n310).normalized();
142 QVector3D n1 = QVector3D(n132 + n012 + n310).normalized();
143 QVector3D n2 = QVector3D(n132 + n012 + n023).normalized();
144 QVector3D n3 = QVector3D(n132 + n310 + n023).normalized();
145
146 // Colors
147 QVector3D red(1.0f, 0.0f, 0.0f);
148 QVector3D green(0.0f, 1.0f, 0.0f);
149 QVector3D blue(0.0f, 0.0f, 1.0f);
150 QVector3D white(1.0f, 1.0f, 1.0f);
151
152 const QVector<QVector3D> vertices = QVector<QVector3D>()
153 << v0 << n0 << red
154 << v1 << n1 << blue
155 << v2 << n2 << green
156 << v3 << n3 << white;
157
158 float *rawVertexArray = reinterpret_cast<float *>(vertexBufferData.data());
159 int idx = 0;
160
161 for (const QVector3D &v : vertices) {
162 rawVertexArray[idx++] = v.x();
163 rawVertexArray[idx++] = v.y();
164 rawVertexArray[idx++] = v.z();
165 }
166
167 // Indices (12)
168 QByteArray indexBufferData;
169 indexBufferData.resize(size: 4 * 3 * sizeof(ushort));
170 ushort *rawIndexArray = reinterpret_cast<ushort *>(indexBufferData.data());
171
172 // Front
173 rawIndexArray[0] = 0;
174 rawIndexArray[1] = 1;
175 rawIndexArray[2] = 2;
176 // Bottom
177 rawIndexArray[3] = 3;
178 rawIndexArray[4] = 1;
179 rawIndexArray[5] = 0;
180 // Left
181 rawIndexArray[6] = 0;
182 rawIndexArray[7] = 2;
183 rawIndexArray[8] = 3;
184 // Right
185 rawIndexArray[9] = 1;
186 rawIndexArray[10] = 3;
187 rawIndexArray[11] = 2;
188
189 vertexDataBuffer->setData(vertexBufferData);
190 indexDataBuffer->setData(indexBufferData);
191
192 // Attributes
193 Qt3DRender::QAttribute *positionAttribute = new Qt3DRender::QAttribute();
194 positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
195 positionAttribute->setBuffer(vertexDataBuffer);
196 positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
197 positionAttribute->setVertexSize(3);
198 positionAttribute->setByteOffset(0);
199 positionAttribute->setByteStride(9 * sizeof(float));
200 positionAttribute->setCount(4);
201 positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName());
202
203 Qt3DRender::QAttribute *normalAttribute = new Qt3DRender::QAttribute();
204 normalAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
205 normalAttribute->setBuffer(vertexDataBuffer);
206 normalAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
207 normalAttribute->setVertexSize(3);
208 normalAttribute->setByteOffset(3 * sizeof(float));
209 normalAttribute->setByteStride(9 * sizeof(float));
210 normalAttribute->setCount(4);
211 normalAttribute->setName(Qt3DRender::QAttribute::defaultNormalAttributeName());
212
213 Qt3DRender::QAttribute *colorAttribute = new Qt3DRender::QAttribute();
214 colorAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute);
215 colorAttribute->setBuffer(vertexDataBuffer);
216 colorAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float);
217 colorAttribute->setVertexSize(3);
218 colorAttribute->setByteOffset(6 * sizeof(float));
219 colorAttribute->setByteStride(9 * sizeof(float));
220 colorAttribute->setCount(4);
221 colorAttribute->setName(Qt3DRender::QAttribute::defaultColorAttributeName());
222
223 Qt3DRender::QAttribute *indexAttribute = new Qt3DRender::QAttribute();
224 indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute);
225 indexAttribute->setBuffer(indexDataBuffer);
226 indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedShort);
227 indexAttribute->setVertexSize(1);
228 indexAttribute->setByteOffset(0);
229 indexAttribute->setByteStride(0);
230 indexAttribute->setCount(12);
231
232 customGeometry->addAttribute(attribute: positionAttribute);
233 customGeometry->addAttribute(attribute: normalAttribute);
234 customGeometry->addAttribute(attribute: colorAttribute);
235 customGeometry->addAttribute(attribute: indexAttribute);
236
237 customMeshRenderer->setInstanceCount(1);
238 customMeshRenderer->setIndexOffset(0);
239 customMeshRenderer->setFirstInstance(0);
240 customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
241 customMeshRenderer->setGeometry(customGeometry);
242 // 4 faces of 3 points
243 customMeshRenderer->setVertexCount(12);
244
245 customMeshEntity->addComponent(comp: customMeshRenderer);
246 customMeshEntity->addComponent(comp: transform);
247 customMeshEntity->addComponent(comp: material);
248
249 view.setRootEntity(rootEntity);
250 view.show();
251
252 return app.exec();
253}
254

source code of qt3d/tests/manual/custom-mesh-cpp/main.cpp