mirror of
https://github.com/overte-org/overte.git
synced 2025-04-18 00:26:33 +02:00
Merge branch 'master' of https://github.com/worklist/hifi into octree_server_scaling
Conflicts: libraries/shared/src/ResourceCache.cpp
This commit is contained in:
commit
ef87fbffac
17 changed files with 267 additions and 79 deletions
|
@ -27,6 +27,9 @@ Agent::Agent(const QByteArray& packet) :
|
|||
_voxelEditSender(),
|
||||
_particleEditSender()
|
||||
{
|
||||
// be the parent of the script engine so it gets moved when we do
|
||||
_scriptEngine.setParent(this);
|
||||
|
||||
_scriptEngine.getVoxelsScriptingInterface()->setPacketSender(&_voxelEditSender);
|
||||
_scriptEngine.getParticlesScriptingInterface()->setPacketSender(&_particleEditSender);
|
||||
}
|
||||
|
|
|
@ -309,6 +309,9 @@ void Avatar::renderBody() {
|
|||
renderBillboard();
|
||||
return;
|
||||
}
|
||||
if (!(_skeletonModel.isRenderable() && getHead()->getFaceModel().isRenderable())) {
|
||||
return; // wait until both models are loaded
|
||||
}
|
||||
_skeletonModel.render(1.0f);
|
||||
getHead()->render(1.0f);
|
||||
getHand()->render(false);
|
||||
|
@ -564,13 +567,13 @@ bool Avatar::findParticleCollisions(const glm::vec3& particleCenter, float parti
|
|||
void Avatar::setFaceModelURL(const QUrl& faceModelURL) {
|
||||
AvatarData::setFaceModelURL(faceModelURL);
|
||||
const QUrl DEFAULT_FACE_MODEL_URL = QUrl::fromLocalFile("resources/meshes/defaultAvatar_head.fst");
|
||||
getHead()->getFaceModel().setURL(_faceModelURL, DEFAULT_FACE_MODEL_URL, !isMyAvatar());
|
||||
getHead()->getFaceModel().setURL(_faceModelURL, DEFAULT_FACE_MODEL_URL, true, !isMyAvatar());
|
||||
}
|
||||
|
||||
void Avatar::setSkeletonModelURL(const QUrl& skeletonModelURL) {
|
||||
AvatarData::setSkeletonModelURL(skeletonModelURL);
|
||||
const QUrl DEFAULT_SKELETON_MODEL_URL = QUrl::fromLocalFile("resources/meshes/defaultAvatar_body.fst");
|
||||
_skeletonModel.setURL(_skeletonModelURL, DEFAULT_SKELETON_MODEL_URL, !isMyAvatar());
|
||||
_skeletonModel.setURL(_skeletonModelURL, DEFAULT_SKELETON_MODEL_URL, true, !isMyAvatar());
|
||||
}
|
||||
|
||||
void Avatar::setDisplayName(const QString& displayName) {
|
||||
|
|
|
@ -677,6 +677,10 @@ void MyAvatar::setSkeletonModelURL(const QUrl& skeletonModelURL) {
|
|||
}
|
||||
|
||||
void MyAvatar::renderBody(bool forceRenderHead) {
|
||||
if (!(_skeletonModel.isRenderable() && getHead()->getFaceModel().isRenderable())) {
|
||||
return; // wait until both models are loaded
|
||||
}
|
||||
|
||||
// Render the body's voxels and head
|
||||
_skeletonModel.render(1.0f);
|
||||
|
||||
|
|
|
@ -28,6 +28,8 @@
|
|||
|
||||
using namespace std;
|
||||
|
||||
static int fbxGeometryMetaTypeId = qRegisterMetaType<FBXGeometry>();
|
||||
|
||||
template<class T> QVariant readBinaryArray(QDataStream& in) {
|
||||
quint32 arrayLength;
|
||||
quint32 encoding;
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#ifndef __interface__FBXReader__
|
||||
#define __interface__FBXReader__
|
||||
|
||||
#include <QMetaType>
|
||||
#include <QUrl>
|
||||
#include <QVarLengthArray>
|
||||
#include <QVariant>
|
||||
|
@ -167,6 +168,8 @@ public:
|
|||
QVector<FBXAttachment> attachments;
|
||||
};
|
||||
|
||||
Q_DECLARE_METATYPE(FBXGeometry)
|
||||
|
||||
/// Reads an FST mapping from the supplied data.
|
||||
QVariantHash readMapping(const QByteArray& data);
|
||||
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
#include <cmath>
|
||||
|
||||
#include <QNetworkReply>
|
||||
#include <QRunnable>
|
||||
#include <QThreadPool>
|
||||
|
||||
#include "Application.h"
|
||||
#include "GeometryCache.h"
|
||||
|
@ -307,6 +309,21 @@ NetworkGeometry::NetworkGeometry(const QUrl& url, const QSharedPointer<NetworkGe
|
|||
_fallback(fallback) {
|
||||
}
|
||||
|
||||
bool NetworkGeometry::isLoadedWithTextures() const {
|
||||
if (!isLoaded()) {
|
||||
return false;
|
||||
}
|
||||
foreach (const NetworkMesh& mesh, _meshes) {
|
||||
foreach (const NetworkMeshPart& part, mesh.parts) {
|
||||
if ((part.diffuseTexture && !part.diffuseTexture->isLoaded()) ||
|
||||
(part.normalTexture && !part.normalTexture->isLoaded())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
QSharedPointer<NetworkGeometry> NetworkGeometry::getLODOrFallback(float distance, float& hysteresis, bool delayLoad) const {
|
||||
if (_lodParent.data() != this) {
|
||||
return _lodParent.data()->getLODOrFallback(distance, hysteresis, delayLoad);
|
||||
|
@ -428,6 +445,46 @@ void NetworkGeometry::clearLoadPriority(const QPointer<QObject>& owner) {
|
|||
}
|
||||
}
|
||||
|
||||
/// Reads geometry in a worker thread.
|
||||
class GeometryReader : public QRunnable {
|
||||
public:
|
||||
|
||||
GeometryReader(const QWeakPointer<Resource>& geometry, const QUrl& url,
|
||||
const QByteArray& data, const QVariantHash& mapping);
|
||||
|
||||
virtual void run();
|
||||
|
||||
private:
|
||||
|
||||
QWeakPointer<Resource> _geometry;
|
||||
QUrl _url;
|
||||
QByteArray _data;
|
||||
QVariantHash _mapping;
|
||||
};
|
||||
|
||||
GeometryReader::GeometryReader(const QWeakPointer<Resource>& geometry, const QUrl& url,
|
||||
const QByteArray& data, const QVariantHash& mapping) :
|
||||
_geometry(geometry),
|
||||
_url(url),
|
||||
_data(data),
|
||||
_mapping(mapping) {
|
||||
}
|
||||
|
||||
void GeometryReader::run() {
|
||||
QSharedPointer<Resource> geometry = _geometry.toStrongRef();
|
||||
if (geometry.isNull()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
QMetaObject::invokeMethod(geometry.data(), "setGeometry", Q_ARG(const FBXGeometry&,
|
||||
_url.path().toLower().endsWith(".svo") ? readSVO(_data) : readFBX(_data, _mapping)));
|
||||
|
||||
} catch (const QString& error) {
|
||||
qDebug() << "Error reading " << _url << ": " << error;
|
||||
QMetaObject::invokeMethod(geometry.data(), "finishedLoading", Q_ARG(bool, false));
|
||||
}
|
||||
}
|
||||
|
||||
void NetworkGeometry::downloadFinished(QNetworkReply* reply) {
|
||||
QUrl url = reply->url();
|
||||
QByteArray data = reply->readAll();
|
||||
|
@ -438,7 +495,7 @@ void NetworkGeometry::downloadFinished(QNetworkReply* reply) {
|
|||
QString filename = _mapping.value("filename").toString();
|
||||
if (filename.isNull()) {
|
||||
qDebug() << "Mapping file " << url << " has no filename.";
|
||||
_failedToLoad = true;
|
||||
finishedLoading(false);
|
||||
|
||||
} else {
|
||||
QString texdir = _mapping.value("texdir").toString();
|
||||
|
@ -452,6 +509,7 @@ void NetworkGeometry::downloadFinished(QNetworkReply* reply) {
|
|||
for (QVariantHash::const_iterator it = lods.begin(); it != lods.end(); it++) {
|
||||
QSharedPointer<NetworkGeometry> geometry(new NetworkGeometry(url.resolved(it.key()),
|
||||
QSharedPointer<NetworkGeometry>(), true, _mapping, _textureBase));
|
||||
geometry->setSelf(geometry.staticCast<Resource>());
|
||||
geometry->setLODParent(_lodParent);
|
||||
_lods.insert(it.value().toFloat(), geometry);
|
||||
}
|
||||
|
@ -466,14 +524,12 @@ void NetworkGeometry::downloadFinished(QNetworkReply* reply) {
|
|||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
_geometry = url.path().toLower().endsWith(".svo") ? readSVO(data) : readFBX(data, _mapping);
|
||||
|
||||
} catch (const QString& error) {
|
||||
qDebug() << "Error reading " << url << ": " << error;
|
||||
_failedToLoad = true;
|
||||
return;
|
||||
}
|
||||
// send the reader off to the thread pool
|
||||
QThreadPool::globalInstance()->start(new GeometryReader(_self, url, data, _mapping));
|
||||
}
|
||||
|
||||
void NetworkGeometry::setGeometry(const FBXGeometry& geometry) {
|
||||
_geometry = geometry;
|
||||
|
||||
foreach (const FBXMesh& mesh, _geometry.meshes) {
|
||||
NetworkMesh networkMesh = { QOpenGLBuffer(QOpenGLBuffer::IndexBuffer), QOpenGLBuffer(QOpenGLBuffer::VertexBuffer) };
|
||||
|
@ -567,6 +623,8 @@ void NetworkGeometry::downloadFinished(QNetworkReply* reply) {
|
|||
|
||||
_meshes.append(networkMesh);
|
||||
}
|
||||
|
||||
finishedLoading(true);
|
||||
}
|
||||
|
||||
bool NetworkMeshPart::isTranslucent() const {
|
||||
|
|
|
@ -69,8 +69,8 @@ public:
|
|||
NetworkGeometry(const QUrl& url, const QSharedPointer<NetworkGeometry>& fallback, bool delayLoad,
|
||||
const QVariantHash& mapping = QVariantHash(), const QUrl& textureBase = QUrl());
|
||||
|
||||
/// Checks whether the geometry is fulled loaded.
|
||||
bool isLoaded() const { return !_geometry.joints.isEmpty(); }
|
||||
/// Checks whether the geometry and its textures are loaded.
|
||||
bool isLoadedWithTextures() const;
|
||||
|
||||
/// Returns a pointer to the geometry appropriate for the specified distance.
|
||||
/// \param hysteresis a hysteresis parameter that prevents rapid model switching
|
||||
|
@ -90,6 +90,8 @@ protected:
|
|||
|
||||
virtual void downloadFinished(QNetworkReply* reply);
|
||||
|
||||
Q_INVOKABLE void setGeometry(const FBXGeometry& geometry);
|
||||
|
||||
private:
|
||||
|
||||
friend class GeometryCache;
|
||||
|
|
|
@ -57,21 +57,6 @@ QVector<Model::JointState> Model::createJointStates(const FBXGeometry& geometry)
|
|||
return jointStates;
|
||||
}
|
||||
|
||||
bool Model::isLoadedWithTextures() const {
|
||||
if (!isActive()) {
|
||||
return false;
|
||||
}
|
||||
foreach (const NetworkMesh& mesh, _geometry->getMeshes()) {
|
||||
foreach (const NetworkMeshPart& part, mesh.parts) {
|
||||
if ((part.diffuseTexture && !part.diffuseTexture->isLoaded()) ||
|
||||
(part.normalTexture && !part.normalTexture->isLoaded())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void Model::init() {
|
||||
if (!_program.isLinked()) {
|
||||
switchToResourcesParentIfRequired();
|
||||
|
@ -117,32 +102,7 @@ void Model::reset() {
|
|||
|
||||
void Model::simulate(float deltaTime, bool delayLoad) {
|
||||
// update our LOD
|
||||
QVector<JointState> newJointStates;
|
||||
if (_geometry) {
|
||||
QSharedPointer<NetworkGeometry> geometry = _geometry->getLODOrFallback(_lodDistance, _lodHysteresis, delayLoad);
|
||||
if (_geometry != geometry) {
|
||||
if (!_jointStates.isEmpty()) {
|
||||
// copy the existing joint states
|
||||
const FBXGeometry& oldGeometry = _geometry->getFBXGeometry();
|
||||
const FBXGeometry& newGeometry = geometry->getFBXGeometry();
|
||||
newJointStates = createJointStates(newGeometry);
|
||||
for (QHash<QString, int>::const_iterator it = oldGeometry.jointIndices.constBegin();
|
||||
it != oldGeometry.jointIndices.constEnd(); it++) {
|
||||
int newIndex = newGeometry.jointIndices.value(it.key());
|
||||
if (newIndex != 0) {
|
||||
newJointStates[newIndex - 1] = _jointStates.at(it.value() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
deleteGeometry();
|
||||
_dilatedTextures.clear();
|
||||
_geometry = geometry;
|
||||
}
|
||||
if (!delayLoad) {
|
||||
_geometry->setLoadPriority(this, -_lodDistance);
|
||||
_geometry->ensureLoading();
|
||||
}
|
||||
}
|
||||
QVector<JointState> newJointStates = updateGeometry(delayLoad);
|
||||
if (!isActive()) {
|
||||
return;
|
||||
}
|
||||
|
@ -447,20 +407,18 @@ float Model::getRightArmLength() const {
|
|||
return getLimbLength(getRightHandJointIndex());
|
||||
}
|
||||
|
||||
void Model::setURL(const QUrl& url, const QUrl& fallback, bool delayLoad) {
|
||||
void Model::setURL(const QUrl& url, const QUrl& fallback, bool retainCurrent, bool delayLoad) {
|
||||
// don't recreate the geometry if it's the same URL
|
||||
if (_url == url) {
|
||||
return;
|
||||
}
|
||||
_url = url;
|
||||
|
||||
// delete our local geometry and custom textures
|
||||
deleteGeometry();
|
||||
_dilatedTextures.clear();
|
||||
_lodHysteresis = NetworkGeometry::NO_HYSTERESIS;
|
||||
|
||||
// we retain a reference to the base geometry so that its reference count doesn't fall to zero
|
||||
_baseGeometry = _geometry = Application::getInstance()->getGeometryCache()->getGeometry(url, fallback, delayLoad);
|
||||
// if so instructed, keep the current geometry until the new one is loaded
|
||||
_nextBaseGeometry = _nextGeometry = Application::getInstance()->getGeometryCache()->getGeometry(url, fallback, delayLoad);
|
||||
if (!retainCurrent || !isActive() || _nextGeometry->isLoaded()) {
|
||||
applyNextGeometry();
|
||||
}
|
||||
}
|
||||
|
||||
glm::vec4 Model::computeAverageColor() const {
|
||||
|
@ -823,6 +781,61 @@ void Model::applyCollision(CollisionInfo& collision) {
|
|||
}
|
||||
}
|
||||
|
||||
QVector<Model::JointState> Model::updateGeometry(bool delayLoad) {
|
||||
QVector<JointState> newJointStates;
|
||||
if (_nextGeometry) {
|
||||
_nextGeometry = _nextGeometry->getLODOrFallback(_lodDistance, _lodHysteresis, delayLoad);
|
||||
if (!delayLoad) {
|
||||
_nextGeometry->setLoadPriority(this, -_lodDistance);
|
||||
_nextGeometry->ensureLoading();
|
||||
}
|
||||
if (_nextGeometry->isLoaded()) {
|
||||
applyNextGeometry();
|
||||
return newJointStates;
|
||||
}
|
||||
}
|
||||
if (!_geometry) {
|
||||
return newJointStates;
|
||||
}
|
||||
QSharedPointer<NetworkGeometry> geometry = _geometry->getLODOrFallback(_lodDistance, _lodHysteresis, delayLoad);
|
||||
if (_geometry != geometry) {
|
||||
if (!_jointStates.isEmpty()) {
|
||||
// copy the existing joint states
|
||||
const FBXGeometry& oldGeometry = _geometry->getFBXGeometry();
|
||||
const FBXGeometry& newGeometry = geometry->getFBXGeometry();
|
||||
newJointStates = createJointStates(newGeometry);
|
||||
for (QHash<QString, int>::const_iterator it = oldGeometry.jointIndices.constBegin();
|
||||
it != oldGeometry.jointIndices.constEnd(); it++) {
|
||||
int newIndex = newGeometry.jointIndices.value(it.key());
|
||||
if (newIndex != 0) {
|
||||
newJointStates[newIndex - 1] = _jointStates.at(it.value() - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
deleteGeometry();
|
||||
_dilatedTextures.clear();
|
||||
_geometry = geometry;
|
||||
}
|
||||
if (!delayLoad) {
|
||||
_geometry->setLoadPriority(this, -_lodDistance);
|
||||
_geometry->ensureLoading();
|
||||
}
|
||||
return newJointStates;
|
||||
}
|
||||
|
||||
void Model::applyNextGeometry() {
|
||||
// delete our local geometry and custom textures
|
||||
deleteGeometry();
|
||||
_dilatedTextures.clear();
|
||||
_lodHysteresis = NetworkGeometry::NO_HYSTERESIS;
|
||||
|
||||
// we retain a reference to the base geometry so that its reference count doesn't fall to zero
|
||||
_baseGeometry = _nextBaseGeometry;
|
||||
_geometry = _nextGeometry;
|
||||
_nextBaseGeometry.reset();
|
||||
_nextGeometry.reset();
|
||||
}
|
||||
|
||||
void Model::deleteGeometry() {
|
||||
foreach (Model* attachment, _attachments) {
|
||||
delete attachment;
|
||||
|
|
|
@ -46,14 +46,22 @@ public:
|
|||
|
||||
bool isActive() const { return _geometry && _geometry->isLoaded(); }
|
||||
|
||||
bool isLoadedWithTextures() const;
|
||||
bool isRenderable() const { return !_meshStates.isEmpty(); }
|
||||
|
||||
bool isLoadedWithTextures() const { return _geometry && _geometry->isLoadedWithTextures(); }
|
||||
|
||||
void init();
|
||||
void reset();
|
||||
void simulate(float deltaTime, bool delayLoad = false);
|
||||
bool render(float alpha);
|
||||
|
||||
Q_INVOKABLE void setURL(const QUrl& url, const QUrl& fallback = QUrl(), bool delayLoad = false);
|
||||
/// Sets the URL of the model to render.
|
||||
/// \param fallback the URL of a fallback model to render if the requested model fails to load
|
||||
/// \param retainCurrent if true, keep rendering the current model until the new one is loaded
|
||||
/// \param delayLoad if true, don't load the model immediately; wait until actually requested
|
||||
Q_INVOKABLE void setURL(const QUrl& url, const QUrl& fallback = QUrl(),
|
||||
bool retainCurrent = false, bool delayLoad = false);
|
||||
|
||||
const QUrl& getURL() const { return _url; }
|
||||
|
||||
/// Sets the distance parameter used for LOD computations.
|
||||
|
@ -229,10 +237,14 @@ protected:
|
|||
|
||||
private:
|
||||
|
||||
QVector<JointState> updateGeometry(bool delayLoad);
|
||||
void applyNextGeometry();
|
||||
void deleteGeometry();
|
||||
void renderMeshes(float alpha, bool translucent);
|
||||
|
||||
QSharedPointer<NetworkGeometry> _baseGeometry; ///< reference required to prevent collection of base
|
||||
QSharedPointer<NetworkGeometry> _nextBaseGeometry;
|
||||
QSharedPointer<NetworkGeometry> _nextGeometry;
|
||||
float _lodDistance;
|
||||
float _lodHysteresis;
|
||||
|
||||
|
|
|
@ -11,6 +11,8 @@
|
|||
#include <QGLWidget>
|
||||
#include <QNetworkReply>
|
||||
#include <QOpenGLFramebufferObject>
|
||||
#include <QRunnable>
|
||||
#include <QThreadPool>
|
||||
|
||||
#include <glm/gtc/random.hpp>
|
||||
|
||||
|
@ -129,6 +131,7 @@ QSharedPointer<NetworkTexture> TextureCache::getTexture(const QUrl& url, bool no
|
|||
QSharedPointer<NetworkTexture> texture = _dilatableNetworkTextures.value(url);
|
||||
if (texture.isNull()) {
|
||||
texture = QSharedPointer<NetworkTexture>(new DilatableNetworkTexture(url));
|
||||
texture->setSelf(texture);
|
||||
_dilatableNetworkTextures.insert(url, texture);
|
||||
}
|
||||
return texture;
|
||||
|
@ -254,8 +257,7 @@ Texture::~Texture() {
|
|||
NetworkTexture::NetworkTexture(const QUrl& url, bool normalMap) :
|
||||
Resource(url),
|
||||
_averageColor(1.0f, 1.0f, 1.0f, 1.0f),
|
||||
_translucent(false),
|
||||
_loaded(false) {
|
||||
_translucent(false) {
|
||||
|
||||
if (!url.isValid()) {
|
||||
_loaded = true;
|
||||
|
@ -268,10 +270,30 @@ NetworkTexture::NetworkTexture(const QUrl& url, bool normalMap) :
|
|||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
void NetworkTexture::downloadFinished(QNetworkReply* reply) {
|
||||
_loaded = true;
|
||||
class ImageReader : public QRunnable {
|
||||
public:
|
||||
|
||||
ImageReader(const QWeakPointer<Resource>& texture, const QByteArray& data);
|
||||
|
||||
QImage image = QImage::fromData(reply->readAll());
|
||||
virtual void run();
|
||||
|
||||
private:
|
||||
|
||||
QWeakPointer<Resource> _texture;
|
||||
QByteArray _data;
|
||||
};
|
||||
|
||||
ImageReader::ImageReader(const QWeakPointer<Resource>& texture, const QByteArray& data) :
|
||||
_texture(texture),
|
||||
_data(data) {
|
||||
}
|
||||
|
||||
void ImageReader::run() {
|
||||
QSharedPointer<Resource> texture = _texture.toStrongRef();
|
||||
if (texture.isNull()) {
|
||||
return;
|
||||
}
|
||||
QImage image = QImage::fromData(_data);
|
||||
if (image.format() != QImage::Format_ARGB32) {
|
||||
image = image.convertToFormat(QImage::Format_ARGB32);
|
||||
}
|
||||
|
@ -295,9 +317,21 @@ void NetworkTexture::downloadFinished(QNetworkReply* reply) {
|
|||
}
|
||||
}
|
||||
int imageArea = image.width() * image.height();
|
||||
_averageColor = accumulated / (imageArea * EIGHT_BIT_MAXIMUM);
|
||||
_translucent = (translucentPixels >= imageArea / 2);
|
||||
QMetaObject::invokeMethod(texture.data(), "setImage", Q_ARG(const QImage&, image),
|
||||
Q_ARG(const glm::vec4&, accumulated / (imageArea * EIGHT_BIT_MAXIMUM)),
|
||||
Q_ARG(bool, translucentPixels >= imageArea / 2));
|
||||
}
|
||||
|
||||
void NetworkTexture::downloadFinished(QNetworkReply* reply) {
|
||||
// send the reader off to the thread pool
|
||||
QThreadPool::globalInstance()->start(new ImageReader(_self, reply->readAll()));
|
||||
}
|
||||
|
||||
void NetworkTexture::setImage(const QImage& image, const glm::vec4& averageColor, bool translucent) {
|
||||
_averageColor = averageColor;
|
||||
_translucent = translucent;
|
||||
|
||||
finishedLoading(true);
|
||||
imageLoaded(image);
|
||||
glBindTexture(GL_TEXTURE_2D, getID());
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 1,
|
||||
|
|
|
@ -117,8 +117,6 @@ public:
|
|||
|
||||
NetworkTexture(const QUrl& url, bool normalMap);
|
||||
|
||||
bool isLoaded() const { return _loaded; }
|
||||
|
||||
/// Returns the average color over the entire texture.
|
||||
const glm::vec4& getAverageColor() const { return _averageColor; }
|
||||
|
||||
|
@ -131,11 +129,12 @@ protected:
|
|||
virtual void downloadFinished(QNetworkReply* reply);
|
||||
virtual void imageLoaded(const QImage& image);
|
||||
|
||||
Q_INVOKABLE void setImage(const QImage& image, const glm::vec4& averageColor, bool translucent);
|
||||
|
||||
private:
|
||||
|
||||
glm::vec4 _averageColor;
|
||||
bool _translucent;
|
||||
bool _loaded;
|
||||
};
|
||||
|
||||
/// Caches derived, dilated textures.
|
||||
|
|
|
@ -61,6 +61,7 @@ NetworkProgram::NetworkProgram(ScriptCache* cache, const QUrl& url) :
|
|||
|
||||
void NetworkProgram::downloadFinished(QNetworkReply* reply) {
|
||||
_program = QScriptProgram(QTextStream(reply).readAll(), reply->url().toString());
|
||||
finishedLoading(true);
|
||||
emit loaded();
|
||||
}
|
||||
|
||||
|
|
|
@ -72,8 +72,6 @@ public:
|
|||
|
||||
ScriptCache* getCache() const { return _cache; }
|
||||
|
||||
bool isLoaded() const { return !_program.isNull(); }
|
||||
|
||||
const QScriptProgram& getProgram() const { return _program; }
|
||||
|
||||
signals:
|
||||
|
|
|
@ -10,7 +10,15 @@
|
|||
|
||||
#include "RegisteredMetaTypes.h"
|
||||
|
||||
static int vec4MetaTypeId = qRegisterMetaType<glm::vec4>();
|
||||
static int vec3MetaTypeId = qRegisterMetaType<glm::vec3>();
|
||||
static int vec2MetaTypeId = qRegisterMetaType<glm::vec2>();
|
||||
static int quatMetaTypeId = qRegisterMetaType<glm::quat>();
|
||||
static int xColorMetaTypeId = qRegisterMetaType<xColor>();
|
||||
static int pickRayMetaTypeId = qRegisterMetaType<PickRay>();
|
||||
|
||||
void registerMetaTypes(QScriptEngine* engine) {
|
||||
qScriptRegisterMetaType(engine, vec4toScriptValue, vec4FromScriptValue);
|
||||
qScriptRegisterMetaType(engine, vec3toScriptValue, vec3FromScriptValue);
|
||||
qScriptRegisterMetaType(engine, vec2toScriptValue, vec2FromScriptValue);
|
||||
qScriptRegisterMetaType(engine, quatToScriptValue, quatFromScriptValue);
|
||||
|
@ -18,6 +26,22 @@ void registerMetaTypes(QScriptEngine* engine) {
|
|||
qScriptRegisterMetaType(engine, pickRayToScriptValue, pickRayFromScriptValue);
|
||||
}
|
||||
|
||||
QScriptValue vec4toScriptValue(QScriptEngine* engine, const glm::vec4& vec4) {
|
||||
QScriptValue obj = engine->newObject();
|
||||
obj.setProperty("x", vec4.x);
|
||||
obj.setProperty("y", vec4.y);
|
||||
obj.setProperty("z", vec4.z);
|
||||
obj.setProperty("w", vec4.w);
|
||||
return obj;
|
||||
}
|
||||
|
||||
void vec4FromScriptValue(const QScriptValue& object, glm::vec4& vec4) {
|
||||
vec4.x = object.property("x").toVariant().toFloat();
|
||||
vec4.y = object.property("y").toVariant().toFloat();
|
||||
vec4.z = object.property("z").toVariant().toFloat();
|
||||
vec4.w = object.property("w").toVariant().toFloat();
|
||||
}
|
||||
|
||||
QScriptValue vec3toScriptValue(QScriptEngine* engine, const glm::vec3 &vec3) {
|
||||
QScriptValue obj = engine->newObject();
|
||||
obj.setProperty("x", vec3.x);
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
#include "SharedUtil.h"
|
||||
|
||||
Q_DECLARE_METATYPE(glm::vec4)
|
||||
Q_DECLARE_METATYPE(glm::vec3)
|
||||
Q_DECLARE_METATYPE(glm::vec2)
|
||||
Q_DECLARE_METATYPE(glm::quat)
|
||||
|
@ -24,6 +25,9 @@ Q_DECLARE_METATYPE(xColor)
|
|||
|
||||
void registerMetaTypes(QScriptEngine* engine);
|
||||
|
||||
QScriptValue vec4toScriptValue(QScriptEngine* engine, const glm::vec4& vec4);
|
||||
void vec4FromScriptValue(const QScriptValue& object, glm::vec4& vec4);
|
||||
|
||||
QScriptValue vec3toScriptValue(QScriptEngine* engine, const glm::vec3 &vec3);
|
||||
void vec3FromScriptValue(const QScriptValue &object, glm::vec3 &vec3);
|
||||
|
||||
|
|
|
@ -27,6 +27,7 @@ QSharedPointer<Resource> ResourceCache::getResource(const QUrl& url, const QUrl&
|
|||
if (resource.isNull()) {
|
||||
resource = createResource(url, fallback.isValid() ?
|
||||
getResource(fallback, QUrl(), true) : QSharedPointer<Resource>(), delayLoad, extra);
|
||||
resource->setSelf(resource);
|
||||
_resources.insert(url, resource);
|
||||
}
|
||||
return resource;
|
||||
|
@ -77,6 +78,7 @@ Resource::Resource(const QUrl& url, bool delayLoad) :
|
|||
_request(url),
|
||||
_startedLoading(false),
|
||||
_failedToLoad(false),
|
||||
_loaded(false),
|
||||
_reply(NULL),
|
||||
_attempts(0) {
|
||||
|
||||
|
@ -106,10 +108,15 @@ void Resource::ensureLoading() {
|
|||
}
|
||||
|
||||
void Resource::setLoadPriority(const QPointer<QObject>& owner, float priority) {
|
||||
_loadPriorities.insert(owner, priority);
|
||||
if (!(_failedToLoad || _loaded)) {
|
||||
_loadPriorities.insert(owner, priority);
|
||||
}
|
||||
}
|
||||
|
||||
void Resource::setLoadPriorities(const QHash<QPointer<QObject>, float>& priorities) {
|
||||
if (_failedToLoad || _loaded) {
|
||||
return;
|
||||
}
|
||||
for (QHash<QPointer<QObject>, float>::const_iterator it = priorities.constBegin();
|
||||
it != priorities.constEnd(); it++) {
|
||||
_loadPriorities.insert(it.key(), it.value());
|
||||
|
@ -117,7 +124,9 @@ void Resource::setLoadPriorities(const QHash<QPointer<QObject>, float>& prioriti
|
|||
}
|
||||
|
||||
void Resource::clearLoadPriority(const QPointer<QObject>& owner) {
|
||||
_loadPriorities.remove(owner);
|
||||
if (!(_failedToLoad || _loaded)) {
|
||||
_loadPriorities.remove(owner);
|
||||
}
|
||||
}
|
||||
|
||||
float Resource::getLoadPriority() {
|
||||
|
@ -138,6 +147,15 @@ void Resource::attemptRequest() {
|
|||
ResourceCache::attemptRequest(this);
|
||||
}
|
||||
|
||||
void Resource::finishedLoading(bool success) {
|
||||
if (success) {
|
||||
_loaded = true;
|
||||
} else {
|
||||
_failedToLoad = true;
|
||||
}
|
||||
_loadPriorities.clear();
|
||||
}
|
||||
|
||||
void Resource::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
|
||||
if (!_reply->isFinished()) {
|
||||
return;
|
||||
|
@ -182,7 +200,7 @@ void Resource::handleReplyError() {
|
|||
// fall through to final failure
|
||||
}
|
||||
default:
|
||||
_failedToLoad = true;
|
||||
finishedLoading(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -88,6 +88,11 @@ public:
|
|||
/// Returns the highest load priority across all owners.
|
||||
float getLoadPriority();
|
||||
|
||||
/// Checks whether the resource has loaded.
|
||||
bool isLoaded() const { return _loaded; }
|
||||
|
||||
void setSelf(const QWeakPointer<Resource>& self) { _self = self; }
|
||||
|
||||
protected slots:
|
||||
|
||||
void attemptRequest();
|
||||
|
@ -96,10 +101,15 @@ protected:
|
|||
|
||||
virtual void downloadFinished(QNetworkReply* reply) = 0;
|
||||
|
||||
/// Should be called by subclasses when all the loading that will be done has been done.
|
||||
Q_INVOKABLE void finishedLoading(bool success);
|
||||
|
||||
QNetworkRequest _request;
|
||||
bool _startedLoading;
|
||||
bool _failedToLoad;
|
||||
bool _loaded;
|
||||
QHash<QPointer<QObject>, float> _loadPriorities;
|
||||
QWeakPointer<Resource> _self;
|
||||
|
||||
private slots:
|
||||
|
||||
|
|
Loading…
Reference in a new issue