Skip to content
Permalink
Browse files

Base class for 3D renderers + a bit of code shuffling

  • Loading branch information
wonder-sk committed Jul 24, 2017
1 parent 3d9d455 commit 76f4f69c2111d47ebc028eb150575d6745e77510
@@ -2,9 +2,11 @@
# sources

SET(QGIS_3D_SRCS
abstract3drenderer.cpp
cameracontroller.cpp
lineentity.cpp
map3d.cpp
phongmaterialsettings.cpp
pointentity.cpp
polygonentity.cpp
polygongeometry.cpp
@@ -60,9 +62,11 @@ QT5_ADD_RESOURCES(QGIS_3D_RCC_SRCS shaders.qrc)

SET(QGIS_3D_HDRS
aabb.h
abstract3drenderer.h
cameracontroller.h
lineentity.h
map3d.h
phongmaterialsettings.h
pointentity.h
polygonentity.h
polygongeometry.h
@@ -0,0 +1,213 @@
#include "abstract3drenderer.h"

#include "lineentity.h"
#include "pointentity.h"
#include "polygonentity.h"

#include "qgsvectorlayer.h"
#include "qgsxmlutils.h"


// ---------------

PolygonRenderer::PolygonRenderer()
: altClamping( AltClampRelative )
, altBinding( AltBindCentroid )
, height( 0 )
, extrusionHeight( 0 )
{
}

void PolygonRenderer::setLayer( QgsVectorLayer *layer )
{
layerRef = QgsMapLayerRef( layer );
}

QgsVectorLayer *PolygonRenderer::layer() const
{
return qobject_cast<QgsVectorLayer *>( layerRef.layer );
}

Abstract3DRenderer *PolygonRenderer::clone() const
{
return new PolygonRenderer( *this );
}

Qt3DCore::QEntity *PolygonRenderer::createEntity( const Map3D &map ) const
{
return new PolygonEntity( map, *this );
}

void PolygonRenderer::writeXml( QDomElement &elem ) const
{
QDomDocument doc = elem.ownerDocument();

QDomElement elemDataProperties = doc.createElement( "data" );
elemDataProperties.setAttribute( "layer", layerRef.layerId );
elemDataProperties.setAttribute( "alt-clamping", Utils::altClampingToString( altClamping ) );
elemDataProperties.setAttribute( "alt-binding", Utils::altBindingToString( altBinding ) );
elemDataProperties.setAttribute( "height", height );
elemDataProperties.setAttribute( "extrusion-height", extrusionHeight );
elem.appendChild( elemDataProperties );

QDomElement elemMaterial = doc.createElement( "material" );
material.writeXml( elemMaterial );
elem.appendChild( elemMaterial );
}

void PolygonRenderer::readXml( const QDomElement &elem )
{
QDomElement elemDataProperties = elem.firstChildElement( "data" );
layerRef = QgsMapLayerRef( elemDataProperties.attribute( "layer" ) );
altClamping = Utils::altClampingFromString( elemDataProperties.attribute( "alt-clamping" ) );
altBinding = Utils::altBindingFromString( elemDataProperties.attribute( "alt-binding" ) );
height = elemDataProperties.attribute( "height" ).toFloat();
extrusionHeight = elemDataProperties.attribute( "extrusion-height" ).toFloat();

QDomElement elemMaterial = elem.firstChildElement( "material" );
material.readXml( elemMaterial );
}

void PolygonRenderer::resolveReferences( const QgsProject &project )
{
layerRef.setLayer( project.mapLayer( layerRef.layerId ) );
}

// ---------------

PointRenderer::PointRenderer()
: height( 0 )
{
}

void PointRenderer::setLayer( QgsVectorLayer *layer )
{
layerRef = QgsMapLayerRef( layer );
}

QgsVectorLayer *PointRenderer::layer() const
{
return qobject_cast<QgsVectorLayer *>( layerRef.layer );
}

Abstract3DRenderer *PointRenderer::clone() const
{
return new PointRenderer( *this );
}

Qt3DCore::QEntity *PointRenderer::createEntity( const Map3D &map ) const
{
return new PointEntity( map, *this );
}

void PointRenderer::writeXml( QDomElement &elem ) const
{
QDomDocument doc = elem.ownerDocument();

QDomElement elemDataProperties = doc.createElement( "data" );
elemDataProperties.setAttribute( "layer", layerRef.layerId );
elemDataProperties.setAttribute( "height", height );
elem.appendChild( elemDataProperties );

QDomElement elemMaterial = doc.createElement( "material" );
material.writeXml( elemMaterial );
elem.appendChild( elemMaterial );

QDomElement elemShapeProperties = doc.createElement( "shape-properties" );
elemShapeProperties.appendChild( QgsXmlUtils::writeVariant( shapeProperties, doc ) );
elem.appendChild( elemShapeProperties );

QDomElement elemTransform = doc.createElement( "transform" );
elemTransform.setAttribute( "matrix", Utils::matrix4x4toString( transform ) );
elem.appendChild( elemTransform );
}

void PointRenderer::readXml( const QDomElement &elem )
{
QDomElement elemDataProperties = elem.firstChildElement( "data" );
layerRef = QgsMapLayerRef( elemDataProperties.attribute( "layer" ) );
height = elemDataProperties.attribute( "height" ).toFloat();

QDomElement elemMaterial = elem.firstChildElement( "material" );
material.readXml( elemMaterial );

QDomElement elemShapeProperties = elem.firstChildElement( "shape-properties" );
shapeProperties = QgsXmlUtils::readVariant( elemShapeProperties.firstChildElement() ).toMap();

QDomElement elemTransform = elem.firstChildElement( "transform" );
transform = Utils::stringToMatrix4x4( elemTransform.attribute( "matrix" ) );
}

void PointRenderer::resolveReferences( const QgsProject &project )
{
layerRef.setLayer( project.mapLayer( layerRef.layerId ) );
}

// ---------------

LineRenderer::LineRenderer()
: altClamping( AltClampRelative )
, altBinding( AltBindCentroid )
, height( 0 )
, extrusionHeight( 0 )
, distance( 1 )
{

}

void LineRenderer::setLayer( QgsVectorLayer *layer )
{
layerRef = QgsMapLayerRef( layer );
}

QgsVectorLayer *LineRenderer::layer() const
{
return qobject_cast<QgsVectorLayer *>( layerRef.layer );
}

Abstract3DRenderer *LineRenderer::clone() const
{
return new LineRenderer( *this );
}

Qt3DCore::QEntity *LineRenderer::createEntity( const Map3D &map ) const
{
return new LineEntity( map, *this );
}

void LineRenderer::writeXml( QDomElement &elem ) const
{
QDomDocument doc = elem.ownerDocument();

QDomElement elemDataProperties = doc.createElement( "data" );
elemDataProperties.setAttribute( "layer", layerRef.layerId );
elemDataProperties.setAttribute( "alt-clamping", Utils::altClampingToString( altClamping ) );
elemDataProperties.setAttribute( "alt-binding", Utils::altBindingToString( altBinding ) );
elemDataProperties.setAttribute( "height", height );
elemDataProperties.setAttribute( "extrusion-height", extrusionHeight );
elemDataProperties.setAttribute( "distance", distance );
elem.appendChild( elemDataProperties );

QDomElement elemMaterial = doc.createElement( "material" );
material.writeXml( elemMaterial );
elem.appendChild( elemMaterial );
}

void LineRenderer::readXml( const QDomElement &elem )
{
QDomElement elemDataProperties = elem.firstChildElement( "data" );
layerRef = QgsMapLayerRef( elemDataProperties.attribute( "layer" ) );
altClamping = Utils::altClampingFromString( elemDataProperties.attribute( "alt-clamping" ) );
altBinding = Utils::altBindingFromString( elemDataProperties.attribute( "alt-binding" ) );
height = elemDataProperties.attribute( "height" ).toFloat();
extrusionHeight = elemDataProperties.attribute( "extrusion-height" ).toFloat();
distance = elemDataProperties.attribute( "distance" ).toFloat();

QDomElement elemMaterial = elem.firstChildElement( "material" );
material.readXml( elemMaterial );
}

void LineRenderer::resolveReferences( const QgsProject &project )
{
layerRef.setLayer( project.mapLayer( layerRef.layerId ) );
}
@@ -0,0 +1,120 @@
#ifndef ABSTRACT3DRENDERER_H
#define ABSTRACT3DRENDERER_H

#include "qgis_3d.h"

#include "phongmaterialsettings.h"
#include "utils.h"

#include "qgsmaplayerref.h"

#include <QObject>

class QgsVectorLayer;

class Map3D;


namespace Qt3DCore
{
class QEntity;
}

class Abstract3DRenderer //: public QObject
{
//Q_OBJECT
public:
virtual ~Abstract3DRenderer() {}

virtual QString type() const = 0;
virtual Abstract3DRenderer *clone() const = 0;
virtual Qt3DCore::QEntity *createEntity( const Map3D &map ) const = 0;

virtual void writeXml( QDomElement &elem ) const = 0;
virtual void readXml( const QDomElement &elem ) = 0;
virtual void resolveReferences( const QgsProject &project ) { Q_UNUSED( project ); }
};


class _3D_EXPORT PolygonRenderer : public Abstract3DRenderer
{
public:
PolygonRenderer();

void setLayer( QgsVectorLayer *layer );
QgsVectorLayer *layer() const;

QString type() const override { return "polygon"; }
Abstract3DRenderer *clone() const override;
Qt3DCore::QEntity *createEntity( const Map3D &map ) const override;

void writeXml( QDomElement &elem ) const override;
void readXml( const QDomElement &elem ) override;
void resolveReferences( const QgsProject &project ) override;

AltitudeClamping altClamping; //! how to handle altitude of vector features
AltitudeBinding altBinding; //! how to handle clamping of vertices of individual features

float height; //!< Base height of polygons
float extrusionHeight; //!< How much to extrude (0 means no walls)
PhongMaterialSettings material; //!< Defines appearance of objects

private:
QgsMapLayerRef layerRef; //!< Layer used to extract polygons from
};

class _3D_EXPORT PointRenderer : public Abstract3DRenderer
{
public:
PointRenderer();

void setLayer( QgsVectorLayer *layer );
QgsVectorLayer *layer() const;

QString type() const override { return "point"; }
Abstract3DRenderer *clone() const override;
Qt3DCore::QEntity *createEntity( const Map3D &map ) const override;

void writeXml( QDomElement &elem ) const override;
void readXml( const QDomElement &elem ) override;
void resolveReferences( const QgsProject &project ) override;

float height;
PhongMaterialSettings material; //!< Defines appearance of objects
QVariantMap shapeProperties; //!< What kind of shape to use and what
QMatrix4x4 transform; //!< Transform of individual instanced models

private:
QgsMapLayerRef layerRef; //!< Layer used to extract points from
};

class _3D_EXPORT LineRenderer : public Abstract3DRenderer
{
public:
LineRenderer();

void setLayer( QgsVectorLayer *layer );
QgsVectorLayer *layer() const;

QString type() const override { return "line"; }
Abstract3DRenderer *clone() const override;
Qt3DCore::QEntity *createEntity( const Map3D &map ) const override;

void writeXml( QDomElement &elem ) const override;
void readXml( const QDomElement &elem ) override;
void resolveReferences( const QgsProject &project ) override;

AltitudeClamping altClamping; //! how to handle altitude of vector features
AltitudeBinding altBinding; //! how to handle clamping of vertices of individual features

float height; //!< Base height of polygons
float extrusionHeight; //!< How much to extrude (0 means no walls)
PhongMaterialSettings material; //!< Defines appearance of objects

float distance; //!< Distance of buffer of lines

private:
QgsMapLayerRef layerRef; //!< Layer used to extract points from
};

#endif // ABSTRACT3DRENDERER_H
@@ -1,5 +1,6 @@
#include "lineentity.h"

#include "abstract3drenderer.h"
#include "polygongeometry.h"
#include "map3d.h"
#include "terraingenerator.h"

0 comments on commit 76f4f69

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