Merge branch 'master' of https://github.com/highfidelity/hifi into metavoxels

Conflicts:
	assignment-client/src/metavoxels/MetavoxelServer.cpp
This commit is contained in:
Andrzej Kapolka 2014-02-20 12:05:15 -08:00
commit 09677b06f2
5 changed files with 133 additions and 22 deletions

View file

@ -24,7 +24,7 @@ ProgramObject MetavoxelSystem::_program;
int MetavoxelSystem::_pointScaleLocation;
MetavoxelSystem::MetavoxelSystem() :
_pointVisitor(_points),
_simulateVisitor(_points),
_buffer(QOpenGLBuffer::VertexBuffer) {
}
@ -48,6 +48,7 @@ void MetavoxelSystem::init() {
void MetavoxelSystem::applyEdit(const MetavoxelEditMessage& edit) {
foreach (const SharedNodePointer& node, NodeList::getInstance()->getNodeHash()) {
if (node->getType() == NodeType::MetavoxelServer) {
QMutexLocker locker(&node->getMutex());
MetavoxelClient* client = static_cast<MetavoxelClient*>(node->getLinkedData());
if (client) {
client->applyEdit(edit);
@ -59,11 +60,14 @@ void MetavoxelSystem::applyEdit(const MetavoxelEditMessage& edit) {
void MetavoxelSystem::simulate(float deltaTime) {
// simulate the clients
_points.clear();
_simulateVisitor.setDeltaTime(deltaTime);
foreach (const SharedNodePointer& node, NodeList::getInstance()->getNodeHash()) {
if (node->getType() == NodeType::MetavoxelServer) {
QMutexLocker locker(&node->getMutex());
MetavoxelClient* client = static_cast<MetavoxelClient*>(node->getLinkedData());
if (client) {
client->simulate(deltaTime, _pointVisitor);
client->simulate(deltaTime);
client->getData().guide(_simulateVisitor);
}
}
}
@ -117,6 +121,16 @@ void MetavoxelSystem::render() {
_buffer.release();
_program.release();
foreach (const SharedNodePointer& node, NodeList::getInstance()->getNodeHash()) {
if (node->getType() == NodeType::MetavoxelServer) {
QMutexLocker locker(&node->getMutex());
MetavoxelClient* client = static_cast<MetavoxelClient*>(node->getLinkedData());
if (client) {
client->getData().guide(_renderVisitor);
}
}
}
}
void MetavoxelSystem::maybeAttachClient(const SharedNodePointer& node) {
@ -126,15 +140,20 @@ void MetavoxelSystem::maybeAttachClient(const SharedNodePointer& node) {
}
}
MetavoxelSystem::PointVisitor::PointVisitor(QVector<Point>& points) :
MetavoxelVisitor(QVector<AttributePointer>() <<
AttributeRegistry::getInstance()->getColorAttribute() <<
AttributeRegistry::getInstance()->getNormalAttribute(),
QVector<AttributePointer>()),
MetavoxelSystem::SimulateVisitor::SimulateVisitor(QVector<Point>& points) :
SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute(),
QVector<AttributePointer>() << AttributeRegistry::getInstance()->getColorAttribute() <<
AttributeRegistry::getInstance()->getNormalAttribute()),
_points(points) {
}
bool MetavoxelSystem::PointVisitor::visit(MetavoxelInfo& info) {
void MetavoxelSystem::SimulateVisitor::visit(Spanner* spanner) {
spanner->getRenderer()->simulate(_deltaTime);
}
bool MetavoxelSystem::SimulateVisitor::visit(MetavoxelInfo& info) {
SpannerVisitor::visit(info);
if (!info.isLeaf) {
return true;
}
@ -149,6 +168,14 @@ bool MetavoxelSystem::PointVisitor::visit(MetavoxelInfo& info) {
return false;
}
MetavoxelSystem::RenderVisitor::RenderVisitor() :
SpannerVisitor(QVector<AttributePointer>() << AttributeRegistry::getInstance()->getSpannersAttribute()) {
}
void MetavoxelSystem::RenderVisitor::visit(Spanner* spanner) {
spanner->getRenderer()->render(1.0f);
}
MetavoxelClient::MetavoxelClient(const SharedNodePointer& node) :
_node(node),
_sequencer(byteArrayWithPopluatedHeader(PacketTypeMetavoxelData)) {
@ -177,13 +204,11 @@ void MetavoxelClient::applyEdit(const MetavoxelEditMessage& edit) {
_sequencer.sendHighPriorityMessage(QVariant::fromValue(edit));
}
void MetavoxelClient::simulate(float deltaTime, MetavoxelVisitor& visitor) {
void MetavoxelClient::simulate(float deltaTime) {
Bitstream& out = _sequencer.startPacket();
ClientStateMessage state = { Application::getInstance()->getCamera()->getPosition() };
out << QVariant::fromValue(state);
_sequencer.endPacket();
_data.guide(visitor);
}
int MetavoxelClient::parseData(const QByteArray& packet) {

View file

@ -53,20 +53,30 @@ private:
quint8 normal[3];
};
class PointVisitor : public MetavoxelVisitor {
class SimulateVisitor : public SpannerVisitor {
public:
PointVisitor(QVector<Point>& points);
SimulateVisitor(QVector<Point>& points);
void setDeltaTime(float deltaTime) { _deltaTime = deltaTime; }
virtual void visit(Spanner* spanner);
virtual bool visit(MetavoxelInfo& info);
private:
QVector<Point>& _points;
float _deltaTime;
};
class RenderVisitor : public SpannerVisitor {
public:
RenderVisitor();
virtual void visit(Spanner* spanner);
};
static ProgramObject _program;
static int _pointScaleLocation;
QVector<Point> _points;
PointVisitor _pointVisitor;
SimulateVisitor _simulateVisitor;
RenderVisitor _renderVisitor;
QOpenGLBuffer _buffer;
};
@ -79,11 +89,11 @@ public:
MetavoxelClient(const SharedNodePointer& node);
virtual ~MetavoxelClient();
MetavoxelData& getData() { return _data; }
void applyEdit(const MetavoxelEditMessage& edit);
void simulate(float deltaTime, MetavoxelVisitor& visitor);
void render();
void simulate(float deltaTime);
virtual int parseData(const QByteArray& packet);

View file

@ -49,6 +49,9 @@ Box MetavoxelData::getBounds() const {
}
void MetavoxelData::guide(MetavoxelVisitor& visitor) {
// let the visitor know we're about to begin a tour
visitor.prepare();
// start with the root values/defaults (plus the guide attribute)
const QVector<AttributePointer>& inputs = visitor.getInputs();
const QVector<AttributePointer>& outputs = visitor.getOutputs();
@ -530,6 +533,32 @@ MetavoxelVisitor::MetavoxelVisitor(const QVector<AttributePointer>& inputs, cons
MetavoxelVisitor::~MetavoxelVisitor() {
}
void MetavoxelVisitor::prepare() {
// nothing by default
}
SpannerVisitor::SpannerVisitor(const QVector<AttributePointer>& spannerInputs, const QVector<AttributePointer>& inputs,
const QVector<AttributePointer>& outputs) :
MetavoxelVisitor(inputs + spannerInputs, outputs),
_spannerInputCount(spannerInputs.size()) {
}
void SpannerVisitor::prepare() {
Spanner::incrementVisit();
}
bool SpannerVisitor::visit(MetavoxelInfo& info) {
for (int i = info.inputValues.size() - _spannerInputCount; i < info.inputValues.size(); i++) {
foreach (const SharedObjectPointer& object, info.inputValues.at(i).getInlineValue<SharedObjectSet>()) {
Spanner* spanner = static_cast<Spanner*>(object.data());
if (spanner->testAndSetVisited()) {
visit(spanner);
}
}
}
return !info.isLeaf;
}
DefaultMetavoxelGuide::DefaultMetavoxelGuide() {
}
@ -788,11 +817,11 @@ void Spanner::setBounds(const Box& bounds) {
emit boundsChanged(_bounds = bounds);
}
bool Spanner::testAndSetVisited(int visit) {
if (_lastVisit == visit) {
bool Spanner::testAndSetVisited() {
if (_lastVisit == _visit) {
return false;
}
_lastVisit = visit;
_lastVisit = _visit;
return true;
}
@ -815,6 +844,8 @@ QByteArray Spanner::getRendererClassName() const {
return "SpannerRendererer";
}
int Spanner::_visit = 0;
SpannerRenderer::SpannerRenderer() {
}
@ -831,6 +862,9 @@ void SpannerRenderer::render(float alpha) {
}
Transformable::Transformable() : _scale(1.0f) {
connect(this, SIGNAL(translationChanged(const glm::vec3&)), SLOT(updateBounds()));
connect(this, SIGNAL(rotationChanged(const glm::vec3&)), SLOT(updateBounds()));
connect(this, SIGNAL(scaleChanged(float)), SLOT(updateBounds()));
}
void Transformable::setTranslation(const glm::vec3& translation) {
@ -851,6 +885,12 @@ void Transformable::setScale(float scale) {
}
}
void Transformable::updateBounds() {
// temporary fake bounds
glm::vec3 scaleVector(_scale, _scale, _scale);
setBounds(Box(_translation - scaleVector, _translation + scaleVector));
}
StaticModel::StaticModel() {
}

View file

@ -28,6 +28,7 @@ class MetavoxelNode;
class MetavoxelVisitation;
class MetavoxelVisitor;
class NetworkValue;
class Spanner;
class SpannerRenderer;
/// The base metavoxel representation shared between server and client.
@ -138,7 +139,8 @@ public:
class MetavoxelVisitor {
public:
MetavoxelVisitor(const QVector<AttributePointer>& inputs, const QVector<AttributePointer>& outputs);
MetavoxelVisitor(const QVector<AttributePointer>& inputs,
const QVector<AttributePointer>& outputs = QVector<AttributePointer>());
virtual ~MetavoxelVisitor();
/// Returns a reference to the list of input attributes desired.
@ -147,6 +149,9 @@ public:
/// Returns a reference to the list of output attributes provided.
const QVector<AttributePointer>& getOutputs() const { return _outputs; }
/// Prepares for a new tour of the metavoxel data.
virtual void prepare();
/// Visits a metavoxel.
/// \param info the metavoxel data
/// \return if true, continue descending; if false, stop
@ -160,6 +165,25 @@ protected:
typedef QSharedPointer<MetavoxelVisitor> MetavoxelVisitorPointer;
/// Interface for visitors to spanners.
class SpannerVisitor : public MetavoxelVisitor {
public:
SpannerVisitor(const QVector<AttributePointer>& spannerInputs,
const QVector<AttributePointer>& inputs = QVector<AttributePointer>(),
const QVector<AttributePointer>& outputs = QVector<AttributePointer>());
/// Visits a spanner.
virtual void visit(Spanner* spanner) = 0;
virtual void prepare();
virtual bool visit(MetavoxelInfo& info);
protected:
int _spannerInputCount;
};
/// Interface for objects that guide metavoxel visitors.
class MetavoxelGuide : public SharedObject {
Q_OBJECT
@ -259,6 +283,9 @@ class Spanner : public SharedObject {
public:
/// Increments the value of the global visit counter.
static void incrementVisit() { _visit++; }
Spanner();
void setBounds(const Box& bounds);
@ -269,7 +296,7 @@ public:
/// Checks whether we've visited this object on the current traversal. If we have, returns false.
/// If we haven't, sets the last visit identifier and returns true.
bool testAndSetVisited(int visit);
bool testAndSetVisited();
/// Returns a pointer to the renderer, creating it if necessary.
SpannerRenderer* getRenderer();
@ -290,6 +317,8 @@ private:
float _granularity;
int _lastVisit; ///< the identifier of the last visit
SpannerRenderer* _renderer;
static int _visit; ///< the global visit counter
};
/// Base class for objects that can render spanners.
@ -331,6 +360,10 @@ signals:
void rotationChanged(const glm::vec3& rotation);
void scaleChanged(float scale);
protected slots:
virtual void updateBounds();
private:
glm::vec3 _translation;

View file

@ -229,6 +229,7 @@ void NodeList::processNodeData(const HifiSockAddr& senderSockAddr, const QByteAr
// send back a reply
SharedNodePointer matchingNode = sendingNodeForPacket(packet);
if (matchingNode) {
matchingNode->setLastHeardMicrostamp(usecTimestampNow());
QByteArray replyPacket = constructPingReplyPacket(packet);
writeDatagram(replyPacket, matchingNode, senderSockAddr);
}
@ -239,6 +240,8 @@ void NodeList::processNodeData(const HifiSockAddr& senderSockAddr, const QByteAr
SharedNodePointer sendingNode = sendingNodeForPacket(packet);
if (sendingNode) {
sendingNode->setLastHeardMicrostamp(usecTimestampNow());
// activate the appropriate socket for this node, if not yet updated
activateSocketFromNodeCommunication(packet, sendingNode);