22#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
23#include <Qt3DRender/QAttribute>
24#include <Qt3DRender/QBuffer>
28#include <Qt3DCore/QAttribute>
29#include <Qt3DCore/QBuffer>
41 for (
int i = 0; i < faceIndex.size(); i += 3 )
43 if ( i + 2 >= faceIndex.size() )
continue;
45 if ( faceIndex[i] == faceIndex[i + 1] || faceIndex[i + 1] == faceIndex[i + 2] || faceIndex[i] == faceIndex[i + 2] )
47 for (
int j = 0; j < 3; ++j )
48 vertexIndex << faceIndex[i + j];
54 for (
int i = 0; i < positionsBuffer.size(); i += 3 )
56 for (
int j = 0; j < 3; ++j )
58 mVertexPosition << positionsBuffer[i + j] * scale + translation[j];
65 insertIndexData<uint>( mIndexes, facesIndexes );
70 Q_UNUSED( lineIndexes );
71 for (
int i = 0; i < mVertexPosition.size(); i += 3 ) mIndexes << i / 3 + 1;
76 mNormals << normalsBuffer;
81 mTexturesUV << texturesBuffer;
87 for (
auto it = parameters.begin(); it != parameters.end(); ++it )
96 for (
const unsigned int vertice : mIndexes )
98 const unsigned int heightIndex = vertice * 3 + 1;
99 minX = std::min( minX, mVertexPosition[heightIndex - 1] );
100 maxX = std::max( maxX, mVertexPosition[heightIndex - 1] );
101 minY = std::min( minY, mVertexPosition[heightIndex] );
102 maxY = std::max( maxY, mVertexPosition[heightIndex] );
103 minZ = std::min( minZ, mVertexPosition[heightIndex + 1] );
104 maxZ = std::max( maxZ, mVertexPosition[heightIndex + 1] );
120 for (
const unsigned int vertice : mIndexes )
122 const int i =
static_cast<int>( vertice * 3 );
125 out << ( mVertexPosition[i] - center.x() ) / scale <<
" ";
126 out << ( mVertexPosition[i + 1] - center.y() ) / scale <<
" ";
127 out << ( mVertexPosition[i + 2] - center.z() ) / scale <<
"\n";
128 if ( i + 3 <= mNormals.size() )
130 out <<
"vn " << mNormals[i] <<
" " << mNormals[i + 1] <<
" " << mNormals[i + 2] <<
"\n";
132 const int u_index = i / 3 * 2;
133 if ( u_index + 1 < mTexturesUV.size() )
136 out <<
"vt " << mTexturesUV[u_index] <<
" " << 1.0f - mTexturesUV[u_index + 1] <<
"\n";
140 bool hasTextures = mTexturesUV.size() == mVertexPosition.size() / 3 * 2;
142 bool hasNormals = mNormals.size() == mVertexPosition.size();
144 if ( !hasNormals && !mNormals.empty() )
146 QgsDebugError(
"Vertex normals count and vertex positions count are different" );
148 int verticesCount = mIndexes.size();
152 auto getVertexIndex = [&](
unsigned int i ) -> QString
154 const int negativeIndex =
static_cast<int>( - ( verticesCount - i ) - mIndexes[0] );
155 if ( hasNormals && !hasTextures )
156 return QStringLiteral(
"%1//%2" ).arg( negativeIndex ).arg( negativeIndex );
157 if ( !hasNormals && hasTextures )
158 return QStringLiteral(
"%1/%2" ).arg( negativeIndex ).arg( negativeIndex );
159 if ( hasNormals && hasTextures )
160 return QStringLiteral(
"%1/%2/%3" ).arg( negativeIndex ).arg( negativeIndex ).arg( negativeIndex );
161 return QString::number( negativeIndex );
167 for (
int i = 0; i < mIndexes.size(); i += 3 )
169 if ( mIndexes[i] == mIndexes[i + 1] && mIndexes[i + 1] == mIndexes[i + 2] )
171 out <<
"f " << getVertexIndex( mIndexes[i] );
172 out <<
" " << getVertexIndex( mIndexes[i + 1] );
173 out <<
" " << getVertexIndex( mIndexes[i + 2] );
180 for (
const unsigned int i : mIndexes )
181 out <<
" " << getVertexIndex( i );
184 else if ( mType ==
Points )
187 for (
const unsigned int i : mIndexes )
188 out <<
" " << getVertexIndex( i );
195 QString materialName = mName +
"_material";
196 if ( mMaterialParameters.size() == 0 && ( mTexturesUV.size() == 0 || mTextureImage.isNull() ) )
return QString();
197 mtlOut <<
"newmtl " << materialName <<
"\n";
198 if ( mTexturesUV.size() != 0 && !mTextureImage.isNull() )
200 const QString filePath = QDir( folderPath ).filePath( materialName +
".jpg" );
201 mTextureImage.save( filePath,
"JPG" );
202 mtlOut <<
"\tmap_Kd " << materialName <<
".jpg" <<
"\n";
204 for (
auto it = mMaterialParameters.constBegin(); it != mMaterialParameters.constEnd(); it++ )
206 mtlOut <<
"\t" << it.key() <<
" " << it.value() <<
"\n";
208 mtlOut <<
"\tillum 2\n";
void setMaterialParameter(const QString ¶meter, const QString &value)
Sets a material parameter to be exported in the .mtl file.
void saveTo(QTextStream &out, float scale, const QVector3D ¢er)
Saves the current object to the output stream while scaling the object and centering it to be visible...
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 setupNormalCoordinates(const QVector< float > &normalsBuffer)
Sets normal coordinates for each vertex.
void setupPositionCoordinates(const QVector< float > &positionsBuffer, float scale=1.0f, const QVector3D &translation=QVector3D(0, 0, 0))
Sets positions coordinates and does the translation and scaling.
void setupMaterial(QgsAbstractMaterialSettings *material)
Sets the material parameters (diffuse color, shininess...) from phong material.
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.
Qt3DCore::QAttribute Qt3DQAttribute
Qt3DCore::QBuffer Qt3DQBuffer
void insertIndexData(QVector< uint > &vertexIndex, const QVector< T > &faceIndex)
#define QgsDebugError(str)