diff options
author | Sean Harmer <sean.harmer@kdab.com> | 2017-09-10 12:17:32 +0100 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2017-10-03 10:38:04 +0000 |
commit | 054ecec467076dbe4e82db27dbc66a5876d1b199 (patch) | |
tree | 6a277749a173491651a661f27e91d9f70630f42c /tests/manual | |
parent | 4f2ff902545842bc715a936ff0c70b3ca3ce0cc9 (diff) |
Share the effect from the phong material
This only loads the shader source once instead of 1000 times. Reduces
the time to first frame by a further 30% by massively reducing the
work the main thread has to do at startup. The Qt3DExtras material
classes are convenient but lead to bad practices like this. We should
provide a way to allow them to share the effect easily but still use
the provided materials for their API. Perhaps a new ctor overload that
takes a QEffect pointer would work nicely.
Also fixed some float vs double vs int precision warnings as a drive
by.
Change-Id: I181de8d95841eb88c91c29ef6f45d215da079b98
Reviewed-by: Paul Lemire <paul.lemire@kdab.com>
Diffstat (limited to 'tests/manual')
-rw-r--r-- | tests/manual/bigscene-cpp/entity.cpp | 20 | ||||
-rw-r--r-- | tests/manual/bigscene-cpp/entity.h | 13 | ||||
-rw-r--r-- | tests/manual/bigscene-cpp/main.cpp | 20 |
3 files changed, 32 insertions, 21 deletions
diff --git a/tests/manual/bigscene-cpp/entity.cpp b/tests/manual/bigscene-cpp/entity.cpp index 6cd969777..876a2d171 100644 --- a/tests/manual/bigscene-cpp/entity.cpp +++ b/tests/manual/bigscene-cpp/entity.cpp @@ -51,15 +51,21 @@ #include "entity.h" #include <Qt3DExtras/QCylinderMesh> -#include <Qt3DExtras/QPhongMaterial> +#include <Qt3DRender/QMaterial> +#include <Qt3DRender/QParameter> #include <Qt3DCore/QTransform> #include <QMatrix4x4> -Entity::Entity(Qt3DCore::QNode *parent) +Entity::Entity(Qt3DRender::QEffect *effect, Qt3DCore::QNode *parent) : QEntity(parent) , m_transform(new Qt3DCore::QTransform()) - , m_material(new Qt3DExtras::QPhongMaterial()) + , m_material(new Qt3DRender::QMaterial()) + , m_diffuseColorParam(new Qt3DRender::QParameter()) { + m_diffuseColorParam->setName(QLatin1String("kd")); + m_material->addParameter(m_diffuseColorParam); + m_material->setEffect(effect); + addComponent(m_transform); addComponent(m_material); } @@ -90,7 +96,7 @@ QVector3D Entity::position() const QColor Entity::diffuseColor() const { - return m_material->diffuse(); + return m_diffuseColorParam->value().value<QColor>(); } void Entity::setTheta(float theta) @@ -129,9 +135,11 @@ void Entity::setPosition(QVector3D position) void Entity::setDiffuseColor(QColor diffuseColor) { - if (m_material->diffuse() == diffuseColor) + if (m_diffuseColorParam->value().value<QColor>() == diffuseColor) return; - m_material->setDiffuse(diffuseColor); + m_diffuseColorParam->setValue(QVariant::fromValue(diffuseColor)); + const bool wasBlocked = blockNotifications(true); emit diffuseColorChanged(diffuseColor); + blockNotifications(wasBlocked); } diff --git a/tests/manual/bigscene-cpp/entity.h b/tests/manual/bigscene-cpp/entity.h index 6c2310a35..656511cdc 100644 --- a/tests/manual/bigscene-cpp/entity.h +++ b/tests/manual/bigscene-cpp/entity.h @@ -62,12 +62,10 @@ class QTransform; } namespace Qt3DRender { +class QEffect; class QGeometryRenderer; -} - -namespace Qt3DExtras { -class QCylinderMesh; -class QPhongMaterial; +class QMaterial; +class QParameter; } QT_END_NAMESPACE @@ -81,7 +79,7 @@ class Entity : public Qt3DCore::QEntity Q_PROPERTY(QColor diffuseColor READ diffuseColor WRITE setDiffuseColor NOTIFY diffuseColorChanged) public: - Entity(Qt3DCore::QNode *parent = 0); + Entity(Qt3DRender::QEffect *effect, Qt3DCore::QNode *parent = 0); float theta() const; float phi() const; @@ -105,7 +103,8 @@ private: private: Qt3DCore::QTransform *m_transform; - Qt3DExtras::QPhongMaterial *m_material; + Qt3DRender::QMaterial *m_material; + Qt3DRender::QParameter *m_diffuseColorParam; float m_theta; float m_phi; QVector3D m_position; diff --git a/tests/manual/bigscene-cpp/main.cpp b/tests/manual/bigscene-cpp/main.cpp index cf893e051..ef296e04d 100644 --- a/tests/manual/bigscene-cpp/main.cpp +++ b/tests/manual/bigscene-cpp/main.cpp @@ -88,6 +88,10 @@ int main(int ac, char **av) mesh->setRadius(2.5f); mesh->setLength(5.0f); + // Material + auto phongMaterial = new Qt3DExtras::QPhongMaterial(root); + auto effect = phongMaterial->effect(); + // Camera QCamera *cameraEntity = view.camera(); cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); @@ -99,18 +103,20 @@ int main(int ac, char **av) Qt3DExtras::QFirstPersonCameraController *camController = new Qt3DExtras::QFirstPersonCameraController(root); camController->setCamera(cameraEntity); - const float radius = 100.0f; + const double radius = 100.0; const int max = 1000; - const float det = 1.0f / max; + const double det = 1.0 / max; // Scene for (int i = 0; i < max; i++) { - Entity *e = new Entity(); + Entity *e = new Entity(effect, root); e->addComponent(mesh); - const float angle = M_PI * 2.0f * i * det * 10.; + const double angle = M_PI * 2.0 * double(i) * det * 10.; - e->setDiffuseColor(QColor(qFabs(qCos(angle)) * 255, 204, 75)); - e->setPosition(QVector3D(radius * qCos(angle), -200.* i * det, radius * qSin(angle))); + e->setDiffuseColor(QColor(int(qFabs(qCos(angle)) * 255.0), 204, 75)); + e->setPosition(QVector3D(float(radius * qCos(angle)), + float(-200.0 * i * det), + float(radius * qSin(angle)))); e->setTheta(30.0f * i); e->setPhi(45.0f * i); @@ -127,8 +133,6 @@ int main(int ac, char **av) animZ->setEndValue(QVariant::fromValue((i + 1) * 380.0f)); animZ->setLoopCount(-1); animZ->start(); - - e->setParent(root); } view.setRootEntity(root); |