Skip to content

Commit

Permalink
Deleted createPlane*Data functions
Browse files Browse the repository at this point in the history
Switched to buffer data generator
  • Loading branch information
NEDJIMAbelgacem committed Jul 10, 2020
1 parent fcdd64d commit 29c980e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 132 deletions.
25 changes: 21 additions & 4 deletions src/3d/qgs3dexportobject.cpp
Expand Up @@ -44,27 +44,44 @@ void Qgs3DExportObject::setupPositionCoordinates( const QVector<float> &position
}
}

void Qgs3DExportObject::setupPositionCoordinates( const QVector<float> &positionsBuffer, const QVector<unsigned int> &faceIndex, float scale, const QVector3D translation )
void insertPositionData( QVector<float> &vertexPosition, const QVector<float> &positionsBuffer, float scale, const QVector3D translation )
{
// TODO: delete vertices that are not used
for ( int i = 0; i < positionsBuffer.size(); i += 3 )
{
for ( int j = 0; j < 3; ++j )
{
mVertexPosition << positionsBuffer[i + j] * scale + translation[j];
vertexPosition << positionsBuffer[i + j] * scale + translation[j];
}
}
}

template<typename T>
void insertIndexData( QVector<uint> &vertexIndex, const QVector<T> &faceIndex )
{
for ( int i = 0; i < faceIndex.size(); i += 3 )
{
// skip invalid triangles
if ( faceIndex[i] == faceIndex[i + 1] && faceIndex[i + 1] == faceIndex[i + 2] )
continue;
for ( int j = 0; j < 3; ++j )
mIndexes << faceIndex[i + j] + 1;
vertexIndex << faceIndex[i + j] + 1;
}
}

void Qgs3DExportObject::setupPositionCoordinates( const QVector<float> &positionsBuffer, const QVector<uint> &faceIndex, float scale, const QVector3D translation )
{
// TODO: delete vertices that are not used
insertPositionData( mVertexPosition, positionsBuffer, scale, translation );
insertIndexData<uint>( mIndexes, faceIndex );
}

void Qgs3DExportObject::setupPositionCoordinates( const QVector<float> &positionsBuffer, const QVector<quint16> &faceIndex, float scale, const QVector3D translation )
{
// TODO: delete vertices that are not used
insertPositionData( mVertexPosition, positionsBuffer, scale, translation );
insertIndexData<quint16>( mIndexes, faceIndex );
}

void Qgs3DExportObject::setupNormalCoordinates( const QVector<float> &normalsBuffer )
{
mNormals << normalsBuffer;
Expand Down
7 changes: 7 additions & 0 deletions src/3d/qgs3dexportobject.h
Expand Up @@ -60,6 +60,12 @@ class Qgs3DExportObject : public QObject
//! Sets positions coordinates from just one positions buffer and indexes buffer and does the translation and scaling
void setupPositionCoordinates( const QVector<float> &positionsBuffer, const QVector<unsigned int> &facesIndexes, float scale = 1.0f, const QVector3D translation = QVector3D( 0, 0, 0 ) );

/**
* Sets positions coordinates from just one positions buffer and indexes buffer and does the translation and scaling
* Overloaded for 16 bit integers
*/
void setupPositionCoordinates( const QVector<float> &positionsBuffer, const QVector<quint16> &facesIndexes, float scale = 1.0f, const QVector3D translation = QVector3D( 0, 0, 0 ) );

//! Sets normal coordinates for each vertex
void setupNormalCoordinates( const QVector<float> &normalsBuffer );
//! Sets texture coordinates for each vertex
Expand All @@ -71,6 +77,7 @@ class Qgs3DExportObject : public QObject
QImage textureImage() { return mTextureImage; }

/**
*
* Updates the box bounds explained with the current object bounds
* This expands the bounding box if the current object outside the bounds of the already established bounds
*/
Expand Down
1 change: 0 additions & 1 deletion src/3d/qgs3dmapscene.cpp
Expand Up @@ -785,7 +785,6 @@ void Qgs3DMapScene::exportScene( const Qgs3DMapExportSettings &exportSettings )
switch ( layerType )
{
case QgsMapLayerType::VectorLayer:
qDebug() << "Parsing vector layer";
exporter.parseVectorLayerEntity( rootEntity );
break;
case QgsMapLayerType::RasterLayer:
Expand Down
153 changes: 26 additions & 127 deletions src/3d/qgs3dsceneexporter.cpp
Expand Up @@ -30,6 +30,8 @@
#include <Qt3DExtras/QTextureMaterial>
#include <Qt3DRender/QTextureImage>
#include <Qt3DRender/QTexture>
#include <Qt3DRender/QBufferDataGenerator>
#include <Qt3DRender/QBufferDataGeneratorPtr>

#include <QByteArray>
#include <QFile>
Expand Down Expand Up @@ -82,15 +84,16 @@ QVector<T> getAttributeData( Qt3DRender::QAttribute *attribute, QByteArray data
return result;
}

QVector<unsigned int> getIndexData( QByteArray data )
template<typename T>
QVector<T> getIndexData( QByteArray data )
{
QVector<unsigned int> result;
for ( int i = 0; i < data.size(); i += sizeof( unsigned int ) )
QVector<T> result;
for ( int i = 0; i < data.size(); i += sizeof( T ) )
{
// maybe a problem with indienness can happen?
unsigned int v;
T v;
char *vArr = ( char * )&v;
for ( unsigned int k = 0; k < sizeof( unsigned int ); ++k )
for ( T k = 0; k < sizeof( T ); ++k )
{
vArr[k] = data.at( i + k );
}
Expand All @@ -100,114 +103,6 @@ QVector<unsigned int> getIndexData( QByteArray data )
return result;
}

QVector<float> createPlaneVertexData( float w, float h, const QSize &resolution )
{
// Taken from Qt3D source with some modifications
Q_ASSERT( w > 0.0f );
Q_ASSERT( h > 0.0f );
Q_ASSERT( resolution.width() >= 2 );
Q_ASSERT( resolution.height() >= 2 );

// Populate a buffer with the interleaved per-vertex data with
// vec3 pos, vec2 texCoord, vec3 normal, vec4 tangent
QVector<float> data;

const float x0 = -w / 2.0f;
const float z0 = -h / 2.0f;
const float dx = w / ( resolution.width() - 1 );
const float dz = h / ( resolution.height() - 1 );

// Iterate over z
for ( int j = 0; j < resolution.height(); ++j )
{
const float z = z0 + static_cast<float>( j ) * dz;

// Iterate over x
for ( int i = 0; i < resolution.width(); ++i )
{
const float x = x0 + static_cast<float>( i ) * dx;

// position
data.push_back( x );
data.push_back( 0.0 );
data.push_back( z );
}
}

return data;
}

QVector<float> createPlaneTexCoordsData( float w, float h, const QSize &resolution, bool mirrored )
{
Q_ASSERT( w > 0.0f );
Q_ASSERT( h > 0.0f );
Q_ASSERT( resolution.width() >= 2 );
Q_ASSERT( resolution.height() >= 2 );

const int nVerts = resolution.width() * resolution.height();

// Populate a buffer with the interleaved per-vertex data with
// vec3 pos, vec2 texCoord, vec3 normal, vec4 tangent
const quint32 elementSize = 3 + 2 + 3 + 4;
const quint32 stride = elementSize * sizeof( float );
QVector<float> buffer;
buffer.resize( stride * nVerts );

const float du = 1.0 / ( resolution.width() - 1 );
const float dv = 1.0 / ( resolution.height() - 1 );

// Iterate over z
for ( int j = 0; j < resolution.height(); ++j )
{
const float v = static_cast<float>( j ) * dv;

// Iterate over x
for ( int i = 0; i < resolution.width(); ++i )
{
const float u = static_cast<float>( i ) * du;

// texture coordinates
buffer.push_back( u );
buffer.push_back( mirrored ? 1.0f - v : v );
}
}

return buffer;
}

QVector<unsigned int> createPlaneIndexData( const QSize &resolution )
{
// Create the index data. 2 triangles per rectangular face
const int faces = 2 * ( resolution.width() - 1 ) * ( resolution.height() - 1 );
const int indices = 3 * faces;
Q_ASSERT( indices < std::numeric_limits<quint16>::max() );
QVector<unsigned int> indexes;

// Iterate over z
for ( int j = 0; j < resolution.height() - 1; ++j )
{
const int rowStartIndex = j * resolution.width();
const int nextRowStartIndex = ( j + 1 ) * resolution.width();

// Iterate over x
for ( int i = 0; i < resolution.width() - 1; ++i )
{
// Split quad into two triangles
indexes.push_back( rowStartIndex + i );
indexes.push_back( nextRowStartIndex + i );
indexes.push_back( rowStartIndex + i + 1 );

indexes.push_back( nextRowStartIndex + i );
indexes.push_back( nextRowStartIndex + i + 1 );
indexes.push_back( rowStartIndex + i + 1 );
}
}

return indexes;
}



Qgs3DSceneExporter::Qgs3DSceneExporter( Qt3DCore::QNode *parent )
: Qt3DCore::QEntity( parent )
, mSmoothEdges( false )
Expand Down Expand Up @@ -360,37 +255,41 @@ void Qgs3DSceneExporter::parseFlatTile( QgsTerrainTileEntity *tileEntity, QgsTer
return;
}

tileGeometry->updateVertices();
tileGeometry->updateIndices();

Qt3DRender::QAttribute *positionAttribute = tileGeometry->positionAttribute();
Qt3DRender::QAttribute *indexAttribute = tileGeometry->indexAttribute();

qDebug() << "Position attribute data size: " << positionAttribute->buffer()->data().size();
qDebug() << "Index attribute data size: " << indexAttribute->buffer()->data().size();

float scale = transform->scale();
QVector3D translation = transform->translation();

QVector<float> positionBuffer = createPlaneVertexData( scale, scale, tileGeometry->resolution() );
QVector<unsigned int> indexesBuffer = createPlaneIndexData( tileGeometry->resolution() );
// Generate vertice data
Qt3DRender::QAttribute *positionAttribute = tileGeometry->positionAttribute();
Qt3DRender::QBufferDataGeneratorPtr positionDataGenerator = positionAttribute->buffer()->dataGenerator();
QByteArray verticesBytes = positionDataGenerator->operator()();
QVector<float> positionBuffer = getAttributeData<float>( positionAttribute, verticesBytes );

// Generate index data
Qt3DRender::QAttribute *indexAttribute = tileGeometry->indexAttribute();
Qt3DRender::QBufferDataGeneratorPtr indexDataGenerator = indexAttribute->buffer()->dataGenerator();
QByteArray indexBytes = indexDataGenerator->operator()();
QVector<quint16> indexesBuffer = getIndexData<quint16>( indexBytes );

Qgs3DExportObject *object = new Qgs3DExportObject( getObjectName( "Flat_tile" ), "", this );
mObjects.push_back( object );

object->setSmoothEdges( mSmoothEdges );
object->setupPositionCoordinates( positionBuffer, indexesBuffer, 1.0f, translation );
object->setupPositionCoordinates( positionBuffer, indexesBuffer, scale, translation );

if ( mExportNormals )
{
// Evert
QVector<float> normalsBuffer;
for ( int i = 0; i < positionBuffer.size(); i += 3 ) normalsBuffer << 0.0f << 1.0f << 0.0f;
object->setupNormalCoordinates( normalsBuffer );
}

if ( mExportTextures )
{
QVector<float> texCoords = createPlaneTexCoordsData( 1.0f, 1.0f, tileGeometry->resolution(), false );
// Reuse vertex buffer data for texture coordinates
Qt3DRender::QAttribute *texCoordsAttribute = tileGeometry->texCoordAttribute();
QVector<float> texCoords = getAttributeData<float>( texCoordsAttribute, verticesBytes );

object->setupTextureCoordinates( texCoords );

QImage img = textureGenerator->renderSynchronously( tileEntity->textureImage()->imageExtent(), tileEntity->textureImage()->imageDebugText() );
Expand Down Expand Up @@ -420,7 +319,7 @@ void Qgs3DSceneExporter::parseDemTile( QgsTerrainTileEntity *tileEntity, QgsTerr

Qt3DRender::QAttribute *indexAttribute = tileGeometry->indexAttribute();
QByteArray indexBytes = indexAttribute->buffer()->data();
QVector<unsigned int> indexBuffer = getIndexData( indexBytes );
QVector<unsigned int> indexBuffer = getIndexData<uint>( indexBytes );

Qgs3DExportObject *object = new Qgs3DExportObject( getObjectName( "DEM_tile" ), "", this );
mObjects.push_back( object );
Expand Down

0 comments on commit 29c980e

Please sign in to comment.