1274 lines · 49.8 KB · C++
RawDownload
////  ApplicationOverlay.cpp//  interface/src/ui/overlays////  Created by Benjamin Arnold on 5/27/14.//  Copyright 2014 High Fidelity, Inc.////  Distributed under the Apache License, Version 2.0.//  See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html// #include "InterfaceConfig.h" #include <QOpenGLFramebufferObject>#include <QOpenGLTexture> #include <avatar/AvatarManager.h>#include <GLMHelpers.h>#include <PathUtils.h>#include <gpu/GLBackend.h>#include <GLMHelpers.h>#include <PerfStat.h>#include <OffscreenUi.h> #include "AudioClient.h"#include "audio/AudioIOStatsRenderer.h"#include "audio/AudioScope.h"#include "audio/AudioToolBox.h"#include "Application.h"#include "ApplicationOverlay.h"#include "devices/CameraToolBox.h" #include "Util.h"#include "ui/Stats.h" // Used to animate the magnification windowsconst float MAG_SPEED = 0.08f; const quint64 MSECS_TO_USECS = 1000ULL; const float WHITE_TEXT[] = { 0.93f, 0.93f, 0.93f };const float RETICLE_COLOR[] = { 0.0f, 198.0f / 255.0f, 244.0f / 255.0f };const float reticleSize = TWO_PI / 100.0f;  const float CONNECTION_STATUS_BORDER_COLOR[] = { 1.0f, 0.0f, 0.0f };const float CONNECTION_STATUS_BORDER_LINE_WIDTH = 4.0f; static const float MOUSE_PITCH_RANGE = 1.0f * PI;static const float MOUSE_YAW_RANGE = 0.5f * TWO_PI;static const glm::vec2 MOUSE_RANGE(MOUSE_YAW_RANGE, MOUSE_PITCH_RANGE); // Return a point's cartesian coordinates on a sphere from pitch and yawglm::vec3 getPoint(float yaw, float pitch) {    return glm::vec3(glm::cos(-pitch) * (-glm::sin(yaw)),                     glm::sin(-pitch),                     glm::cos(-pitch) * (-glm::cos(yaw)));} //Checks if the given ray intersects the sphere at the origin. result will store a multiplier that should//be multiplied by dir and added to origin to get the location of the collisionbool raySphereIntersect(const glm::vec3 &dir, const glm::vec3 &origin, float r, float* result){    //Source: http://wiki.cgsociety.org/index.php/Ray_Sphere_Intersection        //Compute A, B and C coefficients    float a = glm::dot(dir, dir);    float b = 2 * glm::dot(dir, origin);    float c = glm::dot(origin, origin) - (r * r);        //Find discriminant    float disc = b * b - 4 * a * c;        // if discriminant is negative there are no real roots, so return    // false as ray misses sphere    if (disc < 0) {        return false;    }        // compute q as described above    float distSqrt = sqrtf(disc);    float q;    if (b < 0) {        q = (-b - distSqrt) / 2.0;    } else {        q = (-b + distSqrt) / 2.0;    }        // compute t0 and t1    float t0 = q / a;    float t1 = c / q;        // make sure t0 is smaller than t1    if (t0 > t1) {        // if t0 is bigger than t1 swap them around        float temp = t0;        t0 = t1;        t1 = temp;    }        // if t1 is less than zero, the object is in the ray's negative direction    // and consequently the ray misses the sphere    if (t1 < 0) {        return false;    }        // if t0 is less than zero, the intersection point is at t1    if (t0 < 0) {        *result = t1;        return true;    } else { // else the intersection point is at t0        *result = t0;        return true;    }} void ApplicationOverlay::renderReticle(glm::quat orientation, float alpha) {    glPushMatrix(); {        glm::vec3 axis = glm::axis(orientation);        glRotatef(glm::degrees(glm::angle(orientation)), axis.x, axis.y, axis.z);        glm::vec3 topLeft = getPoint(reticleSize / 2.0f, -reticleSize / 2.0f);        glm::vec3 topRight = getPoint(-reticleSize / 2.0f, -reticleSize / 2.0f);        glm::vec3 bottomLeft = getPoint(reticleSize / 2.0f, reticleSize / 2.0f);        glm::vec3 bottomRight = getPoint(-reticleSize / 2.0f, reticleSize / 2.0f);                // TODO: this version of renderQuad() needs to take a color        glm::vec4 reticleColor = { RETICLE_COLOR[0], RETICLE_COLOR[1], RETICLE_COLOR[2], alpha };                                DependencyManager::get<GeometryCache>()->renderQuad(topLeft, bottomLeft, bottomRight, topRight,                                                            glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f),                                                            glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f),                                                             reticleColor, _reticleQuad);    } glPopMatrix();} ApplicationOverlay::ApplicationOverlay() :    _textureFov(glm::radians(DEFAULT_HMD_UI_ANGULAR_SIZE)),    _textureAspectRatio(1.0f),    _lastMouseMove(0),    _magnifier(true),    _alpha(1.0f),    _oculusUIRadius(1.0f),    _trailingAudioLoudness(0.0f),    _crosshairTexture(0),    _previousBorderWidth(-1),    _previousBorderHeight(-1),    _previousMagnifierBottomLeft(),    _previousMagnifierBottomRight(),    _previousMagnifierTopLeft(),    _previousMagnifierTopRight(){    memset(_reticleActive, 0, sizeof(_reticleActive));    memset(_magActive, 0, sizeof(_reticleActive));    memset(_magSizeMult, 0, sizeof(_magSizeMult));     auto geometryCache = DependencyManager::get<GeometryCache>();        _reticleQuad = geometryCache->allocateID();    _magnifierQuad = geometryCache->allocateID();    _audioRedQuad = geometryCache->allocateID();    _audioGreenQuad = geometryCache->allocateID();    _audioBlueQuad = geometryCache->allocateID();    _domainStatusBorder = geometryCache->allocateID();    _magnifierBorder = geometryCache->allocateID();        // Once we move UI rendering and screen rendering to different    // threads, we need to use a sync object to deteremine when    // the current UI texture is no longer being read from, and only     // then release it back to the UI for re-use    auto offscreenUi = DependencyManager::get<OffscreenUi>();    connect(offscreenUi.data(), &OffscreenUi::textureUpdated, this, [&](GLuint textureId) {        auto offscreenUi = DependencyManager::get<OffscreenUi>();        offscreenUi->lockTexture(textureId);        assert(!glGetError());        std::swap(_newUiTexture, textureId);        if (textureId) {            offscreenUi->releaseTexture(textureId);        }    });} ApplicationOverlay::~ApplicationOverlay() {} // Renders the overlays either to a texture or to the screenvoid ApplicationOverlay::renderOverlay(RenderArgs* renderArgs) {    PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), "ApplicationOverlay::displayOverlay()");    Overlays& overlays = qApp->getOverlays();        _textureFov = glm::radians(_hmdUIAngularSize);    glm::vec2 size = qApp->getCanvasSize();    _textureAspectRatio = aspect(size);     //Handle fading and deactivation/activation of UI        // Render 2D overlay    glMatrixMode(GL_PROJECTION);    glDisable(GL_DEPTH_TEST);    glDisable(GL_LIGHTING);    glEnable(GL_BLEND);    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);    _overlays.buildFramebufferObject();    _overlays.bind();    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    glViewport(0, 0, size.x, size.y);     glPushMatrix(); {        const float NEAR_CLIP = -10000;        const float FAR_CLIP = 10000;        glLoadIdentity();        glOrtho(0, size.x, size.y, 0, NEAR_CLIP, FAR_CLIP);         glMatrixMode(GL_MODELVIEW);         renderAudioMeter();        renderCameraToggle();         renderStatsAndLogs();         // give external parties a change to hook in        emit qApp->renderingOverlay();         overlays.renderHUD(renderArgs);         renderPointers();         renderDomainConnectionStatusBorder();         glMatrixMode(GL_PROJECTION);    } glPopMatrix();     glMatrixMode(GL_MODELVIEW);    glEnable(GL_DEPTH_TEST);    glEnable(GL_LIGHTING);    glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE);     _overlays.release();} // A quick and dirty solution for compositing the old overlay // texture with the new onetemplate <typename F>void with_each_texture(GLuint firstPassTexture, GLuint secondPassTexture, F f) {    glEnable(GL_TEXTURE_2D);    glActiveTexture(GL_TEXTURE0);    if (firstPassTexture) {        glBindTexture(GL_TEXTURE_2D, firstPassTexture);        f();    }    if (secondPassTexture) {        glBindTexture(GL_TEXTURE_2D, secondPassTexture);        f();    }    glBindTexture(GL_TEXTURE_2D, 0);    glDisable(GL_TEXTURE_2D);} // Draws the FBO texture for the screenvoid ApplicationOverlay::displayOverlayTexture() {    if (_alpha == 0.0f) {        return;    }    glMatrixMode(GL_PROJECTION);    glPushMatrix(); {        glLoadIdentity();        glDisable(GL_DEPTH_TEST);        glDisable(GL_LIGHTING);        if (_alpha < 1.0) {            glEnable(GL_BLEND);        }        glViewport(0, 0, qApp->getDeviceSize().width(), qApp->getDeviceSize().height());         static const glm::vec2 topLeft(-1, 1);        static const glm::vec2 bottomRight(1, -1);        static const glm::vec2 texCoordTopLeft(0.0f, 1.0f);        static const glm::vec2 texCoordBottomRight(1.0f, 0.0f);        with_each_texture(_overlays.getTexture(), _newUiTexture, [&] {            DependencyManager::get<GeometryCache>()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight,                glm::vec4(1.0f, 1.0f, 1.0f, _alpha));        });    } glPopMatrix();} // Draws the FBO texture for Oculus rift.void ApplicationOverlay::displayOverlayTextureHmd(Camera& whichCamera) {    if (_alpha == 0.0f) {        return;    }     glEnable(GL_BLEND);    glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE);    glEnable(GL_DEPTH_TEST);    glDepthMask(GL_TRUE);    glDisable(GL_LIGHTING);    glEnable(GL_ALPHA_TEST);    glAlphaFunc(GL_GREATER, 0.01f);      //Update and draw the magnifiers    MyAvatar* myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar();    const glm::quat& orientation = myAvatar->getOrientation();    // Always display the HMD overlay relative to the camera position but     // remove the HMD pose offset.  This results in an overlay that sticks with you     // even in third person mode, but isn't drawn at a fixed distance.    glm::vec3 position = whichCamera.getPosition();    position -= qApp->getCamera()->getHmdPosition();    const float scale = myAvatar->getScale() * _oculusUIRadius;    //    glm::vec3 eyeOffset = setEyeOffsetPosition;    glMatrixMode(GL_MODELVIEW);    glPushMatrix(); {        glTranslatef(position.x, position.y, position.z);        glm::mat4 rotation = glm::toMat4(orientation);        glMultMatrixf(&rotation[0][0]);        glScalef(scale, scale, scale);        for (int i = 0; i < NUMBER_OF_RETICLES; i++) {                        if (_magActive[i]) {                _magSizeMult[i] += MAG_SPEED;                if (_magSizeMult[i] > 1.0f) {                    _magSizeMult[i] = 1.0f;                }            } else {                _magSizeMult[i] -= MAG_SPEED;                if (_magSizeMult[i] < 0.0f) {                    _magSizeMult[i] = 0.0f;                }            }                        if (_magSizeMult[i] > 0.0f) {                //Render magnifier, but dont show border for mouse magnifier                glm::vec2 projection = screenToOverlay(glm::vec2(_reticlePosition[MOUSE].x(),                                                                 _reticlePosition[MOUSE].y()));                with_each_texture(_overlays.getTexture(), 0, [&] {                    renderMagnifier(projection, _magSizeMult[i], i != MOUSE);                });            }        }                glDepthMask(GL_FALSE);        glDisable(GL_ALPHA_TEST);                static float textureFOV = 0.0f, textureAspectRatio = 1.0f;        if (textureFOV != _textureFov ||            textureAspectRatio != _textureAspectRatio) {            textureFOV = _textureFov;            textureAspectRatio = _textureAspectRatio;                        _overlays.buildVBO(_textureFov, _textureAspectRatio, 80, 80);        }         with_each_texture(_overlays.getTexture(), _newUiTexture, [&] {            _overlays.render();        });         if (!Application::getInstance()->isMouseHidden()) {            renderPointersOculus(myAvatar->getDefaultEyePosition());        }        glDepthMask(GL_TRUE);        glDisable(GL_TEXTURE_2D);                glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE);        glEnable(GL_LIGHTING);    } glPopMatrix();} // Draws the FBO texture for 3DTV.void ApplicationOverlay::displayOverlayTextureStereo(Camera& whichCamera, float aspectRatio, float fov) {    if (_alpha == 0.0f) {        return;    }        MyAvatar* myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar();    const glm::vec3& viewMatrixTranslation = qApp->getViewMatrixTranslation();        glEnable(GL_BLEND);    glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE);    glEnable(GL_DEPTH_TEST);    glDisable(GL_LIGHTING);        glMatrixMode(GL_MODELVIEW);        glPushMatrix();    glLoadIdentity();    // Transform to world space    glm::quat rotation = whichCamera.getRotation();    glm::vec3 axis2 = glm::axis(rotation);    glRotatef(-glm::degrees(glm::angle(rotation)), axis2.x, axis2.y, axis2.z);    glTranslatef(viewMatrixTranslation.x, viewMatrixTranslation.y, viewMatrixTranslation.z);        // Translate to the front of the camera    glm::vec3 pos = whichCamera.getPosition();    glm::quat rot = myAvatar->getOrientation();    glm::vec3 axis = glm::axis(rot);        glTranslatef(pos.x, pos.y, pos.z);    glRotatef(glm::degrees(glm::angle(rot)), axis.x, axis.y, axis.z);        glm::vec4 overlayColor = {1.0f, 1.0f, 1.0f, _alpha};        //Render    const GLfloat distance = 1.0f;        const GLfloat halfQuadHeight = distance * tan(fov);    const GLfloat halfQuadWidth = halfQuadHeight * aspectRatio;    const GLfloat quadWidth = halfQuadWidth * 2.0f;    const GLfloat quadHeight = halfQuadHeight * 2.0f;        GLfloat x = -halfQuadWidth;    GLfloat y = -halfQuadHeight;    glDisable(GL_DEPTH_TEST);     with_each_texture(_overlays.getTexture(), _newUiTexture, [&] {        DependencyManager::get<GeometryCache>()->renderQuad(glm::vec3(x, y + quadHeight, -distance),                                                glm::vec3(x + quadWidth, y + quadHeight, -distance),                                                glm::vec3(x + quadWidth, y, -distance),                                                glm::vec3(x, y, -distance),                                                glm::vec2(0.0f, 1.0f), glm::vec2(1.0f, 1.0f),                                                 glm::vec2(1.0f, 0.0f), glm::vec2(0.0f, 0.0f),                                                overlayColor);    });        if (!_crosshairTexture) {        _crosshairTexture = TextureCache::getImageTexture(PathUtils::resourcesPath() +                                                          "images/sixense-reticle.png");    }        //draw the mouse pointer    glBindTexture(GL_TEXTURE_2D, gpu::GLBackend::getTextureID(_crosshairTexture));    glm::vec2 canvasSize = qApp->getCanvasSize();    const float reticleSize = 40.0f / canvasSize.x * quadWidth;    x -= reticleSize / 2.0f;    y += reticleSize / 2.0f;    const float mouseX = (qApp->getMouseX() / (float)canvasSize.x) * quadWidth;    const float mouseY = (1.0 - (qApp->getMouseY() / (float)canvasSize.y)) * quadHeight;        glm::vec4 reticleColor = { RETICLE_COLOR[0], RETICLE_COLOR[1], RETICLE_COLOR[2], 1.0f };     DependencyManager::get<GeometryCache>()->renderQuad(glm::vec3(x + mouseX, y + mouseY, -distance),                                                 glm::vec3(x + mouseX + reticleSize, y + mouseY, -distance),                                                glm::vec3(x + mouseX + reticleSize, y + mouseY - reticleSize, -distance),                                                glm::vec3(x + mouseX, y + mouseY - reticleSize, -distance),                                                glm::vec2(0.0f, 0.0f), glm::vec2(1.0f, 0.0f),                                                 glm::vec2(1.0f, 1.0f), glm::vec2(0.0f, 1.0f),                                                reticleColor, _reticleQuad);     glEnable(GL_DEPTH_TEST);        glPopMatrix();        glDepthMask(GL_TRUE);    glBindTexture(GL_TEXTURE_2D, 0);    glDisable(GL_TEXTURE_2D);        glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE);    glEnable(GL_LIGHTING);} void ApplicationOverlay::computeHmdPickRay(glm::vec2 cursorPos, glm::vec3& origin, glm::vec3& direction) const {    cursorPos *= qApp->getCanvasSize();    const glm::vec2 projection = screenToSpherical(cursorPos);    // The overlay space orientation of the mouse coordinates    const glm::quat orientation(glm::vec3(-projection.y, projection.x, 0.0f));    // FIXME We now have the direction of the ray FROM THE DEFAULT HEAD POSE.      // Now we need to account for the actual camera position relative to the overlay    glm::vec3 overlaySpaceDirection = glm::normalize(orientation * IDENTITY_FRONT);      const glm::vec3& hmdPosition = qApp->getCamera()->getHmdPosition();    const glm::quat& hmdOrientation = qApp->getCamera()->getHmdRotation();     // We need the RAW camera orientation and position, because this is what the overlay is    // rendered relative to    const glm::vec3 overlayPosition = qApp->getCamera()->getPosition() - hmdPosition;    const glm::quat overlayOrientation = qApp->getCamera()->getRotation() * glm::inverse(hmdOrientation);     // Intersection UI overlay space    glm::vec3 worldSpaceDirection = overlayOrientation * overlaySpaceDirection;    glm::vec3 intersectionWithUi = glm::normalize(worldSpaceDirection) * _oculusUIRadius;    intersectionWithUi += overlayPosition;     // Intersection in world space    origin = overlayPosition + hmdPosition;    direction = glm::normalize(intersectionWithUi - origin);} //Caculate the click location using one of the sixense controllers. Scale is not appliedQPoint ApplicationOverlay::getPalmClickLocation(const PalmData *palm) const {    MyAvatar* myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar();     glm::vec3 tip = myAvatar->getLaserPointerTipPosition(palm);    glm::vec3 eyePos = myAvatar->getHead()->getEyePosition();    glm::quat invOrientation = glm::inverse(myAvatar->getOrientation());    //direction of ray goes towards camera    glm::vec3 dir = invOrientation * glm::normalize(qApp->getCamera()->getPosition() - tip);    glm::vec3 tipPos = invOrientation * (tip - eyePos);     QPoint rv;

Lines 1–500 of 1274.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t