QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgs3dexportobject.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  Qgs3DExportObject.cpp
3  --------------------------------------
4  Date : June 2020
5  Copyright : (C) 2020 by Belgacem Nedjima
6  Email : gb underscore nedjima at esi dot dz
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgs3dexportobject.h"
17 
18 #include <QVector3D>
19 #include <QDir>
20 #include <QImage>
21 
22 #include <Qt3DRender/QAttribute>
23 #include <Qt3DRender/QBuffer>
24 #include <Qt3DRender/QBufferDataGenerator>
25 #include <Qt3DRender/QBufferDataGeneratorPtr>
26 
27 #include "qgslogger.h"
29 
30 
31 template<typename T>
32 void insertIndexData( QVector<uint> &vertexIndex, const QVector<T> &faceIndex )
33 {
34  for ( int i = 0; i < faceIndex.size(); i += 3 )
35  {
36  if ( i + 2 >= faceIndex.size() ) continue;
37  // skip invalid triangles
38  if ( faceIndex[i] == faceIndex[i + 1] || faceIndex[i + 1] == faceIndex[i + 2] || faceIndex[i] == faceIndex[i + 2] )
39  continue;
40  for ( int j = 0; j < 3; ++j )
41  vertexIndex << faceIndex[i + j] + 1;
42  }
43 }
44 
45 void Qgs3DExportObject::setupPositionCoordinates( const QVector<float> &positionsBuffer, float scale, const QVector3D &translation )
46 {
47  for ( int i = 0; i < positionsBuffer.size(); i += 3 )
48  {
49  for ( int j = 0; j < 3; ++j )
50  {
51  mVertexPosition << positionsBuffer[i + j] * scale + translation[j];
52  }
53  }
54 }
55 
56 void Qgs3DExportObject::setupFaces( const QVector<uint> &facesIndexes )
57 {
58  insertIndexData<uint>( mIndexes, facesIndexes );
59 }
60 
61 void Qgs3DExportObject::setupLine( const QVector<uint> &lineIndexes )
62 {
63  Q_UNUSED( lineIndexes );
64  for ( int i = 0; i < mVertexPosition.size(); i += 3 ) mIndexes << i / 3 + 1;
65 }
66 
67 void Qgs3DExportObject::setupNormalCoordinates( const QVector<float> &normalsBuffer )
68 {
69  mNormals << normalsBuffer;
70 }
71 
72 void Qgs3DExportObject::setupTextureCoordinates( const QVector<float> &texturesBuffer )
73 {
74  mTexturesUV << texturesBuffer;
75 }
76 
78 {
79  QMap<QString, QString> parameters = material->toExportParameters();
80  for ( auto it = parameters.begin(); it != parameters.end(); ++it )
81  {
82  setMaterialParameter( it.key(), it.value() );
83  }
84 }
85 
86 void Qgs3DExportObject::objectBounds( float &minX, float &minY, float &minZ, float &maxX, float &maxY, float &maxZ )
87 {
88  if ( mType != TriangularFaces ) return;
89  for ( unsigned int vertice : mIndexes )
90  {
91  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] );
98  }
99 }
100 
101 void Qgs3DExportObject::saveTo( QTextStream &out, float scale, const QVector3D &center )
102 {
103  // Set groups
104  // turns out grouping doest work as expected in blender
105 
106  // smoothen edges
107  if ( mSmoothEdges )
108  out << "s on\n";
109  else
110  out << "s off\n";
111 
112  // Construct vertices
113  for ( int i = 0; i < mVertexPosition.size(); i += 3 )
114  {
115  // for now just ignore wrong vertex positions
116  out << "v ";
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() )
121  {
122  out << "vn " << mNormals[i] << " " << mNormals[i + 1] << " " << mNormals[i + 2] << "\n";
123  }
124  int u_index = i / 3 * 2;
125  if ( u_index + 1 < mTexturesUV.size() )
126  {
127  // TODO: flip texture in a more appropriate way (for repeated textures)
128  out << "vt " << mTexturesUV[u_index] << " " << 1.0f - mTexturesUV[u_index + 1] << "\n";
129  }
130  }
131 
132  bool hasTextures = mTexturesUV.size() == mVertexPosition.size() / 3 * 2;
133  // if the object has normals then the normals and positions buffers should be the same size
134  bool hasNormals = mNormals.size() == mVertexPosition.size();
135 
136  if ( !hasNormals && !mNormals.empty() )
137  {
138  QgsDebugMsg( "Vertex normals count and vertex positions count are different" );
139  }
140  int verticesCount = mVertexPosition.size() / 3;
141 
142  auto getVertexIndex = [&]( int i ) -> QString
143  {
144  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 );
152  };
153 
154  if ( mType == TriangularFaces )
155  {
156  // Construct triangular faces
157  for ( int i = 0; i < mIndexes.size(); i += 3 )
158  {
159  if ( mIndexes[i] == mIndexes[i + 1] && mIndexes[i + 1] == mIndexes[i + 2] )
160  continue;
161  out << "f " << getVertexIndex( mIndexes[i] );
162  out << " " << getVertexIndex( mIndexes[i + 1] );
163  out << " " << getVertexIndex( mIndexes[i + 2] );
164  out << "\n";
165  }
166  }
167  else if ( mType == LineStrip )
168  {
169  out << "l";
170  for ( int i : mIndexes ) out << " " << getVertexIndex( i );
171  out << "\n";
172  }
173  else if ( mType == Points )
174  {
175  out << "p";
176  for ( int i = 0; i < mVertexPosition.size(); i += 3 )
177  out << " " << getVertexIndex( i / 3 + 1 );
178  out << "\n";
179  }
180 }
181 
182 QString Qgs3DExportObject::saveMaterial( QTextStream &mtlOut, const QString &folderPath )
183 {
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() )
188  {
189  QString filePath = QDir( folderPath ).filePath( materialName + ".jpg" );
190  mTextureImage.save( filePath, "JPG" );
191  mtlOut << "\tmap_Kd " << materialName << ".jpg" << "\n";
192  }
193  for ( QString key : mMaterialParameters.keys() )
194  {
195  mtlOut << "\t" << key << " " << mMaterialParameters[key] << "\n";
196  }
197  mtlOut << "\tillum 2\n";
198  return materialName;
199 }
void setMaterialParameter(const QString &parameter, const QString &value)
Sets a material parameter to be exported in the .mtl file.
void saveTo(QTextStream &out, float scale, const QVector3D &center)
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)
#define QgsDebugMsg(str)
Definition: qgslogger.h:38