/**************************************************************************** ** ** Copyright (C) 2014 Klaralvdalens Datakonsult AB (KDAB). ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt3D module of the Qt Toolkit. ** ** $QT_BEGIN_LICENSE:BSD$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and The Qt Company. For licensing terms ** and conditions see https://www.qt.io/terms-conditions. For further ** information use the contact form at https://www.qt.io/contact-us. ** ** BSD License Usage ** Alternatively, you may use this file under the terms of the BSD license ** as follows: ** ** "Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions are ** met: ** * Redistributions of source code must retain the above copyright ** notice, this list of conditions and the following disclaimer. ** * Redistributions in binary form must reproduce the above copyright ** notice, this list of conditions and the following disclaimer in ** the documentation and/or other materials provided with the ** distribution. ** * Neither the name of The Qt Company Ltd nor the names of its ** contributors may be used to endorse or promote products derived ** from this software without specific prior written permission. ** ** ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "qt3dwindow.h" #include #include #include #include #include #include #include void drawLineStrip(const QVector& vertices, const QVector& indices, const QColor& color, Qt3DCore::QEntity* _rootEntity) { auto geometry = new Qt3DRender::QGeometry(_rootEntity); // position vertices (start and end) QByteArray bufferBytes; bufferBytes.resize(3 * vertices.size() * sizeof(float)); // start.x, start.y, start.end + end.x, end.y, end.z auto positions = reinterpret_cast(bufferBytes.data()); for (int n = vertices.size(), i = 0; i < n; ++i) { *positions++ = vertices[i].x(); *positions++ = vertices[i].y(); *positions++ = vertices[i].z(); } auto buf = new Qt3DRender::QBuffer(geometry); buf->setData(bufferBytes); auto positionAttribute = new Qt3DRender::QAttribute(geometry); positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName()); positionAttribute->setVertexBaseType(Qt3DRender::QAttribute::Float); positionAttribute->setVertexSize(3); positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute); positionAttribute->setBuffer(buf); positionAttribute->setByteStride(3 * sizeof(float)); positionAttribute->setCount(vertices.size()); geometry->addAttribute(positionAttribute); // We add the vertices in the geometry // connectivity between vertices QByteArray indexBytes; indexBytes.resize(indices.size() * sizeof(unsigned int)); // start to end memcpy(indexBytes.data(), indices.data(), indices.size() * sizeof(unsigned int)); auto indexBuffer = new Qt3DRender::QBuffer(geometry); indexBuffer->setData(indexBytes); auto indexAttribute = new Qt3DRender::QAttribute(geometry); indexAttribute->setVertexBaseType(Qt3DRender::QAttribute::UnsignedInt); indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute); indexAttribute->setVertexSize(1); indexAttribute->setBuffer(indexBuffer); indexAttribute->setByteStride(1 * sizeof(unsigned int)); indexAttribute->setCount(indices.size()); geometry->addAttribute(indexAttribute); // We add the indices linking the points in the geometry // mesh auto line = new Qt3DRender::QGeometryRenderer(_rootEntity); line->setGeometry(geometry); line->setRestartIndexValue(-1); line->setPrimitiveRestartEnabled(true); line->setPrimitiveType(Qt3DRender::QGeometryRenderer::LineStrip); auto material = new Qt3DExtras::QPhongMaterial(_rootEntity); material->setAmbient(color); // entity auto lineEntity = new Qt3DCore::QEntity(_rootEntity); lineEntity->addComponent(line); lineEntity->addComponent(material); } int main(int argc, char* argv[]) { QGuiApplication app(argc, argv); Qt3DExtras::Qt3DWindow view; Qt3DCore::QEntity *scene = new Qt3DCore::QEntity; // Camera Qt3DRender::QCamera *camera = view.camera(); camera->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); camera->setPosition(QVector3D(0, 0, 2.0f)); camera->setViewCenter(QVector3D(0, 0, 0)); QColor red(255,0,0); QVector vertices; vertices.resize(((10000) + 1) * 5); QVector indices; indices.resize(((10000) + 1) * 6); double i = 0; int k = 0; while (i < 1000) { int vIdx = 5 * k; vertices[vIdx + 0] = QVector3D(0, 0, -i / 10.0); vertices[vIdx + 1] = QVector3D(1, 0, -i / 10.0); vertices[vIdx + 2] = QVector3D(1, 1, -i / 10.0); vertices[vIdx + 3] = QVector3D(0, 1, -i / 10.0); vertices[vIdx + 4] = QVector3D(0, 0, -i / 10.0); int idx = 6 * k; indices[idx + 0] = vIdx + 0; indices[idx + 1] = vIdx + 1; indices[idx + 2] = vIdx + 2; indices[idx + 3] = vIdx + 3; indices[idx + 4] = vIdx + 4; indices[idx + 5] = -1; i += 0.1; ++k; } drawLineStrip(vertices, indices, red, scene); view.setRootEntity(scene); view.show(); return app.exec(); }