Skip to content
Permalink
Browse files

Deleted createPlane*Data functions

Switched to buffer data generator
  • Loading branch information
NEDJIMAbelgacem committed Jul 10, 2020
1 parent fcdd64d commit 29c980ea519f3940ab94f714d069256d30f992ce
Showing with 54 additions and 132 deletions.
  1. +21 −4 src/3d/qgs3dexportobject.cpp
  2. +7 −0 src/3d/qgs3dexportobject.h
  3. +0 −1 src/3d/qgs3dmapscene.cpp
  4. +26 −127 src/3d/qgs3dsceneexporter.cpp
@@ -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;
@@ -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
@@ -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
*/
@@ -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:
@@ -30,6 +30,8 @@
#include <Qt3DExtras/QTextureMaterial>
#include <Qt3DRender/QTextureImage>
#include <Qt3DRender/QTexture>
#include <Qt3DRender/QBufferDataGenerator>
#include <Qt3DRender/QBufferDataGeneratorPtr>

#include <QByteArray>
#include <QFile>
@@ -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 );
}
@@ -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 )
@@ -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() );
@@ -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 );

0 comments on commit 29c980e

Please sign in to comment.
You can’t perform that action at this time.