22 #include <Qt3DRender/QAttribute>
23 #include <Qt3DRender/QBuffer>
24 #include <Qt3DRender/QBufferDataGenerator>
25 #include <Qt3DRender/QBufferDataGeneratorPtr>
34 for (
int i = 0; i < faceIndex.size(); i += 3 )
36 if ( i + 2 >= faceIndex.size() )
continue;
38 if ( faceIndex[i] == faceIndex[i + 1] || faceIndex[i + 1] == faceIndex[i + 2] || faceIndex[i] == faceIndex[i + 2] )
40 for (
int j = 0; j < 3; ++j )
41 vertexIndex << faceIndex[i + j] + 1;
47 for (
int i = 0; i < positionsBuffer.size(); i += 3 )
49 for (
int j = 0; j < 3; ++j )
51 mVertexPosition << positionsBuffer[i + j] * scale + translation[j];
58 insertIndexData<uint>( mIndexes, facesIndexes );
63 Q_UNUSED( lineIndexes );
64 for (
int i = 0; i < mVertexPosition.size(); i += 3 ) mIndexes << i / 3 + 1;
69 mNormals << normalsBuffer;
74 mTexturesUV << texturesBuffer;
80 for (
auto it = parameters.begin(); it != parameters.end(); ++it )
89 for (
const unsigned int vertice : mIndexes )
91 const int heightIndex = ( vertice - 1 ) * 3 + 1;
92 minX = std::min( minX, mVertexPosition[heightIndex - 1] );
93 maxX = std::max( maxX, mVertexPosition[heightIndex - 1] );
94 minY = std::min( minY, mVertexPosition[heightIndex] );
95 maxY = std::max( maxY, mVertexPosition[heightIndex] );
96 minZ = std::min( minZ, mVertexPosition[heightIndex + 1] );
97 maxZ = std::max( maxZ, mVertexPosition[heightIndex + 1] );
113 for (
int i = 0; i < mVertexPosition.size(); i += 3 )
117 out << ( mVertexPosition[i] - center.x() ) / scale <<
" ";
118 out << ( mVertexPosition[i + 1] - center.y() ) / scale <<
" ";
119 out << ( mVertexPosition[i + 2] - center.z() ) / scale <<
"\n";
120 if ( i + 3 <= mNormals.size() )
122 out <<
"vn " << mNormals[i] <<
" " << mNormals[i + 1] <<
" " << mNormals[i + 2] <<
"\n";
124 const int u_index = i / 3 * 2;
125 if ( u_index + 1 < mTexturesUV.size() )
128 out <<
"vt " << mTexturesUV[u_index] <<
" " << 1.0f - mTexturesUV[u_index + 1] <<
"\n";
132 bool hasTextures = mTexturesUV.size() == mVertexPosition.size() / 3 * 2;
134 bool hasNormals = mNormals.size() == mVertexPosition.size();
136 if ( !hasNormals && !mNormals.empty() )
138 QgsDebugMsg(
"Vertex normals count and vertex positions count are different" );
140 int verticesCount = mVertexPosition.size() / 3;
142 auto getVertexIndex = [&](
int i ) -> QString
144 const int negativeIndex = -1 - ( verticesCount - i );
145 if ( hasNormals && !hasTextures )
146 return QStringLiteral(
"%1//%2" ).arg( negativeIndex ).arg( negativeIndex );
147 if ( !hasNormals && hasTextures )
148 return QStringLiteral(
"%1/%2" ).arg( negativeIndex ).arg( negativeIndex );
149 if ( hasNormals && hasTextures )
150 return QStringLiteral(
"%1/%2/%3" ).arg( negativeIndex ).arg( negativeIndex ).arg( negativeIndex );
151 return QString::number( negativeIndex );
157 for (
int i = 0; i < mIndexes.size(); i += 3 )
159 if ( mIndexes[i] == mIndexes[i + 1] && mIndexes[i + 1] == mIndexes[i + 2] )
161 out <<
"f " << getVertexIndex( mIndexes[i] );
162 out <<
" " << getVertexIndex( mIndexes[i + 1] );
163 out <<
" " << getVertexIndex( mIndexes[i + 2] );
170 for (
const int i : mIndexes ) out <<
" " << getVertexIndex( i );
173 else if ( mType ==
Points )
176 for (
int i = 0; i < mVertexPosition.size(); i += 3 )
177 out <<
" " << getVertexIndex( i / 3 + 1 );
184 QString materialName = mName +
"_material";
185 if ( mMaterialParameters.size() == 0 && ( mTexturesUV.size() == 0 || mTextureImage.isNull() ) )
return QString();
186 mtlOut <<
"newmtl " << materialName <<
"\n";
187 if ( mTexturesUV.size() != 0 && !mTextureImage.isNull() )
189 const QString filePath = QDir( folderPath ).filePath( materialName +
".jpg" );
190 mTextureImage.save( filePath,
"JPG" );
191 mtlOut <<
"\tmap_Kd " << materialName <<
".jpg" <<
"\n";
193 for (
const QString &key : mMaterialParameters.keys() )
195 mtlOut <<
"\t" << key <<
" " << mMaterialParameters[key] <<
"\n";
197 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.
void insertIndexData(QVector< uint > &vertexIndex, const QVector< T > &faceIndex)