From 02d0dbbfba0f85849dc086e96c2c6ea6bfc51acb Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 13 May 2013 15:40:27 -0700 Subject: [PATCH 01/31] move view frustum management to VoxelAgentData class --- voxel-server/src/VoxelAgentData.cpp | 21 +++++++++++++++++++++ voxel-server/src/VoxelAgentData.h | 6 ++++++ voxel-server/src/main.cpp | 22 +++------------------- 3 files changed, 30 insertions(+), 19 deletions(-) diff --git a/voxel-server/src/VoxelAgentData.cpp b/voxel-server/src/VoxelAgentData.cpp index 32526be383..07bf2a6593 100644 --- a/voxel-server/src/VoxelAgentData.cpp +++ b/voxel-server/src/VoxelAgentData.cpp @@ -50,3 +50,24 @@ VoxelAgentData::VoxelAgentData(const VoxelAgentData &otherAgentData) { VoxelAgentData* VoxelAgentData::clone() const { return new VoxelAgentData(*this); } + +void VoxelAgentData::updateViewFrustum() { + // save our currentViewFrustum into our lastKnownViewFrustum + lastKnownViewFrustum = currentViewFrustum; + + // get position and orientation details from the camera + currentViewFrustum.setPosition(getCameraPosition()); + currentViewFrustum.setOrientation(getCameraDirection(), getCameraUp(), getCameraRight()); + + // Also make sure it's got the correct lens details from the camera + currentViewFrustum.setFieldOfView(getCameraFov()); + currentViewFrustum.setAspectRatio(getCameraAspectRatio()); + currentViewFrustum.setNearClip(getCameraNearClip()); + currentViewFrustum.setFarClip(getCameraFarClip()); + + // if there has been a change, then recalculate + if (!lastKnownViewFrustum.matches(currentViewFrustum)) { + currentViewFrustum.calculate(); + } +} + diff --git a/voxel-server/src/VoxelAgentData.h b/voxel-server/src/VoxelAgentData.h index 74a402bb78..ee8b2dd660 100644 --- a/voxel-server/src/VoxelAgentData.h +++ b/voxel-server/src/VoxelAgentData.h @@ -40,6 +40,12 @@ public: void setMaxLevelReached(int maxLevelReached) { _maxLevelReachedInLastSearch = maxLevelReached; } VoxelNodeBag nodeBag; + + ViewFrustum currentViewFrustum; + ViewFrustum lastKnownViewFrustum; + + void updateViewFrustum(); + private: unsigned char* _voxelPacket; unsigned char* _voxelPacketAt; diff --git a/voxel-server/src/main.cpp b/voxel-server/src/main.cpp index 29b211e44f..76941d9bc2 100644 --- a/voxel-server/src/main.cpp +++ b/voxel-server/src/main.cpp @@ -109,8 +109,6 @@ void resInVoxelDistributor(AgentList* agentList, AgentList::iterator& agent, VoxelAgentData* agentData, ViewFrustum& viewFrustum) { - - printf("resInVoxelDistributor()\n"); bool searchReset = false; int searchLoops = 0; int searchLevelWas = agentData->getMaxSearchLevel(); @@ -228,9 +226,6 @@ void deepestLevelVoxelDistributor(AgentList* agentList, AgentList::iterator& agent, VoxelAgentData* agentData, ViewFrustum& viewFrustum) { - - printf("deepestLevelVoxelDistributor()\n"); - int maxLevelReached = 0; double start = usecTimestampNow(); if (agentData->nodeBag.isEmpty()) { @@ -340,23 +335,12 @@ void *distributeVoxelsToListeners(void *args) { // Sometimes the agent data has not yet been linked, in which case we can't really do anything if (agentData) { - ViewFrustum viewFrustum; - // get position and orientation details from the camera - viewFrustum.setPosition(agentData->getCameraPosition()); - viewFrustum.setOrientation(agentData->getCameraDirection(), agentData->getCameraUp(), agentData->getCameraRight()); - - // Also make sure it's got the correct lens details from the camera - viewFrustum.setFieldOfView(agentData->getCameraFov()); - viewFrustum.setAspectRatio(agentData->getCameraAspectRatio()); - viewFrustum.setNearClip(agentData->getCameraNearClip()); - viewFrustum.setFarClip(agentData->getCameraFarClip()); - - viewFrustum.calculate(); + agentData->updateViewFrustum(); if (agentData->getWantResIn()) { - resInVoxelDistributor(agentList, agent, agentData, viewFrustum); + resInVoxelDistributor(agentList, agent, agentData, agentData->currentViewFrustum); } else { - deepestLevelVoxelDistributor(agentList, agent, agentData, viewFrustum); + deepestLevelVoxelDistributor(agentList, agent, agentData, agentData->currentViewFrustum); } } } From e0388515a4edc0e52381366c1cf536f7d2f1d5ad Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Mon, 13 May 2013 21:11:54 -0700 Subject: [PATCH 02/31] Massive migration from GLUT/globals to Qt/class members. Mostly there. --- interface/CMakeLists.txt | 2 +- interface/src/Application.cpp | 1867 +++++++++++++++++++++++++++++++- interface/src/Application.h | 207 +++- interface/src/main.cpp | 29 +- interface/src/ui/ChatEntry.cpp | 46 +- interface/src/ui/ChatEntry.h | 5 +- 6 files changed, 2108 insertions(+), 48 deletions(-) diff --git a/interface/CMakeLists.txt b/interface/CMakeLists.txt index 156e5d8a7c..b3ac6c4b18 100644 --- a/interface/CMakeLists.txt +++ b/interface/CMakeLists.txt @@ -55,7 +55,7 @@ if (APPLE) SET(INTERFACE_SRCS ${INTERFACE_SRCS} ${INTERFACE_RSRCS}) endif (APPLE) -find_package(Qt4 REQUIRED QtCore QtGui) +find_package(Qt4 REQUIRED QtCore QtGui QtOpenGL) include(${QT_USE_FILE}) # run qt moc on qt-enabled headers diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index ff6c80d456..6eb3bc6397 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -5,18 +5,1873 @@ // Created by Andrzej Kapolka on 5/10/13. // Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +#include + +#include + +#ifdef _WIN32 +#include "Syssocket.h" +#include "Systime.h" +#else +#include +#include +#include +#endif + +#include +#include +#include +#include #include +#include +#include #include +#include +#include +#include +#include +#include +#include + #include "Application.h" +#include "InterfaceConfig.h" +#include "Log.h" +#include "OculusManager.h" +#include "Util.h" +#include "renderer/ProgramObject.h" +#include "ui/TextRenderer.h" -Application::Application(int& argc, char** argv) : QApplication(argc, argv) { - // simple menu bar (will only appear on OS X, for now) +using namespace std; + +//const CameraMode DEFAULT_CAMERA_MODE = CAMERA_MODE_FIRST_PERSON; +const CameraMode DEFAULT_CAMERA_MODE = CAMERA_MODE_THIRD_PERSON; + +// Starfield information +static char STAR_FILE[] = "https://s3-us-west-1.amazonaws.com/highfidelity/stars.txt"; +static char STAR_CACHE_FILE[] = "cachedStars.txt"; + +const glm::vec3 START_LOCATION(6.1f, 0, 1.4f); // Where one's own agent begins in the world + // (will be overwritten if avatar data file is found) + +const int IDLE_SIMULATE_MSECS = 16; // How often should call simulate and other stuff + // in the idle loop? (60 FPS is default) + +const bool USING_MOUSE_VIEW_SHIFT = false; +const float MOUSE_VIEW_SHIFT_RATE = 40.0f; +const float MOUSE_VIEW_SHIFT_YAW_MARGIN = (float)(1200 * 0.2f); +const float MOUSE_VIEW_SHIFT_PITCH_MARGIN = (float)(800 * 0.2f); +const float MOUSE_VIEW_SHIFT_YAW_LIMIT = 45.0; +const float MOUSE_VIEW_SHIFT_PITCH_LIMIT = 30.0; + +const bool DISPLAY_HEAD_MOUSE = true; + +// customized canvas that simply forwards requests/events to the singleton application +class GLCanvas : public QGLWidget { +protected: + + virtual void initializeGL(); + virtual void paintGL(); + virtual void resizeGL(int width, int height); + + virtual void keyPressEvent(QKeyEvent* event); + virtual void keyReleaseEvent(QKeyEvent* event); + + virtual void mouseMoveEvent(QMouseEvent* event); + virtual void mousePressEvent(QMouseEvent* event); + virtual void mouseReleaseEvent(QMouseEvent* event); +}; + +void GLCanvas::initializeGL() { + static_cast(QCoreApplication::instance())->initializeGL(); +} + +void GLCanvas::paintGL() { + static_cast(QCoreApplication::instance())->paintGL(); +} + +void GLCanvas::resizeGL(int width, int height) { + static_cast(QCoreApplication::instance())->resizeGL(width, height); +} + +void GLCanvas::keyPressEvent(QKeyEvent* event) { + static_cast(QCoreApplication::instance())->keyPressEvent(event); +} + +void GLCanvas::keyReleaseEvent(QKeyEvent* event) { + static_cast(QCoreApplication::instance())->keyReleaseEvent(event); +} + +void GLCanvas::mouseMoveEvent(QMouseEvent* event) { + static_cast(QCoreApplication::instance())->mouseMoveEvent(event); +} + +void GLCanvas::mousePressEvent(QMouseEvent* event) { + static_cast(QCoreApplication::instance())->mousePressEvent(event); +} + +void GLCanvas::mouseReleaseEvent(QMouseEvent* event) { + static_cast(QCoreApplication::instance())->mouseReleaseEvent(event); +} + +Application::Application(int& argc, char** argv) : + QApplication(argc, argv), + _window(new QMainWindow(desktop())), + _glWidget(new GLCanvas()), + _displayLevels(false), + _frameCount(0), + _fps(120.0f), + _justStarted(true), + _wantToKillLocalVoxels(false), + _frustumDrawingMode(FRUSTUM_DRAW_MODE_ALL), + _viewFrustumOffsetYaw(-135.0), + _viewFrustumOffsetPitch(0.0), + _viewFrustumOffsetRoll(0.0), + _viewFrustumOffsetDistance(25.0), + _viewFrustumOffsetUp(0.0), + _mouseViewShiftYaw(0.0f), + _mouseViewShiftPitch(0.0f), + _audioScope(256, 200, true), + _myAvatar(true), + _mouseX(0), + _mouseY(0), + _mousePressed(false), + _mouseMode(NO_EDIT_MODE), + _mouseVoxelScale(1.0f / 1024.0f), + _paintOn(false), + _dominantColor(0), + _perfStatsOn(false), + _chatEntryOn(false), + _oculusTextureID(0), + _oculusProgram(0), + _oculusDistortionScale(1.25), +#ifndef _WIN32 + _audio(&_audioScope, &_myAvatar), +#endif + _stopNetworkReceiveThread(false), + _packetCount(0), + _packetsPerSecond(0), + _bytesPerSecond(0), + _bytesCount(0) { + + gettimeofday(&_applicationStartupTime, NULL); + printLog("Interface Startup:\n"); + + _voxels.setViewFrustum(&_viewFrustum); + + shared_lib::printLog = & ::printLog; + voxels_lib::printLog = & ::printLog; + avatars_lib::printLog = & ::printLog; + + unsigned int listenPort = AGENT_SOCKET_LISTEN_PORT; + const char** constArgv = const_cast(argv); + const char* portStr = getCmdOption(argc, constArgv, "--listenPort"); + if (portStr) { + listenPort = atoi(portStr); + } + AgentList::createInstance(AGENT_TYPE_AVATAR, listenPort); + _enableNetworkThread = !cmdOptionExists(argc, constArgv, "--nonblocking"); + if (!_enableNetworkThread) { + AgentList::getInstance()->getAgentSocket().setBlocking(false); + } + + const char* domainIP = getCmdOption(argc, constArgv, "--domain"); + if (domainIP) { + strcpy(DOMAIN_IP, domainIP); + } + + // Handle Local Domain testing with the --local command line + if (cmdOptionExists(argc, constArgv, "--local")) { + printLog("Local Domain MODE!\n"); + int ip = getLocalAddress(); + sprintf(DOMAIN_IP,"%d.%d.%d.%d", (ip & 0xFF), ((ip >> 8) & 0xFF),((ip >> 16) & 0xFF), ((ip >> 24) & 0xFF)); + } + + // Check to see if the user passed in a command line option for loading a local + // Voxel File. + _voxelsFilename = getCmdOption(argc, constArgv, "-i"); + + // the callback for our instance of AgentList is attachNewHeadToAgent + AgentList::getInstance()->linkedDataCreateCallback = &attachNewHeadToAgent; + + #ifndef _WIN32 + AgentList::getInstance()->audioMixerSocketUpdate = &audioMixerUpdate; + #endif + + #ifdef _WIN32 + WSADATA WsaData; + int wsaresult = WSAStartup(MAKEWORD(2,2), &WsaData); + #endif + + // start the agentList threads + AgentList::getInstance()->startSilentAgentRemovalThread(); + AgentList::getInstance()->startDomainServerCheckInThread(); + AgentList::getInstance()->startPingUnknownAgentsThread(); + + _window->setCentralWidget(_glWidget); + + initMenu(); + + QRect available = desktop()->availableGeometry(); + _window->resize(available.size()); + _window->setVisible(true); + _glWidget->setFocus(Qt::ActiveWindowFocusReason); + + // initialization continues in initializeGL when OpenGL context is ready +} + +void Application::initializeGL() { + printLog( "Created Display Window.\n" ); + + int argc = 0; + glutInit(&argc, 0); + + #ifdef _WIN32 + glewInit(); + printLog( "Glew Init complete.\n" ); + #endif + + // Before we render anything, let's set up our viewFrustumOffsetCamera with a sufficiently large + // field of view and near and far clip to make it interesting. + //viewFrustumOffsetCamera.setFieldOfView(90.0); + _viewFrustumOffsetCamera.setNearClip(0.1); + _viewFrustumOffsetCamera.setFarClip(500.0 * TREE_SCALE); + + initDisplay(); + printLog( "Initialized Display.\n" ); + + init(); + printLog( "Init() complete.\n" ); + + // Check to see if the user passed in a command line option for randomizing colors + bool wantColorRandomizer = !arguments().contains("--NoColorRandomizer"); + + // Check to see if the user passed in a command line option for loading a local + // Voxel File. If so, load it now. + if (!_voxelsFilename.isEmpty()) { + _voxels.loadVoxelsFile(_voxelsFilename.constData(), wantColorRandomizer); + printLog("Local Voxel File loaded.\n"); + } + + // create thread for receipt of data via UDP + if (_enableNetworkThread) { + pthread_create(&_networkReceiveThread, NULL, networkReceive, NULL); + printLog("Network receive thread created.\n"); + } + + _myAvatar.readAvatarDataFromFile(); + + // call terminate before exiting + connect(this, SIGNAL(aboutToQuit()), SLOT(terminate())); + + // call our timer function every second + QTimer* timer = new QTimer(this); + connect(timer, SIGNAL(timeout()), SLOT(timer())); + timer->start(1000); + + // call our idle function whenever we can + QTimer* idleTimer = new QTimer(this); + connect(idleTimer, SIGNAL(timeout()), SLOT(idle())); + idleTimer->start(0); +} + +void Application::paintGL() { + PerfStat("display"); + + glEnable(GL_LINE_SMOOTH); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glMatrixMode(GL_MODELVIEW); + + glPushMatrix(); { + glLoadIdentity(); + + // camera settings + if (_myCamera.getMode() == CAMERA_MODE_MIRROR) { + _myAvatar.setDisplayingHead(true); + _myCamera.setUpShift (0.0); + _myCamera.setDistance (0.2); + _myCamera.setTightness (100.0f); + _myCamera.setTargetPosition(_myAvatar.getHeadPosition()); + _myCamera.setTargetRotation(_myAvatar.getBodyYaw() - 180.0f, 0.0f, 0.0f); + + } else if (_myCamera.getMode() == CAMERA_MODE_FIRST_PERSON || OculusManager::isConnected()) { + _myAvatar.setDisplayingHead(false); + _myCamera.setUpShift (0.0f); + _myCamera.setDistance (0.0f); + _myCamera.setTightness (100.0f); + _myCamera.setTargetPosition(_myAvatar.getHeadPosition()); + + if (OculusManager::isConnected()) { + _myCamera.setTargetRotation(_myAvatar.getBodyYaw() + _myAvatar.getHeadYaw(), + -_myAvatar.getHeadPitch(), + _myAvatar.getHeadRoll()); + } else { + _myCamera.setTargetRotation(_myAvatar.getAbsoluteHeadYaw()- _mouseViewShiftYaw, + _myAvatar.getAbsoluteHeadPitch() + + _myAvatar.getRenderPitch() + _mouseViewShiftPitch, 0.0f); + } + } else if (_myCamera.getMode() == CAMERA_MODE_THIRD_PERSON) { + _myAvatar.setDisplayingHead(true); + _myCamera.setUpShift (-0.2f); + _myCamera.setDistance (1.5f); + _myCamera.setTightness (8.0f); + _myCamera.setTargetPosition(_myAvatar.getHeadPosition()); + _myCamera.setTargetRotation(_myAvatar.getBodyYaw() - _mouseViewShiftYaw, _mouseViewShiftPitch, 0.0f); + } + + // important... + _myCamera.update( 1.f/_fps ); + + // Render anything (like HUD items) that we want to be in 3D but not in worldspace + /* + const float HUD_Z_OFFSET = -5.f; + glPushMatrix(); + glm::vec3 test(0.5, 0.5, 0.5); + glTranslatef(1, 1, HUD_Z_OFFSET); + drawVector(&test); + glPopMatrix(); + */ + + + // Note: whichCamera is used to pick between the normal camera myCamera for our + // main camera, vs, an alternate camera. The alternate camera we support right now + // is the viewFrustumOffsetCamera. But theoretically, we could use this same mechanism + // to add other cameras. + // + // Why have two cameras? Well, one reason is that because in the case of the renderViewFrustum() + // code, we want to keep the state of "myCamera" intact, so we can render what the view frustum of + // myCamera is. But we also want to do meaningful camera transforms on OpenGL for the offset camera + Camera whichCamera = _myCamera; + + if (_viewFrustumFromOffset->isChecked() && _frustumOn->isChecked()) { + + // set the camera to third-person view but offset so we can see the frustum + _viewFrustumOffsetCamera.setTargetYaw(_viewFrustumOffsetYaw + _myAvatar.getBodyYaw()); + _viewFrustumOffsetCamera.setPitch (_viewFrustumOffsetPitch ); + _viewFrustumOffsetCamera.setRoll (_viewFrustumOffsetRoll ); + _viewFrustumOffsetCamera.setUpShift (_viewFrustumOffsetUp ); + _viewFrustumOffsetCamera.setDistance (_viewFrustumOffsetDistance); + _viewFrustumOffsetCamera.update(1.f/_fps); + whichCamera = _viewFrustumOffsetCamera; + } + + // transform view according to whichCamera + // could be myCamera (if in normal mode) + // or could be viewFrustumOffsetCamera if in offset mode + // I changed the ordering here - roll is FIRST (JJV) + + glRotatef ( whichCamera.getRoll(), IDENTITY_FRONT.x, IDENTITY_FRONT.y, IDENTITY_FRONT.z); + glRotatef ( whichCamera.getPitch(), IDENTITY_RIGHT.x, IDENTITY_RIGHT.y, IDENTITY_RIGHT.z); + glRotatef (180.0 - whichCamera.getYaw(), IDENTITY_UP.x, IDENTITY_UP.y, IDENTITY_UP.z ); + + glTranslatef(-whichCamera.getPosition().x, -whichCamera.getPosition().y, -whichCamera.getPosition().z); + + // Setup 3D lights (after the camera transform, so that they are positioned in world space) + glEnable(GL_COLOR_MATERIAL); + glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); + + GLfloat light_position0[] = { 1.0, 1.0, 0.0, 0.0 }; + glLightfv(GL_LIGHT0, GL_POSITION, light_position0); + GLfloat ambient_color[] = { 0.7, 0.7, 0.8 }; + glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color); + GLfloat diffuse_color[] = { 0.8, 0.7, 0.7 }; + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_color); + GLfloat specular_color[] = { 1.0, 1.0, 1.0, 1.0}; + glLightfv(GL_LIGHT0, GL_SPECULAR, specular_color); + + glMaterialfv(GL_FRONT, GL_SPECULAR, specular_color); + glMateriali(GL_FRONT, GL_SHININESS, 96); + + if (_oculusOn->isChecked()) { + displayOculus(whichCamera); + + } else { + displaySide(whichCamera); + glPopMatrix(); + + displayOverlay(); + } + } + + _frameCount++; + + // If application has just started, report time from startup to now (first frame display) + if (_justStarted) { + float startupTime = (usecTimestampNow() - usecTimestamp(&_applicationStartupTime))/1000000.0; + _justStarted = false; + char title[30]; + snprintf(title, 30, "Interface: %4.2f seconds", startupTime); + _window->setWindowTitle(title); + } +} + +void Application::resizeGL(int width, int height) { + float aspectRatio = ((float)width/(float)height); // based on screen resize + + // get the lens details from the current camera + Camera& camera = _viewFrustumFromOffset ? _viewFrustumOffsetCamera : _myCamera; + float nearClip = camera.getNearClip(); + float farClip = camera.getFarClip(); + float fov; + + if (_oculusOn->isChecked()) { + // more magic numbers; see Oculus SDK docs, p. 32 + camera.setAspectRatio(aspectRatio *= 0.5); + camera.setFieldOfView(fov = 2 * atan((0.0468 * _oculusDistortionScale) / 0.041) * (180 / PI)); + + // resize the render texture + if (_oculusTextureID != 0) { + glBindTexture(GL_TEXTURE_2D, _oculusTextureID); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); + glBindTexture(GL_TEXTURE_2D, 0); + } + } else { + camera.setAspectRatio(aspectRatio); + camera.setFieldOfView(fov = 60); + } + + // Tell our viewFrustum about this change + _viewFrustum.setAspectRatio(aspectRatio); + + glViewport(0, 0, width, height); // shouldn't this account for the menu??? + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + // XXXBHG - If we're in view frustum mode, then we need to do this little bit of hackery so that + // OpenGL won't clip our frustum rendering lines. This is a debug hack for sure! Basically, this makes + // the near clip a little bit closer (therefor you see more) and the far clip a little bit farther (also, + // to see more.) + if (_frustumOn->isChecked()) { + nearClip -= 0.01f; + farClip += 0.01f; + } + + // On window reshape, we need to tell OpenGL about our new setting + gluPerspective(fov,aspectRatio,nearClip,farClip); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + +static void sendVoxelServerEraseAll() { + char message[100]; + sprintf(message,"%c%s",'Z',"erase all"); + int messageSize = strlen(message) + 1; + AgentList::getInstance()->broadcastToAgents((unsigned char*) message, messageSize, &AGENT_TYPE_VOXEL, 1); +} + +static void sendVoxelServerAddScene() { + char message[100]; + sprintf(message,"%c%s",'Z',"add scene"); + int messageSize = strlen(message) + 1; + AgentList::getInstance()->broadcastToAgents((unsigned char*)message, messageSize, &AGENT_TYPE_VOXEL, 1); +} + +void Application::keyPressEvent(QKeyEvent* event) { + if (_chatEntryOn) { + if (_chatEntry.keyPressEvent(event)) { + _myAvatar.setKeyState(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete ? + DELETE_KEY_DOWN : INSERT_KEY_DOWN); + _myAvatar.setChatMessage(string(_chatEntry.getContents().size(), SOLID_BLOCK_CHAR)); + + } else { + _myAvatar.setChatMessage(_chatEntry.getContents()); + _chatEntry.clear(); + _chatEntryOn = false; + } + return; + } + + bool shifted = event->modifiers().testFlag(Qt::ShiftModifier); + switch (event->key()) { + case Qt::Key_BracketLeft: + _viewFrustumOffsetYaw -= 0.5; + break; + + case Qt::Key_BracketRight: + _viewFrustumOffsetYaw += 0.5; + break; + + case Qt::Key_BraceLeft: + _viewFrustumOffsetPitch -= 0.5; + break; + + case Qt::Key_BraceRight: + _viewFrustumOffsetPitch += 0.5; + break; + + case Qt::Key_ParenLeft: + _viewFrustumOffsetRoll -= 0.5; + break; + + case Qt::Key_ParenRight: + _viewFrustumOffsetRoll += 0.5; + break; + + case Qt::Key_Less: + _viewFrustumOffsetDistance -= 0.5; + break; + + case Qt::Key_Greater: + _viewFrustumOffsetDistance += 0.5; + break; + + case Qt::Key_Comma: + _viewFrustumOffsetUp -= 0.05; + break; + + case Qt::Key_Period: + _viewFrustumOffsetUp += 0.05; + break; + + case Qt::Key_Ampersand: + _paintOn = !_paintOn; + setupPaintingVoxel(); + break; + + case Qt::Key_AsciiCircum: + shiftPaintingColor(); + break; + + case Qt::Key_Minus: + sendVoxelServerEraseAll(); + break; + + case Qt::Key_Percent: + sendVoxelServerAddScene(); + break; + + case Qt::Key_1: + _mouseMode = (_mouseMode == ADD_VOXEL_MODE) ? NO_EDIT_MODE : ADD_VOXEL_MODE; + break; + + case Qt::Key_2: + _mouseMode = (_mouseMode == DELETE_VOXEL_MODE) ? NO_EDIT_MODE : DELETE_VOXEL_MODE; + break; + + case Qt::Key_3: + _mouseMode = (_mouseMode == COLOR_VOXEL_MODE) ? NO_EDIT_MODE : COLOR_VOXEL_MODE; + break; + + case Qt::Key_4: + addVoxelInFrontOfAvatar(); + break; + + case Qt::Key_5: + _mouseVoxelScale /= 2; + break; + + case Qt::Key_6: + _mouseVoxelScale *= 2; + break; + + case Qt::Key_L: + _displayLevels = !_displayLevels; + break; + + case Qt::Key_E: + _myAvatar.setDriveKeys(UP, 1); + break; + + case Qt::Key_C: + _myAvatar.setDriveKeys(DOWN, 1); + break; + + case Qt::Key_W: + _myAvatar.setDriveKeys(FWD, 1); + break; + + case Qt::Key_S: + _myAvatar.setDriveKeys(BACK, 1); + break; + + case Qt::Key_Space: + resetSensors(); + break; + + case Qt::Key_A: + _myAvatar.setDriveKeys(ROT_LEFT, 1); + break; + + case Qt::Key_D: + _myAvatar.setDriveKeys(ROT_RIGHT, 1); + break; + + case Qt::Key_Enter: + _chatEntryOn = true; + _myAvatar.setKeyState(NO_KEY_DOWN); + _myAvatar.setChatMessage(string()); + break; + + case Qt::Key_Up: + _myAvatar.setDriveKeys(shifted ? UP : FWD, 1); + break; + + case Qt::Key_Down: + _myAvatar.setDriveKeys(shifted ? DOWN : BACK, 1); + break; + + case Qt::Key_Left: + _myAvatar.setDriveKeys(shifted ? LEFT : ROT_LEFT, 1); + break; + + case Qt::Key_Right: + _myAvatar.setDriveKeys(shifted ? RIGHT : ROT_RIGHT, 1); + break; + + default: + event->ignore(); + break; + } +} + +void Application::keyReleaseEvent(QKeyEvent* event) { + if (_chatEntryOn) { + _myAvatar.setKeyState(NO_KEY_DOWN); + return; + } + + switch (event->key()) { + case Qt::Key_E: + _myAvatar.setDriveKeys(UP, 0); + break; + + case Qt::Key_C: + _myAvatar.setDriveKeys(DOWN, 0); + break; + + case Qt::Key_W: + _myAvatar.setDriveKeys(FWD, 0); + break; + + case Qt::Key_S: + _myAvatar.setDriveKeys(BACK, 0); + break; + + case Qt::Key_A: + _myAvatar.setDriveKeys(ROT_LEFT, 0); + break; + + case Qt::Key_D: + _myAvatar.setDriveKeys(ROT_RIGHT, 0); + break; + + case Qt::Key_Up: + _myAvatar.setDriveKeys(FWD, 0); + _myAvatar.setDriveKeys(UP, 0); + break; + + case Qt::Key_Down: + _myAvatar.setDriveKeys(BACK, 0); + _myAvatar.setDriveKeys(DOWN, 0); + break; + + case Qt::Key_Left: + _myAvatar.setDriveKeys(LEFT, 0); + _myAvatar.setDriveKeys(ROT_LEFT, 0); + break; + + case Qt::Key_Right: + _myAvatar.setDriveKeys(RIGHT, 0); + _myAvatar.setDriveKeys(ROT_RIGHT, 0); + break; + + default: + event->ignore(); + break; + } +} + +void Application::mouseMoveEvent(QMouseEvent* event) { + _mouseX = event->x(); + _mouseY = event->y(); +} + +void Application::mousePressEvent(QMouseEvent* event) { + if (event->button() == Qt::LeftButton) { + _mouseX = event->x(); + _mouseY = event->y(); + _mousePressed = true; + + if (_mouseMode == ADD_VOXEL_MODE || _mouseMode == COLOR_VOXEL_MODE) { + addVoxelUnderCursor(); + + } else if (_mouseMode == DELETE_VOXEL_MODE) { + deleteVoxelUnderCursor(); + } + } else if (event->button() == Qt::RightButton && _mouseMode != NO_EDIT_MODE) { + deleteVoxelUnderCursor(); + } +} + +void Application::mouseReleaseEvent(QMouseEvent* event) { + if (event->button() == Qt::LeftButton) { + _mouseX = event->x(); + _mouseY = event->y(); + _mousePressed = false; + } +} + +// Every second, check the frame rates and other stuff +void Application::timer() { + gettimeofday(&_timerEnd, NULL); + _fps = (float)_frameCount / ((float)diffclock(&_timerStart, &_timerEnd) / 1000.f); + _packetsPerSecond = (float)_packetCount / ((float)diffclock(&_timerStart, &_timerEnd) / 1000.f); + _bytesPerSecond = (float)_bytesCount / ((float)diffclock(&_timerStart, &_timerEnd) / 1000.f); + _frameCount = 0; + _packetCount = 0; + _bytesCount = 0; + + gettimeofday(&_timerStart, NULL); + + // if we haven't detected gyros, check for them now + if (!_serialPort.active) { + _serialPort.pair(); + } +} + +static glm::vec3 getFaceVector(BoxFace face) { + switch (face) { + case MIN_X_FACE: + return glm::vec3(-1, 0, 0); + + case MAX_X_FACE: + return glm::vec3(1, 0, 0); + + case MIN_Y_FACE: + return glm::vec3(0, -1, 0); + + case MAX_Y_FACE: + return glm::vec3(0, 1, 0); + + case MIN_Z_FACE: + return glm::vec3(0, 0, -1); + + case MAX_Z_FACE: + return glm::vec3(0, 0, 1); + } +} + +//Find and return the gravity vector at this location +static glm::vec3 getGravity(const glm::vec3& pos) { + // + // For now, we'll test this with a simple global lookup, but soon we will add getting this + // from the domain/voxelserver (or something similar) + // + if ((pos.x > 0.f) && + (pos.x < 10.f) && + (pos.z > 0.f) && + (pos.z < 10.f) && + (pos.y > 0.f) && + (pos.y < 3.f)) { + // If above ground plane, turn gravity on + return glm::vec3(0.f, -1.f, 0.f); + } else { + // If flying in space, turn gravity OFF + return glm::vec3(0.f, 0.f, 0.f); + } +} + +void Application::idle() { + timeval check; + gettimeofday(&check, NULL); + + // Only run simulation code if more than IDLE_SIMULATE_MSECS have passed since last time + + if (diffclock(&_lastTimeIdle, &check) > IDLE_SIMULATE_MSECS) { + + float deltaTime = 1.f/_fps; + + // update behaviors for avatar hand movement: handControl takes mouse values as input, + // and gives back 3D values modulated for smooth transitioning between interaction modes. + _handControl.update(_mouseX, _mouseY); + _myAvatar.setHandMovementValues(_handControl.getValues()); + + // tell my avatar if the mouse is being pressed... + _myAvatar.setMousePressed(_mousePressed); + + // check what's under the mouse and update the mouse voxel + glm::vec3 origin, direction; + _viewFrustum.computePickRay(_mouseX / (float)_glWidget->width(), + _mouseY / (float)_glWidget->height(), origin, direction); + + float distance; + BoxFace face; + _mouseVoxel.s = 0.0f; + if (_mouseMode != NO_EDIT_MODE && _voxels.findRayIntersection(origin, direction, _mouseVoxel, distance, face)) { + // find the nearest voxel with the desired scale + if (_mouseVoxelScale > _mouseVoxel.s) { + // choose the larger voxel that encompasses the one selected + _mouseVoxel.x = _mouseVoxelScale * floorf(_mouseVoxel.x / _mouseVoxelScale); + _mouseVoxel.y = _mouseVoxelScale * floorf(_mouseVoxel.y / _mouseVoxelScale); + _mouseVoxel.z = _mouseVoxelScale * floorf(_mouseVoxel.z / _mouseVoxelScale); + _mouseVoxel.s = _mouseVoxelScale; + + } else { + glm::vec3 faceVector = getFaceVector(face); + if (_mouseVoxelScale < _mouseVoxel.s) { + // find the closest contained voxel + glm::vec3 pt = (origin + direction * distance) / (float)TREE_SCALE - + faceVector * (_mouseVoxelScale * 0.5f); + _mouseVoxel.x = _mouseVoxelScale * floorf(pt.x / _mouseVoxelScale); + _mouseVoxel.y = _mouseVoxelScale * floorf(pt.y / _mouseVoxelScale); + _mouseVoxel.z = _mouseVoxelScale * floorf(pt.z / _mouseVoxelScale); + _mouseVoxel.s = _mouseVoxelScale; + } + if (_mouseMode == ADD_VOXEL_MODE) { + // use the face to determine the side on which to create a neighbor + _mouseVoxel.x += faceVector.x * _mouseVoxel.s; + _mouseVoxel.y += faceVector.y * _mouseVoxel.s; + _mouseVoxel.z += faceVector.z * _mouseVoxel.s; + } + } + + if (_mouseMode == COLOR_VOXEL_MODE) { + _mouseVoxel.red = 0; + _mouseVoxel.green = 255; + _mouseVoxel.blue = 0; + + } else if (_mouseMode == DELETE_VOXEL_MODE) { + // red indicates deletion + _mouseVoxel.red = 255; + _mouseVoxel.green = _mouseVoxel.blue = 0; + } + } + + // walking triggers the handControl to stop + if (_myAvatar.getMode() == AVATAR_MODE_WALKING) { + _handControl.stop(); + _mouseViewShiftYaw *= 0.9; + _mouseViewShiftPitch *= 0.9; + } + + // Read serial port interface devices + if (_serialPort.active) { + _serialPort.readData(); + } + + // Sample hardware, update view frustum if needed, and send avatar data to mixer/agents + updateAvatar(deltaTime); + + // read incoming packets from network + if (!_enableNetworkThread) { + networkReceive(0); + } + + //loop through all the remote avatars and simulate them... + AgentList* agentList = AgentList::getInstance(); + agentList->lock(); + for(AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { + if (agent->getLinkedData() != NULL) { + Avatar *avatar = (Avatar *)agent->getLinkedData(); + avatar->simulate(deltaTime); + } + } + agentList->unlock(); + + _myAvatar.setGravity(getGravity(_myAvatar.getPosition())); + _myAvatar.simulate(deltaTime); + + // Update audio stats for procedural sounds + #ifndef _WIN32 + _audio.setLastAcceleration(_myAvatar.getThrust()); + _audio.setLastVelocity(_myAvatar.getVelocity()); + #endif + + _glWidget->updateGL(); + _lastTimeIdle = check; + } +} + +void Application::terminate() { + // Close serial port + // close(serial_fd); + + _myAvatar.writeAvatarDataToFile(); + + #ifndef _WIN32 + _audio.terminate(); + #endif + + if (_enableNetworkThread) { + _stopNetworkReceiveThread = true; + pthread_join(_networkReceiveThread, NULL); + } +} + +void Application::setHead(bool head) { + #ifndef _WIN32 + _audio.setMixerLoopbackFlag(head); + _myCamera.setMode(head ? CAMERA_MODE_MIRROR : DEFAULT_CAMERA_MODE); + #endif +} + +void Application::setNoise(bool noise) { + _myAvatar.setNoise(noise); +} + +void Application::setFullscreen(bool fullscreen) { + _window->setWindowState(fullscreen ? (_window->windowState() | Qt::WindowFullScreen) : + (_window->windowState() & ~Qt::WindowFullScreen)); +} + +void Application::setRenderFirstPerson(bool firstPerson) { + _myCamera.setMode(firstPerson ? CAMERA_MODE_FIRST_PERSON : CAMERA_MODE_THIRD_PERSON); +} + +void Application::setOculus(bool oculus) { + resizeGL(_glWidget->width(), _glWidget->height()); +} + +void Application::setMenu(bool menu) { + _window->menuBar()->setVisible(menu); +} + +void Application::setFrustumOffset(bool frustumOffset) { + // reshape so that OpenGL will get the right lens details for the camera of choice + resizeGL(_glWidget->width(), _glWidget->height()); +} + +void Application::cycleFrustumRenderMode() { + _frustumDrawingMode = (FrustumDrawMode)((_frustumDrawingMode + 1) % FRUSTUM_DRAW_MODE_COUNT); + updateFrustumRenderModeAction(); +} + +void Application::setRenderWarnings(bool renderWarnings) { + _voxels.setRenderPipelineWarnings(renderWarnings); +} + +void Application::doKillLocalVoxels() { + _wantToKillLocalVoxels = true; +} + +void Application::doRandomizeVoxelColors() { + _voxels.randomizeVoxelColors(); +} + +void Application::doFalseRandomizeVoxelColors() { + _voxels.falseColorizeRandom(); +} + +void Application::doFalseRandomizeEveryOtherVoxelColors() { + _voxels.falseColorizeRandomEveryOther(); +} + +void Application::doFalseColorizeByDistance() { + loadViewFrustum(_viewFrustum); + _voxels.falseColorizeDistanceFromView(&_viewFrustum); +} + +void Application::doFalseColorizeInView() { + loadViewFrustum(_viewFrustum); + // we probably want to make sure the viewFrustum is initialized first + _voxels.falseColorizeInView(&_viewFrustum); +} + +void Application::doTrueVoxelColors() { + _voxels.trueColorize(); +} + +void Application::doTreeStats() { + _voxels.collectStatsForTreesAndVBOs(); +} + +void Application::initMenu() { QMenuBar* menuBar = new QMenuBar(); - QMenu* fileMenu = menuBar->addMenu("File"); - fileMenu->addAction("Test", this, SLOT(testSlot())); + _window->setMenuBar(menuBar); + + QMenu* optionsMenu = menuBar->addMenu("Options"); + (_lookingInMirror = optionsMenu->addAction("Mirror", this, SLOT(setHead(bool)), Qt::Key_H))->setCheckable(true); + optionsMenu->addAction("Noise", this, SLOT(setNoise(bool)), Qt::Key_N)->setCheckable(true); + (_gyroLook = optionsMenu->addAction("Gyro Look"))->setCheckable(true); + _gyroLook->setChecked(true); + optionsMenu->addAction("Fullscreen", this, SLOT(setFullscreen(bool)), Qt::Key_F)->setCheckable(true); + optionsMenu->addAction("Quit", this, SLOT(quit()), Qt::Key_Q); + + QMenu* renderMenu = menuBar->addMenu("Render"); + (_renderVoxels = renderMenu->addAction("Voxels"))->setCheckable(true); + _renderVoxels->setChecked(true); + _renderVoxels->setShortcut(Qt::Key_V); + (_renderStarsOn = renderMenu->addAction("Stars"))->setCheckable(true); + _renderStarsOn->setChecked(true); + _renderStarsOn->setShortcut(Qt::Key_Asterisk); + (_renderAtmosphereOn = renderMenu->addAction("Atmosphere"))->setCheckable(true); + _renderAtmosphereOn->setChecked(true); + _renderAtmosphereOn->setShortcut(Qt::SHIFT | Qt::Key_A); + (_renderAvatarsOn = renderMenu->addAction("Avatars"))->setCheckable(true); + _renderAvatarsOn->setChecked(true); + renderMenu->addAction("First Person", this, SLOT(setRenderFirstPerson(bool)), Qt::Key_P)->setCheckable(true); + (_oculusOn = renderMenu->addAction("Oculus", this, SLOT(setOculus(bool)), Qt::Key_O))->setCheckable(true); + + QMenu* toolsMenu = menuBar->addMenu("Tools"); + (_renderStatsOn = toolsMenu->addAction("Stats"))->setCheckable(true); + _renderStatsOn->setShortcut(Qt::Key_Slash); + (_logOn = toolsMenu->addAction("Log"))->setCheckable(true); + _logOn->setChecked(true); + QAction* menuAction = toolsMenu->addAction("Menu", this, SLOT(setMenu(bool)), Qt::Key_M); + menuAction->setCheckable(true); + menuAction->setChecked(true); + + QMenu* frustumMenu = menuBar->addMenu("Frustum"); + (_frustumOn = frustumMenu->addAction("Display Frustum"))->setCheckable(true); + _frustumOn->setShortcut(Qt::SHIFT | Qt::Key_F); + (_viewFrustumFromOffset = frustumMenu->addAction( + "Use Offset Camera", this, SLOT(setFrustumOffset(bool)), Qt::SHIFT | Qt::Key_O))->setCheckable(true); + (_cameraFrustum = frustumMenu->addAction("Switch Camera"))->setCheckable(true); + _cameraFrustum->setChecked(true); + _cameraFrustum->setShortcut(Qt::SHIFT | Qt::Key_C); + _frustumRenderModeAction = frustumMenu->addAction( + "Render Mode", this, SLOT(cycleFrustumRenderMode()), Qt::SHIFT | Qt::Key_R); + updateFrustumRenderModeAction(); + + QMenu* debugMenu = menuBar->addMenu("Debug"); + debugMenu->addAction("Show Render Pipeline Warnings", this, SLOT(setRenderWarnings(bool)))->setCheckable(true); + debugMenu->addAction("Kill Local Voxels", this, SLOT(doKillLocalVoxels())); + debugMenu->addAction("Randomize Voxel TRUE Colors", this, SLOT(doRandomizeVoxelColors())); + debugMenu->addAction("FALSE Color Voxels Randomly", this, SLOT(doFalseRandomizeVoxelColors())); + debugMenu->addAction("FALSE Color Voxel Every Other Randomly", this, SLOT(doFalseRandomizeEveryOtherVoxelColors())); + debugMenu->addAction("FALSE Color Voxels by Distance", this, SLOT(doFalseColorizeByDistance())); + debugMenu->addAction("FALSE Color Voxel Out of View", this, SLOT(doFalseColorizeInView())); + debugMenu->addAction("Show TRUE Colors", this, SLOT(doTrueVoxelColors())); + debugMenu->addAction("Calculate Tree Stats", this, SLOT(doTreeStats()), Qt::SHIFT | Qt::Key_S); } -void Application::testSlot() { - qDebug() << "Hello world."; +void Application::updateFrustumRenderModeAction() { + switch (_frustumDrawingMode) { + case FRUSTUM_DRAW_MODE_ALL: + _frustumRenderModeAction->setText("Render Mode - All"); + break; + case FRUSTUM_DRAW_MODE_VECTORS: + _frustumRenderModeAction->setText("Render Mode - Vectors"); + break; + case FRUSTUM_DRAW_MODE_PLANES: + _frustumRenderModeAction->setText("Render Mode - Planes"); + break; + case FRUSTUM_DRAW_MODE_NEAR_PLANE: + _frustumRenderModeAction->setText("Render Mode - Near"); + break; + case FRUSTUM_DRAW_MODE_FAR_PLANE: + _frustumRenderModeAction->setText("Render Mode - Far"); + break; + } } + +void Application::initDisplay() { + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glShadeModel (GL_SMOOTH); + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glEnable(GL_DEPTH_TEST); +} + +void Application::init() { + _voxels.init(); + _voxels.setViewerAvatar(&_myAvatar); + _voxels.setCamera(&_myCamera); + + _environment.init(); + + _handControl.setScreenDimensions(_glWidget->width(), _glWidget->height()); + + _headMouseX = _glWidget->width()/2; + _headMouseY = _glWidget->height()/2; + + _stars.readInput(STAR_FILE, STAR_CACHE_FILE, 0); + + _myAvatar.setPosition(START_LOCATION); + _myCamera.setMode(DEFAULT_CAMERA_MODE); + + OculusManager::connect(); + + gettimeofday(&_timerStart, NULL); + gettimeofday(&_lastTimeIdle, NULL); +} + +static void sendVoxelEditMessage(PACKET_HEADER header, VoxelDetail& detail) { + unsigned char* bufferOut; + int sizeOut; + + if (createVoxelEditMessage(header, 0, 1, &detail, bufferOut, sizeOut)){ + AgentList::getInstance()->broadcastToAgents(bufferOut, sizeOut, &AGENT_TYPE_VOXEL, 1); + delete bufferOut; + } +} + +void Application::updateAvatar(float deltaTime) { + // Update my avatar's head position from gyros + _myAvatar.updateHeadFromGyros(deltaTime, &_serialPort, &_gravity); + + // Grab latest readings from the gyros + float measuredPitchRate = _serialPort.getLastPitchRate(); + float measuredYawRate = _serialPort.getLastYawRate(); + + // Update gyro-based mouse (X,Y on screen) + const float MIN_MOUSE_RATE = 1.0; + const float HORIZONTAL_PIXELS_PER_DEGREE = 2880.f / 45.f; + const float VERTICAL_PIXELS_PER_DEGREE = 1800.f / 30.f; + if (powf(measuredYawRate * measuredYawRate + + measuredPitchRate * measuredPitchRate, 0.5) > MIN_MOUSE_RATE) + { + _headMouseX += measuredYawRate * HORIZONTAL_PIXELS_PER_DEGREE * deltaTime; + _headMouseY -= measuredPitchRate * VERTICAL_PIXELS_PER_DEGREE * deltaTime; + } + _headMouseX = max(_headMouseX, 0); + _headMouseX = min(_headMouseX, _glWidget->width()); + _headMouseY = max(_headMouseY, 0); + _headMouseY = min(_headMouseY, _glWidget->height()); + + // Update head and body pitch and yaw based on measured gyro rates + if (_gyroLook->isChecked()) { + // Render Yaw + float renderYawSpring = fabs(_headMouseX - _glWidget->width() / 2.f) / (_glWidget->width() / 2.f); + const float RENDER_YAW_MULTIPLY = 4.f; + _myAvatar.setRenderYaw((1.f - renderYawSpring * deltaTime) * _myAvatar.getRenderYaw() + + renderYawSpring * deltaTime * -_myAvatar.getHeadYaw() * RENDER_YAW_MULTIPLY); + // Render Pitch + float renderPitchSpring = fabs(_headMouseY - _glWidget->height() / 2.f) / (_glWidget->height() / 2.f); + const float RENDER_PITCH_MULTIPLY = 4.f; + _myAvatar.setRenderPitch((1.f - renderPitchSpring * deltaTime) * _myAvatar.getRenderPitch() + + renderPitchSpring * deltaTime * -_myAvatar.getHeadPitch() * RENDER_PITCH_MULTIPLY); + } + + + if (USING_MOUSE_VIEW_SHIFT) + { + //make it so that when your mouse hits the edge of the screen, the camera shifts + float rightBoundary = (float)_glWidget->width() - MOUSE_VIEW_SHIFT_YAW_MARGIN; + float bottomBoundary = (float)_glWidget->height() - MOUSE_VIEW_SHIFT_PITCH_MARGIN; + + if (_mouseX > rightBoundary) { + float f = (_mouseX - rightBoundary) / ( (float)_glWidget->width() - rightBoundary); + _mouseViewShiftYaw += MOUSE_VIEW_SHIFT_RATE * f * deltaTime; + if (_mouseViewShiftYaw > MOUSE_VIEW_SHIFT_YAW_LIMIT) { _mouseViewShiftYaw = MOUSE_VIEW_SHIFT_YAW_LIMIT; } + } else if (_mouseX < MOUSE_VIEW_SHIFT_YAW_MARGIN) { + float f = 1.0 - (_mouseX / MOUSE_VIEW_SHIFT_YAW_MARGIN); + _mouseViewShiftYaw -= MOUSE_VIEW_SHIFT_RATE * f * deltaTime; + if (_mouseViewShiftYaw < -MOUSE_VIEW_SHIFT_YAW_LIMIT) { _mouseViewShiftYaw = -MOUSE_VIEW_SHIFT_YAW_LIMIT; } + } + if (_mouseY < MOUSE_VIEW_SHIFT_PITCH_MARGIN) { + float f = 1.0 - (_mouseY / MOUSE_VIEW_SHIFT_PITCH_MARGIN); + _mouseViewShiftPitch += MOUSE_VIEW_SHIFT_RATE * f * deltaTime; + if (_mouseViewShiftPitch > MOUSE_VIEW_SHIFT_PITCH_LIMIT ) { _mouseViewShiftPitch = MOUSE_VIEW_SHIFT_PITCH_LIMIT; } + } + else if (_mouseY > bottomBoundary) { + float f = (_mouseY - bottomBoundary) / ((float)_glWidget->height() - bottomBoundary); + _mouseViewShiftPitch -= MOUSE_VIEW_SHIFT_RATE * f * deltaTime; + if (_mouseViewShiftPitch < -MOUSE_VIEW_SHIFT_PITCH_LIMIT) { _mouseViewShiftPitch = -MOUSE_VIEW_SHIFT_PITCH_LIMIT; } + } + } + + if (OculusManager::isConnected()) { + float yaw, pitch, roll; + OculusManager::getEulerAngles(yaw, pitch, roll); + + _myAvatar.setHeadYaw(-yaw); + _myAvatar.setHeadPitch(pitch); + _myAvatar.setHeadRoll(roll); + } + + // Get audio loudness data from audio input device + #ifndef _WIN32 + _myAvatar.setLoudness(_audio.getInputLoudness()); + #endif + + // Update Avatar with latest camera and view frustum data... + // NOTE: we get this from the view frustum, to make it simpler, since the + // loadViewFrumstum() method will get the correct details from the camera + // We could optimize this to not actually load the viewFrustum, since we don't + // actually need to calculate the view frustum planes to send these details + // to the server. + loadViewFrustum(_viewFrustum); + _myAvatar.setCameraPosition(_viewFrustum.getPosition()); + _myAvatar.setCameraDirection(_viewFrustum.getDirection()); + _myAvatar.setCameraUp(_viewFrustum.getUp()); + _myAvatar.setCameraRight(_viewFrustum.getRight()); + _myAvatar.setCameraFov(_viewFrustum.getFieldOfView()); + _myAvatar.setCameraAspectRatio(_viewFrustum.getAspectRatio()); + _myAvatar.setCameraNearClip(_viewFrustum.getNearClip()); + _myAvatar.setCameraFarClip(_viewFrustum.getFarClip()); + + AgentList* agentList = AgentList::getInstance(); + if (agentList->getOwnerID() != UNKNOWN_AGENT_ID) { + // if I know my ID, send head/hand data to the avatar mixer and voxel server + unsigned char broadcastString[200]; + unsigned char* endOfBroadcastStringWrite = broadcastString; + + *(endOfBroadcastStringWrite++) = PACKET_HEADER_HEAD_DATA; + endOfBroadcastStringWrite += packAgentId(endOfBroadcastStringWrite, agentList->getOwnerID()); + + endOfBroadcastStringWrite += _myAvatar.getBroadcastData(endOfBroadcastStringWrite); + + const char broadcastReceivers[2] = {AGENT_TYPE_VOXEL, AGENT_TYPE_AVATAR_MIXER}; + AgentList::getInstance()->broadcastToAgents(broadcastString, endOfBroadcastStringWrite - broadcastString, broadcastReceivers, sizeof(broadcastReceivers)); + } + + // If I'm in paint mode, send a voxel out to VOXEL server agents. + if (_paintOn) { + + glm::vec3 avatarPos = _myAvatar.getPosition(); + + // For some reason, we don't want to flip X and Z here. + _paintingVoxel.x = avatarPos.x / 10.0; + _paintingVoxel.y = avatarPos.y / 10.0; + _paintingVoxel.z = avatarPos.z / 10.0; + + if (_paintingVoxel.x >= 0.0 && _paintingVoxel.x <= 1.0 && + _paintingVoxel.y >= 0.0 && _paintingVoxel.y <= 1.0 && + _paintingVoxel.z >= 0.0 && _paintingVoxel.z <= 1.0) { + + sendVoxelEditMessage(PACKET_HEADER_SET_VOXEL, _paintingVoxel); + } + } +} + +void Application::loadViewFrustum(ViewFrustum& viewFrustum) { + // We will use these below, from either the camera or head vectors calculated above + glm::vec3 position; + glm::vec3 direction; + glm::vec3 up; + glm::vec3 right; + float fov, nearClip, farClip; + + // Camera or Head? + if (_cameraFrustum->isChecked()) { + position = _myCamera.getPosition(); + } else { + position = _myAvatar.getHeadPosition(); + } + + fov = _myCamera.getFieldOfView(); + nearClip = _myCamera.getNearClip(); + farClip = _myCamera.getFarClip(); + + Orientation o = _myCamera.getOrientation(); + + direction = o.getFront(); + up = o.getUp(); + right = o.getRight(); + + /* + printf("position.x=%f, position.y=%f, position.z=%f\n", position.x, position.y, position.z); + printf("yaw=%f, pitch=%f, roll=%f\n", yaw,pitch,roll); + printf("direction.x=%f, direction.y=%f, direction.z=%f\n", direction.x, direction.y, direction.z); + printf("up.x=%f, up.y=%f, up.z=%f\n", up.x, up.y, up.z); + printf("right.x=%f, right.y=%f, right.z=%f\n", right.x, right.y, right.z); + printf("fov=%f\n", fov); + printf("nearClip=%f\n", nearClip); + printf("farClip=%f\n", farClip); + */ + + // Set the viewFrustum up with the correct position and orientation of the camera + viewFrustum.setPosition(position); + viewFrustum.setOrientation(direction,up,right); + + // Also make sure it's got the correct lens details from the camera + viewFrustum.setFieldOfView(fov); + viewFrustum.setNearClip(nearClip); + viewFrustum.setFarClip(farClip); + + // Ask the ViewFrustum class to calculate our corners + viewFrustum.calculate(); +} + +// this shader is an adaptation (HLSL -> GLSL, removed conditional) of the one in the Oculus sample +// code (Samples/OculusRoomTiny/RenderTiny_D3D1X_Device.cpp), which is under the Apache license +// (http://www.apache.org/licenses/LICENSE-2.0) +static const char* DISTORTION_FRAGMENT_SHADER = + "#version 120\n" + "uniform sampler2D texture;" + "uniform vec2 lensCenter;" + "uniform vec2 screenCenter;" + "uniform vec2 scale;" + "uniform vec2 scaleIn;" + "uniform vec4 hmdWarpParam;" + "vec2 hmdWarp(vec2 in01) {" + " vec2 theta = (in01 - lensCenter) * scaleIn;" + " float rSq = theta.x * theta.x + theta.y * theta.y;" + " vec2 theta1 = theta * (hmdWarpParam.x + hmdWarpParam.y * rSq + " + " hmdWarpParam.z * rSq * rSq + hmdWarpParam.w * rSq * rSq * rSq);" + " return lensCenter + scale * theta1;" + "}" + "void main(void) {" + " vec2 tc = hmdWarp(gl_TexCoord[0].st);" + " vec2 below = step(screenCenter.st + vec2(-0.25, -0.5), tc.st);" + " vec2 above = vec2(1.0, 1.0) - step(screenCenter.st + vec2(0.25, 0.5), tc.st);" + " gl_FragColor = mix(vec4(0.0, 0.0, 0.0, 1.0), texture2D(texture, tc), " + " above.s * above.t * below.s * below.t);" + "}"; + +void Application::displayOculus(Camera& whichCamera) { + // magic numbers ahoy! in order to avoid pulling in the Oculus utility library that calculates + // the rendering parameters from the hardware stats, i just folded their calculations into + // constants using the stats for the current-model hardware as contained in the SDK file + // LibOVR/Src/Util/Util_Render_Stereo.cpp + + // eye + + // render the left eye view to the left side of the screen + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + glTranslatef(0.151976, 0, 0); // +h, see Oculus SDK docs p. 26 + gluPerspective(whichCamera.getFieldOfView(), whichCamera.getAspectRatio(), + whichCamera.getNearClip(), whichCamera.getFarClip()); + glTranslatef(0.032, 0, 0); // dip/2, see p. 27 + + glMatrixMode(GL_MODELVIEW); + glViewport(0, 0, _glWidget->width() / 2, _glWidget->height()); + displaySide(whichCamera); + + // and the right eye to the right side + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glTranslatef(-0.151976, 0, 0); // -h + gluPerspective(whichCamera.getFieldOfView(), whichCamera.getAspectRatio(), + whichCamera.getNearClip(), whichCamera.getFarClip()); + glTranslatef(-0.032, 0, 0); + + glMatrixMode(GL_MODELVIEW); + glViewport(_glWidget->width() / 2, 0, _glWidget->width() / 2, _glWidget->height()); + displaySide(whichCamera); + + glPopMatrix(); + + // restore our normal viewport + glViewport(0, 0, _glWidget->width(), _glWidget->height()); + + if (_oculusTextureID == 0) { + glGenTextures(1, &_oculusTextureID); + glBindTexture(GL_TEXTURE_2D, _oculusTextureID); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _glWidget->width(), _glWidget->height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + + _oculusProgram = new ProgramObject(); + _oculusProgram->attachFromSourceCode(GL_FRAGMENT_SHADER_ARB, DISTORTION_FRAGMENT_SHADER); + _oculusProgram->link(); + + _textureLocation = _oculusProgram->getUniformLocation("texture"); + _lensCenterLocation = _oculusProgram->getUniformLocation("lensCenter"); + _screenCenterLocation = _oculusProgram->getUniformLocation("screenCenter"); + _scaleLocation = _oculusProgram->getUniformLocation("scale"); + _scaleInLocation = _oculusProgram->getUniformLocation("scaleIn"); + _hmdWarpParamLocation = _oculusProgram->getUniformLocation("hmdWarpParam"); + + } else { + glBindTexture(GL_TEXTURE_2D, _oculusTextureID); + } + glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, _glWidget->width(), _glWidget->height()); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluOrtho2D(0, _glWidget->width(), 0, _glWidget->height()); + glDisable(GL_DEPTH_TEST); + glDisable(GL_LIGHTING); + + // for reference on setting these values, see SDK file Samples/OculusRoomTiny/RenderTiny_Device.cpp + + float scaleFactor = 1.0 / _oculusDistortionScale; + float aspectRatio = (_glWidget->width() * 0.5) / _glWidget->height(); + + glDisable(GL_BLEND); + glEnable(GL_TEXTURE_2D); + _oculusProgram->bind(); + _oculusProgram->setUniform(_textureLocation, 0); + _oculusProgram->setUniform(_lensCenterLocation, 0.287994, 0.5); // see SDK docs, p. 29 + _oculusProgram->setUniform(_screenCenterLocation, 0.25, 0.5); + _oculusProgram->setUniform(_scaleLocation, 0.25 * scaleFactor, 0.5 * scaleFactor * aspectRatio); + _oculusProgram->setUniform(_scaleInLocation, 4, 2 / aspectRatio); + _oculusProgram->setUniform(_hmdWarpParamLocation, 1.0, 0.22, 0.24, 0); + + glColor3f(1, 0, 1); + glBegin(GL_QUADS); + glTexCoord2f(0, 0); + glVertex2f(0, 0); + glTexCoord2f(0.5, 0); + glVertex2f(_glWidget->width()/2, 0); + glTexCoord2f(0.5, 1); + glVertex2f(_glWidget->width() / 2, _glWidget->height()); + glTexCoord2f(0, 1); + glVertex2f(0, _glWidget->height()); + glEnd(); + + _oculusProgram->setUniform(_lensCenterLocation, 0.787994, 0.5); + _oculusProgram->setUniform(_screenCenterLocation, 0.75, 0.5); + + glBegin(GL_QUADS); + glTexCoord2f(0.5, 0); + glVertex2f(_glWidget->width() / 2, 0); + glTexCoord2f(1, 0); + glVertex2f(_glWidget->width(), 0); + glTexCoord2f(1, 1); + glVertex2f(_glWidget->width(), _glWidget->height()); + glTexCoord2f(0.5, 1); + glVertex2f(_glWidget->width() / 2, _glWidget->height()); + glEnd(); + + glEnable(GL_BLEND); + glDisable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, 0); + _oculusProgram->release(); + + glPopMatrix(); +} + +void Application::displaySide(Camera& whichCamera) { + glPushMatrix(); + + if (_renderStarsOn->isChecked()) { + // should be the first rendering pass - w/o depth buffer / lighting + + // compute starfield alpha based on distance from atmosphere + float alpha = 1.0f; + if (_renderAtmosphereOn->isChecked()) { + float height = glm::distance(whichCamera.getPosition(), _environment.getAtmosphereCenter()); + if (height < _environment.getAtmosphereInnerRadius()) { + alpha = 0.0f; + + } else if (height < _environment.getAtmosphereOuterRadius()) { + alpha = (height - _environment.getAtmosphereInnerRadius()) / + (_environment.getAtmosphereOuterRadius() - _environment.getAtmosphereInnerRadius()); + } + } + + // finally render the starfield + _stars.render(whichCamera.getFieldOfView(), whichCamera.getAspectRatio(), whichCamera.getNearClip(), alpha); + } + + // draw the sky dome + if (_renderAtmosphereOn->isChecked()) { + _environment.renderAtmosphere(whichCamera); + } + + glEnable(GL_LIGHTING); + glEnable(GL_DEPTH_TEST); + + // draw a red sphere + float sphereRadius = 0.25f; + glColor3f(1,0,0); + glPushMatrix(); + glutSolidSphere(sphereRadius, 15, 15); + glPopMatrix(); + + //draw a grid ground plane.... + drawGroundPlaneGrid(10.f); + + // Draw voxels + if (_renderVoxels->isChecked()) { + _voxels.render(); + } + + // indicate what we'll be adding/removing in mouse mode, if anything + if (_mouseVoxel.s != 0) { + glPushMatrix(); + if (_mouseMode == ADD_VOXEL_MODE) { + // use a contrasting color so that we can see what we're doing + glColor3ub(_mouseVoxel.red + 128, _mouseVoxel.green + 128, _mouseVoxel.blue + 128); + } else { + glColor3ub(_mouseVoxel.red, _mouseVoxel.green, _mouseVoxel.blue); + } + glScalef(TREE_SCALE, TREE_SCALE, TREE_SCALE); + glTranslatef(_mouseVoxel.x + _mouseVoxel.s*0.5f, + _mouseVoxel.y + _mouseVoxel.s*0.5f, + _mouseVoxel.z + _mouseVoxel.s*0.5f); + glLineWidth(4.0f); + glutWireCube(_mouseVoxel.s); + glLineWidth(1.0f); + glPopMatrix(); + } + + if (_renderAvatarsOn->isChecked()) { + // Render avatars of other agents + AgentList* agentList = AgentList::getInstance(); + agentList->lock(); + for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { + if (agent->getLinkedData() != NULL && agent->getType() == AGENT_TYPE_AVATAR) { + Avatar *avatar = (Avatar *)agent->getLinkedData(); + avatar->render(0, _myCamera.getPosition()); + } + } + agentList->unlock(); + + // Render my own Avatar + _myAvatar.render(_lookingInMirror, _myCamera.getPosition()); + } + + // Render the world box + if (!_lookingInMirror->isChecked() && _renderStatsOn->isChecked()) { render_world_box(); } + + // brad's frustum for debugging + if (_frustumOn->isChecked()) renderViewFrustum(_viewFrustum); + + glPopMatrix(); +} + +void Application::displayOverlay() { + // Render 2D overlay: I/O level bar graphs and text + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + gluOrtho2D(0, _glWidget->width(), _glWidget->height(), 0); + glDisable(GL_DEPTH_TEST); + glDisable(GL_LIGHTING); + + #ifndef _WIN32 + _audio.render(_glWidget->width(), _glWidget->height()); + _audioScope.render(20, _glWidget->height() - 200); + #endif + + //noiseTest(_glWidget->width(), _glWidget->height()); + + if (DISPLAY_HEAD_MOUSE && !_lookingInMirror->isChecked() && USING_INVENSENSE_MPU9150) { + // Display small target box at center or head mouse target that can also be used to measure LOD + glColor3f(1.0, 1.0, 1.0); + glDisable(GL_LINE_SMOOTH); + const int PIXEL_BOX = 20; + glBegin(GL_LINE_STRIP); + glVertex2f(_headMouseX - PIXEL_BOX/2, _headMouseY - PIXEL_BOX/2); + glVertex2f(_headMouseX + PIXEL_BOX/2, _headMouseY - PIXEL_BOX/2); + glVertex2f(_headMouseX + PIXEL_BOX/2, _headMouseY + PIXEL_BOX/2); + glVertex2f(_headMouseX - PIXEL_BOX/2, _headMouseY + PIXEL_BOX/2); + glVertex2f(_headMouseX - PIXEL_BOX/2, _headMouseY - PIXEL_BOX/2); + glEnd(); + glEnable(GL_LINE_SMOOTH); + } + + // Show detected levels from the serial I/O ADC channel sensors + if (_displayLevels) _serialPort.renderLevels(_glWidget->width(), _glWidget->height()); + + // Display stats and log text onscreen + glLineWidth(1.0f); + glPointSize(1.0f); + + if (_renderStatsOn->isChecked()) { displayStats(); } + if (_logOn->isChecked()) { logger.render(_glWidget->width(), _glWidget->height()); } + + // Show chat entry field + if (_chatEntryOn) { + _chatEntry.render(_glWidget->width(), _glWidget->height()); + } + + // Stats at upper right of screen about who domain server is telling us about + glPointSize(1.0f); + char agents[100]; + + AgentList* agentList = AgentList::getInstance(); + int totalAvatars = 0, totalServers = 0; + + for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { + agent->getType() == AGENT_TYPE_AVATAR ? totalAvatars++ : totalServers++; + } + + sprintf(agents, "Servers: %d, Avatars: %d\n", totalServers, totalAvatars); + drawtext(_glWidget->width() - 150, 20, 0.10, 0, 1.0, 0, agents, 1, 0, 0); + + if (_paintOn) { + + char paintMessage[100]; + sprintf(paintMessage,"Painting (%.3f,%.3f,%.3f/%.3f/%d,%d,%d)", + _paintingVoxel.x, _paintingVoxel.y, _paintingVoxel.z, _paintingVoxel.s, + (unsigned int)_paintingVoxel.red, (unsigned int)_paintingVoxel.green, (unsigned int)_paintingVoxel.blue); + drawtext(_glWidget->width() - 350, 50, 0.10, 0, 1.0, 0, paintMessage, 1, 1, 0); + } + + glPopMatrix(); +} + +void Application::displayStats() { + int statsVerticalOffset = 8; + + char stats[200]; + sprintf(stats, "%3.0f FPS, %d Pkts/sec, %3.2f Mbps", + _fps, _packetsPerSecond, (float)_bytesPerSecond * 8.f / 1000000.f); + drawtext(10, statsVerticalOffset + 15, 0.10f, 0, 1.0, 0, stats); + + std::stringstream voxelStats; + voxelStats.precision(4); + voxelStats << "Voxels Rendered: " << _voxels.getVoxelsRendered() / 1000.f << "K Updated: " << _voxels.getVoxelsUpdated()/1000.f << "K"; + drawtext(10, statsVerticalOffset + 230, 0.10f, 0, 1.0, 0, (char *)voxelStats.str().c_str()); + + voxelStats.str(""); + voxelStats << "Voxels Created: " << _voxels.getVoxelsCreated() / 1000.f << "K (" << _voxels.getVoxelsCreatedPerSecondAverage() / 1000.f + << "Kps) "; + drawtext(10, statsVerticalOffset + 250, 0.10f, 0, 1.0, 0, (char *)voxelStats.str().c_str()); + + voxelStats.str(""); + voxelStats << "Voxels Colored: " << _voxels.getVoxelsColored() / 1000.f << "K (" << _voxels.getVoxelsColoredPerSecondAverage() / 1000.f + << "Kps) "; + drawtext(10, statsVerticalOffset + 270, 0.10f, 0, 1.0, 0, (char *)voxelStats.str().c_str()); + + voxelStats.str(""); + voxelStats << "Voxel Bits Read: " << _voxels.getVoxelsBytesRead() * 8.f / 1000000.f + << "M (" << _voxels.getVoxelsBytesReadPerSecondAverage() * 8.f / 1000000.f << " Mbps)"; + drawtext(10, statsVerticalOffset + 290,0.10f, 0, 1.0, 0, (char *)voxelStats.str().c_str()); + + voxelStats.str(""); + float voxelsBytesPerColored = _voxels.getVoxelsColored() + ? ((float) _voxels.getVoxelsBytesRead() / _voxels.getVoxelsColored()) + : 0; + + voxelStats << "Voxels Bits per Colored: " << voxelsBytesPerColored * 8; + drawtext(10, statsVerticalOffset + 310, 0.10f, 0, 1.0, 0, (char *)voxelStats.str().c_str()); + + Agent *avatarMixer = AgentList::getInstance()->soloAgentOfType(AGENT_TYPE_AVATAR_MIXER); + char avatarMixerStats[200]; + + if (avatarMixer) { + sprintf(avatarMixerStats, "Avatar Mixer: %.f kbps, %.f pps", + roundf(avatarMixer->getAverageKilobitsPerSecond()), + roundf(avatarMixer->getAveragePacketsPerSecond())); + } else { + sprintf(avatarMixerStats, "No Avatar Mixer"); + } + + drawtext(10, statsVerticalOffset + 330, 0.10f, 0, 1.0, 0, avatarMixerStats); + + if (_perfStatsOn) { + // Get the PerfStats group details. We need to allocate and array of char* long enough to hold 1+groups + char** perfStatLinesArray = new char*[PerfStat::getGroupCount()+1]; + int lines = PerfStat::DumpStats(perfStatLinesArray); + int atZ = 150; // arbitrary place on screen that looks good + for (int line=0; line < lines; line++) { + drawtext(10, statsVerticalOffset + atZ, 0.10f, 0, 1.0, 0, perfStatLinesArray[line]); + delete perfStatLinesArray[line]; // we're responsible for cleanup + perfStatLinesArray[line]=NULL; + atZ+=20; // height of a line + } + delete []perfStatLinesArray; // we're responsible for cleanup + } +} + +void Application::renderViewFrustum(ViewFrustum& viewFrustum) { + // Load it with the latest details! + loadViewFrustum(viewFrustum); + + glm::vec3 position = viewFrustum.getPosition(); + glm::vec3 direction = viewFrustum.getDirection(); + glm::vec3 up = viewFrustum.getUp(); + glm::vec3 right = viewFrustum.getRight(); + + // Get ready to draw some lines + glDisable(GL_LIGHTING); + glColor4f(1.0, 1.0, 1.0, 1.0); + glLineWidth(1.0); + glBegin(GL_LINES); + + if (_frustumDrawingMode == FRUSTUM_DRAW_MODE_ALL || _frustumDrawingMode == FRUSTUM_DRAW_MODE_VECTORS) { + // Calculate the origin direction vectors + glm::vec3 lookingAt = position + (direction * 0.2f); + glm::vec3 lookingAtUp = position + (up * 0.2f); + glm::vec3 lookingAtRight = position + (right * 0.2f); + + // Looking At = white + glColor3f(1,1,1); + glVertex3f(position.x, position.y, position.z); + glVertex3f(lookingAt.x, lookingAt.y, lookingAt.z); + + // Looking At Up = purple + glColor3f(1,0,1); + glVertex3f(position.x, position.y, position.z); + glVertex3f(lookingAtUp.x, lookingAtUp.y, lookingAtUp.z); + + // Looking At Right = cyan + glColor3f(0,1,1); + glVertex3f(position.x, position.y, position.z); + glVertex3f(lookingAtRight.x, lookingAtRight.y, lookingAtRight.z); + } + + if (_frustumDrawingMode == FRUSTUM_DRAW_MODE_ALL || _frustumDrawingMode == FRUSTUM_DRAW_MODE_PLANES + || _frustumDrawingMode == FRUSTUM_DRAW_MODE_NEAR_PLANE) { + // Drawing the bounds of the frustum + // viewFrustum.getNear plane - bottom edge + glColor3f(1,0,0); + glVertex3f(viewFrustum.getNearBottomLeft().x, viewFrustum.getNearBottomLeft().y, viewFrustum.getNearBottomLeft().z); + glVertex3f(viewFrustum.getNearBottomRight().x, viewFrustum.getNearBottomRight().y, viewFrustum.getNearBottomRight().z); + + // viewFrustum.getNear plane - top edge + glVertex3f(viewFrustum.getNearTopLeft().x, viewFrustum.getNearTopLeft().y, viewFrustum.getNearTopLeft().z); + glVertex3f(viewFrustum.getNearTopRight().x, viewFrustum.getNearTopRight().y, viewFrustum.getNearTopRight().z); + + // viewFrustum.getNear plane - right edge + glVertex3f(viewFrustum.getNearBottomRight().x, viewFrustum.getNearBottomRight().y, viewFrustum.getNearBottomRight().z); + glVertex3f(viewFrustum.getNearTopRight().x, viewFrustum.getNearTopRight().y, viewFrustum.getNearTopRight().z); + + // viewFrustum.getNear plane - left edge + glVertex3f(viewFrustum.getNearBottomLeft().x, viewFrustum.getNearBottomLeft().y, viewFrustum.getNearBottomLeft().z); + glVertex3f(viewFrustum.getNearTopLeft().x, viewFrustum.getNearTopLeft().y, viewFrustum.getNearTopLeft().z); + } + + if (_frustumDrawingMode == FRUSTUM_DRAW_MODE_ALL || _frustumDrawingMode == FRUSTUM_DRAW_MODE_PLANES + || _frustumDrawingMode == FRUSTUM_DRAW_MODE_FAR_PLANE) { + // viewFrustum.getFar plane - bottom edge + glColor3f(0,1,0); // GREEN!!! + glVertex3f(viewFrustum.getFarBottomLeft().x, viewFrustum.getFarBottomLeft().y, viewFrustum.getFarBottomLeft().z); + glVertex3f(viewFrustum.getFarBottomRight().x, viewFrustum.getFarBottomRight().y, viewFrustum.getFarBottomRight().z); + + // viewFrustum.getFar plane - top edge + glVertex3f(viewFrustum.getFarTopLeft().x, viewFrustum.getFarTopLeft().y, viewFrustum.getFarTopLeft().z); + glVertex3f(viewFrustum.getFarTopRight().x, viewFrustum.getFarTopRight().y, viewFrustum.getFarTopRight().z); + + // viewFrustum.getFar plane - right edge + glVertex3f(viewFrustum.getFarBottomRight().x, viewFrustum.getFarBottomRight().y, viewFrustum.getFarBottomRight().z); + glVertex3f(viewFrustum.getFarTopRight().x, viewFrustum.getFarTopRight().y, viewFrustum.getFarTopRight().z); + + // viewFrustum.getFar plane - left edge + glVertex3f(viewFrustum.getFarBottomLeft().x, viewFrustum.getFarBottomLeft().y, viewFrustum.getFarBottomLeft().z); + glVertex3f(viewFrustum.getFarTopLeft().x, viewFrustum.getFarTopLeft().y, viewFrustum.getFarTopLeft().z); + } + + if (_frustumDrawingMode == FRUSTUM_DRAW_MODE_ALL || _frustumDrawingMode == FRUSTUM_DRAW_MODE_PLANES) { + // RIGHT PLANE IS CYAN + // right plane - bottom edge - viewFrustum.getNear to distant + glColor3f(0,1,1); + glVertex3f(viewFrustum.getNearBottomRight().x, viewFrustum.getNearBottomRight().y, viewFrustum.getNearBottomRight().z); + glVertex3f(viewFrustum.getFarBottomRight().x, viewFrustum.getFarBottomRight().y, viewFrustum.getFarBottomRight().z); + + // right plane - top edge - viewFrustum.getNear to distant + glVertex3f(viewFrustum.getNearTopRight().x, viewFrustum.getNearTopRight().y, viewFrustum.getNearTopRight().z); + glVertex3f(viewFrustum.getFarTopRight().x, viewFrustum.getFarTopRight().y, viewFrustum.getFarTopRight().z); + + // LEFT PLANE IS BLUE + // left plane - bottom edge - viewFrustum.getNear to distant + glColor3f(0,0,1); + glVertex3f(viewFrustum.getNearBottomLeft().x, viewFrustum.getNearBottomLeft().y, viewFrustum.getNearBottomLeft().z); + glVertex3f(viewFrustum.getFarBottomLeft().x, viewFrustum.getFarBottomLeft().y, viewFrustum.getFarBottomLeft().z); + + // 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); + } + + glEnd(); + glEnable(GL_LIGHTING); +} + +void Application::setupPaintingVoxel() { + glm::vec3 avatarPos = _myAvatar.getPosition(); + + _paintingVoxel.x = avatarPos.z/-10.0; // voxel space x is negative z head space + _paintingVoxel.y = avatarPos.y/-10.0; // voxel space y is negative y head space + _paintingVoxel.z = avatarPos.x/-10.0; // voxel space z is negative x head space + _paintingVoxel.s = 1.0/256; + + shiftPaintingColor(); +} + +void Application::shiftPaintingColor() { + // About the color of the paintbrush... first determine the dominant color + _dominantColor = (_dominantColor + 1) % 3; // 0=red,1=green,2=blue + _paintingVoxel.red = (_dominantColor == 0) ? randIntInRange(200, 255) : randIntInRange(40, 100); + _paintingVoxel.green = (_dominantColor == 1) ? randIntInRange(200, 255) : randIntInRange(40, 100); + _paintingVoxel.blue = (_dominantColor == 2) ? randIntInRange(200, 255) : randIntInRange(40, 100); +} + +void Application::addVoxelInFrontOfAvatar() { + VoxelDetail detail; + + glm::vec3 position = (_myAvatar.getPosition() + _myAvatar.getCameraDirection()) * (1.0f / TREE_SCALE); + detail.s = _mouseVoxelScale; + + detail.x = detail.s * floor(position.x / detail.s); + detail.y = detail.s * floor(position.y / detail.s); + detail.z = detail.s * floor(position.z / detail.s); + detail.red = 128; + detail.green = 128; + detail.blue = 128; + + sendVoxelEditMessage(PACKET_HEADER_SET_VOXEL, detail); + + // create the voxel locally so it appears immediately + _voxels.createVoxel(detail.x, detail.y, detail.z, detail.s, detail.red, detail.green, detail.blue); +} + +void Application::addVoxelUnderCursor() { + if (_mouseVoxel.s != 0) { + sendVoxelEditMessage(PACKET_HEADER_SET_VOXEL, _mouseVoxel); + + // create the voxel locally so it appears immediately + _voxels.createVoxel(_mouseVoxel.x, _mouseVoxel.y, _mouseVoxel.z, _mouseVoxel.s, + _mouseVoxel.red, _mouseVoxel.green, _mouseVoxel.blue); + } +} + +void Application::deleteVoxelUnderCursor() { + if (_mouseVoxel.s != 0) { + sendVoxelEditMessage(PACKET_HEADER_ERASE_VOXEL, _mouseVoxel); + + // delete the voxel locally so it disappears immediately + _voxels.deleteVoxelAt(_mouseVoxel.x, _mouseVoxel.y, _mouseVoxel.z, _mouseVoxel.s); + } +} + +void Application::resetSensors() { + _myAvatar.setPosition(START_LOCATION); + _headMouseX = _glWidget->width() / 2; + _headMouseY = _glWidget->height() / 2; + + _myAvatar.reset(); +} + +void Application::attachNewHeadToAgent(Agent *newAgent) { + if (newAgent->getLinkedData() == NULL) { + newAgent->setLinkedData(new Avatar(false)); + } +} + +#ifndef _WIN32 +void Application::audioMixerUpdate(in_addr_t newMixerAddress, in_port_t newMixerPort) { + static_cast(QCoreApplication::instance())->_audio.updateMixerParams(newMixerAddress, newMixerPort); +} +#endif + +// Receive packets from other agents/servers and decide what to do with them! +void* Application::networkReceive(void* args) { + sockaddr senderAddress; + ssize_t bytesReceived; + + Application* app = static_cast(QCoreApplication::instance()); + while (!app->_stopNetworkReceiveThread) { + // check to see if the UI thread asked us to kill the voxel tree. since we're the only thread allowed to do that + if (app->_wantToKillLocalVoxels) { + app->_voxels.killLocalVoxels(); + app->_wantToKillLocalVoxels = false; + } + + if (AgentList::getInstance()->getAgentSocket().receive(&senderAddress, app->_incomingPacket, &bytesReceived)) { + app->_packetCount++; + app->_bytesCount += bytesReceived; + + switch (app->_incomingPacket[0]) { + case PACKET_HEADER_TRANSMITTER_DATA: + // Process UDP packets that are sent to the client from local sensor devices + app->_myAvatar.processTransmitterData(app->_incomingPacket, bytesReceived); + break; + case PACKET_HEADER_VOXEL_DATA: + case PACKET_HEADER_Z_COMMAND: + case PACKET_HEADER_ERASE_VOXEL: + app->_voxels.parseData(app->_incomingPacket, bytesReceived); + break; + case PACKET_HEADER_ENVIRONMENT_DATA: + app->_environment.parseData(app->_incomingPacket, bytesReceived); + break; + case PACKET_HEADER_BULK_AVATAR_DATA: + AgentList::getInstance()->processBulkAgentData(&senderAddress, + app->_incomingPacket, + bytesReceived); + break; + default: + AgentList::getInstance()->processAgentData(&senderAddress, app->_incomingPacket, bytesReceived); + break; + } + } else if (!app->_enableNetworkThread) { + break; + } + } + + if (app->_enableNetworkThread) { + pthread_exit(0); + } + return NULL; +} + diff --git a/interface/src/Application.h b/interface/src/Application.h index c47dc3e33a..1613f734bd 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -9,8 +9,35 @@ #ifndef __interface__Application__ #define __interface__Application__ +#include +#include + #include +#include + +#ifndef _WIN32 +#include "Audio.h" +#endif + +#include "Camera.h" +#include "Environment.h" +#include "HandControl.h" +#include "SerialInterface.h" +#include "Stars.h" +#include "ViewFrustum.h" +#include "VoxelSystem.h" +#include "ui/ChatEntry.h" + +class QAction; +class QGLWidget; +class QKeyEvent; +class QMainWindow; +class QMouseEvent; + +class Agent; +class ProgramObject; + class Application : public QApplication { Q_OBJECT @@ -18,9 +45,185 @@ public: Application(int& argc, char** argv); -public slots: + #ifndef _WIN32 + Audio& getAudio() { return _audio; } + #endif + + void initializeGL(); + void paintGL(); + void resizeGL(int width, int height); + + void keyPressEvent(QKeyEvent* event); + void keyReleaseEvent(QKeyEvent* event); - void testSlot(); + void mouseMoveEvent(QMouseEvent* event); + void mousePressEvent(QMouseEvent* event); + void mouseReleaseEvent(QMouseEvent* event); + +private slots: + + void timer(); + void idle(); + void terminate(); + + void setHead(bool head); + void setNoise(bool noise); + void setFullscreen(bool fullscreen); + + void setRenderFirstPerson(bool firstPerson); + void setOculus(bool oculus); + + void setMenu(bool menu); + + void setFrustumOffset(bool frustumOffset); + void cycleFrustumRenderMode(); + + void setRenderWarnings(bool renderWarnings); + void doKillLocalVoxels(); + void doRandomizeVoxelColors(); + void doFalseRandomizeVoxelColors(); + void doFalseRandomizeEveryOtherVoxelColors(); + void doFalseColorizeByDistance(); + void doFalseColorizeInView(); + void doTrueVoxelColors(); + void doTreeStats(); + +private: + + void initMenu(); + void updateFrustumRenderModeAction(); + void initDisplay(); + void init(); + + void updateAvatar(float deltaTime); + void loadViewFrustum(ViewFrustum& viewFrustum); + + void displayOculus(Camera& whichCamera); + void displaySide(Camera& whichCamera); + void displayOverlay(); + void displayStats(); + + void renderViewFrustum(ViewFrustum& viewFrustum); + + void setupPaintingVoxel(); + void shiftPaintingColor(); + void addVoxelInFrontOfAvatar(); + void addVoxelUnderCursor(); + void deleteVoxelUnderCursor(); + + void resetSensors(); + + static void attachNewHeadToAgent(Agent *newAgent); + #ifndef _WIN32 + static void audioMixerUpdate(in_addr_t newMixerAddress, in_port_t newMixerPort); + #endif + static void* networkReceive(void* args); + + QMainWindow* _window; + QGLWidget* _glWidget; + + QAction* _lookingInMirror; + QAction* _gyroLook; + QAction* _renderVoxels; + QAction* _renderStarsOn; + QAction* _renderAtmosphereOn; + QAction* _renderAvatarsOn; + QAction* _oculusOn; + QAction* _renderStatsOn; + QAction* _logOn; + QAction* _frustumOn; + QAction* _viewFrustumFromOffset; + QAction* _cameraFrustum; + QAction* _frustumRenderModeAction; + + SerialInterface _serialPort; + bool _displayLevels; + + glm::vec3 _gravity; + + // Frame Rate Measurement + int _frameCount; + float _fps; + timeval _applicationStartupTime; + timeval _timerStart, _timerEnd; + timeval _lastTimeIdle; + bool _justStarted; + + Stars _stars; + + VoxelSystem _voxels; + QByteArray _voxelsFilename; + bool _wantToKillLocalVoxels; + + ViewFrustum _viewFrustum; // current state of view frustum, perspective, orientation, etc. + + enum FrustumDrawMode { FRUSTUM_DRAW_MODE_ALL, FRUSTUM_DRAW_MODE_VECTORS, FRUSTUM_DRAW_MODE_PLANES, + FRUSTUM_DRAW_MODE_NEAR_PLANE, FRUSTUM_DRAW_MODE_FAR_PLANE, FRUSTUM_DRAW_MODE_COUNT }; + FrustumDrawMode _frustumDrawingMode; + + float _viewFrustumOffsetYaw; // the following variables control yaw, pitch, roll and distance form regular + float _viewFrustumOffsetPitch; // camera to the offset camera + float _viewFrustumOffsetRoll; + float _viewFrustumOffsetDistance; + float _viewFrustumOffsetUp; + + float _mouseViewShiftYaw; + float _mouseViewShiftPitch; + + Oscilloscope _audioScope; + + Avatar _myAvatar; // The rendered avatar of oneself + Camera _myCamera; // My view onto the world + Camera _viewFrustumOffsetCamera; // The camera we use to sometimes show the view frustum from an offset mode + + Environment _environment; + + int _headMouseX, _headMouseY; + + HandControl _handControl; + + int _mouseX; + int _mouseY; + bool _mousePressed; + + // The current mode for mouse interaction + enum MouseMode { NO_EDIT_MODE, ADD_VOXEL_MODE, DELETE_VOXEL_MODE, COLOR_VOXEL_MODE }; + MouseMode _mouseMode; + VoxelDetail _mouseVoxel; // details of the voxel under the mouse cursor + float _mouseVoxelScale; // the scale for adding/removing voxels + + bool _paintOn; + unsigned char _dominantColor; + VoxelDetail _paintingVoxel; + + bool _perfStatsOn; + + ChatEntry _chatEntry; + bool _chatEntryOn; + + GLuint _oculusTextureID; + ProgramObject* _oculusProgram; + float _oculusDistortionScale; + int _textureLocation; + int _lensCenterLocation; + int _screenCenterLocation; + int _scaleLocation; + int _scaleInLocation; + int _hmdWarpParamLocation; + + #ifndef _WIN32 + Audio _audio; + #endif + + bool _enableNetworkThread; + pthread_t _networkReceiveThread; + bool _stopNetworkReceiveThread; + + unsigned char _incomingPacket[MAX_PACKET_SIZE]; + int _packetCount; + int _packetsPerSecond; + int _bytesPerSecond; + int _bytesCount; }; #endif /* defined(__interface__Application__) */ diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 6fc11d934b..beb5096bd5 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -1559,7 +1559,7 @@ void specialkeyUp(int k, int x, int y) { void specialkey(int k, int x, int y) { if (::chatEntryOn) { - chatEntry.specialKey(k); + return; } @@ -1603,16 +1603,7 @@ void toggleMouseMode(MouseMode mode) { void key(unsigned char k, int x, int y) { if (::chatEntryOn) { - if (chatEntry.key(k)) { - myAvatar.setKeyState(k == '\b' || k == 127 ? // backspace or delete - DELETE_KEY_DOWN : INSERT_KEY_DOWN); - myAvatar.setChatMessage(string(chatEntry.getContents().size(), SOLID_BLOCK_CHAR)); - - } else { - myAvatar.setChatMessage(chatEntry.getContents()); - chatEntry.clear(); - ::chatEntryOn = false; - } + return; } @@ -1777,10 +1768,14 @@ glm::vec3 getFaceVector(BoxFace face) { } } +Application* app; + void idle(void) { timeval check; gettimeofday(&check, NULL); + app->processEvents(); + // Only run simulation code if more than IDLE_SIMULATE_MSECS have passed since last time if (diffclock(&lastTimeIdle, &check) > IDLE_SIMULATE_MSECS) { @@ -2034,6 +2029,14 @@ void audioMixerUpdate(in_addr_t newMixerAddress, in_port_t newMixerPort) { int main(int argc, const char * argv[]) { + Application app(argc, const_cast(argv)); + printLog( "Created QT Application.\n" ); + int exitCode = app.exec(); + printLog("Normal exit.\n"); + return exitCode; + + + gettimeofday(&applicationStartupTime, NULL); printLog("Interface Startup:\n"); @@ -2098,10 +2101,6 @@ int main(int argc, const char * argv[]) { #endif - // we need to create a QApplication instance in order to use Qt's font rendering - Application app(argc, const_cast(argv)); - printLog( "Created QT Application.\n" ); - // Before we render anything, let's set up our viewFrustumOffsetCamera with a sufficiently large // field of view and near and far clip to make it interesting. //viewFrustumOffsetCamera.setFieldOfView(90.0); diff --git a/interface/src/ui/ChatEntry.cpp b/interface/src/ui/ChatEntry.cpp index 2b6144e76d..8263424a0e 100644 --- a/interface/src/ui/ChatEntry.cpp +++ b/interface/src/ui/ChatEntry.cpp @@ -5,9 +5,10 @@ // Created by Andrzej Kapolka on 4/24/13. // Copyright (c) 2013 High Fidelity, Inc. All rights reserved. -#include "InterfaceConfig.h" +#include #include "ChatEntry.h" +#include "InterfaceConfig.h" #include "Util.h" using namespace std; @@ -19,46 +20,47 @@ void ChatEntry::clear() { _cursorPos = 0; } -bool ChatEntry::key(unsigned char k) { - switch (k) { - case '\r': +bool ChatEntry::keyPressEvent(QKeyEvent* event) { + switch (event->key()) { + case Qt::Key_Enter: return false; - case '\b': + case Qt::Key_Backspace: if (_cursorPos != 0) { _contents.erase(_cursorPos - 1, 1); _cursorPos--; } return true; - case 127: // delete + case Qt::Key_Delete: if (_cursorPos < _contents.size()) { _contents.erase(_cursorPos, 1); } return true; - - default: - if (_contents.size() != MAX_CONTENT_LENGTH) { - _contents.insert(_cursorPos, 1, k); - _cursorPos++; - } - return true; - } -} - -void ChatEntry::specialKey(unsigned char k) { - switch (k) { - case GLUT_KEY_LEFT: + + case Qt::Key_Left: if (_cursorPos != 0) { _cursorPos--; } - break; + return true; - case GLUT_KEY_RIGHT: + case Qt::Key_Right: if (_cursorPos != _contents.size()) { _cursorPos++; } - break; + return true; + + default: + QString text = event->text(); + if (text.isEmpty()) { + event->ignore(); + return true; + } + if (_contents.size() != MAX_CONTENT_LENGTH) { + _contents.insert(_cursorPos, 1, text.at(0).toAscii()); + _cursorPos++; + } + return true; } } diff --git a/interface/src/ui/ChatEntry.h b/interface/src/ui/ChatEntry.h index c2f1254c41..6c17690fbd 100644 --- a/interface/src/ui/ChatEntry.h +++ b/interface/src/ui/ChatEntry.h @@ -11,6 +11,8 @@ #include +class QKeyEvent; + class ChatEntry { public: @@ -18,8 +20,7 @@ public: void clear(); - bool key(unsigned char k); - void specialKey(unsigned char k); + bool keyPressEvent(QKeyEvent* event); void render(int screenWidth, int screenHeight); From 1e99389a4bca536dacbef0266b37555cdf44f4e1 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Mon, 13 May 2013 23:15:45 -0700 Subject: [PATCH 03/31] Various fixes, removed the bulk of main.cpp. --- interface/src/Application.cpp | 50 +- interface/src/Application.h | 6 +- interface/src/main.cpp | 2154 +------------------------------- interface/src/ui/ChatEntry.cpp | 9 + interface/src/ui/ChatEntry.h | 2 + 5 files changed, 56 insertions(+), 2165 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 92c9e013f6..d6d829f834 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -216,7 +216,10 @@ Application::Application(int& argc, char** argv) : QRect available = desktop()->availableGeometry(); _window->resize(available.size()); _window->setVisible(true); - _glWidget->setFocus(Qt::ActiveWindowFocusReason); + _glWidget->setFocus(); + + // enable mouse tracking; otherwise, we only get drag events + _glWidget->setMouseTracking(true); // initialization continues in initializeGL when OpenGL context is ready } @@ -410,7 +413,7 @@ void Application::resizeGL(int width, int height) { float aspectRatio = ((float)width/(float)height); // based on screen resize // get the lens details from the current camera - Camera& camera = _viewFrustumFromOffset ? _viewFrustumOffsetCamera : _myCamera; + Camera& camera = _viewFrustumFromOffset->isChecked() ? _viewFrustumOffsetCamera : _myCamera; float nearClip = camera.getNearClip(); float farClip = camera.getFarClip(); float fov; @@ -599,6 +602,7 @@ void Application::keyPressEvent(QKeyEvent* event) { _myAvatar.setDriveKeys(ROT_RIGHT, 1); break; + case Qt::Key_Return: case Qt::Key_Enter: _chatEntryOn = true; _myAvatar.setKeyState(NO_KEY_DOWN); @@ -929,10 +933,6 @@ void Application::setOculus(bool oculus) { resizeGL(_glWidget->width(), _glWidget->height()); } -void Application::setMenu(bool menu) { - _window->menuBar()->setVisible(menu); -} - void Application::setFrustumOffset(bool frustumOffset) { // reshape so that OpenGL will get the right lens details for the camera of choice resizeGL(_glWidget->width(), _glWidget->height()); @@ -982,6 +982,14 @@ void Application::doTreeStats() { _voxels.collectStatsForTreesAndVBOs(); } +void Application::setWantsMonochrome(bool wantsMonochrome) { + _myAvatar.setWantColor(!wantsMonochrome); +} + +void Application::setWantsResIn(bool wantsResIn) { + _myAvatar.setWantResIn(wantsResIn); +} + void Application::initMenu() { QMenuBar* menuBar = new QMenuBar(); _window->setMenuBar(menuBar); @@ -1017,9 +1025,6 @@ void Application::initMenu() { _renderStatsOn->setShortcut(Qt::Key_Slash); (_logOn = toolsMenu->addAction("Log"))->setCheckable(true); _logOn->setChecked(true); - QAction* menuAction = toolsMenu->addAction("Menu", this, SLOT(setMenu(bool)), Qt::Key_M); - menuAction->setCheckable(true); - menuAction->setChecked(true); QMenu* frustumMenu = menuBar->addMenu("Frustum"); (_frustumOn = frustumMenu->addAction("Display Frustum"))->setCheckable(true); @@ -1043,6 +1048,8 @@ void Application::initMenu() { debugMenu->addAction("FALSE Color Voxel Out of View", this, SLOT(doFalseColorizeInView())); debugMenu->addAction("Show TRUE Colors", this, SLOT(doTrueVoxelColors())); debugMenu->addAction("Calculate Tree Stats", this, SLOT(doTreeStats()), Qt::SHIFT | Qt::Key_S); + debugMenu->addAction("Wants Res-In", this, SLOT(setWantsResIn(bool)))->setCheckable(true); + debugMenu->addAction("Wants Monochrome", this, SLOT(setWantsMonochrome(bool)))->setCheckable(true); } void Application::updateFrustumRenderModeAction() { @@ -1236,6 +1243,12 @@ void Application::updateAvatar(float deltaTime) { } } +///////////////////////////////////////////////////////////////////////////////////// +// loadViewFrustum() +// +// Description: this will load the view frustum bounds for EITHER the head +// or the "myCamera". +// void Application::loadViewFrustum(ViewFrustum& viewFrustum) { // We will use these below, from either the camera or head vectors calculated above glm::vec3 position; @@ -1652,6 +1665,24 @@ void Application::displayStats() { } } +///////////////////////////////////////////////////////////////////////////////////// +// renderViewFrustum() +// +// Description: this will render the view frustum bounds for EITHER the head +// or the "myCamera". +// +// Frustum rendering mode. For debug purposes, we allow drawing the frustum in a couple of different ways. +// We can draw it with each of these parts: +// * Origin Direction/Up/Right vectors - these will be drawn at the point of the camera +// * Near plane - this plane is drawn very close to the origin point. +// * Right/Left planes - these two planes are drawn between the near and far planes. +// * Far plane - the plane is drawn in the distance. +// Modes - the following modes, will draw the following parts. +// * All - draws all the parts listed above +// * Planes - draws the planes but not the origin vectors +// * Origin Vectors - draws the origin vectors ONLY +// * Near Plane - draws only the near plane +// * Far Plane - draws only the far plane void Application::renderViewFrustum(ViewFrustum& viewFrustum) { // Load it with the latest details! loadViewFrustum(viewFrustum); @@ -1856,6 +1887,7 @@ void* Application::networkReceive(void* args) { app->_myAvatar.processTransmitterData(app->_incomingPacket, bytesReceived); break; case PACKET_HEADER_VOXEL_DATA: + case PACKET_HEADER_VOXEL_DATA_MONOCHROME: case PACKET_HEADER_Z_COMMAND: case PACKET_HEADER_ERASE_VOXEL: app->_voxels.parseData(app->_incomingPacket, bytesReceived); diff --git a/interface/src/Application.h b/interface/src/Application.h index dfdb9e8726..422b6302e6 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -70,8 +70,6 @@ private slots: void setRenderFirstPerson(bool firstPerson); void setOculus(bool oculus); - void setMenu(bool menu); - void setFrustumOffset(bool frustumOffset); void cycleFrustumRenderMode(); @@ -84,6 +82,8 @@ private slots: void doFalseColorizeInView(); void doTrueVoxelColors(); void doTreeStats(); + void setWantsMonochrome(bool wantsMonochrome); + void setWantsResIn(bool wantsResIn); private: @@ -181,7 +181,7 @@ private: int _mouseX; int _mouseY; - bool _mousePressed; + bool _mousePressed; // true if mouse has been pressed (clear when finished) // The current mode for mouse interaction enum MouseMode { NO_EDIT_MODE, ADD_VOXEL_MODE, DELETE_VOXEL_MODE, COLOR_VOXEL_MODE }; diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 78ecec7ec7..669e64abc1 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -15,2040 +15,8 @@ // Welcome Aboard! // -#include -#include -#include -#include -#include - -#ifdef _WIN32 -#include "Syssocket.h" -#include "Systime.h" -#else -#include -#include -#include -#endif - -#include - -#include -#include -#include - -#include "InterfaceConfig.h" - -#include "Log.h" -#include "shared_Log.h" -#include "voxels_Log.h" -#include "avatars_Log.h" - -#include "world.h" -#include "Util.h" -#ifndef _WIN32 -#include "Audio.h" -#endif - -#include "AngleUtil.h" -#include "Stars.h" - -#include "ui/ChatEntry.h" -#include "ui/MenuRow.h" -#include "ui/MenuColumn.h" -#include "ui/Menu.h" -#include "ui/TextRenderer.h" -#include "renderer/ProgramObject.h" -#include "renderer/ShaderObject.h" - #include "Application.h" -#include "Camera.h" -#include "Avatar.h" -#include -#include -#include "VoxelSystem.h" -#include "Environment.h" -#include "Oscilloscope.h" -#include "UDPSocket.h" -#include "SerialInterface.h" -#include -#include -#include -#include - -#include "ViewFrustum.h" -#include "HandControl.h" -#include "AvatarRenderer.h" -#include "OculusManager.h" - -using namespace std; - -void reshape(int width, int height); // will be defined below -void loadViewFrustum(ViewFrustum& viewFrustum); // will be defined below - -glm::vec3 getGravity(glm::vec3 pos); //get the local gravity vector at this location in the universe - -bool enableNetworkThread = true; -pthread_t networkReceiveThread; -bool stopNetworkReceiveThread = false; - -unsigned char incomingPacket[MAX_PACKET_SIZE]; -int packetCount = 0; -int packetsPerSecond = 0; -int bytesPerSecond = 0; -int bytesCount = 0; - - -int screenWidth = 1200; // Window size -int screenHeight = 800; - -int fullscreen = 0; -float aspectRatio = 1.0f; - -// PER: Jeffrey - please move these our of main.cpp - also these not constants! -float mouseViewShiftYaw = 0.0f; -float mouseViewShiftPitch = 0.0f; -bool USING_MOUSE_VIEW_SHIFT = false; -float MOUSE_VIEW_SHIFT_RATE = 40.0f; -float MOUSE_VIEW_SHIFT_YAW_MARGIN = (float)(::screenWidth * 0.2f); -float MOUSE_VIEW_SHIFT_PITCH_MARGIN = (float)(::screenHeight * 0.2f); -float MOUSE_VIEW_SHIFT_YAW_LIMIT = 45.0; -float MOUSE_VIEW_SHIFT_PITCH_LIMIT = 30.0; - -//CameraMode defaultCameraMode = CAMERA_MODE_FIRST_PERSON; -CameraMode defaultCameraMode = CAMERA_MODE_THIRD_PERSON; - -bool wantColorRandomizer = true; // for addSphere and load file - -Oscilloscope audioScope(256,200,true); - -ViewFrustum viewFrustum; // current state of view frustum, perspective, orientation, etc. - -Avatar myAvatar(true); // The rendered avatar of oneself -Camera myCamera; // My view onto the world -Camera viewFrustumOffsetCamera; // The camera we use to sometimes show the view frustum from an offset mode - -AvatarRenderer avatarRenderer; - -// Starfield information -char starFile[] = "https://s3-us-west-1.amazonaws.com/highfidelity/stars.txt"; -char starCacheFile[] = "cachedStars.txt"; -Stars stars; - -glm::vec3 box(WORLD_SIZE,WORLD_SIZE,WORLD_SIZE); - -VoxelSystem voxels; - -Environment environment; - - -#ifndef _WIN32 -Audio audio(&audioScope, &myAvatar); -#endif - -#define IDLE_SIMULATE_MSECS 16 // How often should call simulate and other stuff - // in the idle loop? (60 FPS is default) - - -glm::vec3 start_location(6.1f, 0, 1.4f); // Where one's own agent begins in the world - // (will be overwritten if avatar data file is found) - -bool renderWarningsOn = false; // Whether to show render pipeline warnings -bool renderStatsOn = false; // Whether to show onscreen text overlay with stats -bool renderVoxels = true; // Whether to render voxels -bool renderStarsOn = true; // Whether to display the stars -bool renderAtmosphereOn = true; // Whether to display the atmosphere -bool renderAvatarsOn = true; // Whether to render avatars -bool renderFirstPersonOn = false; // Whether to render in first person -bool paintOn = false; // Whether to paint voxels as you fly around -VoxelDetail paintingVoxel; // The voxel we're painting if we're painting -unsigned char dominantColor = 0; // The dominant color of the voxel we're painting -bool perfStatsOn = false; // Do we want to display perfStats? -bool wantMonochrome = false; // ask server to send us in monochrome -bool wantResIn = false; // ask server to res in - -bool logOn = true; // Whether to show on-screen log - -bool wantToKillLocalVoxels = false; - -int noiseOn = 0; // Whether to add random noise -float noise = 1.0; // Overall magnitude scaling for random noise levels - -bool gyroLook = true; // Whether to allow the gyro data from head to move your view - -int displayLevels = 0; -bool lookingInMirror = 0; // Are we currently rendering one's own head as if in mirror? - -int displayHeadMouse = 1; // Display sample mouse pointer controlled by head movement -int headMouseX, headMouseY; - -HandControl handControl; - -int mouseX = 0; -int mouseY = 0; - -// Mouse location at start of last down click -int mousePressed = 0; // true if mouse has been pressed (clear when finished) - -// The current mode for mouse interaction -enum MouseMode { NO_EDIT_MODE, ADD_VOXEL_MODE, DELETE_VOXEL_MODE, COLOR_VOXEL_MODE }; -MouseMode mouseMode = NO_EDIT_MODE; -VoxelDetail mouseVoxel; // details of the voxel under the mouse cursor -float mouseVoxelScale = 1.0f / 1024.0f; // the scale for adding/removing voxels - -Menu menu; // main menu -int menuOn = 1; // Whether to show onscreen menu - -ChatEntry chatEntry; // chat entry field -bool chatEntryOn = false; // Whether to show the chat entry - -bool oculusOn = false; // Whether to configure the display for the Oculus Rift -GLuint oculusTextureID = 0; // The texture to which we render for Oculus distortion -ProgramObject* oculusProgram = 0; // The GLSL program containing the distortion shader -float oculusDistortionScale = 1.25; // Controls the Oculus field of viewa - -SerialInterface serialPort; - -glm::vec3 gravity; - -// Frame Rate Measurement - -int frameCount = 0; -float FPS = 120.f; -timeval timerStart, timerEnd; -timeval lastTimeIdle; -double elapsedTime; -timeval applicationStartupTime; -bool justStarted = true; - -// Every second, check the frame rates and other stuff -void Timer(int extra) { - gettimeofday(&timerEnd, NULL); - FPS = (float)frameCount / ((float)diffclock(&timerStart, &timerEnd) / 1000.f); - packetsPerSecond = (float)packetCount / ((float)diffclock(&timerStart, &timerEnd) / 1000.f); - bytesPerSecond = (float)bytesCount / ((float)diffclock(&timerStart, &timerEnd) / 1000.f); - frameCount = 0; - packetCount = 0; - bytesCount = 0; - - glutTimerFunc(1000,Timer,0); - gettimeofday(&timerStart, NULL); - - // if we haven't detected gyros, check for them now - if (!serialPort.active) { - serialPort.pair(); - } -} - -void displayStats(void) { - int statsVerticalOffset = 50; - if (::menuOn == 0) { - statsVerticalOffset = 8; - } - - char stats[200]; - sprintf(stats, "%3.0f FPS, %d Pkts/sec, %3.2f Mbps", - FPS, packetsPerSecond, (float)bytesPerSecond * 8.f / 1000000.f); - drawtext(10, statsVerticalOffset + 15, 0.10f, 0, 1.0, 0, stats); - - std::stringstream voxelStats; - voxelStats.precision(4); - voxelStats << "Voxels Rendered: " << voxels.getVoxelsRendered() / 1000.f << "K Updated: " << voxels.getVoxelsUpdated()/1000.f << "K"; - drawtext(10, statsVerticalOffset + 230, 0.10f, 0, 1.0, 0, (char *)voxelStats.str().c_str()); - - voxelStats.str(""); - voxelStats << "Voxels Created: " << voxels.getVoxelsCreated() / 1000.f << "K (" << voxels.getVoxelsCreatedPerSecondAverage() / 1000.f - << "Kps) "; - drawtext(10, statsVerticalOffset + 250, 0.10f, 0, 1.0, 0, (char *)voxelStats.str().c_str()); - - voxelStats.str(""); - voxelStats << "Voxels Colored: " << voxels.getVoxelsColored() / 1000.f << "K (" << voxels.getVoxelsColoredPerSecondAverage() / 1000.f - << "Kps) "; - drawtext(10, statsVerticalOffset + 270, 0.10f, 0, 1.0, 0, (char *)voxelStats.str().c_str()); - - voxelStats.str(""); - voxelStats << "Voxel Bits Read: " << voxels.getVoxelsBytesRead() * 8.f / 1000000.f - << "M (" << voxels.getVoxelsBytesReadPerSecondAverage() * 8.f / 1000000.f << " Mbps)"; - drawtext(10, statsVerticalOffset + 290,0.10f, 0, 1.0, 0, (char *)voxelStats.str().c_str()); - - voxelStats.str(""); - float voxelsBytesPerColored = voxels.getVoxelsColored() - ? ((float) voxels.getVoxelsBytesRead() / voxels.getVoxelsColored()) - : 0; - - voxelStats << "Voxels Bits per Colored: " << voxelsBytesPerColored * 8; - drawtext(10, statsVerticalOffset + 310, 0.10f, 0, 1.0, 0, (char *)voxelStats.str().c_str()); - - Agent *avatarMixer = AgentList::getInstance()->soloAgentOfType(AGENT_TYPE_AVATAR_MIXER); - char avatarMixerStats[200]; - - if (avatarMixer) { - sprintf(avatarMixerStats, "Avatar Mixer: %.f kbps, %.f pps", - roundf(avatarMixer->getAverageKilobitsPerSecond()), - roundf(avatarMixer->getAveragePacketsPerSecond())); - } else { - sprintf(avatarMixerStats, "No Avatar Mixer"); - } - - drawtext(10, statsVerticalOffset + 330, 0.10f, 0, 1.0, 0, avatarMixerStats); - - if (::perfStatsOn) { - // Get the PerfStats group details. We need to allocate and array of char* long enough to hold 1+groups - char** perfStatLinesArray = new char*[PerfStat::getGroupCount()+1]; - int lines = PerfStat::DumpStats(perfStatLinesArray); - int atZ = 150; // arbitrary place on screen that looks good - for (int line=0; line < lines; line++) { - drawtext(10, statsVerticalOffset + atZ, 0.10f, 0, 1.0, 0, perfStatLinesArray[line]); - delete perfStatLinesArray[line]; // we're responsible for cleanup - perfStatLinesArray[line]=NULL; - atZ+=20; // height of a line - } - delete []perfStatLinesArray; // we're responsible for cleanup - } -} - -void initDisplay(void) { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glShadeModel (GL_SMOOTH); - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - glEnable(GL_DEPTH_TEST); - - if (fullscreen) glutFullScreen(); -} - -void init(void) { - voxels.init(); - voxels.setViewerAvatar(&myAvatar); - voxels.setCamera(&myCamera); - - environment.init(); - - handControl.setScreenDimensions(::screenWidth, ::screenHeight); - - headMouseX = ::screenWidth /2; - headMouseY = ::screenHeight/2; - - stars.readInput(starFile, starCacheFile, 0); - - if (noiseOn) { - myAvatar.setNoise(noise); - } - - myAvatar.setPosition(start_location); - myCamera.setMode(defaultCameraMode); - - OculusManager::connect(); - - gettimeofday(&timerStart, NULL); - gettimeofday(&lastTimeIdle, NULL); -} - -void terminate () { - // Close serial port - // close(serial_fd); - - myAvatar.writeAvatarDataToFile(); - - #ifndef _WIN32 - audio.terminate(); - #endif - - if (enableNetworkThread) { - stopNetworkReceiveThread = true; - pthread_join(networkReceiveThread, NULL); - } - - exit(EXIT_SUCCESS); -} - -void reset_sensors() { - - myAvatar.setPosition(start_location); - headMouseX = ::screenWidth / 2; - headMouseY = ::screenHeight / 2; - - myAvatar.reset(); -} - -void sendVoxelEditMessage(PACKET_HEADER header, VoxelDetail& detail) { - unsigned char* bufferOut; - int sizeOut; - - if (createVoxelEditMessage(header, 0, 1, &detail, bufferOut, sizeOut)){ - AgentList::getInstance()->broadcastToAgents(bufferOut, sizeOut, &AGENT_TYPE_VOXEL, 1); - delete bufferOut; - } -} - -// Using gyro data, update both view frustum and avatar head position -void updateAvatar(float deltaTime) { - - // Update my avatar's head position from gyros - myAvatar.updateHeadFromGyros(deltaTime, &serialPort, &gravity); - - // Grab latest readings from the gyros - float measuredPitchRate = serialPort.getLastPitchRate(); - float measuredYawRate = serialPort.getLastYawRate(); - - // Update gyro-based mouse (X,Y on screen) - const float MIN_MOUSE_RATE = 1.0; - const float HORIZONTAL_PIXELS_PER_DEGREE = 2880.f / 45.f; - const float VERTICAL_PIXELS_PER_DEGREE = 1800.f / 30.f; - if (powf(measuredYawRate * measuredYawRate + - measuredPitchRate * measuredPitchRate, 0.5) > MIN_MOUSE_RATE) - { - headMouseX += measuredYawRate * HORIZONTAL_PIXELS_PER_DEGREE * deltaTime; - headMouseY -= measuredPitchRate * VERTICAL_PIXELS_PER_DEGREE * deltaTime; - } - headMouseX = max(headMouseX, 0); - headMouseX = min(headMouseX, ::screenWidth); - headMouseY = max(headMouseY, 0); - headMouseY = min(headMouseY, ::screenHeight); - - // Update head and body pitch and yaw based on measured gyro rates - if (::gyroLook) { - // Render Yaw - float renderYawSpring = fabs(headMouseX - ::screenWidth / 2.f) / (::screenWidth / 2.f); - const float RENDER_YAW_MULTIPLY = 4.f; - myAvatar.setRenderYaw((1.f - renderYawSpring * deltaTime) * myAvatar.getRenderYaw() + - renderYawSpring * deltaTime * -myAvatar.getHeadYaw() * RENDER_YAW_MULTIPLY); - // Render Pitch - float renderPitchSpring = fabs(headMouseY - ::screenHeight / 2.f) / (::screenHeight / 2.f); - const float RENDER_PITCH_MULTIPLY = 4.f; - myAvatar.setRenderPitch((1.f - renderPitchSpring * deltaTime) * myAvatar.getRenderPitch() + - renderPitchSpring * deltaTime * -myAvatar.getHeadPitch() * RENDER_PITCH_MULTIPLY); - } - - - if (USING_MOUSE_VIEW_SHIFT) - { - //make it so that when your mouse hits the edge of the screen, the camera shifts - float rightBoundary = (float)::screenWidth - MOUSE_VIEW_SHIFT_YAW_MARGIN; - float bottomBoundary = (float)::screenHeight - MOUSE_VIEW_SHIFT_PITCH_MARGIN; - - if (mouseX > rightBoundary) { - float f = (mouseX - rightBoundary) / ( (float)::screenWidth - rightBoundary); - mouseViewShiftYaw += MOUSE_VIEW_SHIFT_RATE * f * deltaTime; - if (mouseViewShiftYaw > MOUSE_VIEW_SHIFT_YAW_LIMIT) { mouseViewShiftYaw = MOUSE_VIEW_SHIFT_YAW_LIMIT; } - } else if (mouseX < MOUSE_VIEW_SHIFT_YAW_MARGIN) { - float f = 1.0 - (mouseX / MOUSE_VIEW_SHIFT_YAW_MARGIN); - mouseViewShiftYaw -= MOUSE_VIEW_SHIFT_RATE * f * deltaTime; - if (mouseViewShiftYaw < -MOUSE_VIEW_SHIFT_YAW_LIMIT) { mouseViewShiftYaw = -MOUSE_VIEW_SHIFT_YAW_LIMIT; } - } - if (mouseY < MOUSE_VIEW_SHIFT_PITCH_MARGIN) { - float f = 1.0 - (mouseY / MOUSE_VIEW_SHIFT_PITCH_MARGIN); - mouseViewShiftPitch += MOUSE_VIEW_SHIFT_RATE * f * deltaTime; - if ( mouseViewShiftPitch > MOUSE_VIEW_SHIFT_PITCH_LIMIT ) { mouseViewShiftPitch = MOUSE_VIEW_SHIFT_PITCH_LIMIT; } - } - else if (mouseY > bottomBoundary) { - float f = (mouseY - bottomBoundary) / ((float)::screenHeight - bottomBoundary); - mouseViewShiftPitch -= MOUSE_VIEW_SHIFT_RATE * f * deltaTime; - if (mouseViewShiftPitch < -MOUSE_VIEW_SHIFT_PITCH_LIMIT) { mouseViewShiftPitch = -MOUSE_VIEW_SHIFT_PITCH_LIMIT; } - } - } - - if (OculusManager::isConnected()) { - float yaw, pitch, roll; - OculusManager::getEulerAngles(yaw, pitch, roll); - - myAvatar.setHeadYaw(-yaw); - myAvatar.setHeadPitch(pitch); - myAvatar.setHeadRoll(roll); - } - - // Get audio loudness data from audio input device - #ifndef _WIN32 - myAvatar.setLoudness(audio.getInputLoudness()); - #endif - - // Update Avatar with latest camera and view frustum data... - // NOTE: we get this from the view frustum, to make it simpler, since the - // loadViewFrumstum() method will get the correct details from the camera - // We could optimize this to not actually load the viewFrustum, since we don't - // actually need to calculate the view frustum planes to send these details - // to the server. - loadViewFrustum(::viewFrustum); - myAvatar.setCameraPosition(::viewFrustum.getPosition()); - myAvatar.setCameraDirection(::viewFrustum.getDirection()); - myAvatar.setCameraUp(::viewFrustum.getUp()); - myAvatar.setCameraRight(::viewFrustum.getRight()); - myAvatar.setCameraFov(::viewFrustum.getFieldOfView()); - myAvatar.setCameraAspectRatio(::viewFrustum.getAspectRatio()); - myAvatar.setCameraNearClip(::viewFrustum.getNearClip()); - myAvatar.setCameraFarClip(::viewFrustum.getFarClip()); - - AgentList* agentList = AgentList::getInstance(); - if (agentList->getOwnerID() != UNKNOWN_AGENT_ID) { - // if I know my ID, send head/hand data to the avatar mixer and voxel server - unsigned char broadcastString[200]; - unsigned char* endOfBroadcastStringWrite = broadcastString; - - *(endOfBroadcastStringWrite++) = PACKET_HEADER_HEAD_DATA; - endOfBroadcastStringWrite += packAgentId(endOfBroadcastStringWrite, agentList->getOwnerID()); - - endOfBroadcastStringWrite += myAvatar.getBroadcastData(endOfBroadcastStringWrite); - - const char broadcastReceivers[2] = {AGENT_TYPE_VOXEL, AGENT_TYPE_AVATAR_MIXER}; - AgentList::getInstance()->broadcastToAgents(broadcastString, endOfBroadcastStringWrite - broadcastString, broadcastReceivers, sizeof(broadcastReceivers)); - } - - // If I'm in paint mode, send a voxel out to VOXEL server agents. - if (::paintOn) { - - glm::vec3 avatarPos = myAvatar.getPosition(); - - // For some reason, we don't want to flip X and Z here. - ::paintingVoxel.x = avatarPos.x / 10.0; - ::paintingVoxel.y = avatarPos.y / 10.0; - ::paintingVoxel.z = avatarPos.z / 10.0; - - if (::paintingVoxel.x >= 0.0 && ::paintingVoxel.x <= 1.0 && - ::paintingVoxel.y >= 0.0 && ::paintingVoxel.y <= 1.0 && - ::paintingVoxel.z >= 0.0 && ::paintingVoxel.z <= 1.0) { - - sendVoxelEditMessage(PACKET_HEADER_SET_VOXEL, ::paintingVoxel); - } - } -} - -///////////////////////////////////////////////////////////////////////////////////// -// loadViewFrustum() -// -// Description: this will load the view frustum bounds for EITHER the head -// or the "myCamera". -// - -// These global scoped variables are used by our loadViewFrustum() and renderViewFrustum functions below, but are also -// available as globals so that the keyboard and menu can manipulate them. - -bool frustumOn = false; // Whether or not to display the debug view frustum -bool cameraFrustum = true; // which frustum to look at - -bool viewFrustumFromOffset =false; // Wether or not to offset the view of the frustum -float viewFrustumOffsetYaw = -135.0; // the following variables control yaw, pitch, roll and distance form regular -float viewFrustumOffsetPitch = 0.0; // camera to the offset camera -float viewFrustumOffsetRoll = 0.0; -float viewFrustumOffsetDistance = 25.0; -float viewFrustumOffsetUp = 0.0; - -void loadViewFrustum(ViewFrustum& viewFrustum) { - // We will use these below, from either the camera or head vectors calculated above - glm::vec3 position; - glm::vec3 direction; - glm::vec3 up; - glm::vec3 right; - float fov, nearClip, farClip; - - // Camera or Head? - if (::cameraFrustum) { - position = ::myCamera.getPosition(); - } else { - position = ::myAvatar.getHeadPosition(); - } - - fov = ::myCamera.getFieldOfView(); - nearClip = ::myCamera.getNearClip(); - farClip = ::myCamera.getFarClip(); - - Orientation o = ::myCamera.getOrientation(); - - direction = o.getFront(); - up = o.getUp(); - right = o.getRight(); - - /* - printf("position.x=%f, position.y=%f, position.z=%f\n", position.x, position.y, position.z); - printf("yaw=%f, pitch=%f, roll=%f\n", yaw,pitch,roll); - printf("direction.x=%f, direction.y=%f, direction.z=%f\n", direction.x, direction.y, direction.z); - printf("up.x=%f, up.y=%f, up.z=%f\n", up.x, up.y, up.z); - printf("right.x=%f, right.y=%f, right.z=%f\n", right.x, right.y, right.z); - printf("fov=%f\n", fov); - printf("nearClip=%f\n", nearClip); - printf("farClip=%f\n", farClip); - */ - - // Set the viewFrustum up with the correct position and orientation of the camera - viewFrustum.setPosition(position); - viewFrustum.setOrientation(direction,up,right); - - // Also make sure it's got the correct lens details from the camera - viewFrustum.setFieldOfView(fov); - viewFrustum.setNearClip(nearClip); - viewFrustum.setFarClip(farClip); - - // Ask the ViewFrustum class to calculate our corners - viewFrustum.calculate(); -} - -///////////////////////////////////////////////////////////////////////////////////// -// renderViewFrustum() -// -// Description: this will render the view frustum bounds for EITHER the head -// or the "myCamera". -// -// Frustum rendering mode. For debug purposes, we allow drawing the frustum in a couple of different ways. -// We can draw it with each of these parts: -// * Origin Direction/Up/Right vectors - these will be drawn at the point of the camera -// * Near plane - this plane is drawn very close to the origin point. -// * Right/Left planes - these two planes are drawn between the near and far planes. -// * Far plane - the plane is drawn in the distance. -// Modes - the following modes, will draw the following parts. -// * All - draws all the parts listed above -// * Planes - draws the planes but not the origin vectors -// * Origin Vectors - draws the origin vectors ONLY -// * Near Plane - draws only the near plane -// * Far Plane - draws only the far plane -#define FRUSTUM_DRAW_MODE_ALL 0 -#define FRUSTUM_DRAW_MODE_VECTORS 1 -#define FRUSTUM_DRAW_MODE_PLANES 2 -#define FRUSTUM_DRAW_MODE_NEAR_PLANE 3 -#define FRUSTUM_DRAW_MODE_FAR_PLANE 4 -#define FRUSTUM_DRAW_MODE_COUNT 5 - -int frustumDrawingMode = FRUSTUM_DRAW_MODE_ALL; // the mode we're drawing the frustum in, see notes above - -void renderViewFrustum(ViewFrustum& viewFrustum) { - - // Load it with the latest details! - loadViewFrustum(viewFrustum); - - glm::vec3 position = viewFrustum.getPosition(); - glm::vec3 direction = viewFrustum.getDirection(); - glm::vec3 up = viewFrustum.getUp(); - glm::vec3 right = viewFrustum.getRight(); - - // Get ready to draw some lines - glDisable(GL_LIGHTING); - glColor4f(1.0, 1.0, 1.0, 1.0); - glLineWidth(1.0); - glBegin(GL_LINES); - - if (::frustumDrawingMode == FRUSTUM_DRAW_MODE_ALL || ::frustumDrawingMode == FRUSTUM_DRAW_MODE_VECTORS) { - // Calculate the origin direction vectors - glm::vec3 lookingAt = position + (direction * 0.2f); - glm::vec3 lookingAtUp = position + (up * 0.2f); - glm::vec3 lookingAtRight = position + (right * 0.2f); - - // Looking At = white - glColor3f(1,1,1); - glVertex3f(position.x, position.y, position.z); - glVertex3f(lookingAt.x, lookingAt.y, lookingAt.z); - - // Looking At Up = purple - glColor3f(1,0,1); - glVertex3f(position.x, position.y, position.z); - glVertex3f(lookingAtUp.x, lookingAtUp.y, lookingAtUp.z); - - // Looking At Right = cyan - glColor3f(0,1,1); - glVertex3f(position.x, position.y, position.z); - glVertex3f(lookingAtRight.x, lookingAtRight.y, lookingAtRight.z); - } - - if (::frustumDrawingMode == FRUSTUM_DRAW_MODE_ALL || ::frustumDrawingMode == FRUSTUM_DRAW_MODE_PLANES - || ::frustumDrawingMode == FRUSTUM_DRAW_MODE_NEAR_PLANE) { - // Drawing the bounds of the frustum - // viewFrustum.getNear plane - bottom edge - glColor3f(1,0,0); - glVertex3f(viewFrustum.getNearBottomLeft().x, viewFrustum.getNearBottomLeft().y, viewFrustum.getNearBottomLeft().z); - glVertex3f(viewFrustum.getNearBottomRight().x, viewFrustum.getNearBottomRight().y, viewFrustum.getNearBottomRight().z); - - // viewFrustum.getNear plane - top edge - glVertex3f(viewFrustum.getNearTopLeft().x, viewFrustum.getNearTopLeft().y, viewFrustum.getNearTopLeft().z); - glVertex3f(viewFrustum.getNearTopRight().x, viewFrustum.getNearTopRight().y, viewFrustum.getNearTopRight().z); - - // viewFrustum.getNear plane - right edge - glVertex3f(viewFrustum.getNearBottomRight().x, viewFrustum.getNearBottomRight().y, viewFrustum.getNearBottomRight().z); - glVertex3f(viewFrustum.getNearTopRight().x, viewFrustum.getNearTopRight().y, viewFrustum.getNearTopRight().z); - - // viewFrustum.getNear plane - left edge - glVertex3f(viewFrustum.getNearBottomLeft().x, viewFrustum.getNearBottomLeft().y, viewFrustum.getNearBottomLeft().z); - glVertex3f(viewFrustum.getNearTopLeft().x, viewFrustum.getNearTopLeft().y, viewFrustum.getNearTopLeft().z); - } - - if (::frustumDrawingMode == FRUSTUM_DRAW_MODE_ALL || ::frustumDrawingMode == FRUSTUM_DRAW_MODE_PLANES - || ::frustumDrawingMode == FRUSTUM_DRAW_MODE_FAR_PLANE) { - // viewFrustum.getFar plane - bottom edge - glColor3f(0,1,0); // GREEN!!! - glVertex3f(viewFrustum.getFarBottomLeft().x, viewFrustum.getFarBottomLeft().y, viewFrustum.getFarBottomLeft().z); - glVertex3f(viewFrustum.getFarBottomRight().x, viewFrustum.getFarBottomRight().y, viewFrustum.getFarBottomRight().z); - - // viewFrustum.getFar plane - top edge - glVertex3f(viewFrustum.getFarTopLeft().x, viewFrustum.getFarTopLeft().y, viewFrustum.getFarTopLeft().z); - glVertex3f(viewFrustum.getFarTopRight().x, viewFrustum.getFarTopRight().y, viewFrustum.getFarTopRight().z); - - // viewFrustum.getFar plane - right edge - glVertex3f(viewFrustum.getFarBottomRight().x, viewFrustum.getFarBottomRight().y, viewFrustum.getFarBottomRight().z); - glVertex3f(viewFrustum.getFarTopRight().x, viewFrustum.getFarTopRight().y, viewFrustum.getFarTopRight().z); - - // viewFrustum.getFar plane - left edge - glVertex3f(viewFrustum.getFarBottomLeft().x, viewFrustum.getFarBottomLeft().y, viewFrustum.getFarBottomLeft().z); - glVertex3f(viewFrustum.getFarTopLeft().x, viewFrustum.getFarTopLeft().y, viewFrustum.getFarTopLeft().z); - } - - if (::frustumDrawingMode == FRUSTUM_DRAW_MODE_ALL || ::frustumDrawingMode == FRUSTUM_DRAW_MODE_PLANES) { - // RIGHT PLANE IS CYAN - // right plane - bottom edge - viewFrustum.getNear to distant - glColor3f(0,1,1); - glVertex3f(viewFrustum.getNearBottomRight().x, viewFrustum.getNearBottomRight().y, viewFrustum.getNearBottomRight().z); - glVertex3f(viewFrustum.getFarBottomRight().x, viewFrustum.getFarBottomRight().y, viewFrustum.getFarBottomRight().z); - - // right plane - top edge - viewFrustum.getNear to distant - glVertex3f(viewFrustum.getNearTopRight().x, viewFrustum.getNearTopRight().y, viewFrustum.getNearTopRight().z); - glVertex3f(viewFrustum.getFarTopRight().x, viewFrustum.getFarTopRight().y, viewFrustum.getFarTopRight().z); - - // LEFT PLANE IS BLUE - // left plane - bottom edge - viewFrustum.getNear to distant - glColor3f(0,0,1); - glVertex3f(viewFrustum.getNearBottomLeft().x, viewFrustum.getNearBottomLeft().y, viewFrustum.getNearBottomLeft().z); - glVertex3f(viewFrustum.getFarBottomLeft().x, viewFrustum.getFarBottomLeft().y, viewFrustum.getFarBottomLeft().z); - - // 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); - } - - glEnd(); - glEnable(GL_LIGHTING); -} - -// displays a single side (left, right, or combined for non-Oculus) -void displaySide(Camera& whichCamera) { - glPushMatrix(); - - if (::renderStarsOn) { - // should be the first rendering pass - w/o depth buffer / lighting - - // compute starfield alpha based on distance from atmosphere - float alpha = 1.0f; - if (::renderAtmosphereOn) { - float height = glm::distance(whichCamera.getPosition(), environment.getAtmosphereCenter()); - if (height < environment.getAtmosphereInnerRadius()) { - alpha = 0.0f; - - } else if (height < environment.getAtmosphereOuterRadius()) { - alpha = (height - environment.getAtmosphereInnerRadius()) / - (environment.getAtmosphereOuterRadius() - environment.getAtmosphereInnerRadius()); - } - } - - // finally render the starfield - stars.render(whichCamera.getFieldOfView(), whichCamera.getAspectRatio(), whichCamera.getNearClip(), alpha); - } - - // draw the sky dome - if (::renderAtmosphereOn) { - environment.renderAtmosphere(whichCamera); - } - - glEnable(GL_LIGHTING); - glEnable(GL_DEPTH_TEST); - - // draw a red sphere - float sphereRadius = 0.25f; - glColor3f(1,0,0); - glPushMatrix(); - glutSolidSphere(sphereRadius, 15, 15); - glPopMatrix(); - - //draw a grid ground plane.... - drawGroundPlaneGrid(10.f); - - // Draw voxels - if (renderVoxels) { - voxels.render(); - } - - // indicate what we'll be adding/removing in mouse mode, if anything - if (::mouseVoxel.s != 0) { - glPushMatrix(); - if (::mouseMode == ADD_VOXEL_MODE) { - // use a contrasting color so that we can see what we're doing - glColor3ub(::mouseVoxel.red + 128, ::mouseVoxel.green + 128, ::mouseVoxel.blue + 128); - } else { - glColor3ub(::mouseVoxel.red, ::mouseVoxel.green, ::mouseVoxel.blue); - } - glScalef(TREE_SCALE, TREE_SCALE, TREE_SCALE); - glTranslatef(::mouseVoxel.x + ::mouseVoxel.s*0.5f, - ::mouseVoxel.y + ::mouseVoxel.s*0.5f, - ::mouseVoxel.z + ::mouseVoxel.s*0.5f); - glLineWidth(4.0f); - glutWireCube(::mouseVoxel.s); - glLineWidth(1.0f); - glPopMatrix(); - } - - if (::renderAvatarsOn) { - // Render avatars of other agents - AgentList* agentList = AgentList::getInstance(); - agentList->lock(); - for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { - if (agent->getLinkedData() != NULL && agent->getType() == AGENT_TYPE_AVATAR) { - Avatar *avatar = (Avatar *)agent->getLinkedData(); - avatar->render(0, ::myCamera.getPosition()); - } - } - agentList->unlock(); - - // Render my own Avatar - myAvatar.render(::lookingInMirror, ::myCamera.getPosition()); - } - - // Render the world box - if (!::lookingInMirror && ::renderStatsOn) { render_world_box(); } - - // brad's frustum for debugging - if (::frustumOn) renderViewFrustum(::viewFrustum); - - glPopMatrix(); -} - -// this shader is an adaptation (HLSL -> GLSL, removed conditional) of the one in the Oculus sample -// code (Samples/OculusRoomTiny/RenderTiny_D3D1X_Device.cpp), which is under the Apache license -// (http://www.apache.org/licenses/LICENSE-2.0) -const char* DISTORTION_FRAGMENT_SHADER = - "#version 120\n" - "uniform sampler2D texture;" - "uniform vec2 lensCenter;" - "uniform vec2 screenCenter;" - "uniform vec2 scale;" - "uniform vec2 scaleIn;" - "uniform vec4 hmdWarpParam;" - "vec2 hmdWarp(vec2 in01) {" - " vec2 theta = (in01 - lensCenter) * scaleIn;" - " float rSq = theta.x * theta.x + theta.y * theta.y;" - " vec2 theta1 = theta * (hmdWarpParam.x + hmdWarpParam.y * rSq + " - " hmdWarpParam.z * rSq * rSq + hmdWarpParam.w * rSq * rSq * rSq);" - " return lensCenter + scale * theta1;" - "}" - "void main(void) {" - " vec2 tc = hmdWarp(gl_TexCoord[0].st);" - " vec2 below = step(screenCenter.st + vec2(-0.25, -0.5), tc.st);" - " vec2 above = vec2(1.0, 1.0) - step(screenCenter.st + vec2(0.25, 0.5), tc.st);" - " gl_FragColor = mix(vec4(0.0, 0.0, 0.0, 1.0), texture2D(texture, tc), " - " above.s * above.t * below.s * below.t);" - "}"; - -// the locations of the uniform variables -int textureLocation; -int lensCenterLocation; -int screenCenterLocation; -int scaleLocation; -int scaleInLocation; -int hmdWarpParamLocation; - -// renders both sides into a texture, then renders the texture to the display with distortion -void displayOculus(Camera& whichCamera) { - // magic numbers ahoy! in order to avoid pulling in the Oculus utility library that calculates - // the rendering parameters from the hardware stats, i just folded their calculations into - // constants using the stats for the current-model hardware as contained in the SDK file - // LibOVR/Src/Util/Util_Render_Stereo.cpp - - // eye - - // render the left eye view to the left side of the screen - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - glTranslatef(0.151976, 0, 0); // +h, see Oculus SDK docs p. 26 - gluPerspective(whichCamera.getFieldOfView(), whichCamera.getAspectRatio(), - whichCamera.getNearClip(), whichCamera.getFarClip()); - glTranslatef(0.032, 0, 0); // dip/2, see p. 27 - - glMatrixMode(GL_MODELVIEW); - glViewport(0, 0, ::screenWidth / 2, ::screenHeight); - displaySide(whichCamera); - - // and the right eye to the right side - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glTranslatef(-0.151976, 0, 0); // -h - gluPerspective(whichCamera.getFieldOfView(), whichCamera.getAspectRatio(), - whichCamera.getNearClip(), whichCamera.getFarClip()); - glTranslatef(-0.032, 0, 0); - - glMatrixMode(GL_MODELVIEW); - glViewport(::screenWidth / 2, 0, ::screenWidth / 2, ::screenHeight); - displaySide(whichCamera); - - glPopMatrix(); - - // restore our normal viewport - glViewport(0, 0, ::screenWidth, ::screenHeight); - - if (::oculusTextureID == 0) { - glGenTextures(1, &::oculusTextureID); - glBindTexture(GL_TEXTURE_2D, ::oculusTextureID); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ::screenWidth, ::screenHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - - ::oculusProgram = new ProgramObject(); - ::oculusProgram->attachFromSourceCode(GL_FRAGMENT_SHADER_ARB, DISTORTION_FRAGMENT_SHADER); - ::oculusProgram->link(); - - textureLocation = ::oculusProgram->getUniformLocation("texture"); - lensCenterLocation = ::oculusProgram->getUniformLocation("lensCenter"); - screenCenterLocation = ::oculusProgram->getUniformLocation("screenCenter"); - scaleLocation = ::oculusProgram->getUniformLocation("scale"); - scaleInLocation = ::oculusProgram->getUniformLocation("scaleIn"); - hmdWarpParamLocation = ::oculusProgram->getUniformLocation("hmdWarpParam"); - - } else { - glBindTexture(GL_TEXTURE_2D, ::oculusTextureID); - } - glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, ::screenWidth, ::screenHeight); - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluOrtho2D(0, ::screenWidth, 0, ::screenHeight); - glDisable(GL_DEPTH_TEST); - glDisable(GL_LIGHTING); - - // for reference on setting these values, see SDK file Samples/OculusRoomTiny/RenderTiny_Device.cpp - - float scaleFactor = 1.0 / ::oculusDistortionScale; - float aspectRatio = (::screenWidth * 0.5) / ::screenHeight; - - glDisable(GL_BLEND); - glEnable(GL_TEXTURE_2D); - ::oculusProgram->bind(); - ::oculusProgram->setUniform(textureLocation, 0); - ::oculusProgram->setUniform(lensCenterLocation, 0.287994, 0.5); // see SDK docs, p. 29 - ::oculusProgram->setUniform(screenCenterLocation, 0.25, 0.5); - ::oculusProgram->setUniform(scaleLocation, 0.25 * scaleFactor, 0.5 * scaleFactor * aspectRatio); - ::oculusProgram->setUniform(scaleInLocation, 4, 2 / aspectRatio); - ::oculusProgram->setUniform(hmdWarpParamLocation, 1.0, 0.22, 0.24, 0); - - glColor3f(1, 0, 1); - glBegin(GL_QUADS); - glTexCoord2f(0, 0); - glVertex2f(0, 0); - glTexCoord2f(0.5, 0); - glVertex2f(::screenWidth/2, 0); - glTexCoord2f(0.5, 1); - glVertex2f(::screenWidth / 2, ::screenHeight); - glTexCoord2f(0, 1); - glVertex2f(0, ::screenHeight); - glEnd(); - - ::oculusProgram->setUniform(lensCenterLocation, 0.787994, 0.5); - ::oculusProgram->setUniform(screenCenterLocation, 0.75, 0.5); - - glBegin(GL_QUADS); - glTexCoord2f(0.5, 0); - glVertex2f(::screenWidth / 2, 0); - glTexCoord2f(1, 0); - glVertex2f(::screenWidth, 0); - glTexCoord2f(1, 1); - glVertex2f(::screenWidth, ::screenHeight); - glTexCoord2f(0.5, 1); - glVertex2f(::screenWidth / 2, ::screenHeight); - glEnd(); - - glEnable(GL_BLEND); - glDisable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, 0); - ::oculusProgram->release(); - - glPopMatrix(); -} - -void displayOverlay() { - // Render 2D overlay: I/O level bar graphs and text - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - gluOrtho2D(0, ::screenWidth, ::screenHeight, 0); - glDisable(GL_DEPTH_TEST); - glDisable(GL_LIGHTING); - - #ifndef _WIN32 - audio.render(::screenWidth, ::screenHeight); - audioScope.render(20, ::screenHeight - 200); - #endif - - //noiseTest(::screenWidth, ::screenHeight); - - if (displayHeadMouse && !::lookingInMirror && USING_INVENSENSE_MPU9150) { - // Display small target box at center or head mouse target that can also be used to measure LOD - glColor3f(1.0, 1.0, 1.0); - glDisable(GL_LINE_SMOOTH); - const int PIXEL_BOX = 20; - glBegin(GL_LINE_STRIP); - glVertex2f(headMouseX - PIXEL_BOX/2, headMouseY - PIXEL_BOX/2); - glVertex2f(headMouseX + PIXEL_BOX/2, headMouseY - PIXEL_BOX/2); - glVertex2f(headMouseX + PIXEL_BOX/2, headMouseY + PIXEL_BOX/2); - glVertex2f(headMouseX - PIXEL_BOX/2, headMouseY + PIXEL_BOX/2); - glVertex2f(headMouseX - PIXEL_BOX/2, headMouseY - PIXEL_BOX/2); - glEnd(); - glEnable(GL_LINE_SMOOTH); - } - - // Show detected levels from the serial I/O ADC channel sensors - if (displayLevels) serialPort.renderLevels(::screenWidth,::screenHeight); - - // Display stats and log text onscreen - glLineWidth(1.0f); - glPointSize(1.0f); - - if (::renderStatsOn) { displayStats(); } - if (::logOn) { logger.render(::screenWidth, ::screenHeight); } - - // Show menu - if (::menuOn) { - glLineWidth(1.0f); - glPointSize(1.0f); - menu.render(::screenWidth,::screenHeight); - } - - // Show chat entry field - if (::chatEntryOn) { - chatEntry.render(::screenWidth, ::screenHeight); - } - - // Stats at upper right of screen about who domain server is telling us about - glPointSize(1.0f); - char agents[100]; - - AgentList* agentList = AgentList::getInstance(); - int totalAvatars = 0, totalServers = 0; - - for (AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { - agent->getType() == AGENT_TYPE_AVATAR ? totalAvatars++ : totalServers++; - } - - sprintf(agents, "Servers: %d, Avatars: %d\n", totalServers, totalAvatars); - drawtext(::screenWidth - 150, 20, 0.10, 0, 1.0, 0, agents, 1, 0, 0); - - if (::paintOn) { - - char paintMessage[100]; - sprintf(paintMessage,"Painting (%.3f,%.3f,%.3f/%.3f/%d,%d,%d)", - ::paintingVoxel.x,::paintingVoxel.y,::paintingVoxel.z,::paintingVoxel.s, - (unsigned int)::paintingVoxel.red,(unsigned int)::paintingVoxel.green,(unsigned int)::paintingVoxel.blue); - drawtext(::screenWidth - 350, 50, 0.10, 0, 1.0, 0, paintMessage, 1, 1, 0); - } - - glPopMatrix(); -} - -void display(void) -{ - PerfStat("display"); - - glEnable(GL_LINE_SMOOTH); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glMatrixMode(GL_MODELVIEW); - - glPushMatrix(); { - glLoadIdentity(); - - // camera settings - if (myCamera.getMode() == CAMERA_MODE_MIRROR) { - myAvatar.setDisplayingHead(true); - myCamera.setUpShift (0.0); - myCamera.setDistance (0.2); - myCamera.setTightness (100.0f); - myCamera.setTargetPosition(myAvatar.getHeadPosition()); - myCamera.setTargetRotation(myAvatar.getBodyYaw() - 180.0f, 0.0f, 0.0f); - - } else if (myCamera.getMode() == CAMERA_MODE_FIRST_PERSON || OculusManager::isConnected()) { - myAvatar.setDisplayingHead(false); - myCamera.setUpShift (0.0f); - myCamera.setDistance (0.0f); - myCamera.setTightness (100.0f); - myCamera.setTargetPosition(myAvatar.getHeadPosition()); - - if (OculusManager::isConnected()) { - myCamera.setTargetRotation(myAvatar.getBodyYaw() + myAvatar.getHeadYaw(), - -myAvatar.getHeadPitch(), - myAvatar.getHeadRoll()); - } else { - myCamera.setTargetRotation(myAvatar.getAbsoluteHeadYaw()- mouseViewShiftYaw, myAvatar.getAbsoluteHeadPitch() + myAvatar.getRenderPitch() + mouseViewShiftPitch, 0.0f); - } - } else if (myCamera.getMode() == CAMERA_MODE_THIRD_PERSON) { - myAvatar.setDisplayingHead(true); - myCamera.setUpShift (-0.2f); - myCamera.setDistance (1.5f); - myCamera.setTightness (8.0f); - myCamera.setTargetPosition(myAvatar.getHeadPosition()); - myCamera.setTargetRotation(myAvatar.getBodyYaw() - mouseViewShiftYaw, mouseViewShiftPitch, 0.0f); - } - - // important... - myCamera.update( 1.f/FPS ); - - // Render anything (like HUD items) that we want to be in 3D but not in worldspace - /* - const float HUD_Z_OFFSET = -5.f; - glPushMatrix(); - glm::vec3 test(0.5, 0.5, 0.5); - glTranslatef(1, 1, HUD_Z_OFFSET); - drawVector(&test); - glPopMatrix(); - */ - - - // Note: whichCamera is used to pick between the normal camera myCamera for our - // main camera, vs, an alternate camera. The alternate camera we support right now - // is the viewFrustumOffsetCamera. But theoretically, we could use this same mechanism - // to add other cameras. - // - // Why have two cameras? Well, one reason is that because in the case of the renderViewFrustum() - // code, we want to keep the state of "myCamera" intact, so we can render what the view frustum of - // myCamera is. But we also want to do meaningful camera transforms on OpenGL for the offset camera - Camera whichCamera = myCamera; - Camera viewFrustumOffsetCamera = myCamera; - - if (::viewFrustumFromOffset && ::frustumOn) { - - // set the camera to third-person view but offset so we can see the frustum - viewFrustumOffsetCamera.setTargetYaw(::viewFrustumOffsetYaw + myAvatar.getBodyYaw()); - viewFrustumOffsetCamera.setPitch (::viewFrustumOffsetPitch ); - viewFrustumOffsetCamera.setRoll (::viewFrustumOffsetRoll ); - viewFrustumOffsetCamera.setUpShift (::viewFrustumOffsetUp ); - viewFrustumOffsetCamera.setDistance (::viewFrustumOffsetDistance); - viewFrustumOffsetCamera.update(1.f/FPS); - whichCamera = viewFrustumOffsetCamera; - } - - // transform view according to whichCamera - // could be myCamera (if in normal mode) - // or could be viewFrustumOffsetCamera if in offset mode - // I changed the ordering here - roll is FIRST (JJV) - - glRotatef ( whichCamera.getRoll(), IDENTITY_FRONT.x, IDENTITY_FRONT.y, IDENTITY_FRONT.z); - glRotatef ( whichCamera.getPitch(), IDENTITY_RIGHT.x, IDENTITY_RIGHT.y, IDENTITY_RIGHT.z); - glRotatef (180.0 - whichCamera.getYaw(), IDENTITY_UP.x, IDENTITY_UP.y, IDENTITY_UP.z ); - - glTranslatef(-whichCamera.getPosition().x, -whichCamera.getPosition().y, -whichCamera.getPosition().z); - - // Setup 3D lights (after the camera transform, so that they are positioned in world space) - glEnable(GL_COLOR_MATERIAL); - glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); - - GLfloat light_position0[] = { 1.0, 1.0, 0.0, 0.0 }; - glLightfv(GL_LIGHT0, GL_POSITION, light_position0); - GLfloat ambient_color[] = { 0.7, 0.7, 0.8 }; - glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color); - GLfloat diffuse_color[] = { 0.8, 0.7, 0.7 }; - glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_color); - GLfloat specular_color[] = { 1.0, 1.0, 1.0, 1.0}; - glLightfv(GL_LIGHT0, GL_SPECULAR, specular_color); - - glMaterialfv(GL_FRONT, GL_SPECULAR, specular_color); - glMateriali(GL_FRONT, GL_SHININESS, 96); - - if (::oculusOn) { - displayOculus(whichCamera); - - } else { - displaySide(whichCamera); - glPopMatrix(); - - displayOverlay(); - } - } - - glutSwapBuffers(); - frameCount++; - - // If application has just started, report time from startup to now (first frame display) - if (justStarted) { - float startupTime = (usecTimestampNow() - usecTimestamp(&applicationStartupTime))/1000000.0; - justStarted = false; - char title[30]; - snprintf(title, 30, "Interface: %4.2f seconds", startupTime); - glutSetWindowTitle(title); - } -} - -// int version of setValue() -int setValue(int state, int *value) { - if (state == MENU_ROW_PICKED) { - *value = !(*value); - } else if (state == MENU_ROW_GET_VALUE) { - return *value; - } else { - *value = state; - } - return *value; -} - -// bool version of setValue() -int setValue(int state, bool *value) { - if (state == MENU_ROW_PICKED) { - *value = !(*value); - } else if (state == MENU_ROW_GET_VALUE) { - return *value; - } else { - *value = state; - } - return *value; -} - -int setHead(int state) { - return setValue(state, &::lookingInMirror); -} - -int setNoise(int state) { - int iRet = setValue(state, &noiseOn); - if (noiseOn) { - myAvatar.setNoise(noise); - } else { - myAvatar.setNoise(0); - } - return iRet; -} - -int setLog(int state) { - int iRet = setValue(state, &::logOn); - return iRet; -} - -int setGyroLook(int state) { - int iRet = setValue(state, &::gyroLook); - return iRet; -} - -int setFullscreen(int state) { - bool wasFullscreen = ::fullscreen; - int value = setValue(state, &::fullscreen); - if (::fullscreen != wasFullscreen) { - if (::fullscreen) { - glutFullScreen(); - - } else { - glutReshapeWindow(::screenWidth, ::screenHeight); - } - } - return value; -} - -int setVoxels(int state) { - return setValue(state, &::renderVoxels); -} - -int setStars(int state) { - return setValue(state, &::renderStarsOn); -} - -int setAtmosphere(int state) { - return setValue(state, &::renderAtmosphereOn); -} - -int setRenderAvatars(int state) { - return setValue(state, &::renderAvatarsOn); -} - -int setRenderFirstPerson(int state) { - bool value = setValue(state, &::renderFirstPersonOn); - if (state == MENU_ROW_PICKED) { - if (::renderFirstPersonOn) { - myCamera.setMode(CAMERA_MODE_FIRST_PERSON); - } else { - myCamera.setMode(CAMERA_MODE_THIRD_PERSON); - } - } - return value; -} - -int setOculus(int state) { - bool wasOn = ::oculusOn; - int value = setValue(state, &::oculusOn); - if (::oculusOn != wasOn) { - reshape(::screenWidth, ::screenHeight); - } - return value; -} - -int setStats(int state) { - return setValue(state, &::renderStatsOn); -} - -int setMenu(int state) { - return setValue(state, &::menuOn); -} - -int setRenderWarnings(int state) { - int value = setValue(state, &::renderWarningsOn); - if (state == MENU_ROW_PICKED) { - ::voxels.setRenderPipelineWarnings(::renderWarningsOn); - } - return value; -} - -int setWantResIn(int state) { - int value = setValue(state, &::wantResIn); - if (state == MENU_ROW_PICKED) { - ::myAvatar.setWantResIn(::wantResIn); - } - return value; -} - -int setWantMonochrome(int state) { - int value = setValue(state, &::wantMonochrome); - if (state == MENU_ROW_PICKED) { - ::myAvatar.setWantColor(!::wantMonochrome); - } - return value; -} - -int setDisplayFrustum(int state) { - return setValue(state, &::frustumOn); -} - -int setFrustumOffset(int state) { - int value = setValue(state, &::viewFrustumFromOffset); - - // reshape so that OpenGL will get the right lens details for the camera of choice - if (state == MENU_ROW_PICKED) { - reshape(::screenWidth, ::screenHeight); - } - - return value; -} - -int setFrustumOrigin(int state) { - return setValue(state, &::cameraFrustum); -} - -int quitApp(int state) { - if (state == MENU_ROW_PICKED) { - ::terminate(); - } - return 2; // non state so menu class doesn't add "state" -} - -int setFrustumRenderMode(int state) { - if (state == MENU_ROW_PICKED) { - ::frustumDrawingMode = (::frustumDrawingMode+1)%FRUSTUM_DRAW_MODE_COUNT; - } - return ::frustumDrawingMode; -} - -int doKillLocalVoxels(int state) { - if (state == MENU_ROW_PICKED) { - ::wantToKillLocalVoxels = true; - } - return state; -} - -int doRandomizeVoxelColors(int state) { - if (state == MENU_ROW_PICKED) { - ::voxels.randomizeVoxelColors(); - } - return state; -} - -int doFalseRandomizeEveryOtherVoxelColors(int state) { - if (state == MENU_ROW_PICKED) { - ::voxels.falseColorizeRandomEveryOther(); - } - return state; -} - -int doTreeStats(int state) { - if (state == MENU_ROW_PICKED) { - ::voxels.collectStatsForTreesAndVBOs(); - } - return state; -} - -int doFalseRandomizeVoxelColors(int state) { - if (state == MENU_ROW_PICKED) { - ::voxels.falseColorizeRandom(); - } - return state; -} - -int doTrueVoxelColors(int state) { - if (state == MENU_ROW_PICKED) { - ::voxels.trueColorize(); - } - return state; -} - -int doFalseColorizeByDistance(int state) { - if (state == MENU_ROW_PICKED) { - loadViewFrustum(::viewFrustum); - voxels.falseColorizeDistanceFromView(&::viewFrustum); - } - return state; -} - -int doFalseColorizeInView(int state) { - if (state == MENU_ROW_PICKED) { - loadViewFrustum(::viewFrustum); - // we probably want to make sure the viewFrustum is initialized first - voxels.falseColorizeInView(&::viewFrustum); - } - return state; -} - - - -const char* modeAll = " - All "; -const char* modeVectors = " - Vectors "; -const char* modePlanes = " - Planes "; -const char* modeNear = " - Near "; -const char* modeFar = " - Far "; - -const char* getFrustumRenderModeName(int state) { - const char * mode; - switch (state) { - case FRUSTUM_DRAW_MODE_ALL: - mode = modeAll; - break; - case FRUSTUM_DRAW_MODE_VECTORS: - mode = modeVectors; - break; - case FRUSTUM_DRAW_MODE_PLANES: - mode = modePlanes; - break; - case FRUSTUM_DRAW_MODE_NEAR_PLANE: - mode = modeNear; - break; - case FRUSTUM_DRAW_MODE_FAR_PLANE: - mode = modeFar; - break; - } - return mode; -} - -void initMenu() { - MenuColumn *menuColumnOptions, *menuColumnRender, *menuColumnTools, *menuColumnDebug, *menuColumnFrustum; - // Options - menuColumnOptions = menu.addColumn("Options"); - menuColumnOptions->addRow("Mirror (h)", setHead); - menuColumnOptions->addRow("Noise (n)", setNoise); - menuColumnOptions->addRow("Gyro Look", setGyroLook); - menuColumnOptions->addRow("Fullscreen (f)", setFullscreen); - menuColumnOptions->addRow("Quit (q)", quitApp); - - // Render - menuColumnRender = menu.addColumn("Render"); - menuColumnRender->addRow("Voxels (V)", setVoxels); - menuColumnRender->addRow("Stars (*)", setStars); - menuColumnRender->addRow("Atmosphere (A)", setAtmosphere); - menuColumnRender->addRow("Avatars", setRenderAvatars); - menuColumnRender->addRow("First Person (p)", setRenderFirstPerson); - menuColumnRender->addRow("Oculus (o)", setOculus); - - // Tools - menuColumnTools = menu.addColumn("Tools"); - menuColumnTools->addRow("Stats (/)", setStats); - menuColumnTools->addRow("Log ", setLog); - menuColumnTools->addRow("(M)enu", setMenu); - - // Frustum Options - menuColumnFrustum = menu.addColumn("Frustum"); - menuColumnFrustum->addRow("Display (F)rustum", setDisplayFrustum); - menuColumnFrustum->addRow("Use (O)ffset Camera", setFrustumOffset); - menuColumnFrustum->addRow("Switch (C)amera", setFrustumOrigin); - menuColumnFrustum->addRow("(R)ender Mode", setFrustumRenderMode, getFrustumRenderModeName); - - // Debug - menuColumnDebug = menu.addColumn("Debug"); - menuColumnDebug->addRow("Show Render Pipeline Warnings", setRenderWarnings); - menuColumnDebug->addRow("Kill Local Voxels", doKillLocalVoxels); - menuColumnDebug->addRow("Randomize Voxel TRUE Colors", doRandomizeVoxelColors); - menuColumnDebug->addRow("FALSE Color Voxels Randomly", doFalseRandomizeVoxelColors); - menuColumnDebug->addRow("FALSE Color Voxel Every Other Randomly", doFalseRandomizeEveryOtherVoxelColors); - menuColumnDebug->addRow("FALSE Color Voxels by Distance", doFalseColorizeByDistance); - menuColumnDebug->addRow("FALSE Color Voxel Out of View", doFalseColorizeInView); - menuColumnDebug->addRow("Show TRUE Colors", doTrueVoxelColors); - menuColumnDebug->addRow("Calculate Tree Stats", doTreeStats); - menuColumnDebug->addRow("Wants Res-In", setWantResIn); - menuColumnDebug->addRow("Wants Monochrome", setWantMonochrome); -} - -void testPointToVoxel() { - float y=0; - float z=0; - float s=0.1; - for (float x=0; x<=1; x+= 0.05) { - printLog(" x=%f"); - - unsigned char red = 200; //randomColorValue(65); - unsigned char green = 200; //randomColorValue(65); - unsigned char blue = 200; //randomColorValue(65); - - unsigned char* voxelCode = pointToVoxel(x, y, z, s,red,green,blue); - printVoxelCode(voxelCode); - delete voxelCode; - printLog("\n"); - } -} - -void sendVoxelServerEraseAll() { - char message[100]; - sprintf(message,"%c%s",'Z',"erase all"); - int messageSize = strlen(message) + 1; - AgentList::getInstance()->broadcastToAgents((unsigned char*) message, messageSize, &AGENT_TYPE_VOXEL, 1); -} - -void sendVoxelServerAddScene() { - char message[100]; - sprintf(message,"%c%s",'Z',"add scene"); - int messageSize = strlen(message) + 1; - AgentList::getInstance()->broadcastToAgents((unsigned char*)message, messageSize, &AGENT_TYPE_VOXEL, 1); -} - -void shiftPaintingColor() -{ - // About the color of the paintbrush... first determine the dominant color - ::dominantColor = (::dominantColor+1)%3; // 0=red,1=green,2=blue - ::paintingVoxel.red = (::dominantColor==0)?randIntInRange(200,255):randIntInRange(40,100); - ::paintingVoxel.green = (::dominantColor==1)?randIntInRange(200,255):randIntInRange(40,100); - ::paintingVoxel.blue = (::dominantColor==2)?randIntInRange(200,255):randIntInRange(40,100); -} - -void setupPaintingVoxel() { - glm::vec3 avatarPos = myAvatar.getPosition(); - - ::paintingVoxel.x = avatarPos.z/-10.0; // voxel space x is negative z head space - ::paintingVoxel.y = avatarPos.y/-10.0; // voxel space y is negative y head space - ::paintingVoxel.z = avatarPos.x/-10.0; // voxel space z is negative x head space - ::paintingVoxel.s = 1.0/256; - - shiftPaintingColor(); -} - -void addVoxelInFrontOfAvatar() { - VoxelDetail detail; - - glm::vec3 position = (myAvatar.getPosition() + myAvatar.getCameraDirection()) * (1.0f / TREE_SCALE); - detail.s = ::mouseVoxelScale; - - detail.x = detail.s * floor(position.x / detail.s); - detail.y = detail.s * floor(position.y / detail.s); - detail.z = detail.s * floor(position.z / detail.s); - detail.red = 128; - detail.green = 128; - detail.blue = 128; - - sendVoxelEditMessage(PACKET_HEADER_SET_VOXEL, detail); - - // create the voxel locally so it appears immediately - voxels.createVoxel(detail.x, detail.y, detail.z, detail.s, detail.red, detail.green, detail.blue); -} - -void addVoxelUnderCursor() { - if (::mouseVoxel.s != 0) { - sendVoxelEditMessage(PACKET_HEADER_SET_VOXEL, ::mouseVoxel); - - // create the voxel locally so it appears immediately - voxels.createVoxel(::mouseVoxel.x, ::mouseVoxel.y, ::mouseVoxel.z, ::mouseVoxel.s, - ::mouseVoxel.red, ::mouseVoxel.green, ::mouseVoxel.blue); - } -} - -void deleteVoxelUnderCursor() { - if (::mouseVoxel.s != 0) { - sendVoxelEditMessage(PACKET_HEADER_ERASE_VOXEL, ::mouseVoxel); - - // delete the voxel locally so it disappears immediately - voxels.deleteVoxelAt(::mouseVoxel.x, ::mouseVoxel.y, ::mouseVoxel.z, ::mouseVoxel.s); - } -} - -const float KEYBOARD_YAW_RATE = 0.8; -const float KEYBOARD_PITCH_RATE = 0.6; -const float KEYBOARD_STRAFE_RATE = 0.03; -const float KEYBOARD_FLY_RATE = 0.08; - -void specialkeyUp(int k, int x, int y) { - if (k == GLUT_KEY_UP) { - myAvatar.setDriveKeys(FWD, 0); - myAvatar.setDriveKeys(UP, 0); - } - if (k == GLUT_KEY_DOWN) { - myAvatar.setDriveKeys(BACK, 0); - myAvatar.setDriveKeys(DOWN, 0); - } - if (k == GLUT_KEY_LEFT) { - myAvatar.setDriveKeys(LEFT, 0); - myAvatar.setDriveKeys(ROT_LEFT, 0); - } - if (k == GLUT_KEY_RIGHT) { - myAvatar.setDriveKeys(RIGHT, 0); - myAvatar.setDriveKeys(ROT_RIGHT, 0); - } -} - -void specialkey(int k, int x, int y) { - if (::chatEntryOn) { - - return; - } - - if (k == GLUT_KEY_UP || k == GLUT_KEY_DOWN || k == GLUT_KEY_LEFT || k == GLUT_KEY_RIGHT) { - if (k == GLUT_KEY_UP) { - if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) myAvatar.setDriveKeys(UP, 1); - else myAvatar.setDriveKeys(FWD, 1); - } - if (k == GLUT_KEY_DOWN) { - if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) myAvatar.setDriveKeys(DOWN, 1); - else myAvatar.setDriveKeys(BACK, 1); - } - if (k == GLUT_KEY_LEFT) { - if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) myAvatar.setDriveKeys(LEFT, 1); - else myAvatar.setDriveKeys(ROT_LEFT, 1); - } - if (k == GLUT_KEY_RIGHT) { - if (glutGetModifiers() == GLUT_ACTIVE_SHIFT) myAvatar.setDriveKeys(RIGHT, 1); - else myAvatar.setDriveKeys(ROT_RIGHT, 1); - } - } -} - -void keyUp(unsigned char k, int x, int y) { - if (::chatEntryOn) { - myAvatar.setKeyState(NO_KEY_DOWN); - return; - } - - if (k == 'e') myAvatar.setDriveKeys(UP, 0); - if (k == 'c') myAvatar.setDriveKeys(DOWN, 0); - if (k == 'w') myAvatar.setDriveKeys(FWD, 0); - if (k == 's') myAvatar.setDriveKeys(BACK, 0); - if (k == 'a') myAvatar.setDriveKeys(ROT_LEFT, 0); - if (k == 'd') myAvatar.setDriveKeys(ROT_RIGHT, 0); -} - -void toggleMouseMode(MouseMode mode) { - ::mouseMode = (::mouseMode == mode) ? NO_EDIT_MODE : mode; -} - -void key(unsigned char k, int x, int y) { - if (::chatEntryOn) { - - return; - } - - // Process keypresses - - if (k == 'S') { - ::voxels.collectStatsForTreesAndVBOs(); - } - - if (k == 'q' || k == 'Q') ::terminate(); - if (k == '/') ::renderStatsOn = !::renderStatsOn; // toggle stats - if (k == '*') ::renderStarsOn = !::renderStarsOn; // toggle stars - if (k == 'V' || k == 'v') ::renderVoxels = !::renderVoxels; // toggle voxels - if (k == 'A') ::renderAtmosphereOn = !::renderAtmosphereOn; - if (k == 'F') ::frustumOn = !::frustumOn; // toggle view frustum debugging - if (k == 'C') ::cameraFrustum = !::cameraFrustum; // toggle which frustum to look at - if (k == 'O' || k == 'G') setFrustumOffset(MENU_ROW_PICKED); // toggle view frustum offset debugging - if (k == 'f') setFullscreen(!::fullscreen); - if (k == 'o') setOculus(!::oculusOn); - if (k == 'p') setRenderFirstPerson(MENU_ROW_PICKED); - if (k == '[') ::viewFrustumOffsetYaw -= 0.5; - if (k == ']') ::viewFrustumOffsetYaw += 0.5; - if (k == '{') ::viewFrustumOffsetPitch -= 0.5; - if (k == '}') ::viewFrustumOffsetPitch += 0.5; - if (k == '(') ::viewFrustumOffsetRoll -= 0.5; - if (k == ')') ::viewFrustumOffsetRoll += 0.5; - if (k == '<') ::viewFrustumOffsetDistance -= 0.5; - if (k == '>') ::viewFrustumOffsetDistance += 0.5; - if (k == ',') ::viewFrustumOffsetUp -= 0.05; - if (k == '.') ::viewFrustumOffsetUp += 0.05; - -// if (k == '|') ViewFrustum::fovAngleAdust -= 0.05; -// if (k == '\\') ViewFrustum::fovAngleAdust += 0.05; - - if (k == 'R') setFrustumRenderMode(MENU_ROW_PICKED); - - if (k == '&') { - ::paintOn = !::paintOn; // toggle paint - ::setupPaintingVoxel(); // also randomizes colors - } - if (k == '^') ::shiftPaintingColor(); // shifts randomize color between R,G,B dominant - if (k == '-') ::sendVoxelServerEraseAll(); // sends erase all command to voxel server - if (k == '%') ::sendVoxelServerAddScene(); // sends add scene command to voxel server - if (k == '1') ::mouseMode = (::mouseMode == ADD_VOXEL_MODE) ? NO_EDIT_MODE : ADD_VOXEL_MODE; - if (k == '2') ::mouseMode = (::mouseMode == DELETE_VOXEL_MODE) ? NO_EDIT_MODE : DELETE_VOXEL_MODE; - if (k == '3') ::mouseMode = (::mouseMode == COLOR_VOXEL_MODE) ? NO_EDIT_MODE : COLOR_VOXEL_MODE; - if (k == '4') addVoxelInFrontOfAvatar(); - if (k == '5') ::mouseVoxelScale /= 2; - if (k == '6') ::mouseVoxelScale *= 2; - if (k == 'n' || k == 'N') - { - noiseOn = !noiseOn; // Toggle noise - if (noiseOn) - { - myAvatar.setNoise(noise); - } - else - { - myAvatar.setNoise(0); - } - } - - if (k == 'h') { - ::lookingInMirror = !::lookingInMirror; - #ifndef _WIN32 - audio.setMixerLoopbackFlag(::lookingInMirror); - - if (::lookingInMirror) { - myCamera.setMode(CAMERA_MODE_MIRROR); - } else { - myCamera.setMode(defaultCameraMode); - } - #endif - } - - if (k == 'm' || k == 'M') setMenu(MENU_ROW_PICKED); - - if (k == 'l') displayLevels = !displayLevels; - if (k == 'e') myAvatar.setDriveKeys(UP, 1); - if (k == 'c') myAvatar.setDriveKeys(DOWN, 1); - if (k == 'w') myAvatar.setDriveKeys(FWD, 1); - if (k == 's') myAvatar.setDriveKeys(BACK, 1); - if (k == ' ') reset_sensors(); - if (k == 'a') myAvatar.setDriveKeys(ROT_LEFT, 1); - if (k == 'd') myAvatar.setDriveKeys(ROT_RIGHT, 1); - - if (k == '\r') { - ::chatEntryOn = true; - myAvatar.setKeyState(NO_KEY_DOWN); - myAvatar.setChatMessage(string()); - } -} - -// Receive packets from other agents/servers and decide what to do with them! -void* networkReceive(void* args) { - sockaddr senderAddress; - ssize_t bytesReceived; - - while (!stopNetworkReceiveThread) { - // check to see if the UI thread asked us to kill the voxel tree. since we're the only thread allowed to do that - if (::wantToKillLocalVoxels) { - ::voxels.killLocalVoxels(); - ::wantToKillLocalVoxels = false; - } - - if (AgentList::getInstance()->getAgentSocket().receive(&senderAddress, incomingPacket, &bytesReceived)) { - packetCount++; - bytesCount += bytesReceived; - - switch (incomingPacket[0]) { - case PACKET_HEADER_TRANSMITTER_DATA: - // Process UDP packets that are sent to the client from local sensor devices - myAvatar.processTransmitterData(incomingPacket, bytesReceived); - break; - case PACKET_HEADER_VOXEL_DATA: - case PACKET_HEADER_VOXEL_DATA_MONOCHROME: - case PACKET_HEADER_Z_COMMAND: - case PACKET_HEADER_ERASE_VOXEL: - voxels.parseData(incomingPacket, bytesReceived); - break; - case PACKET_HEADER_ENVIRONMENT_DATA: - environment.parseData(incomingPacket, bytesReceived); - break; - case PACKET_HEADER_BULK_AVATAR_DATA: - AgentList::getInstance()->processBulkAgentData(&senderAddress, - incomingPacket, - bytesReceived); - break; - default: - AgentList::getInstance()->processAgentData(&senderAddress, incomingPacket, bytesReceived); - break; - } - } else if (!enableNetworkThread) { - break; - } - } - - if (enableNetworkThread) { - pthread_exit(0); - } - return NULL; -} - -glm::vec3 getFaceVector(BoxFace face) { - switch (face) { - case MIN_X_FACE: - return glm::vec3(-1, 0, 0); - - case MAX_X_FACE: - return glm::vec3(1, 0, 0); - - case MIN_Y_FACE: - return glm::vec3(0, -1, 0); - - case MAX_Y_FACE: - return glm::vec3(0, 1, 0); - - case MIN_Z_FACE: - return glm::vec3(0, 0, -1); - - case MAX_Z_FACE: - return glm::vec3(0, 0, 1); - } -} - -Application* app; - -void idle(void) { - timeval check; - gettimeofday(&check, NULL); - - app->processEvents(); - - // Only run simulation code if more than IDLE_SIMULATE_MSECS have passed since last time - - if (diffclock(&lastTimeIdle, &check) > IDLE_SIMULATE_MSECS) { - - float deltaTime = 1.f/FPS; - - // update behaviors for avatar hand movement: handControl takes mouse values as input, - // and gives back 3D values modulated for smooth transitioning between interaction modes. - handControl.update(mouseX, mouseY); - myAvatar.setHandMovementValues(handControl.getValues()); - - // tell my avatar if the mouse is being pressed... - myAvatar.setMousePressed(mousePressed); - - // check what's under the mouse and update the mouse voxel - glm::vec3 origin, direction; - viewFrustum.computePickRay(mouseX / (float)::screenWidth, mouseY / (float)::screenHeight, origin, direction); - - float distance; - BoxFace face; - ::mouseVoxel.s = 0.0f; - if (::mouseMode != NO_EDIT_MODE && voxels.findRayIntersection(origin, direction, ::mouseVoxel, distance, face)) { - // find the nearest voxel with the desired scale - if (::mouseVoxelScale > ::mouseVoxel.s) { - // choose the larger voxel that encompasses the one selected - ::mouseVoxel.x = ::mouseVoxelScale * floorf(::mouseVoxel.x / ::mouseVoxelScale); - ::mouseVoxel.y = ::mouseVoxelScale * floorf(::mouseVoxel.y / ::mouseVoxelScale); - ::mouseVoxel.z = ::mouseVoxelScale * floorf(::mouseVoxel.z / ::mouseVoxelScale); - ::mouseVoxel.s = ::mouseVoxelScale; - - } else { - glm::vec3 faceVector = getFaceVector(face); - if (::mouseVoxelScale < ::mouseVoxel.s) { - // find the closest contained voxel - glm::vec3 pt = (origin + direction * distance) / (float)TREE_SCALE - - faceVector * (::mouseVoxelScale * 0.5f); - ::mouseVoxel.x = ::mouseVoxelScale * floorf(pt.x / ::mouseVoxelScale); - ::mouseVoxel.y = ::mouseVoxelScale * floorf(pt.y / ::mouseVoxelScale); - ::mouseVoxel.z = ::mouseVoxelScale * floorf(pt.z / ::mouseVoxelScale); - ::mouseVoxel.s = ::mouseVoxelScale; - } - if (::mouseMode == ADD_VOXEL_MODE) { - // use the face to determine the side on which to create a neighbor - ::mouseVoxel.x += faceVector.x * ::mouseVoxel.s; - ::mouseVoxel.y += faceVector.y * ::mouseVoxel.s; - ::mouseVoxel.z += faceVector.z * ::mouseVoxel.s; - } - } - - if (::mouseMode == COLOR_VOXEL_MODE) { - ::mouseVoxel.red = 0; - ::mouseVoxel.green = 255; - ::mouseVoxel.blue = 0; - - } else if (::mouseMode == DELETE_VOXEL_MODE) { - // red indicates deletion - ::mouseVoxel.red = 255; - ::mouseVoxel.green = ::mouseVoxel.blue = 0; - } - } - - // walking triggers the handControl to stop - if (myAvatar.getMode() == AVATAR_MODE_WALKING) { - handControl.stop(); - mouseViewShiftYaw *= 0.9; - mouseViewShiftPitch *= 0.9; - } - - // Read serial port interface devices - if (serialPort.active) { - serialPort.readData(); - } - - // Sample hardware, update view frustum if needed, and send avatar data to mixer/agents - updateAvatar(deltaTime); - - // read incoming packets from network - if (!enableNetworkThread) { - networkReceive(0); - } - - //loop through all the remote avatars and simulate them... - AgentList* agentList = AgentList::getInstance(); - agentList->lock(); - for(AgentList::iterator agent = agentList->begin(); agent != agentList->end(); agent++) { - if (agent->getLinkedData() != NULL) { - Avatar *avatar = (Avatar *)agent->getLinkedData(); - avatar->simulate(deltaTime); - } - } - agentList->unlock(); - - myAvatar.setGravity(getGravity(myAvatar.getPosition())); - myAvatar.simulate(deltaTime); - - // Update audio stats for procedural sounds - audio.setLastAcceleration(myAvatar.getThrust()); - audio.setLastVelocity(myAvatar.getVelocity()); - - glutPostRedisplay(); - lastTimeIdle = check; - } -} - -void reshape(int width, int height) { - ::screenWidth = width; - ::screenHeight = height; - aspectRatio = ((float)width/(float)height); // based on screen resize - - // get the lens details from the current camera - Camera& camera = ::viewFrustumFromOffset ? (::viewFrustumOffsetCamera) : (::myCamera); - float nearClip = camera.getNearClip(); - float farClip = camera.getFarClip(); - float fov; - - if (::oculusOn) { - // more magic numbers; see Oculus SDK docs, p. 32 - camera.setAspectRatio(aspectRatio *= 0.5); - camera.setFieldOfView(fov = 2 * atan((0.0468 * ::oculusDistortionScale) / 0.041) * (180 / PI)); - - // resize the render texture - if (::oculusTextureID != 0) { - glBindTexture(GL_TEXTURE_2D, ::oculusTextureID); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ::screenWidth, ::screenHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); - glBindTexture(GL_TEXTURE_2D, 0); - } - } else { - camera.setAspectRatio(aspectRatio); - camera.setFieldOfView(fov = 60); - } - - // Tell our viewFrustum about this change - ::viewFrustum.setAspectRatio(aspectRatio); - - glViewport(0, 0, width, height); // shouldn't this account for the menu??? - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - - // XXXBHG - If we're in view frustum mode, then we need to do this little bit of hackery so that - // OpenGL won't clip our frustum rendering lines. This is a debug hack for sure! Basically, this makes - // the near clip a little bit closer (therefor you see more) and the far clip a little bit farther (also, - // to see more.) - if (::frustumOn) { - nearClip -= 0.01f; - farClip += 0.01f; - } - - // On window reshape, we need to tell OpenGL about our new setting - gluPerspective(fov,aspectRatio,nearClip,farClip); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); -} - -//Find and return the gravity vector at this location -glm::vec3 getGravity(glm::vec3 pos) { - // - // For now, we'll test this with a simple global lookup, but soon we will add getting this - // from the domain/voxelserver (or something similar) - // - if ((pos.x > 0.f) && - (pos.x < 10.f) && - (pos.z > 0.f) && - (pos.z < 10.f) && - (pos.y > 0.f) && - (pos.y < 3.f)) { - // If above ground plane, turn gravity on - return glm::vec3(0.f, -1.f, 0.f); - } else { - // If flying in space, turn gravity OFF - return glm::vec3(0.f, 0.f, 0.f); - } -} - -bool menuDisplayed = false; -void mouseFunc(int button, int state, int x, int y) { - bool menuFound = menu.mouseClick(x, y); - - // If we didn't previously have the menu displayed, and we did just click on the menu, then - // go into menuDisplayed mode.... - if (!::menuDisplayed && menuFound) { - ::menuDisplayed = true; - } - - // If the menu was displayed, and we're not over a menu, then leave menu mode - if (::menuDisplayed && !menuFound) { - ::menuDisplayed = false; - menu.hidePopupMenu(); - } - - // In menu displayed mode use old logic - if (::menuDisplayed) { - if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { - if (state == GLUT_DOWN && !menu.mouseClick(x, y)) { - mouseX = x; - mouseY = y; - mousePressed = 1; - } else if (state == GLUT_UP) { - mouseX = x; - mouseY = y; - mousePressed = 0; - } - } - } else { - if (button == GLUT_LEFT_BUTTON) { - mouseX = x; - mouseY = y; - - if (state == GLUT_DOWN) { - mousePressed = 1; - if (::mouseMode == ADD_VOXEL_MODE || ::mouseMode == COLOR_VOXEL_MODE) { - addVoxelUnderCursor(); - - } else if (::mouseMode == DELETE_VOXEL_MODE) { - deleteVoxelUnderCursor(); - } - } else if (state == GLUT_UP) { - mousePressed = 0; - } - } else if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN && ::mouseMode != NO_EDIT_MODE) { - deleteVoxelUnderCursor(); - } - } -} - - -void motionFunc(int x, int y) { - mouseX = x; - mouseY = y; -} - -void mouseoverFunc(int x, int y){ - menu.mouseOver(x, y); - - mouseX = x; - mouseY = y; -} - -void attachNewHeadToAgent(Agent *newAgent) { - if (newAgent->getLinkedData() == NULL) { - newAgent->setLinkedData(new Avatar(false)); - } -} - -#ifndef _WIN32 -void audioMixerUpdate(in_addr_t newMixerAddress, in_port_t newMixerPort) { - audio.updateMixerParams(newMixerAddress, newMixerPort); -} -#endif +#include "Log.h" int main(int argc, const char * argv[]) { @@ -2057,124 +25,4 @@ int main(int argc, const char * argv[]) { int exitCode = app.exec(); printLog("Normal exit.\n"); return exitCode; - - - - gettimeofday(&applicationStartupTime, NULL); - printLog("Interface Startup:\n"); - - voxels.setViewFrustum(&::viewFrustum); - - shared_lib::printLog = & ::printLog; - voxels_lib::printLog = & ::printLog; - avatars_lib::printLog = & ::printLog; - - unsigned int listenPort = AGENT_SOCKET_LISTEN_PORT; - const char* portStr = getCmdOption(argc, argv, "--listenPort"); - if (portStr) { - listenPort = atoi(portStr); - } - AgentList::createInstance(AGENT_TYPE_AVATAR, listenPort); - enableNetworkThread = !cmdOptionExists(argc, argv, "--nonblocking"); - if (!enableNetworkThread) { - AgentList::getInstance()->getAgentSocket().setBlocking(false); - } - - const char* domainIP = getCmdOption(argc, argv, "--domain"); - if (domainIP) { - strcpy(DOMAIN_IP,domainIP); - } - - // Handle Local Domain testing with the --local command line - if (cmdOptionExists(argc, argv, "--local")) { - printLog("Local Domain MODE!\n"); - int ip = getLocalAddress(); - sprintf(DOMAIN_IP,"%d.%d.%d.%d", (ip & 0xFF), ((ip >> 8) & 0xFF),((ip >> 16) & 0xFF), ((ip >> 24) & 0xFF)); - } - - // the callback for our instance of AgentList is attachNewHeadToAgent - AgentList::getInstance()->linkedDataCreateCallback = &attachNewHeadToAgent; - - #ifndef _WIN32 - AgentList::getInstance()->audioMixerSocketUpdate = &audioMixerUpdate; - #endif - -#ifdef _WIN32 - WSADATA WsaData; - int wsaresult = WSAStartup(MAKEWORD(2,2), &WsaData); -#endif - - // start the agentList threads - AgentList::getInstance()->startSilentAgentRemovalThread(); - AgentList::getInstance()->startDomainServerCheckInThread(); - AgentList::getInstance()->startPingUnknownAgentsThread(); - - glutInit(&argc, (char**)argv); - ::screenWidth = glutGet(GLUT_SCREEN_WIDTH); - ::screenHeight = glutGet(GLUT_SCREEN_HEIGHT); - - glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); - glutInitWindowSize(::screenWidth, ::screenHeight); - glutCreateWindow("Interface"); - printLog( "Created Display Window.\n" ); - - #ifdef _WIN32 - glewInit(); - printLog( "Glew Init complete.\n" ); - - #endif - - // Before we render anything, let's set up our viewFrustumOffsetCamera with a sufficiently large - // field of view and near and far clip to make it interesting. - //viewFrustumOffsetCamera.setFieldOfView(90.0); - viewFrustumOffsetCamera.setNearClip(0.1); - viewFrustumOffsetCamera.setFarClip(500.0*TREE_SCALE); - - - initMenu(); - initDisplay(); - - glutDisplayFunc(display); - glutReshapeFunc(reshape); - glutKeyboardFunc(key); - glutKeyboardUpFunc(keyUp); - glutSpecialFunc(specialkey); - glutSpecialUpFunc(specialkeyUp); - glutMotionFunc(motionFunc); - glutPassiveMotionFunc(mouseoverFunc); - glutMouseFunc(mouseFunc); - glutIdleFunc(idle); - printLog( "Initialized Display.\n" ); - - - init(); - printLog( "Init() complete.\n" ); - - // Check to see if the user passed in a command line option for randomizing colors - if (cmdOptionExists(argc, argv, "--NoColorRandomizer")) { - wantColorRandomizer = false; - } - - // Check to see if the user passed in a command line option for loading a local - // Voxel File. If so, load it now. - const char* voxelsFilename = getCmdOption(argc, argv, "-i"); - if (voxelsFilename) { - voxels.loadVoxelsFile(voxelsFilename,wantColorRandomizer); - printLog("Local Voxel File loaded.\n"); - } - - // create thread for receipt of data via UDP - if (enableNetworkThread) { - pthread_create(&networkReceiveThread, NULL, networkReceive, NULL); - printLog("Network receive thread created.\n"); - } - - myAvatar.readAvatarDataFromFile(); - - glutTimerFunc(1000, Timer, 0); - glutMainLoop(); - - printLog("Normal exit.\n"); - ::terminate(); - return EXIT_SUCCESS; } diff --git a/interface/src/ui/ChatEntry.cpp b/interface/src/ui/ChatEntry.cpp index 8263424a0e..d83bd5d657 100644 --- a/interface/src/ui/ChatEntry.cpp +++ b/interface/src/ui/ChatEntry.cpp @@ -15,16 +15,25 @@ using namespace std; const int MAX_CONTENT_LENGTH = 140; +ChatEntry::ChatEntry() : _cursorPos(0) { +} + void ChatEntry::clear() { _contents.clear(); _cursorPos = 0; } bool ChatEntry::keyPressEvent(QKeyEvent* event) { + event->accept(); switch (event->key()) { + case Qt::Key_Return: case Qt::Key_Enter: return false; + case Qt::Key_Escape: + clear(); + return false; + case Qt::Key_Backspace: if (_cursorPos != 0) { _contents.erase(_cursorPos - 1, 1); diff --git a/interface/src/ui/ChatEntry.h b/interface/src/ui/ChatEntry.h index 6c17690fbd..478641325d 100644 --- a/interface/src/ui/ChatEntry.h +++ b/interface/src/ui/ChatEntry.h @@ -16,6 +16,8 @@ class QKeyEvent; class ChatEntry { public: + ChatEntry(); + const std::string& getContents() const { return _contents; } void clear(); From 31b94203ff80bdab2ebf1111e84be879de2a1306 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 14 May 2013 00:24:53 -0700 Subject: [PATCH 04/31] Disable the menu shortcuts for "regular" keys when the chat entry is showing. --- interface/src/Application.cpp | 22 ++++++++++++++++++++++ interface/src/Application.h | 2 ++ interface/src/ui/ChatEntry.cpp | 1 - 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index d6d829f834..432f47c58f 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -483,6 +484,7 @@ void Application::keyPressEvent(QKeyEvent* event) { _myAvatar.setChatMessage(_chatEntry.getContents()); _chatEntry.clear(); _chatEntryOn = false; + setMenuShortcutsEnabled(true); } return; } @@ -607,6 +609,7 @@ void Application::keyPressEvent(QKeyEvent* event) { _chatEntryOn = true; _myAvatar.setKeyState(NO_KEY_DOWN); _myAvatar.setChatMessage(string()); + setMenuShortcutsEnabled(false); break; case Qt::Key_Up: @@ -1852,6 +1855,25 @@ void Application::resetSensors() { _myAvatar.reset(); } +static void setShortcutsEnabled(QWidget* widget, bool enabled) { + foreach (QAction* action, widget->actions()) { + QKeySequence shortcut = action->shortcut(); + if (!shortcut.isEmpty() && (shortcut[0] & (Qt::CTRL | Qt::ALT | Qt::META)) == 0) { + // it's a shortcut that may coincide with a "regular" key, so switch its context + action->setShortcutContext(enabled ? Qt::WindowShortcut : Qt::WidgetShortcut); + } + } + foreach (QObject* child, widget->children()) { + if (child->isWidgetType()) { + setShortcutsEnabled(static_cast(child), enabled); + } + } +} + +void Application::setMenuShortcutsEnabled(bool enabled) { + setShortcutsEnabled(_window->menuBar(), enabled); +} + void Application::attachNewHeadToAgent(Agent *newAgent) { if (newAgent->getLinkedData() == NULL) { newAgent->setLinkedData(new Avatar(false)); diff --git a/interface/src/Application.h b/interface/src/Application.h index 422b6302e6..011e1a94e5 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -110,6 +110,8 @@ private: void resetSensors(); + void setMenuShortcutsEnabled(bool enabled); + static void attachNewHeadToAgent(Agent *newAgent); #ifndef _WIN32 static void audioMixerUpdate(in_addr_t newMixerAddress, in_port_t newMixerPort); diff --git a/interface/src/ui/ChatEntry.cpp b/interface/src/ui/ChatEntry.cpp index d83bd5d657..ad4cf70ab0 100644 --- a/interface/src/ui/ChatEntry.cpp +++ b/interface/src/ui/ChatEntry.cpp @@ -85,5 +85,4 @@ void ChatEntry::render(int screenWidth, int screenHeight) { glVertex2f(20 + width, screenHeight - 165); glVertex2f(20 + width, screenHeight - 150); glEnd(); - glEnable(GL_LINE_SMOOTH); } From f63407aea5f2641c138475de4316930053344819 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 14 May 2013 08:57:49 -0700 Subject: [PATCH 05/31] added printDebugDetails() --- libraries/voxels/src/ViewFrustum.cpp | 14 ++++++++++++++ libraries/voxels/src/ViewFrustum.h | 2 ++ 2 files changed, 16 insertions(+) diff --git a/libraries/voxels/src/ViewFrustum.cpp b/libraries/voxels/src/ViewFrustum.cpp index a107e2f869..25022ae521 100644 --- a/libraries/voxels/src/ViewFrustum.cpp +++ b/libraries/voxels/src/ViewFrustum.cpp @@ -276,3 +276,17 @@ void ViewFrustum::computePickRay(float x, float y, glm::vec3& origin, glm::vec3& origin = _nearTopLeft + x*(_nearTopRight - _nearTopLeft) + y*(_nearBottomLeft - _nearTopLeft); direction = glm::normalize(origin - _position); } + + +void ViewFrustum::printDebugDetails() const { + printLog("ViewFrustum::printDebugDetails()... \n"); + printLog("_position=%f,%f,%f\n", _position.x, _position.y, _position.z ); + printLog("_direction=%f,%f,%f\n", _direction.x, _direction.y, _direction.z ); + printLog("_up=%f,%f,%f\n", _up.x, _up.y, _up.z ); + printLog("_right=%f,%f,%f\n", _right.x, _right.y, _right.z ); + printLog("_fieldOfView=%f\n", _fieldOfView); + printLog("_aspectRatio=%f\n", _aspectRatio); + printLog("_nearClip=%f\n", _nearClip); + printLog("_farClip=%f\n", _farClip); +} + diff --git a/libraries/voxels/src/ViewFrustum.h b/libraries/voxels/src/ViewFrustum.h index 007654fcf4..aadb1f86e8 100644 --- a/libraries/voxels/src/ViewFrustum.h +++ b/libraries/voxels/src/ViewFrustum.h @@ -102,6 +102,8 @@ public: bool matches(const ViewFrustum& compareTo) const; bool matches(const ViewFrustum* compareTo) const { return matches(*compareTo); }; void computePickRay(float x, float y, glm::vec3& origin, glm::vec3& direction) const; + + void printDebugDetails() const; }; From fa37b88fa84c704d84fa1e43ac3462b118baa642 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 14 May 2013 09:02:01 -0700 Subject: [PATCH 06/31] first cut at delta view frustum encoding --- libraries/voxels/src/VoxelTree.cpp | 31 ++++++++++++++++++++---------- libraries/voxels/src/VoxelTree.h | 12 ++++++++---- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/libraries/voxels/src/VoxelTree.cpp b/libraries/voxels/src/VoxelTree.cpp index 5df39140ec..0476ca0d40 100644 --- a/libraries/voxels/src/VoxelTree.cpp +++ b/libraries/voxels/src/VoxelTree.cpp @@ -617,12 +617,14 @@ void VoxelTree::createSphere(float radius, float xc, float yc, float zc, float v this->reaverageVoxelColors(this->rootNode); } -int VoxelTree::searchForColoredNodes(int maxSearchLevel, VoxelNode* node, const ViewFrustum& viewFrustum, VoxelNodeBag& bag) { +int VoxelTree::searchForColoredNodes(int maxSearchLevel, VoxelNode* node, const ViewFrustum& viewFrustum, VoxelNodeBag& bag, + bool deltaViewFrustum, const ViewFrustum* lastViewFrustum) { // call the recursive version, this will add all found colored node roots to the bag int currentSearchLevel = 0; - int levelReached = searchForColoredNodesRecursion(maxSearchLevel, currentSearchLevel, rootNode, viewFrustum, bag); + int levelReached = searchForColoredNodesRecursion(maxSearchLevel, currentSearchLevel, rootNode, + viewFrustum, bag, deltaViewFrustum, lastViewFrustum); return levelReached; } @@ -667,7 +669,8 @@ bool VoxelTree::findRayIntersection(const glm::vec3& origin, const glm::vec3& di } int VoxelTree::searchForColoredNodesRecursion(int maxSearchLevel, int& currentSearchLevel, - VoxelNode* node, const ViewFrustum& viewFrustum, VoxelNodeBag& bag) { + VoxelNode* node, const ViewFrustum& viewFrustum, VoxelNodeBag& bag, + bool deltaViewFrustum, const ViewFrustum* lastViewFrustum) { // Keep track of how deep we've searched. currentSearchLevel++; @@ -702,7 +705,7 @@ int VoxelTree::searchForColoredNodesRecursion(int maxSearchLevel, int& currentSe bool childIsColored = (childNode && childNode->isColored()); bool childIsInView = (childNode && childNode->isInView(viewFrustum)); bool childIsLeaf = (childNode && childNode->isLeaf()); - + if (childIsInView) { // track children in view as existing and not a leaf @@ -739,7 +742,8 @@ int VoxelTree::searchForColoredNodesRecursion(int maxSearchLevel, int& currentSe for (int i = 0; i < inViewCount; i++) { VoxelNode* childNode = inViewChildren[i]; thisLevel = currentSearchLevel; // reset this, since the children will munge it up - int childLevelReached = searchForColoredNodesRecursion(maxSearchLevel, thisLevel, childNode, viewFrustum, bag); + int childLevelReached = searchForColoredNodesRecursion(maxSearchLevel, thisLevel, childNode, viewFrustum, bag, + deltaViewFrustum, lastViewFrustum); maxChildLevel = std::max(maxChildLevel, childLevelReached); } } @@ -747,7 +751,8 @@ int VoxelTree::searchForColoredNodesRecursion(int maxSearchLevel, int& currentSe } int VoxelTree::encodeTreeBitstream(int maxEncodeLevel, VoxelNode* node, unsigned char* outputBuffer, int availableBytes, - VoxelNodeBag& bag, const ViewFrustum* viewFrustum, bool includeColor) const { + VoxelNodeBag& bag, const ViewFrustum* viewFrustum, bool includeColor, + bool deltaViewFrustum, const ViewFrustum* lastViewFrustum) const { // How many bytes have we written so far at this level; int bytesWritten = 0; @@ -767,7 +772,8 @@ int VoxelTree::encodeTreeBitstream(int maxEncodeLevel, VoxelNode* node, unsigned int currentEncodeLevel = 0; int childBytesWritten = encodeTreeBitstreamRecursion(maxEncodeLevel, currentEncodeLevel, - node, outputBuffer, availableBytes, bag, viewFrustum, includeColor); + node, outputBuffer, availableBytes, bag, viewFrustum, includeColor, + deltaViewFrustum, lastViewFrustum); // if childBytesWritten == 1 then something went wrong... that's not possible assert(childBytesWritten != 1); @@ -790,7 +796,8 @@ int VoxelTree::encodeTreeBitstream(int maxEncodeLevel, VoxelNode* node, unsigned int VoxelTree::encodeTreeBitstreamRecursion(int maxEncodeLevel, int& currentEncodeLevel, VoxelNode* node, unsigned char* outputBuffer, int availableBytes, - VoxelNodeBag& bag, const ViewFrustum* viewFrustum, bool includeColor) const { + VoxelNodeBag& bag, const ViewFrustum* viewFrustum, bool includeColor, + bool deltaViewFrustum, const ViewFrustum* lastViewFrustum) const { // How many bytes have we written so far at this level; int bytesAtThisLevel = 0; @@ -846,7 +853,10 @@ int VoxelTree::encodeTreeBitstreamRecursion(int maxEncodeLevel, int& currentEnco for (int i = 0; i < NUMBER_OF_CHILDREN; i++) { VoxelNode* childNode = node->getChildAtIndex(i); bool childIsInView = (childNode && (!viewFrustum || childNode->isInView(*viewFrustum))); - if (childIsInView) { + bool childWasInView = (childNode && deltaViewFrustum && + (lastViewFrustum && ViewFrustum::INSIDE == childNode->inFrustum(*lastViewFrustum))); + + if (childIsInView && !childWasInView) { // Before we determine consider this further, let's see if it's in our LOD scope... float distance = viewFrustum ? childNode->distanceToCamera(*viewFrustum) : 0; float boundaryDistance = viewFrustum ? boundaryDistanceForRenderLevel(*childNode->getOctalCode() + 1) : 1; @@ -925,7 +935,8 @@ int VoxelTree::encodeTreeBitstreamRecursion(int maxEncodeLevel, int& currentEnco int thisLevel = currentEncodeLevel; int childTreeBytesOut = encodeTreeBitstreamRecursion(maxEncodeLevel, thisLevel, childNode, outputBuffer, availableBytes, bag, - viewFrustum, includeColor); + viewFrustum, includeColor, + deltaViewFrustum, lastViewFrustum); // if the child wrote 0 bytes, it means that nothing below exists or was in view, or we ran out of space, // basically, the children below don't contain any info. diff --git a/libraries/voxels/src/VoxelTree.h b/libraries/voxels/src/VoxelTree.h index 649bbfc92d..3c33c7b02c 100644 --- a/libraries/voxels/src/VoxelTree.h +++ b/libraries/voxels/src/VoxelTree.h @@ -55,9 +55,11 @@ public: void recurseTreeWithOperation(RecurseVoxelTreeOperation operation, void* extraData=NULL); int encodeTreeBitstream(int maxEncodeLevel, VoxelNode* node, unsigned char* outputBuffer, int availableBytes, - VoxelNodeBag& bag, const ViewFrustum* viewFrustum, bool includeColor = true) const; + VoxelNodeBag& bag, const ViewFrustum* viewFrustum, bool includeColor = true, + bool deltaViewFrustum = false, const ViewFrustum* lastViewFrustum = NULL) const; - int searchForColoredNodes(int maxSearchLevel, VoxelNode* node, const ViewFrustum& viewFrustum, VoxelNodeBag& bag); + int searchForColoredNodes(int maxSearchLevel, VoxelNode* node, const ViewFrustum& viewFrustum, VoxelNodeBag& bag, + bool deltaViewFrustum = false, const ViewFrustum* lastViewFrustum = NULL); bool isDirty() const { return _isDirty; }; void clearDirtyBit() { _isDirty = false; }; @@ -79,10 +81,12 @@ public: private: int encodeTreeBitstreamRecursion(int maxEncodeLevel, int& currentEncodeLevel, VoxelNode* node, unsigned char* outputBuffer, int availableBytes, - VoxelNodeBag& bag, const ViewFrustum* viewFrustum, bool includeColor) const; + VoxelNodeBag& bag, const ViewFrustum* viewFrustum, bool includeColor, + bool deltaViewFrustum, const ViewFrustum* lastViewFrustum) const; int searchForColoredNodesRecursion(int maxSearchLevel, int& currentSearchLevel, - VoxelNode* node, const ViewFrustum& viewFrustum, VoxelNodeBag& bag); + VoxelNode* node, const ViewFrustum& viewFrustum, VoxelNodeBag& bag, + bool deltaViewFrustum, const ViewFrustum* lastViewFrustum); static bool countVoxelsOperation(VoxelNode* node, void* extraData); From 2ff8da2b46b00b1942b3d0c4099008f1dc99401c Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 14 May 2013 09:03:38 -0700 Subject: [PATCH 07/31] move view frustum calculation into VoxelAgentData, added last known and current --- voxel-server/src/VoxelAgentData.cpp | 18 +++++++++--------- voxel-server/src/VoxelAgentData.h | 7 +++++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/voxel-server/src/VoxelAgentData.cpp b/voxel-server/src/VoxelAgentData.cpp index 07bf2a6593..df61a0d6f8 100644 --- a/voxel-server/src/VoxelAgentData.cpp +++ b/voxel-server/src/VoxelAgentData.cpp @@ -53,21 +53,21 @@ VoxelAgentData* VoxelAgentData::clone() const { void VoxelAgentData::updateViewFrustum() { // save our currentViewFrustum into our lastKnownViewFrustum - lastKnownViewFrustum = currentViewFrustum; + _lastKnownViewFrustum = _currentViewFrustum; // get position and orientation details from the camera - currentViewFrustum.setPosition(getCameraPosition()); - currentViewFrustum.setOrientation(getCameraDirection(), getCameraUp(), getCameraRight()); + _currentViewFrustum.setPosition(getCameraPosition()); + _currentViewFrustum.setOrientation(getCameraDirection(), getCameraUp(), getCameraRight()); // Also make sure it's got the correct lens details from the camera - currentViewFrustum.setFieldOfView(getCameraFov()); - currentViewFrustum.setAspectRatio(getCameraAspectRatio()); - currentViewFrustum.setNearClip(getCameraNearClip()); - currentViewFrustum.setFarClip(getCameraFarClip()); + _currentViewFrustum.setFieldOfView(getCameraFov()); + _currentViewFrustum.setAspectRatio(getCameraAspectRatio()); + _currentViewFrustum.setNearClip(getCameraNearClip()); + _currentViewFrustum.setFarClip(getCameraFarClip()); // if there has been a change, then recalculate - if (!lastKnownViewFrustum.matches(currentViewFrustum)) { - currentViewFrustum.calculate(); + if (!_lastKnownViewFrustum.matches(_currentViewFrustum)) { + _currentViewFrustum.calculate(); } } diff --git a/voxel-server/src/VoxelAgentData.h b/voxel-server/src/VoxelAgentData.h index ee8b2dd660..cb88c05574 100644 --- a/voxel-server/src/VoxelAgentData.h +++ b/voxel-server/src/VoxelAgentData.h @@ -41,8 +41,8 @@ public: VoxelNodeBag nodeBag; - ViewFrustum currentViewFrustum; - ViewFrustum lastKnownViewFrustum; + ViewFrustum& getCurrentViewFrustum() { return _currentViewFrustum; }; + ViewFrustum& getLastKnownViewFrustum() { return _lastKnownViewFrustum; }; void updateViewFrustum(); @@ -53,6 +53,9 @@ private: bool _voxelPacketWaiting; int _maxSearchLevel; int _maxLevelReachedInLastSearch; + ViewFrustum _currentViewFrustum; + ViewFrustum _lastKnownViewFrustum; + }; #endif /* defined(__hifi__VoxelAgentData__) */ From 294367a417258c8ea8286a3a27e0f29c8f51439c Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 14 May 2013 09:04:35 -0700 Subject: [PATCH 08/31] first cut at delata view frustum encoding --- voxel-server/src/main.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/voxel-server/src/main.cpp b/voxel-server/src/main.cpp index 76941d9bc2..e1293b1cdf 100644 --- a/voxel-server/src/main.cpp +++ b/voxel-server/src/main.cpp @@ -107,8 +107,8 @@ void eraseVoxelTreeAndCleanupAgentVisitData() { // Version of voxel distributor that sends each LOD level at a time void resInVoxelDistributor(AgentList* agentList, AgentList::iterator& agent, - VoxelAgentData* agentData, - ViewFrustum& viewFrustum) { + VoxelAgentData* agentData) { + ViewFrustum viewFrustum = agentData->getCurrentViewFrustum(); bool searchReset = false; int searchLoops = 0; int searchLevelWas = agentData->getMaxSearchLevel(); @@ -224,12 +224,13 @@ void resInVoxelDistributor(AgentList* agentList, // Version of voxel distributor that sends the deepest LOD level at once void deepestLevelVoxelDistributor(AgentList* agentList, AgentList::iterator& agent, - VoxelAgentData* agentData, - ViewFrustum& viewFrustum) { + VoxelAgentData* agentData) { + int maxLevelReached = 0; double start = usecTimestampNow(); if (agentData->nodeBag.isEmpty()) { - maxLevelReached = randomTree.searchForColoredNodes(INT_MAX, randomTree.rootNode, viewFrustum, agentData->nodeBag); + maxLevelReached = randomTree.searchForColoredNodes(INT_MAX, randomTree.rootNode, agentData->getCurrentViewFrustum(), + agentData->nodeBag, true, &agentData->getLastKnownViewFrustum()); } double end = usecTimestampNow(); double elapsedmsec = (end - start)/1000.0; @@ -261,8 +262,9 @@ void deepestLevelVoxelDistributor(AgentList* agentList, VoxelNode* subTree = agentData->nodeBag.extract(); bytesWritten = randomTree.encodeTreeBitstream(INT_MAX, subTree, &tempOutputBuffer[0], MAX_VOXEL_PACKET_SIZE - 1, - agentData->nodeBag, &viewFrustum, - agentData->getWantColor()); + agentData->nodeBag, &agentData->getCurrentViewFrustum(), + agentData->getWantColor(), true, + &agentData->getLastKnownViewFrustum()); if (agentData->getAvailable() >= bytesWritten) { agentData->writeToPacket(&tempOutputBuffer[0], bytesWritten); @@ -338,9 +340,9 @@ void *distributeVoxelsToListeners(void *args) { agentData->updateViewFrustum(); if (agentData->getWantResIn()) { - resInVoxelDistributor(agentList, agent, agentData, agentData->currentViewFrustum); + resInVoxelDistributor(agentList, agent, agentData); } else { - deepestLevelVoxelDistributor(agentList, agent, agentData, agentData->currentViewFrustum); + deepestLevelVoxelDistributor(agentList, agent, agentData); } } } From 096421347e5f60b27a9ca3509638e742d885c07c Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 14 May 2013 10:45:54 -0700 Subject: [PATCH 09/31] Missed some comments in transferring from main.cpp to Application.h; trying to preserve original formatting. --- interface/src/Application.cpp | 4 +++- interface/src/Application.h | 44 +++++++++++++++++------------------ 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 432f47c58f..00833ca8a2 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -54,7 +54,7 @@ static char STAR_FILE[] = "https://s3-us-west-1.amazonaws.com/highfidelity/stars static char STAR_CACHE_FILE[] = "cachedStars.txt"; const glm::vec3 START_LOCATION(6.1f, 0, 1.4f); // Where one's own agent begins in the world - // (will be overwritten if avatar data file is found) + // (will be overwritten if avatar data file is found) const int IDLE_SIMULATE_MSECS = 16; // How often should call simulate and other stuff // in the idle loop? (60 FPS is default) @@ -489,6 +489,8 @@ void Application::keyPressEvent(QKeyEvent* event) { return; } + qDebug() << QKeySequence(event->key()); + bool shifted = event->modifiers().testFlag(Qt::ShiftModifier); switch (event->key()) { case Qt::Key_BracketLeft: diff --git a/interface/src/Application.h b/interface/src/Application.h index 011e1a94e5..c076b91a9e 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -121,18 +121,18 @@ private: QMainWindow* _window; QGLWidget* _glWidget; - QAction* _lookingInMirror; - QAction* _gyroLook; - QAction* _renderVoxels; - QAction* _renderStarsOn; - QAction* _renderAtmosphereOn; - QAction* _renderAvatarsOn; - QAction* _oculusOn; - QAction* _renderStatsOn; - QAction* _logOn; - QAction* _frustumOn; - QAction* _viewFrustumFromOffset; - QAction* _cameraFrustum; + QAction* _lookingInMirror; // Are we currently rendering one's own head as if in mirror? + QAction* _gyroLook; // Whether to allow the gyro data from head to move your view + QAction* _renderVoxels; // Whether to render voxels + QAction* _renderStarsOn; // Whether to display the stars + QAction* _renderAtmosphereOn; // Whether to display the atmosphere + QAction* _renderAvatarsOn; // Whether to render avatars + QAction* _oculusOn; // Whether to configure the display for the Oculus Rift + QAction* _renderStatsOn; // Whether to show onscreen text overlay with stats + QAction* _logOn; // Whether to show on-screen log + QAction* _frustumOn; // Whether or not to display the debug view frustum + QAction* _viewFrustumFromOffset; // Whether or not to offset the view of the frustum + QAction* _cameraFrustum; // which frustum to look at QAction* _frustumRenderModeAction; SerialInterface _serialPort; @@ -189,20 +189,20 @@ private: enum MouseMode { NO_EDIT_MODE, ADD_VOXEL_MODE, DELETE_VOXEL_MODE, COLOR_VOXEL_MODE }; MouseMode _mouseMode; VoxelDetail _mouseVoxel; // details of the voxel under the mouse cursor - float _mouseVoxelScale; // the scale for adding/removing voxels + float _mouseVoxelScale; // the scale for adding/removing voxels - bool _paintOn; - unsigned char _dominantColor; - VoxelDetail _paintingVoxel; + bool _paintOn; // Whether to paint voxels as you fly around + unsigned char _dominantColor; // The dominant color of the voxel we're painting + VoxelDetail _paintingVoxel; // The voxel we're painting if we're painting - bool _perfStatsOn; + bool _perfStatsOn; // Do we want to display perfStats? - ChatEntry _chatEntry; - bool _chatEntryOn; + ChatEntry _chatEntry; // chat entry field + bool _chatEntryOn; // Whether to show the chat entry - GLuint _oculusTextureID; - ProgramObject* _oculusProgram; - float _oculusDistortionScale; + GLuint _oculusTextureID; // The texture to which we render for Oculus distortion + ProgramObject* _oculusProgram; // The GLSL program containing the distortion shader + float _oculusDistortionScale; // Controls the Oculus field of view int _textureLocation; int _lensCenterLocation; int _screenCenterLocation; From a5e7fdeacd2149a25f28de076f8dda9b9cd46e55 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 14 May 2013 10:47:21 -0700 Subject: [PATCH 10/31] Accidentally left a debug line in. --- interface/src/Application.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 00833ca8a2..8ce7111873 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -489,8 +489,6 @@ void Application::keyPressEvent(QKeyEvent* event) { return; } - qDebug() << QKeySequence(event->key()); - bool shifted = event->modifiers().testFlag(Qt::ShiftModifier); switch (event->key()) { case Qt::Key_BracketLeft: From 537686d54182ca9ca935f53563fae7d397b044fa Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 14 May 2013 11:06:03 -0700 Subject: [PATCH 11/31] Allow changing the edit voxel size using the mouse wheel. --- interface/src/Application.cpp | 19 +++++++++++++++++++ interface/src/Application.h | 3 +++ 2 files changed, 22 insertions(+) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 8ce7111873..2c60113d46 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -82,6 +83,8 @@ protected: virtual void mouseMoveEvent(QMouseEvent* event); virtual void mousePressEvent(QMouseEvent* event); virtual void mouseReleaseEvent(QMouseEvent* event); + + virtual void wheelEvent(QWheelEvent* event); }; void GLCanvas::initializeGL() { @@ -116,6 +119,10 @@ void GLCanvas::mouseReleaseEvent(QMouseEvent* event) { static_cast(QCoreApplication::instance())->mouseReleaseEvent(event); } +void GLCanvas::wheelEvent(QWheelEvent* event) { + static_cast(QCoreApplication::instance())->wheelEvent(event); +} + Application::Application(int& argc, char** argv) : QApplication(argc, argv), _window(new QMainWindow(desktop())), @@ -720,6 +727,18 @@ void Application::mouseReleaseEvent(QMouseEvent* event) { _mousePressed = false; } } + +void Application::wheelEvent(QWheelEvent* event) { + if (_mouseMode == NO_EDIT_MODE) { + event->ignore(); + return; + } + if (event->delta() > 0) { + _mouseVoxelScale *= 2; + } else { + _mouseVoxelScale /= 2; + } +} // Every second, check the frame rates and other stuff void Application::timer() { diff --git a/interface/src/Application.h b/interface/src/Application.h index c076b91a9e..035f705b41 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -34,6 +34,7 @@ class QGLWidget; class QKeyEvent; class QMainWindow; class QMouseEvent; +class QWheelEvent; class Agent; class ProgramObject; @@ -55,6 +56,8 @@ public: void mousePressEvent(QMouseEvent* event); void mouseReleaseEvent(QMouseEvent* event); + void wheelEvent(QWheelEvent* event); + private slots: void timer(); From a95549da2ec84274e5253bd38ab6a20d2cfd2aa4 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 14 May 2013 11:08:12 -0700 Subject: [PATCH 12/31] added new menu items for view delta --- interface/src/main.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 3b665f1455..725e6a9f95 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -165,6 +165,7 @@ unsigned char dominantColor = 0; // The dominant color of the voxel we're bool perfStatsOn = false; // Do we want to display perfStats? bool wantMonochrome = false; // ask server to send us in monochrome bool wantResIn = false; // ask server to res in +bool wantDelta = false; // ask server to send delta only bool logOn = true; // Whether to show on-screen log @@ -1296,6 +1297,15 @@ int setWantMonochrome(int state) { return value; } +int setWantDelta(int state) { + int value = setValue(state, &::wantDelta); + if (state == MENU_ROW_PICKED) { + ::myAvatar.setWantDelta(::wantDelta); + } + return value; +} + + int setDisplayFrustum(int state) { return setValue(state, &::frustumOn); } @@ -1463,6 +1473,7 @@ void initMenu() { menuColumnDebug->addRow("Calculate Tree Stats", doTreeStats); menuColumnDebug->addRow("Wants Res-In", setWantResIn); menuColumnDebug->addRow("Wants Monochrome", setWantMonochrome); + menuColumnDebug->addRow("Wants View-Delta Only", setWantDelta); } void testPointToVoxel() { From d59a8143d7ecd6bd9b79a024c30dc0e6bf42d46b Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 14 May 2013 11:08:59 -0700 Subject: [PATCH 13/31] added wantDelta, made want bools a bit mask --- libraries/avatars/src/AvatarData.cpp | 16 ++++++++++++---- libraries/avatars/src/AvatarData.h | 8 ++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index 60d28ce6fb..4ca7a7bd7f 100644 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -108,8 +108,12 @@ int AvatarData::getBroadcastData(unsigned char* destinationBuffer) { destinationBuffer += _chatMessage.size() * sizeof(char); // voxel sending features... - *destinationBuffer++ = _wantResIn; - *destinationBuffer++ = _wantColor; + // 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; return destinationBuffer - bufferStart; } @@ -184,8 +188,12 @@ int AvatarData::parseData(unsigned char* sourceBuffer, int numBytes) { sourceBuffer += chatMessageSize * sizeof(char); // voxel sending features... - _wantResIn = (bool)*sourceBuffer++; - _wantColor = (bool)*sourceBuffer++; + 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); return sourceBuffer - startPosition; } diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index dd1b6b970e..0b6c3157d1 100644 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -15,6 +15,10 @@ #include +const int WANT_RESIN_AT_BIT = 0; +const int WANT_COLOR_AT_BIT = 1; +const int WANT_DELTA_AT_BIT = 2; + enum KeyState { NO_KEY_DOWN, @@ -123,8 +127,10 @@ public: // related to Voxel Sending strategies bool getWantResIn() const { return _wantResIn; } bool getWantColor() const { return _wantColor; } + bool getWantDelta() const { return _wantDelta; } void setWantResIn(bool wantResIn) { _wantResIn = wantResIn; } void setWantColor(bool wantColor) { _wantColor = wantColor; } + void setWantDelta(bool wantDelta) { _wantDelta = wantDelta; } protected: glm::vec3 _position; @@ -167,8 +173,10 @@ protected: // chat message std::string _chatMessage; + // voxel server sending items bool _wantResIn; bool _wantColor; + bool _wantDelta; }; #endif /* defined(__hifi__AvatarData__) */ From 2c1518144ad7fb819acd0fe35bd48650d6f58705 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 14 May 2013 11:09:53 -0700 Subject: [PATCH 14/31] added setAtBit() utility --- libraries/shared/src/SharedUtil.cpp | 5 +++++ libraries/shared/src/SharedUtil.h | 1 + 2 files changed, 6 insertions(+) diff --git a/libraries/shared/src/SharedUtil.cpp b/libraries/shared/src/SharedUtil.cpp index 0c399faf6c..d6820897b8 100644 --- a/libraries/shared/src/SharedUtil.cpp +++ b/libraries/shared/src/SharedUtil.cpp @@ -94,6 +94,11 @@ bool oneAtBit(unsigned char byte, int bitIndex) { return (byte >> (7 - bitIndex) & 1); } +void setAtBit(unsigned char& byte, int bitIndex) { + byte += (1 << (7 - bitIndex)); +} + + void switchToResourcesParentIfRequired() { #ifdef __APPLE__ CFBundleRef mainBundle = CFBundleGetMainBundle(); diff --git a/libraries/shared/src/SharedUtil.h b/libraries/shared/src/SharedUtil.h index 9d2fcb8799..98baa5488a 100644 --- a/libraries/shared/src/SharedUtil.h +++ b/libraries/shared/src/SharedUtil.h @@ -49,6 +49,7 @@ void outputBits(unsigned char byte, bool withNewLine = true); void printVoxelCode(unsigned char* voxelCode); int numberOfOnes(unsigned char byte); bool oneAtBit(unsigned char byte, int bitIndex); +void setAtBit(unsigned char& byte, int bitIndex); void switchToResourcesParentIfRequired(); From 062c1b4c640cab42c445411873e5032eee2a2b7c Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 14 May 2013 11:10:40 -0700 Subject: [PATCH 15/31] tweaks to view frustum delta code --- voxel-server/src/VoxelAgentData.cpp | 29 ++++++++++++++++++----------- voxel-server/src/VoxelAgentData.h | 5 ++++- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/voxel-server/src/VoxelAgentData.cpp b/voxel-server/src/VoxelAgentData.cpp index df61a0d6f8..b265ccb2ba 100644 --- a/voxel-server/src/VoxelAgentData.cpp +++ b/voxel-server/src/VoxelAgentData.cpp @@ -51,23 +51,30 @@ VoxelAgentData* VoxelAgentData::clone() const { return new VoxelAgentData(*this); } -void VoxelAgentData::updateViewFrustum() { - // save our currentViewFrustum into our lastKnownViewFrustum - _lastKnownViewFrustum = _currentViewFrustum; - +bool VoxelAgentData::updateCurrentViewFrustum() { + bool currentViewFrustumChanged = false; + ViewFrustum newestViewFrustum; // get position and orientation details from the camera - _currentViewFrustum.setPosition(getCameraPosition()); - _currentViewFrustum.setOrientation(getCameraDirection(), getCameraUp(), getCameraRight()); + newestViewFrustum.setPosition(getCameraPosition()); + newestViewFrustum.setOrientation(getCameraDirection(), getCameraUp(), getCameraRight()); // Also make sure it's got the correct lens details from the camera - _currentViewFrustum.setFieldOfView(getCameraFov()); - _currentViewFrustum.setAspectRatio(getCameraAspectRatio()); - _currentViewFrustum.setNearClip(getCameraNearClip()); - _currentViewFrustum.setFarClip(getCameraFarClip()); + newestViewFrustum.setFieldOfView(getCameraFov()); + newestViewFrustum.setAspectRatio(getCameraAspectRatio()); + newestViewFrustum.setNearClip(getCameraNearClip()); + newestViewFrustum.setFarClip(getCameraFarClip()); // if there has been a change, then recalculate - if (!_lastKnownViewFrustum.matches(_currentViewFrustum)) { + if (!newestViewFrustum.matches(_currentViewFrustum)) { + _currentViewFrustum = newestViewFrustum; _currentViewFrustum.calculate(); + currentViewFrustumChanged = true; } + return currentViewFrustumChanged; +} + +void VoxelAgentData::updateLastKnownViewFrustum() { + // save our currentViewFrustum into our lastKnownViewFrustum + _lastKnownViewFrustum = _currentViewFrustum; } diff --git a/voxel-server/src/VoxelAgentData.h b/voxel-server/src/VoxelAgentData.h index cb88c05574..4bdbb6069d 100644 --- a/voxel-server/src/VoxelAgentData.h +++ b/voxel-server/src/VoxelAgentData.h @@ -44,7 +44,10 @@ public: ViewFrustum& getCurrentViewFrustum() { return _currentViewFrustum; }; ViewFrustum& getLastKnownViewFrustum() { return _lastKnownViewFrustum; }; - void updateViewFrustum(); + // These are not classic setters because they are calculating and maintaining state + // which is set asynchronously through the network receive + bool updateCurrentViewFrustum(); + void updateLastKnownViewFrustum(); private: unsigned char* _voxelPacket; From 1e0a575d908e84506b159dbb552b74a23f59c354 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 14 May 2013 11:11:34 -0700 Subject: [PATCH 16/31] tweaks to view frustum delta code --- voxel-server/src/main.cpp | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/voxel-server/src/main.cpp b/voxel-server/src/main.cpp index e1293b1cdf..88ad060cf9 100644 --- a/voxel-server/src/main.cpp +++ b/voxel-server/src/main.cpp @@ -224,13 +224,22 @@ void resInVoxelDistributor(AgentList* agentList, // Version of voxel distributor that sends the deepest LOD level at once void deepestLevelVoxelDistributor(AgentList* agentList, AgentList::iterator& agent, - VoxelAgentData* agentData) { + VoxelAgentData* agentData, + bool viewFrustumChanged) { int maxLevelReached = 0; double start = usecTimestampNow(); - if (agentData->nodeBag.isEmpty()) { + + // FOR NOW... agent tells us if it wants to receive only view frustum deltas + bool wantDelta = agentData->getWantDelta(); + const ViewFrustum* lastViewFrustum = wantDelta ? &agentData->getLastKnownViewFrustum() : NULL; + + // If the current view frustum has changed OR we have nothing to send, then search against + // the current view frustum for things to send. + if (viewFrustumChanged || agentData->nodeBag.isEmpty()) { + // If the bag was empty, then send everything in view, not just the delta maxLevelReached = randomTree.searchForColoredNodes(INT_MAX, randomTree.rootNode, agentData->getCurrentViewFrustum(), - agentData->nodeBag, true, &agentData->getLastKnownViewFrustum()); + agentData->nodeBag, wantDelta, lastViewFrustum); } double end = usecTimestampNow(); double elapsedmsec = (end - start)/1000.0; @@ -263,8 +272,7 @@ void deepestLevelVoxelDistributor(AgentList* agentList, bytesWritten = randomTree.encodeTreeBitstream(INT_MAX, subTree, &tempOutputBuffer[0], MAX_VOXEL_PACKET_SIZE - 1, agentData->nodeBag, &agentData->getCurrentViewFrustum(), - agentData->getWantColor(), true, - &agentData->getLastKnownViewFrustum()); + agentData->getWantColor(), wantDelta, lastViewFrustum); if (agentData->getAvailable() >= bytesWritten) { agentData->writeToPacket(&tempOutputBuffer[0], bytesWritten); @@ -310,7 +318,15 @@ void deepestLevelVoxelDistributor(AgentList* agentList, printf("packetLoop() took %lf milliseconds to generate %d bytes in %d packets, %d nodes still to send\n", elapsedmsec, trueBytesSent, truePacketsSent, agentData->nodeBag.count()); } - } + + // if after sending packets we've emptied our bag, then we want to remember that we've sent all + // the voxels from the current view frustum + if (agentData->nodeBag.isEmpty()) { + agentData->updateLastKnownViewFrustum(); + } + + + } // end if bag wasn't empty, and so we sent stuff... } void persistVoxelsWhenDirty() { @@ -337,12 +353,12 @@ void *distributeVoxelsToListeners(void *args) { // Sometimes the agent data has not yet been linked, in which case we can't really do anything if (agentData) { - agentData->updateViewFrustum(); + bool viewFrustumChanged = agentData->updateCurrentViewFrustum(); if (agentData->getWantResIn()) { resInVoxelDistributor(agentList, agent, agentData); } else { - deepestLevelVoxelDistributor(agentList, agent, agentData); + deepestLevelVoxelDistributor(agentList, agent, agentData, viewFrustumChanged); } } } From 241c1703459642f5d7d28cf6d32ff1c24fa6da43 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 14 May 2013 11:46:19 -0700 Subject: [PATCH 17/31] refactor find modules so they complain when things are missing --- cmake/modules/FindLibOVR.cmake | 24 ++++++++++++------------ cmake/modules/FindLodePNG.cmake | 30 +++++++++++------------------- cmake/modules/FindPortAudio.cmake | 28 ++++++++++++++-------------- interface/CMakeLists.txt | 4 ++-- 4 files changed, 39 insertions(+), 47 deletions(-) diff --git a/cmake/modules/FindLibOVR.cmake b/cmake/modules/FindLibOVR.cmake index cad962732c..4b4d35bb55 100644 --- a/cmake/modules/FindLibOVR.cmake +++ b/cmake/modules/FindLibOVR.cmake @@ -6,31 +6,31 @@ # # LIBOVR_FOUND - system found LibOVR # LIBOVR_INCLUDE_DIRS - the LibOVR include directory -# LIBOVR_LIBRARY - Link this to use LibOVR +# LIBOVR_LIBRARIES - Link this to use LibOVR # # Created on 5/9/2013 by Stephen Birarda # Copyright (c) 2013 High Fidelity # -if (LIBOVR_LIBRARY AND LIBOVR_INCLUDE_DIRS) +if (LIBOVR_LIBRARIES AND LIBOVR_INCLUDE_DIRS) # in cache already set(LIBOVR_FOUND TRUE) -else (LIBOVR_LIBRARY AND LIBOVR_INCLUDE_DIRS) - set(LIBOVR_INCLUDE_DIRS ${LIBOVR_ROOT_DIR}/Include) +else (LIBOVR_LIBRARIES AND LIBOVR_INCLUDE_DIRS) + find_path(LIBOVR_INCLUDE_DIRS OVR.h ${LIBOVR_ROOT_DIR}/Include) if (APPLE) - set(LIBOVR_LIBRARY ${LIBOVR_ROOT_DIR}/Lib/MacOS/libovr.a) + find_library(LIBOVR_LIBRARIES libovr.a ${LIBOVR_ROOT_DIR}/Lib/MacOS/) else (WIN32) - set(LIBOVR_LIBRARY ${LIBOVR_ROOT_DIR}/Lib/Win32/libovr.lib) + find_library(LIBOVR_LIBRARIES libovr.lib ${LIBOVR_ROOT_DIR}/Lib/Win32/) endif () - if (LIBOVR_INCLUDE_DIRS AND LIBOVR_LIBRARY) + if (LIBOVR_INCLUDE_DIRS AND LIBOVR_LIBRARIES) set(LIBOVR_FOUND TRUE) - endif (LIBOVR_INCLUDE_DIRS AND LIBOVR_LIBRARY) + endif (LIBOVR_INCLUDE_DIRS AND LIBOVR_LIBRARIES) if (LIBOVR_FOUND) if (NOT LibOVR_FIND_QUIETLY) - message(STATUS "Found LibOVR: ${LIBOVR_LIBRARY}") + message(STATUS "Found LibOVR: ${LIBOVR_LIBRARIES}") endif (NOT LibOVR_FIND_QUIETLY) else (LIBOVR_FOUND) if (LibOVR_FIND_REQUIRED) @@ -38,7 +38,7 @@ else (LIBOVR_LIBRARY AND LIBOVR_INCLUDE_DIRS) endif (LibOVR_FIND_REQUIRED) endif (LIBOVR_FOUND) - # show the LIBOVR_INCLUDE_DIRS and LIBOVR_LIBRARY variables only in the advanced view - mark_as_advanced(LIBOVR_INCLUDE_DIRS LIBOVR_LIBRARY) + # show the LIBOVR_INCLUDE_DIRS and LIBOVR_LIBRARIES variables only in the advanced view + mark_as_advanced(LIBOVR_INCLUDE_DIRS LIBOVR_LIBRARIES) -endif (LIBOVR_LIBRARY AND LIBOVR_INCLUDE_DIRS) \ No newline at end of file +endif (LIBOVR_LIBRARIES AND LIBOVR_INCLUDE_DIRS) \ No newline at end of file diff --git a/cmake/modules/FindLodePNG.cmake b/cmake/modules/FindLodePNG.cmake index 4568d59765..c36e238029 100644 --- a/cmake/modules/FindLodePNG.cmake +++ b/cmake/modules/FindLodePNG.cmake @@ -6,34 +6,26 @@ # # LODEPNG_FOUND - system has LODEPNG_FOUND # LODEPNG_INCLUDE_DIRS - the LodePNG include directory -# LODEPNG_LIBRARY - Link these to use LodePNG +# LODEPNG_LIBRARIES- Link these to use LodePNG # # Copyright (c) 2013 Stephen Birarda # -if (LODEPNG_LIBRARY AND LODEPNG_INCLUDE_DIRS) +if (LODEPNG_LIBRARIES AND LODEPNG_INCLUDE_DIRS) # in cache already set(LODEPNG_FOUND TRUE) -else (LODEPNG_LIBRARY AND LODEPNG_INCLUDE_DIRS) +else (LODEPNG_LIBRARIES AND LODEPNG_INCLUDE_DIRS) - FIND_PATH(LODEPNG_INCLUDE_DIR "lodepng.h" - PATHS ${LODEPNG_ROOT_DIR}) + find_path(LODEPNG_INCLUDE_DIRS lodepng.h ${LODEPNG_ROOT_DIR}) + find_file(LODEPNG_LIBRARIES lodepng.cpp ${LODEPNG_ROOT_DIR}) - set(LODEPNG_INCLUDE_DIRS - ${LODEPNG_INCLUDE_DIR} - ) - - set(LODEPNG_LIBRARY - ${LODEPNG_ROOT_DIR}/lodepng.cpp - ) - - if (LODEPNG_INCLUDE_DIRS AND LODEPNG_LIBRARY) + if (LODEPNG_INCLUDE_DIRS AND LODEPNG_LIBRARIES) set(LODEPNG_FOUND TRUE) - endif (LODEPNG_INCLUDE_DIRS AND LODEPNG_LIBRARY) + endif (LODEPNG_INCLUDE_DIRS AND LODEPNG_LIBRARIES) if (LODEPNG_FOUND) if (NOT LodePNG_FIND_QUIETLY) - message(STATUS "Found LodePNG: ${LODEPNG_LIBRARY}") + message(STATUS "Found LodePNG: ${LODEPNG_LIBRARIES}") endif (NOT LodePNG_FIND_QUIETLY) else (LODEPNG_FOUND) if (LodePNG_FIND_REQUIRED) @@ -41,7 +33,7 @@ else (LODEPNG_LIBRARY AND LODEPNG_INCLUDE_DIRS) endif (LodePNG_FIND_REQUIRED) endif (LODEPNG_FOUND) - # show the LODEPNG_INCLUDE_DIRS and LODEPNG_LIBRARY variables only in the advanced view - mark_as_advanced(LODEPNG_INCLUDE_DIRS LODEPNG_LIBRARY) + # show the LODEPNG_INCLUDE_DIRS and LODEPNG_LIBRARIES variables only in the advanced view + mark_as_advanced(LODEPNG_INCLUDE_DIRS LODEPNG_LIBRARIES) -endif (LODEPNG_LIBRARY AND LODEPNG_INCLUDE_DIRS) \ No newline at end of file +endif (LODEPNG_LIBRARIES AND LODEPNG_INCLUDE_DIRS) \ No newline at end of file diff --git a/cmake/modules/FindPortAudio.cmake b/cmake/modules/FindPortAudio.cmake index e2a1d795d9..9b6ac884b6 100644 --- a/cmake/modules/FindPortAudio.cmake +++ b/cmake/modules/FindPortAudio.cmake @@ -6,31 +6,31 @@ # # PORTAUDIO_FOUND - system found PortAudio # PORTAUDIO_INCLUDE_DIRS - the PortAudio include directory -# PORTAUDIO_LIBRARY - Link this to use PortAudio +# PORTAUDIO_LIBRARIES - Link this to use PortAudio # # Created on 5/14/2013 by Stephen Birarda # Copyright (c) 2013 High Fidelity # -if (PORTAUDIO_LIBRARY AND PORTAUDIO_INCLUDE_DIRS) +if (PORTAUDIO_LIBRARIES AND PORTAUDIO_INCLUDE_DIRS) # in cache already set(PORTAUDIO_FOUND TRUE) -else (PORTAUDIO_LIBRARY AND PORTAUDIO_INCLUDE_DIRS) - set(PORTAUDIO_INCLUDE_DIRS ${PORTAUDIO_ROOT_DIR}/include) +else (PORTAUDIO_LIBRARIES AND PORTAUDIO_INCLUDE_DIRS) + find_path(PORTAUDIO_INCLUDE_DIRS portaudio.h ${PORTAUDIO_ROOT_DIR}/include) if (APPLE) - set(PORTAUDIO_LIBRARY ${PORTAUDIO_ROOT_DIR}/lib/MacOS/libportaudio.a) - else (WIN32) - set(PORTAUDIO_LIBRARY ${PORTAUDIO_ROOT_DIR}/lib/UNIX/libportaudio.a) + find_library(PORTAUDIO_LIBRARIES libportaudio.a ${PORTAUDIO_ROOT_DIR}/lib/MacOS/) + else (UNIX) + find_library(PORTAUDIO_LIBRARIES libportaudio.a ${PORTAUDIO_ROOT_DIR}/lib/UNIX/) endif () - if (PORTAUDIO_INCLUDE_DIRS AND PORTAUDIO_LIBRARY) - set(PORTAUDIO_FOUND TRUE) - endif (PORTAUDIO_INCLUDE_DIRS AND PORTAUDIO_LIBRARY) + if (PORTAUDIO_INCLUDE_DIRS AND PORTAUDIO_LIBRARIES) + set(PORTAUDIO_FOUND TRUE) + endif (PORTAUDIO_INCLUDE_DIRS AND PORTAUDIO_LIBRARIES) if (PORTAUDIO_FOUND) if (NOT PortAudio_FIND_QUIETLY) - message(STATUS "Found PortAudio: ${PORTAUDIO_LIBRARY}") + message(STATUS "Found PortAudio: ${PORTAUDIO_LIBRARIES}") endif (NOT PortAudio_FIND_QUIETLY) else (PORTAUDIO_FOUND) if (PortAudio_FIND_REQUIRED) @@ -38,7 +38,7 @@ else (PORTAUDIO_LIBRARY AND PORTAUDIO_INCLUDE_DIRS) endif (PortAudio_FIND_REQUIRED) endif (PORTAUDIO_FOUND) - # show the PORTAUDIO_INCLUDE_DIRS and PORTAUDIO_LIBRARY variables only in the advanced view - mark_as_advanced(PORTAUDIO_INCLUDE_DIRS PORTAUDIO_LIBRARY) + # show the PORTAUDIO_INCLUDE_DIRS and PORTAUDIO_LIBRARIES variables only in the advanced view + mark_as_advanced(PORTAUDIO_INCLUDE_DIRS PORTAUDIO_LIBRARIES) -endif (PORTAUDIO_LIBRARY AND PORTAUDIO_INCLUDE_DIRS) \ No newline at end of file +endif (PORTAUDIO_LIBRARIES AND PORTAUDIO_INCLUDE_DIRS) \ No newline at end of file diff --git a/interface/CMakeLists.txt b/interface/CMakeLists.txt index 78f736b343..7a8fc599e4 100644 --- a/interface/CMakeLists.txt +++ b/interface/CMakeLists.txt @@ -109,7 +109,7 @@ if (APPLE) ${GLUT} ${OpenGL} ${IOKit} - ${LIBOVR_LIBRARY} + ${LIBOVR_LIBRARIES} ) else (APPLE) find_package(OpenGL REQUIRED) @@ -134,7 +134,7 @@ else (WIN32) # link the PortAudio library find_package(PortAudio REQUIRED) include_directories(${PORTAUDIO_INCLUDE_DIRS}) - target_link_libraries(${TARGET_NAME} ${PORTAUDIO_LIBRARY}) + target_link_libraries(${TARGET_NAME} ${PORTAUDIO_LIBRARIES}) # link required libraries on UNIX if (UNIX AND NOT APPLE) From c117d62532323d5d1e70d7ce1bc88f27d75476eb Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 14 May 2013 11:54:35 -0700 Subject: [PATCH 18/31] fix old reference to LODEPNG_LIBRARY --- interface/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/CMakeLists.txt b/interface/CMakeLists.txt index 7a8fc599e4..4f96770120 100644 --- a/interface/CMakeLists.txt +++ b/interface/CMakeLists.txt @@ -129,7 +129,7 @@ if (WIN32) wsock32.lib ) else (WIN32) - target_link_libraries(${TARGET_NAME} ${LODEPNG_LIBRARY}) + target_link_libraries(${TARGET_NAME} ${LODEPNG_LIBRARIES}) # link the PortAudio library find_package(PortAudio REQUIRED) From c3bbebee588f20b7c11d032b4a188e7082ff65b0 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 14 May 2013 11:59:17 -0700 Subject: [PATCH 19/31] don't allow CMake to escape UNIX in path for PortAudio --- cmake/modules/FindPortAudio.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/modules/FindPortAudio.cmake b/cmake/modules/FindPortAudio.cmake index 9b6ac884b6..cbcc3dc28d 100644 --- a/cmake/modules/FindPortAudio.cmake +++ b/cmake/modules/FindPortAudio.cmake @@ -21,7 +21,7 @@ else (PORTAUDIO_LIBRARIES AND PORTAUDIO_INCLUDE_DIRS) if (APPLE) find_library(PORTAUDIO_LIBRARIES libportaudio.a ${PORTAUDIO_ROOT_DIR}/lib/MacOS/) else (UNIX) - find_library(PORTAUDIO_LIBRARIES libportaudio.a ${PORTAUDIO_ROOT_DIR}/lib/UNIX/) + find_library(PORTAUDIO_LIBRARIES libportaudio.a "${PORTAUDIO_ROOT_DIR}/lib/UNIX/") endif () if (PORTAUDIO_INCLUDE_DIRS AND PORTAUDIO_LIBRARIES) From 9a163d2a72bd1f156581b78b518f6cfaf6bc4348 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 14 May 2013 12:02:54 -0700 Subject: [PATCH 20/31] conditional repairs for custom modules --- cmake/modules/FindLibOVR.cmake | 2 +- cmake/modules/FindPortAudio.cmake | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmake/modules/FindLibOVR.cmake b/cmake/modules/FindLibOVR.cmake index 4b4d35bb55..56f9ece6d8 100644 --- a/cmake/modules/FindLibOVR.cmake +++ b/cmake/modules/FindLibOVR.cmake @@ -20,7 +20,7 @@ else (LIBOVR_LIBRARIES AND LIBOVR_INCLUDE_DIRS) if (APPLE) find_library(LIBOVR_LIBRARIES libovr.a ${LIBOVR_ROOT_DIR}/Lib/MacOS/) - else (WIN32) + elseif (WIN32) find_library(LIBOVR_LIBRARIES libovr.lib ${LIBOVR_ROOT_DIR}/Lib/Win32/) endif () diff --git a/cmake/modules/FindPortAudio.cmake b/cmake/modules/FindPortAudio.cmake index cbcc3dc28d..4a796e16b4 100644 --- a/cmake/modules/FindPortAudio.cmake +++ b/cmake/modules/FindPortAudio.cmake @@ -20,8 +20,8 @@ else (PORTAUDIO_LIBRARIES AND PORTAUDIO_INCLUDE_DIRS) if (APPLE) find_library(PORTAUDIO_LIBRARIES libportaudio.a ${PORTAUDIO_ROOT_DIR}/lib/MacOS/) - else (UNIX) - find_library(PORTAUDIO_LIBRARIES libportaudio.a "${PORTAUDIO_ROOT_DIR}/lib/UNIX/") + elseif (UNIX) + find_library(PORTAUDIO_LIBRARIES libportaudio.a ${PORTAUDIO_ROOT_DIR}/lib/UNIX/) endif () if (PORTAUDIO_INCLUDE_DIRS AND PORTAUDIO_LIBRARIES) From 4e92f5d3d81a94dcc5ed441ec51aa6fc11f7f817 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 14 May 2013 12:11:32 -0700 Subject: [PATCH 21/31] tweaks to delta sending --- libraries/avatars/src/AvatarData.cpp | 2 ++ libraries/voxels/src/VoxelTree.cpp | 11 ++++++----- voxel-server/src/VoxelAgentData.cpp | 1 + voxel-server/src/VoxelAgentData.h | 4 ++++ voxel-server/src/main.cpp | 11 +++++++++++ 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/libraries/avatars/src/AvatarData.cpp b/libraries/avatars/src/AvatarData.cpp index 4ca7a7bd7f..1f4856abf7 100644 --- a/libraries/avatars/src/AvatarData.cpp +++ b/libraries/avatars/src/AvatarData.cpp @@ -121,6 +121,8 @@ int AvatarData::getBroadcastData(unsigned char* destinationBuffer) { // called on the other agents - assigns it to my views of the others int AvatarData::parseData(unsigned char* sourceBuffer, int numBytes) { +//printf("AvatarData::parseData()\n"); + // increment to push past the packet header sourceBuffer += sizeof(PACKET_HEADER_HEAD_DATA); diff --git a/libraries/voxels/src/VoxelTree.cpp b/libraries/voxels/src/VoxelTree.cpp index 0476ca0d40..d22073a54b 100644 --- a/libraries/voxels/src/VoxelTree.cpp +++ b/libraries/voxels/src/VoxelTree.cpp @@ -853,10 +853,8 @@ int VoxelTree::encodeTreeBitstreamRecursion(int maxEncodeLevel, int& currentEnco for (int i = 0; i < NUMBER_OF_CHILDREN; i++) { VoxelNode* childNode = node->getChildAtIndex(i); bool childIsInView = (childNode && (!viewFrustum || childNode->isInView(*viewFrustum))); - bool childWasInView = (childNode && deltaViewFrustum && - (lastViewFrustum && ViewFrustum::INSIDE == childNode->inFrustum(*lastViewFrustum))); - if (childIsInView && !childWasInView) { + if (childIsInView) { // Before we determine consider this further, let's see if it's in our LOD scope... float distance = viewFrustum ? childNode->distanceToCamera(*viewFrustum) : 0; float boundaryDistance = viewFrustum ? boundaryDistanceForRenderLevel(*childNode->getOctalCode() + 1) : 1; @@ -871,9 +869,12 @@ int VoxelTree::encodeTreeBitstreamRecursion(int maxEncodeLevel, int& currentEnco childrenExistBits += (1 << (7 - i)); inViewNotLeafCount++; } + + bool childWasInView = (childNode && deltaViewFrustum && + (lastViewFrustum && ViewFrustum::INSIDE == childNode->inFrustum(*lastViewFrustum))); - // track children with actual color - if (childNode && childNode->isColored()) { + // track children with actual color, only if the child wasn't previously in view! + if (childNode && childNode->isColored() && !childWasInView) { childrenColoredBits += (1 << (7 - i)); inViewWithColorCount++; } diff --git a/voxel-server/src/VoxelAgentData.cpp b/voxel-server/src/VoxelAgentData.cpp index b265ccb2ba..4eb971f275 100644 --- a/voxel-server/src/VoxelAgentData.cpp +++ b/voxel-server/src/VoxelAgentData.cpp @@ -22,6 +22,7 @@ void VoxelAgentData::init() { _maxSearchLevel = 1; _maxLevelReachedInLastSearch = 1; resetVoxelPacket(); + _viewSent = false; } void VoxelAgentData::resetVoxelPacket() { diff --git a/voxel-server/src/VoxelAgentData.h b/voxel-server/src/VoxelAgentData.h index 4bdbb6069d..99be7e6042 100644 --- a/voxel-server/src/VoxelAgentData.h +++ b/voxel-server/src/VoxelAgentData.h @@ -49,7 +49,11 @@ public: bool updateCurrentViewFrustum(); void updateLastKnownViewFrustum(); + bool getViewSent() const { return _viewSent; }; + void setViewSent(bool viewSent) { _viewSent = viewSent; } + private: + bool _viewSent; unsigned char* _voxelPacket; unsigned char* _voxelPacketAt; int _voxelPacketAvailableBytes; diff --git a/voxel-server/src/main.cpp b/voxel-server/src/main.cpp index 88ad060cf9..d15a5325f6 100644 --- a/voxel-server/src/main.cpp +++ b/voxel-server/src/main.cpp @@ -234,12 +234,21 @@ void deepestLevelVoxelDistributor(AgentList* agentList, bool wantDelta = agentData->getWantDelta(); const ViewFrustum* lastViewFrustum = wantDelta ? &agentData->getLastKnownViewFrustum() : NULL; +printf("deepestLevelVoxelDistributor() viewFrustumChanged=%s, nodeBag.isEmpty=%s, viewSent=%s\n", + viewFrustumChanged ? "yes" : "no", + agentData->nodeBag.isEmpty() ? "yes" : "no", + agentData->getViewSent() ? "yes" : "no" + ); + // If the current view frustum has changed OR we have nothing to send, then search against // the current view frustum for things to send. if (viewFrustumChanged || agentData->nodeBag.isEmpty()) { // If the bag was empty, then send everything in view, not just the delta maxLevelReached = randomTree.searchForColoredNodes(INT_MAX, randomTree.rootNode, agentData->getCurrentViewFrustum(), agentData->nodeBag, wantDelta, lastViewFrustum); + + agentData->setViewSent(false); + } double end = usecTimestampNow(); double elapsedmsec = (end - start)/1000.0; @@ -323,6 +332,7 @@ void deepestLevelVoxelDistributor(AgentList* agentList, // the voxels from the current view frustum if (agentData->nodeBag.isEmpty()) { agentData->updateLastKnownViewFrustum(); + agentData->setViewSent(true); } @@ -354,6 +364,7 @@ void *distributeVoxelsToListeners(void *args) { // Sometimes the agent data has not yet been linked, in which case we can't really do anything if (agentData) { bool viewFrustumChanged = agentData->updateCurrentViewFrustum(); + printf("agentData->updateCurrentViewFrustum() viewFrustumChanged=%s\n", (viewFrustumChanged ? "yes" : "no")); if (agentData->getWantResIn()) { resInVoxelDistributor(agentList, agent, agentData); From 306d2a4b989a919275e18d1d27c28d4d200c08d5 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 14 May 2013 12:12:36 -0700 Subject: [PATCH 22/31] casing is important for UNIX --- interface/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/CMakeLists.txt b/interface/CMakeLists.txt index 4f96770120..be966f549f 100644 --- a/interface/CMakeLists.txt +++ b/interface/CMakeLists.txt @@ -10,7 +10,7 @@ project(${TARGET_NAME}) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/../cmake/modules/") set(LODEPNG_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/LodePNG) set(LIBOVR_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/LibOVR) -set(PORTAUDIO_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/portaudio) +set(PORTAUDIO_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/PortAudio) if (APPLE) set(GL_HEADERS "#include \n#include ") From 43e190fe3ceb0024827cac555d5dc9263327eb36 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 14 May 2013 12:19:23 -0700 Subject: [PATCH 23/31] switched Want Delta menu to use new Qt menus --- interface/src/Application.cpp | 6 ++++++ interface/src/Application.h | 1 + 2 files changed, 7 insertions(+) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 7844f08686..eaebaee9ff 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1035,6 +1035,10 @@ void Application::setWantsMonochrome(bool wantsMonochrome) { void Application::setWantsResIn(bool wantsResIn) { _myAvatar.setWantResIn(wantsResIn); } + +void Application::setWantsDelta(bool wantsDelta) { + _myAvatar.setWantDelta(wantsDelta); +} void Application::initMenu() { QMenuBar* menuBar = new QMenuBar(); @@ -1096,10 +1100,12 @@ void Application::initMenu() { debugMenu->addAction("Calculate Tree Stats", this, SLOT(doTreeStats()), Qt::SHIFT | Qt::Key_S); 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); } void Application::updateFrustumRenderModeAction() { switch (_frustumDrawingMode) { + default: case FRUSTUM_DRAW_MODE_ALL: _frustumRenderModeAction->setText("Render Mode - All"); break; diff --git a/interface/src/Application.h b/interface/src/Application.h index 035f705b41..1b1eb69fb3 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -87,6 +87,7 @@ private slots: void doTreeStats(); void setWantsMonochrome(bool wantsMonochrome); void setWantsResIn(bool wantsResIn); + void setWantsDelta(bool wantsDelta); private: From b3f6d40935fd88e7d524d4d361871c0a81db7ea6 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 14 May 2013 12:22:26 -0700 Subject: [PATCH 24/31] hide debug messages unless requested --- voxel-server/src/main.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/voxel-server/src/main.cpp b/voxel-server/src/main.cpp index d15a5325f6..74a2ca73ca 100644 --- a/voxel-server/src/main.cpp +++ b/voxel-server/src/main.cpp @@ -233,12 +233,14 @@ void deepestLevelVoxelDistributor(AgentList* agentList, // FOR NOW... agent tells us if it wants to receive only view frustum deltas bool wantDelta = agentData->getWantDelta(); const ViewFrustum* lastViewFrustum = wantDelta ? &agentData->getLastKnownViewFrustum() : NULL; - -printf("deepestLevelVoxelDistributor() viewFrustumChanged=%s, nodeBag.isEmpty=%s, viewSent=%s\n", - viewFrustumChanged ? "yes" : "no", - agentData->nodeBag.isEmpty() ? "yes" : "no", - agentData->getViewSent() ? "yes" : "no" - ); + + if (::debugVoxelSending) { + printf("deepestLevelVoxelDistributor() viewFrustumChanged=%s, nodeBag.isEmpty=%s, viewSent=%s\n", + viewFrustumChanged ? "yes" : "no", + agentData->nodeBag.isEmpty() ? "yes" : "no", + agentData->getViewSent() ? "yes" : "no" + ); + } // If the current view frustum has changed OR we have nothing to send, then search against // the current view frustum for things to send. @@ -364,7 +366,9 @@ void *distributeVoxelsToListeners(void *args) { // Sometimes the agent data has not yet been linked, in which case we can't really do anything if (agentData) { bool viewFrustumChanged = agentData->updateCurrentViewFrustum(); - printf("agentData->updateCurrentViewFrustum() viewFrustumChanged=%s\n", (viewFrustumChanged ? "yes" : "no")); + if (::debugVoxelSending) { + printf("agentData->updateCurrentViewFrustum() changed=%s\n", (viewFrustumChanged ? "yes" : "no")); + } if (agentData->getWantResIn()) { resInVoxelDistributor(agentList, agent, agentData); From 903315240147a6c58c8210e9cb30ce09bbda956b Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 14 May 2013 13:49:24 -0700 Subject: [PATCH 25/31] Color selection for painting. --- interface/src/Application.cpp | 33 ++++++++++++++++++++++++++------- interface/src/Application.h | 2 ++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 7844f08686..b7761a7042 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -18,6 +18,7 @@ #include #endif +#include #include #include #include @@ -848,9 +849,10 @@ void Application::idle() { } if (_mouseMode == COLOR_VOXEL_MODE) { - _mouseVoxel.red = 0; - _mouseVoxel.green = 255; - _mouseVoxel.blue = 0; + QColor paintColor = _voxelPaintColor->data().value(); + _mouseVoxel.red = paintColor.red(); + _mouseVoxel.green = paintColor.green(); + _mouseVoxel.blue = paintColor.blue(); } else if (_mouseMode == DELETE_VOXEL_MODE) { // red indicates deletion @@ -1035,7 +1037,21 @@ void Application::setWantsMonochrome(bool wantsMonochrome) { void Application::setWantsResIn(bool wantsResIn) { _myAvatar.setWantResIn(wantsResIn); } - + +static QIcon createSwatchIcon(const QColor& color) { + QPixmap map(16, 16); + map.fill(color); + return QIcon(map); +} + +void Application::chooseVoxelPaintColor() { + QColor selected = QColorDialog::getColor(_voxelPaintColor->data().value(), _glWidget, "Voxel Paint Color"); + if (selected.isValid()) { + _voxelPaintColor->setData(selected); + _voxelPaintColor->setIcon(createSwatchIcon(selected)); + } +} + void Application::initMenu() { QMenuBar* menuBar = new QMenuBar(); _window->setMenuBar(menuBar); @@ -1071,6 +1087,8 @@ void Application::initMenu() { _renderStatsOn->setShortcut(Qt::Key_Slash); (_logOn = toolsMenu->addAction("Log"))->setCheckable(true); _logOn->setChecked(true); + _voxelPaintColor = toolsMenu->addAction("Voxel Paint Color", this, SLOT(chooseVoxelPaintColor()), Qt::Key_7); + _voxelPaintColor->setIcon(createSwatchIcon(QColor())); QMenu* frustumMenu = menuBar->addMenu("Frustum"); (_frustumOn = frustumMenu->addAction("Display Frustum"))->setCheckable(true); @@ -1866,9 +1884,10 @@ void Application::addVoxelInFrontOfAvatar() { detail.x = detail.s * floor(position.x / detail.s); detail.y = detail.s * floor(position.y / detail.s); detail.z = detail.s * floor(position.z / detail.s); - detail.red = 128; - detail.green = 128; - detail.blue = 128; + QColor paintColor = _voxelPaintColor->data().value(); + detail.red = paintColor.red(); + detail.green = paintColor.green(); + detail.blue = paintColor.blue(); sendVoxelEditMessage(PACKET_HEADER_SET_VOXEL, detail); diff --git a/interface/src/Application.h b/interface/src/Application.h index 035f705b41..6ad644de63 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -87,6 +87,7 @@ private slots: void doTreeStats(); void setWantsMonochrome(bool wantsMonochrome); void setWantsResIn(bool wantsResIn); + void chooseVoxelPaintColor(); private: @@ -133,6 +134,7 @@ private: QAction* _oculusOn; // Whether to configure the display for the Oculus Rift QAction* _renderStatsOn; // Whether to show onscreen text overlay with stats QAction* _logOn; // Whether to show on-screen log + QAction* _voxelPaintColor; // The color with which to paint voxels QAction* _frustumOn; // Whether or not to display the debug view frustum QAction* _viewFrustumFromOffset; // Whether or not to offset the view of the frustum QAction* _cameraFrustum; // which frustum to look at From 8dc61d89fcb58a00e3fd1117e05939b874f136fb Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 14 May 2013 14:19:31 -0700 Subject: [PATCH 26/31] Added support for "destructive" vs "non-destructive" voxel create - changed VoxelTree to support destructive and non-destructive mode - added protocol packet headers to differentiate - added new UI to switch modes - default mode now, non-destructive. --- interface/src/Application.cpp | 19 ++++++++---- interface/src/Application.h | 2 ++ interface/src/VoxelSystem.cpp | 14 +++++---- interface/src/VoxelSystem.h | 8 +++-- libraries/shared/src/PacketHeaders.h | 1 + libraries/voxels/src/VoxelTree.cpp | 45 ++++++++++++++++------------ libraries/voxels/src/VoxelTree.h | 34 +++++++++++---------- voxel-server/src/main.cpp | 8 +++-- 8 files changed, 79 insertions(+), 52 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index eaebaee9ff..0a6fd4e948 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -147,6 +147,7 @@ Application::Application(int& argc, char** argv) : _paintOn(false), _dominantColor(0), _perfStatsOn(false), + _destructiveAddVoxel(false), _chatEntryOn(false), _oculusTextureID(0), _oculusProgram(0), @@ -989,6 +990,10 @@ void Application::cycleFrustumRenderMode() { updateFrustumRenderModeAction(); } +void Application::setDestructivePaint(bool destructive) { + _destructiveAddVoxel = destructive; +} + void Application::setRenderWarnings(bool renderWarnings) { _voxels.setRenderPipelineWarnings(renderWarnings); } @@ -1075,6 +1080,7 @@ void Application::initMenu() { _renderStatsOn->setShortcut(Qt::Key_Slash); (_logOn = toolsMenu->addAction("Log"))->setCheckable(true); _logOn->setChecked(true); + toolsMenu->addAction("Create Voxel is Destructive", this, SLOT(setDestructivePaint(bool)))->setCheckable(true); QMenu* frustumMenu = menuBar->addMenu("Frustum"); (_frustumOn = frustumMenu->addAction("Display Frustum"))->setCheckable(true); @@ -1295,7 +1301,8 @@ void Application::updateAvatar(float deltaTime) { _paintingVoxel.y >= 0.0 && _paintingVoxel.y <= 1.0 && _paintingVoxel.z >= 0.0 && _paintingVoxel.z <= 1.0) { - sendVoxelEditMessage(PACKET_HEADER_SET_VOXEL, _paintingVoxel); + PACKET_HEADER message = (_destructiveAddVoxel ? PACKET_HEADER_SET_VOXEL_DESTRUCTIVE : PACKET_HEADER_SET_VOXEL); + sendVoxelEditMessage(message, _paintingVoxel); } } } @@ -1876,19 +1883,21 @@ void Application::addVoxelInFrontOfAvatar() { detail.green = 128; detail.blue = 128; - sendVoxelEditMessage(PACKET_HEADER_SET_VOXEL, detail); + PACKET_HEADER message = (_destructiveAddVoxel ? PACKET_HEADER_SET_VOXEL_DESTRUCTIVE : PACKET_HEADER_SET_VOXEL); + sendVoxelEditMessage(message, detail); // create the voxel locally so it appears immediately - _voxels.createVoxel(detail.x, detail.y, detail.z, detail.s, detail.red, detail.green, detail.blue); + _voxels.createVoxel(detail.x, detail.y, detail.z, detail.s, detail.red, detail.green, detail.blue, _destructiveAddVoxel); } void Application::addVoxelUnderCursor() { if (_mouseVoxel.s != 0) { - sendVoxelEditMessage(PACKET_HEADER_SET_VOXEL, _mouseVoxel); + PACKET_HEADER message = (_destructiveAddVoxel ? PACKET_HEADER_SET_VOXEL_DESTRUCTIVE : PACKET_HEADER_SET_VOXEL); + sendVoxelEditMessage(message, _mouseVoxel); // create the voxel locally so it appears immediately _voxels.createVoxel(_mouseVoxel.x, _mouseVoxel.y, _mouseVoxel.z, _mouseVoxel.s, - _mouseVoxel.red, _mouseVoxel.green, _mouseVoxel.blue); + _mouseVoxel.red, _mouseVoxel.green, _mouseVoxel.blue, _destructiveAddVoxel); } } diff --git a/interface/src/Application.h b/interface/src/Application.h index 1b1eb69fb3..e22409a77b 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -76,6 +76,7 @@ private slots: void setFrustumOffset(bool frustumOffset); void cycleFrustumRenderMode(); + void setDestructivePaint(bool destructive); void setRenderWarnings(bool renderWarnings); void doKillLocalVoxels(); void doRandomizeVoxelColors(); @@ -200,6 +201,7 @@ private: VoxelDetail _paintingVoxel; // The voxel we're painting if we're painting bool _perfStatsOn; // Do we want to display perfStats? + bool _destructiveAddVoxel; // when doing voxel editing do we want them to be destructive ChatEntry _chatEntry; // chat entry field bool _chatEntryOn; // Whether to show the chat entry diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index b3c483717d..513253ec19 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -1020,22 +1020,24 @@ VoxelNode* VoxelSystem::getVoxelAt(float x, float y, float z, float s) const { return _tree->getVoxelAt(x, y, z, s); }; -void VoxelSystem::createVoxel(float x, float y, float z, float s, unsigned char red, unsigned char green, unsigned char blue) { +void VoxelSystem::createVoxel(float x, float y, float z, float s, + unsigned char red, unsigned char green, unsigned char blue, bool destructive) { pthread_mutex_lock(&_treeLock); //printLog("VoxelSystem::createVoxel(%f,%f,%f,%f)\n",x,y,z,s); - _tree->createVoxel(x, y, z, s, red, green, blue); + _tree->createVoxel(x, y, z, s, red, green, blue, destructive); setupNewVoxelsForDrawing(); pthread_mutex_unlock(&_treeLock); }; -void VoxelSystem::createLine(glm::vec3 point1, glm::vec3 point2, float unitSize, rgbColor color) { - _tree->createLine(point1, point2, unitSize, color); +void VoxelSystem::createLine(glm::vec3 point1, glm::vec3 point2, float unitSize, rgbColor color, bool destructive) { + _tree->createLine(point1, point2, unitSize, color, destructive); setupNewVoxelsForDrawing(); }; -void VoxelSystem::createSphere(float r,float xc, float yc, float zc, float s, bool solid, creationMode mode, bool debug) { - _tree->createSphere(r, xc, yc, zc, s, solid, mode, debug); +void VoxelSystem::createSphere(float r,float xc, float yc, float zc, float s, bool solid, + creationMode mode, bool destructive, bool debug) { + _tree->createSphere(r, xc, yc, zc, s, solid, mode, destructive, debug); setupNewVoxelsForDrawing(); }; diff --git a/interface/src/VoxelSystem.h b/interface/src/VoxelSystem.h index 31bf781311..50997d84bf 100644 --- a/interface/src/VoxelSystem.h +++ b/interface/src/VoxelSystem.h @@ -73,9 +73,11 @@ public: void deleteVoxelAt(float x, float y, float z, float s); VoxelNode* getVoxelAt(float x, float y, float z, float s) const; - void createVoxel(float x, float y, float z, float s, unsigned char red, unsigned char green, unsigned char blue); - void createLine(glm::vec3 point1, glm::vec3 point2, float unitSize, rgbColor color); - void createSphere(float r,float xc, float yc, float zc, float s, bool solid, creationMode mode, bool debug = false); + void createVoxel(float x, float y, float z, float s, + unsigned char red, unsigned char green, unsigned char blue, bool destructive = false); + void createLine(glm::vec3 point1, glm::vec3 point2, float unitSize, rgbColor color, bool destructive = false); + void createSphere(float r,float xc, float yc, float zc, float s, bool solid, + creationMode mode, bool destructive = false, bool debug = false); private: int _callsToTreesToArrays; diff --git a/libraries/shared/src/PacketHeaders.h b/libraries/shared/src/PacketHeaders.h index 49b2fe9d00..bd16bf9ea7 100644 --- a/libraries/shared/src/PacketHeaders.h +++ b/libraries/shared/src/PacketHeaders.h @@ -21,6 +21,7 @@ const PACKET_HEADER PACKET_HEADER_HEAD_DATA = 'H'; const PACKET_HEADER PACKET_HEADER_Z_COMMAND = 'Z'; const PACKET_HEADER PACKET_HEADER_INJECT_AUDIO = 'I'; const PACKET_HEADER PACKET_HEADER_SET_VOXEL = 'S'; +const PACKET_HEADER PACKET_HEADER_SET_VOXEL_DESTRUCTIVE = 'O'; const PACKET_HEADER PACKET_HEADER_ERASE_VOXEL = 'E'; const PACKET_HEADER PACKET_HEADER_VOXEL_DATA = 'V'; const PACKET_HEADER PACKET_HEADER_VOXEL_DATA_MONOCHROME = 'v'; diff --git a/libraries/voxels/src/VoxelTree.cpp b/libraries/voxels/src/VoxelTree.cpp index d22073a54b..e3cf16b753 100644 --- a/libraries/voxels/src/VoxelTree.cpp +++ b/libraries/voxels/src/VoxelTree.cpp @@ -287,7 +287,7 @@ void VoxelTree::eraseAllVoxels() { _isDirty = true; } -void VoxelTree::readCodeColorBufferToTree(unsigned char *codeColorBuffer) { +void VoxelTree::readCodeColorBufferToTree(unsigned char *codeColorBuffer, bool destructive) { VoxelNode* lastCreatedNode = nodeForOctalCode(rootNode, codeColorBuffer, NULL); // create the node if it does not exist @@ -297,19 +297,27 @@ void VoxelTree::readCodeColorBufferToTree(unsigned char *codeColorBuffer) { } else { // if it does exist, make sure it has no children for (int i = 0; i < 8; i++) { - lastCreatedNode->deleteChildAtIndex(i); + if (lastCreatedNode->getChildAtIndex(i)) { + if (destructive) { + lastCreatedNode->deleteChildAtIndex(i); + } else { + printLog("WARNING! operation would require deleting child at index %d, add Voxel ignored!\n ", i); + } + } } } - // give this node its color - int octalCodeBytes = bytesRequiredForCodeLength(*codeColorBuffer); + if (lastCreatedNode->isLeaf()) { + // give this node its color + int octalCodeBytes = bytesRequiredForCodeLength(*codeColorBuffer); - nodeColor newColor; - memcpy(newColor, codeColorBuffer + octalCodeBytes, 3); - newColor[3] = 1; - lastCreatedNode->setColor(newColor); - if (lastCreatedNode->isDirty()) { - _isDirty = true; + nodeColor newColor; + memcpy(newColor, codeColorBuffer + octalCodeBytes, 3); + newColor[3] = 1; + lastCreatedNode->setColor(newColor); + if (lastCreatedNode->isDirty()) { + _isDirty = true; + } } } @@ -463,14 +471,15 @@ VoxelNode* VoxelTree::getVoxelAt(float x, float y, float z, float s) const { return node; } -void VoxelTree::createVoxel(float x, float y, float z, float s, unsigned char red, unsigned char green, unsigned char blue) { +void VoxelTree::createVoxel(float x, float y, float z, float s, + unsigned char red, unsigned char green, unsigned char blue, bool destructive) { unsigned char* voxelData = pointToVoxel(x,y,z,s,red,green,blue); - this->readCodeColorBufferToTree(voxelData); + this->readCodeColorBufferToTree(voxelData, destructive); delete voxelData; } -void VoxelTree::createLine(glm::vec3 point1, glm::vec3 point2, float unitSize, rgbColor color) { +void VoxelTree::createLine(glm::vec3 point1, glm::vec3 point2, float unitSize, rgbColor color, bool destructive) { glm::vec3 distance = point2 - point1; glm::vec3 items = distance / unitSize; int maxItems = std::max(items.x, std::max(items.y, items.z)); @@ -478,14 +487,12 @@ void VoxelTree::createLine(glm::vec3 point1, glm::vec3 point2, float unitSize, r glm::vec3 pointAt = point1; for (int i = 0; i <= maxItems; i++ ) { pointAt += increment; - unsigned char* voxelData = pointToVoxel(pointAt.x,pointAt.y,pointAt.z,unitSize,color[0],color[1],color[2]); - readCodeColorBufferToTree(voxelData); - delete voxelData; + createVoxel(pointAt.x, pointAt.y, pointAt.z, unitSize, color[0], color[1], color[2], destructive); } } void VoxelTree::createSphere(float radius, float xc, float yc, float zc, float voxelSize, - bool solid, creationMode mode, bool debug) { + bool solid, creationMode mode, bool destructive, bool debug) { bool wantColorRandomizer = (mode == RANDOM); bool wantNaturalSurface = (mode == NATURAL); @@ -601,13 +608,13 @@ void VoxelTree::createSphere(float radius, float xc, float yc, float zc, float v x = xc + (thisRadius + i * subVoxelScale) * cos(theta) * sin(phi); y = yc + (thisRadius + i * subVoxelScale) * sin(theta) * sin(phi); z = zc + (thisRadius + i * subVoxelScale) * cos(phi); - this->createVoxel(x, y, z, subVoxelScale, red, green, blue); + this->createVoxel(x, y, z, subVoxelScale, red, green, blue, destructive); } naturalSurfaceRendered = true; } } if (!naturalSurfaceRendered) { - this->createVoxel(x, y, z, thisVoxelSize, red, green, blue); + this->createVoxel(x, y, z, thisVoxelSize, red, green, blue, destructive); } } } diff --git a/libraries/voxels/src/VoxelTree.h b/libraries/voxels/src/VoxelTree.h index 3c33c7b02c..6d384f0244 100644 --- a/libraries/voxels/src/VoxelTree.h +++ b/libraries/voxels/src/VoxelTree.h @@ -22,36 +22,38 @@ typedef enum {GRADIENT, RANDOM, NATURAL} creationMode; class VoxelTree { public: // when a voxel is created in the tree (object new'd) - long voxelsCreated; + long voxelsCreated; // when a voxel is colored/set in the tree (object may have already existed) - long voxelsColored; - long voxelsBytesRead; - + long voxelsColored; + long voxelsBytesRead; + SimpleMovingAverage voxelsCreatedStats; - SimpleMovingAverage voxelsColoredStats; - SimpleMovingAverage voxelsBytesReadStats; + SimpleMovingAverage voxelsColoredStats; + SimpleMovingAverage voxelsBytesReadStats; VoxelTree(); ~VoxelTree(); - + VoxelNode *rootNode; int leavesWrittenToBitstream; - void eraseAllVoxels(); + void eraseAllVoxels(); - void processRemoveVoxelBitstream(unsigned char * bitstream, int bufferSizeBytes); + void processRemoveVoxelBitstream(unsigned char * bitstream, int bufferSizeBytes); void readBitstreamToTree(unsigned char * bitstream, unsigned long int bufferSizeBytes, bool includeColor = true); - void readCodeColorBufferToTree(unsigned char *codeColorBuffer); - void deleteVoxelCodeFromTree(unsigned char *codeBuffer, bool stage = false); + void readCodeColorBufferToTree(unsigned char *codeColorBuffer, bool destructive = false); + void deleteVoxelCodeFromTree(unsigned char *codeBuffer, bool stage = false); void printTreeForDebugging(VoxelNode *startNode); void reaverageVoxelColors(VoxelNode *startNode); void deleteVoxelAt(float x, float y, float z, float s, bool stage = false); VoxelNode* getVoxelAt(float x, float y, float z, float s) const; - void createVoxel(float x, float y, float z, float s, unsigned char red, unsigned char green, unsigned char blue); - void createLine(glm::vec3 point1, glm::vec3 point2, float unitSize, rgbColor color); - void createSphere(float r,float xc, float yc, float zc, float s, bool solid, creationMode mode, bool debug = false); - + void createVoxel(float x, float y, float z, float s, + unsigned char red, unsigned char green, unsigned char blue, bool destructive = false); + void createLine(glm::vec3 point1, glm::vec3 point2, float unitSize, rgbColor color, bool destructive = false); + void createSphere(float r,float xc, float yc, float zc, float s, bool solid, + creationMode mode, bool destructive = false, bool debug = false); + void recurseTreeWithOperation(RecurseVoxelTreeOperation operation, void* extraData=NULL); int encodeTreeBitstream(int maxEncodeLevel, VoxelNode* node, unsigned char* outputBuffer, int availableBytes, @@ -70,7 +72,7 @@ public: VoxelNode*& node, float& distance, BoxFace& face); // Note: this assumes the fileFormat is the HIO individual voxels code files - void loadVoxelsFile(const char* fileName, bool wantColorRandomizer); + void loadVoxelsFile(const char* fileName, bool wantColorRandomizer); // these will read/write files that match the wireformat, excluding the 'V' leading void writeToFileV2(const char* filename) const; diff --git a/voxel-server/src/main.cpp b/voxel-server/src/main.cpp index 74a2ca73ca..6530bc1d6f 100644 --- a/voxel-server/src/main.cpp +++ b/voxel-server/src/main.cpp @@ -503,9 +503,11 @@ int main(int argc, const char * argv[]) if (agentList->getAgentSocket().receive(&agentPublicAddress, packetData, &receivedBytes)) { // XXXBHG: Hacked in support for 'S' SET command - if (packetData[0] == PACKET_HEADER_SET_VOXEL) { + if (packetData[0] == PACKET_HEADER_SET_VOXEL || packetData[0] == PACKET_HEADER_SET_VOXEL_DESTRUCTIVE) { + bool destructive = (packetData[0] == PACKET_HEADER_SET_VOXEL_DESTRUCTIVE); unsigned short int itemNumber = (*((unsigned short int*)&packetData[1])); - printf("got I - insert voxels - command from client receivedBytes=%ld itemNumber=%d\n", + printf("got %s - command from client receivedBytes=%ld itemNumber=%d\n", + destructive ? "PACKET_HEADER_SET_VOXEL_DESTRUCTIVE" : "PACKET_HEADER_SET_VOXEL", receivedBytes,itemNumber); int atByte = 3; unsigned char* pVoxelData = (unsigned char*)&packetData[3]; @@ -534,7 +536,7 @@ int main(int argc, const char * argv[]) printf("inserting voxel at: %f,%f,%f\n",vertices[0],vertices[1],vertices[2]); delete []vertices; - randomTree.readCodeColorBufferToTree(pVoxelData); + randomTree.readCodeColorBufferToTree(pVoxelData, destructive); // skip to next pVoxelData+=voxelDataSize; atByte+=voxelDataSize; From ebc612f87aeef6e4b8ecc7ea22381d28b41a08b0 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 14 May 2013 14:23:08 -0700 Subject: [PATCH 27/31] Start out with gray, fix coloring smaller portions by (similar to deleting) breaking up leaf nodes into their components. --- interface/src/Application.cpp | 4 +++- libraries/voxels/src/VoxelTree.cpp | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 289c2b7a45..0275de2d9f 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1093,7 +1093,9 @@ void Application::initMenu() { (_logOn = toolsMenu->addAction("Log"))->setCheckable(true); _logOn->setChecked(true); _voxelPaintColor = toolsMenu->addAction("Voxel Paint Color", this, SLOT(chooseVoxelPaintColor()), Qt::Key_7); - _voxelPaintColor->setIcon(createSwatchIcon(QColor())); + QColor paintColor(128, 128, 128); + _voxelPaintColor->setData(paintColor); + _voxelPaintColor->setIcon(createSwatchIcon(paintColor)); QMenu* frustumMenu = menuBar->addMenu("Frustum"); (_frustumOn = frustumMenu->addAction("Display Frustum"))->setCheckable(true); diff --git a/libraries/voxels/src/VoxelTree.cpp b/libraries/voxels/src/VoxelTree.cpp index d22073a54b..fb9e33dc47 100644 --- a/libraries/voxels/src/VoxelTree.cpp +++ b/libraries/voxels/src/VoxelTree.cpp @@ -103,7 +103,13 @@ VoxelNode* VoxelTree::createMissingNode(VoxelNode* lastParentNode, unsigned char int indexOfNewChild = branchIndexWithDescendant(lastParentNode->getOctalCode(), codeToReach); // we could be coming down a branch that was already created, so don't stomp on it. - if (!lastParentNode->getChildAtIndex(indexOfNewChild)) { + if (lastParentNode->isLeaf() && lastParentNode->isColored()) { + // for colored leaves, we must add *all* the children + for (int i = 0; i < NUMBER_OF_CHILDREN; i++) { + lastParentNode->addChildAtIndex(i); + lastParentNode->getChildAtIndex(i)->setColor(lastParentNode->getColor()); + } + } else if (!lastParentNode->getChildAtIndex(indexOfNewChild)) { lastParentNode->addChildAtIndex(indexOfNewChild); } From 053c0d1ca57201b036790cef78c1f430719d2e40 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 14 May 2013 14:34:36 -0700 Subject: [PATCH 28/31] Focus tweak; focus isn't returning to the GL widget after closing the color dialog on OS X. --- interface/src/Application.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 0275de2d9f..a0c0a151a8 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -222,6 +222,7 @@ Application::Application(int& argc, char** argv) : QRect available = desktop()->availableGeometry(); _window->resize(available.size()); _window->setVisible(true); + _glWidget->setFocusPolicy(Qt::StrongFocus); _glWidget->setFocus(); // enable mouse tracking; otherwise, we only get drag events From b4ca7ca5e350391b86c564d0791cd9a3f08c8b8b Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 14 May 2013 14:36:43 -0700 Subject: [PATCH 29/31] Just manually request focus. --- interface/src/Application.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index a0c0a151a8..0f50d67a69 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1056,6 +1056,7 @@ void Application::chooseVoxelPaintColor() { _voxelPaintColor->setData(selected); _voxelPaintColor->setIcon(createSwatchIcon(selected)); } + _glWidget->setFocus(); } void Application::initMenu() { From e1016ad902edef66bd587c9899d7c3b010f82c18 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 14 May 2013 14:40:18 -0700 Subject: [PATCH 30/31] What matters is the active state, not the focus. --- interface/src/Application.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 0f50d67a69..4816d96c87 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1056,7 +1056,9 @@ void Application::chooseVoxelPaintColor() { _voxelPaintColor->setData(selected); _voxelPaintColor->setIcon(createSwatchIcon(selected)); } - _glWidget->setFocus(); + + // restore the main window's active state + _window->activateWindow(); } void Application::initMenu() { From aca2ce47b65fd8c333fe90bce68281ec687ce679 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Tue, 14 May 2013 16:29:39 -0700 Subject: [PATCH 31/31] add a V2 transmitter data packet for new iOS transmitter --- interface/src/Application.cpp | 11 ++++++++++- libraries/shared/src/PacketHeaders.h | 3 ++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index eaebaee9ff..e628188e28 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1961,10 +1961,19 @@ void* Application::networkReceive(void* args) { app->_bytesCount += bytesReceived; switch (app->_incomingPacket[0]) { - case PACKET_HEADER_TRANSMITTER_DATA: + case PACKET_HEADER_TRANSMITTER_DATA_V1: // Process UDP packets that are sent to the client from local sensor devices app->_myAvatar.processTransmitterData(app->_incomingPacket, bytesReceived); break; + case PACKET_HEADER_TRANSMITTER_DATA_V2: + float rotationRates[3]; + float accelerations[3]; + + memcpy(rotationRates, app->_incomingPacket + 2, sizeof(rotationRates)); + memcpy(accelerations, app->_incomingPacket + 3 + sizeof(rotationRates), sizeof(accelerations)); + + printf("The rotation: %f, %f, %f\n", rotationRates[0], rotationRates[1], rotationRates[2]); + break; case PACKET_HEADER_VOXEL_DATA: case PACKET_HEADER_VOXEL_DATA_MONOCHROME: case PACKET_HEADER_Z_COMMAND: diff --git a/libraries/shared/src/PacketHeaders.h b/libraries/shared/src/PacketHeaders.h index 49b2fe9d00..398a3e54ab 100644 --- a/libraries/shared/src/PacketHeaders.h +++ b/libraries/shared/src/PacketHeaders.h @@ -25,7 +25,8 @@ const PACKET_HEADER PACKET_HEADER_ERASE_VOXEL = 'E'; const PACKET_HEADER PACKET_HEADER_VOXEL_DATA = 'V'; const PACKET_HEADER PACKET_HEADER_VOXEL_DATA_MONOCHROME = 'v'; const PACKET_HEADER PACKET_HEADER_BULK_AVATAR_DATA = 'X'; -const PACKET_HEADER PACKET_HEADER_TRANSMITTER_DATA = 't'; +const PACKET_HEADER PACKET_HEADER_TRANSMITTER_DATA_V1 = 't'; +const PACKET_HEADER PACKET_HEADER_TRANSMITTER_DATA_V2 = 'T'; const PACKET_HEADER PACKET_HEADER_ENVIRONMENT_DATA = 'e'; const PACKET_HEADER PACKET_HEADER_DOMAIN_LIST_REQUEST = 'L'; const PACKET_HEADER PACKET_HEADER_DOMAIN_RFD = 'C';