23 #include <Qt3DRender/QAttribute>
24 #include <Qt3DRender/QBuffer>
25 #include <Qt3DRender/QBufferDataGenerator>
26 #include <Qt3DRender/QBufferDataGeneratorPtr>
35 for (
int i = 0; i < faceIndex.size(); i += 3 )
37 if ( i + 2 >= faceIndex.size() )
continue;
39 if ( faceIndex[i] == faceIndex[i + 1] || faceIndex[i + 1] == faceIndex[i + 2] || faceIndex[i] == faceIndex[i + 2] )
41 for (
int j = 0; j < 3; ++j )
42 vertexIndex << faceIndex[i + j] + 1;
48 for (
int i = 0; i < positionsBuffer.size(); i += 3 )
50 for (
int j = 0; j < 3; ++j )
52 mVertexPosition << positionsBuffer[i + j] * scale + translation[j];
59 insertIndexData<uint>( mIndexes, facesIndexes );
64 Q_UNUSED( lineIndexes );
65 for (
int i = 0; i < mVertexPosition.size(); i += 3 ) mIndexes << i / 3 + 1;
70 mNormals << normalsBuffer;
75 mTexturesUV << texturesBuffer;
81 for (
auto it = parameters.begin(); it != parameters.end(); ++it )
90 for (
unsigned int vertice : mIndexes )
92 int heightIndex = ( vertice - 1 ) * 3 + 1;
93 minX = std::min( minX, mVertexPosition[heightIndex - 1] );
94 maxX = std::max( maxX, mVertexPosition[heightIndex - 1] );
95 minY = std::min( minY, mVertexPosition[heightIndex] );
96 maxY = std::max( maxY, mVertexPosition[heightIndex] );
97 minZ = std::min( minZ, mVertexPosition[heightIndex + 1] );
98 maxZ = std::max( maxZ, mVertexPosition[heightIndex + 1] );
114 for (
int i = 0; i < mVertexPosition.size(); i += 3 )
118 out << ( mVertexPosition[i] - center.x() ) / scale <<
" ";
119 out << ( mVertexPosition[i + 1] - center.y() ) / scale <<
" ";
120 out << ( mVertexPosition[i + 2] - center.z() ) / scale <<
"\n";
121 if ( i + 3 <= mNormals.size() )
123 out <<
"vn " << mNormals[i] <<
" " << mNormals[i + 1] <<
" " << mNormals[i + 2] <<
"\n";
125 int u_index = i / 3 * 2;
126 if ( u_index + 1 < mTexturesUV.size() )
129 out <<
"vt " << mTexturesUV[u_index] <<
" " << 1.0f - mTexturesUV[u_index + 1] <<
"\n";
133 bool hasTextures = mTexturesUV.size() == mVertexPosition.size() / 3 * 2;
135 bool hasNormals = mNormals.size() == mVertexPosition.size();
137 if ( !hasNormals && !mNormals.empty() )
139 QgsDebugMsg(
"Vertex normals count and vertex positions count are different" );
141 int verticesCount = mVertexPosition.size() / 3;
143 auto getVertexIndex = [&](
int i ) -> QString
145 int negativeIndex = -1 - ( verticesCount - i );
146 if ( hasNormals && !hasTextures )
147 return QStringLiteral(
"%1//%2" ).arg( negativeIndex ).arg( negativeIndex );
148 if ( !hasNormals && hasTextures )
149 return QStringLiteral(
"%1/%2" ).arg( negativeIndex ).arg( negativeIndex );
150 if ( hasNormals && hasTextures )
151 return QStringLiteral(
"%1/%2/%3" ).arg( negativeIndex ).arg( negativeIndex ).arg( negativeIndex );
152 return QString::number( negativeIndex );
158 for (
int i = 0; i < mIndexes.size(); i += 3 )
160 if ( mIndexes[i] == mIndexes[i + 1] && mIndexes[i + 1] == mIndexes[i + 2] )
162 out <<
"f " << getVertexIndex( mIndexes[i] );
163 out <<
" " << getVertexIndex( mIndexes[i + 1] );
164 out <<
" " << getVertexIndex( mIndexes[i + 2] );
171 for (
int i : mIndexes ) out <<
" " << getVertexIndex( i );
174 else if ( mType ==
Points )
177 for (
int i = 0; i < mVertexPosition.size(); i += 3 )
178 out <<
" " << getVertexIndex( i / 3 + 1 );
185 QString materialName = mName +
"_material";
186 if ( mMaterialParameters.size() == 0 && ( mTexturesUV.size() == 0 || mTextureImage.isNull() ) )
return QString();
187 mtlOut <<
"newmtl " << materialName <<
"\n";
188 if ( mTexturesUV.size() != 0 && !mTextureImage.isNull() )
190 QString filePath = QDir( folderPath ).filePath( materialName +
".jpg" );
191 mTextureImage.save( filePath,
"JPG" );
192 mtlOut <<
"\tmap_Kd " << materialName <<
".jpg" <<
"\n";
194 for ( QString key : mMaterialParameters.keys() )
196 mtlOut <<
"\t" << key <<
" " << mMaterialParameters[key] <<
"\n";
198 mtlOut <<
"\tillum 2\n";