3
0
Fork 0
mirror of https://github.com/lubosz/overte.git synced 2025-04-27 04:55:32 +02:00

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

This commit is contained in:
Andrzej Kapolka 2014-03-20 13:12:28 -07:00
commit 93846460ff
75 changed files with 1514 additions and 1191 deletions

View file

@ -26,8 +26,7 @@
Agent::Agent(const QByteArray& packet) :
ThreadedAssignment(packet),
_voxelEditSender(),
_particleEditSender(),
_avatarAudioStream(NULL)
_particleEditSender()
{
// be the parent of the script engine so it gets moved when we do
_scriptEngine.setParent(this);
@ -36,30 +35,6 @@ Agent::Agent(const QByteArray& packet) :
_scriptEngine.getParticlesScriptingInterface()->setPacketSender(&_particleEditSender);
}
Agent::~Agent() {
delete _avatarAudioStream;
}
const int SCRIPT_AUDIO_BUFFER_SAMPLES = floor(((SCRIPT_DATA_CALLBACK_USECS * SAMPLE_RATE) / (1000 * 1000)) + 0.5);
void Agent::setSendAvatarAudioStream(bool sendAvatarAudioStream) {
if (sendAvatarAudioStream) {
// the agentAudioStream number of samples is related to the ScriptEngine callback rate
_avatarAudioStream = new int16_t[SCRIPT_AUDIO_BUFFER_SAMPLES];
// fill the _audioStream with zeroes to start
memset(_avatarAudioStream, 0, SCRIPT_AUDIO_BUFFER_SAMPLES * sizeof(int16_t));
_scriptEngine.setNumAvatarAudioBufferSamples(SCRIPT_AUDIO_BUFFER_SAMPLES);
_scriptEngine.setAvatarAudioBuffer(_avatarAudioStream);
} else {
delete _avatarAudioStream;
_avatarAudioStream = NULL;
_scriptEngine.setAvatarAudioBuffer(NULL);
}
}
void Agent::readPendingDatagrams() {
QByteArray receivedPacket;
HifiSockAddr senderSockAddr;
@ -95,12 +70,19 @@ void Agent::readPendingDatagrams() {
// also give our local particle tree a chance to remap any internal locally created particles
_particleViewer.getTree()->handleAddParticleResponse(receivedPacket);
// Make sure our Node and NodeList knows we've heard from this node.
SharedNodePointer sourceNode = nodeList->sendingNodeForPacket(receivedPacket);
sourceNode->setLastHeardMicrostamp(usecTimestampNow());
} else if (datagramPacketType == PacketTypeParticleData
|| datagramPacketType == PacketTypeParticleErase
|| datagramPacketType == PacketTypeOctreeStats
|| datagramPacketType == PacketTypeVoxelData
) {
// Make sure our Node and NodeList knows we've heard from this node.
SharedNodePointer sourceNode = nodeList->sendingNodeForPacket(receivedPacket);
sourceNode->setLastHeardMicrostamp(usecTimestampNow());
QByteArray mutablePacket = receivedPacket;
ssize_t messageLength = mutablePacket.size();

View file

@ -28,20 +28,24 @@ class Agent : public ThreadedAssignment {
Q_OBJECT
Q_PROPERTY(bool isAvatar READ isAvatar WRITE setIsAvatar)
Q_PROPERTY(bool sendAvatarAudioStream READ isSendingAvatarAudioStream WRITE setSendAvatarAudioStream)
Q_PROPERTY(bool isPlayingAvatarSound READ isPlayingAvatarSound)
Q_PROPERTY(bool isListeningToAudioStream READ isListeningToAudioStream WRITE setIsListeningToAudioStream)
public:
Agent(const QByteArray& packet);
~Agent();
void setIsAvatar(bool isAvatar) { QMetaObject::invokeMethod(&_scriptEngine, "setIsAvatar", Q_ARG(bool, isAvatar)); }
bool isAvatar() const { return _scriptEngine.isAvatar(); }
void setSendAvatarAudioStream(bool sendAvatarAudioStream);
bool isSendingAvatarAudioStream() const { return (bool) _scriptEngine.sendsAvatarAudioStream(); }
bool isPlayingAvatarSound() const { return _scriptEngine.isPlayingAvatarSound(); }
bool isListeningToAudioStream() const { return _scriptEngine.isListeningToAudioStream(); }
void setIsListeningToAudioStream(bool isListeningToAudioStream)
{ _scriptEngine.setIsListeningToAudioStream(isListeningToAudioStream); }
public slots:
void run();
void readPendingDatagrams();
void playAvatarSound(Sound* avatarSound) { _scriptEngine.setAvatarSound(avatarSound); }
private:
ScriptEngine _scriptEngine;
@ -50,8 +54,6 @@ private:
ParticleTreeHeadlessViewer _particleViewer;
VoxelTreeHeadlessViewer _voxelViewer;
int16_t* _avatarAudioStream;
};
#endif /* defined(__hifi__Agent__) */

View file

@ -200,13 +200,13 @@ void AssignmentClient::assignmentCompleted() {
qDebug("Assignment finished or never started - waiting for new assignment.");
NodeList* nodeList = NodeList::getInstance();
// have us handle incoming NodeList datagrams again
disconnect(&nodeList->getNodeSocket(), 0, _currentAssignment, 0);
connect(&nodeList->getNodeSocket(), &QUdpSocket::readyRead, this, &AssignmentClient::readPendingDatagrams);
_currentAssignment = NULL;
// reset our NodeList by switching back to unassigned and clearing the list
nodeList->setOwnerType(NodeType::Unassigned);
nodeList->reset();

View file

@ -286,7 +286,7 @@ void AudioMixer::prepareMixForListeningNode(Node* node) {
AvatarAudioRingBuffer* nodeRingBuffer = ((AudioMixerClientData*) node->getLinkedData())->getAvatarAudioRingBuffer();
// zero out the client mix for this node
memset(_clientSamples, 0, sizeof(_clientSamples));
memset(_clientSamples, 0, NETWORK_BUFFER_LENGTH_BYTES_STEREO);
// loop through all other nodes that have sufficient audio to mix
foreach (const SharedNodePointer& otherNode, NodeList::getInstance()->getNodeHash()) {
@ -321,7 +321,8 @@ void AudioMixer::readPendingDatagrams() {
PacketType mixerPacketType = packetTypeForPacket(receivedPacket);
if (mixerPacketType == PacketTypeMicrophoneAudioNoEcho
|| mixerPacketType == PacketTypeMicrophoneAudioWithEcho
|| mixerPacketType == PacketTypeInjectAudio) {
|| mixerPacketType == PacketTypeInjectAudio
|| mixerPacketType == PacketTypeSilentAudioFrame) {
nodeList->findNodeAndUpdateWithDataFromPacket(receivedPacket);
} else {

View file

@ -41,7 +41,8 @@ AvatarAudioRingBuffer* AudioMixerClientData::getAvatarAudioRingBuffer() const {
int AudioMixerClientData::parseData(const QByteArray& packet) {
PacketType packetType = packetTypeForPacket(packet);
if (packetType == PacketTypeMicrophoneAudioWithEcho
|| packetType == PacketTypeMicrophoneAudioNoEcho) {
|| packetType == PacketTypeMicrophoneAudioNoEcho
|| packetType == PacketTypeSilentAudioFrame) {
// grab the AvatarAudioRingBuffer from the vector (or create it if it doesn't exist)
AvatarAudioRingBuffer* avatarRingBuffer = getAvatarAudioRingBuffer();

View file

@ -62,7 +62,8 @@ void broadcastAvatarData() {
mixedAvatarByteArray.resize(numPacketHeaderBytes);
AvatarMixerClientData* myData = reinterpret_cast<AvatarMixerClientData*>(node->getLinkedData());
glm::vec3 myPosition = myData->getPosition();
AvatarData& avatar = myData->getAvatar();
glm::vec3 myPosition = avatar.getPosition();
// this is an AGENT we have received head data from
// send back a packet with other active node data to this node
@ -70,7 +71,8 @@ void broadcastAvatarData() {
if (otherNode->getLinkedData() && otherNode->getUUID() != node->getUUID()) {
AvatarMixerClientData* otherNodeData = reinterpret_cast<AvatarMixerClientData*>(otherNode->getLinkedData());
glm::vec3 otherPosition = otherNodeData->getPosition();
AvatarData& otherAvatar = otherNodeData->getAvatar();
glm::vec3 otherPosition = otherAvatar.getPosition();
float distanceToAvatar = glm::length(myPosition - otherPosition);
// The full rate distance is the distance at which EVERY update will be sent for this avatar
// at a distance of twice the full rate distance, there will be a 50% chance of sending this avatar's update
@ -79,7 +81,7 @@ void broadcastAvatarData() {
if ((distanceToAvatar == 0.f) || (randFloat() < FULL_RATE_DISTANCE / distanceToAvatar)) {
QByteArray avatarByteArray;
avatarByteArray.append(otherNode->getUUID().toRfc4122());
avatarByteArray.append(otherNodeData->toByteArray());
avatarByteArray.append(otherAvatar.toByteArray());
if (avatarByteArray.size() + mixedAvatarByteArray.size() > MAX_PACKET_SIZE) {
nodeList->writeDatagram(mixedAvatarByteArray, node);
@ -110,7 +112,8 @@ void broadcastIdentityPacket() {
if (node->getLinkedData() && node->getType() == NodeType::Agent) {
AvatarMixerClientData* nodeData = reinterpret_cast<AvatarMixerClientData*>(node->getLinkedData());
QByteArray individualData = nodeData->identityByteArray();
AvatarData& avatar = nodeData->getAvatar();
QByteArray individualData = avatar.identityByteArray();
individualData.replace(0, NUM_BYTES_RFC4122_UUID, node->getUUID().toRfc4122());
if (avatarIdentityPacket.size() + individualData.size() > MAX_PACKET_SIZE) {
@ -135,9 +138,10 @@ void broadcastIdentityPacket() {
void broadcastBillboardPacket(const SharedNodePointer& sendingNode) {
AvatarMixerClientData* nodeData = static_cast<AvatarMixerClientData*>(sendingNode->getLinkedData());
AvatarData& avatar = nodeData->getAvatar();
QByteArray packet = byteArrayWithPopulatedHeader(PacketTypeAvatarBillboard);
packet.append(sendingNode->getUUID().toRfc4122());
packet.append(nodeData->getBillboard());
packet.append(avatar.getBillboard());
NodeList* nodeList = NodeList::getInstance();
foreach (const SharedNodePointer& node, nodeList->getNodeHash()) {
@ -190,12 +194,13 @@ void AvatarMixer::readPendingDatagrams() {
if (avatarNode && avatarNode->getLinkedData()) {
AvatarMixerClientData* nodeData = reinterpret_cast<AvatarMixerClientData*>(avatarNode->getLinkedData());
if (nodeData->hasIdentityChangedAfterParsing(receivedPacket)
AvatarData& avatar = nodeData->getAvatar();
if (avatar.hasIdentityChangedAfterParsing(receivedPacket)
&& !nodeData->hasSentIdentityBetweenKeyFrames()) {
// this avatar changed their identity in some way and we haven't sent a packet in this keyframe
QByteArray identityPacket = byteArrayWithPopulatedHeader(PacketTypeAvatarIdentity);
QByteArray individualByteArray = nodeData->identityByteArray();
QByteArray individualByteArray = avatar.identityByteArray();
individualByteArray.replace(0, NUM_BYTES_RFC4122_UUID, avatarNode->getUUID().toRfc4122());
identityPacket.append(individualByteArray);
@ -213,7 +218,8 @@ void AvatarMixer::readPendingDatagrams() {
if (avatarNode && avatarNode->getLinkedData()) {
AvatarMixerClientData* nodeData = static_cast<AvatarMixerClientData*>(avatarNode->getLinkedData());
if (nodeData->hasBillboardChangedAfterParsing(receivedPacket)
AvatarData& avatar = nodeData->getAvatar();
if (avatar.hasBillboardChangedAfterParsing(receivedPacket)
&& !nodeData->hasSentBillboardBetweenKeyFrames()) {
// this avatar changed their billboard and we haven't sent a packet in this keyframe
broadcastBillboardPacket(avatarNode);

View file

@ -9,8 +9,15 @@
#include "AvatarMixerClientData.h"
AvatarMixerClientData::AvatarMixerClientData() :
NodeData(),
_hasSentIdentityBetweenKeyFrames(false),
_hasSentBillboardBetweenKeyFrames(false)
{
}
int AvatarMixerClientData::parseData(const QByteArray& packet) {
// compute the offset to the data payload
int offset = numBytesForPacketHeader(packet);
return _avatar.parseDataAtOffset(packet, offset);
}

View file

@ -12,11 +12,14 @@
#include <QtCore/QUrl>
#include <AvatarData.h>
#include <NodeData.h>
class AvatarMixerClientData : public AvatarData {
class AvatarMixerClientData : public NodeData {
Q_OBJECT
public:
AvatarMixerClientData();
int parseData(const QByteArray& packet);
bool hasSentIdentityBetweenKeyFrames() const { return _hasSentIdentityBetweenKeyFrames; }
void setHasSentIdentityBetweenKeyFrames(bool hasSentIdentityBetweenKeyFrames)
@ -25,11 +28,14 @@ public:
bool hasSentBillboardBetweenKeyFrames() const { return _hasSentBillboardBetweenKeyFrames; }
void setHasSentBillboardBetweenKeyFrames(bool hasSentBillboardBetweenKeyFrames)
{ _hasSentBillboardBetweenKeyFrames = hasSentBillboardBetweenKeyFrames; }
AvatarData& getAvatar() { return _avatar; }
private:
bool _hasSentIdentityBetweenKeyFrames;
bool _hasSentBillboardBetweenKeyFrames;
AvatarData _avatar;
};
#endif /* defined(__hifi__AvatarMixerClientData__) */

View file

@ -44,7 +44,7 @@ typedef std::map<QUuid, SingleSenderStats>::iterator NodeToSenderStatsMapIterato
/// Handles processing of incoming network packets for the voxel-server. As with other ReceivedPacketProcessor classes
/// the user is responsible for reading inbound packets and adding them to the processing queue by calling queueReceivedPacket()
class OctreeInboundPacketProcessor : public ReceivedPacketProcessor {
Q_OBJECT
public:
OctreeInboundPacketProcessor(OctreeServer* myServer);

View file

@ -35,7 +35,8 @@ OctreeQueryNode::OctreeQueryNode() :
_lastClientOctreeSizeScale(DEFAULT_OCTREE_SIZE_SCALE),
_lodChanged(false),
_lodInitialized(false),
_sequenceNumber(0)
_sequenceNumber(0),
_lastRootTimestamp(0)
{
}

View file

@ -23,6 +23,7 @@ class OctreeSendThread;
class OctreeServer;
class OctreeQueryNode : public OctreeQuery {
Q_OBJECT
public:
OctreeQueryNode();
virtual ~OctreeQueryNode();
@ -85,6 +86,12 @@ public:
void dumpOutOfView();
quint64 getLastRootTimestamp() const { return _lastRootTimestamp; }
void setLastRootTimestamp(quint64 timestamp) { _lastRootTimestamp = timestamp; }
unsigned int getlastOctreePacketLength() const { return _lastOctreePacketLength; }
int getDuplicatePacketCount() const { return _duplicatePacketCount; }
private:
OctreeQueryNode(const OctreeQueryNode &);
OctreeQueryNode& operator= (const OctreeQueryNode&);
@ -119,6 +126,7 @@ private:
bool _lodInitialized;
OCTREE_PACKET_SEQUENCE _sequenceNumber;
quint64 _lastRootTimestamp;
};
#endif /* defined(__hifi__OctreeQueryNode__) */

View file

@ -23,18 +23,28 @@ OctreeSendThread::OctreeSendThread(const QUuid& nodeUUID, OctreeServer* myServer
_packetData(),
_nodeMissingCount(0)
{
qDebug() << "client connected - starting sending thread";
QString safeServerName("Octree");
if (_myServer) {
safeServerName = _myServer->getMyServerName();
}
qDebug() << qPrintable(safeServerName) << "server [" << _myServer << "]: client connected "
"- starting sending thread [" << this << "]";
OctreeServer::clientConnected();
}
OctreeSendThread::~OctreeSendThread() {
qDebug() << "client disconnected - ending sending thread";
QString safeServerName("Octree");
if (_myServer) {
safeServerName = _myServer->getMyServerName();
}
qDebug() << qPrintable(safeServerName) << "server [" << _myServer << "]: client disconnected "
"- ending sending thread [" << this << "]";
OctreeServer::clientDisconnected();
}
bool OctreeSendThread::process() {
const int MAX_NODE_MISSING_CHECKS = 10;
if (_nodeMissingCount > MAX_NODE_MISSING_CHECKS) {
qDebug() << "our target node:" << _nodeUUID << "has been missing the last" << _nodeMissingCount
@ -57,18 +67,11 @@ bool OctreeSendThread::process() {
// Sometimes the node data has not yet been linked, in which case we can't really do anything
if (nodeData) {
bool viewFrustumChanged = nodeData->updateCurrentViewFrustum();
if (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug()) {
printf("nodeData->updateCurrentViewFrustum() changed=%s\n", debug::valueOf(viewFrustumChanged));
}
packetDistributor(node, nodeData, viewFrustumChanged);
}
} else {
_nodeMissingCount++;
}
} else {
if (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug()) {
qDebug("OctreeSendThread::process() waiting for isInitialLoadComplete()");
}
}
// Only sleep if we're still running and we got the lock last time we tried, otherwise try to get the lock asap
@ -81,10 +84,6 @@ bool OctreeSendThread::process() {
PerformanceWarning warn(false,"OctreeSendThread... usleep()",false,&_usleepTime,&_usleepCalls);
usleep(usecToSleep);
} else {
if (true || (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug())) {
qDebug() << "Last send took too much time (" << (elapsed / USECS_PER_MSEC)
<<" msecs), barely sleeping 1 usec!\n";
}
const int MIN_USEC_TO_SLEEP = 1;
usleep(MIN_USEC_TO_SLEEP);
}
@ -114,7 +113,7 @@ int OctreeSendThread::handlePacketSend(const SharedNodePointer& node, OctreeQuer
quint64 lockWaitEnd = usecTimestampNow();
float lockWaitElapsedUsec = (float)(lockWaitEnd - lockWaitStart);
OctreeServer::trackNodeWaitTime(lockWaitElapsedUsec);
const HifiSockAddr* nodeAddress = node->getActiveSocket();
if (!nodeAddress) {
return packetsSent; // without sending...
@ -235,8 +234,6 @@ int OctreeSendThread::handlePacketSend(const SharedNodePointer& node, OctreeQuer
/// Version of voxel distributor that sends the deepest LOD level at once
int OctreeSendThread::packetDistributor(const SharedNodePointer& node, OctreeQueryNode* nodeData, bool viewFrustumChanged) {
bool forceDebugging = false;
int truePacketsSent = 0;
int trueBytesSent = 0;
int packetsSentThisInterval = 0;
@ -259,72 +256,22 @@ int OctreeSendThread::packetDistributor(const SharedNodePointer& node, OctreeQue
// then let's just send that waiting packet.
if (!nodeData->getCurrentPacketFormatMatches()) {
if (nodeData->isPacketWaiting()) {
if (forceDebugging || (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug())) {
qDebug("about to call handlePacketSend() .... line: %d -- format change "
"wantColor=%s wantCompression=%s SENDING PARTIAL PACKET! currentPacketIsColor=%s "
"currentPacketIsCompressed=%s",
__LINE__,
debug::valueOf(wantColor), debug::valueOf(wantCompression),
debug::valueOf(nodeData->getCurrentPacketIsColor()),
debug::valueOf(nodeData->getCurrentPacketIsCompressed()) );
}
packetsSentThisInterval += handlePacketSend(node, nodeData, trueBytesSent, truePacketsSent);
} else {
if (forceDebugging || (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug())) {
qDebug("wantColor=%s wantCompression=%s FIXING HEADER! currentPacketIsColor=%s currentPacketIsCompressed=%s",
debug::valueOf(wantColor), debug::valueOf(wantCompression),
debug::valueOf(nodeData->getCurrentPacketIsColor()),
debug::valueOf(nodeData->getCurrentPacketIsCompressed()) );
}
nodeData->resetOctreePacket();
}
int targetSize = MAX_OCTREE_PACKET_DATA_SIZE;
if (wantCompression) {
targetSize = nodeData->getAvailable() - sizeof(OCTREE_PACKET_INTERNAL_SECTION_SIZE);
}
if (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug()) {
qDebug("line:%d _packetData.changeSettings() wantCompression=%s targetSize=%d", __LINE__,
debug::valueOf(wantCompression), targetSize);
}
_packetData.changeSettings(wantCompression, targetSize);
}
if (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug()) {
qDebug("wantColor/isColor=%s/%s wantCompression/isCompressed=%s/%s viewFrustumChanged=%s, getWantLowResMoving()=%s",
debug::valueOf(wantColor), debug::valueOf(nodeData->getCurrentPacketIsColor()),
debug::valueOf(wantCompression), debug::valueOf(nodeData->getCurrentPacketIsCompressed()),
debug::valueOf(viewFrustumChanged), debug::valueOf(nodeData->getWantLowResMoving()));
}
const ViewFrustum* lastViewFrustum = wantDelta ? &nodeData->getLastKnownViewFrustum() : NULL;
if (forceDebugging || (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug())) {
qDebug("packetDistributor() viewFrustumChanged=%s, nodeBag.isEmpty=%s, viewSent=%s",
debug::valueOf(viewFrustumChanged), debug::valueOf(nodeData->nodeBag.isEmpty()),
debug::valueOf(nodeData->getViewSent())
);
}
// If the current view frustum has changed OR we have nothing to send, then search against
// the current view frustum for things to send.
if (viewFrustumChanged || nodeData->nodeBag.isEmpty()) {
quint64 now = usecTimestampNow();
if (forceDebugging || (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug())) {
qDebug("(viewFrustumChanged=%s || nodeData->nodeBag.isEmpty() =%s)...",
debug::valueOf(viewFrustumChanged), debug::valueOf(nodeData->nodeBag.isEmpty()));
if (nodeData->getLastTimeBagEmpty() > 0) {
float elapsedSceneSend = (now - nodeData->getLastTimeBagEmpty()) / 1000000.0f;
if (viewFrustumChanged) {
qDebug("viewFrustumChanged resetting after elapsed time to send scene = %f seconds", elapsedSceneSend);
} else {
qDebug("elapsed time to send scene = %f seconds", elapsedSceneSend);
}
qDebug("[ occlusionCulling:%s, wantDelta:%s, wantColor:%s ]",
debug::valueOf(nodeData->getWantOcclusionCulling()), debug::valueOf(wantDelta),
debug::valueOf(wantColor));
}
}
// if our view has changed, we need to reset these things...
if (viewFrustumChanged) {
@ -342,44 +289,25 @@ int OctreeSendThread::packetDistributor(const SharedNodePointer& node, OctreeQue
// track completed scenes and send out the stats packet accordingly
nodeData->stats.sceneCompleted();
::endSceneSleepTime = _usleepTime;
unsigned long sleepTime = ::endSceneSleepTime - ::startSceneSleepTime;
nodeData->setLastRootTimestamp(_myServer->getOctree()->getRoot()->getLastChanged());
unsigned long encodeTime = nodeData->stats.getTotalEncodeTime();
unsigned long elapsedTime = nodeData->stats.getElapsedTime();
// TODO: add these to stats page
//::endSceneSleepTime = _usleepTime;
//unsigned long sleepTime = ::endSceneSleepTime - ::startSceneSleepTime;
//unsigned long encodeTime = nodeData->stats.getTotalEncodeTime();
//unsigned long elapsedTime = nodeData->stats.getElapsedTime();
if (forceDebugging || (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug())) {
qDebug("about to call handlePacketSend() .... line: %d -- completed scene", __LINE__ );
}
int packetsJustSent = handlePacketSend(node, nodeData, trueBytesSent, truePacketsSent);
packetsSentThisInterval += packetsJustSent;
if (forceDebugging) {
qDebug("packetsJustSent=%d packetsSentThisInterval=%d", packetsJustSent, packetsSentThisInterval);
}
if (forceDebugging || _myServer->wantsDebugSending()) {
qDebug() << "Scene completed at " << usecTimestampNow()
<< "encodeTime:" << encodeTime
<< " sleepTime:" << sleepTime
<< " elapsed:" << elapsedTime
<< " Packets:" << _totalPackets
<< " Bytes:" << _totalBytes
<< " Wasted:" << _totalWastedBytes;
}
// If we're starting a full scene, then definitely we want to empty the nodeBag
if (isFullScene) {
nodeData->nodeBag.deleteAll();
}
if (forceDebugging || _myServer->wantsDebugSending()) {
qDebug() << "Scene started at " << usecTimestampNow()
<< " Packets:" << _totalPackets
<< " Bytes:" << _totalBytes
<< " Wasted:" << _totalWastedBytes;
}
::startSceneSleepTime = _usleepTime;
// TODO: add these to stats page
//::startSceneSleepTime = _usleepTime;
// start tracking our stats
nodeData->stats.sceneStarted(isFullScene, viewFrustumChanged, _myServer->getOctree()->getRoot(), _myServer->getJurisdiction());
@ -398,60 +326,79 @@ int OctreeSendThread::packetDistributor(const SharedNodePointer& node, OctreeQue
if (!nodeData->nodeBag.isEmpty()) {
int bytesWritten = 0;
quint64 start = usecTimestampNow();
quint64 startCompressTimeMsecs = OctreePacketData::getCompressContentTime() / 1000;
quint64 startCompressCalls = OctreePacketData::getCompressContentCalls();
// TODO: add these to stats page
//quint64 startCompressTimeMsecs = OctreePacketData::getCompressContentTime() / 1000;
//quint64 startCompressCalls = OctreePacketData::getCompressContentCalls();
int clientMaxPacketsPerInterval = std::max(1,(nodeData->getMaxOctreePacketsPerSecond() / INTERVALS_PER_SECOND));
int maxPacketsPerInterval = std::min(clientMaxPacketsPerInterval, _myServer->getPacketsPerClientPerInterval());
if (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug()) {
qDebug("truePacketsSent=%d packetsSentThisInterval=%d maxPacketsPerInterval=%d server PPI=%d nodePPS=%d nodePPI=%d",
truePacketsSent, packetsSentThisInterval, maxPacketsPerInterval, _myServer->getPacketsPerClientPerInterval(),
nodeData->getMaxOctreePacketsPerSecond(), clientMaxPacketsPerInterval);
}
int extraPackingAttempts = 0;
bool completedScene = false;
while (somethingToSend && packetsSentThisInterval < maxPacketsPerInterval) {
if (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug()) {
qDebug("truePacketsSent=%d packetsSentThisInterval=%d maxPacketsPerInterval=%d server PPI=%d nodePPS=%d nodePPI=%d",
truePacketsSent, packetsSentThisInterval, maxPacketsPerInterval, _myServer->getPacketsPerClientPerInterval(),
nodeData->getMaxOctreePacketsPerSecond(), clientMaxPacketsPerInterval);
}
float lockWaitElapsedUsec = OctreeServer::SKIP_TIME;
float encodeElapsedUsec = OctreeServer::SKIP_TIME;
float compressAndWriteElapsedUsec = OctreeServer::SKIP_TIME;
float packetSendingElapsedUsec = OctreeServer::SKIP_TIME;
quint64 startInside = usecTimestampNow();
bool lastNodeDidntFit = false; // assume each node fits
if (!nodeData->nodeBag.isEmpty()) {
OctreeElement* subTree = nodeData->nodeBag.extract();
/* TODO: Looking for a way to prevent locking and encoding a tree that is not
// going to result in any packets being sent...
//
// If our node is root, and the root hasn't changed, and our view hasn't changed,
// and we've already seen at least one duplicate packet, then we probably don't need
// to lock the tree and encode, because the result should be that no bytes will be
// encoded, and this will be a duplicate packet from the last one we sent...
OctreeElement* root = _myServer->getOctree()->getRoot();
bool skipEncode = false;
if (
(subTree == root)
&& (nodeData->getLastRootTimestamp() == root->getLastChanged())
&& !viewFrustumChanged
&& (nodeData->getDuplicatePacketCount() > 0)
) {
qDebug() << "is root, root not changed, view not changed, already seen a duplicate!"
<< "Can we skip it?";
skipEncode = true;
}
*/
bool wantOcclusionCulling = nodeData->getWantOcclusionCulling();
CoverageMap* coverageMap = wantOcclusionCulling ? &nodeData->map : IGNORE_COVERAGE_MAP;
float voxelSizeScale = nodeData->getOctreeSizeScale();
int boundaryLevelAdjustClient = nodeData->getBoundaryLevelAdjust();
int boundaryLevelAdjust = boundaryLevelAdjustClient + (viewFrustumChanged && nodeData->getWantLowResMoving()
? LOW_RES_MOVING_ADJUST : NO_BOUNDARY_ADJUST);
? LOW_RES_MOVING_ADJUST : NO_BOUNDARY_ADJUST);
EncodeBitstreamParams params(INT_MAX, &nodeData->getCurrentViewFrustum(), wantColor,
WANT_EXISTS_BITS, DONT_CHOP, wantDelta, lastViewFrustum,
wantOcclusionCulling, coverageMap, boundaryLevelAdjust, voxelSizeScale,
nodeData->getLastTimeBagEmpty(),
isFullScene, &nodeData->stats, _myServer->getJurisdiction());
// TODO: should this include the lock time or not? This stat is sent down to the client,
// it seems like it may be a good idea to include the lock time as part of the encode time
// are reported to client. Since you can encode without the lock
nodeData->stats.encodeStarted();
quint64 lockWaitStart = usecTimestampNow();
_myServer->getOctree()->lockForRead();
quint64 lockWaitEnd = usecTimestampNow();
float lockWaitElapsedUsec = (float)(lockWaitEnd - lockWaitStart);
OctreeServer::trackTreeWaitTime(lockWaitElapsedUsec);
nodeData->stats.encodeStarted();
lockWaitElapsedUsec = (float)(lockWaitEnd - lockWaitStart);
quint64 encodeStart = usecTimestampNow();
bytesWritten = _myServer->getOctree()->encodeTreeBitstream(subTree, &_packetData, nodeData->nodeBag, params);
quint64 encodeEnd = usecTimestampNow();
int encodeElapsedMsec = (encodeEnd - encodeStart)/USECS_PER_MSEC;
OctreeServer::trackEncodeTime(encodeElapsedMsec);
encodeElapsedUsec = (float)(encodeEnd - encodeStart);
// If after calling encodeTreeBitstream() there are no nodes left to send, then we know we've
// sent the entire scene. We want to know this below so we'll actually write this content into
// the packet and send it
@ -490,6 +437,9 @@ int OctreeSendThread::packetDistributor(const SharedNodePointer& node, OctreeQue
// mean we should send the previous packet contents and reset it.
if (completedScene || lastNodeDidntFit) {
if (_packetData.hasContent()) {
quint64 compressAndWriteStart = usecTimestampNow();
// if for some reason the finalized size is greater than our available size, then probably the "compressed"
// form actually inflated beyond our padding, and in this case we will send the current packet, then
// write to out new packet...
@ -498,27 +448,19 @@ int OctreeSendThread::packetDistributor(const SharedNodePointer& node, OctreeQue
if (writtenSize > nodeData->getAvailable()) {
if (forceDebugging || (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug())) {
qDebug("about to call handlePacketSend() .... line: %d -- "
"writtenSize[%d] > available[%d] too big, sending packet as is.",
__LINE__, writtenSize, nodeData->getAvailable());
}
packetsSentThisInterval += handlePacketSend(node, nodeData, trueBytesSent, truePacketsSent);
}
if (forceDebugging || (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug())) {
qDebug(">>>>>> calling writeToPacket() available=%d compressedSize=%d uncompressedSize=%d target=%u",
nodeData->getAvailable(), _packetData.getFinalizedSize(),
_packetData.getUncompressedSize(), _packetData.getTargetSize());
}
nodeData->writeToPacket(_packetData.getFinalizedData(), _packetData.getFinalizedSize());
extraPackingAttempts = 0;
quint64 compressAndWriteEnd = usecTimestampNow();
compressAndWriteElapsedUsec = (float)(compressAndWriteEnd - compressAndWriteStart);
}
// If we're not running compressed, then we know we can just send now. Or if we're running compressed, but
// the packet doesn't have enough space to bother attempting to pack more...
bool sendNow = true;
if (nodeData->getCurrentPacketIsCompressed() &&
nodeData->getAvailable() >= MINIMUM_ATTEMPT_MORE_PACKING &&
extraPackingAttempts <= REASONABLE_NUMBER_OF_PACKING_ATTEMPTS) {
@ -527,10 +469,11 @@ int OctreeSendThread::packetDistributor(const SharedNodePointer& node, OctreeQue
int targetSize = MAX_OCTREE_PACKET_DATA_SIZE;
if (sendNow) {
if (forceDebugging) {
qDebug("about to call handlePacketSend() .... line: %d -- sendNow = TRUE", __LINE__);
}
quint64 packetSendingStart = usecTimestampNow();
packetsSentThisInterval += handlePacketSend(node, nodeData, trueBytesSent, truePacketsSent);
quint64 packetSendingEnd = usecTimestampNow();
packetSendingElapsedUsec = (float)(packetSendingEnd - packetSendingStart);
if (wantCompression) {
targetSize = nodeData->getAvailable() - sizeof(OCTREE_PACKET_INTERNAL_SECTION_SIZE);
}
@ -543,12 +486,17 @@ int OctreeSendThread::packetDistributor(const SharedNodePointer& node, OctreeQue
// a larger compressed size then uncompressed size
targetSize = nodeData->getAvailable() - sizeof(OCTREE_PACKET_INTERNAL_SECTION_SIZE) - COMPRESS_PADDING;
}
if (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug()) {
qDebug("line:%d _packetData.changeSettings() wantCompression=%s targetSize=%d",__LINE__,
debug::valueOf(nodeData->getWantCompression()), targetSize);
}
_packetData.changeSettings(nodeData->getWantCompression(), targetSize); // will do reset
}
OctreeServer::trackTreeWaitTime(lockWaitElapsedUsec);
OctreeServer::trackEncodeTime(encodeElapsedUsec);
OctreeServer::trackCompressAndWriteTime(compressAndWriteElapsedUsec);
OctreeServer::trackPacketSendingTime(packetSendingElapsedUsec);
quint64 endInside = usecTimestampNow();
quint64 elapsedInsideUsecs = endInside - startInside;
OctreeServer::trackInsideTime((float)elapsedInsideUsecs);
}
@ -566,53 +514,21 @@ int OctreeSendThread::packetDistributor(const SharedNodePointer& node, OctreeQue
int elapsedmsec = (end - start)/USECS_PER_MSEC;
OctreeServer::trackLoopTime(elapsedmsec);
quint64 endCompressCalls = OctreePacketData::getCompressContentCalls();
int elapsedCompressCalls = endCompressCalls - startCompressCalls;
quint64 endCompressTimeMsecs = OctreePacketData::getCompressContentTime() / 1000;
int elapsedCompressTimeMsecs = endCompressTimeMsecs - startCompressTimeMsecs;
if (elapsedmsec > 100) {
if (elapsedmsec > 1000) {
int elapsedsec = (end - start)/1000000;
qDebug("WARNING! packetLoop() took %d seconds [%d milliseconds %d calls in compress] "
"to generate %d bytes in %d packets %d nodes still to send",
elapsedsec, elapsedCompressTimeMsecs, elapsedCompressCalls,
trueBytesSent, truePacketsSent, nodeData->nodeBag.count());
} else {
qDebug("WARNING! packetLoop() took %d milliseconds [%d milliseconds %d calls in compress] "
"to generate %d bytes in %d packets, %d nodes still to send",
elapsedmsec, elapsedCompressTimeMsecs, elapsedCompressCalls,
trueBytesSent, truePacketsSent, nodeData->nodeBag.count());
}
} else if (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug()) {
qDebug("packetLoop() took %d milliseconds [%d milliseconds %d calls in compress] "
"to generate %d bytes in %d packets, %d nodes still to send",
elapsedmsec, elapsedCompressTimeMsecs, elapsedCompressCalls,
trueBytesSent, truePacketsSent, nodeData->nodeBag.count());
}
// TODO: add these to stats page
//quint64 endCompressCalls = OctreePacketData::getCompressContentCalls();
//int elapsedCompressCalls = endCompressCalls - startCompressCalls;
//quint64 endCompressTimeMsecs = OctreePacketData::getCompressContentTime() / 1000;
//int elapsedCompressTimeMsecs = endCompressTimeMsecs - startCompressTimeMsecs;
// if after sending packets we've emptied our bag, then we want to remember that we've sent all
// the voxels from the current view frustum
if (nodeData->nodeBag.isEmpty()) {
nodeData->updateLastKnownViewFrustum();
nodeData->setViewSent(true);
if (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug()) {
nodeData->map.printStats();
}
nodeData->map.erase(); // It would be nice if we could save this, and only reset it when the view frustum changes
}
if (_myServer->wantsDebugSending() && _myServer->wantsVerboseDebug()) {
qDebug("truePacketsSent=%d packetsSentThisInterval=%d maxPacketsPerInterval=%d "
"server PPI=%d nodePPS=%d nodePPI=%d",
truePacketsSent, packetsSentThisInterval, maxPacketsPerInterval,
_myServer->getPacketsPerClientPerInterval(), nodeData->getMaxOctreePacketsPerSecond(),
clientMaxPacketsPerInterval);
}
} // end if bag wasn't empty, and so we sent stuff...
return truePacketsSent;
}

View file

@ -19,6 +19,7 @@
/// Threaded processor for sending voxel packets to a single client
class OctreeSendThread : public GenericThread {
Q_OBJECT
public:
OctreeSendThread(const QUuid& nodeUUID, OctreeServer* myServer);
virtual ~OctreeSendThread();

View file

@ -20,10 +20,150 @@
OctreeServer* OctreeServer::_instance = NULL;
int OctreeServer::_clientCount = 0;
SimpleMovingAverage OctreeServer::_averageLoopTime(10000);
SimpleMovingAverage OctreeServer::_averageEncodeTime(10000);
SimpleMovingAverage OctreeServer::_averageTreeWaitTime(10000);
SimpleMovingAverage OctreeServer::_averageNodeWaitTime(10000);
const int MOVING_AVERAGE_SAMPLE_COUNTS = 1000000;
float OctreeServer::SKIP_TIME = -1.0f; // use this for trackXXXTime() calls for non-times
SimpleMovingAverage OctreeServer::_averageLoopTime(MOVING_AVERAGE_SAMPLE_COUNTS);
SimpleMovingAverage OctreeServer::_averageInsideTime(MOVING_AVERAGE_SAMPLE_COUNTS);
SimpleMovingAverage OctreeServer::_averageEncodeTime(MOVING_AVERAGE_SAMPLE_COUNTS);
SimpleMovingAverage OctreeServer::_averageShortEncodeTime(MOVING_AVERAGE_SAMPLE_COUNTS);
SimpleMovingAverage OctreeServer::_averageLongEncodeTime(MOVING_AVERAGE_SAMPLE_COUNTS);
SimpleMovingAverage OctreeServer::_averageExtraLongEncodeTime(MOVING_AVERAGE_SAMPLE_COUNTS);
int OctreeServer::_extraLongEncode = 0;
int OctreeServer::_longEncode = 0;
int OctreeServer::_shortEncode = 0;
int OctreeServer::_noEncode = 0;
SimpleMovingAverage OctreeServer::_averageTreeWaitTime(MOVING_AVERAGE_SAMPLE_COUNTS);
SimpleMovingAverage OctreeServer::_averageTreeShortWaitTime(MOVING_AVERAGE_SAMPLE_COUNTS);
SimpleMovingAverage OctreeServer::_averageTreeLongWaitTime(MOVING_AVERAGE_SAMPLE_COUNTS);
SimpleMovingAverage OctreeServer::_averageTreeExtraLongWaitTime(MOVING_AVERAGE_SAMPLE_COUNTS);
int OctreeServer::_extraLongTreeWait = 0;
int OctreeServer::_longTreeWait = 0;
int OctreeServer::_shortTreeWait = 0;
int OctreeServer::_noTreeWait = 0;
SimpleMovingAverage OctreeServer::_averageNodeWaitTime(MOVING_AVERAGE_SAMPLE_COUNTS);
SimpleMovingAverage OctreeServer::_averageCompressAndWriteTime(MOVING_AVERAGE_SAMPLE_COUNTS);
SimpleMovingAverage OctreeServer::_averageShortCompressTime(MOVING_AVERAGE_SAMPLE_COUNTS);
SimpleMovingAverage OctreeServer::_averageLongCompressTime(MOVING_AVERAGE_SAMPLE_COUNTS);
SimpleMovingAverage OctreeServer::_averageExtraLongCompressTime(MOVING_AVERAGE_SAMPLE_COUNTS);
int OctreeServer::_extraLongCompress = 0;
int OctreeServer::_longCompress = 0;
int OctreeServer::_shortCompress = 0;
int OctreeServer::_noCompress = 0;
SimpleMovingAverage OctreeServer::_averagePacketSendingTime(MOVING_AVERAGE_SAMPLE_COUNTS);
int OctreeServer::_noSend = 0;
void OctreeServer::resetSendingStats() {
_averageLoopTime.reset();
_averageEncodeTime.reset();
_averageShortEncodeTime.reset();
_averageLongEncodeTime.reset();
_averageExtraLongEncodeTime.reset();
_extraLongEncode = 0;
_longEncode = 0;
_shortEncode = 0;
_noEncode = 0;
_averageInsideTime.reset();
_averageTreeWaitTime.reset();
_averageTreeShortWaitTime.reset();
_averageTreeLongWaitTime.reset();
_averageTreeExtraLongWaitTime.reset();
_extraLongTreeWait = 0;
_longTreeWait = 0;
_shortTreeWait = 0;
_noTreeWait = 0;
_averageNodeWaitTime.reset();
_averageCompressAndWriteTime.reset();
_averageShortCompressTime.reset();
_averageLongCompressTime.reset();
_averageExtraLongCompressTime.reset();
_extraLongCompress = 0;
_longCompress = 0;
_shortCompress = 0;
_noCompress = 0;
_averagePacketSendingTime.reset();
_noSend = 0;
}
void OctreeServer::trackEncodeTime(float time) {
const float MAX_SHORT_TIME = 10.0f;
const float MAX_LONG_TIME = 100.0f;
if (time == SKIP_TIME) {
_noEncode++;
time = 0.0f;
} else if (time <= MAX_SHORT_TIME) {
_shortEncode++;
_averageShortEncodeTime.updateAverage(time);
} else if (time <= MAX_LONG_TIME) {
_longEncode++;
_averageLongEncodeTime.updateAverage(time);
} else {
_extraLongEncode++;
_averageExtraLongEncodeTime.updateAverage(time);
}
_averageEncodeTime.updateAverage(time);
}
void OctreeServer::trackTreeWaitTime(float time) {
const float MAX_SHORT_TIME = 10.0f;
const float MAX_LONG_TIME = 100.0f;
if (time == SKIP_TIME) {
_noTreeWait++;
time = 0.0f;
} else if (time <= MAX_SHORT_TIME) {
_shortTreeWait++;
_averageTreeShortWaitTime.updateAverage(time);
} else if (time <= MAX_LONG_TIME) {
_longTreeWait++;
_averageTreeLongWaitTime.updateAverage(time);
} else {
_extraLongTreeWait++;
_averageTreeExtraLongWaitTime.updateAverage(time);
}
_averageTreeWaitTime.updateAverage(time);
}
void OctreeServer::trackCompressAndWriteTime(float time) {
const float MAX_SHORT_TIME = 10.0f;
const float MAX_LONG_TIME = 100.0f;
if (time == SKIP_TIME) {
_noCompress++;
time = 0.0f;
} else if (time <= MAX_SHORT_TIME) {
_shortCompress++;
_averageShortCompressTime.updateAverage(time);
} else if (time <= MAX_LONG_TIME) {
_longCompress++;
_averageLongCompressTime.updateAverage(time);
} else {
_extraLongCompress++;
_averageExtraLongCompressTime.updateAverage(time);
}
_averageCompressAndWriteTime.updateAverage(time);
}
void OctreeServer::trackPacketSendingTime(float time) {
if (time == SKIP_TIME) {
_noSend++;
time = 0.0f;
}
_averagePacketSendingTime.updateAverage(time);
}
void OctreeServer::attachQueryNodeToNode(Node* newNode) {
if (!newNode->getLinkedData()) {
@ -55,9 +195,11 @@ OctreeServer::OctreeServer(const QByteArray& packet) :
{
_instance = this;
_averageLoopTime.updateAverage(0);
qDebug() << "Octree server starting... [" << this << "]";
}
OctreeServer::~OctreeServer() {
qDebug() << qPrintable(_safeServerName) << "server shutting down... [" << this << "]";
if (_parsedArgV) {
for (int i = 0; i < _argc; i++) {
delete[] _parsedArgV[i];
@ -82,7 +224,7 @@ OctreeServer::~OctreeServer() {
delete _jurisdiction;
_jurisdiction = NULL;
qDebug() << "OctreeServer::~OctreeServer()... DONE";
qDebug() << qPrintable(_safeServerName) << "server DONE shutting down... [" << this << "]";
}
void OctreeServer::initHTTPManager(int port) {
@ -121,6 +263,7 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QString&
showStats = true;
} else if (path == "/resetStats") {
_octreeInboundPacketProcessor->resetStats();
resetSendingStats();
showStats = true;
}
}
@ -255,7 +398,9 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QString&
statsString += "\r\n";
// display outbound packet stats
statsString += QString("<b>%1 Outbound Packet Statistics...</b>\r\n").arg(getMyServerName());
statsString += QString("<b>%1 Outbound Packet Statistics... "
"<a href='/resetStats'>[RESET]</a></b>\r\n").arg(getMyServerName());
quint64 totalOutboundPackets = OctreeSendThread::_totalPackets;
quint64 totalOutboundBytes = OctreeSendThread::_totalBytes;
quint64 totalWastedBytes = OctreeSendThread::_totalWastedBytes;
@ -263,7 +408,7 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QString&
quint64 totalBytesOfBitMasks = OctreePacketData::getTotalBytesOfBitMasks();
quint64 totalBytesOfColor = OctreePacketData::getTotalBytesOfColor();
const int COLUMN_WIDTH = 10;
const int COLUMN_WIDTH = 19;
statsString += QString(" Configured Max PPS/Client: %1 pps/client\r\n")
.arg(locale.toString((uint)getPacketsPerClientPerSecond()).rightJustified(COLUMN_WIDTH, ' '));
statsString += QString(" Configured Max PPS/Server: %1 pps/server\r\n\r\n")
@ -272,21 +417,130 @@ bool OctreeServer::handleHTTPRequest(HTTPConnection* connection, const QString&
.arg(locale.toString((uint)getCurrentClientCount()).rightJustified(COLUMN_WIDTH, ' '));
float averageLoopTime = getAverageLoopTime();
statsString += QString().sprintf(" Average packetLoop() time: %5.2f msecs\r\n", averageLoopTime);
qDebug() << "averageLoopTime=" << averageLoopTime;
statsString += QString().sprintf(" Average packetLoop() time: %7.2f msecs\r\n", averageLoopTime);
float averageEncodeTime = getAverageEncodeTime();
statsString += QString().sprintf(" Average encode time: %5.2f msecs\r\n", averageEncodeTime);
qDebug() << "averageEncodeTime=" << averageEncodeTime;
float averageInsideTime = getAverageInsideTime();
statsString += QString().sprintf(" Average 'inside' time: %9.2f usecs\r\n\r\n", averageInsideTime);
int allWaitTimes = _extraLongTreeWait +_longTreeWait + _shortTreeWait + _noTreeWait;
float averageTreeWaitTime = getAverageTreeWaitTime();
statsString += QString().sprintf(" Average tree lock wait time: %7.2f usecs\r\n", averageTreeWaitTime);
qDebug() << "averageTreeWaitTime=" << averageTreeWaitTime;
statsString += QString().sprintf(" Average tree lock wait time:"
" %9.2f usecs samples: %12d \r\n",
averageTreeWaitTime, allWaitTimes);
float zeroVsTotal = (allWaitTimes > 0) ? ((float)_noTreeWait / (float)allWaitTimes) : 0.0f;
statsString += QString().sprintf(" No Lock Wait:"
" (%6.2f%%) samples: %12d \r\n",
zeroVsTotal * AS_PERCENT, _noTreeWait);
float shortVsTotal = (allWaitTimes > 0) ? ((float)_shortTreeWait / (float)allWaitTimes) : 0.0f;
statsString += QString().sprintf(" Avg tree lock short wait time:"
" %9.2f usecs (%6.2f%%) samples: %12d \r\n",
_averageTreeShortWaitTime.getAverage(),
shortVsTotal * AS_PERCENT, _shortTreeWait);
float longVsTotal = (allWaitTimes > 0) ? ((float)_longTreeWait / (float)allWaitTimes) : 0.0f;
statsString += QString().sprintf(" Avg tree lock long wait time:"
" %9.2f usecs (%6.2f%%) samples: %12d \r\n",
_averageTreeLongWaitTime.getAverage(),
longVsTotal * AS_PERCENT, _longTreeWait);
float extraLongVsTotal = (allWaitTimes > 0) ? ((float)_extraLongTreeWait / (float)allWaitTimes) : 0.0f;
statsString += QString().sprintf(" Avg tree lock extra long wait time:"
" %9.2f usecs (%6.2f%%) samples: %12d \r\n\r\n",
_averageTreeExtraLongWaitTime.getAverage(),
extraLongVsTotal * AS_PERCENT, _extraLongTreeWait);
float averageEncodeTime = getAverageEncodeTime();
statsString += QString().sprintf(" Average encode time: %9.2f usecs\r\n", averageEncodeTime);
int allEncodeTimes = _noEncode + _shortEncode + _longEncode + _extraLongEncode;
float zeroVsTotalEncode = (allEncodeTimes > 0) ? ((float)_noEncode / (float)allEncodeTimes) : 0.0f;
statsString += QString().sprintf(" No Encode:"
" (%6.2f%%) samples: %12d \r\n",
zeroVsTotalEncode * AS_PERCENT, _noEncode);
float shortVsTotalEncode = (allEncodeTimes > 0) ? ((float)_shortEncode / (float)allEncodeTimes) : 0.0f;
statsString += QString().sprintf(" Avg short encode time:"
" %9.2f usecs (%6.2f%%) samples: %12d \r\n",
_averageShortEncodeTime.getAverage(),
shortVsTotalEncode * AS_PERCENT, _shortEncode);
float longVsTotalEncode = (allEncodeTimes > 0) ? ((float)_longEncode / (float)allEncodeTimes) : 0.0f;
statsString += QString().sprintf(" Avg long encode time:"
" %9.2f usecs (%6.2f%%) samples: %12d \r\n",
_averageLongEncodeTime.getAverage(),
longVsTotalEncode * AS_PERCENT, _longEncode);
float extraLongVsTotalEncode = (allEncodeTimes > 0) ? ((float)_extraLongEncode / (float)allEncodeTimes) : 0.0f;
statsString += QString().sprintf(" Avg extra long encode time:"
" %9.2f usecs (%6.2f%%) samples: %12d \r\n\r\n",
_averageExtraLongEncodeTime.getAverage(),
extraLongVsTotalEncode * AS_PERCENT, _extraLongEncode);
float averageCompressAndWriteTime = getAverageCompressAndWriteTime();
statsString += QString().sprintf(" Average compress and write time: %9.2f usecs\r\n", averageCompressAndWriteTime);
int allCompressTimes = _noCompress + _shortCompress + _longCompress + _extraLongCompress;
float zeroVsTotalCompress = (allCompressTimes > 0) ? ((float)_noCompress / (float)allCompressTimes) : 0.0f;
statsString += QString().sprintf(" No compression:"
" (%6.2f%%) samples: %12d \r\n",
zeroVsTotalCompress * AS_PERCENT, _noCompress);
float shortVsTotalCompress = (allCompressTimes > 0) ? ((float)_shortCompress / (float)allCompressTimes) : 0.0f;
statsString += QString().sprintf(" Avg short compress time:"
" %9.2f usecs (%6.2f%%) samples: %12d \r\n",
_averageShortCompressTime.getAverage(),
shortVsTotalCompress * AS_PERCENT, _shortCompress);
float longVsTotalCompress = (allCompressTimes > 0) ? ((float)_longCompress / (float)allCompressTimes) : 0.0f;
statsString += QString().sprintf(" Avg long compress time:"
" %9.2f usecs (%6.2f%%) samples: %12d \r\n",
_averageLongCompressTime.getAverage(),
longVsTotalCompress * AS_PERCENT, _longCompress);
float extraLongVsTotalCompress = (allCompressTimes > 0) ? ((float)_extraLongCompress / (float)allCompressTimes) : 0.0f;
statsString += QString().sprintf(" Avg extra long compress time:"
" %9.2f usecs (%6.2f%%) samples: %12d \r\n\r\n",
_averageExtraLongCompressTime.getAverage(),
extraLongVsTotalCompress * AS_PERCENT, _extraLongCompress);
float averagePacketSendingTime = getAveragePacketSendingTime();
statsString += QString().sprintf(" Average packet sending time: %9.2f usecs (includes node lock)\r\n",
averagePacketSendingTime);
float noVsTotalSend = (_averagePacketSendingTime.getSampleCount() > 0) ?
((float)_noSend / (float)_averagePacketSendingTime.getSampleCount()) : 0.0f;
statsString += QString().sprintf(" Not sending:"
" (%6.2f%%) samples: %12d \r\n",
noVsTotalSend * AS_PERCENT, _noSend);
float averageNodeWaitTime = getAverageNodeWaitTime();
statsString += QString().sprintf(" Average node lock wait time: %7.2f usecs\r\n", averageNodeWaitTime);
qDebug() << "averageNodeWaitTime=" << averageNodeWaitTime;
statsString += QString().sprintf(" Average node lock wait time: %9.2f usecs\r\n", averageNodeWaitTime);
statsString += QString().sprintf("--------------------------------------------------------------\r\n");
float encodeToInsidePercent = averageInsideTime == 0.0f ? 0.0f : (averageEncodeTime / averageInsideTime) * AS_PERCENT;
statsString += QString().sprintf(" encode ratio: %5.2f%%\r\n",
encodeToInsidePercent);
float waitToInsidePercent = averageInsideTime == 0.0f ? 0.0f
: ((averageTreeWaitTime + averageNodeWaitTime) / averageInsideTime) * AS_PERCENT;
statsString += QString().sprintf(" waiting ratio: %5.2f%%\r\n", waitToInsidePercent);
float compressAndWriteToInsidePercent = averageInsideTime == 0.0f ? 0.0f
: (averageCompressAndWriteTime / averageInsideTime) * AS_PERCENT;
statsString += QString().sprintf(" compress and write ratio: %5.2f%%\r\n",
compressAndWriteToInsidePercent);
float sendingToInsidePercent = averageInsideTime == 0.0f ? 0.0f
: (averagePacketSendingTime / averageInsideTime) * AS_PERCENT;
statsString += QString().sprintf(" sending ratio: %5.2f%%\r\n", sendingToInsidePercent);
statsString += QString("\r\n");
@ -556,6 +810,7 @@ void OctreeServer::readPendingDatagrams() {
}
void OctreeServer::run() {
_safeServerName = getMyServerName();
// Before we do anything else, create our tree...
_tree = createTree();
@ -611,6 +866,7 @@ void OctreeServer::run() {
connect(nodeList, SIGNAL(nodeAdded(SharedNodePointer)), SLOT(nodeAdded(SharedNodePointer)));
connect(nodeList, SIGNAL(nodeKilled(SharedNodePointer)),SLOT(nodeKilled(SharedNodePointer)));
// we need to ask the DS about agents so we can ping/reply with them
nodeList->addNodeTypeToInterestSet(NodeType::Agent);
@ -732,17 +988,26 @@ void OctreeServer::run() {
void OctreeServer::nodeAdded(SharedNodePointer node) {
// we might choose to use this notifier to track clients in a pending state
qDebug() << qPrintable(_safeServerName) << "server added node:" << *node;
}
void OctreeServer::nodeKilled(SharedNodePointer node) {
qDebug() << qPrintable(_safeServerName) << "server killed node:" << *node;
OctreeQueryNode* nodeData = static_cast<OctreeQueryNode*>(node->getLinkedData());
if (nodeData) {
// Note: It should be safe to do this without locking the node, because if any other threads
// are using the SharedNodePointer, then they have a reference to the SharedNodePointer and the deleteLater()
// won't actually delete it until all threads have released their references to the pointer.
// But we can and should clear the linked data so that no one else tries to access it.
qDebug() << qPrintable(_safeServerName) << "server resetting Linked Data for node:" << *node;
node->setLinkedData(NULL); // set this first in case another thread comes through and tryes to acces this
qDebug() << qPrintable(_safeServerName) << "server deleting Linked Data for node:" << *node;
nodeData->deleteLater();
node->setLinkedData(NULL);
}
}
void OctreeServer::aboutToFinish() {
qDebug() << qPrintable(_safeServerName) << "server STARTING about to finish...";
foreach (const SharedNodePointer& node, NodeList::getInstance()->getNodeHash()) {
qDebug() << qPrintable(_safeServerName) << "server about to finish while node still connected node:" << *node;
nodeKilled(node);
}
qDebug() << qPrintable(_safeServerName) << "server ENDING about to finish...";
}

View file

@ -73,19 +73,34 @@ public:
virtual int sendSpecialPacket(const SharedNodePointer& node) { return 0; }
static void attachQueryNodeToNode(Node* newNode);
static float SKIP_TIME; // use this for trackXXXTime() calls for non-times
static void trackLoopTime(float time) { _averageLoopTime.updateAverage(time); }
static float getAverageLoopTime() { return _averageLoopTime.getAverage(); }
static void trackEncodeTime(float time) { _averageEncodeTime.updateAverage(time); }
static void trackEncodeTime(float time);
static float getAverageEncodeTime() { return _averageEncodeTime.getAverage(); }
static void trackTreeWaitTime(float time) { _averageTreeWaitTime.updateAverage(time); }
static void trackInsideTime(float time) { _averageInsideTime.updateAverage(time); }
static float getAverageInsideTime() { return _averageInsideTime.getAverage(); }
static void trackTreeWaitTime(float time);
static float getAverageTreeWaitTime() { return _averageTreeWaitTime.getAverage(); }
static void trackNodeWaitTime(float time) { _averageNodeWaitTime.updateAverage(time); }
static float getAverageNodeWaitTime() { return _averageNodeWaitTime.getAverage(); }
static void trackCompressAndWriteTime(float time);
static float getAverageCompressAndWriteTime() { return _averageCompressAndWriteTime.getAverage(); }
static void trackPacketSendingTime(float time);
static float getAveragePacketSendingTime() { return _averagePacketSendingTime.getAverage(); }
bool handleHTTPRequest(HTTPConnection* connection, const QString& path);
virtual void aboutToFinish();
public slots:
/// runs the voxel server assignment
void run();
@ -96,6 +111,7 @@ public slots:
protected:
void parsePayload();
void initHTTPManager(int port);
void resetSendingStats();
int _argc;
const char** _argv;
@ -120,12 +136,44 @@ protected:
time_t _started;
quint64 _startedUSecs;
QString _safeServerName;
static int _clientCount;
static SimpleMovingAverage _averageLoopTime;
static SimpleMovingAverage _averageEncodeTime;
static SimpleMovingAverage _averageShortEncodeTime;
static SimpleMovingAverage _averageLongEncodeTime;
static SimpleMovingAverage _averageExtraLongEncodeTime;
static int _extraLongEncode;
static int _longEncode;
static int _shortEncode;
static int _noEncode;
static SimpleMovingAverage _averageInsideTime;
static SimpleMovingAverage _averageTreeWaitTime;
static SimpleMovingAverage _averageTreeShortWaitTime;
static SimpleMovingAverage _averageTreeLongWaitTime;
static SimpleMovingAverage _averageTreeExtraLongWaitTime;
static int _extraLongTreeWait;
static int _longTreeWait;
static int _shortTreeWait;
static int _noTreeWait;
static SimpleMovingAverage _averageNodeWaitTime;
static SimpleMovingAverage _averageCompressAndWriteTime;
static SimpleMovingAverage _averageShortCompressTime;
static SimpleMovingAverage _averageLongCompressTime;
static SimpleMovingAverage _averageExtraLongCompressTime;
static int _extraLongCompress;
static int _longCompress;
static int _shortCompress;
static int _noCompress;
static SimpleMovingAverage _averagePacketSendingTime;
static int _noSend;
};
#endif // __octree_server__OctreeServer__

View file

@ -29,7 +29,6 @@ var CHANCE_OF_BIG_MOVE = 0.1;
var isMoving = false;
var isTurningHead = false;
var isPlayingAudio = false;
var X_MIN = 0.0;
var X_MAX = 5.0;
@ -60,20 +59,11 @@ function clamp(val, min, max){
}
// Play a random sound from a list of conversational audio clips
function audioDone() {
isPlayingAudio = false;
}
var AVERAGE_AUDIO_LENGTH = 8000;
function playRandomSound(position) {
if (!isPlayingAudio) {
function playRandomSound() {
if (!Agent.isPlayingAvatarSound) {
var whichSound = Math.floor((Math.random() * sounds.length) % sounds.length);
var audioOptions = new AudioInjectionOptions();
audioOptions.volume = 0.25 + (Math.random() * 0.75);
audioOptions.position = position;
Audio.playSound(sounds[whichSound], audioOptions);
isPlayingAudio = true;
Script.setTimeout(audioDone, AVERAGE_AUDIO_LENGTH);
Agent.playAvatarSound(sounds[whichSound]);
}
}
@ -104,6 +94,7 @@ Avatar.skeletonModelURL = "https://s3-us-west-1.amazonaws.com/highfidelity-publi
Avatar.billboardURL = "https://s3-us-west-1.amazonaws.com/highfidelity-public/meshes/billboards/bot" + botNumber + ".png";
Agent.isAvatar = true;
Agent.isListeningToAudioStream = true;
// change the avatar's position to the random one
Avatar.position = firstPosition;
@ -111,10 +102,10 @@ printVector("New bot, position = ", Avatar.position);
function updateBehavior(deltaTime) {
if (Math.random() < CHANCE_OF_SOUND) {
playRandomSound(Avatar.position);
playRandomSound();
}
if (isPlayingAudio) {
if (Agent.isPlayingAvatarSound) {
Avatar.handPosition = Vec3.sum(Avatar.position, Quat.getFront(Avatar.orientation));
}

View file

@ -13,18 +13,23 @@ var yawDirection = -1;
var yaw = 45;
var yawMax = 70;
var yawMin = 20;
var vantagePoint = {x: 5000, y: 500, z: 5000};
var isLocal = false;
// set up our VoxelViewer with a position and orientation
var orientation = Quat.fromPitchYawRollDegrees(0, yaw, 0);
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function init() {
if (isLocal) {
MyAvatar.position = {x: 5000, y: 500, z: 5000};
MyAvatar.position = vantagePoint;
MyAvatar.orientation = orientation;
} else {
VoxelViewer.setPosition({x: 5000, y: 500, z: 5000});
VoxelViewer.setPosition(vantagePoint);
VoxelViewer.setOrientation(orientation);
VoxelViewer.queryOctree();
Agent.isAvatar = true;
@ -38,21 +43,26 @@ function keepLooking(deltaTime) {
init();
}
count++;
if (count % 10 == 0) {
if (count % getRandomInt(5, 15) == 0) {
yaw += yawDirection;
orientation = Quat.fromPitchYawRollDegrees(0, yaw, 0);
if (yaw > yawMax || yaw < yawMin) {
yawDirection = yawDirection * -1;
}
print("calling VoxelViewer.queryOctree()... count=" + count + " yaw=" + yaw);
if (count % 10000 == 0) {
print("calling VoxelViewer.queryOctree()... count=" + count + " yaw=" + yaw);
}
if (isLocal) {
MyAvatar.orientation = orientation;
} else {
VoxelViewer.setOrientation(orientation);
VoxelViewer.queryOctree();
print("VoxelViewer.getOctreeElementsCount()=" + VoxelViewer.getOctreeElementsCount());
if (count % 10000 == 0) {
print("VoxelViewer.getOctreeElementsCount()=" + VoxelViewer.getOctreeElementsCount());
}
}
}

View file

@ -40,7 +40,7 @@ detect_strip_roi_width 2
detect_strip_roi_height 4
smoothing_factors
150 15 -2 100 -1 50 50 0
150 5 -2 100 -1 50 50 0
#translation rotation action_units eyebrows mouth gaze eye_closure other
process_eyes 1

View file

@ -436,9 +436,8 @@ void Audio::handleAudioInput() {
}
}
if (!_noiseGateOpen) {
for (int i = 0; i < NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL; i++) {
monoAudioSamples[i] = 0;
}
memset(monoAudioSamples, 0, NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL);
_lastInputLoudness = 0;
}
}
@ -451,7 +450,7 @@ void Audio::handleAudioInput() {
// our input loudness is 0, since we're muted
_lastInputLoudness = 0;
}
// add procedural effects to the appropriate input samples
addProceduralSounds(monoAudioSamples,
NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL);
@ -482,9 +481,26 @@ void Audio::handleAudioInput() {
// we need the amount of bytes in the buffer + 1 for type
// + 12 for 3 floats for position + float for bearing + 1 attenuation byte
PacketType packetType = Menu::getInstance()->isOptionChecked(MenuOption::EchoServerAudio)
? PacketTypeMicrophoneAudioWithEcho : PacketTypeMicrophoneAudioNoEcho;
int numAudioBytes = 0;
PacketType packetType;
if (_lastInputLoudness == 0) {
packetType = PacketTypeSilentAudioFrame;
// we need to indicate how many silent samples this is to the audio mixer
monoAudioSamples[0] = NETWORK_BUFFER_LENGTH_SAMPLES_PER_CHANNEL;
numAudioBytes = sizeof(int16_t);
} else {
numAudioBytes = NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL;
if (Menu::getInstance()->isOptionChecked(MenuOption::EchoServerAudio)) {
packetType = PacketTypeMicrophoneAudioWithEcho;
} else {
packetType = PacketTypeMicrophoneAudioNoEcho;
}
}
char* currentPacketPtr = monoAudioDataPacket + populatePacketHeader(monoAudioDataPacket, packetType);
@ -495,13 +511,11 @@ void Audio::handleAudioInput() {
// memcpy our orientation
memcpy(currentPacketPtr, &headOrientation, sizeof(headOrientation));
currentPacketPtr += sizeof(headOrientation);
nodeList->writeDatagram(monoAudioDataPacket,
NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL + leadingBytes,
audioMixer);
nodeList->writeDatagram(monoAudioDataPacket, numAudioBytes + leadingBytes, audioMixer);
Application::getInstance()->getBandwidthMeter()->outputStream(BandwidthMeter::AUDIO)
.updateValue(NETWORK_BUFFER_LENGTH_BYTES_PER_CHANNEL + leadingBytes);
.updateValue(numAudioBytes + leadingBytes);
}
delete[] inputAudioSamples;
}
@ -638,7 +652,14 @@ void Audio::addProceduralSounds(int16_t* monoInput, int numSamples) {
int16_t collisionSample = (int16_t) sample;
_lastInputLoudness = 0;
monoInput[i] = glm::clamp(monoInput[i] + collisionSample, MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE);
_lastInputLoudness += fabsf(monoInput[i]);
_lastInputLoudness /= numSamples;
_lastInputLoudness /= MAX_SAMPLE_VALUE;
_localProceduralSamples[i] = glm::clamp(_localProceduralSamples[i] + collisionSample,
MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE);
@ -662,7 +683,14 @@ void Audio::addProceduralSounds(int16_t* monoInput, int numSamples) {
int16_t collisionSample = (int16_t) sample;
_lastInputLoudness = 0;
monoInput[i] = glm::clamp(monoInput[i] + collisionSample, MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE);
_lastInputLoudness += fabsf(monoInput[i]);
_lastInputLoudness /= numSamples;
_lastInputLoudness /= MAX_SAMPLE_VALUE;
_localProceduralSamples[i] = glm::clamp(_localProceduralSamples[i] + collisionSample,
MIN_SAMPLE_VALUE, MAX_SAMPLE_VALUE);

View file

@ -24,11 +24,12 @@
#include <QSlider>
#include <QStandardPaths>
#include <QUuid>
#include <QWindow>
#include <QHBoxLayout>
#include <AccountManager.h>
#include <XmppClient.h>
#include <UUID.h>
#include <FileDownloader.h>
#include "Application.h"
#include "Menu.h"
@ -36,6 +37,7 @@
#include "Util.h"
#include "InfoView.h"
#include "ui/MetavoxelEditor.h"
#include "ModelBrowser.h"
Menu* Menu::_instance = NULL;
@ -165,7 +167,7 @@ Menu::Menu() :
addCheckableActionToQMenuAndActionHash(editMenu, MenuOption::Gravity, Qt::SHIFT | Qt::Key_G, false);
addCheckableActionToQMenuAndActionHash(editMenu, MenuOption::ClickToFly);
addAvatarCollisionSubMenu(editMenu);
@ -692,6 +694,10 @@ void Menu::loginForCurrentDomain() {
void Menu::editPreferences() {
Application* applicationInstance = Application::getInstance();
ModelBrowser headBrowser(Head);
ModelBrowser skeletonBrowser(Skeleton);
const QString BROWSE_BUTTON_TEXT = "Browse";
QDialog dialog(applicationInstance->getWindow());
dialog.setWindowTitle("Interface Preferences");
@ -702,17 +708,33 @@ void Menu::editPreferences() {
QFormLayout* form = new QFormLayout();
layout->addLayout(form, 1);
QHBoxLayout headModelLayout;
QString faceURLString = applicationInstance->getAvatar()->getHead()->getFaceModel().getURL().toString();
QLineEdit* faceURLEdit = new QLineEdit(faceURLString);
faceURLEdit->setMinimumWidth(QLINE_MINIMUM_WIDTH);
faceURLEdit->setPlaceholderText(DEFAULT_HEAD_MODEL_URL.toString());
form->addRow("Face URL:", faceURLEdit);
QLineEdit headURLEdit(faceURLString);
QPushButton headBrowseButton(BROWSE_BUTTON_TEXT);
connect(&headBrowseButton, SIGNAL(clicked()), &headBrowser, SLOT(browse()));
connect(&headBrowser, SIGNAL(selected(QString)), &headURLEdit, SLOT(setText(QString)));
headURLEdit.setReadOnly(true);
headURLEdit.setMinimumWidth(QLINE_MINIMUM_WIDTH);
headURLEdit.setPlaceholderText(DEFAULT_HEAD_MODEL_URL.toString());
headModelLayout.addWidget(&headURLEdit);
headModelLayout.addWidget(&headBrowseButton);
form->addRow("Head URL:", &headModelLayout);
QHBoxLayout skeletonModelLayout;
QString skeletonURLString = applicationInstance->getAvatar()->getSkeletonModel().getURL().toString();
QLineEdit* skeletonURLEdit = new QLineEdit(skeletonURLString);
skeletonURLEdit->setMinimumWidth(QLINE_MINIMUM_WIDTH);
skeletonURLEdit->setPlaceholderText(DEFAULT_BODY_MODEL_URL.toString());
form->addRow("Skeleton URL:", skeletonURLEdit);
QLineEdit skeletonURLEdit(skeletonURLString);
QPushButton SkeletonBrowseButton(BROWSE_BUTTON_TEXT);
connect(&SkeletonBrowseButton, SIGNAL(clicked()), &skeletonBrowser, SLOT(browse()));
connect(&skeletonBrowser, SIGNAL(selected(QString)), &skeletonURLEdit, SLOT(setText(QString)));
skeletonURLEdit.setReadOnly(true);
skeletonURLEdit.setMinimumWidth(QLINE_MINIMUM_WIDTH);
skeletonURLEdit.setPlaceholderText(DEFAULT_BODY_MODEL_URL.toString());
skeletonModelLayout.addWidget(&skeletonURLEdit);
skeletonModelLayout.addWidget(&SkeletonBrowseButton);
form->addRow("Skeleton URL:", &skeletonModelLayout);
QString displayNameString = applicationInstance->getAvatar()->getDisplayName();
QLineEdit* displayNameEdit = new QLineEdit(displayNameString);
@ -774,21 +796,17 @@ void Menu::editPreferences() {
int ret = dialog.exec();
if (ret == QDialog::Accepted) {
QUrl faceModelURL(faceURLEdit->text());
bool shouldDispatchIdentityPacket = false;
if (faceModelURL.toString() != faceURLString) {
if (headURLEdit.text() != faceURLString && !headURLEdit.text().isEmpty()) {
// change the faceModelURL in the profile, it will also update this user's BlendFace
applicationInstance->getAvatar()->setFaceModelURL(faceModelURL);
applicationInstance->getAvatar()->setFaceModelURL(QUrl(headURLEdit.text()));
shouldDispatchIdentityPacket = true;
}
QUrl skeletonModelURL(skeletonURLEdit->text());
if (skeletonModelURL.toString() != skeletonURLString) {
if (skeletonURLEdit.text() != skeletonURLString && !skeletonURLEdit.text().isEmpty()) {
// change the skeletonModelURL in the profile, it will also update this user's Body
applicationInstance->getAvatar()->setSkeletonModelURL(skeletonModelURL);
applicationInstance->getAvatar()->setSkeletonModelURL(QUrl(skeletonURLEdit.text()));
shouldDispatchIdentityPacket = true;
}

View file

@ -272,7 +272,6 @@ namespace MenuOption {
const QString OffAxisProjection = "Off-Axis Projection";
const QString OldVoxelCullingMode = "Old Voxel Culling Mode";
const QString TurnWithHead = "Turn using Head";
const QString ClickToFly = "Fly to voxel on click";
const QString LoadScript = "Open and Run Script...";
const QString Oscilloscope = "Audio Oscilloscope";
const QString Pair = "Pair";

View file

@ -185,10 +185,11 @@ int MetavoxelSystem::SimulateVisitor::visit(MetavoxelInfo& info) {
}
QRgb color = info.inputValues.at(0).getInlineValue<QRgb>();
QRgb normal = info.inputValues.at(1).getInlineValue<QRgb>();
int alpha = qAlpha(color);
quint8 alpha = qAlpha(color);
if (alpha > 0) {
Point point = { glm::vec4(info.minimum + glm::vec3(info.size, info.size, info.size) * 0.5f, info.size),
{ qRed(color), qGreen(color), qBlue(color), alpha }, { qRed(normal), qGreen(normal), qBlue(normal) } };
{ quint8(qRed(color)), quint8(qGreen(color)), quint8(qBlue(color)), alpha },
{ quint8(qRed(normal)), quint8(qGreen(normal)), quint8(qBlue(normal)) } };
_points.append(point);
}
return STOP_RECURSION;

View file

@ -0,0 +1,150 @@
//
// ModelBrowser.cpp
// hifi
//
// Created by Clement on 3/17/14.
// Copyright (c) 2014 HighFidelity, Inc. All rights reserved.
//
#include <QUrl>
#include <QXmlStreamReader>
#include <QEventLoop>
#include <QMessageBox>
#include <QGridLayout>
#include <QDialog>
#include <QStringListModel>
#include <QDialogButtonBox>
#include <Application.h>
#include "ModelBrowser.h"
static const QString PREFIX_PARAMETER_NAME = "prefix";
static const QString MARKER_PARAMETER_NAME = "marker";
static const QString IS_TRUNCATED_NAME = "IsTruncated";
static const QString CONTAINER_NAME = "Contents";
static const QString KEY_NAME = "Key";
ModelBrowser::ModelBrowser(ModelType modelType, QWidget* parent) : QWidget(parent), _type(modelType) {
QUrl url(S3_URL);
QUrlQuery query;
if (_type == Head) {
query.addQueryItem(PREFIX_PARAMETER_NAME, HEAD_MODELS_LOCATION);
} else if (_type == Skeleton) {
query.addQueryItem(PREFIX_PARAMETER_NAME, SKELETON_MODELS_LOCATION);
}
url.setQuery(query);
_downloader = new FileDownloader(url);
connect(_downloader, SIGNAL(done(QNetworkReply::NetworkError)), SLOT(downloadFinished()));
}
ModelBrowser::~ModelBrowser() {
delete _downloader;
}
void ModelBrowser::downloadFinished() {
parseXML(_downloader->getData());
}
void ModelBrowser::browse() {
QDialog dialog(this);
dialog.setWindowTitle("Browse models");
QGridLayout* layout = new QGridLayout(&dialog);
dialog.setLayout(layout);
QLineEdit* searchBar = new QLineEdit(&dialog);
layout->addWidget(searchBar, 0, 0);
ListView* listView = new ListView(&dialog);
listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
layout->addWidget(listView, 1, 0);
listView->connect(searchBar, SIGNAL(textChanged(const QString&)), SLOT(keyboardSearch(const QString&)));
dialog.connect(listView, SIGNAL(doubleClicked(const QModelIndex&)), SLOT(accept()));
QStringListModel* model = new QStringListModel(_models.keys(), listView);
model->sort(0);
listView->setModel(model);
QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
layout->addWidget(buttons, 2, 0);
dialog.connect(buttons, SIGNAL(accepted()), SLOT(accept()));
dialog.connect(buttons, SIGNAL(rejected()), SLOT(reject()));
if (dialog.exec() == QDialog::Rejected) {
return;
}
QString selectedKey = model->data(listView->currentIndex(), Qt::DisplayRole).toString();
emit selected(_models[selectedKey]);
}
bool ModelBrowser::parseXML(QByteArray xmlFile) {
QXmlStreamReader xml(xmlFile);
QRegExp rx(".*fst");
bool truncated = false;
QString lastKey;
// Read xml until the end or an error is detected
while(!xml.atEnd() && !xml.hasError()) {
if(xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == IS_TRUNCATED_NAME) {
while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == IS_TRUNCATED_NAME)) {
// Let's check if there is more
xml.readNext();
if (xml.text().toString() == "True") {
truncated = true;
}
}
}
if(xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == CONTAINER_NAME) {
while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == CONTAINER_NAME)) {
// If a file is find, process it
if(xml.tokenType() == QXmlStreamReader::StartElement && xml.name() == KEY_NAME) {
xml.readNext();
lastKey = xml.text().toString();
if (rx.exactMatch(xml.text().toString())) {
// Add the found file to the list
_models.insert(QFileInfo(xml.text().toString()).baseName(),
S3_URL + "/" + xml.text().toString());
}
}
xml.readNext();
}
}
xml.readNext();
}
// Error handling
if(xml.hasError()) {
_models.clear();
QMessageBox::critical(this,
"ModelBrowser::ModelBrowser()",
xml.errorString(),
QMessageBox::Ok);
return false;
}
// If we didn't all the files, download the next ones
if (truncated) {
QUrl url(S3_URL);
QUrlQuery query;
if (_type == Head) {
query.addQueryItem(PREFIX_PARAMETER_NAME, HEAD_MODELS_LOCATION);
} else if (_type == Skeleton) {
query.addQueryItem(PREFIX_PARAMETER_NAME, SKELETON_MODELS_LOCATION);
}
query.addQueryItem(MARKER_PARAMETER_NAME, lastKey);
url.setQuery(query);
delete _downloader;
_downloader = new FileDownloader(url);
connect(_downloader, SIGNAL(done(QNetworkReply::NetworkError)), SLOT(downloadFinished()));
}
return true;
}

View file

@ -0,0 +1,62 @@
//
// ModelBrowser.h
// hifi
//
// Created by Clement on 3/17/14.
// Copyright (c) 2014 HighFidelity, Inc. All rights reserved.
//
#ifndef __hifi__ModelBrowser__
#define __hifi__ModelBrowser__
#include <FileDownloader.h>
#include <QDialog>
#include <QListView>
static const QString S3_URL = "http://highfidelity-public.s3-us-west-1.amazonaws.com";
static const QString HEAD_MODELS_LOCATION = "models/heads/";
static const QString SKELETON_MODELS_LOCATION = "models/skeletons/";
enum ModelType {
Head,
Skeleton
};
class ModelBrowser : public QWidget {
Q_OBJECT
public:
ModelBrowser(ModelType modelType, QWidget* parent = NULL);
~ModelBrowser();
signals:
void selected(QString);
public slots:
void browse();
private slots:
void downloadFinished();
private:
ModelType _type;
FileDownloader* _downloader;
QHash<QString, QString> _models;
bool parseXML(QByteArray xmlFile);
};
class ListView : public QListView {
Q_OBJECT
public:
ListView(QWidget* parent) : QListView(parent) {}
public slots:
void keyboardSearch(const QString& text) {
QAbstractItemView::keyboardSearch(text);
}
};
#endif /* defined(__hifi__ModelBrowser__) */

View file

@ -16,6 +16,7 @@
/// Generalized threaded processor for handling received inbound packets.
class VoxelHideShowThread : public GenericThread {
Q_OBJECT
public:
VoxelHideShowThread(VoxelSystem* theSystem);

View file

@ -16,6 +16,7 @@
/// Handles processing of incoming voxel packets for the interface application. As with other ReceivedPacketProcessor classes
/// the user is responsible for reading inbound packets and adding them to the processing queue by calling queueReceivedPacket()
class VoxelPacketProcessor : public ReceivedPacketProcessor {
Q_OBJECT
protected:
virtual void processPacket(const SharedNodePointer& sendingNode, const QByteArray& packet);
};

View file

@ -189,55 +189,60 @@ static TextRenderer* textRenderer(TextRendererType type) {
return displayNameRenderer;
}
void Avatar::render(bool forShadowMap) {
void Avatar::render(const glm::vec3& cameraPosition, bool forShadowMap) {
// simple frustum check
float boundingRadius = getBillboardSize();
if (Application::getInstance()->getViewFrustum()->sphereInFrustum(_position, boundingRadius) == ViewFrustum::OUTSIDE) {
if (Application::getInstance()->getViewFrustum()->sphereInFrustum(cameraPosition, boundingRadius) == ViewFrustum::OUTSIDE) {
return;
}
glm::vec3 toTarget = _position - Application::getInstance()->getAvatar()->getPosition();
float lengthToTarget = glm::length(toTarget);
glm::vec3 toTarget = cameraPosition - Application::getInstance()->getAvatar()->getPosition();
float distanceToTarget = glm::length(toTarget);
{
// glow when moving in the distance
// glow when moving far away
const float GLOW_DISTANCE = 20.0f;
Glower glower(_moving && lengthToTarget > GLOW_DISTANCE && !forShadowMap ? 1.0f : 0.0f);
Glower glower(_moving && distanceToTarget > GLOW_DISTANCE && !forShadowMap ? 1.0f : 0.0f);
// render body
if (Menu::getInstance()->isOptionChecked(MenuOption::Avatars)) {
renderBody(forShadowMap);
}
if (Menu::getInstance()->isOptionChecked(MenuOption::RenderSkeletonCollisionProxies)) {
_skeletonModel.renderCollisionProxies(0.7f);
}
if (Menu::getInstance()->isOptionChecked(MenuOption::RenderHeadCollisionProxies)) {
getHead()->getFaceModel().renderCollisionProxies(0.7f);
}
if (Menu::getInstance()->isOptionChecked(MenuOption::Avatars)) {
renderBody();
}
// render voice intensity sphere for avatars that are farther away
const float MAX_SPHERE_ANGLE = 10.f * RADIANS_PER_DEGREE;
const float MIN_SPHERE_ANGLE = 1.f * RADIANS_PER_DEGREE;
const float MIN_SPHERE_SIZE = 0.01f;
const float SPHERE_LOUDNESS_SCALING = 0.0005f;
const float SPHERE_COLOR[] = { 0.5f, 0.8f, 0.8f };
float height = getSkeletonHeight();
glm::vec3 delta = height * (getHead()->getCameraOrientation() * IDENTITY_UP) / 2.f;
float angle = abs(angleBetween(toTarget + delta, toTarget - delta));
float sphereRadius = getHead()->getAverageLoudness() * SPHERE_LOUDNESS_SCALING;
if (!forShadowMap && (sphereRadius > MIN_SPHERE_SIZE) && (angle < MAX_SPHERE_ANGLE) && (angle > MIN_SPHERE_ANGLE)) {
glColor4f(SPHERE_COLOR[0], SPHERE_COLOR[1], SPHERE_COLOR[2], 1.f - angle / MAX_SPHERE_ANGLE);
glPushMatrix();
glTranslatef(_position.x, _position.y, _position.z);
glScalef(height, height, height);
glutSolidSphere(sphereRadius, 15, 15);
glPopMatrix();
// quick check before falling into the code below:
// (a 10 degree breadth of an almost 2 meter avatar kicks in at about 12m)
const float MIN_VOICE_SPHERE_DISTANCE = 12.f;
if (distanceToTarget > MIN_VOICE_SPHERE_DISTANCE) {
// render voice intensity sphere for avatars that are farther away
const float MAX_SPHERE_ANGLE = 10.f * RADIANS_PER_DEGREE;
const float MIN_SPHERE_ANGLE = 1.f * RADIANS_PER_DEGREE;
const float MIN_SPHERE_SIZE = 0.01f;
const float SPHERE_LOUDNESS_SCALING = 0.0005f;
const float SPHERE_COLOR[] = { 0.5f, 0.8f, 0.8f };
float height = getSkeletonHeight();
glm::vec3 delta = height * (getHead()->getCameraOrientation() * IDENTITY_UP) / 2.f;
float angle = abs(angleBetween(toTarget + delta, toTarget - delta));
float sphereRadius = getHead()->getAverageLoudness() * SPHERE_LOUDNESS_SCALING;
if (!forShadowMap && (sphereRadius > MIN_SPHERE_SIZE) && (angle < MAX_SPHERE_ANGLE) && (angle > MIN_SPHERE_ANGLE)) {
glColor4f(SPHERE_COLOR[0], SPHERE_COLOR[1], SPHERE_COLOR[2], 1.f - angle / MAX_SPHERE_ANGLE);
glPushMatrix();
glTranslatef(_position.x, _position.y, _position.z);
glScalef(height, height, height);
glutSolidSphere(sphereRadius, 15, 15);
glPopMatrix();
}
}
}
const float DISPLAYNAME_DISTANCE = 10.0f;
setShowDisplayName(!forShadowMap && lengthToTarget < DISPLAYNAME_DISTANCE);
setShowDisplayName(!forShadowMap && distanceToTarget < DISPLAYNAME_DISTANCE);
if (forShadowMap) {
return;
}
@ -257,7 +262,6 @@ void Avatar::render(bool forShadowMap) {
glm::vec3 chatAxis = glm::axis(chatRotation);
glRotatef(glm::degrees(glm::angle(chatRotation)), chatAxis.x, chatAxis.y, chatAxis.z);
glColor3f(0.f, 0.8f, 0.f);
glRotatef(180.f, 0.f, 1.f, 0.f);
glRotatef(180.f, 0.f, 0.f, 1.f);
@ -302,9 +306,12 @@ glm::quat Avatar::computeRotationFromBodyToWorldUp(float proportion) const {
return glm::angleAxis(angle * proportion, axis);
}
void Avatar::renderBody() {
void Avatar::renderBody(bool forShadowMap) {
if (_shouldRenderBillboard || !(_skeletonModel.isRenderable() && getHead()->getFaceModel().isRenderable())) {
// render the billboard until both models are loaded
if (forShadowMap) {
return;
}
renderBillboard();
return;
}
@ -626,11 +633,11 @@ void Avatar::setBillboard(const QByteArray& billboard) {
_billboardTexture.reset();
}
int Avatar::parseData(const QByteArray& packet) {
int Avatar::parseDataAtOffset(const QByteArray& packet, int offset) {
// change in position implies movement
glm::vec3 oldPosition = _position;
int bytesRead = AvatarData::parseData(packet);
int bytesRead = AvatarData::parseDataAtOffset(packet, offset);
const float MOVE_DISTANCE_THRESHOLD = 0.001f;
_moving = glm::distance(oldPosition, _position) > MOVE_DISTANCE_THRESHOLD;

View file

@ -74,7 +74,7 @@ public:
void init();
void simulate(float deltaTime);
void render(bool forShadowMap = false);
virtual void render(const glm::vec3& cameraPosition, bool forShadowMap);
//setters
void setDisplayingLookatVectors(bool displayingLookatVectors) { getHead()->setRenderLookatVectors(displayingLookatVectors); }
@ -133,7 +133,7 @@ public:
void setShowDisplayName(bool showDisplayName);
int parseData(const QByteArray& packet);
int parseDataAtOffset(const QByteArray& packet, int offset);
static void renderJointConnectingCone(glm::vec3 position1, glm::vec3 position2, float radius1, float radius2);
@ -181,6 +181,7 @@ protected:
float getPelvisToHeadLength() const;
void renderDisplayName();
virtual void renderBody(bool forShadowMap);
private:
@ -189,7 +190,6 @@ private:
bool _shouldRenderBillboard;
bool _modelsDirty;
void renderBody();
void renderBillboard();
float getBillboardSize() const;

View file

@ -77,7 +77,7 @@ void AvatarManager::renderAvatars(bool forShadowMapOrMirror, bool selfAvatarOnly
"Application::renderAvatars()");
bool renderLookAtVectors = Menu::getInstance()->isOptionChecked(MenuOption::LookAtVectors);
glm::vec3 cameraPosition = Application::getInstance()->getCamera()->getPosition();
if (!selfAvatarOnly) {
foreach (const AvatarSharedPointer& avatarPointer, _avatarHash) {
@ -85,17 +85,13 @@ void AvatarManager::renderAvatars(bool forShadowMapOrMirror, bool selfAvatarOnly
if (!avatar->isInitialized()) {
continue;
}
if (avatar == static_cast<Avatar*>(_myAvatar.data())) {
_myAvatar->render(forShadowMapOrMirror);
} else {
avatar->render(forShadowMapOrMirror);
}
avatar->render(cameraPosition, forShadowMapOrMirror);
avatar->setDisplayingLookatVectors(renderLookAtVectors);
}
renderAvatarFades(forShadowMapOrMirror);
renderAvatarFades(cameraPosition, forShadowMapOrMirror);
} else {
// just render myAvatar
_myAvatar->render(forShadowMapOrMirror);
_myAvatar->render(cameraPosition, forShadowMapOrMirror);
_myAvatar->setDisplayingLookatVectors(renderLookAtVectors);
}
}
@ -118,13 +114,15 @@ void AvatarManager::simulateAvatarFades(float deltaTime) {
}
}
void AvatarManager::renderAvatarFades(bool forShadowMap) {
void AvatarManager::renderAvatarFades(const glm::vec3& cameraPosition, bool forShadowMap) {
// render avatar fades
Glower glower(forShadowMap ? 0.0f : 1.0f);
foreach(const AvatarSharedPointer& fadingAvatar, _avatarFades) {
Avatar* avatar = static_cast<Avatar*>(fadingAvatar.data());
avatar->render(forShadowMap);
if (avatar != static_cast<Avatar*>(_myAvatar.data())) {
avatar->render(cameraPosition, forShadowMap);
}
}
}
@ -150,14 +148,11 @@ void AvatarManager::processAvatarMixerDatagram(const QByteArray& datagram, const
void AvatarManager::processAvatarDataPacket(const QByteArray &datagram, const QWeakPointer<Node> &mixerWeakPointer) {
int bytesRead = numBytesForPacketHeader(datagram);
QByteArray dummyAvatarByteArray = byteArrayWithPopulatedHeader(PacketTypeAvatarData);
int numDummyHeaderBytes = dummyAvatarByteArray.size();
int numDummyHeaderBytesWithoutUUID = numDummyHeaderBytes - NUM_BYTES_RFC4122_UUID;
// enumerate over all of the avatars in this packet
// only add them if mixerWeakPointer points to something (meaning that mixer is still around)
while (bytesRead < datagram.size() && mixerWeakPointer.data()) {
QUuid nodeUUID = QUuid::fromRfc4122(datagram.mid(bytesRead, NUM_BYTES_RFC4122_UUID));
bytesRead += NUM_BYTES_RFC4122_UUID;
AvatarSharedPointer matchingAvatar = _avatarHash.value(nodeUUID);
@ -173,16 +168,9 @@ void AvatarManager::processAvatarDataPacket(const QByteArray &datagram, const QW
qDebug() << "Adding avatar with UUID" << nodeUUID << "to AvatarManager hash.";
}
// copy the rest of the packet to the avatarData holder so we can read the next Avatar from there
dummyAvatarByteArray.resize(numDummyHeaderBytesWithoutUUID);
// make this Avatar's UUID the UUID in the packet and tack the remaining data onto the end
dummyAvatarByteArray.append(datagram.mid(bytesRead));
// have the matching (or new) avatar parse the data from the packet
bytesRead += matchingAvatar->parseData(dummyAvatarByteArray) - numDummyHeaderBytesWithoutUUID;
bytesRead += matchingAvatar->parseDataAtOffset(datagram, bytesRead);
}
}
void AvatarManager::processAvatarIdentityPacket(const QByteArray &packet) {

View file

@ -45,7 +45,7 @@ private:
void processKillAvatar(const QByteArray& datagram);
void simulateAvatarFades(float deltaTime);
void renderAvatarFades(bool forShadowMap);
void renderAvatarFades(const glm::vec3& cameraPosition, bool forShadowMap);
// virtual override
AvatarHash::iterator erase(const AvatarHash::iterator& iterator);

View file

@ -206,8 +206,8 @@ void Hand::collideAgainstOurself() {
}
// ignoring everything below the parent of the parent of the last free joint
int skipIndex = skeletonModel.getParentJointIndex(skeletonModel.getParentJointIndex(
skeletonModel.getLastFreeJointIndex(((int)i == leftPalmIndex) ? skeletonModel.getLeftHandJointIndex() :
((int)i == rightPalmIndex) ? skeletonModel.getRightHandJointIndex() : -1)));
skeletonModel.getLastFreeJointIndex((int(i) == leftPalmIndex) ? skeletonModel.getLeftHandJointIndex() :
(int(i) == rightPalmIndex) ? skeletonModel.getRightHandJointIndex() : -1)));
handCollisions.clear();
if (_owningAvatar->findSphereCollisions(palm.getPosition(), scaledPalmRadius, handCollisions, skipIndex)) {

View file

@ -85,6 +85,7 @@ void MyAvatar::reset() {
setVelocity(glm::vec3(0,0,0));
setThrust(glm::vec3(0,0,0));
setOrientation(glm::quat(glm::vec3(0,0,0)));
}
void MyAvatar::setMoveTarget(const glm::vec3 moveTarget) {
@ -423,11 +424,6 @@ void MyAvatar::updateFromGyros(float deltaTime) {
}
}
static TextRenderer* textRenderer() {
static TextRenderer* renderer = new TextRenderer(SANS_FONT_FAMILY, 24, -1, false, TextRenderer::SHADOW_EFFECT);
return renderer;
}
void MyAvatar::renderDebugBodyPoints() {
glm::vec3 torsoPosition(getPosition());
glm::vec3 headPosition(getHead()->getEyePosition());
@ -453,68 +449,14 @@ void MyAvatar::renderDebugBodyPoints() {
}
void MyAvatar::render(bool forShadowMapOrMirror) {
// virtual
void MyAvatar::render(const glm::vec3& cameraPosition, bool forShadowMapOrMirror) {
// don't render if we've been asked to disable local rendering
if (!_shouldRender) {
return; // exit early
}
if (Menu::getInstance()->isOptionChecked(MenuOption::Avatars)) {
renderBody(forShadowMapOrMirror);
}
// render body
if (Menu::getInstance()->isOptionChecked(MenuOption::RenderSkeletonCollisionProxies)) {
_skeletonModel.renderCollisionProxies(0.8f);
}
if (Menu::getInstance()->isOptionChecked(MenuOption::RenderHeadCollisionProxies)) {
getHead()->getFaceModel().renderCollisionProxies(0.8f);
}
setShowDisplayName(!forShadowMapOrMirror);
if (forShadowMapOrMirror) {
return;
}
renderDisplayName();
if (!_chatMessage.empty()) {
int width = 0;
int lastWidth = 0;
for (string::iterator it = _chatMessage.begin(); it != _chatMessage.end(); it++) {
width += (lastWidth = textRenderer()->computeWidth(*it));
}
glPushMatrix();
glm::vec3 chatPosition = getHead()->getEyePosition() + getBodyUpDirection() * CHAT_MESSAGE_HEIGHT * _scale;
glTranslatef(chatPosition.x, chatPosition.y, chatPosition.z);
glm::quat chatRotation = Application::getInstance()->getCamera()->getRotation();
glm::vec3 chatAxis = glm::axis(chatRotation);
glRotatef(glm::degrees(glm::angle(chatRotation)), chatAxis.x, chatAxis.y, chatAxis.z);
glColor3f(0.f, 0.8f, 0.f);
glRotatef(180.f, 0.f, 1.f, 0.f);
glRotatef(180.f, 0.f, 0.f, 1.f);
glScalef(_scale * CHAT_MESSAGE_SCALE, _scale * CHAT_MESSAGE_SCALE, 1.0f);
glDisable(GL_LIGHTING);
glDepthMask(false);
if (_keyState == NO_KEY_DOWN) {
textRenderer()->draw(-width / 2.0f, 0, _chatMessage.c_str());
} else {
// rather than using substr and allocating a new string, just replace the last
// character with a null, then restore it
int lastIndex = _chatMessage.size() - 1;
char lastChar = _chatMessage[lastIndex];
_chatMessage[lastIndex] = '\0';
textRenderer()->draw(-width / 2.0f, 0, _chatMessage.c_str());
_chatMessage[lastIndex] = lastChar;
glColor3f(0.f, 1.f, 0.f);
textRenderer()->draw(width / 2.0f - lastWidth, 0, _chatMessage.c_str() + lastIndex);
}
glEnable(GL_LIGHTING);
glDepthMask(true);
glPopMatrix();
}
Avatar::render(cameraPosition, forShadowMapOrMirror);
}
void MyAvatar::renderHeadMouse() const {
@ -1189,9 +1131,9 @@ void MyAvatar::goToLocationFromResponse(const QJsonObject& jsonObject) {
NodeList::getInstance()->getDomainInfo().setHostname(domainHostnameString);
// orient the user to face the target
glm::quat newOrientation = glm::quat(glm::vec3(orientationItems[0].toFloat(),
orientationItems[1].toFloat(),
orientationItems[2].toFloat()))
glm::quat newOrientation = glm::quat(glm::radians(glm::vec3(orientationItems[0].toFloat(),
orientationItems[1].toFloat(),
orientationItems[2].toFloat())))
* glm::angleAxis(PI, glm::vec3(0.0f, 1.0f, 0.0f));
setOrientation(newOrientation);

View file

@ -35,7 +35,8 @@ public:
void simulate(float deltaTime);
void updateFromGyros(float deltaTime);
void render(bool forShadowMapOrMirror = false);
void render(const glm::vec3& cameraPosition, bool forShadowMapOrMirror = false);
void renderBody(bool forceRenderHead);
void renderDebugBodyPoints();
void renderHeadMouse() const;
@ -120,7 +121,6 @@ private:
bool _billboardValid;
// private methods
void renderBody(bool forceRenderHead);
void updateThrust(float deltaTime);
void updateHandMovementAndTouching(float deltaTime);
void updateCollisionWithAvatars(float deltaTime);

View file

@ -140,7 +140,7 @@ void SkeletonModel::applyPalmData(int jointIndex, const QVector<int>& fingerJoin
direction += fingerVector / length;
}
fingerVector = glm::inverse(palmRotation) * fingerVector * -sign;
IndexValue indexValue = { i, atan2f(fingerVector.z, fingerVector.x) };
IndexValue indexValue = { int(i), atan2f(fingerVector.z, fingerVector.x) };
fingerIndices.append(indexValue);
}
qSort(fingerIndices.begin(), fingerIndices.end());

View file

@ -103,7 +103,7 @@ void Faceshift::reset() {
}
void Faceshift::updateFakeCoefficients(float leftBlink, float rightBlink, float browUp,
float jawOpen, std::vector<float>& coefficients) const {
float jawOpen, QVector<float>& coefficients) const {
coefficients.resize(max((int)coefficients.size(), _jawOpenIndex + 1));
qFill(coefficients.begin(), coefficients.end(), 0.0f);
coefficients[_leftBlinkIndex] = leftBlink;
@ -204,7 +204,7 @@ void Faceshift::receive(const QByteArray& buffer) {
_eyeGazeLeftYaw = data.m_eyeGazeLeftYaw;
_eyeGazeRightPitch = -data.m_eyeGazeRightPitch;
_eyeGazeRightYaw = data.m_eyeGazeRightYaw;
_blendshapeCoefficients = data.m_coeffs;
_blendshapeCoefficients = QVector<float>::fromStdVector(data.m_coeffs);
_lastTrackingStateReceived = usecTimestampNow();
}

View file

@ -9,8 +9,6 @@
#ifndef __interface__Faceshift__
#define __interface__Faceshift__
#include <vector>
#include <QTcpSocket>
#include <QUdpSocket>
@ -47,7 +45,7 @@ public:
float getEstimatedEyePitch() const { return _estimatedEyePitch; }
float getEstimatedEyeYaw() const { return _estimatedEyeYaw; }
const std::vector<float>& getBlendshapeCoefficients() const { return _blendshapeCoefficients; }
const QVector<float>& getBlendshapeCoefficients() const { return _blendshapeCoefficients; }
float getLeftBlink() const { return getBlendshapeCoefficient(_leftBlinkIndex); }
float getRightBlink() const { return getBlendshapeCoefficient(_rightBlinkIndex); }
@ -68,7 +66,7 @@ public:
void reset();
void updateFakeCoefficients(float leftBlink, float rightBlink, float browUp,
float jawOpen, std::vector<float>& coefficients) const;
float jawOpen, QVector<float>& coefficients) const;
signals:
@ -111,7 +109,7 @@ private:
float _eyeGazeRightPitch;
float _eyeGazeRightYaw;
std::vector<float> _blendshapeCoefficients;
QVector<float> _blendshapeCoefficients;
int _leftBlinkIndex;
int _rightBlinkIndex;

View file

@ -9,8 +9,6 @@
#ifndef __interface__Visage__
#define __interface__Visage__
#include <vector>
#include <QMultiHash>
#include <QPair>
#include <QVector>
@ -42,7 +40,7 @@ public:
float getEstimatedEyePitch() const { return _estimatedEyePitch; }
float getEstimatedEyeYaw() const { return _estimatedEyeYaw; }
const std::vector<float>& getBlendshapeCoefficients() const { return _blendshapeCoefficients; }
const QVector<float>& getBlendshapeCoefficients() const { return _blendshapeCoefficients; }
void update();
void reset();
@ -71,7 +69,7 @@ private:
float _estimatedEyePitch;
float _estimatedEyeYaw;
std::vector<float> _blendshapeCoefficients;
QVector<float> _blendshapeCoefficients;
};
#endif /* defined(__interface__Visage__) */

View file

@ -54,6 +54,15 @@ QStringList FBXGeometry::getJointNames() const {
return names;
}
bool FBXGeometry::hasBlendedMeshes() const {
foreach (const FBXMesh& mesh, meshes) {
if (!mesh.blendshapes.isEmpty()) {
return true;
}
}
return false;
}
static int fbxGeometryMetaTypeId = qRegisterMetaType<FBXGeometry>();
template<class T> QVariant readBinaryArray(QDataStream& in) {
@ -1331,14 +1340,11 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping)
geometry.staticExtents.reset();
geometry.meshExtents.reset();
QVariantHash springs = mapping.value("spring").toHash();
QVariant defaultSpring = springs.value("default");
for (QHash<QString, ExtractedMesh>::iterator it = meshes.begin(); it != meshes.end(); it++) {
ExtractedMesh& extracted = it.value();
// accumulate local transforms
QString modelID = models.contains(it.key()) ? it.key() : parentMap.value(it.key());
extracted.mesh.springiness = springs.value(models.value(modelID).name, defaultSpring).toFloat();
glm::mat4 modelTransform = getGlobalTransform(parentMap, models, modelID);
// compute the mesh extents from the transformed vertices
@ -1591,49 +1597,7 @@ FBXGeometry extractFBXGeometry(const FBXNode& node, const QVariantHash& mapping)
}
}
extracted.mesh.isEye = (maxJointIndex == geometry.leftEyeJointIndex || maxJointIndex == geometry.rightEyeJointIndex);
// extract spring edges, connections if springy
if (extracted.mesh.springiness > 0.0f) {
QSet<QPair<int, int> > edges;
extracted.mesh.vertexConnections.resize(extracted.mesh.vertices.size());
foreach (const FBXMeshPart& part, extracted.mesh.parts) {
for (int i = 0; i < part.quadIndices.size(); i += 4) {
int index0 = part.quadIndices.at(i);
int index1 = part.quadIndices.at(i + 1);
int index2 = part.quadIndices.at(i + 2);
int index3 = part.quadIndices.at(i + 3);
edges.insert(QPair<int, int>(qMin(index0, index1), qMax(index0, index1)));
edges.insert(QPair<int, int>(qMin(index1, index2), qMax(index1, index2)));
edges.insert(QPair<int, int>(qMin(index2, index3), qMax(index2, index3)));
edges.insert(QPair<int, int>(qMin(index3, index0), qMax(index3, index0)));
extracted.mesh.vertexConnections[index0].append(QPair<int, int>(index3, index1));
extracted.mesh.vertexConnections[index1].append(QPair<int, int>(index0, index2));
extracted.mesh.vertexConnections[index2].append(QPair<int, int>(index1, index3));
extracted.mesh.vertexConnections[index3].append(QPair<int, int>(index2, index0));
}
for (int i = 0; i < part.triangleIndices.size(); i += 3) {
int index0 = part.triangleIndices.at(i);
int index1 = part.triangleIndices.at(i + 1);
int index2 = part.triangleIndices.at(i + 2);
edges.insert(QPair<int, int>(qMin(index0, index1), qMax(index0, index1)));
edges.insert(QPair<int, int>(qMin(index1, index2), qMax(index1, index2)));
edges.insert(QPair<int, int>(qMin(index2, index0), qMax(index2, index0)));
extracted.mesh.vertexConnections[index0].append(QPair<int, int>(index2, index1));
extracted.mesh.vertexConnections[index1].append(QPair<int, int>(index0, index2));
extracted.mesh.vertexConnections[index2].append(QPair<int, int>(index1, index0));
}
}
for (QSet<QPair<int, int> >::const_iterator edge = edges.constBegin(); edge != edges.constEnd(); edge++) {
extracted.mesh.springEdges.append(*edge);
}
}
geometry.meshes.append(extracted.mesh);
}
@ -1797,7 +1761,6 @@ FBXGeometry readSVO(const QByteArray& model) {
// and one mesh with one cluster and one part
FBXMesh mesh;
mesh.isEye = false;
mesh.springiness = 0.0f;
FBXCluster cluster = { 0 };
mesh.clusters.append(cluster);

View file

@ -130,10 +130,6 @@ public:
bool isEye;
QVector<FBXBlendshape> blendshapes;
float springiness;
QVector<QPair<int, int> > springEdges;
QVector<QVarLengthArray<QPair<int, int>, 4> > vertexConnections;
};
/// An attachment to an FBX document.
@ -185,6 +181,8 @@ public:
int getJointIndex(const QString& name) const { return jointIndices.value(name) - 1; }
QStringList getJointNames() const;
bool hasBlendedMeshes() const;
};
Q_DECLARE_METATYPE(FBXGeometry)

View file

@ -13,6 +13,7 @@
#include "Application.h"
#include "GeometryCache.h"
#include "Model.h"
#include "world.h"
GeometryCache::~GeometryCache() {
@ -291,6 +292,13 @@ QSharedPointer<NetworkGeometry> GeometryCache::getGeometry(const QUrl& url, cons
return getResource(url, fallback, delayLoad).staticCast<NetworkGeometry>();
}
void GeometryCache::setBlendedVertices(const QPointer<Model>& model, const QWeakPointer<NetworkGeometry>& geometry,
const QVector<glm::vec3>& vertices, const QVector<glm::vec3>& normals) {
if (!model.isNull() && model->getGeometry() == geometry) {
model->setBlendedVertices(vertices, normals);
}
}
QSharedPointer<Resource> GeometryCache::createResource(const QUrl& url,
const QSharedPointer<Resource>& fallback, bool delayLoad, const void* extra) {
@ -565,8 +573,8 @@ void NetworkGeometry::setGeometry(const FBXGeometry& geometry) {
networkMesh.vertexBuffer.bind();
networkMesh.vertexBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
// if we don't need to do any blending or springing, then the positions/normals can be static
if (mesh.blendshapes.isEmpty() && mesh.springiness == 0.0f) {
// if we don't need to do any blending, the positions/normals can be static
if (mesh.blendshapes.isEmpty()) {
int normalsOffset = mesh.vertices.size() * sizeof(glm::vec3);
int tangentsOffset = normalsOffset + mesh.normals.size() * sizeof(glm::vec3);
int colorsOffset = tangentsOffset + mesh.tangents.size() * sizeof(glm::vec3);
@ -587,8 +595,8 @@ void NetworkGeometry::setGeometry(const FBXGeometry& geometry) {
networkMesh.vertexBuffer.write(clusterWeightsOffset, mesh.clusterWeights.constData(),
mesh.clusterWeights.size() * sizeof(glm::vec4));
// if there's no springiness, then the cluster indices/weights can be static
} else if (mesh.springiness == 0.0f) {
// otherwise, at least the cluster indices/weights can be static
} else {
int colorsOffset = mesh.tangents.size() * sizeof(glm::vec3);
int texCoordsOffset = colorsOffset + mesh.colors.size() * sizeof(glm::vec3);
int clusterIndicesOffset = texCoordsOffset + mesh.texCoords.size() * sizeof(glm::vec2);
@ -601,16 +609,7 @@ void NetworkGeometry::setGeometry(const FBXGeometry& geometry) {
networkMesh.vertexBuffer.write(clusterIndicesOffset, mesh.clusterIndices.constData(),
mesh.clusterIndices.size() * sizeof(glm::vec4));
networkMesh.vertexBuffer.write(clusterWeightsOffset, mesh.clusterWeights.constData(),
mesh.clusterWeights.size() * sizeof(glm::vec4));
} else {
int colorsOffset = mesh.tangents.size() * sizeof(glm::vec3);
int texCoordsOffset = colorsOffset + mesh.colors.size() * sizeof(glm::vec3);
networkMesh.vertexBuffer.allocate(texCoordsOffset + mesh.texCoords.size() * sizeof(glm::vec2));
networkMesh.vertexBuffer.write(0, mesh.tangents.constData(), mesh.tangents.size() * sizeof(glm::vec3));
networkMesh.vertexBuffer.write(colorsOffset, mesh.colors.constData(), mesh.colors.size() * sizeof(glm::vec3));
networkMesh.vertexBuffer.write(texCoordsOffset, mesh.texCoords.constData(),
mesh.texCoords.size() * sizeof(glm::vec2));
mesh.clusterWeights.size() * sizeof(glm::vec4));
}
networkMesh.vertexBuffer.release();

View file

@ -19,15 +19,18 @@
#include "FBXReader.h"
class Model;
class NetworkGeometry;
class NetworkMesh;
class NetworkTexture;
/// Stores cached geometry.
class GeometryCache : public ResourceCache {
Q_OBJECT
public:
~GeometryCache();
virtual ~GeometryCache();
void renderHemisphere(int slices, int stacks);
void renderSquare(int xDivisions, int yDivisions);
@ -38,7 +41,12 @@ public:
/// \param fallback a fallback URL to load if the desired one is unavailable
/// \param delayLoad if true, don't load the geometry immediately; wait until load is first requested
QSharedPointer<NetworkGeometry> getGeometry(const QUrl& url, const QUrl& fallback = QUrl(), bool delayLoad = false);
public slots:
void setBlendedVertices(const QPointer<Model>& model, const QWeakPointer<NetworkGeometry>& geometry,
const QVector<glm::vec3>& vertices, const QVector<glm::vec3>& normals);
protected:
virtual QSharedPointer<Resource> createResource(const QUrl& url,

View file

@ -6,6 +6,10 @@
// Copyright (c) 2013 High Fidelity, Inc. All rights reserved.
//
#include <QMetaType>
#include <QRunnable>
#include <QThreadPool>
#include <glm/gtx/transform.hpp>
#include <GeometryUtil.h>
@ -19,6 +23,10 @@
using namespace std;
static int modelPointerTypeId = qRegisterMetaType<QPointer<Model> >();
static int weakNetworkGeometryPointerTypeId = qRegisterMetaType<QWeakPointer<NetworkGeometry> >();
static int vec3VectorTypeId = qRegisterMetaType<QVector<glm::vec3> >();
Model::Model(QObject* parent) :
QObject(parent),
_scale(1.0f, 1.0f, 1.0f),
@ -104,8 +112,6 @@ void Model::init() {
}
void Model::reset() {
_resetStates = true;
foreach (Model* attachment, _attachments) {
attachment->reset();
}
@ -170,20 +176,10 @@ bool Model::render(float alpha) {
return false;
}
// set up blended buffer ids on first render after load/simulate
// set up dilated textures on first render after load/simulate
const FBXGeometry& geometry = _geometry->getFBXGeometry();
if (_blendedVertexBufferIDs.isEmpty()) {
if (_dilatedTextures.isEmpty()) {
foreach (const FBXMesh& mesh, geometry.meshes) {
GLuint id = 0;
if (!mesh.blendshapes.isEmpty() || mesh.springiness > 0.0f) {
glGenBuffers(1, &id);
glBindBuffer(GL_ARRAY_BUFFER, id);
glBufferData(GL_ARRAY_BUFFER, (mesh.vertices.size() + mesh.normals.size()) * sizeof(glm::vec3),
NULL, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
_blendedVertexBufferIDs.append(id);
QVector<QSharedPointer<Texture> > dilated;
dilated.resize(mesh.parts.size());
_dilatedTextures.append(dilated);
@ -478,6 +474,68 @@ QVector<Model::JointState> Model::updateGeometry() {
return newJointStates;
}
class Blender : public QRunnable {
public:
Blender(Model* model, const QWeakPointer<NetworkGeometry>& geometry,
const QVector<FBXMesh>& meshes, const QVector<float>& blendshapeCoefficients);
virtual void run();
private:
QPointer<Model> _model;
QWeakPointer<NetworkGeometry> _geometry;
QVector<FBXMesh> _meshes;
QVector<float> _blendshapeCoefficients;
};
Blender::Blender(Model* model, const QWeakPointer<NetworkGeometry>& geometry,
const QVector<FBXMesh>& meshes, const QVector<float>& blendshapeCoefficients) :
_model(model),
_geometry(geometry),
_meshes(meshes),
_blendshapeCoefficients(blendshapeCoefficients) {
}
void Blender::run() {
// make sure the model/geometry still exists
if (_model.isNull() || _geometry.isNull()) {
return;
}
QVector<glm::vec3> vertices, normals;
int offset = 0;
foreach (const FBXMesh& mesh, _meshes) {
if (mesh.blendshapes.isEmpty()) {
continue;
}
vertices += mesh.vertices;
normals += mesh.normals;
glm::vec3* meshVertices = vertices.data() + offset;
glm::vec3* meshNormals = normals.data() + offset;
offset += mesh.vertices.size();
const float NORMAL_COEFFICIENT_SCALE = 0.01f;
for (int i = 0, n = qMin(_blendshapeCoefficients.size(), mesh.blendshapes.size()); i < n; i++) {
float vertexCoefficient = _blendshapeCoefficients.at(i);
if (vertexCoefficient < EPSILON) {
continue;
}
float normalCoefficient = vertexCoefficient * NORMAL_COEFFICIENT_SCALE;
const FBXBlendshape& blendshape = mesh.blendshapes.at(i);
for (int j = 0; j < blendshape.indices.size(); j++) {
int index = blendshape.indices.at(j);
meshVertices[index] += blendshape.vertices.at(j) * vertexCoefficient;
meshNormals[index] += blendshape.normals.at(j) * normalCoefficient;
}
}
}
// post the result to the geometry cache, which will dispatch to the model if still alive
QMetaObject::invokeMethod(Application::getInstance()->getGeometryCache(), "setBlendedVertices",
Q_ARG(const QPointer<Model>&, _model), Q_ARG(const QWeakPointer<NetworkGeometry>&, _geometry),
Q_ARG(const QVector<glm::vec3>&, vertices), Q_ARG(const QVector<glm::vec3>&, normals));
}
void Model::simulate(float deltaTime, bool fullUpdate, const QVector<JointState>& newJointStates) {
if (!isActive()) {
return;
@ -490,12 +548,20 @@ void Model::simulate(float deltaTime, bool fullUpdate, const QVector<JointState>
foreach (const FBXMesh& mesh, geometry.meshes) {
MeshState state;
state.clusterMatrices.resize(mesh.clusters.size());
if (mesh.springiness > 0.0f) {
state.worldSpaceVertices.resize(mesh.vertices.size());
state.vertexVelocities.resize(mesh.vertices.size());
state.worldSpaceNormals.resize(mesh.vertices.size());
}
_meshStates.append(state);
QOpenGLBuffer buffer;
if (!mesh.blendshapes.isEmpty()) {
buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
buffer.create();
buffer.bind();
buffer.allocate((mesh.vertices.size() + mesh.normals.size()) * sizeof(glm::vec3));
buffer.write(0, mesh.vertices.constData(), mesh.vertices.size() * sizeof(glm::vec3));
buffer.write(mesh.vertices.size() * sizeof(glm::vec3), mesh.normals.constData(),
mesh.normals.size() * sizeof(glm::vec3));
buffer.release();
}
_blendedVertexBuffers.append(buffer);
}
foreach (const FBXAttachment& attachment, geometry.attachments) {
Model* model = new Model(this);
@ -503,12 +569,12 @@ void Model::simulate(float deltaTime, bool fullUpdate, const QVector<JointState>
model->setURL(attachment.url);
_attachments.append(model);
}
_resetStates = fullUpdate = true;
fullUpdate = true;
createCollisionShapes();
}
// exit early if we don't have to perform a full update
if (!(fullUpdate || _resetStates)) {
if (!fullUpdate) {
return;
}
@ -541,82 +607,12 @@ void Model::simulate(float deltaTime, bool fullUpdate, const QVector<JointState>
const FBXCluster& cluster = mesh.clusters.at(j);
state.clusterMatrices[j] = _jointStates[cluster.jointIndex].transform * cluster.inverseBindMatrix;
}
int vertexCount = state.worldSpaceVertices.size();
if (vertexCount == 0) {
continue;
}
glm::vec3* destVertices = state.worldSpaceVertices.data();
glm::vec3* destVelocities = state.vertexVelocities.data();
glm::vec3* destNormals = state.worldSpaceNormals.data();
const glm::vec3* sourceVertices = mesh.vertices.constData();
if (!mesh.blendshapes.isEmpty()) {
_blendedVertices.resize(max(_blendedVertices.size(), vertexCount));
memcpy(_blendedVertices.data(), mesh.vertices.constData(), vertexCount * sizeof(glm::vec3));
// blend in each coefficient
for (unsigned int j = 0; j < _blendshapeCoefficients.size(); j++) {
float coefficient = _blendshapeCoefficients[j];
if (coefficient == 0.0f || j >= (unsigned int)mesh.blendshapes.size() || mesh.blendshapes[j].vertices.isEmpty()) {
continue;
}
const glm::vec3* vertex = mesh.blendshapes[j].vertices.constData();
for (const int* index = mesh.blendshapes[j].indices.constData(),
*end = index + mesh.blendshapes[j].indices.size(); index != end; index++, vertex++) {
_blendedVertices[*index] += *vertex * coefficient;
}
}
sourceVertices = _blendedVertices.constData();
}
glm::mat4 transform = glm::translate(_translation);
if (mesh.clusters.size() > 1) {
_blendedVertices.resize(max(_blendedVertices.size(), vertexCount));
// skin each vertex
const glm::vec4* clusterIndices = mesh.clusterIndices.constData();
const glm::vec4* clusterWeights = mesh.clusterWeights.constData();
for (int j = 0; j < vertexCount; j++) {
_blendedVertices[j] =
glm::vec3(state.clusterMatrices[clusterIndices[j][0]] *
glm::vec4(sourceVertices[j], 1.0f)) * clusterWeights[j][0] +
glm::vec3(state.clusterMatrices[clusterIndices[j][1]] *
glm::vec4(sourceVertices[j], 1.0f)) * clusterWeights[j][1] +
glm::vec3(state.clusterMatrices[clusterIndices[j][2]] *
glm::vec4(sourceVertices[j], 1.0f)) * clusterWeights[j][2] +
glm::vec3(state.clusterMatrices[clusterIndices[j][3]] *
glm::vec4(sourceVertices[j], 1.0f)) * clusterWeights[j][3];
}
sourceVertices = _blendedVertices.constData();
} else {
transform = state.clusterMatrices[0];
}
if (_resetStates) {
for (int j = 0; j < vertexCount; j++) {
destVertices[j] = glm::vec3(transform * glm::vec4(sourceVertices[j], 1.0f));
destVelocities[j] = glm::vec3();
}
} else {
const float SPRINGINESS_MULTIPLIER = 200.0f;
const float DAMPING = 5.0f;
for (int j = 0; j < vertexCount; j++) {
destVelocities[j] += ((glm::vec3(transform * glm::vec4(sourceVertices[j], 1.0f)) - destVertices[j]) *
mesh.springiness * SPRINGINESS_MULTIPLIER - destVelocities[j] * DAMPING) * deltaTime;
destVertices[j] += destVelocities[j] * deltaTime;
}
}
for (int j = 0; j < vertexCount; j++) {
destNormals[j] = glm::vec3();
const glm::vec3& middle = destVertices[j];
for (QVarLengthArray<QPair<int, int>, 4>::const_iterator connection = mesh.vertexConnections.at(j).constBegin();
connection != mesh.vertexConnections.at(j).constEnd(); connection++) {
destNormals[j] += glm::normalize(glm::cross(destVertices[connection->second] - middle,
destVertices[connection->first] - middle));
}
}
}
_resetStates = false;
// post the blender
if (geometry.hasBlendedMeshes()) {
QThreadPool::globalInstance()->start(new Blender(this, _geometry, geometry.meshes, _blendshapeCoefficients));
}
}
void Model::updateJointState(int index) {
@ -915,6 +911,27 @@ void Model::applyCollision(CollisionInfo& collision) {
}
}
void Model::setBlendedVertices(const QVector<glm::vec3>& vertices, const QVector<glm::vec3>& normals) {
if (_blendedVertexBuffers.isEmpty()) {
return;
}
const FBXGeometry& geometry = _geometry->getFBXGeometry();
int index = 0;
for (int i = 0; i < geometry.meshes.size(); i++) {
const FBXMesh& mesh = geometry.meshes.at(i);
if (mesh.blendshapes.isEmpty()) {
continue;
}
QOpenGLBuffer& buffer = _blendedVertexBuffers[i];
buffer.bind();
buffer.write(0, vertices.constData() + index, mesh.vertices.size() * sizeof(glm::vec3));
buffer.write(mesh.vertices.size() * sizeof(glm::vec3), normals.constData() + index,
mesh.normals.size() * sizeof(glm::vec3));
buffer.release();
index += mesh.vertices.size();
}
}
void Model::applyNextGeometry() {
// delete our local geometry and custom textures
deleteGeometry();
@ -933,10 +950,7 @@ void Model::deleteGeometry() {
delete attachment;
}
_attachments.clear();
foreach (GLuint id, _blendedVertexBufferIDs) {
glDeleteBuffers(1, &id);
}
_blendedVertexBufferIDs.clear();
_blendedVertexBuffers.clear();
_jointStates.clear();
_meshStates.clear();
clearShapes();
@ -980,34 +994,30 @@ void Model::renderMeshes(float alpha, bool translucent) {
const MeshState& state = _meshStates.at(i);
ProgramObject* activeProgram = program;
int tangentLocation = _normalMapTangentLocation;
if (state.worldSpaceVertices.isEmpty()) {
glPushMatrix();
Application::getInstance()->loadTranslatedViewMatrix(_translation);
if (state.clusterMatrices.size() > 1) {
skinProgram->bind();
glUniformMatrix4fvARB(skinLocations->clusterMatrices, state.clusterMatrices.size(), false,
(const float*)state.clusterMatrices.constData());
int offset = (mesh.tangents.size() + mesh.colors.size()) * sizeof(glm::vec3) +
mesh.texCoords.size() * sizeof(glm::vec2) +
(mesh.blendshapes.isEmpty() ? vertexCount * 2 * sizeof(glm::vec3) : 0);
skinProgram->setAttributeBuffer(skinLocations->clusterIndices, GL_FLOAT, offset, 4);
skinProgram->setAttributeBuffer(skinLocations->clusterWeights, GL_FLOAT,
offset + vertexCount * sizeof(glm::vec4), 4);
skinProgram->enableAttributeArray(skinLocations->clusterIndices);
skinProgram->enableAttributeArray(skinLocations->clusterWeights);
activeProgram = skinProgram;
tangentLocation = skinLocations->tangent;
} else {
glMultMatrixf((const GLfloat*)&state.clusterMatrices[0]);
program->bind();
}
} else {
glPushMatrix();
Application::getInstance()->loadTranslatedViewMatrix(_translation);
if (state.clusterMatrices.size() > 1) {
skinProgram->bind();
glUniformMatrix4fvARB(skinLocations->clusterMatrices, state.clusterMatrices.size(), false,
(const float*)state.clusterMatrices.constData());
int offset = (mesh.tangents.size() + mesh.colors.size()) * sizeof(glm::vec3) +
mesh.texCoords.size() * sizeof(glm::vec2) +
(mesh.blendshapes.isEmpty() ? vertexCount * 2 * sizeof(glm::vec3) : 0);
skinProgram->setAttributeBuffer(skinLocations->clusterIndices, GL_FLOAT, offset, 4);
skinProgram->setAttributeBuffer(skinLocations->clusterWeights, GL_FLOAT,
offset + vertexCount * sizeof(glm::vec4), 4);
skinProgram->enableAttributeArray(skinLocations->clusterIndices);
skinProgram->enableAttributeArray(skinLocations->clusterWeights);
activeProgram = skinProgram;
tangentLocation = skinLocations->tangent;
} else {
glMultMatrixf((const GLfloat*)&state.clusterMatrices[0]);
program->bind();
}
if (mesh.blendshapes.isEmpty() && mesh.springiness == 0.0f) {
if (mesh.blendshapes.isEmpty()) {
if (!mesh.tangents.isEmpty()) {
activeProgram->setAttributeBuffer(tangentLocation, GL_FLOAT, vertexCount * 2 * sizeof(glm::vec3), 3);
activeProgram->enableAttributeArray(tangentLocation);
@ -1024,40 +1034,7 @@ void Model::renderMeshes(float alpha, bool translucent) {
}
glColorPointer(3, GL_FLOAT, 0, (void*)(mesh.tangents.size() * sizeof(glm::vec3)));
glTexCoordPointer(2, GL_FLOAT, 0, (void*)((mesh.tangents.size() + mesh.colors.size()) * sizeof(glm::vec3)));
glBindBuffer(GL_ARRAY_BUFFER, _blendedVertexBufferIDs.at(i));
if (!state.worldSpaceVertices.isEmpty()) {
glBufferSubData(GL_ARRAY_BUFFER, 0, vertexCount * sizeof(glm::vec3), state.worldSpaceVertices.constData());
glBufferSubData(GL_ARRAY_BUFFER, vertexCount * sizeof(glm::vec3),
vertexCount * sizeof(glm::vec3), state.worldSpaceNormals.constData());
} else {
_blendedVertices.resize(max(_blendedVertices.size(), vertexCount));
_blendedNormals.resize(_blendedVertices.size());
memcpy(_blendedVertices.data(), mesh.vertices.constData(), vertexCount * sizeof(glm::vec3));
memcpy(_blendedNormals.data(), mesh.normals.constData(), vertexCount * sizeof(glm::vec3));
// blend in each coefficient
for (unsigned int j = 0; j < _blendshapeCoefficients.size(); j++) {
float coefficient = _blendshapeCoefficients[j];
if (coefficient == 0.0f || j >= (unsigned int)mesh.blendshapes.size() || mesh.blendshapes[j].vertices.isEmpty()) {
continue;
}
const float NORMAL_COEFFICIENT_SCALE = 0.01f;
float normalCoefficient = coefficient * NORMAL_COEFFICIENT_SCALE;
const glm::vec3* vertex = mesh.blendshapes[j].vertices.constData();
const glm::vec3* normal = mesh.blendshapes[j].normals.constData();
for (const int* index = mesh.blendshapes[j].indices.constData(),
*end = index + mesh.blendshapes[j].indices.size(); index != end; index++, vertex++, normal++) {
_blendedVertices[*index] += *vertex * coefficient;
_blendedNormals[*index] += *normal * normalCoefficient;
}
}
glBufferSubData(GL_ARRAY_BUFFER, 0, vertexCount * sizeof(glm::vec3), _blendedVertices.constData());
glBufferSubData(GL_ARRAY_BUFFER, vertexCount * sizeof(glm::vec3),
vertexCount * sizeof(glm::vec3), _blendedNormals.constData());
}
_blendedVertexBuffers[i].bind();
}
glVertexPointer(3, GL_FLOAT, 0, 0);
glNormalPointer(GL_FLOAT, 0, (void*)(vertexCount * sizeof(glm::vec3)));
@ -1126,14 +1103,13 @@ void Model::renderMeshes(float alpha, bool translucent) {
activeProgram->disableAttributeArray(tangentLocation);
}
if (state.worldSpaceVertices.isEmpty()) {
if (state.clusterMatrices.size() > 1) {
skinProgram->disableAttributeArray(skinLocations->clusterIndices);
skinProgram->disableAttributeArray(skinLocations->clusterWeights);
}
glPopMatrix();
}
if (state.clusterMatrices.size() > 1) {
skinProgram->disableAttributeArray(skinLocations->clusterIndices);
skinProgram->disableAttributeArray(skinLocations->clusterWeights);
}
glPopMatrix();
activeProgram->release();
}
}

View file

@ -43,8 +43,8 @@ public:
void setPupilDilation(float dilation) { _pupilDilation = dilation; }
float getPupilDilation() const { return _pupilDilation; }
void setBlendshapeCoefficients(const std::vector<float>& coefficients) { _blendshapeCoefficients = coefficients; }
const std::vector<float>& getBlendshapeCoefficients() const { return _blendshapeCoefficients; }
void setBlendshapeCoefficients(const QVector<float>& coefficients) { _blendshapeCoefficients = coefficients; }
const QVector<float>& getBlendshapeCoefficients() const { return _blendshapeCoefficients; }
bool isActive() const { return _geometry && _geometry->isLoaded(); }
@ -195,6 +195,9 @@ public:
/// Use the collision to affect the model
void applyCollision(CollisionInfo& collision);
/// Sets blended vertices computed in a separate thread.
void setBlendedVertices(const QVector<glm::vec3>& vertices, const QVector<glm::vec3>& normals);
protected:
QSharedPointer<NetworkGeometry> _geometry;
@ -219,9 +222,6 @@ protected:
class MeshState {
public:
QVector<glm::mat4> clusterMatrices;
QVector<glm::vec3> worldSpaceVertices;
QVector<glm::vec3> vertexVelocities;
QVector<glm::vec3> worldSpaceNormals;
};
QVector<MeshState> _meshStates;
@ -271,16 +271,13 @@ private:
float _nextLODHysteresis;
float _pupilDilation;
std::vector<float> _blendshapeCoefficients;
QVector<float> _blendshapeCoefficients;
QUrl _url;
QVector<GLuint> _blendedVertexBufferIDs;
QVector<QVector<QSharedPointer<Texture> > > _dilatedTextures;
bool _resetStates;
QVector<QOpenGLBuffer> _blendedVertexBuffers;
QVector<glm::vec3> _blendedVertices;
QVector<glm::vec3> _blendedNormals;
QVector<QVector<QSharedPointer<Texture> > > _dilatedTextures;
QVector<Model*> _attachments;
@ -306,4 +303,8 @@ private:
static QVector<JointState> createJointStates(const FBXGeometry& geometry);
};
Q_DECLARE_METATYPE(QPointer<Model>)
Q_DECLARE_METATYPE(QWeakPointer<NetworkGeometry>)
Q_DECLARE_METATYPE(QVector<glm::vec3>)
#endif /* defined(__interface__Model__) */

View file

@ -38,9 +38,6 @@ TextureCache::~TextureCache() {
if (_whiteTextureID != 0) {
glDeleteTextures(1, &_whiteTextureID);
}
foreach (GLuint id, _fileTextureIDs) {
glDeleteTextures(1, &id);
}
if (_primaryFramebufferObject) {
glDeleteTextures(1, &_primaryDepthTextureID);
}
@ -104,23 +101,6 @@ GLuint TextureCache::getBlueTextureID() {
return _blueTextureID;
}
GLuint TextureCache::getFileTextureID(const QString& filename) {
GLuint id = _fileTextureIDs.value(filename);
if (id == 0) {
QImage image = QImage(filename).convertToFormat(QImage::Format_ARGB32);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 1,
GL_BGRA, GL_UNSIGNED_BYTE, image.constBits());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
_fileTextureIDs.insert(filename, id);
}
return id;
}
QSharedPointer<NetworkTexture> TextureCache::getTexture(const QUrl& url, bool normalMap, bool dilatable) {
if (!dilatable) {
return ResourceCache::getResource(url, QUrl(), false, &normalMap).staticCast<NetworkTexture>();
@ -293,27 +273,50 @@ void ImageReader::run() {
_reply->deleteLater();
return;
}
QUrl url = _reply->url();
QImage image = QImage::fromData(_reply->readAll());
_reply->deleteLater();
// enforce a fixed maximum
const int MAXIMUM_SIZE = 1024;
if (image.width() > MAXIMUM_SIZE || image.height() > MAXIMUM_SIZE) {
qDebug() << "Image greater than maximum size:" << url << image.width() << image.height();
image = image.scaled(MAXIMUM_SIZE, MAXIMUM_SIZE, Qt::KeepAspectRatio);
}
if (!image.hasAlphaChannel()) {
if (image.format() != QImage::Format_RGB888) {
image = image.convertToFormat(QImage::Format_RGB888);
}
QMetaObject::invokeMethod(texture.data(), "setImage", Q_ARG(const QImage&, image), Q_ARG(bool, false));
return;
}
if (image.format() != QImage::Format_ARGB32) {
image = image.convertToFormat(QImage::Format_ARGB32);
}
// check for translucency
// check for translucency/false transparency
int opaquePixels = 0;
int translucentPixels = 0;
const int EIGHT_BIT_MAXIMUM = 255;
const int RGB_BITS = 24;
for (int y = 0; y < image.height(); y++) {
for (int x = 0; x < image.width(); x++) {
int alpha = image.pixel(x, y) >> RGB_BITS;
if (alpha != 0 && alpha != EIGHT_BIT_MAXIMUM) {
if (alpha == EIGHT_BIT_MAXIMUM) {
opaquePixels++;
} else if (alpha != 0) {
translucentPixels++;
}
}
}
int imageArea = image.width() * image.height();
if (opaquePixels == imageArea) {
qDebug() << "Image with alpha channel is completely opaque:" << url;
image.convertToFormat(QImage::Format_RGB888);
}
QMetaObject::invokeMethod(texture.data(), "setImage", Q_ARG(const QImage&, image),
Q_ARG(bool, translucentPixels >= imageArea / 2));
_reply->deleteLater();
}
void NetworkTexture::downloadFinished(QNetworkReply* reply) {
@ -327,8 +330,13 @@ void NetworkTexture::setImage(const QImage& image, bool translucent) {
finishedLoading(true);
imageLoaded(image);
glBindTexture(GL_TEXTURE_2D, getID());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 1,
GL_BGRA, GL_UNSIGNED_BYTE, image.constBits());
if (image.hasAlphaChannel()) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 1,
GL_BGRA, GL_UNSIGNED_BYTE, image.constBits());
} else {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width(), image.height(), 1,
GL_RGB, GL_UNSIGNED_BYTE, image.constBits());
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
}
@ -360,8 +368,13 @@ QSharedPointer<Texture> DilatableNetworkTexture::getDilatedTexture(float dilatio
painter.end();
glBindTexture(GL_TEXTURE_2D, texture->getID());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dilatedImage.width(), dilatedImage.height(), 1,
GL_BGRA, GL_UNSIGNED_BYTE, dilatedImage.constBits());
if (dilatedImage.hasAlphaChannel()) {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dilatedImage.width(), dilatedImage.height(), 1,
GL_BGRA, GL_UNSIGNED_BYTE, dilatedImage.constBits());
} else {
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, dilatedImage.width(), dilatedImage.height(), 1,
GL_RGB, GL_UNSIGNED_BYTE, dilatedImage.constBits());
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
}

View file

@ -27,7 +27,7 @@ class TextureCache : public ResourceCache {
public:
TextureCache();
~TextureCache();
virtual ~TextureCache();
/// Returns the ID of the permutation/normal texture used for Perlin noise shader programs. This texture
/// has two lines: the first, a set of random numbers in [0, 255] to be used as permutation offsets, and
@ -39,9 +39,6 @@ public:
/// Returns the ID of a pale blue texture (useful for a normal map).
GLuint getBlueTextureID();
/// Returns the ID of a texture containing the contents of the specified file, loading it if necessary.
GLuint getFileTextureID(const QString& filename);
/// Loads a texture from the specified URL.
QSharedPointer<NetworkTexture> getTexture(const QUrl& url, bool normalMap = false, bool dilatable = false);
@ -84,8 +81,6 @@ private:
GLuint _whiteTextureID;
GLuint _blueTextureID;
QHash<QString, GLuint> _fileTextureIDs;
QHash<QUrl, QWeakPointer<NetworkTexture> > _dilatableNetworkTextures;
GLuint _primaryDepthTextureID;

View file

@ -156,6 +156,21 @@ unsigned int AudioRingBuffer::samplesAvailable() const {
}
}
void AudioRingBuffer::addSilentFrame(int numSilentSamples) {
// memset zeroes into the buffer, accomodate a wrap around the end
// push the _endOfLastWrite to the correct spot
if (_endOfLastWrite + numSilentSamples <= _buffer + _sampleCapacity) {
memset(_endOfLastWrite, 0, numSilentSamples * sizeof(int16_t));
_endOfLastWrite += numSilentSamples;
} else {
int numSamplesToEnd = (_buffer + _sampleCapacity) - _endOfLastWrite;
memset(_endOfLastWrite, 0, numSamplesToEnd * sizeof(int16_t));
memset(_buffer, 0, (numSilentSamples - numSamplesToEnd) * sizeof(int16_t));
_endOfLastWrite = _buffer + (numSilentSamples - numSamplesToEnd);
}
}
bool AudioRingBuffer::isNotStarvedOrHasMinimumSamples(unsigned int numRequiredSamples) const {
if (!_isStarved) {
return true;

View file

@ -70,6 +70,8 @@ public:
void setIsStarved(bool isStarved) { _isStarved = isStarved; }
bool hasStarted() const { return _hasStarted; }
void addSilentFrame(int numSilentSamples);
protected:
// disallow copying of AudioRingBuffer objects
AudioRingBuffer(const AudioRingBuffer&);

View file

@ -38,16 +38,27 @@ PositionalAudioRingBuffer::~PositionalAudioRingBuffer() {
}
int PositionalAudioRingBuffer::parseData(const QByteArray& packet) {
QDataStream packetStream(packet);
// skip the packet header (includes the source UUID)
packetStream.skipRawData(numBytesForPacketHeader(packet));
int readBytes = numBytesForPacketHeader(packet);
packetStream.skipRawData(parsePositionalData(packet.mid(packetStream.device()->pos())));
packetStream.skipRawData(writeData(packet.data() + packetStream.device()->pos(),
packet.size() - packetStream.device()->pos()));
return packetStream.device()->pos();
readBytes += parsePositionalData(packet.mid(readBytes));
if (packetTypeForPacket(packet) == PacketTypeSilentAudioFrame) {
// this source had no audio to send us, but this counts as a packet
// write silence equivalent to the number of silent samples they just sent us
int16_t numSilentSamples;
memcpy(&numSilentSamples, packet.data() + readBytes, sizeof(int16_t));
readBytes += sizeof(int16_t);
addSilentFrame(numSilentSamples);
} else {
// there is audio data to read
readBytes += writeData(packet.data() + readBytes, packet.size() - readBytes);
}
return readBytes;
}
int PositionalAudioRingBuffer::parsePositionalData(const QByteArray& positionalByteArray) {

View file

@ -29,7 +29,6 @@ using namespace std;
QNetworkAccessManager* AvatarData::networkAccessManager = NULL;
AvatarData::AvatarData() :
NodeData(),
_handPosition(0,0,0),
_bodyYaw(-90.f),
_bodyPitch(0.0f),
@ -94,18 +93,12 @@ QByteArray AvatarData::toByteArray() {
destinationBuffer += packFloatAngleToTwoByte(destinationBuffer, _headData->getTweakedPitch());
destinationBuffer += packFloatAngleToTwoByte(destinationBuffer, _headData->getTweakedRoll());
// Head lean X,Z (head lateral and fwd/back motion relative to torso)
memcpy(destinationBuffer, &_headData->_leanSideways, sizeof(_headData->_leanSideways));
destinationBuffer += sizeof(_headData->_leanSideways);
memcpy(destinationBuffer, &_headData->_leanForward, sizeof(_headData->_leanForward));
destinationBuffer += sizeof(_headData->_leanForward);
// Hand Position - is relative to body position
glm::vec3 handPositionRelative = _handPosition - _position;
memcpy(destinationBuffer, &handPositionRelative, sizeof(float) * 3);
destinationBuffer += sizeof(float) * 3;
// Lookat Position
memcpy(destinationBuffer, &_headData->_lookAtPosition, sizeof(_headData->_lookAtPosition));
destinationBuffer += sizeof(_headData->_lookAtPosition);
@ -178,14 +171,11 @@ QByteArray AvatarData::toByteArray() {
}
}
// hand data
destinationBuffer += HandData::encodeData(_handData, destinationBuffer);
return avatarDataByteArray.left(destinationBuffer - startPosition);
}
// called on the other nodes - assigns it to my views of the others
int AvatarData::parseData(const QByteArray& packet) {
// read data in packet starting at byte offset and return number of bytes parsed
int AvatarData::parseDataAtOffset(const QByteArray& packet, int offset) {
// lazily allocate memory for HeadData in case we're not an Avatar instance
if (!_headData) {
@ -197,9 +187,8 @@ int AvatarData::parseData(const QByteArray& packet) {
_handData = new HandData(this);
}
// increment to push past the packet header
const unsigned char* startPosition = reinterpret_cast<const unsigned char*>(packet.data());
const unsigned char* sourceBuffer = startPosition + numBytesForPacketHeader(packet);
const unsigned char* startPosition = reinterpret_cast<const unsigned char*>(packet.data()) + offset;
const unsigned char* sourceBuffer = startPosition;
// Body world position
memcpy(&_position, sourceBuffer, sizeof(float) * 3);
@ -218,7 +207,6 @@ int AvatarData::parseData(const QByteArray& packet) {
sourceBuffer += unpackFloatAngleFromTwoByte((uint16_t*) sourceBuffer, &headYaw);
sourceBuffer += unpackFloatAngleFromTwoByte((uint16_t*) sourceBuffer, &headPitch);
sourceBuffer += unpackFloatAngleFromTwoByte((uint16_t*) sourceBuffer, &headRoll);
_headData->setYaw(headYaw);
_headData->setPitch(headPitch);
_headData->setRoll(headRoll);
@ -229,12 +217,6 @@ int AvatarData::parseData(const QByteArray& packet) {
memcpy(&_headData->_leanForward, sourceBuffer, sizeof(_headData->_leanForward));
sourceBuffer += sizeof(_headData->_leanForward);
// Hand Position - is relative to body position
glm::vec3 handPositionRelative;
memcpy(&handPositionRelative, sourceBuffer, sizeof(float) * 3);
_handPosition = _position + handPositionRelative;
sourceBuffer += sizeof(float) * 3;
// Lookat Position
memcpy(&_headData->_lookAtPosition, sourceBuffer, sizeof(_headData->_lookAtPosition));
sourceBuffer += sizeof(_headData->_lookAtPosition);
@ -288,13 +270,13 @@ int AvatarData::parseData(const QByteArray& packet) {
// joint data
int jointCount = *sourceBuffer++;
_jointData.resize(jointCount);
unsigned char validity = 0; // although always set below, this fixes a warning of potential uninitialized use
unsigned char validity = 0;
int validityBit = 0;
for (int i = 0; i < jointCount; i++) {
if (validityBit == 0) {
validity = *sourceBuffer++;
}
_jointData[i].valid = validity & (1 << validityBit);
_jointData[i].valid = (bool)(validity & (1 << validityBit));
validityBit = (validityBit + 1) % BITS_IN_BYTE;
}
for (int i = 0; i < jointCount; i++) {
@ -304,12 +286,6 @@ int AvatarData::parseData(const QByteArray& packet) {
}
}
// hand data
if (sourceBuffer - startPosition < packet.size()) {
// check passed, bytes match
sourceBuffer += _handData->decodeRemoteData(packet.mid(sourceBuffer - startPosition));
}
return sourceBuffer - startPosition;
}

View file

@ -40,7 +40,6 @@ typedef unsigned long long quint64;
#include <CollisionInfo.h>
#include <RegisteredMetaTypes.h>
#include <NodeData.h>
#include "HeadData.h"
#include "HandData.h"
@ -74,7 +73,7 @@ class QNetworkAccessManager;
class JointData;
class AvatarData : public NodeData {
class AvatarData : public QObject {
Q_OBJECT
Q_PROPERTY(glm::vec3 position READ getPosition WRITE setPosition)
@ -97,7 +96,7 @@ class AvatarData : public NodeData {
Q_PROPERTY(QString billboardURL READ getBillboardURL WRITE setBillboardFromURL)
public:
AvatarData();
~AvatarData();
virtual ~AvatarData();
const glm::vec3& getPosition() const { return _position; }
void setPosition(const glm::vec3 position) { _position = position; }
@ -106,7 +105,11 @@ public:
void setHandPosition(const glm::vec3& handPosition);
QByteArray toByteArray();
int parseData(const QByteArray& packet);
/// \param packet byte array of data
/// \param offset number of bytes into packet where data starts
/// \return number of bytes parsed
virtual int parseDataAtOffset(const QByteArray& packet, int offset);
// Body Rotation (degrees)
float getBodyYaw() const { return _bodyYaw; }

View file

@ -8,14 +8,11 @@
#include <QtCore/QDataStream>
#include "HandData.h"
#include "AvatarData.h"
#include <SharedUtil.h>
#include <GeometryUtil.h>
#include <SharedUtil.h>
// When converting between fixed and float, use this as the radix.
const int fingerVectorRadix = 4;
#include "AvatarData.h"
#include "HandData.h"
HandData::HandData(AvatarData* owningAvatar) :
_owningAvatarData(owningAvatar)
@ -109,123 +106,6 @@ _owningHandData(owningHandData)
setTrailLength(standardTrailLength);
}
// static
int HandData::encodeData(HandData* hand, unsigned char* destinationBuffer) {
if (hand) {
return hand->encodeRemoteData(destinationBuffer);
}
// else encode empty data:
// One byte for zero hands
// One byte for error checking.
*destinationBuffer = 0;
*(destinationBuffer + 1) = 1;
return 2;
}
int HandData::encodeRemoteData(unsigned char* destinationBuffer) {
const unsigned char* startPosition = destinationBuffer;
unsigned int numPalms = 0;
for (unsigned int handIndex = 0; handIndex < getNumPalms(); ++handIndex) {
PalmData& palm = getPalms()[handIndex];
if (palm.isActive()) {
numPalms++;
}
}
*destinationBuffer++ = numPalms;
for (unsigned int handIndex = 0; handIndex < getNumPalms(); ++handIndex) {
PalmData& palm = getPalms()[handIndex];
if (palm.isActive()) {
destinationBuffer += packFloatVec3ToSignedTwoByteFixed(destinationBuffer, palm.getRawPosition(), fingerVectorRadix);
destinationBuffer += packFloatVec3ToSignedTwoByteFixed(destinationBuffer, palm.getRawNormal(), fingerVectorRadix);
unsigned int numFingers = 0;
for (unsigned int fingerIndex = 0; fingerIndex < palm.getNumFingers(); ++fingerIndex) {
FingerData& finger = palm.getFingers()[fingerIndex];
if (finger.isActive()) {
numFingers++;
}
}
*destinationBuffer++ = numFingers;
for (unsigned int fingerIndex = 0; fingerIndex < palm.getNumFingers(); ++fingerIndex) {
FingerData& finger = palm.getFingers()[fingerIndex];
if (finger.isActive()) {
destinationBuffer += packFloatVec3ToSignedTwoByteFixed(destinationBuffer, finger.getTipRawPosition(), fingerVectorRadix);
destinationBuffer += packFloatVec3ToSignedTwoByteFixed(destinationBuffer, finger.getRootRawPosition(), fingerVectorRadix);
}
}
}
}
// One byte for error checking safety.
size_t checkLength = destinationBuffer - startPosition;
*destinationBuffer++ = (unsigned char)checkLength;
// just a double-check, while tracing a crash.
// decodeRemoteData(destinationBuffer - (destinationBuffer - startPosition));
return destinationBuffer - startPosition;
}
int HandData::decodeRemoteData(const QByteArray& dataByteArray) {
const unsigned char* startPosition;
const unsigned char* sourceBuffer = startPosition = reinterpret_cast<const unsigned char*>(dataByteArray.data());
unsigned int numPalms = *sourceBuffer++;
for (unsigned int handIndex = 0; handIndex < numPalms; ++handIndex) {
if (handIndex >= (unsigned int)getNumPalms())
addNewPalm();
PalmData& palm = getPalms()[handIndex];
glm::vec3 handPosition;
glm::vec3 handNormal;
sourceBuffer += unpackFloatVec3FromSignedTwoByteFixed(sourceBuffer, handPosition, fingerVectorRadix);
sourceBuffer += unpackFloatVec3FromSignedTwoByteFixed(sourceBuffer, handNormal, fingerVectorRadix);
unsigned int numFingers = *sourceBuffer++;
palm.setRawPosition(handPosition);
palm.setRawNormal(handNormal);
palm.setActive(true);
// For received data, set the sixense controller ID to match the order initialized and sent - 0 Left, 1 Right
palm.setSixenseID(handIndex);
for (unsigned int fingerIndex = 0; fingerIndex < numFingers; ++fingerIndex) {
if (fingerIndex < (unsigned int)palm.getNumFingers()) {
FingerData& finger = palm.getFingers()[fingerIndex];
glm::vec3 tipPosition;
glm::vec3 rootPosition;
sourceBuffer += unpackFloatVec3FromSignedTwoByteFixed(sourceBuffer, tipPosition, fingerVectorRadix);
sourceBuffer += unpackFloatVec3FromSignedTwoByteFixed(sourceBuffer, rootPosition, fingerVectorRadix);
finger.setRawTipPosition(tipPosition);
finger.setRawRootPosition(rootPosition);
finger.setActive(true);
}
}
// Turn off any fingers which weren't used.
for (unsigned int fingerIndex = numFingers; fingerIndex < palm.getNumFingers(); ++fingerIndex) {
FingerData& finger = palm.getFingers()[fingerIndex];
finger.setActive(false);
}
}
// Turn off any hands which weren't used.
for (unsigned int handIndex = numPalms; handIndex < getNumPalms(); ++handIndex) {
PalmData& palm = getPalms()[handIndex];
palm.setActive(false);
}
// One byte for error checking safety. Last byte contains the expected length (less itself)
// actualLength less expected byte = (sourceBuffer - startPosition)
// expectedLength less expected byte = (*sourceBuffer)
assert((unsigned char)(sourceBuffer - startPosition) == (unsigned char)(*sourceBuffer));
sourceBuffer++; // skip the trailing byte which is expected length
return sourceBuffer - startPosition;
}
void HandData::setFingerTrailLength(unsigned int length) {
for (size_t i = 0; i < getNumPalms(); ++i) {
PalmData& palm = getPalms()[i];

View file

@ -63,12 +63,6 @@ public:
void setFingerTrailLength(unsigned int length);
void updateFingerTrails();
static int encodeData(HandData* hand, unsigned char* destinationBuffer);
// Use these for sending and receiving hand data
int encodeRemoteData(unsigned char* destinationBuffer);
int decodeRemoteData(const QByteArray& dataByteArray);
/// Checks for penetration between the described sphere and the hand.
/// \param penetratorCenter the center of the penetration test sphere
/// \param penetratorRadius the radius of the penetration test sphere

View file

@ -10,7 +10,8 @@
#define __hifi__HeadData__
#include <iostream>
#include <vector>
#include <QVector>
#include <glm/glm.hpp>
#include <glm/gtc/quaternion.hpp>
@ -54,7 +55,7 @@ public:
float getAudioAverageLoudness() const { return _audioAverageLoudness; }
void setAudioAverageLoudness(float audioAverageLoudness) { _audioAverageLoudness = audioAverageLoudness; }
const std::vector<float>& getBlendshapeCoefficients() const { return _blendshapeCoefficients; }
const QVector<float>& getBlendshapeCoefficients() const { return _blendshapeCoefficients; }
float getPupilDilation() const { return _pupilDilation; }
void setPupilDilation(float pupilDilation) { _pupilDilation = pupilDilation; }
@ -86,7 +87,7 @@ protected:
float _averageLoudness;
float _browAudioLift;
float _audioAverageLoudness;
std::vector<float> _blendshapeCoefficients;
QVector<float> _blendshapeCoefficients;
float _pupilDilation;
AvatarData* _owningAvatar;

View file

@ -1096,7 +1096,7 @@ QScriptValue ScriptedMetavoxelGuide::visit(QScriptContext* context, QScriptEngin
QScriptValue minimum = infoValue.property(guide->_minimumHandle);
MetavoxelInfo info = {
glm::vec3(minimum.property(0).toNumber(), minimum.property(1).toNumber(), minimum.property(2).toNumber()),
infoValue.property(guide->_sizeHandle).toNumber(), guide->_visitation->info.inputValues,
float(infoValue.property(guide->_sizeHandle).toNumber()), guide->_visitation->info.inputValues,
guide->_visitation->info.outputValues, infoValue.property(guide->_isLeafHandle).toBool() };
// extract and convert the values provided by the script

View file

@ -355,15 +355,7 @@ void Octree::deleteOctalCodeFromTree(const unsigned char* codeBuffer, bool colla
OctreeElement* node = _rootNode;
// We can't encode and delete nodes at the same time, so we guard against deleting any node that is actively
// being encoded. And we stick that code on our pendingDelete list.
if (isEncoding(codeBuffer)) {
queueForLaterDelete(codeBuffer);
} else {
startDeleting(codeBuffer);
deleteOctalCodeFromTreeRecursion(node, &args);
doneDeleting(codeBuffer);
}
deleteOctalCodeFromTreeRecursion(node, &args);
}
void Octree::deleteOctalCodeFromTreeRecursion(OctreeElement* node, void* extraData) {
@ -796,11 +788,8 @@ int Octree::encodeTreeBitstream(OctreeElement* node,
return bytesWritten;
}
startEncoding(node);
// If we're at a node that is out of view, then we can return, because no nodes below us will be in view!
if (params.viewFrustum && !node->isInView(*params.viewFrustum)) {
doneEncoding(node);
params.stopReason = EncodeBitstreamParams::OUT_OF_VIEW;
return bytesWritten;
}
@ -825,7 +814,6 @@ int Octree::encodeTreeBitstream(OctreeElement* node,
// If the octalcode couldn't fit, then we can return, because no nodes below us will fit...
if (!roomForOctalCode) {
doneEncoding(node);
bag.insert(node); // add the node back to the bag so it will eventually get included
params.stopReason = EncodeBitstreamParams::DIDNT_FIT;
return bytesWritten;
@ -841,7 +829,10 @@ int Octree::encodeTreeBitstream(OctreeElement* node,
params.stats->traversed(node);
}
int childBytesWritten = encodeTreeBitstreamRecursion(node, packetData, bag, params, currentEncodeLevel);
ViewFrustum::location parentLocationThisView = ViewFrustum::INTERSECT; // assume parent is in view, but not fully
int childBytesWritten = encodeTreeBitstreamRecursion(node, packetData, bag, params,
currentEncodeLevel, parentLocationThisView);
// if childBytesWritten == 1 then something went wrong... that's not possible
assert(childBytesWritten != 1);
@ -868,14 +859,13 @@ int Octree::encodeTreeBitstream(OctreeElement* node,
packetData->endSubTree();
}
doneEncoding(node);
return bytesWritten;
}
int Octree::encodeTreeBitstreamRecursion(OctreeElement* node,
OctreePacketData* packetData, OctreeElementBag& bag,
EncodeBitstreamParams& params, int& currentEncodeLevel) const {
EncodeBitstreamParams& params, int& currentEncodeLevel,
const ViewFrustum::location& parentLocationThisView) const {
// How many bytes have we written so far at this level;
int bytesAtThisLevel = 0;
@ -906,6 +896,8 @@ int Octree::encodeTreeBitstreamRecursion(OctreeElement* node,
return bytesAtThisLevel;
}
}
ViewFrustum::location nodeLocationThisView = ViewFrustum::INSIDE; // assume we're inside
// caller can pass NULL as viewFrustum if they want everything
if (params.viewFrustum) {
@ -922,10 +914,17 @@ int Octree::encodeTreeBitstreamRecursion(OctreeElement* node,
return bytesAtThisLevel;
}
// if the parent isn't known to be INSIDE, then it must be INTERSECT, and we should double check to see
// if we are INSIDE, INTERSECT, or OUTSIDE
if (parentLocationThisView != ViewFrustum::INSIDE) {
assert(parentLocationThisView != ViewFrustum::OUTSIDE); // we shouldn't be here if our parent was OUTSIDE!
nodeLocationThisView = node->inFrustum(*params.viewFrustum);
}
// If we're at a node that is out of view, then we can return, because no nodes below us will be in view!
// although technically, we really shouldn't ever be here, because our callers shouldn't be calling us if
// we're out of view
if (!node->isInView(*params.viewFrustum)) {
if (nodeLocationThisView == ViewFrustum::OUTSIDE) {
if (params.stats) {
params.stats->skippedOutOfView(node);
}
@ -1079,7 +1078,11 @@ int Octree::encodeTreeBitstreamRecursion(OctreeElement* node,
OctreeElement* childNode = sortedChildren[i];
int originalIndex = indexOfChildren[i];
bool childIsInView = (childNode && (!params.viewFrustum || childNode->isInView(*params.viewFrustum)));
bool childIsInView = (childNode &&
( !params.viewFrustum || // no view frustum was given, everything is assumed in view
(nodeLocationThisView == ViewFrustum::INSIDE) || // the parent was fully in view, we can assume ALL children are
(nodeLocationThisView == ViewFrustum::INTERSECT && childNode->isInView(*params.viewFrustum)) // the parent intersects and the child is in view
));
if (!childIsInView) {
// must check childNode here, because it could be we got here because there was no childNode
@ -1306,7 +1309,8 @@ int Octree::encodeTreeBitstreamRecursion(OctreeElement* node,
// This only applies in the view frustum case, in other cases, like file save and copy/past where
// no viewFrustum was requested, we still want to recurse the child tree.
if (!params.viewFrustum || !oneAtBit(childrenColoredBits, originalIndex)) {
childTreeBytesOut = encodeTreeBitstreamRecursion(childNode, packetData, bag, params, thisLevel);
childTreeBytesOut = encodeTreeBitstreamRecursion(childNode, packetData, bag, params,
thisLevel, nodeLocationThisView);
}
// remember this for reshuffling
@ -1621,56 +1625,6 @@ void dumpSetContents(const char* name, std::set<unsigned char*> set) {
*/
}
void Octree::startEncoding(OctreeElement* node) {
_encodeSetLock.lock();
_codesBeingEncoded.insert(node->getOctalCode());
_encodeSetLock.unlock();
}
void Octree::doneEncoding(OctreeElement* node) {
_encodeSetLock.lock();
_codesBeingEncoded.erase(node->getOctalCode());
_encodeSetLock.unlock();
// if we have any pending delete codes, then delete them now.
emptyDeleteQueue();
}
void Octree::startDeleting(const unsigned char* code) {
_deleteSetLock.lock();
_codesBeingDeleted.insert(code);
_deleteSetLock.unlock();
}
void Octree::doneDeleting(const unsigned char* code) {
_deleteSetLock.lock();
_codesBeingDeleted.erase(code);
_deleteSetLock.unlock();
}
bool Octree::isEncoding(const unsigned char* codeBuffer) {
_encodeSetLock.lock();
bool isEncoding = (_codesBeingEncoded.find(codeBuffer) != _codesBeingEncoded.end());
_encodeSetLock.unlock();
return isEncoding;
}
void Octree::queueForLaterDelete(const unsigned char* codeBuffer) {
_deletePendingSetLock.lock();
_codesPendingDelete.insert(codeBuffer);
_deletePendingSetLock.unlock();
}
void Octree::emptyDeleteQueue() {
_deletePendingSetLock.lock();
for (std::set<const unsigned char*>::iterator i = _codesPendingDelete.begin(); i != _codesPendingDelete.end(); ++i) {
const unsigned char* codeToDelete = *i;
_codesBeingDeleted.erase(codeToDelete);
deleteOctalCodeFromTree(codeToDelete, COLLAPSE_EMPTY_TREE);
}
_deletePendingSetLock.unlock();
}
void Octree::cancelImport() {
_stopImport = true;
}

View file

@ -287,7 +287,8 @@ protected:
int encodeTreeBitstreamRecursion(OctreeElement* node,
OctreePacketData* packetData, OctreeElementBag& bag,
EncodeBitstreamParams& params, int& currentEncodeLevel) const;
EncodeBitstreamParams& params, int& currentEncodeLevel,
const ViewFrustum::location& parentLocationThisView) const;
static bool countOctreeElementsOperation(OctreeElement* node, void* extraData);
@ -302,40 +303,6 @@ protected:
bool _shouldReaverage;
bool _stopImport;
/// Octal Codes of any subtrees currently being encoded. While any of these codes is being encoded, ancestors and
/// descendants of them can not be deleted.
std::set<const unsigned char*> _codesBeingEncoded;
/// mutex lock to protect the encoding set
QMutex _encodeSetLock;
/// Called to indicate that a OctreeElement is in the process of being encoded.
void startEncoding(OctreeElement* node);
/// Called to indicate that a OctreeElement is done being encoded.
void doneEncoding(OctreeElement* node);
/// Is the Octal Code currently being deleted?
bool isEncoding(const unsigned char* codeBuffer);
/// Octal Codes of any subtrees currently being deleted. While any of these codes is being deleted, ancestors and
/// descendants of them can not be encoded.
std::set<const unsigned char*> _codesBeingDeleted;
/// mutex lock to protect the deleting set
QMutex _deleteSetLock;
/// Called to indicate that an octal code is in the process of being deleted.
void startDeleting(const unsigned char* code);
/// Called to indicate that an octal code is done being deleted.
void doneDeleting(const unsigned char* code);
/// Octal Codes that were attempted to be deleted but couldn't be because they were actively being encoded, and were
/// instead queued for later delete
std::set<const unsigned char*> _codesPendingDelete;
/// mutex lock to protect the deleting set
QMutex _deletePendingSetLock;
/// Adds an Octal Code to the set of codes that needs to be deleted
void queueForLaterDelete(const unsigned char* codeBuffer);
/// flushes out any Octal Codes that had to be queued
void emptyDeleteQueue();
QReadWriteLock _lock;
/// This tree is receiving inbound viewer datagrams.

View file

@ -1183,13 +1183,6 @@ float OctreeElement::getEnclosingRadius() const {
return getScale() * sqrtf(3.0f) / 2.0f;
}
bool OctreeElement::isInView(const ViewFrustum& viewFrustum) const {
AABox box = _box; // use temporary box so we can scale it
box.scale(TREE_SCALE);
bool inView = (ViewFrustum::OUTSIDE != viewFrustum.boxInFrustum(box));
return inView;
}
ViewFrustum::location OctreeElement::inFrustum(const ViewFrustum& viewFrustum) const {
AABox box = _box; // use temporary box so we can scale it
box.scale(TREE_SCALE);
@ -1209,23 +1202,27 @@ bool OctreeElement::calculateShouldRender(const ViewFrustum* viewFrustum, float
bool shouldRender = false;
if (hasContent()) {
float furthestDistance = furthestDistanceToCamera(*viewFrustum);
float boundary = boundaryDistanceForRenderLevel(getLevel() + boundaryLevelAdjust, voxelScaleSize);
float childBoundary = boundaryDistanceForRenderLevel(getLevel() + 1 + boundaryLevelAdjust, voxelScaleSize);
bool inBoundary = (furthestDistance <= boundary);
bool inChildBoundary = (furthestDistance <= childBoundary);
shouldRender = (isLeaf() && inChildBoundary) || (inBoundary && !inChildBoundary);
float childBoundary = boundaryDistanceForRenderLevel(getLevel() + 1 + boundaryLevelAdjust, voxelScaleSize);
bool inChildBoundary = (furthestDistance <= childBoundary);
if (isLeaf() && inChildBoundary) {
shouldRender = true;
} else {
float boundary = childBoundary * 2.0f; // the boundary is always twice the distance of the child boundary
bool inBoundary = (furthestDistance <= boundary);
shouldRender = inBoundary && !inChildBoundary;
}
}
return shouldRender;
}
// Calculates the distance to the furthest point of the voxel to the camera
// does as much math as possible in voxel scale and then scales up to TREE_SCALE at end
float OctreeElement::furthestDistanceToCamera(const ViewFrustum& viewFrustum) const {
AABox box = getAABox();
box.scale(TREE_SCALE);
glm::vec3 furthestPoint = viewFrustum.getFurthestPointFromCamera(box);
glm::vec3 temp = viewFrustum.getPosition() - furthestPoint;
float distanceToVoxelCenter = sqrtf(glm::dot(temp, temp));
return distanceToVoxelCenter;
glm::vec3 furthestPoint;
viewFrustum.getFurthestPointFromCameraVoxelScale(getAABox(), furthestPoint);
glm::vec3 temp = viewFrustum.getPositionVoxelScale() - furthestPoint;
float distanceToFurthestPoint = sqrtf(glm::dot(temp, temp));
return distanceToFurthestPoint * (float)TREE_SCALE;
}
float OctreeElement::distanceToCamera(const ViewFrustum& viewFrustum) const {

View file

@ -112,9 +112,7 @@ public:
int getLevel() const { return numberOfThreeBitSectionsInCode(getOctalCode()) + 1; }
float getEnclosingRadius() const;
bool isInView(const ViewFrustum& viewFrustum) const;
bool isInView(const ViewFrustum& viewFrustum) const { return inFrustum(viewFrustum) != ViewFrustum::OUTSIDE; }
ViewFrustum::location inFrustum(const ViewFrustum& viewFrustum) const;
float distanceToCamera(const ViewFrustum& viewFrustum) const;
float furthestDistanceToCamera(const ViewFrustum& viewFrustum) const;
@ -126,7 +124,7 @@ public:
float distanceSquareToPoint(const glm::vec3& point) const; // when you don't need the actual distance, use this.
float distanceToPoint(const glm::vec3& point) const;
bool isLeaf() const { return getChildCount() == 0; }
bool isLeaf() const { return _childBitmask == 0; }
int getChildCount() const { return numberOfOnes(_childBitmask); }
void printDebugDetails(const char* label) const;
bool isDirty() const { return _isDirty; }

View file

@ -10,9 +10,8 @@
#include <OctalCode.h>
OctreeElementBag::OctreeElementBag() :
_bagElements(NULL),
_elementsInUse(0),
_sizeOfElementsArray(0) {
_bagElements()
{
OctreeElement::addDeleteHook(this);
};
@ -21,114 +20,35 @@ OctreeElementBag::~OctreeElementBag() {
deleteAll();
}
void OctreeElementBag::deleteAll() {
if (_bagElements) {
delete[] _bagElements;
}
_bagElements = NULL;
_elementsInUse = 0;
_sizeOfElementsArray = 0;
}
const int GROW_BAG_BY = 100;
// put a node into the bag
void OctreeElementBag::insert(OctreeElement* element) {
// Search for where we should live in the bag (sorted)
// Note: change this to binary search... instead of linear!
int insertAt = _elementsInUse;
for (int i = 0; i < _elementsInUse; i++) {
// just compare the pointers... that's good enough
if (_bagElements[i] == element) {
return; // exit early!!
}
if (_bagElements[i] > element) {
insertAt = i;
break;
}
}
// at this point, inserAt will be the location we want to insert at.
// If we don't have room in our bag, then grow the bag
if (_sizeOfElementsArray < _elementsInUse + 1) {
OctreeElement** oldBag = _bagElements;
_bagElements = new OctreeElement*[_sizeOfElementsArray + GROW_BAG_BY];
_sizeOfElementsArray += GROW_BAG_BY;
// If we had an old bag...
if (oldBag) {
// copy old elements into the new bag, but leave a space where we need to
// insert the new node
memcpy(_bagElements, oldBag, insertAt * sizeof(OctreeElement*));
memcpy(&_bagElements[insertAt + 1], &oldBag[insertAt], (_elementsInUse - insertAt) * sizeof(OctreeElement*));
delete[] oldBag;
}
} else {
// move existing elements further back in the bag array, leave a space where we need to
// insert the new node
memmove(&_bagElements[insertAt + 1], &_bagElements[insertAt], (_elementsInUse - insertAt) * sizeof(OctreeElement*));
}
_bagElements[insertAt] = element;
_elementsInUse++;
}
// pull a node out of the bag (could come in any order)
OctreeElement* OctreeElementBag::extract() {
// pull the last node out, and shrink our list...
if (_elementsInUse) {
// get the last element
OctreeElement* element = _bagElements[_elementsInUse - 1];
// reduce the count
_elementsInUse--;
return element;
}
return NULL;
}
bool OctreeElementBag::contains(OctreeElement* element) {
for (int i = 0; i < _elementsInUse; i++) {
// just compare the pointers... that's good enough
if (_bagElements[i] == element) {
return true; // exit early!!
}
// if we're past where it should be, then it's not here!
if (_bagElements[i] > element) {
return false;
}
}
// if we made it through the entire bag, it's not here!
return false;
}
void OctreeElementBag::remove(OctreeElement* element) {
int foundAt = -1;
for (int i = 0; i < _elementsInUse; i++) {
// just compare the pointers... that's good enough
if (_bagElements[i] == element) {
foundAt = i;
break;
}
// if we're past where it should be, then it's not here!
if (_bagElements[i] > element) {
break;
}
}
// if we found it, then we need to remove it....
if (foundAt != -1) {
memmove(&_bagElements[foundAt], &_bagElements[foundAt + 1], (_elementsInUse - foundAt) * sizeof(OctreeElement*));
_elementsInUse--;
}
}
void OctreeElementBag::elementDeleted(OctreeElement* element) {
remove(element); // note: remove can safely handle nodes that aren't in it, so we don't need to check contains()
}
void OctreeElementBag::deleteAll() {
_bagElements.clear();
}
void OctreeElementBag::insert(OctreeElement* element) {
_bagElements.insert(element);
}
OctreeElement* OctreeElementBag::extract() {
OctreeElement* result = NULL;
if (_bagElements.size() > 0) {
QSet<OctreeElement*>::iterator front = _bagElements.begin();
result = *front;
_bagElements.erase(front);
}
return result;
}
bool OctreeElementBag::contains(OctreeElement* element) {
return _bagElements.contains(element);
}
void OctreeElementBag::remove(OctreeElement* element) {
_bagElements.remove(element);
}

View file

@ -27,18 +27,14 @@ public:
bool contains(OctreeElement* element); // is this element in the bag?
void remove(OctreeElement* element); // remove a specific element from the bag
bool isEmpty() const { return (_elementsInUse == 0); }
int count() const { return _elementsInUse; }
bool isEmpty() const { return _bagElements.isEmpty(); }
int count() const { return _bagElements.size(); }
void deleteAll();
virtual void elementDeleted(OctreeElement* element);
private:
OctreeElement** _bagElements;
int _elementsInUse;
int _sizeOfElementsArray;
//int _hookID;
QSet<OctreeElement*> _bagElements;
};
#endif /* defined(__hifi__OctreeElementBag__) */

View file

@ -25,6 +25,7 @@ using namespace std;
ViewFrustum::ViewFrustum() :
_position(0,0,0),
_positionVoxelScale(0,0,0),
_orientation(),
_direction(IDENTITY_FRONT),
_up(IDENTITY_UP),
@ -277,6 +278,10 @@ ViewFrustum::location ViewFrustum::boxInFrustum(const AABox& box) const {
return keyholeResult;
}
// TODO: These calculations are expensive, taking up 80% of our time in this function.
// This appears to be expensive because we have to test the distance to each plane.
// One suggested optimization is to first check against the approximated cone. We might
// also be able to test against the cone to the bounding sphere of the box.
for(int i=0; i < 6; i++) {
const glm::vec3& normal = _planes[i].getNormal();
const glm::vec3& boxVertexP = box.getVertexP(normal);
@ -691,39 +696,60 @@ OctreeProjectedPolygon ViewFrustum::getProjectedPolygon(const AABox& box) const
return projectedPolygon;
}
// Similar strategy to getProjectedPolygon() we use the knowledge of camera position relative to the
// axis-aligned voxels to determine which of the voxels vertices must be the furthest. No need for
// squares and square-roots. Just compares.
glm::vec3 ViewFrustum::getFurthestPointFromCamera(const AABox& box) const {
void ViewFrustum::getFurthestPointFromCamera(const AABox& box, glm::vec3& furthestPoint) const {
const glm::vec3& bottomNearRight = box.getCorner();
glm::vec3 center = box.calcCenter();
glm::vec3 topFarLeft = box.calcTopFarLeft();
float scale = box.getScale();
float halfScale = scale * 0.5f;
glm::vec3 furthestPoint;
if (_position.x < center.x) {
if (_position.x < bottomNearRight.x + halfScale) {
// we are to the right of the center, so the left edge is furthest
furthestPoint.x = topFarLeft.x;
furthestPoint.x = bottomNearRight.x + scale;
} else {
// we are to the left of the center, so the right edge is furthest (at center ok too)
furthestPoint.x = bottomNearRight.x;
}
if (_position.y < center.y) {
if (_position.y < bottomNearRight.y + halfScale) {
// we are below of the center, so the top edge is furthest
furthestPoint.y = topFarLeft.y;
furthestPoint.y = bottomNearRight.y + scale;
} else {
// we are above the center, so the lower edge is furthest (at center ok too)
furthestPoint.y = bottomNearRight.y;
}
if (_position.z < center.z) {
if (_position.z < bottomNearRight.z + halfScale) {
// we are to the near side of the center, so the far side edge is furthest
furthestPoint.z = topFarLeft.z;
furthestPoint.z = bottomNearRight.z + scale;
} else {
// we are to the far side of the center, so the near side edge is furthest (at center ok too)
furthestPoint.z = bottomNearRight.z;
}
return furthestPoint;
}
void ViewFrustum::getFurthestPointFromCameraVoxelScale(const AABox& box, glm::vec3& furthestPoint) const {
const glm::vec3& bottomNearRight = box.getCorner();
float scale = box.getScale();
float halfScale = scale * 0.5f;
if (_positionVoxelScale.x < bottomNearRight.x + halfScale) {
// we are to the right of the center, so the left edge is furthest
furthestPoint.x = bottomNearRight.x + scale;
} else {
furthestPoint.x = bottomNearRight.x;
}
if (_positionVoxelScale.y < bottomNearRight.y + halfScale) {
// we are below of the center, so the top edge is furthest
furthestPoint.y = bottomNearRight.y + scale;
} else {
furthestPoint.y = bottomNearRight.y;
}
if (_positionVoxelScale.z < bottomNearRight.z + halfScale) {
// we are to the near side of the center, so the far side edge is furthest
furthestPoint.z = bottomNearRight.z + scale;
} else {
furthestPoint.z = bottomNearRight.z;
}
}

View file

@ -29,11 +29,12 @@ const float DEFAULT_FAR_CLIP = 50.0f * TREE_SCALE;
class ViewFrustum {
public:
// setters for camera attributes
void setPosition(const glm::vec3& p) { _position = p; }
void setPosition(const glm::vec3& p) { _position = p; _positionVoxelScale = (p / (float)TREE_SCALE); }
void setOrientation(const glm::quat& orientationAsQuaternion);
// getters for camera attributes
const glm::vec3& getPosition() const { return _position; }
const glm::vec3& getPositionVoxelScale() const { return _positionVoxelScale; }
const glm::quat& getOrientation() const { return _orientation; }
const glm::vec3& getDirection() const { return _direction; }
const glm::vec3& getUp() const { return _up; }
@ -102,8 +103,11 @@ public:
glm::vec2 projectPoint(glm::vec3 point, bool& pointInView) const;
OctreeProjectedPolygon getProjectedPolygon(const AABox& box) const;
glm::vec3 getFurthestPointFromCamera(const AABox& box) const;
void getFurthestPointFromCamera(const AABox& box, glm::vec3& furthestPoint) const;
// assumes box is in voxel scale, not TREE_SCALE, will scale view frustum's position accordingly
void getFurthestPointFromCameraVoxelScale(const AABox& box, glm::vec3& furthestPoint) const;
private:
// Used for keyhole calculations
ViewFrustum::location pointInKeyhole(const glm::vec3& point) const;
@ -111,7 +115,8 @@ private:
ViewFrustum::location boxInKeyhole(const AABox& box) const;
// camera location/orientation attributes
glm::vec3 _position;
glm::vec3 _position; // the position in TREE_SCALE
glm::vec3 _positionVoxelScale; // the position in voxel scale
glm::quat _orientation;
// calculated for orientation

View file

@ -14,6 +14,7 @@
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <AudioRingBuffer.h>
#include <AvatarData.h>
#include <NodeList.h>
#include <PacketHeaders.h>
@ -52,7 +53,9 @@ ScriptEngine::ScriptEngine(const QString& scriptContents, bool wantMenuItems, co
_avatarIdentityTimer(NULL),
_avatarBillboardTimer(NULL),
_timerFunctionMap(),
_avatarAudioBuffer(NULL),
_isListeningToAudioStream(false),
_avatarSound(NULL),
_numAvatarSoundSentBytes(0),
_controllerScriptingInterface(controllerScriptingInterface),
_avatarData(NULL),
_wantMenuItems(wantMenuItems),
@ -260,29 +263,82 @@ void ScriptEngine::run() {
}
if (_isAvatar && _avatarData) {
const int SCRIPT_AUDIO_BUFFER_SAMPLES = floor(((SCRIPT_DATA_CALLBACK_USECS * SAMPLE_RATE) / (1000 * 1000)) + 0.5);
const int SCRIPT_AUDIO_BUFFER_BYTES = SCRIPT_AUDIO_BUFFER_SAMPLES * sizeof(int16_t);
QByteArray avatarPacket = byteArrayWithPopulatedHeader(PacketTypeAvatarData);
avatarPacket.append(_avatarData->toByteArray());
nodeList->broadcastToNodes(avatarPacket, NodeSet() << NodeType::AvatarMixer);
if (_avatarAudioBuffer && _numAvatarAudioBufferSamples > 0) {
// if have an avatar audio stream then send it out to our audio-mixer
QByteArray audioPacket = byteArrayWithPopulatedHeader(PacketTypeMicrophoneAudioNoEcho);
if (_isListeningToAudioStream || _avatarSound) {
// if we have an avatar audio stream then send it out to our audio-mixer
bool silentFrame = true;
int16_t numAvailableSamples = SCRIPT_AUDIO_BUFFER_SAMPLES;
const int16_t* nextSoundOutput = NULL;
if (_avatarSound) {
const QByteArray& soundByteArray = _avatarSound->getByteArray();
nextSoundOutput = reinterpret_cast<const int16_t*>(soundByteArray.data()
+ _numAvatarSoundSentBytes);
int numAvailableBytes = (soundByteArray.size() - _numAvatarSoundSentBytes) > SCRIPT_AUDIO_BUFFER_BYTES
? SCRIPT_AUDIO_BUFFER_BYTES
: soundByteArray.size() - _numAvatarSoundSentBytes;
numAvailableSamples = numAvailableBytes / sizeof(int16_t);
// check if the all of the _numAvatarAudioBufferSamples to be sent are silence
for (int i = 0; i < numAvailableSamples; ++i) {
if (nextSoundOutput[i] != 0) {
silentFrame = false;
break;
}
}
_numAvatarSoundSentBytes += numAvailableBytes;
if (_numAvatarSoundSentBytes == soundByteArray.size()) {
// we're done with this sound object - so set our pointer back to NULL
// and our sent bytes back to zero
_avatarSound = NULL;
_numAvatarSoundSentBytes = 0;
}
}
QByteArray audioPacket = byteArrayWithPopulatedHeader(silentFrame
? PacketTypeSilentAudioFrame
: PacketTypeMicrophoneAudioNoEcho);
QDataStream packetStream(&audioPacket, QIODevice::Append);
// use the orientation and position of this avatar for the source of this audio
packetStream.writeRawData(reinterpret_cast<const char*>(&_avatarData->getPosition()), sizeof(glm::vec3));
glm::quat headOrientation = _avatarData->getHeadOrientation();
packetStream.writeRawData(reinterpret_cast<const char*>(&headOrientation), sizeof(glm::quat));
packetStream.writeRawData(reinterpret_cast<const char*>(_avatarAudioBuffer),
_numAvatarAudioBufferSamples * sizeof(int16_t));
if (silentFrame) {
if (!_isListeningToAudioStream) {
// if we have a silent frame and we're not listening then just send nothing and break out of here
break;
}
// write the number of silent samples so the audio-mixer can uphold timing
packetStream.writeRawData(reinterpret_cast<const char*>(&SCRIPT_AUDIO_BUFFER_SAMPLES), sizeof(int16_t));
} else if (nextSoundOutput) {
// write the raw audio data
packetStream.writeRawData(reinterpret_cast<const char*>(nextSoundOutput),
numAvailableSamples * sizeof(int16_t));
}
nodeList->broadcastToNodes(audioPacket, NodeSet() << NodeType::AudioMixer);
}
}
qint64 now = usecTimestampNow();
float deltaTime = (float)(now - lastUpdate)/(float)USECS_PER_SECOND;
float deltaTime = (float) (now - lastUpdate) / (float) USECS_PER_SECOND;
emit update(deltaTime);
lastUpdate = now;

View file

@ -56,10 +56,11 @@ public:
void setAvatarData(AvatarData* avatarData, const QString& objectName);
void setAvatarAudioBuffer(int16_t* avatarAudioBuffer) { _avatarAudioBuffer = avatarAudioBuffer; }
bool sendsAvatarAudioStream() const { return (bool) _avatarAudioBuffer; }
void setNumAvatarAudioBufferSamples(int numAvatarAudioBufferSamples)
{ _numAvatarAudioBufferSamples = numAvatarAudioBufferSamples; }
bool isListeningToAudioStream() const { return _isListeningToAudioStream; }
void setIsListeningToAudioStream(bool isListeningToAudioStream) { _isListeningToAudioStream = isListeningToAudioStream; }
void setAvatarSound(Sound* avatarSound) { _avatarSound = avatarSound; }
bool isPlayingAvatarSound() const { return _avatarSound != NULL; }
void init();
void run(); /// runs continuously until Agent.stop() is called
@ -91,8 +92,9 @@ protected:
QTimer* _avatarIdentityTimer;
QTimer* _avatarBillboardTimer;
QHash<QTimer*, QScriptValue> _timerFunctionMap;
int16_t* _avatarAudioBuffer;
int _numAvatarAudioBufferSamples;
bool _isListeningToAudioStream;
Sound* _avatarSound;
int _numAvatarSoundSentBytes;
private:
void sendAvatarIdentityPacket();

View file

@ -0,0 +1,65 @@
//
// FileDownloader.cpp
// hifi
//
// Created by Clement Brisset on 3/14/14.
// Copyright (c) 2014 HighFidelity, Inc. All rights reserved.
//
//
#include <QUrl>
#include <QNetworkRequest>
#include <QEventLoop>
#include <QTimer>
#include "FileDownloader.h"
FileDownloader::FileDownloader(const QUrl dataURL, QObject* parent) :
QObject(parent),
_done(false)
{
connect(&_networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(processReply(QNetworkReply*)));
QNetworkRequest request(dataURL);
_networkAccessManager.get(request);
}
void FileDownloader::processReply(QNetworkReply *reply) {
if (reply->error() == QNetworkReply::NoError) {
_downloadedData = reply->readAll();
}
reply->deleteLater();
_done = true;
emit done(reply->error());
}
void FileDownloader::waitForFile(int timeout) {
QTimer timer;
QEventLoop loop;
connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
connect(this, SIGNAL(done(QNetworkReply::NetworkError)), &loop, SLOT(quit()));
if (!_done) {
if (timeout > 0) {
timer.start(timeout);
}
loop.exec();
}
}
QByteArray FileDownloader::download(const QUrl dataURL, int timeout) {
QTimer timer;
QEventLoop loop;
connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit));
FileDownloader downloader(dataURL);
connect(&downloader, SIGNAL(done(QNetworkReply::NetworkError)), &loop, SLOT(quit()));
if (timeout > 0) {
timer.start(timeout);
}
loop.exec();
return downloader.getData();
}

View file

@ -0,0 +1,44 @@
//
// FileDownloader.h
// hifi
//
// Created by Clement Brisset on 3/14/14.
// Copyright (c) 2014 HighFidelity, Inc. All rights reserved.
//
//
#ifndef __hifi__FileDownloader__
#define __hifi__FileDownloader__
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
class FileDownloader : public QObject {
Q_OBJECT
public:
FileDownloader(const QUrl dataURL, QObject* parent = NULL);
void waitForFile(int timeout = 0);
QByteArray getData() const { return _downloadedData; }
bool done() { return _done; }
static QByteArray download(const QUrl dataURL, int timeout = 0);
signals:
void done(QNetworkReply::NetworkError);
private slots:
void processReply(QNetworkReply* reply);
private:
QNetworkAccessManager _networkAccessManager;
QByteArray _downloadedData;
bool _done;
};
#endif /* defined(__hifi__FileDownloader__) */

View file

@ -88,9 +88,18 @@ bool NodeList::packetVersionAndHashMatch(const QByteArray& packet) {
PacketType mismatchType = packetTypeForPacket(packet);
int numPacketTypeBytes = numBytesArithmeticCodingFromBuffer(packet.data());
qDebug() << "Packet version mismatch on" << packetTypeForPacket(packet) << "- Sender"
static QMultiMap<QUuid, PacketType> versionDebugSuppressMap;
QUuid senderUUID = uuidFromPacketHeader(packet);
if (!versionDebugSuppressMap.contains(senderUUID, checkType)) {
qDebug() << "Packet version mismatch on" << packetTypeForPacket(packet) << "- Sender"
<< uuidFromPacketHeader(packet) << "sent" << qPrintable(QString::number(packet[numPacketTypeBytes])) << "but"
<< qPrintable(QString::number(versionForPacketType(mismatchType))) << "expected.";
versionDebugSuppressMap.insert(senderUUID, checkType);
}
return false;
}
const QSet<PacketType> NON_VERIFIED_PACKETS = QSet<PacketType>()

View file

@ -45,7 +45,7 @@ int packArithmeticallyCodedValue(int value, char* destination) {
PacketVersion versionForPacketType(PacketType type) {
switch (type) {
case PacketTypeAvatarData:
return 1;
return 3;
case PacketTypeParticleData:
return 1;
case PacketTypeDomainList:

View file

@ -32,7 +32,7 @@ enum PacketType {
PacketTypeMicrophoneAudioNoEcho,
PacketTypeMicrophoneAudioWithEcho,
PacketTypeBulkAvatarData,
PacketTypeTransmitterData, // usable
PacketTypeSilentAudioFrame,
PacketTypeEnvironmentData,
PacketTypeDomainListRequest,
PacketTypeRequestAssignment,

View file

@ -18,6 +18,7 @@
/// Generalized threaded processor for handling received inbound packets.
class ReceivedPacketProcessor : public GenericThread {
Q_OBJECT
public:
ReceivedPacketProcessor() { }

View file

@ -130,14 +130,22 @@ void outputBits(unsigned char byte, QDebug* continuedDebug) {
}
int numberOfOnes(unsigned char byte) {
return (byte >> 7)
+ ((byte >> 6) & 1)
+ ((byte >> 5) & 1)
+ ((byte >> 4) & 1)
+ ((byte >> 3) & 1)
+ ((byte >> 2) & 1)
+ ((byte >> 1) & 1)
+ (byte & 1);
static const int nbits[256] = {
0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,
4,3,4,4,5,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,
4,5,3,4,4,5,4,5,5,6,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,
3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,
4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,1,2,2,3,2,3,3,
4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,
3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,
6,6,7,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,
4,5,5,6,5,6,6,7,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,
6,5,6,6,7,5,6,6,7,6,7,7,8
};
return nbits[(unsigned char) byte];
}
bool oneAtBit(unsigned char byte, int bitIndex) {

View file

@ -42,13 +42,15 @@ int SimpleMovingAverage::updateAverage(float sample) {
void SimpleMovingAverage::reset() {
_numSamples = 0;
_average = 0;
_eventDeltaAverage = 0;
}
float SimpleMovingAverage::getEventDeltaAverage() {
float SimpleMovingAverage::getEventDeltaAverage() const {
return (ONE_MINUS_WEIGHTING * _eventDeltaAverage) +
(WEIGHTING * ((usecTimestampNow() - _lastEventTimestamp) / 1000000.0f));
}
float SimpleMovingAverage::getAverageSampleValuePerSecond() {
float SimpleMovingAverage::getAverageSampleValuePerSecond() const {
return _average * (1 / getEventDeltaAverage());
}

View file

@ -20,10 +20,10 @@ public:
int updateAverage(float sample);
void reset();
int getSampleCount() { return _numSamples; };
float getAverage() { return _average; };
float getEventDeltaAverage();
float getAverageSampleValuePerSecond();
int getSampleCount() const { return _numSamples; };
float getAverage() const { return _average; };
float getEventDeltaAverage() const;
float getAverageSampleValuePerSecond() const;
private:
int _numSamples;
uint64_t _lastEventTimestamp;

View file

@ -29,6 +29,7 @@ void ThreadedAssignment::setFinished(bool isFinished) {
_isFinished = isFinished;
if (_isFinished) {
aboutToFinish();
emit finished();
}
}

View file

@ -15,18 +15,17 @@ class ThreadedAssignment : public Assignment {
Q_OBJECT
public:
ThreadedAssignment(const QByteArray& packet);
void setFinished(bool isFinished);
virtual void aboutToFinish() { };
public slots:
/// threaded run of assignment
virtual void run() = 0;
virtual void deleteLater();
virtual void readPendingDatagrams() = 0;
protected:
bool readAvailableDatagram(QByteArray& destinationByteArray, HifiSockAddr& senderSockAddr);
void commonInit(const QString& targetName, NodeType_t nodeType);
bool _isFinished;
private slots: