46 #include <Qt3DCore/QNode>
60 using namespace DISP3DLIB;
61 using namespace Qt3DRender;
72 Qt3DCore::QNode *tParent)
73 : QGeometryRenderer(tParent)
74 , m_pGeometry(tGeometry)
75 , m_pTransformBuffer(new QT_COMPATIBILITY_3D::QBuffer())
76 , m_pColorBuffer(new QT_COMPATIBILITY_3D::QBuffer())
77 , m_pTransformAttribute(new QT_COMPATIBILITY_3D::QAttribute())
78 , m_pColorAttribute(new QT_COMPATIBILITY_3D::QAttribute())
87 m_pGeometry->deleteLater();
88 m_pTransformBuffer->deleteLater();
89 m_pColorBuffer->deleteLater();
90 m_pTransformAttribute->deleteLater();
91 m_pColorAttribute->deleteLater();
99 m_pTransformBuffer->setData(buildTransformBuffer(tInstanceTansform));
101 this->setInstanceCount(tInstanceTansform.size());
109 m_pColorBuffer->setData(buildColorBuffer(tInstanceColors));
111 if(tInstanceColors.size() > 1) {
112 m_pColorAttribute->setDivisor(1);
115 m_pColorAttribute->setDivisor(0);
118 this->setInstanceCount(tInstanceColors.size());
123 void GeometryMultiplier::init()
126 m_pTransformAttribute->setName(QStringLiteral(
"instanceModelMatrix"));
127 m_pTransformAttribute->setAttributeType(QT_COMPATIBILITY_3D::QAttribute::VertexAttribute);
128 m_pTransformAttribute->setVertexBaseType(QT_COMPATIBILITY_3D::QAttribute::Float);
129 m_pTransformAttribute->setVertexSize(16);
130 m_pTransformAttribute->setDivisor(1);
131 m_pTransformAttribute->setByteOffset(0);
132 m_pTransformAttribute->setBuffer(m_pTransformBuffer);
135 m_pColorAttribute->setName(QStringLiteral(
"instanceColor"));
136 m_pColorAttribute->setAttributeType(QT_COMPATIBILITY_3D::QAttribute::VertexAttribute);
137 m_pColorAttribute->setVertexBaseType(QT_COMPATIBILITY_3D::QAttribute::Float);
138 m_pColorAttribute->setVertexSize(4);
141 m_pColorAttribute->setDivisor(0);
142 m_pColorAttribute->setByteOffset(0);
143 m_pColorAttribute->setBuffer(m_pColorBuffer);
146 QVector<QColor> tempColors;
147 tempColors.push_back(QColor(0, 0, 255));
151 QVector<QMatrix4x4> tempTrans;
152 tempTrans.push_back(QMatrix4x4());
156 m_pGeometry->addAttribute(m_pTransformAttribute);
157 m_pGeometry->addAttribute(m_pColorAttribute);
160 this->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles);
161 this->setIndexOffset(0);
162 this->setFirstInstance(0);
163 this->setGeometry(m_pGeometry.data());
168 QByteArray GeometryMultiplier::buildTransformBuffer(
const QVector<QMatrix4x4> &tInstanceTransform)
170 const uint iVertNum = tInstanceTransform.size();
171 const uint iMatrixDim = 4;
172 const uint iMatrixSize = iMatrixDim * iMatrixDim;
174 QByteArray bufferData;
175 bufferData.resize(iVertNum * iMatrixSize * (
int)
sizeof(
float));
176 float *rawVertexArray =
reinterpret_cast<float *
>(bufferData.data());
179 for(uint i = 0 ; i < iVertNum; i++)
181 const float *rawMatrix = tInstanceTransform.at(i).data();
182 for(uint idx = 0; idx < iMatrixSize; idx++)
184 rawVertexArray[iMatrixSize * i + idx] = rawMatrix[idx];
193 QByteArray GeometryMultiplier::buildColorBuffer(
const QVector<QColor> &tInstanceColor)
195 const uint iVertSize = 4;
197 QByteArray bufferData;
198 bufferData.resize(tInstanceColor.size() * iVertSize * (
int)
sizeof(
float));
199 float *rawVertexArray =
reinterpret_cast<float *
>(bufferData.data());
202 for(
int i = 0; i < tInstanceColor.size(); i++)
204 rawVertexArray[iVertSize * i] = tInstanceColor[i].redF();
205 rawVertexArray[iVertSize * i + 1] = tInstanceColor[i].greenF();
206 rawVertexArray[iVertSize * i + 2] = tInstanceColor[i].blueF();
207 rawVertexArray[iVertSize * i + 3] = tInstanceColor[i].alphaF();