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() )
continue;
46 if ( faceIndex[i] == faceIndex[i + 1] || faceIndex[i + 1] == faceIndex[i + 2] || faceIndex[i] == faceIndex[i + 2] )
48 for (
int j = 0; j < 3; ++j )
49 vertexIndex << faceIndex[i + j];
55 for (
int i = 0; i < positionsBuffer.size(); i += 3 )
57 const QVector3D position( positionsBuffer[i], positionsBuffer[i + 1], positionsBuffer[i + 2] );
58 const QVector3D positionFinal = transform.map( position );
59 mVertexPosition << positionFinal.x() << positionFinal.y() << positionFinal.z();
65 insertIndexData<uint>( mIndexes, facesIndexes );
70 Q_UNUSED( lineIndexes );
71 for (
int i = 0; i < mVertexPosition.size(); i += 3 ) mIndexes << i / 3 + 1;
77 QMatrix3x3 normal3x3 = transform.normalMatrix();
78 QMatrix4x4 normal4x4( normal3x3( 0, 0 ), normal3x3( 0, 1 ), normal3x3( 0, 2 ), 0,
79 normal3x3( 1, 0 ), normal3x3( 1, 1 ), normal3x3( 1, 2 ), 0,
80 normal3x3( 2, 0 ), normal3x3( 2, 1 ), normal3x3( 2, 2 ), 0,
83 for (
int i = 0; i < normalsBuffer.size(); i += 3 )
85 const QVector3D normalVector( normalsBuffer[i], normalsBuffer[i + 1], normalsBuffer[i + 2] );
86 QVector3D v = normal4x4.mapVector( normalVector );
91 mNormals << v.x() << v.y() << v.z();
97 mTexturesUV << texturesBuffer;
103 for (
auto it = parameters.begin(); it != parameters.end(); ++it )
112 for (
const unsigned int vertice : qAsConst( mIndexes ) )
114 const int heightIndex =
static_cast<int>( vertice ) * 3 + 1;
115 minX = std::min( minX, mVertexPosition[heightIndex - 1] );
116 maxX = std::max( maxX, mVertexPosition[heightIndex - 1] );
117 minY = std::min( minY, mVertexPosition[heightIndex] );
118 maxY = std::max( maxY, mVertexPosition[heightIndex] );
119 minZ = std::min( minZ, mVertexPosition[heightIndex + 1] );
120 maxZ = std::max( maxZ, mVertexPosition[heightIndex + 1] );
128 out << qSetRealNumberPrecision(
precision );
139 for (
const unsigned int vertice : qAsConst( mIndexes ) )
141 const int i =
static_cast<int>( vertice * 3 );
144 out << ( mVertexPosition[i] - center.x() ) / scale <<
" ";
145 out << ( mVertexPosition[i + 1] - center.y() ) / scale <<
" ";
146 out << ( mVertexPosition[i + 2] - center.z() ) / scale <<
"\n";
147 if ( i + 3 <= mNormals.size() )
149 out <<
"vn " << mNormals[i] <<
" " << mNormals[i + 1] <<
" " << mNormals[i + 2] <<
"\n";
151 const int u_index = i / 3 * 2;
152 if ( u_index + 1 < mTexturesUV.size() )
155 out <<
"vt " << mTexturesUV[u_index] <<
" " << 1.0f - mTexturesUV[u_index + 1] <<
"\n";
159 bool hasTextures = mTexturesUV.size() == mVertexPosition.size() / 3 * 2;
161 bool hasNormals = mNormals.size() == mVertexPosition.size();
163 if ( !hasNormals && !mNormals.empty() )
165 QgsDebugError(
"Vertex normals count and vertex positions count are different" );
167 const int verticesCount = mIndexes.size();
171 auto getVertexIndex = [&](
unsigned int i ) -> QString
173 const int negativeIndex =
static_cast<int>( i - verticesCount );
174 if ( hasNormals && !hasTextures )
175 return QStringLiteral(
"%1//%2" ).arg( negativeIndex ).arg( negativeIndex );
176 if ( !hasNormals && hasTextures )
177 return QStringLiteral(
"%1/%2" ).arg( negativeIndex ).arg( negativeIndex );
178 if ( hasNormals && hasTextures )
179 return QStringLiteral(
"%1/%2/%3" ).arg( negativeIndex ).arg( negativeIndex ).arg( negativeIndex );
180 return QString::number( negativeIndex );
188 for (
int i = 0; i < mIndexes.size(); i += 3 )
190 out <<
"f " << getVertexIndex( i );
191 out <<
" " << getVertexIndex( i + 1 );
192 out <<
" " << getVertexIndex( i + 2 );
199 for (
const unsigned int i : qAsConst( mIndexes ) )
200 out <<
" " << getVertexIndex( i );
203 else if ( mType ==
Points )
206 for (
const unsigned int i : qAsConst( mIndexes ) )
207 out <<
" " << getVertexIndex( i );
214 QString materialName = mName +
"_material";
215 if ( mMaterialParameters.size() == 0 && ( mTexturesUV.size() == 0 || mTextureImage.isNull() ) )
return QString();
216 mtlOut <<
"newmtl " << materialName <<
"\n";
217 if ( mTexturesUV.size() != 0 && !mTextureImage.isNull() )
219 const QString filePath = QDir( folderPath ).filePath( materialName +
".jpg" );
220 mTextureImage.save( filePath,
"JPG" );
221 mtlOut <<
"\tmap_Kd " << materialName <<
".jpg" <<
"\n";
223 for (
auto it = mMaterialParameters.constBegin(); it != mMaterialParameters.constEnd(); it++ )
225 mtlOut <<
"\t" << it.key() <<
" " << it.value() <<
"\n";
227 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)