|
| 1 | +/* |
| 2 | + * Copyright (c) 2022, Yung-Yu Chen <[email protected]> |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions are met: |
| 6 | + * |
| 7 | + * - Redistributions of source code must retain the above copyright notice, |
| 8 | + * this list of conditions and the following disclaimer. |
| 9 | + * - Redistributions in binary form must reproduce the above copyright notice, |
| 10 | + * this list of conditions and the following disclaimer in the documentation |
| 11 | + * and/or other materials provided with the distribution. |
| 12 | + * - Neither the name of the copyright holder nor the names of its contributors |
| 13 | + * may be used to endorse or promote products derived from this software |
| 14 | + * without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + */ |
| 28 | + |
| 29 | +#include <modmesh/view/common_detail.hpp> // Must be the first include. |
| 30 | +#include <modmesh/view/RAxisMark.hpp> |
| 31 | + |
| 32 | +#include <modmesh/modmesh.hpp> |
| 33 | + |
| 34 | +#include <Qt> |
| 35 | +#include <QWidget> |
| 36 | +#include <Qt3DWindow> |
| 37 | + |
| 38 | +#include <QByteArray> |
| 39 | +#include <QGeometryRenderer> |
| 40 | + |
| 41 | +#include <Qt3DCore/QBuffer> |
| 42 | +#include <Qt3DCore/QEntity> |
| 43 | +#include <Qt3DCore/QGeometry> |
| 44 | +#include <Qt3DCore/QAttribute> |
| 45 | +#include <Qt3DCore/QTransform> |
| 46 | + |
| 47 | +#include <Qt3DExtras/QDiffuseSpecularMaterial> |
| 48 | + |
| 49 | +namespace modmesh |
| 50 | +{ |
| 51 | + |
| 52 | +RLine::RLine(QVector3D const & v0, QVector3D const & v1, QColor const & color, Qt3DCore::QNode * parent) |
| 53 | + : Qt3DCore::QEntity(parent) |
| 54 | + , m_geometry(new Qt3DCore::QGeometry(this)) |
| 55 | + , m_renderer(new Qt3DRender::QGeometryRenderer()) |
| 56 | + , m_material(new Qt3DExtras::QDiffuseSpecularMaterial()) |
| 57 | +{ |
| 58 | + { |
| 59 | + auto * buf = new Qt3DCore::QBuffer(m_geometry); |
| 60 | + { |
| 61 | + QByteArray barray; |
| 62 | + barray.resize(3 * 2 * sizeof(float)); |
| 63 | + float * ptr = reinterpret_cast<float *>(barray.data()); |
| 64 | + ptr[0] = v0.x(); |
| 65 | + ptr[1] = v0.y(); |
| 66 | + ptr[2] = v0.z(); |
| 67 | + ptr[3] = v1.x(); |
| 68 | + ptr[4] = v1.y(); |
| 69 | + ptr[5] = v1.z(); |
| 70 | + buf->setData(barray); |
| 71 | + } |
| 72 | + |
| 73 | + { |
| 74 | + auto * vertices = new Qt3DCore::QAttribute(m_geometry); |
| 75 | + vertices->setName(Qt3DCore::QAttribute::defaultPositionAttributeName()); |
| 76 | + vertices->setAttributeType(Qt3DCore::QAttribute::VertexAttribute); |
| 77 | + vertices->setVertexBaseType(Qt3DCore::QAttribute::Float); |
| 78 | + vertices->setVertexSize(3); |
| 79 | + vertices->setBuffer(buf); |
| 80 | + vertices->setByteStride(3 * sizeof(float)); |
| 81 | + vertices->setCount(2); |
| 82 | + m_geometry->addAttribute(vertices); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + { |
| 87 | + auto * buf = new Qt3DCore::QBuffer(m_geometry); |
| 88 | + { |
| 89 | + QByteArray barray; |
| 90 | + barray.resize(2 * sizeof(uint32_t)); |
| 91 | + auto * indices = reinterpret_cast<uint32_t *>(barray.data()); |
| 92 | + indices[0] = 0; |
| 93 | + indices[1] = 1; |
| 94 | + buf->setData(barray); |
| 95 | + } |
| 96 | + |
| 97 | + { |
| 98 | + auto * indices = new Qt3DCore::QAttribute(m_geometry); |
| 99 | + indices->setVertexBaseType(Qt3DCore::QAttribute::UnsignedInt); |
| 100 | + indices->setAttributeType(Qt3DCore::QAttribute::IndexAttribute); |
| 101 | + indices->setBuffer(buf); |
| 102 | + indices->setCount(2); |
| 103 | + m_geometry->addAttribute(indices); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + m_renderer->setGeometry(m_geometry); |
| 108 | + m_renderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Lines); |
| 109 | + addComponent(m_renderer); |
| 110 | + addComponent(m_material); |
| 111 | + m_material->setAmbient(color); |
| 112 | +} |
| 113 | + |
| 114 | +RAxisMark::RAxisMark(Qt3DCore::QNode * parent) |
| 115 | + : Qt3DCore::QEntity(parent) |
| 116 | + , m_xmark(new RLine(QVector3D(0, 0, 0), QVector3D(1, 0, 0), QColor(255, 0, 0, 255), this)) |
| 117 | + , m_ymark(new RLine(QVector3D(0, 0, 0), QVector3D(0, 1, 0), QColor(0, 255, 0, 255), this)) |
| 118 | + , m_zmark(new RLine(QVector3D(0, 0, 0), QVector3D(0, 0, 1), QColor(0, 0, 255, 255), this)) |
| 119 | +{ |
| 120 | +} |
| 121 | + |
| 122 | +} /* end namespace modmesh */ |
| 123 | + |
| 124 | +// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4: |
0 commit comments