Merge branch 'master' of https://github.com/worklist/hifi into 19418

This commit is contained in:
stojce 2013-09-10 21:05:44 +02:00
commit 114cc065bd
33 changed files with 920 additions and 297 deletions

View file

@ -7,82 +7,202 @@
//
#include <arpa/inet.h>
#include <errno.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <Assignment.h>
#include <AudioMixer.h>
#include <AvatarMixer.h>
#include <Logging.h>
#include <NodeList.h>
#include <PacketHeaders.h>
#include <SharedUtil.h>
const int ASSIGNMENT_REQUEST_INTERVAL_USECS = 1 * 1000 * 1000;
const long long ASSIGNMENT_REQUEST_INTERVAL_USECS = 1 * 1000 * 1000;
const char PARENT_TARGET_NAME[] = "assignment-client-monitor";
const char CHILD_TARGET_NAME[] = "assignment-client";
int main(int argc, char* const argv[]) {
pid_t* childForks = NULL;
sockaddr_in customAssignmentSocket = {};
const char* assignmentPool = NULL;
int numForks = 0;
void childClient() {
// this is one of the child forks or there is a single assignment client, continue assignment-client execution
setvbuf(stdout, NULL, _IOLBF, 0);
// set the logging target to the the CHILD_TARGET_NAME
Logging::setTargetName(CHILD_TARGET_NAME);
// create a NodeList as an unassigned client
NodeList* nodeList = NodeList::createInstance(NODE_TYPE_UNASSIGNED);
// set the custom assignment socket if we have it
if (customAssignmentSocket.sin_addr.s_addr != 0) {
nodeList->setAssignmentServerSocket((sockaddr*) &customAssignmentSocket);
}
// change the timeout on the nodelist socket to be as often as we want to re-request
nodeList->getNodeSocket()->setBlockingReceiveTimeoutInUsecs(ASSIGNMENT_REQUEST_INTERVAL_USECS);
timeval lastRequest = {};
unsigned char packetData[MAX_PACKET_SIZE];
ssize_t receivedBytes = 0;
// loop the parameters to see if we were passed a pool
int parameter = -1;
const char ALLOWED_PARAMETERS[] = "p::";
const char POOL_PARAMETER_CHAR = 'p';
char* assignmentPool = NULL;
while ((parameter = getopt(argc, argv, ALLOWED_PARAMETERS)) != -1) {
if (parameter == POOL_PARAMETER_CHAR) {
// copy the passed assignment pool
int poolLength = strlen(optarg);
assignmentPool = new char[poolLength + sizeof(char)];
strcpy(assignmentPool, optarg);
}
}
// create a request assignment, accept all assignments, pass the desired pool (if it exists)
Assignment requestAssignment(Assignment::Request, Assignment::All, assignmentPool);
while (true) {
// if we're here we have no assignment, so send a request
qDebug() << "Sending an assignment request -" << requestAssignment;
nodeList->sendAssignment(requestAssignment);
if (usecTimestampNow() - usecTimestamp(&lastRequest) >= ASSIGNMENT_REQUEST_INTERVAL_USECS) {
gettimeofday(&lastRequest, NULL);
// if we're here we have no assignment, so send a request
nodeList->sendAssignment(requestAssignment);
}
while (nodeList->getNodeSocket()->receive(packetData, &receivedBytes)) {
if (packetData[0] == PACKET_TYPE_DEPLOY_ASSIGNMENT && packetVersionMatch(packetData)) {
if (nodeList->getNodeSocket()->receive(packetData, &receivedBytes) &&
packetData[0] == PACKET_TYPE_DEPLOY_ASSIGNMENT && packetVersionMatch(packetData)) {
// construct the deployed assignment from the packet data
Assignment deployedAssignment(packetData, receivedBytes);
qDebug() << "Received an assignment -" << deployedAssignment << "\n";
// switch our nodelist DOMAIN_IP to the ip receieved in the assignment
if (deployedAssignment.getAttachedPublicSocket()->sa_family == AF_INET) {
in_addr domainSocketAddr = ((sockaddr_in*) deployedAssignment.getAttachedPublicSocket())->sin_addr;
nodeList->setDomainIP(inet_ntoa(domainSocketAddr));
// construct the deployed assignment from the packet data
Assignment deployedAssignment(packetData, receivedBytes);
qDebug() << "Received an assignment - " << deployedAssignment << "\n";
// switch our nodelist DOMAIN_IP to the ip receieved in the assignment
if (deployedAssignment.getDomainSocket()->sa_family == AF_INET) {
in_addr domainSocketAddr = ((sockaddr_in*) deployedAssignment.getDomainSocket())->sin_addr;
nodeList->setDomainIP(inet_ntoa(domainSocketAddr));
qDebug() << "Changed domain IP to " << inet_ntoa(domainSocketAddr);
}
qDebug("Destination IP for assignment is %s\n", inet_ntoa(domainSocketAddr));
if (deployedAssignment.getType() == Assignment::AudioMixer) {
AudioMixer::run();
} else {
AvatarMixer::run();
}
} else {
qDebug("Received a bad destination socket for assignment.\n");
}
qDebug("Assignment finished or never started - waiting for new assignment\n");
// reset our NodeList by switching back to unassigned and clearing the list
nodeList->setOwnerType(NODE_TYPE_UNASSIGNED);
nodeList->clear();
// reset the logging target to the the CHILD_TARGET_NAME
Logging::setTargetName(CHILD_TARGET_NAME);
}
}
}
void sigchldHandler(int sig) {
pid_t processID;
int status;
while ((processID = waitpid(-1, &status, WNOHANG)) != -1) {
if (processID == 0) {
// there are no more children to process, break out of here
break;
}
int newForkProcessID = 0;
// find the dead process in the array of child forks
for (int i = 0; i < ::numForks; i++) {
if (::childForks[i] == processID) {
qDebug() << "Assignment finished or never started - waiting for new assignment";
// reset our NodeList by switching back to unassigned and clearing the list
nodeList->setOwnerType(NODE_TYPE_UNASSIGNED);
nodeList->clear();
newForkProcessID = fork();
if (newForkProcessID == 0) {
// this is the child, call childClient
childClient();
// break out so we don't fork bomb
break;
} else {
// this is the parent, replace the dead process with the new one
::childForks[i] = newForkProcessID;
qDebug("Replaced dead %d with new fork %d\n", processID, newForkProcessID);
break;
}
}
}
}
}
void parentMonitor() {
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = sigchldHandler;
sigaction(SIGCHLD, &sa, NULL);
pid_t childID = 0;
// don't bail until all children have finished
while ((childID = waitpid(-1, NULL, 0))) {
if (errno == ECHILD) {
break;
}
}
// delete the array of pid_t holding the forked process IDs
delete[] ::childForks;
}
int main(int argc, const char* argv[]) {
setvbuf(stdout, NULL, _IOLBF, 0);
// use the verbose message handler in Logging
qInstallMessageHandler(Logging::verboseMessageHandler);
// start the Logging class with the parent's target name
Logging::setTargetName(PARENT_TARGET_NAME);
// grab the overriden assignment-server hostname from argv, if it exists
const char* customAssignmentServer = getCmdOption(argc, argv, "-a");
if (customAssignmentServer) {
::customAssignmentSocket = socketForHostnameAndHostOrderPort(customAssignmentServer, ASSIGNMENT_SERVER_PORT);
}
const char* NUM_FORKS_PARAMETER = "-n";
const char* numForksString = getCmdOption(argc, argv, NUM_FORKS_PARAMETER);
// grab the assignment pool from argv, if it was passed
const char* ASSIGNMENT_POOL_PARAMETER = "-p";
::assignmentPool = getCmdOption(argc, argv, ASSIGNMENT_POOL_PARAMETER);
int processID = 0;
if (numForksString) {
::numForks = atoi(numForksString);
qDebug("Starting %d assignment clients\n", ::numForks);
::childForks = new pid_t[::numForks];
// fire off as many children as we need (this is one less than the parent since the parent will run as well)
for (int i = 0; i < ::numForks; i++) {
processID = fork();
if (processID == 0) {
// this is in one of the children, break so we don't start a fork bomb
break;
} else {
// this is in the parent, save the ID of the forked process
childForks[i] = processID;
}
}
}
if (processID == 0 || ::numForks == 0) {
childClient();
} else {
parentMonitor();
}
}

View file

@ -68,6 +68,15 @@ int main(int argc, const char* argv[]) {
&& strcmp((*assignment)->getPool(), requestAssignment.getPool()) == 0)
|| !eitherHasPool) {
// check if the requestor is on the same network as the destination for the assignment
if (senderSocket.sin_addr.s_addr ==
((sockaddr_in*) (*assignment)->getAttachedPublicSocket())->sin_addr.s_addr) {
// if this is the case we remove the public socket on the assignment by setting it to NULL
// this ensures the local IP and port sent to the requestor is the local address of destination
(*assignment)->setAttachedPublicSocket(NULL);
}
int numAssignmentBytes = (*assignment)->packToBuffer(assignmentPacket + numSendHeaderBytes);
// send the assignment
@ -96,10 +105,10 @@ int main(int argc, const char* argv[]) {
qDebug() << "Received a created assignment:" << *createdAssignment;
qDebug() << "Current queue size is" << assignmentQueue.size();
// assignment server is on a public server
// assignment server is likely on a public server
// assume that the address we now have for the sender is the public address/port
// and store that with the assignment so it can be given to the requestor later
createdAssignment->setDomainSocket((sockaddr*) &senderSocket);
// and store that with the assignment so it can be given to the requestor later if necessary
createdAssignment->setAttachedPublicSocket((sockaddr*) &senderSocket);
// add this assignment to the queue
assignmentQueue.push_back(createdAssignment);

View file

@ -28,7 +28,7 @@
#include "Assignment.h"
#include "NodeList.h"
#include "NodeTypes.h"
#include "Logstash.h"
#include "Logging.h"
#include "PacketHeaders.h"
#include "SharedUtil.h"
@ -48,7 +48,7 @@ unsigned char* addNodeToBroadcastPacket(unsigned char* currentPosition, Node* no
return currentPosition;
}
int main(int argc, char* const argv[]) {
int main(int argc, const char* argv[]) {
NodeList* nodeList = NodeList::createInstance(NODE_TYPE_DOMAIN, DOMAIN_LISTEN_PORT);
// If user asks to run in "local" mode then we do NOT replace the IP
// with the EC2 IP. Otherwise, we will replace the IP like we used to
@ -85,56 +85,64 @@ int main(int argc, char* const argv[]) {
timeval lastStatSendTime = {};
// loop the parameters to see if we were passed a pool for assignment
int parameter = -1;
const char ALLOWED_PARAMETERS[] = "p::-local::";
const char POOL_PARAMETER_CHAR = 'p';
const char ASSIGNMENT_POOL_OPTION[] = "-p";
const char ASSIGNMENT_SERVER_OPTION[] = "-a";
char* assignmentPool = NULL;
// set our assignment pool from argv, if it exists
const char* assignmentPool = getCmdOption(argc, argv, ASSIGNMENT_POOL_OPTION);
while ((parameter = getopt(argc, argv, ALLOWED_PARAMETERS)) != -1) {
if (parameter == POOL_PARAMETER_CHAR) {
// copy the passed assignment pool
int poolLength = strlen(optarg);
assignmentPool = new char[poolLength + sizeof(char)];
strcpy(assignmentPool, optarg);
}
// grab the overriden assignment-server hostname from argv, if it exists
const char* customAssignmentServer = getCmdOption(argc, argv, ASSIGNMENT_SERVER_OPTION);
if (customAssignmentServer) {
sockaddr_in customAssignmentSocket = socketForHostnameAndHostOrderPort(customAssignmentServer, ASSIGNMENT_SERVER_PORT);
nodeList->setAssignmentServerSocket((sockaddr*) &customAssignmentSocket);
}
// use a map to keep track of iterations of silence for assignment creation requests
const int ASSIGNMENT_SILENCE_MAX_ITERATIONS = 5;
std::map<Assignment*, int> assignmentSilenceCount;
const long long ASSIGNMENT_SILENCE_MAX_USECS = 5 * 1000 * 1000;
// as a domain-server we will always want an audio mixer and avatar mixer
// setup the create assignments for those
Assignment audioAssignment(Assignment::Create, Assignment::AudioMixer, assignmentPool);
Assignment avatarAssignment(Assignment::Create, Assignment::AvatarMixer, assignmentPool);
// setup the create assignment pointers for those
Assignment* audioAssignment = NULL;
Assignment* avatarAssignment = NULL;
// construct a local socket to send with our created assignments
sockaddr_in localSocket = {};
localSocket.sin_family = AF_INET;
localSocket.sin_port = htons(nodeList->getInstance()->getNodeSocket()->getListeningPort());
localSocket.sin_addr.s_addr = serverLocalAddress;
while (true) {
if (!nodeList->soloNodeOfType(NODE_TYPE_AUDIO_MIXER)) {
if (assignmentSilenceCount[&audioAssignment] == ASSIGNMENT_SILENCE_MAX_ITERATIONS) {
nodeList->sendAssignment(audioAssignment);
assignmentSilenceCount[&audioAssignment] = 0;
} else {
assignmentSilenceCount[&audioAssignment]++;
if (!audioAssignment
|| usecTimestampNow() - usecTimestamp(&audioAssignment->getTime()) >= ASSIGNMENT_SILENCE_MAX_USECS) {
if (!audioAssignment) {
audioAssignment = new Assignment(Assignment::Create, Assignment::AudioMixer, assignmentPool);
audioAssignment->setAttachedLocalSocket((sockaddr*) &localSocket);
}
nodeList->sendAssignment(*audioAssignment);
audioAssignment->setCreateTimeToNow();
}
} else {
assignmentSilenceCount[&audioAssignment] = 0;
}
if (!nodeList->soloNodeOfType(NODE_TYPE_AVATAR_MIXER)) {
if (assignmentSilenceCount[&avatarAssignment] == ASSIGNMENT_SILENCE_MAX_ITERATIONS) {
nodeList->sendAssignment(avatarAssignment);
assignmentSilenceCount[&avatarAssignment] = 0;
} else {
assignmentSilenceCount[&avatarAssignment]++;
if (!avatarAssignment
|| usecTimestampNow() - usecTimestamp(&avatarAssignment->getTime()) >= ASSIGNMENT_SILENCE_MAX_USECS) {
if (!avatarAssignment) {
avatarAssignment = new Assignment(Assignment::Create, Assignment::AvatarMixer, assignmentPool);
avatarAssignment->setAttachedLocalSocket((sockaddr*) &localSocket);
}
nodeList->sendAssignment(*avatarAssignment);
// reset the create time on the assignment so re-request is in ASSIGNMENT_SILENCE_MAX_USECS
avatarAssignment->setCreateTimeToNow();
}
} else {
assignmentSilenceCount[&avatarAssignment] = 0;
}
if (nodeList->getNodeSocket()->receive((sockaddr *)&nodePublicAddress, packetData, &receivedBytes) &&
(packetData[0] == PACKET_TYPE_DOMAIN_REPORT_FOR_DUTY || packetData[0] == PACKET_TYPE_DOMAIN_LIST_REQUEST) &&
packetVersionMatch(packetData)) {
@ -234,17 +242,20 @@ int main(int argc, char* const argv[]) {
}
}
if (Logstash::shouldSendStats()) {
if (Logging::shouldSendStats()) {
if (usecTimestampNow() - usecTimestamp(&lastStatSendTime) >= (NODE_COUNT_STAT_INTERVAL_MSECS * 1000)) {
// time to send our count of nodes and servers to logstash
const char NODE_COUNT_LOGSTASH_KEY[] = "ds-node-count";
Logstash::stashValue(STAT_TYPE_TIMER, NODE_COUNT_LOGSTASH_KEY, nodeList->getNumAliveNodes());
Logging::stashValue(STAT_TYPE_TIMER, NODE_COUNT_LOGSTASH_KEY, nodeList->getNumAliveNodes());
gettimeofday(&lastStatSendTime, NULL);
}
}
}
delete audioAssignment;
delete avatarAssignment;
return 0;
}

View file

@ -44,8 +44,7 @@ float texCoordToViewSpaceZ(vec2 texCoord) {
// given a texture coordinate, returns the 3D view space coordinate
vec3 texCoordToViewSpace(vec2 texCoord) {
float z = texCoordToViewSpaceZ(texCoord);
return vec3(((texCoord * 2.0 - vec2(1.0 - gl_ProjectionMatrix[2][0], 1.0)) *
(rightTop - leftBottom) + rightTop + leftBottom) * z / (-2.0 * near), z);
return vec3((leftBottom + texCoord * (rightTop - leftBottom)) * (-z / near), z);
}
void main(void) {

View file

@ -48,7 +48,7 @@
#include <NodeTypes.h>
#include <AudioInjectionManager.h>
#include <AudioInjector.h>
#include <Logstash.h>
#include <Logging.h>
#include <OctalCode.h>
#include <PacketHeaders.h>
#include <PairingHandler.h>
@ -129,7 +129,8 @@ Application::Application(int& argc, char** argv, timeval &startup_time) :
_bytesPerSecond(0),
_bytesCount(0),
_swatch(NULL),
_pasteMode(false)
_pasteMode(false),
_finishedNudge(true)
{
_applicationStartupTime = startup_time;
_window->setWindowTitle("Interface");
@ -319,7 +320,7 @@ void Application::initializeGL() {
const char LOGSTASH_INTERFACE_START_TIME_KEY[] = "interface-start-time";
// ask the Logstash class to record the startup time
Logstash::stashValue(STAT_TYPE_TIMER, LOGSTASH_INTERFACE_START_TIME_KEY, startupTime);
Logging::stashValue(STAT_TYPE_TIMER, LOGSTASH_INTERFACE_START_TIME_KEY, startupTime);
}
// update before the first render
@ -426,9 +427,6 @@ void Application::resizeGL(int width, int height) {
resetCamerasOnResizeGL(_viewFrustumOffsetCamera, width, height);
resetCamerasOnResizeGL(_myCamera, width, height);
// Tell our viewFrustum about this change, using the application camera
loadViewFrustum(_myCamera, _viewFrustum);
glViewport(0, 0, width, height); // shouldn't this account for the menu???
updateProjectionMatrix();
@ -439,9 +437,12 @@ void Application::updateProjectionMatrix() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Tell our viewFrustum about this change, using the application camera
loadViewFrustum(_myCamera, _viewFrustum);
float left, right, bottom, top, nearVal, farVal;
glm::vec4 nearClipPlane, farClipPlane;
_viewFrustum.computeOffAxisFrustum(left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane);
computeOffAxisFrustum(left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane);
// If we're in Display Frustum mode, then we want to use the slightly adjust near/far clip values of the
// _viewFrustumOffsetCamera, so that we can see more of the application content in the application's frustum
@ -634,8 +635,8 @@ void Application::keyPressEvent(QKeyEvent* event) {
case Qt::Key_J:
if (isShifted) {
_myCamera.setEyeOffsetOrientation(glm::normalize(
glm::quat(glm::vec3(0, 0.002f, 0)) * _myCamera.getEyeOffsetOrientation()));
_viewFrustum.setFocalLength(_viewFrustum.getFocalLength() - 0.1f);
} else {
_myCamera.setEyeOffsetPosition(_myCamera.getEyeOffsetPosition() + glm::vec3(-0.001, 0, 0));
}
@ -644,8 +645,8 @@ void Application::keyPressEvent(QKeyEvent* event) {
case Qt::Key_M:
if (isShifted) {
_myCamera.setEyeOffsetOrientation(glm::normalize(
glm::quat(glm::vec3(0, -0.002f, 0)) * _myCamera.getEyeOffsetOrientation()));
_viewFrustum.setFocalLength(_viewFrustum.getFocalLength() + 0.1f);
} else {
_myCamera.setEyeOffsetPosition(_myCamera.getEyeOffsetPosition() + glm::vec3(0.001, 0, 0));
}
@ -704,6 +705,9 @@ void Application::keyPressEvent(QKeyEvent* event) {
case Qt::Key_O:
Menu::getInstance()->triggerOption(MenuOption::VoxelSelectMode);
break;
case Qt::Key_N:
Menu::getInstance()->triggerOption(MenuOption::VoxelNudgeMode);
break;
case Qt::Key_Slash:
Menu::getInstance()->triggerOption(MenuOption::Stats);
break;
@ -844,6 +848,14 @@ void Application::mousePressEvent(QMouseEvent* event) {
pasteVoxels();
}
if (Menu::getInstance()->isOptionChecked(MenuOption::VoxelNudgeMode)) {
VoxelNode* clickedNode = _voxels.getVoxelAt(_mouseVoxel.x, _mouseVoxel.y, _mouseVoxel.z, _mouseVoxel.s);
if (clickedNode) {
_nudgeVoxel = _mouseVoxel;
_finishedNudge = false;
}
}
if (MAKE_SOUND_ON_VOXEL_CLICK && _isHoverVoxel && !_isHoverVoxelSounding) {
_hoverVoxelOriginalColor[0] = _hoverVoxel.red;
_hoverVoxelOriginalColor[1] = _hoverVoxel.green;
@ -1282,6 +1294,20 @@ void Application::pasteVoxels() {
}
}
void Application::nudgeVoxels() {
if (Menu::getInstance()->isOptionChecked(MenuOption::VoxelNudgeMode)) {
// calculate nudgeVec
glm::vec3 nudgeVec(_mouseVoxel.x - _nudgeVoxel.x, _mouseVoxel.y - _nudgeVoxel.y, _mouseVoxel.z - _nudgeVoxel.z);
VoxelNode* nodeToNudge = _voxels.getVoxelAt(_nudgeVoxel.x, _nudgeVoxel.y, _nudgeVoxel.z, _nudgeVoxel.s);
if (nodeToNudge) {
_voxels.getTree()->nudgeSubTree(nodeToNudge, nudgeVec, _voxelEditSender);
_finishedNudge = true;
}
}
}
void Application::setListenModeNormal() {
_audio.setListenMode(AudioRingBuffer::NORMAL);
}
@ -1378,6 +1404,7 @@ void Application::init() {
_palette.addAction(Menu::getInstance()->getActionForOption(MenuOption::VoxelColorMode), 0, 2);
_palette.addAction(Menu::getInstance()->getActionForOption(MenuOption::VoxelGetColorMode), 0, 3);
_palette.addAction(Menu::getInstance()->getActionForOption(MenuOption::VoxelSelectMode), 0, 4);
_palette.addAction(Menu::getInstance()->getActionForOption(MenuOption::VoxelNudgeMode), 0, 5);
_pieMenu.init("./resources/images/hifi-interface-tools-v2-pie.svg",
_glWidget->width(),
@ -1534,14 +1561,15 @@ void Application::update(float deltaTime) {
// Set where I am looking based on my mouse ray (so that other people can see)
glm::vec3 lookAtSpot;
// Update faceshift
_faceshift.update();
// if we have faceshift, use that to compute the lookat direction
glm::vec3 lookAtRayOrigin = mouseRayOrigin, lookAtRayDirection = mouseRayDirection;
if (_faceshift.isActive()) {
lookAtRayOrigin = _myAvatar.getHead().calculateAverageEyePosition();
float averagePitch = (_faceshift.getEyeGazeLeftPitch() + _faceshift.getEyeGazeRightPitch()) / 2.0f;
float averageYaw = (_faceshift.getEyeGazeLeftYaw() + _faceshift.getEyeGazeRightYaw()) / 2.0f;
lookAtRayDirection = _myAvatar.getHead().getOrientation() *
glm::quat(glm::radians(glm::vec3(averagePitch, averageYaw, 0.0f))) * glm::vec3(0.0f, 0.0f, -1.0f);
lookAtRayDirection = _myAvatar.getHead().getOrientation() * glm::quat(glm::radians(glm::vec3(
_faceshift.getEstimatedEyePitch(), _faceshift.getEstimatedEyeYaw(), 0.0f))) * glm::vec3(0.0f, 0.0f, -1.0f);
}
_isLookingAtOtherAvatar = isLookingAtOtherAvatar(lookAtRayOrigin, lookAtRayDirection, lookAtSpot);
@ -1558,7 +1586,7 @@ void Application::update(float deltaTime) {
lookAtSpot = lookAtRayOrigin + lookAtRayDirection * FAR_AWAY_STARE;
_myAvatar.getHead().setLookAtPosition(lookAtSpot);
}
// Find the voxel we are hovering over, and respond if clicked
float distance;
BoxFace face;
@ -1652,7 +1680,8 @@ void Application::update(float deltaTime) {
_mouseVoxel.s = 0.0f;
}
} else if (Menu::getInstance()->isOptionChecked(MenuOption::VoxelAddMode)
|| Menu::getInstance()->isOptionChecked(MenuOption::VoxelSelectMode)) {
|| Menu::getInstance()->isOptionChecked(MenuOption::VoxelSelectMode)
|| Menu::getInstance()->isOptionChecked(MenuOption::VoxelNudgeMode)) {
// place the voxel a fixed distance away
float worldMouseVoxelScale = _mouseVoxelScale * TREE_SCALE;
glm::vec3 pt = mouseRayOrigin + mouseRayDirection * (2.0f + worldMouseVoxelScale * 0.5f);
@ -1667,9 +1696,11 @@ void Application::update(float deltaTime) {
_mouseVoxel.red = 255;
_mouseVoxel.green = _mouseVoxel.blue = 0;
} else if (Menu::getInstance()->isOptionChecked(MenuOption::VoxelSelectMode)) {
// yellow indicates deletion
// yellow indicates selection
_mouseVoxel.red = _mouseVoxel.green = 255;
_mouseVoxel.blue = 0;
} else if (Menu::getInstance()->isOptionChecked(MenuOption::VoxelNudgeMode)) {
_mouseVoxel.red = _mouseVoxel.green = _mouseVoxel.blue = 255;
} else { // _addVoxelMode->isChecked() || _colorVoxelMode->isChecked()
QColor paintColor = Menu::getInstance()->getActionForOption(MenuOption::VoxelPaintColor)->data().value<QColor>();
_mouseVoxel.red = paintColor.red();
@ -1710,8 +1741,6 @@ void Application::update(float deltaTime) {
_serialHeadSensor.readData(deltaTime);
}
// Update transmitter
// Sample hardware, update view frustum if needed, and send avatar data to mixer/nodes
updateAvatar(deltaTime);
@ -1808,15 +1837,17 @@ void Application::update(float deltaTime) {
}
if (Menu::getInstance()->isOptionChecked(MenuOption::OffAxisProjection)) {
float xSign = Menu::getInstance()->isOptionChecked(MenuOption::Mirror) ? 1.0f : -1.0f;
if (_faceshift.isActive()) {
const float EYE_OFFSET_SCALE = 0.005f;
const float EYE_OFFSET_SCALE = 0.025f;
glm::vec3 position = _faceshift.getHeadTranslation() * EYE_OFFSET_SCALE;
_myCamera.setEyeOffsetPosition(glm::vec3(-position.x, position.y, position.z));
_myCamera.setEyeOffsetPosition(glm::vec3(position.x * xSign, position.y, position.z));
updateProjectionMatrix();
} else if (_webcam.isActive()) {
const float EYE_OFFSET_SCALE = 5.0f;
_myCamera.setEyeOffsetPosition(_webcam.getEstimatedPosition() * EYE_OFFSET_SCALE);
const float EYE_OFFSET_SCALE = 0.5f;
glm::vec3 position = _webcam.getEstimatedPosition() * EYE_OFFSET_SCALE;
_myCamera.setEyeOffsetPosition(glm::vec3(position.x * xSign, -position.y, position.z));
updateProjectionMatrix();
}
}
@ -2138,6 +2169,19 @@ void Application::setupWorldLight(Camera& whichCamera) {
glMateriali(GL_FRONT, GL_SHININESS, 96);
}
void Application::computeOffAxisFrustum(float& left, float& right, float& bottom, float& top, float& near,
float& far, glm::vec4& nearClipPlane, glm::vec4& farClipPlane) const {
_viewFrustum.computeOffAxisFrustum(left, right, bottom, top, near, far, nearClipPlane, farClipPlane);
// when mirrored, we must flip left and right
if (Menu::getInstance()->isOptionChecked(MenuOption::Mirror)) {
float tmp = left;
left = -right;
right = -tmp;
}
}
void Application::displaySide(Camera& whichCamera) {
// transform by eye offset
@ -2251,7 +2295,23 @@ void Application::displaySide(Camera& whichCamera) {
glDisable(GL_LIGHTING);
glPushMatrix();
glScalef(TREE_SCALE, TREE_SCALE, TREE_SCALE);
renderMouseVoxelGrid(_mouseVoxel.x, _mouseVoxel.y, _mouseVoxel.z, _mouseVoxel.s);
if (Menu::getInstance()->isOptionChecked(MenuOption::VoxelNudgeMode)) {
if (!_finishedNudge) {
renderNudgeGuide(_mouseVoxel.x, _mouseVoxel.y, _mouseVoxel.z, _nudgeVoxel.s);
renderNudgeGrid(_nudgeVoxel.x, _nudgeVoxel.y, _nudgeVoxel.z, _nudgeVoxel.s, _mouseVoxel.s);
glPushMatrix();
glTranslatef(_nudgeVoxel.x + _nudgeVoxel.s * 0.5f,
_nudgeVoxel.y + _nudgeVoxel.s * 0.5f,
_nudgeVoxel.z + _nudgeVoxel.s * 0.5f);
glColor3ub(255, 255, 255);
glLineWidth(4.0f);
glutWireCube(_nudgeVoxel.s);
glPopMatrix();
}
} else {
renderMouseVoxelGrid(_mouseVoxel.x, _mouseVoxel.y, _mouseVoxel.z, _mouseVoxel.s);
}
if (Menu::getInstance()->isOptionChecked(MenuOption::VoxelAddMode)) {
// use a contrasting color so that we can see what we're doing
glColor3ub(_mouseVoxel.red + 128, _mouseVoxel.green + 128, _mouseVoxel.blue + 128);
@ -2262,7 +2322,15 @@ void Application::displaySide(Camera& whichCamera) {
_mouseVoxel.y + _mouseVoxel.s*0.5f,
_mouseVoxel.z + _mouseVoxel.s*0.5f);
glLineWidth(4.0f);
glutWireCube(_mouseVoxel.s);
if (Menu::getInstance()->isOptionChecked(MenuOption::VoxelNudgeMode)) {
if (_nudgeVoxel.s) {
glutWireCube(_nudgeVoxel.s);
} else {
glutWireCube(_mouseVoxel.s);
}
} else {
glutWireCube(_mouseVoxel.s);
}
glLineWidth(1.0f);
glPopMatrix();
glEnable(GL_LIGHTING);
@ -2921,6 +2989,30 @@ void Application::renderViewFrustum(ViewFrustum& viewFrustum) {
// left plane - top edge - viewFrustum.getNear to distant
glVertex3f(viewFrustum.getNearTopLeft().x, viewFrustum.getNearTopLeft().y, viewFrustum.getNearTopLeft().z);
glVertex3f(viewFrustum.getFarTopLeft().x, viewFrustum.getFarTopLeft().y, viewFrustum.getFarTopLeft().z);
// focal plane - bottom edge
glColor3f(1.0f, 0.0f, 1.0f);
float focalProportion = (viewFrustum.getFocalLength() - viewFrustum.getNearClip()) /
(viewFrustum.getFarClip() - viewFrustum.getNearClip());
glm::vec3 focalBottomLeft = glm::mix(viewFrustum.getNearBottomLeft(), viewFrustum.getFarBottomLeft(), focalProportion);
glm::vec3 focalBottomRight = glm::mix(viewFrustum.getNearBottomRight(),
viewFrustum.getFarBottomRight(), focalProportion);
glVertex3f(focalBottomLeft.x, focalBottomLeft.y, focalBottomLeft.z);
glVertex3f(focalBottomRight.x, focalBottomRight.y, focalBottomRight.z);
// focal plane - top edge
glm::vec3 focalTopLeft = glm::mix(viewFrustum.getNearTopLeft(), viewFrustum.getFarTopLeft(), focalProportion);
glm::vec3 focalTopRight = glm::mix(viewFrustum.getNearTopRight(), viewFrustum.getFarTopRight(), focalProportion);
glVertex3f(focalTopLeft.x, focalTopLeft.y, focalTopLeft.z);
glVertex3f(focalTopRight.x, focalTopRight.y, focalTopRight.z);
// focal plane - left edge
glVertex3f(focalBottomLeft.x, focalBottomLeft.y, focalBottomLeft.z);
glVertex3f(focalTopLeft.x, focalTopLeft.y, focalTopLeft.z);
// focal plane - right edge
glVertex3f(focalBottomRight.x, focalBottomRight.y, focalBottomRight.z);
glVertex3f(focalTopRight.x, focalTopRight.y, focalTopRight.z);
}
glEnd();
glEnable(GL_LIGHTING);

View file

@ -135,6 +135,10 @@ public:
void setupWorldLight(Camera& whichCamera);
/// Computes the off-axis frustum parameters for the view frustum, taking mirroring into account.
void computeOffAxisFrustum(float& left, float& right, float& bottom, float& top, float& near,
float& far, glm::vec4& nearClipPlane, glm::vec4& farClipPlane) const;
virtual void nodeAdded(Node* node);
virtual void nodeKilled(Node* node);
virtual void packetSentNotification(ssize_t length);
@ -146,6 +150,7 @@ public slots:
void cutVoxels();
void copyVoxels();
void pasteVoxels();
void nudgeVoxels();
void setRenderVoxels(bool renderVoxels);
void doKillLocalVoxels();
@ -302,6 +307,8 @@ private:
glm::vec3 _lastMouseVoxelPos; // the position of the last mouse voxel edit
bool _justEditedVoxel; // set when we've just added/deleted/colored a voxel
VoxelDetail _nudgeVoxel; // details of the voxel to be nudged
bool _isLookingAtOtherAvatar;
glm::vec3 _lookatOtherPosition;
float _lookatIndicatorScale;
@ -354,6 +361,7 @@ private:
Swatch _swatch;
bool _pasteMode;
bool _finishedNudge;
PieMenu _pieMenu;

View file

@ -108,6 +108,7 @@ Menu::Menu() :
addActionToQMenuAndActionHash(editMenu, MenuOption::CutVoxels, Qt::CTRL | Qt::Key_X, appInstance, SLOT(cutVoxels()));
addActionToQMenuAndActionHash(editMenu, MenuOption::CopyVoxels, Qt::CTRL | Qt::Key_C, appInstance, SLOT(copyVoxels()));
addActionToQMenuAndActionHash(editMenu, MenuOption::PasteVoxels, Qt::CTRL | Qt::Key_V, appInstance, SLOT(pasteVoxels()));
addActionToQMenuAndActionHash(editMenu, MenuOption::NudgeVoxels, Qt::CTRL | Qt::Key_N, appInstance, SLOT(nudgeVoxels()));
addDisabledActionAndSeparator(editMenu, "Physics");
addCheckableActionToQMenuAndActionHash(editMenu, MenuOption::Gravity, Qt::SHIFT | Qt::Key_G, true);
@ -131,6 +132,9 @@ Menu::Menu() :
QAction* colorVoxelMode = addCheckableActionToQMenuAndActionHash(toolsMenu, MenuOption::VoxelColorMode, Qt::Key_B);
_voxelModeActionsGroup->addAction(colorVoxelMode);
QAction* nudgeVoxelMode = addCheckableActionToQMenuAndActionHash(toolsMenu, MenuOption::VoxelNudgeMode, Qt::Key_N);
_voxelModeActionsGroup->addAction(nudgeVoxelMode);
QAction* selectVoxelMode = addCheckableActionToQMenuAndActionHash(toolsMenu, MenuOption::VoxelSelectMode, Qt::Key_O);
_voxelModeActionsGroup->addAction(selectVoxelMode);

View file

@ -169,6 +169,7 @@ namespace MenuOption {
const QString LookAtVectors = "Look-at Vectors";
const QString LowRes = "Lower Resolution While Moving";
const QString Mirror = "Mirror";
const QString NudgeVoxels = "Nudge Voxels";
const QString OcclusionCulling = "Occlusion Culling";
const QString OffAxisProjection = "Off-Axis Projection";
const QString Oscilloscope = "Audio Oscilloscope";
@ -204,6 +205,7 @@ namespace MenuOption {
const QString VoxelGetColorMode = "Get Color Mode";
const QString VoxelMode = "Cycle Voxel Mode";
const QString VoxelPaintColor = "Voxel Paint Color";
const QString VoxelNudgeMode = "Nudge Voxel Mode";
const QString VoxelSelectMode = "Select Voxel Mode";
const QString VoxelStats = "Voxel Stats";
const QString VoxelTextures = "Voxel Textures";

View file

@ -414,6 +414,66 @@ void renderMouseVoxelGrid(const float& mouseVoxelX, const float& mouseVoxelY, co
glEnd();
}
void renderNudgeGrid(float voxelX, float voxelY, float voxelZ, float voxelS, float voxelPrecision) {
glm::vec3 origin = glm::vec3(voxelX, voxelY, voxelZ);
glLineWidth(1.0);
const int GRID_DIMENSIONS = 4;
const int GRID_SCALER = voxelS / voxelPrecision;
const int GRID_SEGMENTS = GRID_DIMENSIONS * GRID_SCALER;
glBegin(GL_LINES);
for (int xz = - (GRID_SEGMENTS / 2); xz <= GRID_SEGMENTS / 2 + GRID_SCALER; xz++) {
glm::vec3 xColor(0.0, 0.6, 0.0);
glColor3fv(&xColor.x);
glVertex3f(origin.x + GRID_DIMENSIONS * voxelS, 0, origin.z + xz * voxelPrecision);
glVertex3f(origin.x - (GRID_DIMENSIONS - 1) * voxelS, 0, origin.z + xz * voxelPrecision);
glm::vec3 zColor(0.0, 0.0, 0.6);
glColor3fv(&zColor.x);
glVertex3f(origin.x + xz * voxelPrecision, 0, origin.z + GRID_DIMENSIONS * voxelS);
glVertex3f(origin.x + xz * voxelPrecision, 0, origin.z - (GRID_DIMENSIONS - 1) * voxelS);
}
glEnd();
glColor3f(1.0f,1.0f,1.0f);
glBegin(GL_POLYGON);//begin drawing of square
glVertex3f(voxelX, 0.0f, voxelZ);//first vertex
glVertex3f(voxelX + voxelS, 0.0f, voxelZ);//second vertex
glVertex3f(voxelX + voxelS, 0.0f, voxelZ + voxelS);//third vertex
glVertex3f(voxelX, 0.0f, voxelZ + voxelS);//fourth vertex
glEnd();//end drawing of polygon
}
void renderNudgeGuide(float voxelX, float voxelY, float voxelZ, float voxelS) {
glm::vec3 origin = glm::vec3(voxelX, voxelY, voxelZ);
glLineWidth(3.0);
glBegin(GL_LINES);
glm::vec3 guideColor(1.0, 1.0, 1.0);
glColor3fv(&guideColor.x);
glVertex3f(origin.x + voxelS, 0, origin.z);
glVertex3f(origin.x, 0, origin.z);
glVertex3f(origin.x, 0, origin.z);
glVertex3f(origin.x, 0, origin.z + voxelS);
glVertex3f(origin.x + voxelS, 0, origin.z);
glVertex3f(origin.x + voxelS, 0, origin.z + voxelS);
glVertex3f(origin.x, 0, origin.z + voxelS);
glVertex3f(origin.x + voxelS, 0, origin.z + voxelS);
glEnd();
}
void renderDiskShadow(glm::vec3 position, glm::vec3 upDirection, float radius, float darkness) {
glColor4f(0.0f, 0.0f, 0.0f, darkness);

View file

@ -61,6 +61,10 @@ void renderGroundPlaneGrid(float size, float impact);
void renderMouseVoxelGrid(const float& mouseVoxelX, const float& mouseVoxelY, const float& mouseVoxelZ, const float& mouseVoxelS);
void renderNudgeGrid(float voxelX, float voxelY, float voxelZ, float voxelS, float voxelPrecision);
void renderNudgeGuide(float voxelX, float voxelY, float voxelZ, float voxelS);
void renderCollisionOverlay(int width, int height, float magnitude);
void renderDiskShadow(glm::vec3 position, glm::vec3 upDirection, float radius, float darkness);

View file

@ -146,19 +146,6 @@ void Head::resetHairPhysics() {
void Head::simulate(float deltaTime, bool isMine, float gyroCameraSensitivity) {
// Update eye saccades
const float AVERAGE_MICROSACCADE_INTERVAL = 0.50f;
const float AVERAGE_SACCADE_INTERVAL = 4.0f;
const float MICROSACCADE_MAGNITUDE = 0.002f;
const float SACCADE_MAGNITUDE = 0.04;
if (randFloat() < deltaTime / AVERAGE_MICROSACCADE_INTERVAL) {
_saccadeTarget = MICROSACCADE_MAGNITUDE * randVector();
} else if (randFloat() < deltaTime / AVERAGE_SACCADE_INTERVAL) {
_saccadeTarget = SACCADE_MAGNITUDE * randVector();
}
_saccade += (_saccadeTarget - _saccade) * 0.50f;
// Update audio trailing average for rendering facial animations
Faceshift* faceshift = Application::getInstance()->getFaceshift();
if (isMine && faceshift->isActive()) {
@ -173,6 +160,19 @@ void Head::simulate(float deltaTime, bool isMine, float gyroCameraSensitivity) {
_browAudioLift = faceshift->getBrowHeight() * BROW_HEIGHT_SCALE;
} else {
// Update eye saccades
const float AVERAGE_MICROSACCADE_INTERVAL = 0.50f;
const float AVERAGE_SACCADE_INTERVAL = 4.0f;
const float MICROSACCADE_MAGNITUDE = 0.002f;
const float SACCADE_MAGNITUDE = 0.04f;
if (randFloat() < deltaTime / AVERAGE_MICROSACCADE_INTERVAL) {
_saccadeTarget = MICROSACCADE_MAGNITUDE * randVector();
} else if (randFloat() < deltaTime / AVERAGE_SACCADE_INTERVAL) {
_saccadeTarget = SACCADE_MAGNITUDE * randVector();
}
_saccade += (_saccadeTarget - _saccade) * 0.50f;
const float AUDIO_AVERAGING_SECS = 0.05;
_averageLoudness = (1.f - deltaTime / AUDIO_AVERAGING_SECS) * _averageLoudness +
(deltaTime / AUDIO_AVERAGING_SECS) * _audioLoudness;

View file

@ -26,13 +26,30 @@ Faceshift::Faceshift() :
_browHeight(0.0f),
_browUpCenterIndex(-1),
_mouthSize(0.0f),
_jawOpenIndex(-1)
_jawOpenIndex(-1),
_longTermAverageEyePitch(0.0f),
_longTermAverageEyeYaw(0.0f),
_estimatedEyePitch(0.0f),
_estimatedEyeYaw(0.0f)
{
connect(&_socket, SIGNAL(connected()), SLOT(noteConnected()));
connect(&_socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(noteError(QAbstractSocket::SocketError)));
connect(&_socket, SIGNAL(readyRead()), SLOT(readFromSocket()));
}
void Faceshift::update() {
if (!isActive()) {
return;
}
float averageEyePitch = (_eyeGazeLeftPitch + _eyeGazeRightPitch) / 2.0f;
float averageEyeYaw = (_eyeGazeLeftYaw + _eyeGazeRightYaw) / 2.0f;
const float LONG_TERM_AVERAGE_SMOOTHING = 0.999f;
_longTermAverageEyePitch = glm::mix(averageEyePitch, _longTermAverageEyePitch, LONG_TERM_AVERAGE_SMOOTHING);
_longTermAverageEyeYaw = glm::mix(averageEyeYaw, _longTermAverageEyeYaw, LONG_TERM_AVERAGE_SMOOTHING);
_estimatedEyePitch = averageEyePitch - _longTermAverageEyePitch;
_estimatedEyeYaw = averageEyeYaw - _longTermAverageEyeYaw;
}
void Faceshift::reset() {
if (isActive()) {
string message;

View file

@ -35,6 +35,9 @@ public:
float getEyeGazeRightPitch() const { return _eyeGazeRightPitch; }
float getEyeGazeRightYaw() const { return _eyeGazeRightYaw; }
float getEstimatedEyePitch() const { return _estimatedEyePitch; }
float getEstimatedEyeYaw() const { return _estimatedEyeYaw; }
float getLeftBlink() const { return _leftBlink; }
float getRightBlink() const { return _rightBlink; }
@ -42,6 +45,7 @@ public:
float getMouthSize() const { return _mouthSize; }
void update();
void reset();
public slots:
@ -85,6 +89,12 @@ private:
float _mouthSize;
int _jawOpenIndex;
float _longTermAverageEyePitch;
float _longTermAverageEyeYaw;
float _estimatedEyePitch;
float _estimatedEyeYaw;
};
#endif /* defined(__interface__Faceshift__) */

View file

@ -103,7 +103,7 @@ void AmbientOcclusionEffect::render() {
float left, right, bottom, top, nearVal, farVal;
glm::vec4 nearClipPlane, farClipPlane;
Application::getInstance()->getViewFrustum()->computeOffAxisFrustum(
Application::getInstance()->computeOffAxisFrustum(
left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane);
_occlusionProgram->bind();

View file

@ -102,8 +102,7 @@ static Closure cmakeBuild(srcDir, instCommand) {
def targets = [
'animation-server':true,
'assignment-server':true,
'audio-mixer':true,
'avatar-mixer':true,
'assignment-client':true,
'domain-server':true,
'eve':true,
'pairing-server':true,
@ -188,4 +187,4 @@ doxygenJob.with {
}
}
queue doxygenJob
queue doxygenJob

View file

@ -32,7 +32,7 @@
#include <glm/gtx/norm.hpp>
#include <glm/gtx/vector_angle.hpp>
#include <Logstash.h>
#include <Logging.h>
#include <NodeList.h>
#include <Node.h>
#include <NodeTypes.h>
@ -57,6 +57,8 @@ const unsigned int BUFFER_SEND_INTERVAL_USECS = floorf((BUFFER_LENGTH_SAMPLES_PE
const int MAX_SAMPLE_VALUE = std::numeric_limits<int16_t>::max();
const int MIN_SAMPLE_VALUE = std::numeric_limits<int16_t>::min();
const char AUDIO_MIXER_LOGGING_TARGET_NAME[] = "audio-mixer";
void attachNewBufferToNode(Node *newNode) {
if (!newNode->getLinkedData()) {
if (newNode->getType() == NODE_TYPE_AGENT) {
@ -68,6 +70,8 @@ void attachNewBufferToNode(Node *newNode) {
}
void AudioMixer::run() {
// change the logging target name while this is running
Logging::setTargetName(AUDIO_MIXER_LOGGING_TARGET_NAME);
NodeList *nodeList = NodeList::getInstance();
nodeList->setOwnerType(NODE_TYPE_AUDIO_MIXER);
@ -105,8 +109,8 @@ void AudioMixer::run() {
stk::StkFrames stkFrameBuffer(BUFFER_LENGTH_SAMPLES_PER_CHANNEL, 1);
// if we'll be sending stats, call the Logstash::socket() method to make it load the logstash IP outside the loop
if (Logstash::shouldSendStats()) {
Logstash::socket();
if (Logging::shouldSendStats()) {
Logging::socket();
}
while (true) {
@ -114,7 +118,7 @@ void AudioMixer::run() {
break;
}
if (Logstash::shouldSendStats()) {
if (Logging::shouldSendStats()) {
gettimeofday(&beginSendTime, NULL);
}
@ -123,12 +127,12 @@ void AudioMixer::run() {
gettimeofday(&lastDomainServerCheckIn, NULL);
NodeList::getInstance()->sendDomainServerCheckIn();
if (Logstash::shouldSendStats() && numStatCollections > 0) {
if (Logging::shouldSendStats() && numStatCollections > 0) {
// if we should be sending stats to Logstash send the appropriate average now
const char MIXER_LOGSTASH_METRIC_NAME[] = "audio-mixer-frame-time-usage";
float averageFrameTimePercentage = sumFrameTimePercentages / numStatCollections;
Logstash::stashValue(STAT_TYPE_TIMER, MIXER_LOGSTASH_METRIC_NAME, averageFrameTimePercentage);
Logging::stashValue(STAT_TYPE_TIMER, MIXER_LOGSTASH_METRIC_NAME, averageFrameTimePercentage);
sumFrameTimePercentages = 0.0f;
numStatCollections = 0;
@ -398,7 +402,7 @@ void AudioMixer::run() {
}
}
if (Logstash::shouldSendStats()) {
if (Logging::shouldSendStats()) {
// send a packet to our logstash instance
// calculate the percentage value for time elapsed for this send (of the max allowable time)

View file

@ -10,6 +10,7 @@
// The avatar mixer receives head, hand and positional data from all connected
// nodes, and broadcasts that data back to them, every BROADCAST_INTERVAL ms.
#include <Logging.h>
#include <NodeList.h>
#include <PacketHeaders.h>
#include <SharedUtil.h>
@ -18,6 +19,8 @@
#include "AvatarMixer.h"
const char AVATAR_MIXER_LOGGING_NAME[] = "avatar-mixer";
unsigned char* addNodeToBroadcastPacket(unsigned char *currentPosition, Node *nodeToAdd) {
currentPosition += packNodeId(currentPosition, nodeToAdd->getNodeID());
@ -81,6 +84,9 @@ void broadcastAvatarData(NodeList* nodeList, sockaddr* nodeAddress) {
}
void AvatarMixer::run() {
// change the logging target name while AvatarMixer is running
Logging::setTargetName(AVATAR_MIXER_LOGGING_NAME);
NodeList* nodeList = NodeList::getInstance();
nodeList->setOwnerType(NODE_TYPE_AVATAR_MIXER);

View file

@ -6,8 +6,6 @@
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
//
#include <sys/time.h>
#include "PacketHeaders.h"
#include "Assignment.h"
@ -19,7 +17,8 @@ Assignment::Assignment(Assignment::Direction direction, Assignment::Type type, c
_direction(direction),
_type(type),
_pool(NULL),
_domainSocket(NULL)
_attachedPublicSocket(NULL),
_attachedLocalSocket(NULL)
{
// set the create time on this assignment
gettimeofday(&_time, NULL);
@ -36,7 +35,8 @@ Assignment::Assignment(Assignment::Direction direction, Assignment::Type type, c
Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) :
_pool(NULL),
_domainSocket(NULL)
_attachedPublicSocket(NULL),
_attachedLocalSocket(NULL)
{
// set the create time on this assignment
gettimeofday(&_time, NULL);
@ -66,26 +66,58 @@ Assignment::Assignment(const unsigned char* dataBuffer, int numBytes) :
}
if (numBytes > numBytesRead) {
sockaddr* newSocket = NULL;
if (dataBuffer[numBytesRead++] == IPv4_ADDRESS_DESIGNATOR) {
// IPv4 address
sockaddr_in destinationSocket = {};
memcpy(&destinationSocket, dataBuffer + numBytesRead, sizeof(sockaddr_in));
destinationSocket.sin_family = AF_INET;
setDomainSocket((sockaddr*) &destinationSocket);
newSocket = (sockaddr*) new sockaddr_in;
unpackSocket(dataBuffer + numBytesRead, newSocket);
} else {
// IPv6 address
sockaddr_in6 destinationSocket = {};
memcpy(&destinationSocket, dataBuffer + numBytesRead, sizeof(sockaddr_in6));
setDomainSocket((sockaddr*) &destinationSocket);
// IPv6 address, or bad designator
qDebug("Received a socket that cannot be unpacked!\n");
}
if (_direction == Assignment::Create) {
delete _attachedLocalSocket;
_attachedLocalSocket = newSocket;
} else {
delete _attachedPublicSocket;
_attachedPublicSocket = newSocket;
}
}
}
Assignment::~Assignment() {
delete _domainSocket;
delete _attachedPublicSocket;
delete _attachedLocalSocket;
delete _pool;
}
void Assignment::setAttachedPublicSocket(const sockaddr* attachedPublicSocket) {
if (_attachedPublicSocket) {
// delete the old socket if it exists
delete _attachedPublicSocket;
_attachedPublicSocket = NULL;
}
if (attachedPublicSocket) {
copySocketToEmptySocketPointer(&_attachedPublicSocket, attachedPublicSocket);
}
}
void Assignment::setAttachedLocalSocket(const sockaddr* attachedLocalSocket) {
if (_attachedLocalSocket) {
// delete the old socket if it exists
delete _attachedLocalSocket;
_attachedLocalSocket = NULL;
}
if (attachedLocalSocket) {
copySocketToEmptySocketPointer(&_attachedLocalSocket, attachedLocalSocket);
}
}
int Assignment::packToBuffer(unsigned char* buffer) {
int numPackedBytes = 0;
@ -102,36 +134,19 @@ int Assignment::packToBuffer(unsigned char* buffer) {
numPackedBytes += sizeof(char);
}
if (_domainSocket) {
buffer[numPackedBytes++] = (_domainSocket->sa_family == AF_INET) ? IPv4_ADDRESS_DESIGNATOR : IPv6_ADDRESS_DESIGNATOR;
if (_attachedPublicSocket || _attachedLocalSocket) {
sockaddr* socketToPack = (_attachedPublicSocket) ? _attachedPublicSocket : _attachedLocalSocket;
int numSocketBytes = (_domainSocket->sa_family == AF_INET) ? sizeof(sockaddr_in) : sizeof(sockaddr_in6);
// we have a socket to pack, add the designator
buffer[numPackedBytes++] = (socketToPack->sa_family == AF_INET)
? IPv4_ADDRESS_DESIGNATOR : IPv6_ADDRESS_DESIGNATOR;
memcpy(buffer + numPackedBytes, _domainSocket, numSocketBytes);
numPackedBytes += numSocketBytes;
numPackedBytes += packSocket(buffer + numPackedBytes, socketToPack);
}
return numPackedBytes;
}
void Assignment::setDomainSocket(const sockaddr* domainSocket) {
if (_domainSocket) {
// delete the old _domainSocket if it exists
delete _domainSocket;
_domainSocket = NULL;
}
// create a new sockaddr or sockaddr_in depending on what type of address this is
if (domainSocket->sa_family == AF_INET) {
_domainSocket = (sockaddr*) new sockaddr_in;
memcpy(_domainSocket, domainSocket, sizeof(sockaddr_in));
} else {
_domainSocket = (sockaddr*) new sockaddr_in6;
memcpy(_domainSocket, domainSocket, sizeof(sockaddr_in6));
}
}
QDebug operator<<(QDebug debug, const Assignment &assignment) {
debug << "T:" << assignment.getType() << "P:" << assignment.getPool();
return debug.nospace();

View file

@ -9,6 +9,8 @@
#ifndef __hifi__Assignment__
#define __hifi__Assignment__
#include <sys/time.h>
#include "NodeList.h"
/// Holds information used for request, creation, and deployment of assignments
@ -41,19 +43,26 @@ public:
const char* getPool() const { return _pool; }
const timeval& getTime() const { return _time; }
const sockaddr* getDomainSocket() { return _domainSocket; }
void setDomainSocket(const sockaddr* domainSocket);
const sockaddr* getAttachedPublicSocket() { return _attachedPublicSocket; }
void setAttachedPublicSocket(const sockaddr* attachedPublicSocket);
const sockaddr* getAttachedLocalSocket() { return _attachedLocalSocket; }
void setAttachedLocalSocket(const sockaddr* attachedLocalSocket);
/// Packs the assignment to the passed buffer
/// \param buffer the buffer in which to pack the assignment
/// \return number of bytes packed into buffer
int packToBuffer(unsigned char* buffer);
/// Sets _time to the current time given by gettimeofday
void setCreateTimeToNow() { gettimeofday(&_time, NULL); }
private:
Assignment::Direction _direction; /// the direction of the assignment (Create, Deploy, Request)
Assignment::Type _type; /// the type of the assignment, defines what the assignee will do
char* _pool; /// the pool this assignment is for/from
sockaddr* _domainSocket; /// pointer to socket for domain server that created assignment
sockaddr* _attachedPublicSocket; /// pointer to a public socket that relates to assignment, depends on direction
sockaddr* _attachedLocalSocket; /// pointer to a local socket that relates to assignment, depends on direction
timeval _time; /// time the assignment was created (set in constructor)
};

View file

@ -0,0 +1,118 @@
//
// Logging.cpp
// hifi
//
// Created by Stephen Birarda on 6/11/13.
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
//
#include <cstring>
#include <cstdio>
#include <iostream>
#include <netdb.h>
#include "SharedUtil.h"
#include "NodeList.h"
#include "Logging.h"
sockaddr_in Logging::logstashSocket = {};
char* Logging::targetName = NULL;
sockaddr* Logging::socket() {
if (logstashSocket.sin_addr.s_addr == 0) {
// we need to construct the socket object
// assume IPv4
logstashSocket.sin_family = AF_INET;
// use the constant port
logstashSocket.sin_port = htons(LOGSTASH_UDP_PORT);
// lookup the IP address for the constant hostname
struct hostent* logstashHostInfo;
if ((logstashHostInfo = gethostbyname(LOGSTASH_HOSTNAME))) {
memcpy(&logstashSocket.sin_addr, logstashHostInfo->h_addr_list[0], logstashHostInfo->h_length);
} else {
printf("Failed to lookup logstash IP - will try again on next log attempt.\n");
}
}
return (sockaddr*) &logstashSocket;
}
bool Logging::shouldSendStats() {
static bool shouldSendStats = isInEnvironment("production");
return shouldSendStats;
}
void Logging::stashValue(char statType, const char* key, float value) {
static char logstashPacket[MAX_PACKET_SIZE];
// load up the logstash packet with the key and the passed float value
// send it to 4 decimal places
int numPacketBytes = sprintf(logstashPacket, "%c %s %.4f", statType, key, value);
NodeList *nodeList = NodeList::getInstance();
if (nodeList) {
nodeList->getNodeSocket()->send(socket(), logstashPacket, numPacketBytes);
}
}
void Logging::setTargetName(const char* targetName) {
// remove the old target name, if it exists
delete Logging::targetName;
// copy over the new target name
Logging::targetName = new char[strlen(targetName)];
strcpy(Logging::targetName, targetName);
}
const char* stringForLogType(QtMsgType msgType) {
switch (msgType) {
case QtDebugMsg:
return "DEBUG";
case QtCriticalMsg:
return "CRITICAL";
case QtFatalMsg:
return "FATAL";
case QtWarningMsg:
return "WARNING";
}
}
// the following will produce 2000-10-02 13:55:36 -0700
const char DATE_STRING_FORMAT[] = "%F %H:%M:%S %z";
void Logging::verboseMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) {
// log prefix is in the following format
// [DEBUG] [TIMESTAMP] [PID:PARENT_PID] [TARGET] logged string
QString prefixString = QString("[%1]").arg(stringForLogType(type));
time_t rawTime;
time(&rawTime);
struct tm* localTime = localtime(&rawTime);
char dateString[100];
strftime(dateString, sizeof(dateString), DATE_STRING_FORMAT, localTime);
prefixString.append(QString(" [%1]").arg(dateString));
prefixString.append(QString(" [%1").arg(getpid()));
pid_t parentProcessID = getppid();
if (parentProcessID != 0) {
prefixString.append(QString(":%1]").arg(parentProcessID));
} else {
prefixString.append("]");
}
if (Logging::targetName) {
prefixString.append(QString(" [%1]").arg(Logging::targetName));
}
fprintf(stdout, "%s %s", prefixString.toLocal8Bit().constData(), message.toLocal8Bit().constData());
}

View file

@ -0,0 +1,51 @@
//
// Logging.h
// hifi
//
// Created by Stephen Birarda on 6/11/13.
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
//
#ifndef __hifi__Logging__
#define __hifi__Logging__
#include <netinet/in.h>
#include <QtCore/QString>
const int LOGSTASH_UDP_PORT = 9500;
const char LOGSTASH_HOSTNAME[] = "graphite.highfidelity.io";
const char STAT_TYPE_TIMER = 't';
const char STAT_TYPE_COUNTER = 'c';
const char STAT_TYPE_GAUGE = 'g';
/// Handles custom message handling and sending of stats/logs to Logstash instance
class Logging {
public:
/// \return the socket used to send stats to logstash
static sockaddr* socket();
/// checks if this target should send stats to logstash, given its current environment
/// \return true if the caller should send stats to logstash
static bool shouldSendStats();
/// stashes a float value to Logstash instance
/// \param statType a stat type from the constants in this file
/// \param key the key at which to store the stat
/// \param value the value to store
static void stashValue(char statType, const char* key, float value);
/// sets the target name to output via the verboseMessageHandler, called once before logging begins
/// \param targetName the desired target name to output in logs
static void setTargetName(const char* targetName);
/// a qtMessageHandler that can be hooked up to a target that links to Qt
/// prints various process, message type, and time information
static void verboseMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString &message);
private:
static sockaddr_in logstashSocket;
static char* targetName;
};
#endif /* defined(__hifi__Logstash__) */

View file

@ -1,60 +0,0 @@
//
// Logstash.cpp
// hifi
//
// Created by Stephen Birarda on 6/11/13.
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
//
#include <cstring>
#include <cstdio>
#include <netdb.h>
#include "SharedUtil.h"
#include "NodeList.h"
#include "Logstash.h"
sockaddr_in Logstash::logstashSocket = {};
sockaddr* Logstash::socket() {
if (logstashSocket.sin_addr.s_addr == 0) {
// we need to construct the socket object
// assume IPv4
logstashSocket.sin_family = AF_INET;
// use the constant port
logstashSocket.sin_port = htons(LOGSTASH_UDP_PORT);
// lookup the IP address for the constant hostname
struct hostent* logstashHostInfo;
if ((logstashHostInfo = gethostbyname(LOGSTASH_HOSTNAME))) {
memcpy(&logstashSocket.sin_addr, logstashHostInfo->h_addr_list[0], logstashHostInfo->h_length);
} else {
printf("Failed to lookup logstash IP - will try again on next log attempt.\n");
}
}
return (sockaddr*) &logstashSocket;
}
bool Logstash::shouldSendStats() {
static bool shouldSendStats = isInEnvironment("production");
return shouldSendStats;
}
void Logstash::stashValue(char statType, const char* key, float value) {
static char logstashPacket[MAX_PACKET_SIZE];
// load up the logstash packet with the key and the passed float value
// send it to 4 decimal places
int numPacketBytes = sprintf(logstashPacket, "%c %s %.4f", statType, key, value);
NodeList *nodeList = NodeList::getInstance();
if (nodeList) {
nodeList->getNodeSocket()->send(socket(), logstashPacket, numPacketBytes);
}
}

View file

@ -1,30 +0,0 @@
//
// Logstash.h
// hifi
//
// Created by Stephen Birarda on 6/11/13.
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
//
#ifndef __hifi__Logstash__
#define __hifi__Logstash__
#include <netinet/in.h>
const int LOGSTASH_UDP_PORT = 9500;
const char LOGSTASH_HOSTNAME[] = "graphite.highfidelity.io";
const char STAT_TYPE_TIMER = 't';
const char STAT_TYPE_COUNTER = 'c';
const char STAT_TYPE_GAUGE = 'g';
class Logstash {
public:
static sockaddr* socket();
static bool shouldSendStats();
static void stashValue(char statType, const char* key, float value);
private:
static sockaddr_in logstashSocket;
};
#endif /* defined(__hifi__Logstash__) */

View file

@ -14,6 +14,7 @@
#include <QtCore/QDebug>
#include "Assignment.h"
#include "Logging.h"
#include "NodeList.h"
#include "NodeTypes.h"
#include "PacketHeaders.h"
@ -43,7 +44,7 @@ NodeList* NodeList::createInstance(char ownerType, unsigned short int socketList
if (!_sharedInstance) {
_sharedInstance = new NodeList(ownerType, socketListenPort);
} else {
qDebug("NodeList createInstance called with existing instance.\n");
qDebug("NodeList createInstance called with existing instance.");
}
return _sharedInstance;
@ -51,7 +52,7 @@ NodeList* NodeList::createInstance(char ownerType, unsigned short int socketList
NodeList* NodeList::getInstance() {
if (!_sharedInstance) {
qDebug("NodeList getInstance called before call to createInstance. Returning NULL pointer.\n");
qDebug("NodeList getInstance called before call to createInstance. Returning NULL pointer.");
}
return _sharedInstance;
@ -65,7 +66,8 @@ NodeList::NodeList(char newOwnerType, unsigned short int newSocketListenPort) :
_nodeTypesOfInterest(NULL),
_ownerID(UNKNOWN_NODE_ID),
_lastNodeID(UNKNOWN_NODE_ID + 1),
_numNoReplyDomainCheckIns(0)
_numNoReplyDomainCheckIns(0),
_assignmentServerSocket(NULL)
{
memcpy(_domainHostname, DEFAULT_DOMAIN_HOSTNAME, sizeof(DEFAULT_DOMAIN_HOSTNAME));
memcpy(_domainIP, DEFAULT_DOMAIN_IP, sizeof(DEFAULT_DOMAIN_IP));
@ -255,6 +257,7 @@ void NodeList::clear() {
}
_numNodes = 0;
_numNoReplyDomainCheckIns = 0;
}
void NodeList::setNodeTypesOfInterest(const char* nodeTypesOfInterest, int numNodeTypesOfInterest) {
@ -276,7 +279,6 @@ void NodeList::sendDomainServerCheckIn() {
sockaddr_in tempAddress;
memcpy(&tempAddress.sin_addr, pHostInfo->h_addr_list[0], pHostInfo->h_length);
strcpy(_domainIP, inet_ntoa(tempAddress.sin_addr));
qDebug("Domain Server: %s\n", _domainHostname);
} else {
qDebug("Failed domain server lookup\n");
@ -374,10 +376,9 @@ int NodeList::processDomainServerList(unsigned char* packetData, size_t dataByte
return readNodes;
}
const char ASSIGNMENT_SERVER_HOSTNAME[] = "assignment.highfidelity.io";
const sockaddr_in assignmentServerSocket = socketForHostnameAndHostOrderPort(ASSIGNMENT_SERVER_HOSTNAME,
ASSIGNMENT_SERVER_PORT);
const char GLOBAL_ASSIGNMENT_SERVER_HOSTNAME[] = "assignment.highfidelity.io";
const sockaddr_in GLOBAL_ASSIGNMENT_SOCKET = socketForHostnameAndHostOrderPort(GLOBAL_ASSIGNMENT_SERVER_HOSTNAME,
ASSIGNMENT_SERVER_PORT);
void NodeList::sendAssignment(Assignment& assignment) {
unsigned char assignmentPacket[MAX_PACKET_SIZE];
@ -387,8 +388,12 @@ void NodeList::sendAssignment(Assignment& assignment) {
int numHeaderBytes = populateTypeAndVersion(assignmentPacket, assignmentPacketType);
int numAssignmentBytes = assignment.packToBuffer(assignmentPacket + numHeaderBytes);
_nodeSocket.send((sockaddr*) &assignmentServerSocket, assignmentPacket, numHeaderBytes + numAssignmentBytes);
sockaddr* assignmentServerSocket = (_assignmentServerSocket == NULL)
? (sockaddr*) &GLOBAL_ASSIGNMENT_SOCKET
: _assignmentServerSocket;
_nodeSocket.send((sockaddr*) assignmentServerSocket, assignmentPacket, numHeaderBytes + numAssignmentBytes);
}
Node* NodeList::addOrUpdateNode(sockaddr* publicSocket, sockaddr* localSocket, char nodeType, uint16_t nodeId) {
@ -511,7 +516,7 @@ void* removeSilentNodes(void *args) {
if ((checkTimeUSecs - node->getLastHeardMicrostamp()) > NODE_SILENCE_THRESHOLD_USECS) {
qDebug() << "Killed" << *node << "\n";
qDebug() << "Killed " << *node << "\n";
nodeList->notifyHooksOfKilledNode(&*node);

View file

@ -99,6 +99,7 @@ public:
void sendDomainServerCheckIn();
int processDomainServerList(unsigned char *packetData, size_t dataBytes);
void setAssignmentServerSocket(sockaddr* serverSocket) { _assignmentServerSocket = serverSocket; }
void sendAssignment(Assignment& assignment);
Node* nodeWithAddress(sockaddr *senderAddress);
@ -151,6 +152,7 @@ private:
pthread_t removeSilentNodesThread;
pthread_t checkInWithDomainServerThread;
int _numNoReplyDomainCheckIns;
sockaddr* _assignmentServerSocket;
void handlePingReply(sockaddr *nodeAddress);
void timePingReply(sockaddr *nodeAddress, unsigned char *packetData);

View file

@ -23,6 +23,7 @@
#include <QtCore/QDebug>
#include "Logging.h"
#include "UDPSocket.h"
sockaddr_in destSockaddr, senderAddress;
@ -67,13 +68,25 @@ int packSocket(unsigned char* packStore, sockaddr* socketToPack) {
return packSocket(packStore, ((sockaddr_in*) socketToPack)->sin_addr.s_addr, ((sockaddr_in*) socketToPack)->sin_port);
}
int unpackSocket(unsigned char* packedData, sockaddr* unpackDestSocket) {
int unpackSocket(const unsigned char* packedData, sockaddr* unpackDestSocket) {
sockaddr_in* destinationSocket = (sockaddr_in*) unpackDestSocket;
destinationSocket->sin_family = AF_INET;
destinationSocket->sin_addr.s_addr = (packedData[0] << 24) + (packedData[1] << 16) + (packedData[2] << 8) + packedData[3];
destinationSocket->sin_port = (packedData[4] << 8) + packedData[5];
return 6; // this could be more if we ever need IPv6
}
void copySocketToEmptySocketPointer(sockaddr** destination, const sockaddr* source) {
// create a new sockaddr or sockaddr_in depending on what type of address this is
if (source->sa_family == AF_INET) {
*destination = (sockaddr*) new sockaddr_in;
memcpy(*destination, source, sizeof(sockaddr_in));
} else {
*destination = (sockaddr*) new sockaddr_in6;
memcpy(*destination, source, sizeof(sockaddr_in6));
}
}
int getLocalAddress() {
// get this node's local address so we can pass that to DS
struct ifaddrs* ifAddrStruct = NULL;
@ -170,7 +183,7 @@ UDPSocket::UDPSocket(unsigned short int listeningPort) :
const int DEFAULT_BLOCKING_SOCKET_TIMEOUT_USECS = 0.5 * 1000000;
setBlockingReceiveTimeoutInUsecs(DEFAULT_BLOCKING_SOCKET_TIMEOUT_USECS);
qDebug("Created UDP socket listening on port %hu.\n", _listeningPort);
qDebug("Created UDP Socket listening on %hd\n", _listeningPort);
}
UDPSocket::~UDPSocket() {

View file

@ -44,7 +44,8 @@ private:
bool socketMatch(const sockaddr* first, const sockaddr* second);
int packSocket(unsigned char* packStore, in_addr_t inAddress, in_port_t networkOrderPort);
int packSocket(unsigned char* packStore, sockaddr* socketToPack);
int unpackSocket(unsigned char* packedData, sockaddr* unpackDestSocket);
int unpackSocket(const unsigned char* packedData, sockaddr* unpackDestSocket);
void copySocketToEmptySocketPointer(sockaddr** destination, const sockaddr* source);
int getLocalAddress();
unsigned short loadBufferWithSocketInfo(char* addressBuffer, sockaddr* socket);
sockaddr_in socketForHostnameAndHostOrderPort(const char* hostname, unsigned short port = 0);

View file

@ -32,6 +32,7 @@ ViewFrustum::ViewFrustum() :
_aspectRatio(1.0),
_nearClip(0.1),
_farClip(500.0),
_focalLength(0.25f),
_keyholeRadius(DEFAULT_KEYHOLE_RADIUS),
_farTopLeft(0,0,0),
_farTopRight(0,0,0),
@ -310,15 +311,16 @@ bool testMatches(float lhs, float rhs) {
bool ViewFrustum::matches(const ViewFrustum& compareTo, bool debug) const {
bool result =
testMatches(compareTo._position, _position ) &&
testMatches(compareTo._direction, _direction ) &&
testMatches(compareTo._up, _up ) &&
testMatches(compareTo._right, _right ) &&
testMatches(compareTo._fieldOfView, _fieldOfView ) &&
testMatches(compareTo._aspectRatio, _aspectRatio ) &&
testMatches(compareTo._nearClip, _nearClip ) &&
testMatches(compareTo._farClip, _farClip ) &&
testMatches(compareTo._eyeOffsetPosition, _eyeOffsetPosition ) &&
testMatches(compareTo._position, _position) &&
testMatches(compareTo._direction, _direction) &&
testMatches(compareTo._up, _up) &&
testMatches(compareTo._right, _right) &&
testMatches(compareTo._fieldOfView, _fieldOfView) &&
testMatches(compareTo._aspectRatio, _aspectRatio) &&
testMatches(compareTo._nearClip, _nearClip) &&
testMatches(compareTo._farClip, _farClip) &&
testMatches(compareTo._focalLength, _focalLength) &&
testMatches(compareTo._eyeOffsetPosition, _eyeOffsetPosition) &&
testMatches(compareTo._eyeOffsetOrientation, _eyeOffsetOrientation);
if (!result && debug) {
@ -351,6 +353,9 @@ bool ViewFrustum::matches(const ViewFrustum& compareTo, bool debug) const {
qDebug("%s -- compareTo._farClip=%f _farClip=%f\n",
(testMatches(compareTo._farClip, _farClip) ? "MATCHES " : "NO MATCH"),
compareTo._farClip, _farClip);
qDebug("%s -- compareTo._focalLength=%f _focalLength=%f\n",
(testMatches(compareTo._focalLength, _focalLength) ? "MATCHES " : "NO MATCH"),
compareTo._focalLength, _focalLength);
qDebug("%s -- compareTo._eyeOffsetPosition=%f,%f,%f _eyeOffsetPosition=%f,%f,%f\n",
(testMatches(compareTo._eyeOffsetPosition, _eyeOffsetPosition) ? "MATCHES " : "NO MATCH"),
compareTo._eyeOffsetPosition.x, compareTo._eyeOffsetPosition.y, compareTo._eyeOffsetPosition.z,
@ -401,13 +406,17 @@ void ViewFrustum::computeOffAxisFrustum(float& left, float& right, float& bottom
nearClipPlane = glm::vec4(-normal.x, -normal.y, -normal.z, glm::dot(normal, corners[0]));
farClipPlane = glm::vec4(normal.x, normal.y, normal.z, -glm::dot(normal, corners[4]));
// compute the focal proportion (zero is near clip, one is far clip)
float focalProportion = (_focalLength - _nearClip) / (_farClip - _nearClip);
// get the extents at Z = -near
left = FLT_MAX;
right = -FLT_MAX;
bottom = FLT_MAX;
top = -FLT_MAX;
for (int i = 0; i < 4; i++) {
glm::vec4 intersection = corners[i] * (-near / corners[i].z);
glm::vec4 corner = glm::mix(corners[i], corners[i + 4], focalProportion);
glm::vec4 intersection = corner * (-near / corner.z);
left = min(left, intersection.x);
right = max(right, intersection.x);
bottom = min(bottom, intersection.y);
@ -426,6 +435,7 @@ void ViewFrustum::printDebugDetails() const {
qDebug("_keyHoleRadius=%f\n", _keyholeRadius);
qDebug("_nearClip=%f\n", _nearClip);
qDebug("_farClip=%f\n", _farClip);
qDebug("_focalLength=%f\n", _focalLength);
qDebug("_eyeOffsetPosition=%f,%f,%f\n", _eyeOffsetPosition.x, _eyeOffsetPosition.y, _eyeOffsetPosition.z );
qDebug("_eyeOffsetOrientation=%f,%f,%f,%f\n", _eyeOffsetOrientation.x, _eyeOffsetOrientation.y, _eyeOffsetOrientation.z,
_eyeOffsetOrientation.w );

View file

@ -39,6 +39,7 @@ public:
void setAspectRatio(float a) { _aspectRatio = a; }
void setNearClip(float n) { _nearClip = n; }
void setFarClip(float f) { _farClip = f; }
void setFocalLength(float length) { _focalLength = length; }
void setEyeOffsetPosition(const glm::vec3& p) { _eyeOffsetPosition = p; }
void setEyeOffsetOrientation(const glm::quat& o) { _eyeOffsetOrientation = o; }
@ -47,6 +48,7 @@ public:
float getAspectRatio() const { return _aspectRatio; }
float getNearClip() const { return _nearClip; }
float getFarClip() const { return _farClip; }
float getFocalLength() const { return _focalLength; }
const glm::vec3& getEyeOffsetPosition() const { return _eyeOffsetPosition; }
const glm::quat& getEyeOffsetOrientation() const { return _eyeOffsetOrientation; }
@ -111,12 +113,13 @@ private:
glm::vec3 _right;
// Lens attributes
float _fieldOfView;
float _aspectRatio;
float _nearClip;
float _farClip;
glm::vec3 _eyeOffsetPosition;
glm::quat _eyeOffsetOrientation;
float _fieldOfView;
float _aspectRatio;
float _nearClip;
float _farClip;
float _focalLength;
glm::vec3 _eyeOffsetPosition;
glm::quat _eyeOffsetOrientation;
// keyhole attributes
float _keyholeRadius;

View file

@ -12,6 +12,7 @@
#define __shared__VoxelEditPacketSender__
#include <PacketSender.h>
#include <PacketHeaders.h>
#include <SharedUtil.h> // for VoxelDetail
#include "JurisdictionMap.h"

View file

@ -422,4 +422,4 @@ void VoxelNode::notifyDeleteHooks() {
for (int i = 0; i < _hooks.size(); i++) {
_hooks[i]->nodeDeleted(this);
}
}
}

View file

@ -31,6 +31,7 @@
#include "VoxelConstants.h"
#include "VoxelNodeBag.h"
#include "VoxelTree.h"
#include <PacketHeaders.h>
float boundaryDistanceForRenderLevel(unsigned int renderLevel) {
return ::VOXEL_SIZE_SCALE / powf(2, renderLevel);
@ -151,7 +152,7 @@ VoxelNode* VoxelTree::nodeForOctalCode(VoxelNode* ancestorNode,
return childNode;
} else {
// we need to go deeper
return nodeForOctalCode(childNode, needleCode,parentOfFoundNode);
return nodeForOctalCode(childNode, needleCode, parentOfFoundNode);
}
}
}
@ -397,10 +398,10 @@ void VoxelTree::deleteVoxelCodeFromTreeRecursion(VoxelNode* node, void* extraDat
}
}
}
int lengthOfancestorNode = numberOfThreeBitSectionsInCode(ancestorNode->getOctalCode());
int lengthOfAncestorNode = numberOfThreeBitSectionsInCode(ancestorNode->getOctalCode());
// If we've reached the parent of the target, then stop breaking up children
if (lengthOfancestorNode == (args->lengthOfCode - 1)) {
if (lengthOfAncestorNode == (args->lengthOfCode - 1)) {
break;
}
ancestorNode->addChildAtIndex(index);
@ -1856,3 +1857,133 @@ void VoxelTree::emptyDeleteQueue() {
void VoxelTree::cancelImport() {
_stopImport = true;
}
class NodeChunkArgs {
public:
VoxelTree* thisVoxelTree;
float ancestorSize;
glm::vec3 nudgeVec;
VoxelEditPacketSender* voxelEditSenderPtr;
};
float findNewLeafSize(const glm::vec3& nudgeAmount, float leafSize) {
// we want the smallest non-zero and non-negative new leafSize
float newLeafSizeX = fabs(fmod(nudgeAmount.x, leafSize));
float newLeafSizeY = fabs(fmod(nudgeAmount.y, leafSize));
float newLeafSizeZ = fabs(fmod(nudgeAmount.z, leafSize));
float newLeafSize = leafSize;
if (newLeafSizeX) {
newLeafSize = fmin(newLeafSize, newLeafSizeX);
}
if (newLeafSizeY) {
newLeafSize = fmin(newLeafSize, newLeafSizeY);
}
if (newLeafSizeZ) {
newLeafSize = fmin(newLeafSize, newLeafSizeZ);
}
return newLeafSize;
}
bool VoxelTree::nudgeCheck(VoxelNode* node, void* extraData) {
if (node->isLeaf()) {
// we have reached the deepest level of nodes/voxels
// now there are two scenarios
// 1) this node's size is <= the minNudgeAmount
// in which case we will simply call nudgeLeaf on this leaf
// 2) this node's size is still not <= the minNudgeAmount
// in which case we need to break this leaf down until the leaf sizes are <= minNudgeAmount
NodeChunkArgs* args = (NodeChunkArgs*)extraData;
// get octal code of this node
unsigned char* octalCode = node->getOctalCode();
// get voxel position/size
VoxelPositionSize unNudgedDetails;
voxelDetailsForCode(octalCode, unNudgedDetails);
// find necessary leaf size
float newLeafSize = findNewLeafSize(args->nudgeVec, unNudgedDetails.s);
// check to see if this unNudged node can be nudged
if (unNudgedDetails.s <= newLeafSize) {
args->thisVoxelTree->nudgeLeaf(node, extraData);
return false;
} else {
// break the current leaf into smaller chunks
args->thisVoxelTree->chunkifyLeaf(node);
}
}
return true;
}
void VoxelTree::chunkifyLeaf(VoxelNode* node) {
// because this function will continue being called recursively
// we only need to worry about breaking this specific leaf down
if (!node->isColored()) {
return;
}
for (int i = 0; i < NUMBER_OF_CHILDREN; i++) {
node->addChildAtIndex(i);
node->getChildAtIndex(i)->setColor(node->getColor());
}
}
// This function is called to nudge the leaves of a tree, given that the
// nudge amount is >= to the leaf scale.
void VoxelTree::nudgeLeaf(VoxelNode* node, void* extraData) {
NodeChunkArgs* args = (NodeChunkArgs*)extraData;
// get octal code of this node
unsigned char* octalCode = node->getOctalCode();
// get voxel position/size
VoxelPositionSize unNudgedDetails;
voxelDetailsForCode(octalCode, unNudgedDetails);
VoxelDetail voxelDetails;
voxelDetails.x = unNudgedDetails.x;
voxelDetails.y = unNudgedDetails.y;
voxelDetails.z = unNudgedDetails.z;
voxelDetails.s = unNudgedDetails.s;
voxelDetails.red = node->getColor()[RED_INDEX];
voxelDetails.green = node->getColor()[GREEN_INDEX];
voxelDetails.blue = node->getColor()[BLUE_INDEX];
glm::vec3 nudge = args->nudgeVec;
// delete the old node
// if the nudge replaces the node in an area outside of the ancestor node
if (fabs(nudge.x) >= args->ancestorSize || fabs(nudge.y) >= args->ancestorSize || fabs(nudge.z) >= args->ancestorSize) {
args->voxelEditSenderPtr->sendVoxelEditMessage(PACKET_TYPE_ERASE_VOXEL, voxelDetails);
}
// nudge the old node
voxelDetails.x = unNudgedDetails.x + nudge.x;
voxelDetails.y = unNudgedDetails.y + nudge.y;
voxelDetails.z = unNudgedDetails.z + nudge.z;
// create a new voxel in its stead
args->voxelEditSenderPtr->sendVoxelEditMessage(PACKET_TYPE_SET_VOXEL_DESTRUCTIVE, voxelDetails);
}
void VoxelTree::nudgeSubTree(VoxelNode* nodeToNudge, const glm::vec3& nudgeAmount, VoxelEditPacketSender& voxelEditSender) {
if (nudgeAmount == glm::vec3(0, 0, 0)) {
return;
}
// get octal code of this node
unsigned char* octalCode = nodeToNudge->getOctalCode();
// get voxel position/size
VoxelPositionSize ancestorDetails;
voxelDetailsForCode(octalCode, ancestorDetails);
NodeChunkArgs args;
args.thisVoxelTree = this;
args.ancestorSize = ancestorDetails.s;
args.nudgeVec = nudgeAmount;
args.voxelEditSenderPtr = &voxelEditSender;
recurseNodeWithOperation(nodeToNudge, nudgeCheck, &args);
}

View file

@ -18,6 +18,7 @@
#include "VoxelNode.h"
#include "VoxelNodeBag.h"
#include "VoxelSceneStats.h"
#include "VoxelEditPacketSender.h"
#include <QObject>
@ -188,6 +189,9 @@ public:
void recurseNodeWithOperation(VoxelNode* node, RecurseVoxelTreeOperation operation, void* extraData);
void recurseNodeWithOperationDistanceSorted(VoxelNode* node, RecurseVoxelTreeOperation operation,
const glm::vec3& point, void* extraData);
void nudgeSubTree(VoxelNode* nodeToNudge, const glm::vec3& nudgeAmount, VoxelEditPacketSender& voxelEditSender);
signals:
void importSize(float x, float y, float z);
void importProgress(int progress);
@ -247,6 +251,11 @@ private:
void queueForLaterDelete(unsigned char* codeBuffer);
/// flushes out any Octal Codes that had to be queued
void emptyDeleteQueue();
// helper functions for nudgeSubTree
static bool nudgeCheck(VoxelNode* node, void* extraData);
void nudgeLeaf(VoxelNode* node, void* extraData);
void chunkifyLeaf(VoxelNode* node);
};
float boundaryDistanceForRenderLevel(unsigned int renderLevel);