23#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
24#include <Qt3DRender/QAttribute>
25#include <Qt3DRender/QBuffer>
29#include <Qt3DCore/QAttribute>
30#include <Qt3DCore/QBuffer>
42 for (
int i = 0; i < faceIndex.size(); i += 3 )
44 if ( i + 2 >= faceIndex.size() )
47 if ( faceIndex[i] == faceIndex[i + 1] || faceIndex[i + 1] == faceIndex[i + 2] || faceIndex[i] == faceIndex[i + 2] )
49 for (
int j = 0; j < 3; ++j )
50 vertexIndex << faceIndex[i + j];
56 for (
int i = 0; i < positionsBuffer.size(); i += 3 )
58 const QVector3D position( positionsBuffer[i], positionsBuffer[i + 1], positionsBuffer[i + 2] );
59 const QVector3D positionFinal = transform.map( position );
60 mVertexPosition << positionFinal.x() << positionFinal.y() << positionFinal.z();
66 insertIndexData<uint>( mIndexes, facesIndexes );
71 Q_UNUSED( lineIndexes );
72 for (
int i = 0; i < mVertexPosition.size(); i += 3 )
73 mIndexes << i / 3 + 1;
79 QMatrix3x3 normal3x3 = transform.normalMatrix();
80 QMatrix4x4 normal4x4( normal3x3( 0, 0 ), normal3x3( 0, 1 ), normal3x3( 0, 2 ), 0, normal3x3( 1, 0 ), normal3x3( 1, 1 ), normal3x3( 1, 2 ), 0, normal3x3( 2, 0 ), normal3x3( 2, 1 ), normal3x3( 2, 2 ), 0, 0, 0, 0, 1 );
82 for (
int i = 0; i < normalsBuffer.size(); i += 3 )
84 const QVector3D normalVector( normalsBuffer[i], normalsBuffer[i + 1], normalsBuffer[i + 2] );
85 QVector3D v = normal4x4.mapVector( normalVector );
93 mNormals << v.x() << v.y() << v.z();
99 mTexturesUV << texturesBuffer;
105 for (
auto it = parameters.begin(); it != parameters.end(); ++it )
115 for (
const unsigned int vertice : qAsConst( mIndexes ) )
117 const int heightIndex =
static_cast<int>( vertice ) * 3 + 1;
118 minX = std::min( minX, mVertexPosition[heightIndex - 1] );
119 maxX = std::max( maxX, mVertexPosition[heightIndex - 1] );
120 minY = std::min( minY, mVertexPosition[heightIndex] );
121 maxY = std::max( maxY, mVertexPosition[heightIndex] );
122 minZ = std::min( minZ, mVertexPosition[heightIndex + 1] );
123 maxZ = std::max( maxZ, mVertexPosition[heightIndex + 1] );
131 out << qSetRealNumberPrecision(
precision );
142 for (
const unsigned int vertice : qAsConst( mIndexes ) )
144 const int i =
static_cast<int>( vertice * 3 );
147 out << ( mVertexPosition[i] - center.x() ) / scale <<
" ";
148 out << ( mVertexPosition[i + 1] - center.y() ) / scale <<
" ";
149 out << ( mVertexPosition[i + 2] - center.z() ) / scale <<
"\n";
150 if ( i + 3 <= mNormals.size() )
152 out <<
"vn " << mNormals[i] <<
" " << mNormals[i + 1] <<
" " << mNormals[i + 2] <<
"\n";
154 const int u_index = i / 3 * 2;
155 if ( u_index + 1 < mTexturesUV.size() )
158 out <<
"vt " << mTexturesUV[u_index] <<
" " << 1.0f - mTexturesUV[u_index + 1] <<
"\n";
162 bool hasTextures = mTexturesUV.size() == mVertexPosition.size() / 3 * 2;
164 bool hasNormals = mNormals.size() == mVertexPosition.size();
166 if ( !hasNormals && !mNormals.empty() )
168 QgsDebugError(
"Vertex normals count and vertex positions count are different" );
170 const int verticesCount = mIndexes.size();
174 auto getVertexIndex = [&](
unsigned int i ) -> QString {
175 const int negativeIndex =
static_cast<int>( i - verticesCount );
176 if ( hasNormals && !hasTextures )
177 return QStringLiteral(
"%1//%2" ).arg( negativeIndex ).arg( negativeIndex );
178 if ( !hasNormals && hasTextures )
179 return QStringLiteral(
"%1/%2" ).arg( negativeIndex ).arg( negativeIndex );
180 if ( hasNormals && hasTextures )
181 return QStringLiteral(
"%1/%2/%3" ).arg( negativeIndex ).arg( negativeIndex ).arg( negativeIndex );
182 return QString::number( negativeIndex );
190 for (
int i = 0; i < mIndexes.size(); i += 3 )
192 out <<
"f " << getVertexIndex( i );
193 out <<
" " << getVertexIndex( i + 1 );
194 out <<
" " << getVertexIndex( i + 2 );
201 for (
const unsigned int i : qAsConst( mIndexes ) )
202 out <<
" " << getVertexIndex( i );
205 else if ( mType ==
Points )
208 for (
const unsigned int i : qAsConst( mIndexes ) )
209 out <<
" " << getVertexIndex( i );
216 QString materialName = mName +
"_material";
217 if ( mMaterialParameters.size() == 0 && ( mTexturesUV.size() == 0 || mTextureImage.isNull() ) )
219 mtlOut <<
"newmtl " << materialName <<
"\n";
220 if ( mTexturesUV.size() != 0 && !mTextureImage.isNull() )
222 const QString filePath = QDir( folderPath ).filePath( materialName +
".jpg" );
223 mTextureImage.save( filePath,
"JPG" );
224 mtlOut <<
"\tmap_Kd " << materialName <<
".jpg" <<
"\n";
226 for (
auto it = mMaterialParameters.constBegin(); it != mMaterialParameters.constEnd(); it++ )
228 mtlOut <<
"\t" << it.key() <<
" " << it.value() <<
"\n";
230 mtlOut <<
"\tillum 2\n";
void setMaterialParameter(const QString ¶meter, const QString &value)
Sets a material parameter to be exported in the .mtl file.
void setupPositionCoordinates(const QVector< float > &positionsBuffer, const QMatrix4x4 &transform)
Sets positions coordinates and does the translation, rotation and scaling.
void objectBounds(float &minX, float &minY, float &minZ, float &maxX, float &maxY, float &maxZ)
Updates the box bounds explained with the current object bounds This expands the bounding box if the ...
void saveTo(QTextStream &out, float scale, const QVector3D ¢er, int precision=6)
Saves the current object to the output stream while scaling the object and centering it to be visible...
void setupMaterial(QgsAbstractMaterialSettings *material)
Sets the material parameters (diffuse color, shininess...) from phong material.
void setupNormalCoordinates(const QVector< float > &normalsBuffer, const QMatrix4x4 &transform)
Sets normal coordinates for each vertex.
QString saveMaterial(QTextStream &mtlOut, const QString &folder)
saves the texture of the object and material information
void setupLine(const QVector< uint > &facesIndexes)
sets line vertex indexes
void setupFaces(const QVector< uint > &facesIndexes)
Sets the faces in facesIndexes to the faces in the object.
void setupTextureCoordinates(const QVector< float > &texturesBuffer)
Sets texture coordinates for each vertex.
virtual QMap< QString, QString > toExportParameters() const =0
Returns the parameters to be exported to .mtl file.
bool qgsFloatNear(float a, float b, float epsilon=4 *FLT_EPSILON)
Compare two floats (but allow some difference)
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
void insertIndexData(QVector< uint > &vertexIndex, const QVector< T > &faceIndex)
#define QgsDebugError(str)