diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 9bed329251..cb4335f553 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -225,6 +225,8 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : _glWidget->setMouseTracking(true); // initialization continues in initializeGL when OpenGL context is ready + + QCoreApplication::setOrganizationDomain("highfidelity.io"); // Used by QSettings on OS X } void Application::initializeGL() { @@ -277,8 +279,6 @@ void Application::initializeGL() { connect(idleTimer, SIGNAL(timeout()), SLOT(idle())); idleTimer->start(0); - readSettings(); - if (_justStarted) { float startupTime = (usecTimestampNow() - usecTimestamp(&_applicationStartupTime))/1000000.0; _justStarted = false; @@ -740,177 +740,6 @@ void Application::wheelEvent(QWheelEvent* event) { } } -const char AVATAR_DATA_FILENAME[] = "avatar.ifd"; - -void Application::readSettingsFile() { - FILE* settingsFile = fopen(AVATAR_DATA_FILENAME, "rt"); - - if (settingsFile) { - char line[LINE_MAX]; - - while (fgets(line, LINE_MAX, settingsFile) != NULL) - { - if (strcmp(line, " \n") > 0) { - char* token = NULL; - char* settingLine = NULL; - char* toFree = NULL; - - settingLine = strdup(line); - - if (settingLine != NULL) { - toFree = settingLine; - - int i = 0; - - char key[128]; - char value[128]; - - - while ((token = strsep(&settingLine, "=")) != NULL) - { - switch (i) { - case 0: - strcpy(key, token); - _settingsTable[key] = ""; - break; - - case 1: - strcpy(value, token); - _settingsTable[key] = token; - break; - - default: - break; - } - - i++; - } - - free(toFree); - } - } - } - - fclose(settingsFile); - } -} - -void Application::saveSettingsFile() { - FILE* settingsFile = fopen(AVATAR_DATA_FILENAME, "wt"); - - if (settingsFile) { - for (std::map::iterator i = _settingsTable.begin(); i != _settingsTable.end(); i++) - { - fprintf(settingsFile, "\n%s=%s", i->first.data(), i->second.data()); - } - } - - fclose(settingsFile); -} - -bool Application::getSetting(const char* setting, bool& value, const bool defaultSetting) const { - std::map::const_iterator iter = _settingsTable.find(setting); - - if (iter != _settingsTable.end()) { - int readBool; - - int res = sscanf(iter->second.data(), "%d", &readBool); - - const char EXPECTED_ITEMS = 1; - - if (res == EXPECTED_ITEMS) { - if (readBool == 1) { - value = true; - } else if (readBool == 0) { - value = false; - } - } - } else { - value = defaultSetting; - return false; - } - - return true; -} - -bool Application::getSetting(const char* setting, float& value, const float defaultSetting) const { - std::map::const_iterator iter = _settingsTable.find(setting); - - if (iter != _settingsTable.end()) { - float readFloat; - - int res = sscanf(iter->second.data(), "%f", &readFloat); - - const char EXPECTED_ITEMS = 1; - - if (res == EXPECTED_ITEMS) { - if (!isnan(readFloat)) { - value = readFloat; - } else { - value = defaultSetting; - return false; - } - } else { - value = defaultSetting; - return false; - } - } else { - value = defaultSetting; - return false; - } - - return true; -} - -bool Application::getSetting(const char* setting, glm::vec3& value, const glm::vec3& defaultSetting) const { - std::map::const_iterator iter = _settingsTable.find(setting); - - if (iter != _settingsTable.end()) { - glm::vec3 readVec; - - int res = sscanf(iter->second.data(), "%f,%f,%f", &readVec.x, &readVec.y, &readVec.z); - - const char EXPECTED_ITEMS = 3; - - if (res == EXPECTED_ITEMS) { - if (!isnan(readVec.x) && !isnan(readVec.y) && !isnan(readVec.z)) { - value = readVec; - } else { - value = defaultSetting; - return false; - } - } else { - value = defaultSetting; - return false; - } - } else { - value = defaultSetting; - return false; - } - - return true; -} - -const short MAX_SETTINGS_LENGTH = 128; - -void Application::setSetting(const char* setting, const bool value) { - char settingValues[MAX_SETTINGS_LENGTH]; - sprintf(settingValues, "%d", value); - _settingsTable[setting] = settingValues; -} - -void Application::setSetting(const char* setting, const float value) { - char settingValues[MAX_SETTINGS_LENGTH]; - sprintf(settingValues, "%f", value); - _settingsTable[setting] = settingValues; -} - -void Application::setSetting(const char* setting, const glm::vec3& value) { - char settingValues[MAX_SETTINGS_LENGTH]; - sprintf(settingValues, "%f,%f,%f", value.x, value.y, value.z); - _settingsTable[setting] = settingValues; -} - // Every second, check the frame rates and other stuff void Application::timer() { gettimeofday(&_timerEnd, NULL); @@ -1083,6 +912,9 @@ void Application::idle() { for(AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { if (agent->getLinkedData() != NULL) { Avatar *avatar = (Avatar *)agent->getLinkedData(); + if (!avatar->isInitialized()) { + avatar->init(); + } avatar->simulate(deltaTime, NULL); avatar->setMouseRay(mouseRayOrigin, mouseRayDirection); } @@ -1140,7 +972,10 @@ void Application::terminate() { // Close serial port // close(serial_fd); - saveSettings(); + if (_autosave) { + saveSettings(); + _settings->sync(); + } if (_enableNetworkThread) { _stopNetworkReceiveThread = true; @@ -1285,7 +1120,7 @@ static void sendVoxelEditMessage(PACKET_HEADER header, VoxelDetail& detail) { void Application::addVoxelInFrontOfAvatar() { VoxelDetail detail; - glm::vec3 position = (_myAvatar.getPosition() + _myAvatar.getCameraDirection()) * (1.0f / TREE_SCALE); + glm::vec3 position = (_myAvatar.getPosition() + _myAvatar.calculateCameraDirection()) * (1.0f / TREE_SCALE); detail.s = _mouseVoxelScale; detail.x = detail.s * floor(position.x / detail.s); @@ -1611,6 +1446,14 @@ void Application::initMenu() { debugMenu->addAction("Wants Res-In", this, SLOT(setWantsResIn(bool)))->setCheckable(true); debugMenu->addAction("Wants Monochrome", this, SLOT(setWantsMonochrome(bool)))->setCheckable(true); debugMenu->addAction("Wants View Delta Sending", this, SLOT(setWantsDelta(bool)))->setCheckable(true); + + QMenu* settingsMenu = menuBar->addMenu("Settings"); + (_settingsAutosave = settingsMenu->addAction("Autosave", this, SLOT(setAutosave(bool))))->setCheckable(true); + _settingsAutosave->setChecked(true); + settingsMenu->addAction("Load settings", this, SLOT(loadSettings())); + settingsMenu->addAction("Save settings", this, SLOT(saveSettings())); + settingsMenu->addAction("Import settings", this, SLOT(importSettings())); + settingsMenu->addAction("Export settings", this, SLOT(exportSettings())); _networkAccessManager = new QNetworkAccessManager(this); _settings = new QSettings("High Fidelity", "Interface", this); @@ -1675,6 +1518,8 @@ void Application::init() { gettimeofday(&_timerStart, NULL); gettimeofday(&_lastTimeIdle, NULL); + + loadSettings(); } void Application::updateAvatar(float deltaTime) { @@ -1735,9 +1580,7 @@ void Application::updateAvatar(float deltaTime) { // to the server. loadViewFrustum(_myCamera, _viewFrustum); _myAvatar.setCameraPosition(_viewFrustum.getPosition()); - _myAvatar.setCameraDirection(_viewFrustum.getDirection()); - _myAvatar.setCameraUp(_viewFrustum.getUp()); - _myAvatar.setCameraRight(_viewFrustum.getRight()); + _myAvatar.setCameraOrientation(_viewFrustum.getOrientation()); _myAvatar.setCameraFov(_viewFrustum.getFieldOfView()); _myAvatar.setCameraAspectRatio(_viewFrustum.getAspectRatio()); _myAvatar.setCameraNearClip(_viewFrustum.getNearClip()); @@ -1801,13 +1644,10 @@ void Application::loadViewFrustum(Camera& camera, ViewFrustum& viewFrustum) { float farClip = camera.getFarClip(); glm::quat rotation = camera.getRotation(); - glm::vec3 direction = rotation * AVATAR_FRONT; - glm::vec3 up = rotation * AVATAR_UP; - glm::vec3 right = rotation * AVATAR_RIGHT; // Set the viewFrustum up with the correct position and orientation of the camera viewFrustum.setPosition(position); - viewFrustum.setOrientation(direction,up,right); + viewFrustum.setOrientation(rotation); // Also make sure it's got the correct lens details from the camera viewFrustum.setFieldOfView(fov); @@ -2075,6 +1915,9 @@ void Application::displaySide(Camera& whichCamera) { for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { if (agent->getLinkedData() != NULL && agent->getType() == AGENT_TYPE_AVATAR) { Avatar *avatar = (Avatar *)agent->getLinkedData(); + if (!avatar->isInitialized()) { + avatar->init(); + } avatar->render(false); } } @@ -2548,9 +2391,7 @@ QAction* Application::checkedVoxelModeAction() const { void Application::attachNewHeadToAgent(Agent* newAgent) { if (newAgent->getLinkedData() == NULL) { - Avatar* newAvatar = new Avatar(newAgent); - newAvatar->init(); - newAgent->setLinkedData(newAvatar); + newAgent->setLinkedData(new Avatar(newAgent)); } } @@ -2609,95 +2450,82 @@ void* Application::networkReceive(void* args) { return NULL; } -void Application::saveSettings() -{ - // Handle any persistent settings saving here when we get a call to terminate. - // This should probably be moved to a map stored in memory at some point to cache settings. - _myAvatar.writeAvatarDataToFile(); - - setSetting("_gyroLook", _gyroLook->isChecked()); - - setSetting("_mouseLook", _mouseLook->isChecked()); - - setSetting("_transmitterDrives", _transmitterDrives->isChecked()); - - setSetting("_renderVoxels", _renderVoxels->isChecked()); - - setSetting("_renderVoxelTextures", _renderVoxelTextures->isChecked()); - - setSetting("_renderStarsOn", _renderStarsOn->isChecked()); - - setSetting("_renderAtmosphereOn", _renderAtmosphereOn->isChecked()); - - setSetting("_renderAvatarsOn", _renderAvatarsOn->isChecked()); - - setSetting("_renderStatsOn", _renderStatsOn->isChecked()); - - setSetting("_renderFrameTimerOn", _renderFrameTimerOn->isChecked()); - - setSetting("_renderLookatOn", _renderLookatOn->isChecked()); - - setSetting("_logOn", _logOn->isChecked()); - - setSetting("_frustumOn", _frustumOn->isChecked()); - - setSetting("_viewFrustumFromOffset", _viewFrustumFromOffset->isChecked()); - - setSetting("_cameraFrustum", _cameraFrustum->isChecked()); - - saveSettingsFile(); +void Application::scanMenuBar(settingsAction modifySetting, QSettings* set) { + if (!_window->menuBar()) { + return; + } + + QList menus = _window->menuBar()->findChildren(); + + for (QList::const_iterator it = menus.begin(); menus.end() != it; ++it) { + scanMenu(*it, modifySetting, set); + } } -void Application::readSettings() -{ - readSettingsFile(); - _myAvatar.readAvatarDataFromFile(); - - bool settingState; - getSetting("_gyroLook", settingState, _gyroLook->isChecked()); - _gyroLook->setChecked(settingState); - - getSetting("_mouseLook", settingState, _mouseLook->isChecked()); - _mouseLook->setChecked(settingState); - - getSetting("_transmitterDrives", settingState, _transmitterDrives->isChecked()); - _transmitterDrives->setChecked(settingState); - - getSetting("_renderVoxels", settingState, _renderVoxels->isChecked()); - _renderVoxels->setChecked(settingState); - - getSetting("_renderVoxelTextures", settingState, _renderVoxelTextures->isChecked()); - _renderVoxelTextures->setChecked(settingState); - - getSetting("_renderStarsOn", settingState, _renderStarsOn->isChecked()); - _renderStarsOn->setChecked(settingState); - - getSetting("_renderAtmosphereOn", settingState, _renderAtmosphereOn->isChecked()); - _renderAtmosphereOn->setChecked(settingState); - - getSetting("_renderAvatarsOn", settingState, _renderAvatarsOn->isChecked()); - _renderAvatarsOn->setChecked(settingState); - - getSetting("_renderStatsOn", settingState, _renderStatsOn->isChecked()); - _renderStatsOn->setChecked(settingState); - - getSetting("_renderFrameTimerOn", settingState, _renderFrameTimerOn->isChecked()); - _renderFrameTimerOn->setChecked(settingState); - - getSetting("_renderLookatOn", settingState, _renderLookatOn->isChecked()); - _renderLookatOn->setChecked(settingState); - - getSetting("_logOn", settingState, _logOn->isChecked()); - _logOn->setChecked(settingState); - - getSetting("_frustumOn", settingState, _frustumOn->isChecked()); - _frustumOn->setChecked(settingState); - - getSetting("_viewFrustumFromOffset", settingState, _viewFrustumFromOffset->isChecked()); - _viewFrustumFromOffset->setChecked(settingState); - - getSetting("_cameraFrustum", settingState, _cameraFrustum->isChecked()); - _cameraFrustum->setChecked(settingState); - +void Application::scanMenu(QMenu* menu, settingsAction modifySetting, QSettings* set) { + QList actions = menu->actions(); + + set->beginGroup(menu->title()); + for (QList::const_iterator it = actions.begin(); actions.end() != it; ++it) { + if ((*it)->menu()) { + scanMenu((*it)->menu(), modifySetting, set); + } + if ((*it)->isCheckable()) { + modifySetting(set, *it); + } + } + set->endGroup(); } +void Application::loadAction(QSettings* set, QAction* action) { + action->setChecked(set->value(action->text(), action->isChecked()).toBool()); +} + +void Application::saveAction(QSettings* set, QAction* action) { + set->setValue(action->text(), action->isChecked()); +} + +void Application::setAutosave(bool wantsAutosave) { + _autosave = wantsAutosave; +} + +void Application::loadSettings(QSettings* set) { + if (!set) set = getSettings(); + + scanMenuBar(&Application::loadAction, set); + getAvatar()->loadData(set); +} + +void Application::saveSettings(QSettings* set) { + if (!set) set = getSettings(); + + scanMenuBar(&Application::saveAction, set); + getAvatar()->saveData(set); +} + +void Application::importSettings() { + QString locationDir(QDesktopServices::displayName(QDesktopServices::DesktopLocation)); + QString fileName = QFileDialog::getOpenFileName(_window, + tr("Open .ini config file"), + locationDir, + tr("Text files (*.ini)")); + if (fileName != "") { + QSettings tmp(fileName, QSettings::IniFormat); + loadSettings(&tmp); + } +} + +void Application::exportSettings() { + QString locationDir(QDesktopServices::displayName(QDesktopServices::DesktopLocation)); + QString fileName = QFileDialog::getSaveFileName(_window, + tr("Save .ini config file"), + locationDir, + tr("Text files (*.ini)")); + if (fileName != "") { + QSettings tmp(fileName, QSettings::IniFormat); + saveSettings(&tmp); + tmp.sync(); + } +} + + diff --git a/interface/src/Application.h b/interface/src/Application.h index 6983f6d08a..9d37925c05 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -15,6 +15,8 @@ #include #include +#include +#include #include @@ -69,62 +71,12 @@ public: Camera* getCamera() { return &_myCamera; } ViewFrustum* getViewFrustum() { return &_viewFrustum; } VoxelSystem* getVoxels() { return &_voxels; } + QSettings* getSettings() { return _settings; } Environment* getEnvironment() { return &_environment; } bool shouldEchoAudio() { return _echoAudioMode->isChecked(); } QNetworkAccessManager* getNetworkAccessManager() { return _networkAccessManager; } - /*! - @fn getSettingBool - @brief A function for getting boolean settings from the settings file. - @param settingName The desired setting to get the value for. - @param boolSetting The referenced variable where the setting will be stored. - @param defaultSetting The default setting to assign to boolSetting if this function fails to find the appropriate setting. Defaults to false. - */ - bool getSetting(const char* setting, bool &value, const bool defaultSetting = false) const; - - /*! - @fn getSettingFloat - @brief A function for getting float settings from the settings file. - @param settingName The desired setting to get the value for. - @param floatSetting The referenced variable where the setting will be stored. - @param defaultSetting The default setting to assign to boolSetting if this function fails to find the appropriate setting. Defaults to 0.0f. - */ - bool getSetting(const char* setting, float &value, const float defaultSetting = 0.0f) const; - - /*! - @fn getSettingVec3 - @brief A function for getting boolean settings from the settings file. - @param settingName The desired setting to get the value for. - @param vecSetting The referenced variable where the setting will be stored. - @param defaultSetting The default setting to assign to boolSetting if this function fails to find the appropriate setting. Defaults to <0.0f, 0.0f, 0.0f> - */ - bool getSetting(const char* setting, glm::vec3 &value, const glm::vec3& defaultSetting = glm::vec3(0.0f, 0.0f, 0.0f)) const; - - /*! - @fn setSettingBool - @brief A function for setting boolean setting values when saving the settings file. - @param settingName The desired setting to populate a value for. - @param boolSetting The value to set. - */ - void setSetting(const char* setting, const bool value); - - /*! - @fn setSettingFloat - @brief A function for setting boolean setting values when saving the settings file. - @param settingName The desired setting to populate a value for. - @param floatSetting The value to set. - */ - void setSetting(const char* setting, const float value); - - /*! - @fn setSettingVec3 - @brief A function for setting boolean setting values when saving the settings file. - @param settingName The desired setting to populate a value for. - @param vecSetting The value to set. - */ - void setSetting(const char* setting, const glm::vec3& value); - private slots: void timer(); @@ -161,12 +113,16 @@ private slots: void decreaseVoxelSize(); void increaseVoxelSize(); void chooseVoxelPaintColor(); + void setAutosave(bool wantsAutosave); + void loadSettings(QSettings* set = NULL); + void saveSettings(QSettings* set = NULL); + void importSettings(); + void exportSettings(); void exportVoxels(); void importVoxels(); void cutVoxels(); void copyVoxels(); void pasteVoxels(); - private: static bool sendVoxelsOperation(VoxelNode* node, void* extraData); @@ -203,13 +159,13 @@ private: static void attachNewHeadToAgent(Agent *newAgent); static void* networkReceive(void* args); - // These two functions are technically not necessary, but they help keep things in one place. - void readSettings(); //! This function is largely to help consolidate getting settings in one place. - void saveSettings(); //! This function is to consolidate any settings setting in one place. - - void readSettingsFile(); //! This function reads data from the settings file, splitting data into key value pairs using '=' as a delimiter. - void saveSettingsFile(); //! This function writes all changes in the settings table to the settings file, serializing all settings added through the setSetting functions. - + // methodes handling menu settings + typedef void(*settingsAction)(QSettings*, QAction*); + static void loadAction(QSettings* set, QAction* action); + static void saveAction(QSettings* set, QAction* action); + void scanMenuBar(settingsAction modifySetting, QSettings* set); + void scanMenu(QMenu* menu, settingsAction modifySetting, QSettings* set); + QMainWindow* _window; QGLWidget* _glWidget; @@ -241,6 +197,7 @@ private: QAction* _cameraFrustum; // which frustum to look at QAction* _fullScreenMode; // whether we are in full screen mode QAction* _frustumRenderModeAction; + QAction* _settingsAutosave; // Whether settings are saved automatically SerialInterface _serialHeadSensor; QNetworkAccessManager* _networkAccessManager; @@ -335,11 +292,8 @@ private: int _bytesPerSecond; int _bytesCount; - /*! - * Store settings in a map, storing keys and values as strings. - * Interpret values as needed on demand. through the appropriate getters and setters. - */ - std::map _settingsTable; + QSettings* _settings; // Contain Menu settings and Avatar data + bool _autosave; // True if the autosave is on. }; #endif /* defined(__interface__Application__) */ diff --git a/interface/src/Avatar.cpp b/interface/src/Avatar.cpp index 4a3e8db2bc..49b83dd670 100644 --- a/interface/src/Avatar.cpp +++ b/interface/src/Avatar.cpp @@ -62,6 +62,7 @@ float chatMessageHeight = 0.20; Avatar::Avatar(Agent* owningAgent) : AvatarData(owningAgent), + _initialized(false), _head(this), _ballSpringsInitialized(false), _TEST_bigSphereRadius(0.5f), @@ -266,6 +267,7 @@ Avatar::~Avatar() { void Avatar::init() { _voxels.init(); + _initialized = true; } void Avatar::reset() { @@ -427,9 +429,9 @@ void Avatar::simulate(float deltaTime, Transmitter* transmitter) { } glm::quat orientation = getOrientation(); - glm::vec3 front = orientation * AVATAR_FRONT; - glm::vec3 right = orientation * AVATAR_RIGHT; - glm::vec3 up = orientation * AVATAR_UP; + glm::vec3 front = orientation * IDENTITY_FRONT; + glm::vec3 right = orientation * IDENTITY_RIGHT; + glm::vec3 up = orientation * IDENTITY_UP; // driving the avatar around should only apply if this is my avatar (as opposed to an avatar being driven remotely) const float THRUST_MAG = 600.0f; @@ -649,9 +651,9 @@ void Avatar::updateHandMovementAndTouching(float deltaTime) { glm::quat orientation = getOrientation(); // reset hand and arm positions according to hand movement - glm::vec3 right = orientation * AVATAR_RIGHT; - glm::vec3 up = orientation * AVATAR_UP; - glm::vec3 front = orientation * AVATAR_FRONT; + glm::vec3 right = orientation * IDENTITY_RIGHT; + glm::vec3 up = orientation * IDENTITY_UP; + glm::vec3 front = orientation * IDENTITY_FRONT; glm::vec3 transformedHandMovement = right * _movedHandOffset.x * 2.0f @@ -1123,14 +1125,14 @@ void Avatar::updateArmIKAndConstraints(float deltaTime) { glm::quat Avatar::computeRotationFromBodyToWorldUp(float proportion) const { glm::quat orientation = getOrientation(); - glm::vec3 currentUp = orientation * AVATAR_UP; + glm::vec3 currentUp = orientation * IDENTITY_UP; float angle = glm::degrees(acosf(glm::clamp(glm::dot(currentUp, _worldUpDirection), -1.0f, 1.0f))); if (angle < EPSILON) { return glm::quat(); } glm::vec3 axis; if (angle > 179.99f) { // 180 degree rotation; must use another axis - axis = orientation * AVATAR_RIGHT; + axis = orientation * IDENTITY_RIGHT; } else { axis = glm::normalize(glm::cross(currentUp, _worldUpDirection)); } @@ -1250,33 +1252,43 @@ void Avatar::setHeadFromGyros(glm::vec3* eulerAngles, glm::vec3* angularVelocity } } +void Avatar::loadData(QSettings* set) { + set->beginGroup("Avatar"); + + _bodyYaw = set->value("bodyYaw", _bodyYaw).toFloat(); + _bodyPitch = set->value("bodyPitch", _bodyPitch).toFloat(); + _bodyRoll = set->value("bodyRoll", _bodyRoll).toFloat(); + + _position.x = set->value("position_x", _position.x).toFloat(); + _position.y = set->value("position_y", _position.y).toFloat(); + _position.z = set->value("position_z", _position.z).toFloat(); + + set->endGroup(); +} + void Avatar::getBodyBallTransform(AvatarJointID jointID, glm::vec3& position, glm::quat& rotation) const { position = _bodyBall[jointID].position; rotation = _bodyBall[jointID].rotation; } -void Avatar::writeAvatarDataToFile() { - Application::getInstance()->setSetting("avatarPos", _position); - Application::getInstance()->setSetting("avatarRotation", glm::vec3(_bodyYaw, _bodyPitch, _bodyRoll)); -} +void Avatar::saveData(QSettings* set) { + set->beginGroup("Avatar"); -void Avatar::readAvatarDataFromFile() { - glm::vec3 readPosition; - glm::vec3 readRotation; - - Application::getInstance()->getSetting("avatarPos", readPosition, glm::vec3(6.1f, 0, 1.4f)); - Application::getInstance()->getSetting("avatarRotation", readRotation, glm::vec3(0, 0, 0)); - - _bodyYaw = readRotation.x; - _bodyPitch = readRotation.y; - _bodyRoll = readRotation.z; - _position = readPosition; + set->setValue("bodyYaw", _bodyYaw); + set->setValue("bodyPitch", _bodyPitch); + set->setValue("bodyRoll", _bodyRoll); + + set->setValue("position_x", _position.x); + set->setValue("position_y", _position.y); + set->setValue("position_z", _position.z); + + set->endGroup(); } // render a makeshift cone section that serves as a body part connecting joint spheres void Avatar::renderJointConnectingCone(glm::vec3 position1, glm::vec3 position2, float radius1, float radius2) { - glBegin(GL_TRIANGLES); + glBegin(GL_TRIANGLES); glm::vec3 axis = position2 - position1; float length = glm::length(axis); diff --git a/interface/src/Avatar.h b/interface/src/Avatar.h index 20c431ef82..73ab3ddaba 100644 --- a/interface/src/Avatar.h +++ b/interface/src/Avatar.h @@ -11,6 +11,7 @@ #include #include #include +#include #include "world.h" #include "AvatarTouch.h" #include "AvatarVoxelSystem.h" @@ -100,15 +101,16 @@ public: void setOrientation (const glm::quat& orientation); //getters + bool isInitialized () const { return _initialized;} const Skeleton& getSkeleton () const { return _skeleton;} float getHeadYawRate () const { return _head.yawRate;} float getBodyYaw () const { return _bodyYaw;} bool getIsNearInteractingOther () const { return _avatarTouch.getAbleToReachOtherAvatar();} const glm::vec3& getHeadJointPosition () const { return _skeleton.joint[ AVATAR_JOINT_HEAD_BASE ].position;} const glm::vec3& getBallPosition (AvatarJointID j) const { return _bodyBall[j].position;} - glm::vec3 getBodyRightDirection () const { return getOrientation() * AVATAR_RIGHT; } - glm::vec3 getBodyUpDirection () const { return getOrientation() * AVATAR_UP; } - glm::vec3 getBodyFrontDirection () const { return getOrientation() * AVATAR_FRONT; } + glm::vec3 getBodyRightDirection () const { return getOrientation() * IDENTITY_RIGHT; } + glm::vec3 getBodyUpDirection () const { return getOrientation() * IDENTITY_UP; } + glm::vec3 getBodyFrontDirection () const { return getOrientation() * IDENTITY_FRONT; } const glm::vec3& getVelocity () const { return _velocity;} float getSpeed () const { return _speed;} float getHeight () const { return _height;} @@ -129,6 +131,10 @@ public: void addThrust(glm::vec3 newThrust) { _thrust += newThrust; }; glm::vec3 getThrust() { return _thrust; }; + // get/set avatar data + void saveData(QSettings* set); + void loadData(QSettings* set); + // Get the position/rotation of a single body ball void getBodyBallTransform(AvatarJointID jointID, glm::vec3& position, glm::quat& rotation) const; @@ -156,6 +162,7 @@ private: float touchForce; // a scalar determining the amount that the cursor (or hand) is penetrating the ball }; + bool _initialized; Head _head; Skeleton _skeleton; bool _ballSpringsInitialized; diff --git a/interface/src/AvatarTouch.h b/interface/src/AvatarTouch.h index da550432eb..84fd1bced4 100644 --- a/interface/src/AvatarTouch.h +++ b/interface/src/AvatarTouch.h @@ -15,7 +15,7 @@ enum AvatarHandState { - HAND_STATE_NULL = -1, + HAND_STATE_NULL = 0, HAND_STATE_OPEN, HAND_STATE_GRASPING, HAND_STATE_POINTING, diff --git a/interface/src/AvatarVoxelSystem.cpp b/interface/src/AvatarVoxelSystem.cpp index 2500c9b2d4..d1d463561b 100644 --- a/interface/src/AvatarVoxelSystem.cpp +++ b/interface/src/AvatarVoxelSystem.cpp @@ -88,7 +88,7 @@ void AvatarVoxelSystem::loadVoxelsFromURL(const QUrl& url) { return; } _voxelReply = Application::getInstance()->getNetworkAccessManager()->get(QNetworkRequest(url)); - connect(_voxelReply, SIGNAL(readyRead()), SLOT(readVoxelDataFromReply())); + connect(_voxelReply, SIGNAL(downloadProgress(qint64,qint64)), SLOT(handleVoxelDownloadProgress(qint64,qint64))); connect(_voxelReply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(handleVoxelReplyError())); } @@ -181,9 +181,9 @@ void AvatarVoxelSystem::removeScaleAndReleaseProgram(bool texture) { _skinProgram->disableAttributeArray(_boneWeightsLocation); } -void AvatarVoxelSystem::readVoxelDataFromReply() { +void AvatarVoxelSystem::handleVoxelDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) { // for now, just wait until we have the full business - if (!_voxelReply->isFinished()) { + if (bytesReceived < bytesTotal) { return; } QByteArray entirety = _voxelReply->readAll(); diff --git a/interface/src/AvatarVoxelSystem.h b/interface/src/AvatarVoxelSystem.h index edeeadb17c..3894fd75b9 100644 --- a/interface/src/AvatarVoxelSystem.h +++ b/interface/src/AvatarVoxelSystem.h @@ -46,7 +46,7 @@ protected: private slots: - void readVoxelDataFromReply(); + void handleVoxelDownloadProgress(qint64 bytesReceived, qint64 bytesTotal); void handleVoxelReplyError(); private: diff --git a/interface/src/Head.cpp b/interface/src/Head.cpp index bbf773a8a9..98e5c57cb1 100644 --- a/interface/src/Head.cpp +++ b/interface/src/Head.cpp @@ -171,9 +171,9 @@ void Head::determineIfLookingAtSomething() { void Head::calculateGeometry() { //generate orientation directions glm::quat orientation = getOrientation(); - glm::vec3 right = orientation * AVATAR_RIGHT; - glm::vec3 up = orientation * AVATAR_UP; - glm::vec3 front = orientation * AVATAR_FRONT; + glm::vec3 right = orientation * IDENTITY_RIGHT; + glm::vec3 up = orientation * IDENTITY_UP; + glm::vec3 front = orientation * IDENTITY_FRONT; //calculate the eye positions _leftEyePosition = _position @@ -346,9 +346,9 @@ void Head::renderMouth() { float s = sqrt(_averageLoudness); glm::quat orientation = getOrientation(); - glm::vec3 right = orientation * AVATAR_RIGHT; - glm::vec3 up = orientation * AVATAR_UP; - glm::vec3 front = orientation * AVATAR_FRONT; + glm::vec3 right = orientation * IDENTITY_RIGHT; + glm::vec3 up = orientation * IDENTITY_UP; + glm::vec3 front = orientation * IDENTITY_FRONT; glm::vec3 r = right * _scale * (0.30f + s * 0.0014f ); glm::vec3 u = up * _scale * (0.05f + s * 0.0040f ); @@ -414,9 +414,9 @@ void Head::renderEyeBrows() { glm::vec3 rightBottom = _leftEyePosition; glm::quat orientation = getOrientation(); - glm::vec3 right = orientation * AVATAR_RIGHT; - glm::vec3 up = orientation * AVATAR_UP; - glm::vec3 front = orientation * AVATAR_FRONT; + glm::vec3 right = orientation * IDENTITY_RIGHT; + glm::vec3 up = orientation * IDENTITY_UP; + glm::vec3 front = orientation * IDENTITY_FRONT; glm::vec3 r = right * length; glm::vec3 u = up * height; @@ -501,20 +501,20 @@ void Head::renderEyeBalls() { //rotate the eyeball to aim towards the lookat position glm::vec3 targetLookatAxis = glm::normalize(_lookAtPosition - _leftEyePosition); // the lookat direction - glm::vec3 rotationAxis = glm::cross(targetLookatAxis, AVATAR_UP); - float angle = 180.0f - angleBetween(targetLookatAxis, AVATAR_UP); + glm::vec3 rotationAxis = glm::cross(targetLookatAxis, IDENTITY_UP); + float angle = 180.0f - angleBetween(targetLookatAxis, IDENTITY_UP); glRotatef(angle, rotationAxis.x, rotationAxis.y, rotationAxis.z); glRotatef(180.0, 0.0f, 1.0f, 0.0f); //adjust roll to correct after previous rotations } else { //rotate the eyeball to aim straight ahead - glm::vec3 rotationAxisToHeadFront = glm::cross(front, AVATAR_UP); - float angleToHeadFront = 180.0f - angleBetween(front, AVATAR_UP); + glm::vec3 rotationAxisToHeadFront = glm::cross(front, IDENTITY_UP); + float angleToHeadFront = 180.0f - angleBetween(front, IDENTITY_UP); glRotatef(angleToHeadFront, rotationAxisToHeadFront.x, rotationAxisToHeadFront.y, rotationAxisToHeadFront.z); //set the amount of roll (for correction after previous rotations) - float rollRotation = angleBetween(front, AVATAR_FRONT); - float dot = glm::dot(front, -AVATAR_RIGHT); + float rollRotation = angleBetween(front, IDENTITY_FRONT); + float dot = glm::dot(front, -IDENTITY_RIGHT); if ( dot < 0.0f ) { rollRotation = -rollRotation; } glRotatef(rollRotation, 0.0f, 1.0f, 0.0f); //roll the iris or correct roll about the lookat vector } @@ -545,21 +545,21 @@ void Head::renderEyeBalls() { //rotate the eyeball to aim towards the lookat position glm::vec3 targetLookatAxis = glm::normalize(_lookAtPosition - _rightEyePosition); - glm::vec3 rotationAxis = glm::cross(targetLookatAxis, AVATAR_UP); - float angle = 180.0f - angleBetween(targetLookatAxis, AVATAR_UP); + glm::vec3 rotationAxis = glm::cross(targetLookatAxis, IDENTITY_UP); + float angle = 180.0f - angleBetween(targetLookatAxis, IDENTITY_UP); glRotatef(angle, rotationAxis.x, rotationAxis.y, rotationAxis.z); glRotatef(180.0f, 0.0f, 1.0f, 0.0f); //adjust roll to correct after previous rotations } else { //rotate the eyeball to aim straight ahead - glm::vec3 rotationAxisToHeadFront = glm::cross(front, AVATAR_UP); - float angleToHeadFront = 180.0f - angleBetween(front, AVATAR_UP); + glm::vec3 rotationAxisToHeadFront = glm::cross(front, IDENTITY_UP); + float angleToHeadFront = 180.0f - angleBetween(front, IDENTITY_UP); glRotatef(angleToHeadFront, rotationAxisToHeadFront.x, rotationAxisToHeadFront.y, rotationAxisToHeadFront.z); //set the amount of roll (for correction after previous rotations) - float rollRotation = angleBetween(front, AVATAR_FRONT); - float dot = glm::dot(front, -AVATAR_RIGHT); + float rollRotation = angleBetween(front, IDENTITY_FRONT); + float dot = glm::dot(front, -IDENTITY_RIGHT); if ( dot < 0.0f ) { rollRotation = -rollRotation; } glRotatef(rollRotation, 0.0f, 1.0f, 0.0f); //roll the iris or correct roll about the lookat vector } @@ -595,9 +595,8 @@ void Head::renderLookatVectors(glm::vec3 leftEyePosition, glm::vec3 rightEyePosi void Head::updateHairPhysics(float deltaTime) { glm::quat orientation = getOrientation(); - glm::vec3 right = orientation * AVATAR_RIGHT; - glm::vec3 up = orientation * AVATAR_UP; - glm::vec3 front = orientation * AVATAR_FRONT; + glm::vec3 up = orientation * IDENTITY_UP; + glm::vec3 front = orientation * IDENTITY_FRONT; for (int t = 0; t < NUM_HAIR_TUFTS; t ++) { diff --git a/interface/src/Head.h b/interface/src/Head.h index f49e127caf..66ce07d133 100644 --- a/interface/src/Head.h +++ b/interface/src/Head.h @@ -49,9 +49,9 @@ public: glm::quat getOrientation() const; glm::quat getWorldAlignedOrientation () const; - glm::vec3 getRightDirection() const { return getOrientation() * AVATAR_RIGHT; } - glm::vec3 getUpDirection () const { return getOrientation() * AVATAR_UP; } - glm::vec3 getFrontDirection() const { return getOrientation() * AVATAR_FRONT; } + glm::vec3 getRightDirection() const { return getOrientation() * IDENTITY_RIGHT; } + glm::vec3 getUpDirection () const { return getOrientation() * IDENTITY_UP; } + glm::vec3 getFrontDirection() const { return getOrientation() * IDENTITY_FRONT; } const bool getReturnToCenter() const { return _returnHeadToCenter; } // Do you want head to try to return to center (depends on interface detected) float getAverageLoudness() {return _averageLoudness;}; diff --git a/interface/src/Util.cpp b/interface/src/Util.cpp index 08ec6cf011..d16e6d36bb 100644 --- a/interface/src/Util.cpp +++ b/interface/src/Util.cpp @@ -21,6 +21,8 @@ #include "world.h" #include "Util.h" +#include "VoxelConstants.h" + using namespace std; // no clue which versions are affected... @@ -415,9 +417,9 @@ void renderCircle(glm::vec3 position, float radius, glm::vec3 surfaceNormal, int void renderOrientationDirections(glm::vec3 position, const glm::quat& orientation, float size) { - glm::vec3 pRight = position + orientation * AVATAR_RIGHT * size; - glm::vec3 pUp = position + orientation * AVATAR_UP * size; - glm::vec3 pFront = position + orientation * AVATAR_FRONT * size; + glm::vec3 pRight = position + orientation * IDENTITY_RIGHT * size; + glm::vec3 pUp = position + orientation * IDENTITY_UP * size; + glm::vec3 pFront = position + orientation * IDENTITY_FRONT * size; glColor3f(1.0f, 0.0f, 0.0f); glBegin(GL_LINE_STRIP); diff --git a/libraries/avatars/CMakeLists.txt b/libraries/avatars/CMakeLists.txt index b1c593a4a6..207057e244 100644 --- a/libraries/avatars/CMakeLists.txt +++ b/libraries/avatars/CMakeLists.txt @@ -15,4 +15,7 @@ include(${MACRO_DIR}/IncludeGLM.cmake) include_glm(${TARGET_NAME} ${ROOT_DIR}) include(${MACRO_DIR}/LinkHifiLibrary.cmake) -link_hifi_library(shared ${TARGET_NAME} ${ROOT_DIR}) \ No newline at end of file +link_hifi_library(shared ${TARGET_NAME} ${ROOT_DIR}) + +# link in the hifi voxels library +link_hifi_library(voxels ${TARGET_NAME} ${ROOT_DIR}) diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index 99e3b61656..d24754b28f 100644 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -14,23 +14,10 @@ #include #include "AvatarData.h" +#include using namespace std; -int packFloatAngleToTwoByte(unsigned char* buffer, float angle) { - const float ANGLE_CONVERSION_RATIO = (std::numeric_limits::max() / 360.0); - - uint16_t angleHolder = floorf((angle + 180) * ANGLE_CONVERSION_RATIO); - memcpy(buffer, &angleHolder, sizeof(uint16_t)); - - return sizeof(uint16_t); -} - -int unpackFloatAngleFromTwoByte(uint16_t* byteAnglePointer, float* destinationPointer) { - *destinationPointer = (*byteAnglePointer / (float) std::numeric_limits::max()) * 360.0 - 180; - return sizeof(uint16_t); -} - AvatarData::AvatarData(Agent* owningAgent) : AgentData(owningAgent), _handPosition(0,0,0), @@ -39,9 +26,7 @@ AvatarData::AvatarData(Agent* owningAgent) : _bodyRoll(0.0), _handState(0), _cameraPosition(0,0,0), - _cameraDirection(0,0,0), - _cameraUp(0,0,0), - _cameraRight(0,0,0), + _cameraOrientation(), _cameraFov(0.0f), _cameraAspectRatio(0.0f), _cameraNearClip(0.0f), @@ -91,55 +76,45 @@ int AvatarData::getBroadcastData(unsigned char* destinationBuffer) { memcpy(destinationBuffer, &_headData->_leanForward, sizeof(_headData->_leanForward)); destinationBuffer += sizeof(_headData->_leanForward); - // Hand Position - memcpy(destinationBuffer, &_handPosition, sizeof(float) * 3); + // 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); - // Hand State (0 = not grabbing, 1 = grabbing) - memcpy(destinationBuffer, &_handState, sizeof(char)); - destinationBuffer += sizeof(char); - // Instantaneous audio loudness (used to drive facial animation) + //destinationBuffer += packFloatToByte(destinationBuffer, std::min(MAX_AUDIO_LOUDNESS, _audioLoudness), MAX_AUDIO_LOUDNESS); memcpy(destinationBuffer, &_headData->_audioLoudness, sizeof(float)); destinationBuffer += sizeof(float); // camera details memcpy(destinationBuffer, &_cameraPosition, sizeof(_cameraPosition)); destinationBuffer += sizeof(_cameraPosition); - memcpy(destinationBuffer, &_cameraDirection, sizeof(_cameraDirection)); - destinationBuffer += sizeof(_cameraDirection); - memcpy(destinationBuffer, &_cameraRight, sizeof(_cameraRight)); - destinationBuffer += sizeof(_cameraRight); - memcpy(destinationBuffer, &_cameraUp, sizeof(_cameraUp)); - destinationBuffer += sizeof(_cameraUp); - memcpy(destinationBuffer, &_cameraFov, sizeof(_cameraFov)); - destinationBuffer += sizeof(_cameraFov); - memcpy(destinationBuffer, &_cameraAspectRatio, sizeof(_cameraAspectRatio)); - destinationBuffer += sizeof(_cameraAspectRatio); - memcpy(destinationBuffer, &_cameraNearClip, sizeof(_cameraNearClip)); - destinationBuffer += sizeof(_cameraNearClip); - memcpy(destinationBuffer, &_cameraFarClip, sizeof(_cameraFarClip)); - destinationBuffer += sizeof(_cameraFarClip); - - // key state - *destinationBuffer++ = _keyState; + destinationBuffer += packOrientationQuatToBytes(destinationBuffer, _cameraOrientation); + destinationBuffer += packFloatAngleToTwoByte(destinationBuffer, _cameraFov); + destinationBuffer += packFloatRatioToTwoByte(destinationBuffer, _cameraAspectRatio); + destinationBuffer += packClipValueToTwoByte(destinationBuffer, _cameraNearClip); + destinationBuffer += packClipValueToTwoByte(destinationBuffer, _cameraFarClip); // chat message *destinationBuffer++ = _chatMessage.size(); memcpy(destinationBuffer, _chatMessage.data(), _chatMessage.size() * sizeof(char)); destinationBuffer += _chatMessage.size() * sizeof(char); - // voxel sending features... - // voxel sending features... - unsigned char wantItems = 0; - if (_wantResIn) { setAtBit(wantItems,WANT_RESIN_AT_BIT); } - if (_wantColor) { setAtBit(wantItems,WANT_COLOR_AT_BIT); } - if (_wantDelta) { setAtBit(wantItems,WANT_DELTA_AT_BIT); } - *destinationBuffer++ = wantItems; + // bitMask of less than byte wide items + unsigned char bitItems = 0; + if (_wantResIn) { setAtBit(bitItems,WANT_RESIN_AT_BIT); } + if (_wantColor) { setAtBit(bitItems,WANT_COLOR_AT_BIT); } + if (_wantDelta) { setAtBit(bitItems,WANT_DELTA_AT_BIT); } + + // key state + setSemiNibbleAt(bitItems,KEY_STATE_START_BIT,_keyState); + // hand state + setSemiNibbleAt(bitItems,HAND_STATE_START_BIT,_handState); + *destinationBuffer++ = bitItems; return destinationBuffer - bufferStart; } @@ -185,54 +160,170 @@ int AvatarData::parseData(unsigned char* sourceBuffer, int numBytes) { memcpy(&_headData->_leanForward, sourceBuffer, sizeof(_headData->_leanForward)); sourceBuffer += sizeof(_headData->_leanForward); - // Hand Position - memcpy(&_handPosition, sourceBuffer, sizeof(float) * 3); + // 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); - - // Hand State - memcpy(&_handState, sourceBuffer, sizeof(char)); - sourceBuffer += sizeof(char); - + // Instantaneous audio loudness (used to drive facial animation) + //sourceBuffer += unpackFloatFromByte(sourceBuffer, _audioLoudness, MAX_AUDIO_LOUDNESS); memcpy(&_headData->_audioLoudness, sourceBuffer, sizeof(float)); sourceBuffer += sizeof(float); // camera details memcpy(&_cameraPosition, sourceBuffer, sizeof(_cameraPosition)); sourceBuffer += sizeof(_cameraPosition); - memcpy(&_cameraDirection, sourceBuffer, sizeof(_cameraDirection)); - sourceBuffer += sizeof(_cameraDirection); - memcpy(&_cameraRight, sourceBuffer, sizeof(_cameraRight)); - sourceBuffer += sizeof(_cameraRight); - memcpy(&_cameraUp, sourceBuffer, sizeof(_cameraUp)); - sourceBuffer += sizeof(_cameraUp); - memcpy(&_cameraFov, sourceBuffer, sizeof(_cameraFov)); - sourceBuffer += sizeof(_cameraFov); - memcpy(&_cameraAspectRatio, sourceBuffer, sizeof(_cameraAspectRatio)); - sourceBuffer += sizeof(_cameraAspectRatio); - memcpy(&_cameraNearClip, sourceBuffer, sizeof(_cameraNearClip)); - sourceBuffer += sizeof(_cameraNearClip); - memcpy(&_cameraFarClip, sourceBuffer, sizeof(_cameraFarClip)); - sourceBuffer += sizeof(_cameraFarClip); - - // key state - _keyState = (KeyState)*sourceBuffer++; - + sourceBuffer += unpackOrientationQuatFromBytes(sourceBuffer, _cameraOrientation); + sourceBuffer += unpackFloatAngleFromTwoByte((uint16_t*) sourceBuffer, &_cameraFov); + sourceBuffer += unpackFloatRatioFromTwoByte(sourceBuffer,_cameraAspectRatio); + sourceBuffer += unpackClipValueFromTwoByte(sourceBuffer,_cameraNearClip); + sourceBuffer += unpackClipValueFromTwoByte(sourceBuffer,_cameraFarClip); + // the rest is a chat message int chatMessageSize = *sourceBuffer++; _chatMessage = string((char*)sourceBuffer, chatMessageSize); sourceBuffer += chatMessageSize * sizeof(char); // voxel sending features... - unsigned char wantItems = 0; - wantItems = (unsigned char)*sourceBuffer++; - _wantResIn = oneAtBit(wantItems,WANT_RESIN_AT_BIT); - _wantColor = oneAtBit(wantItems,WANT_COLOR_AT_BIT); - _wantDelta = oneAtBit(wantItems,WANT_DELTA_AT_BIT); + unsigned char bitItems = 0; + bitItems = (unsigned char)*sourceBuffer++; + _wantResIn = oneAtBit(bitItems,WANT_RESIN_AT_BIT); + _wantColor = oneAtBit(bitItems,WANT_COLOR_AT_BIT); + _wantDelta = oneAtBit(bitItems,WANT_DELTA_AT_BIT); + + // key state, stored as a semi-nibble in the bitItems + _keyState = (KeyState)getSemiNibbleAt(bitItems,KEY_STATE_START_BIT); + + // hand state, stored as a semi-nibble in the bitItems + _handState = getSemiNibbleAt(bitItems,HAND_STATE_START_BIT); return sourceBuffer - startPosition; } + +glm::vec3 AvatarData::calculateCameraDirection() const { + glm::vec3 direction = glm::vec3(_cameraOrientation * glm::vec4(IDENTITY_FRONT, 0.0f)); + return direction; +} + + +int packFloatAngleToTwoByte(unsigned char* buffer, float angle) { + const float ANGLE_CONVERSION_RATIO = (std::numeric_limits::max() / 360.0); + + uint16_t angleHolder = floorf((angle + 180) * ANGLE_CONVERSION_RATIO); + memcpy(buffer, &angleHolder, sizeof(uint16_t)); + + return sizeof(uint16_t); +} + +int unpackFloatAngleFromTwoByte(uint16_t* byteAnglePointer, float* destinationPointer) { + *destinationPointer = (*byteAnglePointer / (float) std::numeric_limits::max()) * 360.0 - 180; + return sizeof(uint16_t); +} + +int packOrientationQuatToBytes(unsigned char* buffer, const glm::quat& quatInput) { + const float QUAT_PART_CONVERSION_RATIO = (std::numeric_limits::max() / 2.0); + uint16_t quatParts[4]; + quatParts[0] = floorf((quatInput.x + 1.0) * QUAT_PART_CONVERSION_RATIO); + quatParts[1] = floorf((quatInput.y + 1.0) * QUAT_PART_CONVERSION_RATIO); + quatParts[2] = floorf((quatInput.z + 1.0) * QUAT_PART_CONVERSION_RATIO); + quatParts[3] = floorf((quatInput.w + 1.0) * QUAT_PART_CONVERSION_RATIO); + + memcpy(buffer, &quatParts, sizeof(quatParts)); + return sizeof(quatParts); +} + +int unpackOrientationQuatFromBytes(unsigned char* buffer, glm::quat& quatOutput) { + uint16_t quatParts[4]; + memcpy(&quatParts, buffer, sizeof(quatParts)); + + quatOutput.x = ((quatParts[0] / (float) std::numeric_limits::max()) * 2.0) - 1.0; + quatOutput.y = ((quatParts[1] / (float) std::numeric_limits::max()) * 2.0) - 1.0; + quatOutput.z = ((quatParts[2] / (float) std::numeric_limits::max()) * 2.0) - 1.0; + quatOutput.w = ((quatParts[3] / (float) std::numeric_limits::max()) * 2.0) - 1.0; + + return sizeof(quatParts); +} + +float SMALL_LIMIT = 10.0; +float LARGE_LIMIT = 1000.0; + +int packFloatRatioToTwoByte(unsigned char* buffer, float ratio) { + // if the ratio is less than 10, then encode it as a positive number scaled from 0 to int16::max() + int16_t ratioHolder; + + if (ratio < SMALL_LIMIT) { + const float SMALL_RATIO_CONVERSION_RATIO = (std::numeric_limits::max() / SMALL_LIMIT); + ratioHolder = floorf(ratio * SMALL_RATIO_CONVERSION_RATIO); + } else { + const float LARGE_RATIO_CONVERSION_RATIO = std::numeric_limits::min() / LARGE_LIMIT; + ratioHolder = floorf((std::min(ratio,LARGE_LIMIT) - SMALL_LIMIT) * LARGE_RATIO_CONVERSION_RATIO); + } + memcpy(buffer, &ratioHolder, sizeof(ratioHolder)); + return sizeof(ratioHolder); +} + +int unpackFloatRatioFromTwoByte(unsigned char* buffer, float& ratio) { + int16_t ratioHolder; + memcpy(&ratioHolder, buffer, sizeof(ratioHolder)); + + // If it's positive, than the original ratio was less than SMALL_LIMIT + if (ratioHolder > 0) { + ratio = (ratioHolder / (float) std::numeric_limits::max()) * SMALL_LIMIT; + } else { + // If it's negative, than the original ratio was between SMALL_LIMIT and LARGE_LIMIT + ratio = ((ratioHolder / (float) std::numeric_limits::min()) * LARGE_LIMIT) + SMALL_LIMIT; + } + return sizeof(ratioHolder); +} + +int packClipValueToTwoByte(unsigned char* buffer, float clipValue) { + // Clip values must be less than max signed 16bit integers + assert(clipValue < std::numeric_limits::max()); + int16_t holder; + + // if the clip is less than 10, then encode it as a positive number scaled from 0 to int16::max() + if (clipValue < SMALL_LIMIT) { + const float SMALL_RATIO_CONVERSION_RATIO = (std::numeric_limits::max() / SMALL_LIMIT); + holder = floorf(clipValue * SMALL_RATIO_CONVERSION_RATIO); + } else { + // otherwise we store it as a negative integer + holder = -1 * floorf(clipValue); + } + memcpy(buffer, &holder, sizeof(holder)); + return sizeof(holder); +} + +int unpackClipValueFromTwoByte(unsigned char* buffer, float& clipValue) { + int16_t holder; + memcpy(&holder, buffer, sizeof(holder)); + + // If it's positive, than the original clipValue was less than SMALL_LIMIT + if (holder > 0) { + clipValue = (holder / (float) std::numeric_limits::max()) * SMALL_LIMIT; + } else { + // If it's negative, than the original holder can be found as the opposite sign of holder + clipValue = -1.0f * holder; + } + return sizeof(holder); +} + +int packFloatToByte(unsigned char* buffer, float value, float scaleBy) { + unsigned char holder; + const float CONVERSION_RATIO = (255 / scaleBy); + holder = floorf(value * CONVERSION_RATIO); + memcpy(buffer, &holder, sizeof(holder)); + return sizeof(holder); +} + +int unpackFloatFromByte(unsigned char* buffer, float& value, float scaleBy) { + unsigned char holder; + memcpy(&holder, buffer, sizeof(holder)); + value = ((float)holder / (float) 255) * scaleBy; + return sizeof(holder); +} + diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index 49a31a7e6a..cf9845ab4c 100644 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -10,8 +10,10 @@ #define __hifi__AvatarData__ #include +#include #include +#include #include #include "HeadData.h" @@ -19,15 +21,15 @@ const int WANT_RESIN_AT_BIT = 0; const int WANT_COLOR_AT_BIT = 1; const int WANT_DELTA_AT_BIT = 2; +const int KEY_STATE_START_BIT = 3; // 4th and 5th bits +const int HAND_STATE_START_BIT = 5; // 6th and 7th bits + +const float MAX_AUDIO_LOUDNESS = 1000.0; // close enough for mouth animation -// this is where the coordinate system is represented -const glm::vec3 AVATAR_RIGHT = glm::vec3(1.0f, 0.0f, 0.0f); -const glm::vec3 AVATAR_UP = glm::vec3(0.0f, 1.0f, 0.0f); -const glm::vec3 AVATAR_FRONT = glm::vec3(0.0f, 0.0f, -1.0f); enum KeyState { - NO_KEY_DOWN, + NO_KEY_DOWN = 0, INSERT_KEY_DOWN, DELETE_KEY_DOWN }; @@ -59,23 +61,21 @@ public: // getters for camera details const glm::vec3& getCameraPosition() const { return _cameraPosition; }; - const glm::vec3& getCameraDirection() const { return _cameraDirection; } - const glm::vec3& getCameraUp() const { return _cameraUp; } - const glm::vec3& getCameraRight() const { return _cameraRight; } + const glm::quat& getCameraOrientation() const { return _cameraOrientation; } float getCameraFov() const { return _cameraFov; } float getCameraAspectRatio() const { return _cameraAspectRatio; } float getCameraNearClip() const { return _cameraNearClip; } float getCameraFarClip() const { return _cameraFarClip; } + glm::vec3 calculateCameraDirection() const; + // setters for camera details - void setCameraPosition(const glm::vec3& position) { _cameraPosition = position; }; - void setCameraDirection(const glm::vec3& direction) { _cameraDirection = direction; } - void setCameraUp(const glm::vec3& up) { _cameraUp = up; } - void setCameraRight(const glm::vec3& right) { _cameraRight = right; } - void setCameraFov(float fov) { _cameraFov = fov; } - void setCameraAspectRatio(float aspectRatio) { _cameraAspectRatio = aspectRatio; } - void setCameraNearClip(float nearClip) { _cameraNearClip = nearClip; } - void setCameraFarClip(float farClip) { _cameraFarClip = farClip; } + void setCameraPosition(const glm::vec3& position) { _cameraPosition = position; } + void setCameraOrientation(const glm::quat& orientation) { _cameraOrientation = orientation; } + void setCameraFov(float fov) { _cameraFov = fov; } + void setCameraAspectRatio(float aspectRatio) { _cameraAspectRatio = aspectRatio; } + void setCameraNearClip(float nearClip) { _cameraNearClip = nearClip; } + void setCameraFarClip(float farClip) { _cameraFarClip = farClip; } // key state void setKeyState(KeyState s) { _keyState = s; } @@ -109,11 +109,7 @@ protected: // camera details for the avatar glm::vec3 _cameraPosition; - - // can we describe this in less space? For example, a Quaternion? or Euler angles? - glm::vec3 _cameraDirection; - glm::vec3 _cameraUp; - glm::vec3 _cameraRight; + glm::quat _cameraOrientation; float _cameraFov; float _cameraAspectRatio; float _cameraNearClip; @@ -137,4 +133,31 @@ private: AvatarData& operator= (const AvatarData&); }; + +// These pack/unpack functions are designed to start specific known types in as efficient a manner +// as possible. Taking advantage of the known characteristics of the semantic types. + +// Angles are known to be between 0 and 360deg, this allows us to encode in 16bits with great accuracy +int packFloatAngleToTwoByte(unsigned char* buffer, float angle); +int unpackFloatAngleFromTwoByte(uint16_t* byteAnglePointer, float* destinationPointer); + +// Orientation Quats are known to have 4 normalized components be between -1.0 and 1.0 +// this allows us to encode each component in 16bits with great accuracy +int packOrientationQuatToBytes(unsigned char* buffer, const glm::quat& quatInput); +int unpackOrientationQuatFromBytes(unsigned char* buffer, glm::quat& quatOutput); + +// Ratios need the be highly accurate when less than 10, but not very accurate above 10, and they +// are never greater than 1000 to 1, this allows us to encode each component in 16bits +int packFloatRatioToTwoByte(unsigned char* buffer, float ratio); +int unpackFloatRatioFromTwoByte(unsigned char* buffer, float& ratio); + +// Near/Far Clip values need the be highly accurate when less than 10, but only integer accuracy above 10 and +// they are never greater than 16,000, this allows us to encode each component in 16bits +int packClipValueToTwoByte(unsigned char* buffer, float clipValue); +int unpackClipValueFromTwoByte(unsigned char* buffer, float& clipValue); + +// Positive floats that don't need to be very precise +int packFloatToByte(unsigned char* buffer, float value, float scaleBy); +int unpackFloatFromByte(unsigned char* buffer, float& value, float scaleBy); + #endif /* defined(__hifi__AvatarData__) */ diff --git a/libraries/shared/src/SharedUtil.cpp b/libraries/shared/src/SharedUtil.cpp index 5829bbe34a..08b84e2b7e 100644 --- a/libraries/shared/src/SharedUtil.cpp +++ b/libraries/shared/src/SharedUtil.cpp @@ -101,6 +101,15 @@ void setAtBit(unsigned char& byte, int bitIndex) { byte += (1 << (7 - bitIndex)); } +int getSemiNibbleAt(unsigned char& byte, int bitIndex) { + return (byte >> (7 - bitIndex) & 3); // semi-nibbles store 00, 01, 10, or 11 +} + +void setSemiNibbleAt(unsigned char& byte, int bitIndex, int value) { + //assert(value <= 3 && value >= 0); + byte += ((value & 3) << (7 - bitIndex)); // semi-nibbles store 00, 01, 10, or 11 +} + void switchToResourcesParentIfRequired() { #ifdef __APPLE__ @@ -421,3 +430,4 @@ int insertIntoSortedArrays(void* value, float key, int originalIndex, } return -1; // error case } + diff --git a/libraries/shared/src/SharedUtil.h b/libraries/shared/src/SharedUtil.h index 466f45157e..28c4adb296 100644 --- a/libraries/shared/src/SharedUtil.h +++ b/libraries/shared/src/SharedUtil.h @@ -54,6 +54,10 @@ int numberOfOnes(unsigned char byte); bool oneAtBit(unsigned char byte, int bitIndex); void setAtBit(unsigned char& byte, int bitIndex); +int getSemiNibbleAt(unsigned char& byte, int bitIndex); +void setSemiNibbleAt(unsigned char& byte, int bitIndex, int value); + + void switchToResourcesParentIfRequired(); void loadRandomIdentifier(unsigned char* identifierBuffer, int numBytes); @@ -88,4 +92,5 @@ class debug { public: static const char* valueOf(bool checkValue) { return checkValue ? "yes" : "no"; }; }; + #endif /* defined(__hifi__SharedUtil__) */ diff --git a/libraries/voxels/src/ViewFrustum.cpp b/libraries/voxels/src/ViewFrustum.cpp index 061727b003..b3c890ed07 100644 --- a/libraries/voxels/src/ViewFrustum.cpp +++ b/libraries/voxels/src/ViewFrustum.cpp @@ -13,28 +13,37 @@ #include #include "ViewFrustum.h" +#include "VoxelConstants.h" #include "SharedUtil.h" #include "Log.h" using namespace std; ViewFrustum::ViewFrustum() : - _position(glm::vec3(0,0,0)), - _direction(glm::vec3(0,0,0)), - _up(glm::vec3(0,0,0)), - _right(glm::vec3(0,0,0)), + _position(0,0,0), + _orientation(), + _direction(0,0,0), + _up(0,0,0), + _right(0,0,0), _fieldOfView(0.0), _aspectRatio(1.0), _nearClip(0.1), _farClip(500.0), - _farTopLeft(glm::vec3(0,0,0)), - _farTopRight(glm::vec3(0,0,0)), - _farBottomLeft(glm::vec3(0,0,0)), - _farBottomRight(glm::vec3(0,0,0)), - _nearTopLeft(glm::vec3(0,0,0)), - _nearTopRight(glm::vec3(0,0,0)), - _nearBottomLeft(glm::vec3(0,0,0)), - _nearBottomRight(glm::vec3(0,0,0)) { } + _farTopLeft(0,0,0), + _farTopRight(0,0,0), + _farBottomLeft(0,0,0), + _farBottomRight(0,0,0), + _nearTopLeft(0,0,0), + _nearTopRight(0,0,0), + _nearBottomLeft(0,0,0), + _nearBottomRight(0,0,0) { } + +void ViewFrustum::setOrientation(const glm::quat& orientationAsQuaternion) { + _orientation = orientationAsQuaternion; + _right = glm::vec3(orientationAsQuaternion * glm::vec4(IDENTITY_RIGHT, 0.0f)); + _up = glm::vec3(orientationAsQuaternion * glm::vec4(IDENTITY_UP, 0.0f)); + _direction = glm::vec3(orientationAsQuaternion * glm::vec4(IDENTITY_FRONT, 0.0f)); +} ///////////////////////////////////////////////////////////////////////////////////// // ViewFrustum::calculateViewFrustum() diff --git a/libraries/voxels/src/ViewFrustum.h b/libraries/voxels/src/ViewFrustum.h index 1ae33d55ad..1c61160566 100644 --- a/libraries/voxels/src/ViewFrustum.h +++ b/libraries/voxels/src/ViewFrustum.h @@ -21,6 +21,9 @@ private: // camera location/orientation attributes glm::vec3 _position; + glm::quat _orientation; + + // calculated for orientation glm::vec3 _direction; glm::vec3 _up; glm::vec3 _right; @@ -53,23 +56,23 @@ private: public: // setters for camera attributes - void setPosition (const glm::vec3& p) { _position = p; } - void setOrientation (const glm::vec3& d, const glm::vec3& u, const glm::vec3& r ) - { _direction = d; _up = u; _right = r; } + void setPosition (const glm::vec3& p) { _position = p; }; + void setOrientation (const glm::quat& orientationAsQuaternion); // getters for camera attributes - const glm::vec3& getPosition() const { return _position; }; - const glm::vec3& getDirection() const { return _direction; }; - const glm::vec3& getUp() const { return _up; }; - const glm::vec3& getRight() const { return _right; }; + const glm::vec3& getPosition() const { return _position; }; + const glm::quat& getOrientation() const { return _orientation; }; + const glm::vec3& getDirection() const { return _direction; }; + const glm::vec3& getUp() const { return _up; }; + const glm::vec3& getRight() const { return _right; }; // setters for lens attributes - void setFieldOfView ( float f ) { _fieldOfView = f; } - void setAspectRatio ( float a ) { _aspectRatio = a; } - void setNearClip ( float n ) { _nearClip = n; } - void setFarClip ( float f ) { _farClip = f; } - void setEyeOffsetPosition (const glm::vec3& p) { _eyeOffsetPosition = p; } - void setEyeOffsetOrientation (const glm::quat& o) { _eyeOffsetOrientation = o; } + void setFieldOfView ( float f ) { _fieldOfView = f; }; + void setAspectRatio ( float a ) { _aspectRatio = a; }; + void setNearClip ( float n ) { _nearClip = n; }; + void setFarClip ( float f ) { _farClip = f; }; + void setEyeOffsetPosition (const glm::vec3& p) { _eyeOffsetPosition = p; }; + void setEyeOffsetOrientation (const glm::quat& o) { _eyeOffsetOrientation = o; }; // getters for lens attributes diff --git a/libraries/voxels/src/VoxelConstants.h b/libraries/voxels/src/VoxelConstants.h index 5bf1345d73..d0ae6f538a 100644 --- a/libraries/voxels/src/VoxelConstants.h +++ b/libraries/voxels/src/VoxelConstants.h @@ -15,6 +15,11 @@ #include #include +// this is where the coordinate system is represented +const glm::vec3 IDENTITY_RIGHT = glm::vec3( 1.0f, 0.0f, 0.0f); +const glm::vec3 IDENTITY_UP = glm::vec3( 0.0f, 1.0f, 0.0f); +const glm::vec3 IDENTITY_FRONT = glm::vec3( 0.0f, 0.0f,-1.0f); + const int TREE_SCALE = 128; const int NUMBER_OF_CHILDREN = 8; diff --git a/voxel-server/src/VoxelAgentData.cpp b/voxel-server/src/VoxelAgentData.cpp index 082fc5e7fc..289b1161e6 100644 --- a/voxel-server/src/VoxelAgentData.cpp +++ b/voxel-server/src/VoxelAgentData.cpp @@ -48,7 +48,7 @@ bool VoxelAgentData::updateCurrentViewFrustum() { ViewFrustum newestViewFrustum; // get position and orientation details from the camera newestViewFrustum.setPosition(getCameraPosition()); - newestViewFrustum.setOrientation(getCameraDirection(), getCameraUp(), getCameraRight()); + newestViewFrustum.setOrientation(getCameraOrientation()); // Also make sure it's got the correct lens details from the camera newestViewFrustum.setFieldOfView(getCameraFov());