From 597842eba20df0c5c6d0ef49a0f2ff988ba89c55 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Wed, 7 Aug 2013 14:17:52 -0700 Subject: [PATCH 01/41] Starting on glow effect. --- interface/src/Application.cpp | 3 +++ interface/src/Application.h | 2 ++ interface/src/renderer/GlowEffect.cpp | 11 +++++++++++ interface/src/renderer/GlowEffect.h | 19 +++++++++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 interface/src/renderer/GlowEffect.cpp create mode 100644 interface/src/renderer/GlowEffect.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 61317cc4dd..02d1772ebf 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3150,6 +3150,9 @@ void Application::displaySide(Camera& whichCamera) { } renderFollowIndicator(); + + // render the glow effect + _glowEffect.render(); } void Application::displayOverlay() { diff --git a/interface/src/Application.h b/interface/src/Application.h index d9c945f34e..1e9b4a173c 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -43,6 +43,7 @@ #include "avatar/Avatar.h" #include "avatar/HandControl.h" #include "renderer/GeometryCache.h" +#include "renderer/GlowEffect.h" #include "renderer/TextureCache.h" #include "ui/BandwidthDialog.h" #include "ui/ChatEntry.h" @@ -443,6 +444,7 @@ private: TextureCache _textureCache; ParticleSystem _particleSystem; + GlowEffect _glowEffect; #ifndef _WIN32 Audio _audio; diff --git a/interface/src/renderer/GlowEffect.cpp b/interface/src/renderer/GlowEffect.cpp new file mode 100644 index 0000000000..56b3cb5233 --- /dev/null +++ b/interface/src/renderer/GlowEffect.cpp @@ -0,0 +1,11 @@ +// +// GlowEffect.cpp +// interface +// +// Created by Andrzej Kapolka on 8/7/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. + +#include "GlowEffect.h" + +void GlowEffect::render() { +} diff --git a/interface/src/renderer/GlowEffect.h b/interface/src/renderer/GlowEffect.h new file mode 100644 index 0000000000..25dd1062be --- /dev/null +++ b/interface/src/renderer/GlowEffect.h @@ -0,0 +1,19 @@ +// +// GlowEffect.h +// interface +// +// Created by Andrzej Kapolka on 8/7/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// + +#ifndef __interface__GlowEffect__ +#define __interface__GlowEffect__ + +class GlowEffect { +public: + + void prepare(); + void render(); +}; + +#endif /* defined(__interface__GlowEffect__) */ From 1828a105d46f542a00bf89f011912b4117e4ca1b Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Thu, 8 Aug 2013 16:55:45 -0700 Subject: [PATCH 02/41] Basic glow effect. --- .../resources/shaders/horizontal_blur.frag | 32 +++++++ .../resources/shaders/vertical_blur.frag | 32 +++++++ interface/src/Application.cpp | 12 +++ interface/src/Application.h | 1 + interface/src/renderer/GlowEffect.cpp | 94 +++++++++++++++++++ interface/src/renderer/GlowEffect.h | 13 +++ interface/src/renderer/TextureCache.cpp | 44 ++++++++- interface/src/renderer/TextureCache.h | 14 ++- 8 files changed, 240 insertions(+), 2 deletions(-) create mode 100644 interface/resources/shaders/horizontal_blur.frag create mode 100644 interface/resources/shaders/vertical_blur.frag diff --git a/interface/resources/shaders/horizontal_blur.frag b/interface/resources/shaders/horizontal_blur.frag new file mode 100644 index 0000000000..b1b40c2187 --- /dev/null +++ b/interface/resources/shaders/horizontal_blur.frag @@ -0,0 +1,32 @@ +#version 120 + +// +// horizontal_blur.frag +// fragment shader +// +// Created by Andrzej Kapolka on 8/8/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// + +// the texture containing the color to blur +uniform sampler2D colorTexture; + +void main(void) { + float ds = dFdx(gl_TexCoord[0].s); + gl_FragColor = (texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * -15.5, 0.0)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * -13.5, 0.0)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * -11.5, 0.0)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * -9.5, 0.0)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * -7.5, 0.0)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * -5.5, 0.0)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * -3.5, 0.0)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * -1.5, 0.0)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * 1.5, 0.0)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * 3.5, 0.0)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * 5.5, 0.0)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * 7.5, 0.0)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * 9.5, 0.0)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * 11.5, 0.0)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * 13.5, 0.0)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * 15.5, 0.0))) / 16.0; +} diff --git a/interface/resources/shaders/vertical_blur.frag b/interface/resources/shaders/vertical_blur.frag new file mode 100644 index 0000000000..0641c773e7 --- /dev/null +++ b/interface/resources/shaders/vertical_blur.frag @@ -0,0 +1,32 @@ +#version 120 + +// +// vertical_blur.frag +// fragment shader +// +// Created by Andrzej Kapolka on 8/8/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// + +// the texture containing the color to blur +uniform sampler2D colorTexture; + +void main(void) { + float dt = dFdy(gl_TexCoord[0].t); + gl_FragColor = (texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * -15.5)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * -13.5)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * -11.5)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * -9.5)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * -7.5)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * -4.5)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * -3.5)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * -1.5)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * 1.5)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * 3.5)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * 5.5)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * 7.5)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * 9.5)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * 11.5)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * 13.5)) + + texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * 15.5))) / 16.0; +} diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 5239750447..55c4a249dc 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2202,6 +2202,8 @@ void Application::init() { _environment.init(); + _glowEffect.init(); + _handControl.setScreenDimensions(_glWidget->width(), _glWidget->height()); _headMouseX = _mouseX = _glWidget->width() / 2; @@ -3022,6 +3024,9 @@ void Application::displaySide(Camera& whichCamera) { glEnable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); + // prepare the glow effect + _glowEffect.prepare(); + // Enable to show line from me to the voxel I am touching //renderLineToTouchedVoxel(); //renderThrustAtVoxel(_voxelThrust); @@ -3118,6 +3123,13 @@ void Application::displaySide(Camera& whichCamera) { _myAvatar.getHead().setLookAtPosition(_myCamera.getPosition()); } _myAvatar.render(_lookingInMirror->isChecked(), _renderAvatarBalls->isChecked()); + + _glowEffect.bind(); + _myAvatar.render(_lookingInMirror->isChecked(), _renderAvatarBalls->isChecked()); + _glowEffect.release(); + + _myAvatar.render(_lookingInMirror->isChecked(), _renderAvatarBalls->isChecked()); + _myAvatar.setDisplayingLookatVectors(_renderLookatOn->isChecked()); if (_renderLookatIndicatorOn->isChecked() && _isLookingAtOtherAvatar) { diff --git a/interface/src/Application.h b/interface/src/Application.h index 662604a3dd..f2c9c38e15 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -101,6 +101,7 @@ public: void updateParticleSystem(float deltaTime); + QGLWidget* getGLWidget() { return _glWidget; } Avatar* getAvatar() { return &_myAvatar; } Audio* getAudio() { return &_audio; } Camera* getCamera() { return &_myCamera; } diff --git a/interface/src/renderer/GlowEffect.cpp b/interface/src/renderer/GlowEffect.cpp index 56b3cb5233..82a9c43820 100644 --- a/interface/src/renderer/GlowEffect.cpp +++ b/interface/src/renderer/GlowEffect.cpp @@ -5,7 +5,101 @@ // Created by Andrzej Kapolka on 8/7/13. // Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +#include + +#include "Application.h" #include "GlowEffect.h" +#include "InterfaceConfig.h" +#include "ProgramObject.h" + +static ProgramObject* createBlurProgram(const QString& direction) { + ProgramObject* program = new ProgramObject(); + program->addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/" + direction + "_blur.frag"); + program->link(); + + program->bind(); + program->setUniformValue("colorTexture", 0); + program->release(); + + return program; +} + +void GlowEffect::init() { + switchToResourcesParentIfRequired(); + _horizontalBlurProgram = createBlurProgram("horizontal"); + _verticalBlurProgram = createBlurProgram("vertical"); +} + +void GlowEffect::prepare() { + bind(); + glClear(GL_COLOR_BUFFER_BIT); + release(); +} + +void GlowEffect::bind() { + Application::getInstance()->getTextureCache()->getPrimaryFramebufferObject()->bind(); +} + +void GlowEffect::release() { + Application::getInstance()->getTextureCache()->getPrimaryFramebufferObject()->release(); +} + +static void renderFullscreenQuad() { + glBegin(GL_QUADS); + glTexCoord2f(0.0f, 0.0f); + glVertex2f(-1.0f, -1.0f); + glTexCoord2f(1.0f, 0.0f); + glVertex2f(1.0f, -1.0f); + glTexCoord2f(1.0f, 1.0f); + glVertex2f(1.0f, 1.0f); + glTexCoord2f(0.0f, 1.0f); + glVertex2f(-1.0f, 1.0f); + glEnd(); +} void GlowEffect::render() { + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, Application::getInstance()->getTextureCache()->getPrimaryFramebufferObject()->texture()); + glDisable(GL_LIGHTING); + + glPushMatrix(); + glLoadIdentity(); + + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + + glDisable(GL_BLEND); + + // render the primary to the secondary with the horizontal blur + QOpenGLFramebufferObject* secondaryFBO = Application::getInstance()->getTextureCache()->getSecondaryFramebufferObject(); + secondaryFBO->bind(); + + _horizontalBlurProgram->bind(); + renderFullscreenQuad(); + _horizontalBlurProgram->release(); + + secondaryFBO->release(); + + // render the secondary to the screen with the vertical blur + glBindTexture(GL_TEXTURE_2D, secondaryFBO->texture()); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE); + + _verticalBlurProgram->bind(); + renderFullscreenQuad(); + _verticalBlurProgram->release(); + + glPopMatrix(); + + glMatrixMode(GL_MODELVIEW); + glPopMatrix(); + + glBindTexture(GL_TEXTURE_2D, 0); + + glDisable(GL_TEXTURE_2D); + glEnable(GL_LIGHTING); + + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } diff --git a/interface/src/renderer/GlowEffect.h b/interface/src/renderer/GlowEffect.h index 25dd1062be..ab07556fee 100644 --- a/interface/src/renderer/GlowEffect.h +++ b/interface/src/renderer/GlowEffect.h @@ -9,11 +9,24 @@ #ifndef __interface__GlowEffect__ #define __interface__GlowEffect__ +class ProgramObject; + class GlowEffect { public: + void init(); + void prepare(); + + void bind(); + void release(); + void render(); + +private: + + ProgramObject* _horizontalBlurProgram; + ProgramObject* _verticalBlurProgram; }; #endif /* defined(__interface__GlowEffect__) */ diff --git a/interface/src/renderer/TextureCache.cpp b/interface/src/renderer/TextureCache.cpp index 1c832a648e..98cfebcee0 100644 --- a/interface/src/renderer/TextureCache.cpp +++ b/interface/src/renderer/TextureCache.cpp @@ -5,17 +5,28 @@ // Created by Andrzej Kapolka on 8/6/13. // Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +#include +#include + #include +#include "Application.h" #include "TextureCache.h" -TextureCache::TextureCache() : _permutationNormalTextureID(0) { +TextureCache::TextureCache() : _permutationNormalTextureID(0), + _primaryFramebufferObject(NULL), _secondaryFramebufferObject(NULL) { } TextureCache::~TextureCache() { if (_permutationNormalTextureID != 0) { glDeleteTextures(1, &_permutationNormalTextureID); } + if (_primaryFramebufferObject != NULL) { + delete _primaryFramebufferObject; + } + if (_secondaryFramebufferObject != NULL) { + delete _secondaryFramebufferObject; + } } GLuint TextureCache::getPermutationNormalTextureID() { @@ -43,3 +54,34 @@ GLuint TextureCache::getPermutationNormalTextureID() { } return _permutationNormalTextureID; } + +QOpenGLFramebufferObject* TextureCache::getPrimaryFramebufferObject() { + if (_primaryFramebufferObject == NULL) { + _primaryFramebufferObject = new QOpenGLFramebufferObject(Application::getInstance()->getGLWidget()->size()); + Application::getInstance()->getGLWidget()->installEventFilter(this); + } + return _primaryFramebufferObject; +} + +QOpenGLFramebufferObject* TextureCache::getSecondaryFramebufferObject() { + if (_secondaryFramebufferObject == NULL) { + _secondaryFramebufferObject = new QOpenGLFramebufferObject(Application::getInstance()->getGLWidget()->size()); + Application::getInstance()->getGLWidget()->installEventFilter(this); + } + return _secondaryFramebufferObject; +} + +bool TextureCache::eventFilter(QObject* watched, QEvent* event) { + if (event->type() == QEvent::Resize) { + QSize size = static_cast(event)->size(); + if (_primaryFramebufferObject != NULL && _primaryFramebufferObject->size() != size) { + delete _primaryFramebufferObject; + _primaryFramebufferObject = NULL; + } + if (_secondaryFramebufferObject != NULL && _secondaryFramebufferObject->size() != size) { + delete _secondaryFramebufferObject; + _secondaryFramebufferObject = NULL; + } + } + return false; +} diff --git a/interface/src/renderer/TextureCache.h b/interface/src/renderer/TextureCache.h index 9804f038ba..29920e1f1f 100644 --- a/interface/src/renderer/TextureCache.h +++ b/interface/src/renderer/TextureCache.h @@ -9,9 +9,13 @@ #ifndef __interface__TextureCache__ #define __interface__TextureCache__ +#include + #include "InterfaceConfig.h" -class TextureCache { +class QOpenGLFramebufferObject; + +class TextureCache : public QObject { public: TextureCache(); @@ -19,9 +23,17 @@ public: GLuint getPermutationNormalTextureID(); + QOpenGLFramebufferObject* getPrimaryFramebufferObject(); + QOpenGLFramebufferObject* getSecondaryFramebufferObject(); + + virtual bool eventFilter(QObject* watched, QEvent* event); + private: GLuint _permutationNormalTextureID; + + QOpenGLFramebufferObject* _primaryFramebufferObject; + QOpenGLFramebufferObject* _secondaryFramebufferObject; }; #endif /* defined(__interface__TextureCache__) */ From a3cee5d052e4149f32b961d1eb4e99c845f2e396 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Fri, 9 Aug 2013 16:16:02 -0700 Subject: [PATCH 03/41] More efficiency for glow effect. --- interface/src/Application.cpp | 29 ++++++++++++--------- interface/src/VoxelSystem.cpp | 4 +-- interface/src/renderer/GlowEffect.cpp | 37 +++++++++++++++------------ interface/src/renderer/GlowEffect.h | 8 +++--- 4 files changed, 43 insertions(+), 35 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index e4eee008c6..2b0a2be54c 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -22,6 +22,9 @@ #include #include +// include this before QGLWidget, which includes an earlier version of OpenGL +#include "InterfaceConfig.h" + #include #include #include @@ -59,7 +62,6 @@ #include #include "Application.h" -#include "InterfaceConfig.h" #include "LogDisplay.h" #include "LeapManager.h" #include "OculusManager.h" @@ -92,6 +94,10 @@ const int STARTUP_JITTER_SAMPLES = PACKET_LENGTH_SAMPLES_PER_CHANNEL / 2; // customized canvas that simply forwards requests/events to the singleton application class GLCanvas : public QGLWidget { +public: + + GLCanvas(); + protected: virtual void initializeGL(); @@ -110,6 +116,9 @@ protected: virtual void wheelEvent(QWheelEvent* event); }; +GLCanvas::GLCanvas() : QGLWidget(QGLFormat(QGL::AlphaChannel)) { +} + void GLCanvas::initializeGL() { Application::getInstance()->initializeGL(); setAttribute(Qt::WA_AcceptTouchEvents); @@ -2199,8 +2208,8 @@ void Application::runTests() { void Application::initDisplay() { glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glShadeModel (GL_SMOOTH); + glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE); + glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); @@ -3001,6 +3010,9 @@ void Application::displaySide(Camera& whichCamera) { // Setup 3D lights (after the camera transform, so that they are positioned in world space) setupWorldLight(whichCamera); + // prepare the glow effect + _glowEffect.prepare(); + if (_renderStarsOn->isChecked()) { if (!_stars.getFileLoaded()) { _stars.readInput(STAR_FILE, STAR_CACHE_FILE, 0); @@ -3033,9 +3045,6 @@ void Application::displaySide(Camera& whichCamera) { glEnable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); - // prepare the glow effect - _glowEffect.prepare(); - // Enable to show line from me to the voxel I am touching //renderLineToTouchedVoxel(); //renderThrustAtVoxel(_voxelThrust); @@ -3131,13 +3140,9 @@ void Application::displaySide(Camera& whichCamera) { if (_myCamera.getMode() == CAMERA_MODE_MIRROR) { _myAvatar.getHead().setLookAtPosition(_myCamera.getPosition()); } + _glowEffect.begin(); _myAvatar.render(_lookingInMirror->isChecked(), _renderAvatarBalls->isChecked()); - - _glowEffect.bind(); - _myAvatar.render(_lookingInMirror->isChecked(), _renderAvatarBalls->isChecked()); - _glowEffect.release(); - - _myAvatar.render(_lookingInMirror->isChecked(), _renderAvatarBalls->isChecked()); + _glowEffect.end(); _myAvatar.setDisplayingLookatVectors(_renderLookatOn->isChecked()); diff --git a/interface/src/VoxelSystem.cpp b/interface/src/VoxelSystem.cpp index 5351f129be..becf9f868f 100644 --- a/interface/src/VoxelSystem.cpp +++ b/interface/src/VoxelSystem.cpp @@ -695,8 +695,7 @@ void VoxelSystem::render(bool texture) { applyScaleAndBindProgram(texture); - // for performance, disable blending and enable backface culling - glDisable(GL_BLEND); + // for performance, enable backface culling glEnable(GL_CULL_FACE); // draw the number of voxels we have @@ -704,7 +703,6 @@ void VoxelSystem::render(bool texture) { glDrawRangeElementsEXT(GL_TRIANGLES, 0, VERTICES_PER_VOXEL * _voxelsInReadArrays - 1, 36 * _voxelsInReadArrays, GL_UNSIGNED_INT, 0); - glEnable(GL_BLEND); glDisable(GL_CULL_FACE); removeScaleAndReleaseProgram(texture); diff --git a/interface/src/renderer/GlowEffect.cpp b/interface/src/renderer/GlowEffect.cpp index 82a9c43820..89aff160ff 100644 --- a/interface/src/renderer/GlowEffect.cpp +++ b/interface/src/renderer/GlowEffect.cpp @@ -5,11 +5,13 @@ // Created by Andrzej Kapolka on 8/7/13. // Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// include this before QGLWidget, which includes an earlier version of OpenGL +#include "InterfaceConfig.h" + #include #include "Application.h" #include "GlowEffect.h" -#include "InterfaceConfig.h" #include "ProgramObject.h" static ProgramObject* createBlurProgram(const QString& direction) { @@ -31,17 +33,16 @@ void GlowEffect::init() { } void GlowEffect::prepare() { - bind(); - glClear(GL_COLOR_BUFFER_BIT); - release(); + _isEmpty = true; } -void GlowEffect::bind() { - Application::getInstance()->getTextureCache()->getPrimaryFramebufferObject()->bind(); +void GlowEffect::begin(float amount) { + glBlendColor(0.0f, 0.0f, 0.0f, amount); + _isEmpty = false; } -void GlowEffect::release() { - Application::getInstance()->getTextureCache()->getPrimaryFramebufferObject()->release(); +void GlowEffect::end() { + glBlendColor(0.0f, 0.0f, 0.0f, 0.0f); } static void renderFullscreenQuad() { @@ -58,10 +59,15 @@ static void renderFullscreenQuad() { } void GlowEffect::render() { - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, Application::getInstance()->getTextureCache()->getPrimaryFramebufferObject()->texture()); - glDisable(GL_LIGHTING); - + if (_isEmpty) { + return; // nothing glowing + } + QOpenGLFramebufferObject* primaryFBO = Application::getInstance()->getTextureCache()->getPrimaryFramebufferObject(); + glBindTexture(GL_TEXTURE_2D, primaryFBO->texture()); + + QSize size = primaryFBO->size(); + glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, size.width(), size.height()); + glPushMatrix(); glLoadIdentity(); @@ -85,7 +91,7 @@ void GlowEffect::render() { glBindTexture(GL_TEXTURE_2D, secondaryFBO->texture()); glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE); + glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_CONSTANT_ALPHA, GL_ONE); _verticalBlurProgram->bind(); renderFullscreenQuad(); @@ -98,8 +104,5 @@ void GlowEffect::render() { glBindTexture(GL_TEXTURE_2D, 0); - glDisable(GL_TEXTURE_2D); - glEnable(GL_LIGHTING); - - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE); } diff --git a/interface/src/renderer/GlowEffect.h b/interface/src/renderer/GlowEffect.h index ab07556fee..06ca6e5712 100644 --- a/interface/src/renderer/GlowEffect.h +++ b/interface/src/renderer/GlowEffect.h @@ -18,15 +18,17 @@ public: void prepare(); - void bind(); - void release(); + void begin(float amount = 1.0f); + void end(); void render(); private: ProgramObject* _horizontalBlurProgram; - ProgramObject* _verticalBlurProgram; + ProgramObject* _verticalBlurProgram; + + bool _isEmpty; }; #endif /* defined(__interface__GlowEffect__) */ From 70344cdaf27232ababd1a70dbb70f3d6baf68074 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 12 Aug 2013 11:46:57 -0700 Subject: [PATCH 04/41] move voxel receiving into class --- interface/src/Application.cpp | 90 ++----------------------- interface/src/Application.h | 11 ++- interface/src/VoxelPacketReceiver.cpp | 62 +++++++++++++++++ interface/src/VoxelPacketReceiver.h | 26 +++++++ libraries/shared/src/PacketReceiver.cpp | 70 +++++++++++++++++++ libraries/shared/src/PacketReceiver.h | 45 +++++++++++++ 6 files changed, 213 insertions(+), 91 deletions(-) create mode 100644 interface/src/VoxelPacketReceiver.cpp create mode 100644 interface/src/VoxelPacketReceiver.h create mode 100644 libraries/shared/src/PacketReceiver.cpp create mode 100644 libraries/shared/src/PacketReceiver.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 174dfc2527..46b2a2243c 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -216,7 +216,7 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : _audio(&_audioScope, STARTUP_JITTER_SAMPLES), #endif _stopNetworkReceiveThread(false), - _stopProcessVoxelsThread(false), + _voxelReceiver(this), _packetCount(0), _packetsPerSecond(0), _bytesPerSecond(0), @@ -368,9 +368,9 @@ void Application::initializeGL() { } // create thread for parsing of voxel data independent of the main network and rendering threads + _voxelReceiver.initialize(_enableProcessVoxelsThread); if (_enableProcessVoxelsThread) { - pthread_create(&_processVoxelsThread, NULL, processVoxels, NULL); - qDebug("Voxel parsing thread created.\n"); + qDebug("Voxel parsing thread created.\n"); } // call terminate before exiting @@ -1178,10 +1178,7 @@ void Application::terminate() { pthread_join(_networkReceiveThread, NULL); } - if (_enableProcessVoxelsThread) { - _stopProcessVoxelsThread = true; - pthread_join(_processVoxelsThread, NULL); - } + _voxelReceiver.terminate(); } void Application::sendAvatarVoxelURLMessage(const QUrl& url) { @@ -2580,7 +2577,7 @@ void Application::update(float deltaTime) { // parse voxel packets if (!_enableProcessVoxelsThread) { - processVoxels(0); + _voxelReceiver.threadRoutine(); } //loop through all the other avatars and simulate them... @@ -4034,75 +4031,6 @@ int Application::parseVoxelStats(unsigned char* messageData, ssize_t messageLeng return statsMessageLength; } -// Receive packets from other nodes/servers and decide what to do with them! -void* Application::processVoxels(void* args) { - Application* app = Application::getInstance(); - while (!app->_stopProcessVoxelsThread) { - - // 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; - } - - while (app->_voxelPackets.size() > 0) { - NetworkPacket& packet = app->_voxelPackets.front(); - app->processVoxelPacket(packet.getSenderAddress(), packet.getData(), packet.getLength()); - app->_voxelPackets.erase(app->_voxelPackets.begin()); - } - - if (!app->_enableProcessVoxelsThread) { - break; - } - } - - if (app->_enableProcessVoxelsThread) { - pthread_exit(0); - } - return NULL; -} - -void Application::queueVoxelPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { - _voxelPackets.push_back(NetworkPacket(senderAddress, packetData, packetLength)); -} - -void Application::processVoxelPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { - PerformanceWarning warn(_renderPipelineWarnings->isChecked(),"processVoxelPacket()"); - ssize_t messageLength = packetLength; - - // note: PACKET_TYPE_VOXEL_STATS can have PACKET_TYPE_VOXEL_DATA or PACKET_TYPE_VOXEL_DATA_MONOCHROME - // immediately following them inside the same packet. So, we process the PACKET_TYPE_VOXEL_STATS first - // then process any remaining bytes as if it was another packet - if (packetData[0] == PACKET_TYPE_VOXEL_STATS) { - - int statsMessageLength = parseVoxelStats(packetData, messageLength, senderAddress); - if (messageLength > statsMessageLength) { - packetData += statsMessageLength; - messageLength -= statsMessageLength; - if (!packetVersionMatch(packetData)) { - return; // bail since piggyback data doesn't match our versioning - } - } else { - return; // bail since no piggyback data - } - } // fall through to piggyback message - - if (_renderVoxels->isChecked()) { - Node* voxelServer = NodeList::getInstance()->nodeWithAddress(&senderAddress); - if (voxelServer && socketMatch(voxelServer->getActiveSocket(), &senderAddress)) { - voxelServer->lock(); - if (packetData[0] == PACKET_TYPE_ENVIRONMENT_DATA) { - _environment.parseData(&senderAddress, packetData, messageLength); - } else { - _voxels.setDataSourceID(voxelServer->getNodeID()); - _voxels.parseData(packetData, messageLength); - _voxels.setDataSourceID(UNKNOWN_NODE_ID); - } - voxelServer->unlock(); - } - } -} - // Receive packets from other nodes/servers and decide what to do with them! void* Application::networkReceive(void* args) { sockaddr senderAddress; @@ -4110,12 +4038,6 @@ void* Application::networkReceive(void* args) { Application* app = Application::getInstance(); 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 (NodeList::getInstance()->getNodeSocket()->receive(&senderAddress, app->_incomingPacket, &bytesReceived)) { app->_packetCount++; @@ -4139,7 +4061,7 @@ void* Application::networkReceive(void* args) { case PACKET_TYPE_VOXEL_STATS: case PACKET_TYPE_ENVIRONMENT_DATA: { // add this packet to our list of voxel packets and process them on the voxel processing - app->queueVoxelPacket(senderAddress, app->_incomingPacket, bytesReceived); + app->_voxelReceiver.queuePacket(senderAddress, app->_incomingPacket, bytesReceived); break; } case PACKET_TYPE_BULK_AVATAR_DATA: diff --git a/interface/src/Application.h b/interface/src/Application.h index 29f7e8bea7..0a62fdd665 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -38,6 +38,7 @@ #include "ToolsPalette.h" #include "ViewFrustum.h" #include "VoxelFade.h" +#include "VoxelPacketReceiver.h" #include "VoxelSystem.h" #include "Webcam.h" #include "PieMenu.h" @@ -72,6 +73,8 @@ static const float NODE_KILLED_BLUE = 0.0f; class Application : public QApplication, public NodeListHook { Q_OBJECT + friend class VoxelPacketReceiver; + public: static Application* getInstance() { return static_cast(QCoreApplication::instance()); } @@ -263,10 +266,6 @@ private: static void attachNewHeadToNode(Node *newNode); static void* networkReceive(void* args); // network receive thread - static void* processVoxels(void* args); // voxel parsing thread - void processVoxelPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); - void queueVoxelPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); - // methodes handling menu settings typedef void(*settingsAction)(QSettings*, QAction*); static void loadAction(QSettings* set, QAction* action); @@ -457,9 +456,7 @@ private: bool _stopNetworkReceiveThread; bool _enableProcessVoxelsThread; - pthread_t _processVoxelsThread; - bool _stopProcessVoxelsThread; - std::vector _voxelPackets; + VoxelPacketReceiver _voxelReceiver; unsigned char _incomingPacket[MAX_PACKET_SIZE]; diff --git a/interface/src/VoxelPacketReceiver.cpp b/interface/src/VoxelPacketReceiver.cpp new file mode 100644 index 0000000000..fdaa96e8dc --- /dev/null +++ b/interface/src/VoxelPacketReceiver.cpp @@ -0,0 +1,62 @@ +// +// VoxelPacketReceiver.cpp +// interface +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Threaded or non-threaded voxel packet receiver for the Application +// + +#include + +#include "Application.h" +#include "VoxelPacketReceiver.h" + +VoxelPacketReceiver::VoxelPacketReceiver(Application* app) : + _app(app) { +} + +void VoxelPacketReceiver::processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { + PerformanceWarning warn(_app->_renderPipelineWarnings->isChecked(),"processVoxelPacket()"); + ssize_t messageLength = packetLength; + + // 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; + } + + // note: PACKET_TYPE_VOXEL_STATS can have PACKET_TYPE_VOXEL_DATA or PACKET_TYPE_VOXEL_DATA_MONOCHROME + // immediately following them inside the same packet. So, we process the PACKET_TYPE_VOXEL_STATS first + // then process any remaining bytes as if it was another packet + if (packetData[0] == PACKET_TYPE_VOXEL_STATS) { + + int statsMessageLength = _app->parseVoxelStats(packetData, messageLength, senderAddress); + if (messageLength > statsMessageLength) { + packetData += statsMessageLength; + messageLength -= statsMessageLength; + if (!packetVersionMatch(packetData)) { + return; // bail since piggyback data doesn't match our versioning + } + } else { + return; // bail since no piggyback data + } + } // fall through to piggyback message + + if (_app->_renderVoxels->isChecked()) { + Node* voxelServer = NodeList::getInstance()->nodeWithAddress(&senderAddress); + if (voxelServer && socketMatch(voxelServer->getActiveSocket(), &senderAddress)) { + voxelServer->lock(); + if (packetData[0] == PACKET_TYPE_ENVIRONMENT_DATA) { + _app->_environment.parseData(&senderAddress, packetData, messageLength); + } else { + _app->_voxels.setDataSourceID(voxelServer->getNodeID()); + _app->_voxels.parseData(packetData, messageLength); + _app->_voxels.setDataSourceID(UNKNOWN_NODE_ID); + } + voxelServer->unlock(); + } + } +} + diff --git a/interface/src/VoxelPacketReceiver.h b/interface/src/VoxelPacketReceiver.h new file mode 100644 index 0000000000..9d27b37fba --- /dev/null +++ b/interface/src/VoxelPacketReceiver.h @@ -0,0 +1,26 @@ +// +// VoxelPacketReceiver.h +// interface +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Voxel Packet Receiver +// + +#ifndef __shared__VoxelPacketReceiver__ +#define __shared__VoxelPacketReceiver__ + +#include + +class Application; + +class VoxelPacketReceiver : public PacketReceiver { +public: + VoxelPacketReceiver(Application* app); + virtual void processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); + +private: + Application* _app; +}; +#endif // __shared__VoxelPacketReceiver__ diff --git a/libraries/shared/src/PacketReceiver.cpp b/libraries/shared/src/PacketReceiver.cpp new file mode 100644 index 0000000000..9bf4764ebb --- /dev/null +++ b/libraries/shared/src/PacketReceiver.cpp @@ -0,0 +1,70 @@ +// +// PacketReceiver.cpp +// shared +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Threaded or non-threaded packet receiver. +// + +#include "PacketReceiver.h" + +PacketReceiver::PacketReceiver() : + _stopThread(false), + _isThreaded(false) // assume non-threaded, must call initialize() +{ +} + +PacketReceiver::~PacketReceiver() { + terminate(); +} + +void PacketReceiver::initialize(bool isThreaded) { + _isThreaded = isThreaded; + if (_isThreaded) { + pthread_create(&_thread, NULL, PacketReceiverThreadEntry, this); + } +} + +void PacketReceiver::terminate() { + if (_isThreaded) { + _stopThread = true; + pthread_join(_thread, NULL); + _isThreaded = false; + } +} + +void PacketReceiver::queuePacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { + _packets.push_back(NetworkPacket(senderAddress, packetData, packetLength)); +} + +void PacketReceiver::processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { + // Default implementation does nothing... packet is discarded +} + +void* PacketReceiver::threadRoutine() { + while (!_stopThread) { + while (_packets.size() > 0) { + NetworkPacket& packet = _packets.front(); + processPacket(packet.getSenderAddress(), packet.getData(), packet.getLength()); + _packets.erase(_packets.begin()); + } + + // In non-threaded mode, this will break each time you call it so it's the + // callers responsibility to continuously call this method + if (!_isThreaded) { + break; + } + } + + if (_isThreaded) { + pthread_exit(0); + } + return NULL; +} + +extern "C" void* PacketReceiverThreadEntry(void* arg) { + PacketReceiver* packetReceiver = (PacketReceiver*)arg; + return packetReceiver->threadRoutine(); +} \ No newline at end of file diff --git a/libraries/shared/src/PacketReceiver.h b/libraries/shared/src/PacketReceiver.h new file mode 100644 index 0000000000..f236b362e6 --- /dev/null +++ b/libraries/shared/src/PacketReceiver.h @@ -0,0 +1,45 @@ +// +// PacketReceiver.h +// shared +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Threaded or non-threaded packet receiver. +// + +#ifndef __shared__PacketReceiver__ +#define __shared__PacketReceiver__ + +#include "NetworkPacket.h" + +class PacketReceiver { +public: + PacketReceiver(); + virtual ~PacketReceiver(); + + // Call this when your network receive gets a packet + void queuePacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); + + // implement this to process the incoming packets + virtual void processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); + + // Call to start the thread + void initialize(bool isThreaded); + + // Call when you're ready to stop the thread + void terminate(); + + // If you're running in non-threaded mode, you must call this regularly + void* threadRoutine(); + +private: + bool _stopThread; + bool _isThreaded; + pthread_t _thread; + std::vector _packets; +}; + +extern "C" void* PacketReceiverThreadEntry(void* arg); + +#endif // __shared__PacketReceiver__ From e7b3d41c330b4d65c5433a7120a74139519fb14f Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 12 Aug 2013 12:04:05 -0700 Subject: [PATCH 05/41] make PacketReceiver derive from GenericThread --- interface/src/Application.cpp | 2 +- libraries/shared/src/GenericThread.cpp | 62 +++++++++++++++++++++++++ libraries/shared/src/GenericThread.h | 41 ++++++++++++++++ libraries/shared/src/PacketReceiver.cpp | 58 +++-------------------- libraries/shared/src/PacketReceiver.h | 19 ++------ 5 files changed, 114 insertions(+), 68 deletions(-) create mode 100644 libraries/shared/src/GenericThread.cpp create mode 100644 libraries/shared/src/GenericThread.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 46b2a2243c..0c19d3f5c6 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2577,7 +2577,7 @@ void Application::update(float deltaTime) { // parse voxel packets if (!_enableProcessVoxelsThread) { - _voxelReceiver.threadRoutine(); + _voxelReceiver.process(); } //loop through all the other avatars and simulate them... diff --git a/libraries/shared/src/GenericThread.cpp b/libraries/shared/src/GenericThread.cpp new file mode 100644 index 0000000000..d75e16627c --- /dev/null +++ b/libraries/shared/src/GenericThread.cpp @@ -0,0 +1,62 @@ +// +// GenericThread.cpp +// shared +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Generic Threaded or non-threaded processing class +// + +#include "GenericThread.h" + +GenericThread::GenericThread() : + _stopThread(false), + _isThreaded(false) // assume non-threaded, must call initialize() +{ +} + +GenericThread::~GenericThread() { + terminate(); +} + +void GenericThread::initialize(bool isThreaded) { + _isThreaded = isThreaded; + if (_isThreaded) { + pthread_create(&_thread, NULL, GenericThreadEntry, this); + } +} + +void GenericThread::terminate() { + if (_isThreaded) { + _stopThread = true; + pthread_join(_thread, NULL); + _isThreaded = false; + } +} + +void* GenericThread::threadRoutine() { + while (!_stopThread) { + + // override this function to do whatever your class actually does, return false to exit thread early + if (!process()) { + break; + } + + // In non-threaded mode, this will break each time you call it so it's the + // callers responsibility to continuously call this method + if (!_isThreaded) { + break; + } + } + + if (_isThreaded) { + pthread_exit(0); + } + return NULL; +} + +extern "C" void* GenericThreadEntry(void* arg) { + GenericThread* genericThread = (GenericThread*)arg; + return genericThread->threadRoutine(); +} \ No newline at end of file diff --git a/libraries/shared/src/GenericThread.h b/libraries/shared/src/GenericThread.h new file mode 100644 index 0000000000..e0c372fe00 --- /dev/null +++ b/libraries/shared/src/GenericThread.h @@ -0,0 +1,41 @@ +// +// GenericThread.h +// shared +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Generic Threaded or non-threaded processing class. +// + +#ifndef __shared__GenericThread__ +#define __shared__GenericThread__ + +#include + +class GenericThread { +public: + GenericThread(); + virtual ~GenericThread(); + + // Call to start the thread + void initialize(bool isThreaded); + + // override this function to do whatever your class actually does, return false to exit thread early + virtual bool process() = 0; + + // Call when you're ready to stop the thread + void terminate(); + + // If you're running in non-threaded mode, you must call this regularly + void* threadRoutine(); + +private: + bool _stopThread; + bool _isThreaded; + pthread_t _thread; +}; + +extern "C" void* GenericThreadEntry(void* arg); + +#endif // __shared__GenericThread__ diff --git a/libraries/shared/src/PacketReceiver.cpp b/libraries/shared/src/PacketReceiver.cpp index 9bf4764ebb..39542eb21e 100644 --- a/libraries/shared/src/PacketReceiver.cpp +++ b/libraries/shared/src/PacketReceiver.cpp @@ -10,61 +10,15 @@ #include "PacketReceiver.h" -PacketReceiver::PacketReceiver() : - _stopThread(false), - _isThreaded(false) // assume non-threaded, must call initialize() -{ -} - -PacketReceiver::~PacketReceiver() { - terminate(); -} - -void PacketReceiver::initialize(bool isThreaded) { - _isThreaded = isThreaded; - if (_isThreaded) { - pthread_create(&_thread, NULL, PacketReceiverThreadEntry, this); - } -} - -void PacketReceiver::terminate() { - if (_isThreaded) { - _stopThread = true; - pthread_join(_thread, NULL); - _isThreaded = false; - } -} - void PacketReceiver::queuePacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { _packets.push_back(NetworkPacket(senderAddress, packetData, packetLength)); } -void PacketReceiver::processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { - // Default implementation does nothing... packet is discarded -} - -void* PacketReceiver::threadRoutine() { - while (!_stopThread) { - while (_packets.size() > 0) { - NetworkPacket& packet = _packets.front(); - processPacket(packet.getSenderAddress(), packet.getData(), packet.getLength()); - _packets.erase(_packets.begin()); - } - - // In non-threaded mode, this will break each time you call it so it's the - // callers responsibility to continuously call this method - if (!_isThreaded) { - break; - } +bool PacketReceiver::process() { + while (_packets.size() > 0) { + NetworkPacket& packet = _packets.front(); + processPacket(packet.getSenderAddress(), packet.getData(), packet.getLength()); + _packets.erase(_packets.begin()); } - - if (_isThreaded) { - pthread_exit(0); - } - return NULL; + return true; // keep running till they terminate us } - -extern "C" void* PacketReceiverThreadEntry(void* arg) { - PacketReceiver* packetReceiver = (PacketReceiver*)arg; - return packetReceiver->threadRoutine(); -} \ No newline at end of file diff --git a/libraries/shared/src/PacketReceiver.h b/libraries/shared/src/PacketReceiver.h index f236b362e6..c4dabb4906 100644 --- a/libraries/shared/src/PacketReceiver.h +++ b/libraries/shared/src/PacketReceiver.h @@ -11,32 +11,21 @@ #ifndef __shared__PacketReceiver__ #define __shared__PacketReceiver__ +#include "GenericThread.h" #include "NetworkPacket.h" -class PacketReceiver { +class PacketReceiver : public GenericThread { public: - PacketReceiver(); - virtual ~PacketReceiver(); // Call this when your network receive gets a packet void queuePacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); // implement this to process the incoming packets - virtual void processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); + virtual void processPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) = 0; - // Call to start the thread - void initialize(bool isThreaded); - - // Call when you're ready to stop the thread - void terminate(); - - // If you're running in non-threaded mode, you must call this regularly - void* threadRoutine(); + virtual bool process(); private: - bool _stopThread; - bool _isThreaded; - pthread_t _thread; std::vector _packets; }; From 4ea0de16375ed7a2db71be59fa2636f8a7a18615 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 12 Aug 2013 13:39:01 -0700 Subject: [PATCH 06/41] deleting old dead code --- interface/src/Application.cpp | 89 ++++------------------------------- interface/src/Application.h | 7 --- 2 files changed, 9 insertions(+), 87 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 0c19d3f5c6..40eec6c471 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -205,8 +205,6 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : _justEditedVoxel(false), _isLookingAtOtherAvatar(false), _lookatIndicatorScale(1.0f), - _paintOn(false), - _dominantColor(0), _perfStatsOn(false), _chatEntryOn(false), _oculusTextureID(0), @@ -540,27 +538,20 @@ void Application::controlledBroadcastToNodes(unsigned char* broadcastData, size_ // Feed number of bytes to corresponding channel of the bandwidth meter, if any (done otherwise) BandwidthMeter::ChannelIndex channel; switch (nodeTypes[i]) { - case NODE_TYPE_AGENT: - case NODE_TYPE_AVATAR_MIXER: - channel = BandwidthMeter::AVATARS; - break; - case NODE_TYPE_VOXEL_SERVER: - channel = BandwidthMeter::VOXELS; - break; - default: - continue; + case NODE_TYPE_AGENT: + case NODE_TYPE_AVATAR_MIXER: + channel = BandwidthMeter::AVATARS; + break; + case NODE_TYPE_VOXEL_SERVER: + channel = BandwidthMeter::VOXELS; + break; + default: + continue; } self->_bandwidthMeter.outputStream(channel).updateValue(nReceivingNodes * dataBytes); } } -void Application::sendVoxelServerAddScene() { - char message[100]; - sprintf(message,"%c%s",'Z',"add scene"); - int messageSize = strlen(message) + 1; - controlledBroadcastToNodes((unsigned char*)message, messageSize, & NODE_TYPE_VOXEL_SERVER, 1); -} - void Application::keyPressEvent(QKeyEvent* event) { if (activeWindow() == _window) { if (_chatEntryOn) { @@ -626,19 +617,6 @@ void Application::keyPressEvent(QKeyEvent* event) { _viewFrustumOffsetUp += 0.05; break; - case Qt::Key_Ampersand: - _paintOn = !_paintOn; - setupPaintingVoxel(); - break; - - case Qt::Key_AsciiCircum: - shiftPaintingColor(); - break; - - case Qt::Key_Percent: - sendVoxelServerAddScene(); - break; - case Qt::Key_Semicolon: _audio.ping(); break; @@ -2761,26 +2739,6 @@ void Application::updateAvatar(float deltaTime) { sendAvatarVoxelURLMessage(_myAvatar.getVoxels()->getVoxelURL()); } } - - // If I'm in paint mode, send a voxel out to VOXEL server nodes. - 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) { - - PACKET_TYPE message = (_destructiveAddVoxel->isChecked() ? - PACKET_TYPE_SET_VOXEL_DESTRUCTIVE : PACKET_TYPE_SET_VOXEL); - sendVoxelEditMessage(message, _paintingVoxel); - } - } } ///////////////////////////////////////////////////////////////////////////////////// @@ -3268,15 +3226,6 @@ void Application::displayOverlay() { sprintf(nodes, "Servers: %d, Avatars: %d\n", totalServers, totalAvatars); drawtext(_glWidget->width() - 150, 20, 0.10, 0, 1.0, 0, nodes, 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); - } - // render the webcam input frame _webcam.renderPreview(_glWidget->width(), _glWidget->height()); @@ -3734,26 +3683,6 @@ void Application::renderViewFrustum(ViewFrustum& viewFrustum) { } } -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::injectVoxelAddedSoundEffect() { AudioInjector* voxelInjector = AudioInjectionManager::injectorWithCapacity(11025); diff --git a/interface/src/Application.h b/interface/src/Application.h index 0a62fdd665..66a40e80ca 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -218,7 +218,6 @@ private: static void controlledBroadcastToNodes(unsigned char* broadcastData, size_t dataBytes, const char* nodeTypes, int numNodeTypes); - static void sendVoxelServerAddScene(); static bool sendVoxelsOperation(VoxelNode* node, void* extraData); static void sendVoxelEditMessage(PACKET_TYPE type, VoxelDetail& detail); static void sendAvatarVoxelURLMessage(const QUrl& url); @@ -249,8 +248,6 @@ private: void checkBandwidthMeterClick(); - void setupPaintingVoxel(); - void shiftPaintingColor(); bool maybeEditVoxelUnderCursor(); void deleteVoxelUnderCursor(); void eyedropperVoxelUnderCursor(); @@ -423,10 +420,6 @@ private: glm::vec3 _lookatOtherPosition; float _lookatIndicatorScale; - 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; // Do we want to display perfStats? ChatEntry _chatEntry; // chat entry field From ce0c868c89e713f977d25dadc15973cd09000f6e Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 12 Aug 2013 13:40:21 -0700 Subject: [PATCH 07/41] cleanup naming --- libraries/shared/src/NetworkPacket.cpp | 6 +++--- libraries/shared/src/NetworkPacket.h | 11 +++++------ libraries/shared/src/NodeList.cpp | 2 +- libraries/shared/src/PacketReceiver.cpp | 6 +++--- libraries/shared/src/PacketReceiver.h | 2 -- 5 files changed, 12 insertions(+), 15 deletions(-) diff --git a/libraries/shared/src/NetworkPacket.cpp b/libraries/shared/src/NetworkPacket.cpp index 99dc00e9e2..a3d6e5558d 100644 --- a/libraries/shared/src/NetworkPacket.cpp +++ b/libraries/shared/src/NetworkPacket.cpp @@ -17,13 +17,13 @@ NetworkPacket::NetworkPacket() : _packetLength(0) { } NetworkPacket::NetworkPacket(const NetworkPacket& packet) { - memcpy(&_senderAddress, &packet.getSenderAddress(), sizeof(_senderAddress)); + memcpy(&_address, &packet.getAddress(), sizeof(_address)); _packetLength = packet.getLength(); memcpy(&_packetData[0], packet.getData(), _packetLength); } -NetworkPacket::NetworkPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { - memcpy(&_senderAddress, &senderAddress, sizeof(_senderAddress)); +NetworkPacket::NetworkPacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength) { + memcpy(&_address, &address, sizeof(_address)); _packetLength = packetLength; memcpy(&_packetData[0], packetData, packetLength); }; diff --git a/libraries/shared/src/NetworkPacket.h b/libraries/shared/src/NetworkPacket.h index 9f35004443..4c7f2e4466 100644 --- a/libraries/shared/src/NetworkPacket.h +++ b/libraries/shared/src/NetworkPacket.h @@ -19,20 +19,19 @@ class NetworkPacket { public: - NetworkPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); + NetworkPacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength); NetworkPacket(const NetworkPacket& packet); NetworkPacket(); - //~NetworkPacket(); - sockaddr& getSenderAddress() { return _senderAddress; }; + sockaddr& getAddress() { return _address; }; ssize_t getLength() const { return _packetLength; }; unsigned char* getData() { return &_packetData[0]; }; - const sockaddr& getSenderAddress() const { return _senderAddress; }; - const unsigned char* getData() const { return &_packetData[0]; }; + const sockaddr& getAddress() const { return _address; }; + const unsigned char* getData() const { return &_packetData[0]; }; private: - sockaddr _senderAddress; + sockaddr _address; ssize_t _packetLength; unsigned char _packetData[MAX_PACKET_SIZE]; }; diff --git a/libraries/shared/src/NodeList.cpp b/libraries/shared/src/NodeList.cpp index 6c2647efd5..23b39df205 100644 --- a/libraries/shared/src/NodeList.cpp +++ b/libraries/shared/src/NodeList.cpp @@ -439,7 +439,7 @@ void NodeList::addNodeToList(Node* newNode) { notifyHooksOfAddedNode(newNode); } -unsigned NodeList::broadcastToNodes(unsigned char *broadcastData, size_t dataBytes, const char* nodeTypes, int numNodeTypes) { +unsigned NodeList::broadcastToNodes(unsigned char* broadcastData, size_t dataBytes, const char* nodeTypes, int numNodeTypes) { unsigned n = 0; for(NodeList::iterator node = begin(); node != end(); node++) { // only send to the NodeTypes we are asked to send to. diff --git a/libraries/shared/src/PacketReceiver.cpp b/libraries/shared/src/PacketReceiver.cpp index 39542eb21e..0f7d9ab1ab 100644 --- a/libraries/shared/src/PacketReceiver.cpp +++ b/libraries/shared/src/PacketReceiver.cpp @@ -10,14 +10,14 @@ #include "PacketReceiver.h" -void PacketReceiver::queuePacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { - _packets.push_back(NetworkPacket(senderAddress, packetData, packetLength)); +void PacketReceiver::queuePacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength) { + _packets.push_back(NetworkPacket(address, packetData, packetLength)); } bool PacketReceiver::process() { while (_packets.size() > 0) { NetworkPacket& packet = _packets.front(); - processPacket(packet.getSenderAddress(), packet.getData(), packet.getLength()); + processPacket(packet.getAddress(), packet.getData(), packet.getLength()); _packets.erase(_packets.begin()); } return true; // keep running till they terminate us diff --git a/libraries/shared/src/PacketReceiver.h b/libraries/shared/src/PacketReceiver.h index c4dabb4906..27ab68f5de 100644 --- a/libraries/shared/src/PacketReceiver.h +++ b/libraries/shared/src/PacketReceiver.h @@ -29,6 +29,4 @@ private: std::vector _packets; }; -extern "C" void* PacketReceiverThreadEntry(void* arg); - #endif // __shared__PacketReceiver__ From 7d2c69f530618331780bbd7c52af495f6bd00f9a Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 12 Aug 2013 16:55:58 -0700 Subject: [PATCH 08/41] latest work on threaded sending --- interface/src/Application.cpp | 112 +++--------------------- interface/src/Application.h | 6 +- interface/src/VoxelEditPacketSender.cpp | 84 ++++++++++++++++++ interface/src/VoxelEditPacketSender.h | 37 ++++++++ libraries/shared/src/GenericThread.cpp | 2 + libraries/shared/src/GenericThread.h | 6 ++ libraries/shared/src/NetworkPacket.cpp | 46 ++++++++-- libraries/shared/src/NetworkPacket.h | 13 ++- libraries/shared/src/PacketHeaders.h | 1 + libraries/shared/src/PacketReceiver.cpp | 8 +- libraries/shared/src/PacketReceiver.h | 2 +- libraries/shared/src/PacketSender.cpp | 58 ++++++++++++ libraries/shared/src/PacketSender.h | 32 +++++++ 13 files changed, 295 insertions(+), 112 deletions(-) create mode 100644 interface/src/VoxelEditPacketSender.cpp create mode 100644 interface/src/VoxelEditPacketSender.h create mode 100644 libraries/shared/src/PacketSender.cpp create mode 100644 libraries/shared/src/PacketSender.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 40eec6c471..dfe548afd4 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -215,6 +215,7 @@ Application::Application(int& argc, char** argv, timeval &startup_time) : #endif _stopNetworkReceiveThread(false), _voxelReceiver(this), + _voxelEditSender(this), _packetCount(0), _packetsPerSecond(0), _bytesPerSecond(0), @@ -367,6 +368,7 @@ void Application::initializeGL() { // create thread for parsing of voxel data independent of the main network and rendering threads _voxelReceiver.initialize(_enableProcessVoxelsThread); + _voxelEditSender.initialize(_enableProcessVoxelsThread); if (_enableProcessVoxelsThread) { qDebug("Voxel parsing thread created.\n"); } @@ -1157,6 +1159,7 @@ void Application::terminate() { } _voxelReceiver.terminate(); + _voxelEditSender.terminate(); } void Application::sendAvatarVoxelURLMessage(const QUrl& url) { @@ -1506,16 +1509,6 @@ void Application::updateVoxelModeActions() { } } -void Application::sendVoxelEditMessage(PACKET_TYPE type, VoxelDetail& detail) { - unsigned char* bufferOut; - int sizeOut; - - if (createVoxelEditMessage(type, 0, 1, &detail, bufferOut, sizeOut)){ - Application::controlledBroadcastToNodes(bufferOut, sizeOut, & NODE_TYPE_VOXEL_SERVER, 1); - delete[] bufferOut; - } -} - const glm::vec3 Application::getMouseVoxelWorldCoordinates(const VoxelDetail _mouseVoxel) { return glm::vec3((_mouseVoxel.x + _mouseVoxel.s / 2.f) * TREE_SCALE, (_mouseVoxel.y + _mouseVoxel.s / 2.f) * TREE_SCALE, @@ -1553,12 +1546,8 @@ void Application::chooseVoxelPaintColor() { const int MAXIMUM_EDIT_VOXEL_MESSAGE_SIZE = 1500; struct SendVoxelsOperationArgs { - unsigned char* newBaseOctCode; - unsigned char messageBuffer[MAXIMUM_EDIT_VOXEL_MESSAGE_SIZE]; - int bufferInUse; - uint64_t lastSendTime; - int packetsSent; - uint64_t bytesSent; + unsigned char* newBaseOctCode; + Application* app; }; bool Application::sendVoxelsOperation(VoxelNode* node, void* extraData) { @@ -1590,37 +1579,9 @@ bool Application::sendVoxelsOperation(VoxelNode* node, void* extraData) { codeColorBuffer[bytesInCode + GREEN_INDEX] = node->getColor()[GREEN_INDEX]; codeColorBuffer[bytesInCode + BLUE_INDEX ] = node->getColor()[BLUE_INDEX ]; - // if we have room don't have room in the buffer, then send the previously generated message first - if (args->bufferInUse + codeAndColorLength > MAXIMUM_EDIT_VOXEL_MESSAGE_SIZE) { - - args->packetsSent++; - args->bytesSent += args->bufferInUse; - - controlledBroadcastToNodes(args->messageBuffer, args->bufferInUse, & NODE_TYPE_VOXEL_SERVER, 1); - args->bufferInUse = numBytesForPacketHeader((unsigned char*) &PACKET_TYPE_SET_VOXEL_DESTRUCTIVE) - + sizeof(unsigned short int); // reset - - uint64_t now = usecTimestampNow(); - // dynamically sleep until we need to fire off the next set of voxels - uint64_t elapsed = now - args->lastSendTime; - int usecToSleep = CLIENT_TO_SERVER_VOXEL_SEND_INTERVAL_USECS - elapsed; - if (usecToSleep > 0) { - //qDebug("sendVoxelsOperation: packet: %d bytes:%lld elapsed %lld usecs, sleeping for %d usecs!\n", - // args->packetsSent, (long long int)args->bytesSent, (long long int)elapsed, usecToSleep); - - Application::getInstance()->timer(); - - usleep(usecToSleep); - } else { - //qDebug("sendVoxelsOperation: packet: %d bytes:%lld elapsed %lld usecs, no need to sleep!\n", - // args->packetsSent, (long long int)args->bytesSent, (long long int)elapsed); - } - args->lastSendTime = now; - } + args->app->_voxelEditSender.queueVoxelEditMessage(PACKET_TYPE_SET_VOXEL_DESTRUCTIVE, codeColorBuffer, codeAndColorLength); - // copy this node's code color details into our buffer. - memcpy(&args->messageBuffer[args->bufferInUse], codeColorBuffer, codeAndColorLength); - args->bufferInUse += codeAndColorLength; + delete[] codeColorBuffer; } return true; // keep going } @@ -1802,15 +1763,7 @@ void Application::importVoxels() { // the server as an set voxel message, this will also rebase the voxels to the new location unsigned char* calculatedOctCode = NULL; SendVoxelsOperationArgs args; - args.lastSendTime = usecTimestampNow(); - args.packetsSent = 0; - args.bytesSent = 0; - - int numBytesPacketHeader = populateTypeAndVersion(args.messageBuffer, PACKET_TYPE_SET_VOXEL_DESTRUCTIVE); - - unsigned short int* sequenceAt = (unsigned short int*)&args.messageBuffer[numBytesPacketHeader]; - *sequenceAt = 0; - args.bufferInUse = numBytesPacketHeader + sizeof(unsigned short int); // set to command + sequence + args.app = this; // we only need the selected voxel to get the newBaseOctCode, which we can actually calculate from the // voxel size/position details. @@ -1824,31 +1777,8 @@ void Application::importVoxels() { // send the insert/paste of these voxels importVoxels.recurseTreeWithOperation(sendVoxelsOperation, &args); + _voxelEditSender.flushQueue(); - // If we have voxels left in the packet, then send the packet - if (args.bufferInUse > (numBytesPacketHeader + sizeof(unsigned short int))) { - controlledBroadcastToNodes(args.messageBuffer, args.bufferInUse, & NODE_TYPE_VOXEL_SERVER, 1); - - - args.packetsSent++; - args.bytesSent += args.bufferInUse; - - uint64_t now = usecTimestampNow(); - // dynamically sleep until we need to fire off the next set of voxels - uint64_t elapsed = now - args.lastSendTime; - int usecToSleep = CLIENT_TO_SERVER_VOXEL_SEND_INTERVAL_USECS - elapsed; - - if (usecToSleep > 0) { - //qDebug("after sendVoxelsOperation: packet: %d bytes:%lld elapsed %lld usecs, sleeping for %d usecs!\n", - // args.packetsSent, (long long int)args.bytesSent, (long long int)elapsed, usecToSleep); - usleep(usecToSleep); - } else { - //qDebug("after sendVoxelsOperation: packet: %d bytes:%lld elapsed %lld usecs, no need to sleep!\n", - // args.packetsSent, (long long int)args.bytesSent, (long long int)elapsed); - } - args.lastSendTime = now; - } - if (calculatedOctCode) { delete[] calculatedOctCode; } @@ -1882,15 +1812,7 @@ void Application::pasteVoxels() { // Recurse the clipboard tree, where everything is root relative, and send all the colored voxels to // the server as an set voxel message, this will also rebase the voxels to the new location SendVoxelsOperationArgs args; - args.lastSendTime = usecTimestampNow(); - args.packetsSent = 0; - args.bytesSent = 0; - - int numBytesPacketHeader = populateTypeAndVersion(args.messageBuffer, PACKET_TYPE_SET_VOXEL_DESTRUCTIVE); - - unsigned short int* sequenceAt = (unsigned short int*)&args.messageBuffer[numBytesPacketHeader]; - *sequenceAt = 0; - args.bufferInUse = numBytesPacketHeader + sizeof(unsigned short int); // set to command + sequence + args.app = this; // we only need the selected voxel to get the newBaseOctCode, which we can actually calculate from the // voxel size/position details. If we don't have an actual selectedNode then use the mouseVoxel to create a @@ -1902,14 +1824,7 @@ void Application::pasteVoxels() { } _clipboardTree.recurseTreeWithOperation(sendVoxelsOperation, &args); - - // If we have voxels left in the packet, then send the packet - if (args.bufferInUse > (numBytesPacketHeader + sizeof(unsigned short int))) { - controlledBroadcastToNodes(args.messageBuffer, args.bufferInUse, & NODE_TYPE_VOXEL_SERVER, 1); - qDebug("sending packet: %d\n", ++args.packetsSent); - args.bytesSent += args.bufferInUse; - qDebug("total bytes sent: %lld\n", (long long int)args.bytesSent); - } + _voxelEditSender.flushQueue(); if (calculatedOctCode) { delete[] calculatedOctCode; @@ -2556,6 +2471,7 @@ void Application::update(float deltaTime) { // parse voxel packets if (!_enableProcessVoxelsThread) { _voxelReceiver.process(); + _voxelEditSender.process(); } //loop through all the other avatars and simulate them... @@ -3744,7 +3660,7 @@ bool Application::maybeEditVoxelUnderCursor() { if (_mouseVoxel.s != 0) { PACKET_TYPE message = (_destructiveAddVoxel->isChecked() ? PACKET_TYPE_SET_VOXEL_DESTRUCTIVE : PACKET_TYPE_SET_VOXEL); - sendVoxelEditMessage(message, _mouseVoxel); + _voxelEditSender.sendVoxelEditMessage(message, _mouseVoxel); // create the voxel locally so it appears immediately _voxels.createVoxel(_mouseVoxel.x, _mouseVoxel.y, _mouseVoxel.z, _mouseVoxel.s, @@ -3790,7 +3706,7 @@ bool Application::maybeEditVoxelUnderCursor() { void Application::deleteVoxelUnderCursor() { if (_mouseVoxel.s != 0) { // sending delete to the server is sufficient, server will send new version so we see updates soon enough - sendVoxelEditMessage(PACKET_TYPE_ERASE_VOXEL, _mouseVoxel); + _voxelEditSender.sendVoxelEditMessage(PACKET_TYPE_ERASE_VOXEL, _mouseVoxel); AudioInjector* voxelInjector = AudioInjectionManager::injectorWithCapacity(5000); if (voxelInjector) { diff --git a/interface/src/Application.h b/interface/src/Application.h index 66a40e80ca..89b8f9d28b 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -38,6 +38,7 @@ #include "ToolsPalette.h" #include "ViewFrustum.h" #include "VoxelFade.h" +#include "VoxelEditPacketSender.h" #include "VoxelPacketReceiver.h" #include "VoxelSystem.h" #include "Webcam.h" @@ -74,6 +75,7 @@ class Application : public QApplication, public NodeListHook { Q_OBJECT friend class VoxelPacketReceiver; + friend class VoxelEditPacketSender; public: static Application* getInstance() { return static_cast(QCoreApplication::instance()); } @@ -219,7 +221,6 @@ private: const char* nodeTypes, int numNodeTypes); static bool sendVoxelsOperation(VoxelNode* node, void* extraData); - static void sendVoxelEditMessage(PACKET_TYPE type, VoxelDetail& detail); static void sendAvatarVoxelURLMessage(const QUrl& url); static void processAvatarVoxelURLMessage(unsigned char* packetData, size_t dataBytes); static void processAvatarFaceVideoMessage(unsigned char* packetData, size_t dataBytes); @@ -449,7 +450,8 @@ private: bool _stopNetworkReceiveThread; bool _enableProcessVoxelsThread; - VoxelPacketReceiver _voxelReceiver; + VoxelPacketReceiver _voxelReceiver; + VoxelEditPacketSender _voxelEditSender; unsigned char _incomingPacket[MAX_PACKET_SIZE]; diff --git a/interface/src/VoxelEditPacketSender.cpp b/interface/src/VoxelEditPacketSender.cpp new file mode 100644 index 0000000000..0e1a2b3343 --- /dev/null +++ b/interface/src/VoxelEditPacketSender.cpp @@ -0,0 +1,84 @@ +// +// VoxelEditPacketSender.cpp +// interface +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Threaded or non-threaded voxel packet Sender for the Application +// + +#include + +#include "Application.h" +#include "VoxelEditPacketSender.h" + +VoxelEditPacketSender::VoxelEditPacketSender(Application* app) : + _app(app), + _currentType(PACKET_TYPE_UNKNOWN), + _currentSize(0) +{ +} + +void VoxelEditPacketSender::sendVoxelEditMessage(PACKET_TYPE type, VoxelDetail& detail) { + + // if the app has Voxels disabled, we don't do any of this... + if (!_app->_renderVoxels->isChecked()) { + return; // bail early + } + + unsigned char* bufferOut; + int sizeOut; + int totalBytesSent = 0; + + if (createVoxelEditMessage(type, 0, 1, &detail, bufferOut, sizeOut)){ + actuallySendMessage(bufferOut, sizeOut); + delete[] bufferOut; + } + + // Tell the application's bandwidth meters about what we've sent + _app->_bandwidthMeter.outputStream(BandwidthMeter::VOXELS).updateValue(totalBytesSent); +} + +void VoxelEditPacketSender::actuallySendMessage(unsigned char* bufferOut, ssize_t sizeOut) { + qDebug("VoxelEditPacketSender::actuallySendMessage() sizeOut=%lu\n", sizeOut); + NodeList* nodeList = NodeList::getInstance(); + for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) { + // only send to the NodeTypes we are asked to send to. + if (node->getActiveSocket() != NULL && node->getType() == NODE_TYPE_VOXEL_SERVER) { + // we know which socket is good for this node, send there + sockaddr* nodeAddress = node->getActiveSocket(); + queuePacket(*nodeAddress, bufferOut, sizeOut); + } + } +} + +void VoxelEditPacketSender::queueVoxelEditMessage(PACKET_TYPE type, unsigned char* codeColorBuffer, ssize_t length) { + // If we're switching type, then we send the last one and start over + if ((type != _currentType && _currentSize > 0) || (_currentSize + length >= MAX_PACKET_SIZE)) { + flushQueue(); + initializePacket(type); + } + + // If the buffer is empty and not correctly initialized for our type... + if (type != _currentType && _currentSize == 0) { + initializePacket(type); + } + + memcpy(&_currentBuffer[_currentSize], codeColorBuffer, length); + _currentSize += length; +} + +void VoxelEditPacketSender::flushQueue() { + actuallySendMessage(&_currentBuffer[0], _currentSize); + _currentSize = 0; + _currentType = PACKET_TYPE_UNKNOWN; +} + +void VoxelEditPacketSender::initializePacket(PACKET_TYPE type) { + _currentSize = populateTypeAndVersion(&_currentBuffer[0], type); + unsigned short int* sequenceAt = (unsigned short int*)&_currentBuffer[_currentSize]; + *sequenceAt = 0; + _currentSize += sizeof(unsigned short int); // set to command + sequence + _currentType = type; +} diff --git a/interface/src/VoxelEditPacketSender.h b/interface/src/VoxelEditPacketSender.h new file mode 100644 index 0000000000..5bcc73e7a5 --- /dev/null +++ b/interface/src/VoxelEditPacketSender.h @@ -0,0 +1,37 @@ +// +// VoxelEditPacketSender.h +// interface +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Voxel Packet Sender +// + +#ifndef __shared__VoxelEditPacketSender__ +#define __shared__VoxelEditPacketSender__ + +#include +#include // for VoxelDetail + +class Application; + +class VoxelEditPacketSender : public PacketSender { +public: + VoxelEditPacketSender(Application* app); + + // Some ways you can send voxel edit messages... + void sendVoxelEditMessage(PACKET_TYPE type, VoxelDetail& detail); // sends it right away + void queueVoxelEditMessage(PACKET_TYPE type, unsigned char* codeColorBuffer, ssize_t length); // queues it into a multi-command packet + void flushQueue(); // flushes any queued packets + +private: + void actuallySendMessage(unsigned char* bufferOut, ssize_t sizeOut); + void initializePacket(PACKET_TYPE type); + + Application* _app; + PACKET_TYPE _currentType ; + unsigned char _currentBuffer[MAX_PACKET_SIZE]; + ssize_t _currentSize; +}; +#endif // __shared__VoxelEditPacketSender__ diff --git a/libraries/shared/src/GenericThread.cpp b/libraries/shared/src/GenericThread.cpp index d75e16627c..9f43473278 100644 --- a/libraries/shared/src/GenericThread.cpp +++ b/libraries/shared/src/GenericThread.cpp @@ -14,10 +14,12 @@ GenericThread::GenericThread() : _stopThread(false), _isThreaded(false) // assume non-threaded, must call initialize() { + pthread_mutex_init(&_mutex, 0); } GenericThread::~GenericThread() { terminate(); + pthread_mutex_destroy(&_mutex); } void GenericThread::initialize(bool isThreaded) { diff --git a/libraries/shared/src/GenericThread.h b/libraries/shared/src/GenericThread.h index e0c372fe00..c41e5b29cb 100644 --- a/libraries/shared/src/GenericThread.h +++ b/libraries/shared/src/GenericThread.h @@ -29,8 +29,14 @@ public: // If you're running in non-threaded mode, you must call this regularly void* threadRoutine(); + +protected: + void lock() { pthread_mutex_lock(&_mutex); } + void unlock() { pthread_mutex_unlock(&_mutex); } private: + pthread_mutex_t _mutex; + bool _stopThread; bool _isThreaded; pthread_t _thread; diff --git a/libraries/shared/src/NetworkPacket.cpp b/libraries/shared/src/NetworkPacket.cpp index a3d6e5558d..803972d502 100644 --- a/libraries/shared/src/NetworkPacket.cpp +++ b/libraries/shared/src/NetworkPacket.cpp @@ -10,20 +10,52 @@ #include #include +#include #include "NetworkPacket.h" -NetworkPacket::NetworkPacket() : _packetLength(0) { +NetworkPacket::NetworkPacket() { + _packetLength = 0; +} + +NetworkPacket::~NetworkPacket() { + // nothing to do +} + +void NetworkPacket::copyContents(const sockaddr& address, const unsigned char* packetData, ssize_t packetLength) { + _packetLength = 0; + if (packetLength >=0 && packetLength <= MAX_PACKET_SIZE) { + memcpy(&_address, &address, sizeof(_address)); + _packetLength = packetLength; + memcpy(&_packetData[0], packetData, packetLength); + } else { + qDebug(">>> NetworkPacket::copyContents() unexpected length=%lu\n",packetLength); + } } NetworkPacket::NetworkPacket(const NetworkPacket& packet) { - memcpy(&_address, &packet.getAddress(), sizeof(_address)); - _packetLength = packet.getLength(); - memcpy(&_packetData[0], packet.getData(), _packetLength); + copyContents(packet.getAddress(), packet.getData(), packet.getLength()); } +// move?? // same as copy, but other packet won't be used further +NetworkPacket::NetworkPacket(NetworkPacket && packet) { + copyContents(packet.getAddress(), packet.getData(), packet.getLength()); +} + + NetworkPacket::NetworkPacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength) { - memcpy(&_address, &address, sizeof(_address)); - _packetLength = packetLength; - memcpy(&_packetData[0], packetData, packetLength); + copyContents(address, packetData, packetLength); }; + +// copy assignment +NetworkPacket& NetworkPacket::operator=(NetworkPacket const& other) { + copyContents(other.getAddress(), other.getData(), other.getLength()); + return *this; +} + +// move assignment +NetworkPacket& NetworkPacket::operator=(NetworkPacket&& other) { + _packetLength = 0; + copyContents(other.getAddress(), other.getData(), other.getLength()); + return *this; +} diff --git a/libraries/shared/src/NetworkPacket.h b/libraries/shared/src/NetworkPacket.h index 4c7f2e4466..d82a254591 100644 --- a/libraries/shared/src/NetworkPacket.h +++ b/libraries/shared/src/NetworkPacket.h @@ -19,10 +19,15 @@ class NetworkPacket { public: - NetworkPacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength); - NetworkPacket(const NetworkPacket& packet); NetworkPacket(); - + NetworkPacket(const NetworkPacket& packet); // copy constructor + NetworkPacket(NetworkPacket && packet); // move?? // same as copy, but other packet won't be used further + ~NetworkPacket(); // destructor + NetworkPacket& operator= (NetworkPacket const &other); // copy assignment + NetworkPacket& operator= (NetworkPacket&& other); // move assignment + + NetworkPacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength); + sockaddr& getAddress() { return _address; }; ssize_t getLength() const { return _packetLength; }; unsigned char* getData() { return &_packetData[0]; }; @@ -31,6 +36,8 @@ public: const unsigned char* getData() const { return &_packetData[0]; }; private: + void copyContents(const sockaddr& address, const unsigned char* packetData, ssize_t packetLength); + sockaddr _address; ssize_t _packetLength; unsigned char _packetData[MAX_PACKET_SIZE]; diff --git a/libraries/shared/src/PacketHeaders.h b/libraries/shared/src/PacketHeaders.h index 2b021db82c..3d49b6b066 100644 --- a/libraries/shared/src/PacketHeaders.h +++ b/libraries/shared/src/PacketHeaders.h @@ -13,6 +13,7 @@ #define hifi_PacketHeaders_h typedef char PACKET_TYPE; +const PACKET_TYPE PACKET_TYPE_UNKNOWN = 0; const PACKET_TYPE PACKET_TYPE_DOMAIN = 'D'; const PACKET_TYPE PACKET_TYPE_PING = 'P'; const PACKET_TYPE PACKET_TYPE_PING_REPLY = 'R'; diff --git a/libraries/shared/src/PacketReceiver.cpp b/libraries/shared/src/PacketReceiver.cpp index 0f7d9ab1ab..801243eb5c 100644 --- a/libraries/shared/src/PacketReceiver.cpp +++ b/libraries/shared/src/PacketReceiver.cpp @@ -11,14 +11,20 @@ #include "PacketReceiver.h" void PacketReceiver::queuePacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength) { - _packets.push_back(NetworkPacket(address, packetData, packetLength)); + NetworkPacket packet(address, packetData, packetLength); + lock(); + _packets.push_back(packet); + unlock(); } bool PacketReceiver::process() { while (_packets.size() > 0) { NetworkPacket& packet = _packets.front(); processPacket(packet.getAddress(), packet.getData(), packet.getLength()); + + lock(); _packets.erase(_packets.begin()); + unlock(); } return true; // keep running till they terminate us } diff --git a/libraries/shared/src/PacketReceiver.h b/libraries/shared/src/PacketReceiver.h index 27ab68f5de..7b7f792862 100644 --- a/libraries/shared/src/PacketReceiver.h +++ b/libraries/shared/src/PacketReceiver.h @@ -16,7 +16,6 @@ class PacketReceiver : public GenericThread { public: - // Call this when your network receive gets a packet void queuePacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength); @@ -26,6 +25,7 @@ public: virtual bool process(); private: + std::vector _packets; }; diff --git a/libraries/shared/src/PacketSender.cpp b/libraries/shared/src/PacketSender.cpp new file mode 100644 index 0000000000..61b173c5ee --- /dev/null +++ b/libraries/shared/src/PacketSender.cpp @@ -0,0 +1,58 @@ +// +// PacketSender.cpp +// shared +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Threaded or non-threaded packet sender. +// + +#include + +const uint64_t SEND_INTERVAL_USECS = 1000 * 5; // no more than 200pps... should be settable + +#include "NodeList.h" +#include "PacketSender.h" +#include "SharedUtil.h" + +PacketSender::PacketSender() { + _lastSendTime = usecTimestampNow(); +} + + +void PacketSender::queuePacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength) { + NetworkPacket packet(address, packetData, packetLength); + lock(); + _packets.push_back(packet); + unlock(); +} + +bool PacketSender::process() { + while (_packets.size() > 0) { + NetworkPacket& packet = _packets.front(); + + // send the packet through the NodeList... + UDPSocket* nodeSocket = NodeList::getInstance()->getNodeSocket(); + + qDebug("PacketSender::process()... nodeSocket->send() length=%lu\n", packet.getLength()); + + nodeSocket->send(&packet.getAddress(), packet.getData(), packet.getLength()); + + lock(); + _packets.erase(_packets.begin()); + unlock(); + + uint64_t now = usecTimestampNow(); + // dynamically sleep until we need to fire off the next set of voxels + uint64_t elapsed = now - _lastSendTime; + int usecToSleep = SEND_INTERVAL_USECS - elapsed; + _lastSendTime = now; + if (usecToSleep > 0) { + //qDebug("PacketSender::process()... sleeping for %d useconds\n", usecToSleep); + usleep(usecToSleep); + } + + } + return true; // keep running till they terminate us +} diff --git a/libraries/shared/src/PacketSender.h b/libraries/shared/src/PacketSender.h new file mode 100644 index 0000000000..72b61011cc --- /dev/null +++ b/libraries/shared/src/PacketSender.h @@ -0,0 +1,32 @@ +// +// PacketSender.h +// shared +// +// Created by Brad Hefta-Gaub on 8/12/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// +// Threaded or non-threaded packet sender. +// + +#ifndef __shared__PacketSender__ +#define __shared__PacketSender__ + +#include "GenericThread.h" +#include "NetworkPacket.h" + +class PacketSender : public GenericThread { +public: + + PacketSender(); + // Call this when you have a packet you'd like sent... + void queuePacket(sockaddr& address, unsigned char* packetData, ssize_t packetLength); + + virtual bool process(); + +private: + std::vector _packets; + uint64_t _lastSendTime; + +}; + +#endif // __shared__PacketSender__ From 60dedee7391e5390f3f6dc16bcd7d8f9cd227a82 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 13 Aug 2013 11:08:43 -0700 Subject: [PATCH 09/41] make JurisdictionMap handle copy/move/assigment so that it will work in std::vector<> and std::map<>, switch application to have map of JurisdictionMap objects instead of just root codes --- interface/src/Application.cpp | 20 ++++---- interface/src/Application.h | 2 +- interface/src/VoxelEditPacketSender.cpp | 35 +++++++++++++- libraries/shared/src/OctalCode.h | 2 +- libraries/voxels/src/JurisdictionMap.cpp | 60 ++++++++++++++++++++++++ libraries/voxels/src/JurisdictionMap.h | 12 ++++- libraries/voxels/src/VoxelSceneStats.h | 5 +- 7 files changed, 120 insertions(+), 16 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index dfe548afd4..8ee1fd2b24 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3826,15 +3826,16 @@ void Application::nodeKilled(Node* node) { uint16_t nodeID = node->getNodeID(); // see if this is the first we've heard of this node... if (_voxelServerJurisdictions.find(nodeID) != _voxelServerJurisdictions.end()) { - VoxelPositionSize jurisditionDetails; - jurisditionDetails = _voxelServerJurisdictions[nodeID]; + unsigned char* rootCode = _voxelServerJurisdictions[nodeID].getRootOctalCode(); + VoxelPositionSize rootDetails; + voxelDetailsForCode(rootCode, rootDetails); printf("voxel server going away...... v[%f, %f, %f, %f]\n", - jurisditionDetails.x, jurisditionDetails.y, jurisditionDetails.z, jurisditionDetails.s); + rootDetails.x, rootDetails.y, rootDetails.z, rootDetails.s); // Add the jurisditionDetails object to the list of "fade outs" VoxelFade fade(VoxelFade::FADE_OUT, NODE_KILLED_RED, NODE_KILLED_GREEN, NODE_KILLED_BLUE); - fade.voxelDetails = jurisditionDetails; + fade.voxelDetails = rootDetails; const float slightly_smaller = 0.99; fade.voxelDetails.s = fade.voxelDetails.s * slightly_smaller; _voxelFades.push_back(fade); @@ -3855,23 +3856,24 @@ int Application::parseVoxelStats(unsigned char* messageData, ssize_t messageLeng if (voxelServer) { uint16_t nodeID = voxelServer->getNodeID(); - VoxelPositionSize jurisditionDetails; - voxelDetailsForCode(_voxelSceneStats.getJurisdictionRoot(), jurisditionDetails); + VoxelPositionSize rootDetails; + voxelDetailsForCode(_voxelSceneStats.getJurisdictionRoot(), rootDetails); // see if this is the first we've heard of this node... if (_voxelServerJurisdictions.find(nodeID) == _voxelServerJurisdictions.end()) { printf("stats from new voxel server... v[%f, %f, %f, %f]\n", - jurisditionDetails.x, jurisditionDetails.y, jurisditionDetails.z, jurisditionDetails.s); + rootDetails.x, rootDetails.y, rootDetails.z, rootDetails.s); // Add the jurisditionDetails object to the list of "fade outs" VoxelFade fade(VoxelFade::FADE_OUT, NODE_ADDED_RED, NODE_ADDED_GREEN, NODE_ADDED_BLUE); - fade.voxelDetails = jurisditionDetails; + fade.voxelDetails = rootDetails; const float slightly_smaller = 0.99; fade.voxelDetails.s = fade.voxelDetails.s * slightly_smaller; _voxelFades.push_back(fade); } // store jurisdiction details for later use - _voxelServerJurisdictions[nodeID] = jurisditionDetails; + _voxelServerJurisdictions[nodeID] = JurisdictionMap(_voxelSceneStats.getJurisdictionRoot(), + _voxelSceneStats.getJurisdictionEndNodes()); } return statsMessageLength; } diff --git a/interface/src/Application.h b/interface/src/Application.h index 89b8f9d28b..9ef808f603 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -471,7 +471,7 @@ private: VoxelSceneStats _voxelSceneStats; int parseVoxelStats(unsigned char* messageData, ssize_t messageLength, sockaddr senderAddress); - std::map _voxelServerJurisdictions; + std::map _voxelServerJurisdictions; std::vector _voxelFades; }; diff --git a/interface/src/VoxelEditPacketSender.cpp b/interface/src/VoxelEditPacketSender.cpp index 0e1a2b3343..64016bc592 100644 --- a/interface/src/VoxelEditPacketSender.cpp +++ b/interface/src/VoxelEditPacketSender.cpp @@ -44,9 +44,12 @@ void VoxelEditPacketSender::actuallySendMessage(unsigned char* bufferOut, ssize_ qDebug("VoxelEditPacketSender::actuallySendMessage() sizeOut=%lu\n", sizeOut); NodeList* nodeList = NodeList::getInstance(); for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) { - // only send to the NodeTypes we are asked to send to. + // only send to the NodeTypes that are NODE_TYPE_VOXEL_SERVER if (node->getActiveSocket() != NULL && node->getType() == NODE_TYPE_VOXEL_SERVER) { - // we know which socket is good for this node, send there + + // We want to filter out edit messages for voxel servers based on the server's Jurisdiction + // But we can't really do that with a packed message, since each edit message could be destined + // for a different voxel server... sockaddr* nodeAddress = node->getActiveSocket(); queuePacket(*nodeAddress, bufferOut, sizeOut); } @@ -54,6 +57,34 @@ void VoxelEditPacketSender::actuallySendMessage(unsigned char* bufferOut, ssize_ } void VoxelEditPacketSender::queueVoxelEditMessage(PACKET_TYPE type, unsigned char* codeColorBuffer, ssize_t length) { +/**** + // We want to filter out edit messages for voxel servers based on the server's Jurisdiction + // But we can't really do that with a packed message, since each edit message could be destined + // for a different voxel server... So we need to actually manage multiple queued packets... one + // for each voxel server + for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) { + // only send to the NodeTypes that are NODE_TYPE_VOXEL_SERVER + if (node->getActiveSocket() != NULL && node->getType() == NODE_TYPE_VOXEL_SERVER) { + + // we need to get the jurisdiction for this + // here we need to get the "pending packet" for this server + + // If we're switching type, then we send the last one and start over + if ((type != _currentType && _currentSize > 0) || (_currentSize + length >= MAX_PACKET_SIZE)) { + flushQueue(); + initializePacket(type); + } + + // If the buffer is empty and not correctly initialized for our type... + if (type != _currentType && _currentSize == 0) { + initializePacket(type); + } + + memcpy(&_currentBuffer[_currentSize], codeColorBuffer, length); + _currentSize += length; + } + } +****/ // If we're switching type, then we send the last one and start over if ((type != _currentType && _currentSize > 0) || (_currentSize + length >= MAX_PACKET_SIZE)) { flushQueue(); diff --git a/libraries/shared/src/OctalCode.h b/libraries/shared/src/OctalCode.h index 405b85164d..846b65203b 100644 --- a/libraries/shared/src/OctalCode.h +++ b/libraries/shared/src/OctalCode.h @@ -39,7 +39,7 @@ void copyFirstVertexForCode(unsigned char * octalCode, float* output); struct VoxelPositionSize { float x, y, z, s; }; -void voxelDetailsForCode(unsigned char * octalCode, VoxelPositionSize& voxelPositionSize); +void voxelDetailsForCode(unsigned char* octalCode, VoxelPositionSize& voxelPositionSize); typedef enum { ILLEGAL_CODE = -2, diff --git a/libraries/voxels/src/JurisdictionMap.cpp b/libraries/voxels/src/JurisdictionMap.cpp index b452c12d14..eec78fcda9 100644 --- a/libraries/voxels/src/JurisdictionMap.cpp +++ b/libraries/voxels/src/JurisdictionMap.cpp @@ -13,8 +13,64 @@ #include "JurisdictionMap.h" #include "VoxelNode.h" + +// standard assignment +// copy assignment +JurisdictionMap& JurisdictionMap::operator=(const JurisdictionMap& other) { + copyContents(other); + printf("JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap const& other) COPY ASSIGNMENT %p from %p\n", this, &other); + return *this; +} + +// move assignment +JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap&& other) { + init(other._rootOctalCode, other._endNodes); + other._rootOctalCode = NULL; + other._endNodes.clear(); + printf("JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap&& other) MOVE ASSIGNMENT %p from %p\n", this, &other); + return *this; +} + +// Move constructor +JurisdictionMap::JurisdictionMap(JurisdictionMap&& other) : _rootOctalCode(NULL) { + init(other._rootOctalCode, other._endNodes); + other._rootOctalCode = NULL; + other._endNodes.clear(); + printf("JurisdictionMap::JurisdictionMap(JurisdictionMap&& other) MOVE CONSTRUCTOR %p from %p\n", this, &other); +} + +// Copy constructor +JurisdictionMap::JurisdictionMap(const JurisdictionMap& other) : _rootOctalCode(NULL) { + copyContents(other); + printf("JurisdictionMap::JurisdictionMap(const JurisdictionMap& other) COPY CONSTRUCTOR %p from %p\n", this, &other); +} + +void JurisdictionMap::copyContents(const JurisdictionMap& other) { + unsigned char* rootCode; + std::vector endNodes; + if (other._rootOctalCode) { + int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(other._rootOctalCode)); + rootCode = new unsigned char[bytes]; + memcpy(rootCode, other._rootOctalCode, bytes); + } else { + rootCode = new unsigned char[1]; + *rootCode = 0; + } + + for (int i = 0; i < other._endNodes.size(); i++) { + if (other._endNodes[i]) { + int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(other._endNodes[i])); + unsigned char* endNodeCode = new unsigned char[bytes]; + memcpy(endNodeCode, other._endNodes[i], bytes); + endNodes.push_back(endNodeCode); + } + } + init(rootCode, endNodes); +} + JurisdictionMap::~JurisdictionMap() { clear(); + printf("JurisdictionMap::~JurisdictionMap() DESTRUCTOR %p\n",this); } void JurisdictionMap::clear() { @@ -37,16 +93,19 @@ JurisdictionMap::JurisdictionMap() : _rootOctalCode(NULL) { std::vector emptyEndNodes; init(rootCode, emptyEndNodes); + printf("JurisdictionMap::~JurisdictionMap() DEFAULT CONSTRUCTOR %p\n",this); } JurisdictionMap::JurisdictionMap(const char* filename) : _rootOctalCode(NULL) { clear(); // clean up our own memory readFromFile(filename); + printf("JurisdictionMap::~JurisdictionMap() INI FILE CONSTRUCTOR %p\n",this); } JurisdictionMap::JurisdictionMap(unsigned char* rootOctalCode, const std::vector& endNodes) : _rootOctalCode(NULL) { init(rootOctalCode, endNodes); + printf("JurisdictionMap::~JurisdictionMap() OCTCODE CONSTRUCTOR %p\n",this); } JurisdictionMap::JurisdictionMap(const char* rootHexCode, const char* endNodesHexCodes) { @@ -63,6 +122,7 @@ JurisdictionMap::JurisdictionMap(const char* rootHexCode, const char* endNodesHe //printOctalCode(endNodeOctcode); _endNodes.push_back(endNodeOctcode); } + printf("JurisdictionMap::~JurisdictionMap() HEX STRING CONSTRUCTOR %p\n",this); } diff --git a/libraries/voxels/src/JurisdictionMap.h b/libraries/voxels/src/JurisdictionMap.h index 6622292df6..d54f5147d0 100644 --- a/libraries/voxels/src/JurisdictionMap.h +++ b/libraries/voxels/src/JurisdictionMap.h @@ -20,7 +20,16 @@ public: BELOW }; - JurisdictionMap(); + // standard constructors + JurisdictionMap(); // default constructor + JurisdictionMap(const JurisdictionMap& other); // copy constructor + JurisdictionMap(JurisdictionMap&& other); // move constructor + + // standard assignment + JurisdictionMap& operator= (JurisdictionMap const &other); // copy assignment + JurisdictionMap& operator= (JurisdictionMap&& other); // move assignment + + // application constructors JurisdictionMap(const char* filename); JurisdictionMap(unsigned char* rootOctalCode, const std::vector& endNodes); JurisdictionMap(const char* rootHextString, const char* endNodesHextString); @@ -36,6 +45,7 @@ public: int getEndNodeCount() const { return _endNodes.size(); } private: + void copyContents(const JurisdictionMap& other); void clear(); void init(unsigned char* rootOctalCode, const std::vector& endNodes); diff --git a/libraries/voxels/src/VoxelSceneStats.h b/libraries/voxels/src/VoxelSceneStats.h index 910859ff3e..ba9fa559b1 100644 --- a/libraries/voxels/src/VoxelSceneStats.h +++ b/libraries/voxels/src/VoxelSceneStats.h @@ -81,7 +81,7 @@ public: char* getItemValue(int item); unsigned char* getJurisdictionRoot() const { return _jurisdictionRoot; } - + const std::vector& getJurisdictionEndNodes() const { return _jurisdictionEndNodes; } private: bool _isReadyToSend; @@ -171,7 +171,8 @@ private: static int const MAX_ITEM_VALUE_LENGTH = 128; char _itemValueBuffer[MAX_ITEM_VALUE_LENGTH]; - unsigned char* _jurisdictionRoot; + unsigned char* _jurisdictionRoot; + std::vector _jurisdictionEndNodes; }; #endif /* defined(__hifi__VoxelSceneStats__) */ From d3ce3e4e605a8508b0d2d9dd82d3a1291892296e Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 13 Aug 2013 11:37:57 -0700 Subject: [PATCH 10/41] some cleanup and fixing of memory issue --- interface/src/Application.cpp | 9 ++- libraries/shared/src/OctalCode.cpp | 45 +++++++++++++ libraries/shared/src/OctalCode.h | 5 ++ libraries/voxels/src/JurisdictionMap.cpp | 83 ++++++------------------ libraries/voxels/src/JurisdictionMap.h | 7 +- 5 files changed, 81 insertions(+), 68 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 8ee1fd2b24..082d942ee7 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3872,8 +3872,13 @@ int Application::parseVoxelStats(unsigned char* messageData, ssize_t messageLeng _voxelFades.push_back(fade); } // store jurisdiction details for later use - _voxelServerJurisdictions[nodeID] = JurisdictionMap(_voxelSceneStats.getJurisdictionRoot(), - _voxelSceneStats.getJurisdictionEndNodes()); + + // This is bit of fiddling is because JurisdictionMap assumes it is the owner of the values used to construct it + // but VoxelSceneStats thinks it's just returning a reference to it's contents. So we need to make a copy of the + // details from the VoxelSceneStats to construct the JurisdictionMap + JurisdictionMap jurisdictionMap; + jurisdictionMap.copyContents(_voxelSceneStats.getJurisdictionRoot(), _voxelSceneStats.getJurisdictionEndNodes()); + _voxelServerJurisdictions[nodeID] = jurisdictionMap; } return statsMessageLength; } diff --git a/libraries/shared/src/OctalCode.cpp b/libraries/shared/src/OctalCode.cpp index 5513d362ac..8146924cf8 100644 --- a/libraries/shared/src/OctalCode.cpp +++ b/libraries/shared/src/OctalCode.cpp @@ -319,3 +319,48 @@ bool isAncestorOf(unsigned char* possibleAncestor, unsigned char* possibleDescen // they all match, so we are an ancestor return true; } + +unsigned char* hexStringToOctalCode(const QString& input) { + const int HEX_NUMBER_BASE = 16; + const int HEX_BYTE_SIZE = 2; + int stringIndex = 0; + int byteArrayIndex = 0; + + // allocate byte array based on half of string length + unsigned char* bytes = new unsigned char[(input.length()) / HEX_BYTE_SIZE]; + + // loop through the string - 2 bytes at a time converting + // it to decimal equivalent and store in byte array + bool ok; + while (stringIndex < input.length()) { + uint value = input.mid(stringIndex, HEX_BYTE_SIZE).toUInt(&ok, HEX_NUMBER_BASE); + if (!ok) { + break; + } + bytes[byteArrayIndex] = (unsigned char)value; + stringIndex += HEX_BYTE_SIZE; + byteArrayIndex++; + } + + // something went wrong + if (!ok) { + delete[] bytes; + return NULL; + } + return bytes; +} + +QString octalCodeToHexString(unsigned char* octalCode) { + const int HEX_NUMBER_BASE = 16; + const int HEX_BYTE_SIZE = 2; + QString output; + if (!octalCode) { + output = "00"; + } else { + for (int i = 0; i < bytesRequiredForCodeLength(*octalCode); i++) { + output.append(QString("%1").arg(octalCode[i], HEX_BYTE_SIZE, HEX_NUMBER_BASE, QChar('0')).toUpper()); + } + } + return output; +} + diff --git a/libraries/shared/src/OctalCode.h b/libraries/shared/src/OctalCode.h index 846b65203b..4a43142bb8 100644 --- a/libraries/shared/src/OctalCode.h +++ b/libraries/shared/src/OctalCode.h @@ -10,6 +10,7 @@ #define __hifi__OctalCode__ #include +#include const int BITS_IN_BYTE = 8; const int BITS_IN_OCTAL = 3; @@ -49,4 +50,8 @@ typedef enum { } OctalCodeComparison; OctalCodeComparison compareOctalCodes(unsigned char* code1, unsigned char* code2); + +QString octalCodeToHexString(unsigned char* octalCode); +unsigned char* hexStringToOctalCode(const QString& input); + #endif /* defined(__hifi__OctalCode__) */ diff --git a/libraries/voxels/src/JurisdictionMap.cpp b/libraries/voxels/src/JurisdictionMap.cpp index eec78fcda9..a89d12fa6b 100644 --- a/libraries/voxels/src/JurisdictionMap.cpp +++ b/libraries/voxels/src/JurisdictionMap.cpp @@ -18,7 +18,7 @@ // copy assignment JurisdictionMap& JurisdictionMap::operator=(const JurisdictionMap& other) { copyContents(other); - printf("JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap const& other) COPY ASSIGNMENT %p from %p\n", this, &other); + //printf("JurisdictionMap COPY ASSIGNMENT %p from %p\n", this, &other); return *this; } @@ -27,7 +27,7 @@ JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap&& other) { init(other._rootOctalCode, other._endNodes); other._rootOctalCode = NULL; other._endNodes.clear(); - printf("JurisdictionMap& JurisdictionMap::operator=(JurisdictionMap&& other) MOVE ASSIGNMENT %p from %p\n", this, &other); + //printf("JurisdictionMap MOVE ASSIGNMENT %p from %p\n", this, &other); return *this; } @@ -36,41 +36,45 @@ JurisdictionMap::JurisdictionMap(JurisdictionMap&& other) : _rootOctalCode(NULL) init(other._rootOctalCode, other._endNodes); other._rootOctalCode = NULL; other._endNodes.clear(); - printf("JurisdictionMap::JurisdictionMap(JurisdictionMap&& other) MOVE CONSTRUCTOR %p from %p\n", this, &other); + //printf("JurisdictionMap MOVE CONSTRUCTOR %p from %p\n", this, &other); } // Copy constructor JurisdictionMap::JurisdictionMap(const JurisdictionMap& other) : _rootOctalCode(NULL) { copyContents(other); - printf("JurisdictionMap::JurisdictionMap(const JurisdictionMap& other) COPY CONSTRUCTOR %p from %p\n", this, &other); + //printf("JurisdictionMap COPY CONSTRUCTOR %p from %p\n", this, &other); } -void JurisdictionMap::copyContents(const JurisdictionMap& other) { +void JurisdictionMap::copyContents(unsigned char* rootCodeIn, const std::vector endNodesIn) { unsigned char* rootCode; std::vector endNodes; - if (other._rootOctalCode) { - int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(other._rootOctalCode)); + if (rootCodeIn) { + int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(rootCodeIn)); rootCode = new unsigned char[bytes]; - memcpy(rootCode, other._rootOctalCode, bytes); + memcpy(rootCode, rootCodeIn, bytes); } else { rootCode = new unsigned char[1]; *rootCode = 0; } - for (int i = 0; i < other._endNodes.size(); i++) { - if (other._endNodes[i]) { - int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(other._endNodes[i])); + for (int i = 0; i < endNodesIn.size(); i++) { + if (endNodesIn[i]) { + int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(endNodesIn[i])); unsigned char* endNodeCode = new unsigned char[bytes]; - memcpy(endNodeCode, other._endNodes[i], bytes); + memcpy(endNodeCode, endNodesIn[i], bytes); endNodes.push_back(endNodeCode); } } init(rootCode, endNodes); } +void JurisdictionMap::copyContents(const JurisdictionMap& other) { + copyContents(other._rootOctalCode, other._endNodes); +} + JurisdictionMap::~JurisdictionMap() { clear(); - printf("JurisdictionMap::~JurisdictionMap() DESTRUCTOR %p\n",this); + //printf("JurisdictionMap DESTRUCTOR %p\n",this); } void JurisdictionMap::clear() { @@ -93,19 +97,19 @@ JurisdictionMap::JurisdictionMap() : _rootOctalCode(NULL) { std::vector emptyEndNodes; init(rootCode, emptyEndNodes); - printf("JurisdictionMap::~JurisdictionMap() DEFAULT CONSTRUCTOR %p\n",this); + //printf("JurisdictionMap DEFAULT CONSTRUCTOR %p\n",this); } JurisdictionMap::JurisdictionMap(const char* filename) : _rootOctalCode(NULL) { clear(); // clean up our own memory readFromFile(filename); - printf("JurisdictionMap::~JurisdictionMap() INI FILE CONSTRUCTOR %p\n",this); + //printf("JurisdictionMap INI FILE CONSTRUCTOR %p\n",this); } JurisdictionMap::JurisdictionMap(unsigned char* rootOctalCode, const std::vector& endNodes) : _rootOctalCode(NULL) { init(rootOctalCode, endNodes); - printf("JurisdictionMap::~JurisdictionMap() OCTCODE CONSTRUCTOR %p\n",this); + //printf("JurisdictionMap OCTCODE CONSTRUCTOR %p\n",this); } JurisdictionMap::JurisdictionMap(const char* rootHexCode, const char* endNodesHexCodes) { @@ -122,7 +126,7 @@ JurisdictionMap::JurisdictionMap(const char* rootHexCode, const char* endNodesHe //printOctalCode(endNodeOctcode); _endNodes.push_back(endNodeOctcode); } - printf("JurisdictionMap::~JurisdictionMap() HEX STRING CONSTRUCTOR %p\n",this); + //printf("JurisdictionMap HEX STRING CONSTRUCTOR %p\n",this); } @@ -207,48 +211,3 @@ bool JurisdictionMap::writeToFile(const char* filename) { settings.endGroup(); return true; } - - -unsigned char* JurisdictionMap::hexStringToOctalCode(const QString& input) const { - const int HEX_NUMBER_BASE = 16; - const int HEX_BYTE_SIZE = 2; - int stringIndex = 0; - int byteArrayIndex = 0; - - // allocate byte array based on half of string length - unsigned char* bytes = new unsigned char[(input.length()) / HEX_BYTE_SIZE]; - - // loop through the string - 2 bytes at a time converting - // it to decimal equivalent and store in byte array - bool ok; - while (stringIndex < input.length()) { - uint value = input.mid(stringIndex, HEX_BYTE_SIZE).toUInt(&ok, HEX_NUMBER_BASE); - if (!ok) { - break; - } - bytes[byteArrayIndex] = (unsigned char)value; - stringIndex += HEX_BYTE_SIZE; - byteArrayIndex++; - } - - // something went wrong - if (!ok) { - delete[] bytes; - return NULL; - } - return bytes; -} - -QString JurisdictionMap::octalCodeToHexString(unsigned char* octalCode) const { - const int HEX_NUMBER_BASE = 16; - const int HEX_BYTE_SIZE = 2; - QString output; - if (!octalCode) { - output = "00"; - } else { - for (int i = 0; i < bytesRequiredForCodeLength(*octalCode); i++) { - output.append(QString("%1").arg(octalCode[i], HEX_BYTE_SIZE, HEX_NUMBER_BASE, QChar('0')).toUpper()); - } - } - return output; -} diff --git a/libraries/voxels/src/JurisdictionMap.h b/libraries/voxels/src/JurisdictionMap.h index d54f5147d0..ff05c59584 100644 --- a/libraries/voxels/src/JurisdictionMap.h +++ b/libraries/voxels/src/JurisdictionMap.h @@ -43,15 +43,14 @@ public: unsigned char* getRootOctalCode() const { return _rootOctalCode; } unsigned char* getEndNodeOctalCode(int index) const { return _endNodes[index]; } int getEndNodeCount() const { return _endNodes.size(); } + + void copyContents(unsigned char* rootCodeIn, const std::vector endNodesIn); private: - void copyContents(const JurisdictionMap& other); + void copyContents(const JurisdictionMap& other); // use assignment instead void clear(); void init(unsigned char* rootOctalCode, const std::vector& endNodes); - unsigned char* hexStringToOctalCode(const QString& input) const; - QString octalCodeToHexString(unsigned char* octalCode) const; - unsigned char* _rootOctalCode; std::vector _endNodes; }; From b05b43f0270f20e390089e3eaec88f35942626c9 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 13 Aug 2013 12:27:52 -0700 Subject: [PATCH 11/41] Render to texture first, rather than copying from the frame buffer. Copying from the frame buffer requires it to have an alpha channel, which actually does something on OS X (meaning we have to set it to 1.0). We're going to want to render to texture anyway for SSAO (or other effects). --- .../resources/shaders/horizontal_blur.frag | 36 ++++----- .../resources/shaders/vertical_blur.frag | 40 +++++----- interface/src/Application.cpp | 5 +- interface/src/renderer/GlowEffect.cpp | 78 ++++++++++++------- interface/src/renderer/TextureCache.cpp | 3 +- 5 files changed, 93 insertions(+), 69 deletions(-) diff --git a/interface/resources/shaders/horizontal_blur.frag b/interface/resources/shaders/horizontal_blur.frag index b1b40c2187..2f31849815 100644 --- a/interface/resources/shaders/horizontal_blur.frag +++ b/interface/resources/shaders/horizontal_blur.frag @@ -8,25 +8,25 @@ // Copyright (c) 2013 High Fidelity, Inc. All rights reserved. // -// the texture containing the color to blur -uniform sampler2D colorTexture; +// the texture containing the original color +uniform sampler2D originalTexture; void main(void) { float ds = dFdx(gl_TexCoord[0].s); - gl_FragColor = (texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * -15.5, 0.0)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * -13.5, 0.0)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * -11.5, 0.0)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * -9.5, 0.0)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * -7.5, 0.0)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * -5.5, 0.0)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * -3.5, 0.0)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * -1.5, 0.0)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * 1.5, 0.0)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * 3.5, 0.0)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * 5.5, 0.0)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * 7.5, 0.0)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * 9.5, 0.0)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * 11.5, 0.0)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * 13.5, 0.0)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(ds * 15.5, 0.0))) / 16.0; + gl_FragColor = (texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -15.5, 0.0)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -13.5, 0.0)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -11.5, 0.0)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -9.5, 0.0)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -7.5, 0.0)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -5.5, 0.0)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -3.5, 0.0)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -1.5, 0.0)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 1.5, 0.0)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 3.5, 0.0)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 5.5, 0.0)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 7.5, 0.0)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 9.5, 0.0)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 11.5, 0.0)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 13.5, 0.0)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 15.5, 0.0))) / 16.0; } diff --git a/interface/resources/shaders/vertical_blur.frag b/interface/resources/shaders/vertical_blur.frag index 0641c773e7..5d136a29c9 100644 --- a/interface/resources/shaders/vertical_blur.frag +++ b/interface/resources/shaders/vertical_blur.frag @@ -8,25 +8,29 @@ // Copyright (c) 2013 High Fidelity, Inc. All rights reserved. // -// the texture containing the color to blur -uniform sampler2D colorTexture; +// the texture containing the original color +uniform sampler2D originalTexture; + +// the texture containing the horizontally blurred color +uniform sampler2D horizontallyBlurredTexture; void main(void) { float dt = dFdy(gl_TexCoord[0].t); - gl_FragColor = (texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * -15.5)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * -13.5)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * -11.5)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * -9.5)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * -7.5)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * -4.5)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * -3.5)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * -1.5)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * 1.5)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * 3.5)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * 5.5)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * 7.5)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * 9.5)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * 11.5)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * 13.5)) + - texture2D(colorTexture, gl_TexCoord[0].st + vec2(0.0, dt * 15.5))) / 16.0; + vec4 blurred = (texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -15.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -13.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -11.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -9.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -7.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -4.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -3.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -1.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 1.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 3.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 5.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 7.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 9.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 11.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 13.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 15.5))) / 16.0; + gl_FragColor = blurred * blurred.a + texture2D(originalTexture, gl_TexCoord[0].st); } diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 9227b7be11..fbab11fe83 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -116,7 +116,7 @@ protected: virtual void wheelEvent(QWheelEvent* event); }; -GLCanvas::GLCanvas() : QGLWidget(QGLFormat(QGL::AlphaChannel)) { +GLCanvas::GLCanvas() : QGLWidget(QGLFormat(QGL::NoDepthBuffer, QGL::NoStencilBuffer)) { } void GLCanvas::initializeGL() { @@ -418,8 +418,7 @@ void Application::paintGL() { PerfStat("display"); glEnable(GL_LINE_SMOOTH); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - + if (_myCamera.getMode() == CAMERA_MODE_MIRROR) { _myCamera.setTightness (100.0f); _myCamera.setTargetPosition(_myAvatar.getUprightHeadPosition()); diff --git a/interface/src/renderer/GlowEffect.cpp b/interface/src/renderer/GlowEffect.cpp index 89aff160ff..11c73dd12d 100644 --- a/interface/src/renderer/GlowEffect.cpp +++ b/interface/src/renderer/GlowEffect.cpp @@ -20,7 +20,7 @@ static ProgramObject* createBlurProgram(const QString& direction) { program->link(); program->bind(); - program->setUniformValue("colorTexture", 0); + program->setUniformValue("originalTexture", 0); program->release(); return program; @@ -30,9 +30,16 @@ void GlowEffect::init() { switchToResourcesParentIfRequired(); _horizontalBlurProgram = createBlurProgram("horizontal"); _verticalBlurProgram = createBlurProgram("vertical"); + + _verticalBlurProgram->bind(); + _verticalBlurProgram->setUniformValue("horizontallyBlurredTexture", 1); + _verticalBlurProgram->release(); } void GlowEffect::prepare() { + Application::getInstance()->getTextureCache()->getPrimaryFramebufferObject()->bind(); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + _isEmpty = true; } @@ -59,15 +66,10 @@ static void renderFullscreenQuad() { } void GlowEffect::render() { - if (_isEmpty) { - return; // nothing glowing - } QOpenGLFramebufferObject* primaryFBO = Application::getInstance()->getTextureCache()->getPrimaryFramebufferObject(); + primaryFBO->release(); glBindTexture(GL_TEXTURE_2D, primaryFBO->texture()); - QSize size = primaryFBO->size(); - glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, size.width(), size.height()); - glPushMatrix(); glLoadIdentity(); @@ -76,33 +78,51 @@ void GlowEffect::render() { glLoadIdentity(); glDisable(GL_BLEND); + glDisable(GL_DEPTH_TEST); - // render the primary to the secondary with the horizontal blur - QOpenGLFramebufferObject* secondaryFBO = Application::getInstance()->getTextureCache()->getSecondaryFramebufferObject(); - secondaryFBO->bind(); - - _horizontalBlurProgram->bind(); - renderFullscreenQuad(); - _horizontalBlurProgram->release(); - - secondaryFBO->release(); - - // render the secondary to the screen with the vertical blur - glBindTexture(GL_TEXTURE_2D, secondaryFBO->texture()); - - glEnable(GL_BLEND); - glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_CONSTANT_ALPHA, GL_ONE); - - _verticalBlurProgram->bind(); - renderFullscreenQuad(); - _verticalBlurProgram->release(); + if (_isEmpty) { + // copy the primary to the screen + if (QOpenGLFramebufferObject::hasOpenGLFramebufferBlit()) { + QOpenGLFramebufferObject::blitFramebuffer(NULL, primaryFBO); + + } else { + glEnable(GL_TEXTURE_2D); + glDisable(GL_LIGHTING); + glColor3f(1.0f, 1.0f, 1.0f); + renderFullscreenQuad(); + glDisable(GL_TEXTURE_2D); + glEnable(GL_LIGHTING); + } + } else { + // render the primary to the secondary with the horizontal blur + QOpenGLFramebufferObject* secondaryFBO = + Application::getInstance()->getTextureCache()->getSecondaryFramebufferObject(); + secondaryFBO->bind(); + + _horizontalBlurProgram->bind(); + renderFullscreenQuad(); + _horizontalBlurProgram->release(); + + secondaryFBO->release(); + + // render the secondary to the screen with the vertical blur + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, secondaryFBO->texture()); + + _verticalBlurProgram->bind(); + renderFullscreenQuad(); + _verticalBlurProgram->release(); + + glBindTexture(GL_TEXTURE_2D, 0); + glActiveTexture(GL_TEXTURE0); + } glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); - + + glEnable(GL_BLEND); + glEnable(GL_DEPTH_TEST); glBindTexture(GL_TEXTURE_2D, 0); - - glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE); } diff --git a/interface/src/renderer/TextureCache.cpp b/interface/src/renderer/TextureCache.cpp index 98cfebcee0..8ff1673d18 100644 --- a/interface/src/renderer/TextureCache.cpp +++ b/interface/src/renderer/TextureCache.cpp @@ -57,7 +57,8 @@ GLuint TextureCache::getPermutationNormalTextureID() { QOpenGLFramebufferObject* TextureCache::getPrimaryFramebufferObject() { if (_primaryFramebufferObject == NULL) { - _primaryFramebufferObject = new QOpenGLFramebufferObject(Application::getInstance()->getGLWidget()->size()); + _primaryFramebufferObject = new QOpenGLFramebufferObject(Application::getInstance()->getGLWidget()->size(), + QOpenGLFramebufferObject::Depth); Application::getInstance()->getGLWidget()->installEventFilter(this); } return _primaryFramebufferObject; From f6209b702c46914489c730fb68fff15fbf9180bd Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 13 Aug 2013 13:16:59 -0700 Subject: [PATCH 12/41] Use glow effect for follow indicator. --- interface/src/Application.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index fbab11fe83..af103d4c28 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2337,6 +2337,8 @@ void Application::renderLookatIndicator(glm::vec3 pointOfInterest, Camera& which void Application::renderFollowIndicator() { NodeList* nodeList = NodeList::getInstance(); + _glowEffect.begin(); + glLineWidth(5); glBegin(GL_LINES); for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); ++node) { @@ -2382,6 +2384,8 @@ void Application::renderFollowIndicator() { } glEnd(); + + _glowEffect.end(); } void Application::update(float deltaTime) { @@ -3158,9 +3162,7 @@ void Application::displaySide(Camera& whichCamera) { if (_myCamera.getMode() == CAMERA_MODE_MIRROR) { _myAvatar.getHead().setLookAtPosition(_myCamera.getPosition()); } - _glowEffect.begin(); _myAvatar.render(_lookingInMirror->isChecked(), _renderAvatarBalls->isChecked()); - _glowEffect.end(); _myAvatar.setDisplayingLookatVectors(_renderLookatOn->isChecked()); From 27d001a84dea4ff2f2039f4a6ef5bab4802b6a83 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 13 Aug 2013 13:29:55 -0700 Subject: [PATCH 13/41] copy the jurisdication end nodes into voxel scene stats --- libraries/voxels/src/VoxelSceneStats.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/libraries/voxels/src/VoxelSceneStats.cpp b/libraries/voxels/src/VoxelSceneStats.cpp index d525725371..d199caf9ef 100644 --- a/libraries/voxels/src/VoxelSceneStats.cpp +++ b/libraries/voxels/src/VoxelSceneStats.cpp @@ -29,9 +29,13 @@ VoxelSceneStats::~VoxelSceneStats() { if (_jurisdictionRoot) { delete[] _jurisdictionRoot; } + for (int i=0; i < _jurisdictionEndNodes.size(); i++) { + if (_jurisdictionEndNodes[i]) { + delete[] _jurisdictionEndNodes[i]; + } + } } - void VoxelSceneStats::sceneStarted(bool isFullScene, bool isMoving, VoxelNode* root, JurisdictionMap* jurisdictionMap) { reset(); // resets packet and voxel stats _isStarted = true; @@ -54,6 +58,16 @@ void VoxelSceneStats::sceneStarted(bool isFullScene, bool isMoving, VoxelNode* r _jurisdictionRoot = new unsigned char[bytes]; memcpy(_jurisdictionRoot, jurisdictionRoot, bytes); } + + for (int i=0; i < jurisdictionMap->getEndNodeCount(); i++) { + unsigned char* endNodeCode = jurisdictionMap->getEndNodeOctalCode(i); + if (endNodeCode) { + int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(endNodeCode)); + unsigned char* endNodeCodeCopy = new unsigned char[bytes]; + memcpy(endNodeCodeCopy, endNodeCode, bytes); + _jurisdictionEndNodes.push_back(endNodeCodeCopy); + } + } } } From 02f2de6101f5f3f49b1be97c51b61b3f78bf5e4f Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 13 Aug 2013 13:43:46 -0700 Subject: [PATCH 14/41] properly send full jurisdiction details in scene stats --- libraries/shared/src/PacketHeaders.cpp | 2 +- libraries/voxels/src/VoxelSceneStats.cpp | 54 +++++++++++++++++++----- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/libraries/shared/src/PacketHeaders.cpp b/libraries/shared/src/PacketHeaders.cpp index e8a459d633..5a1c3f9b4c 100644 --- a/libraries/shared/src/PacketHeaders.cpp +++ b/libraries/shared/src/PacketHeaders.cpp @@ -26,7 +26,7 @@ PACKET_VERSION versionForPacketType(PACKET_TYPE type) { return 1; case PACKET_TYPE_VOXEL_STATS: - return 1; + return 2; default: return 0; } diff --git a/libraries/voxels/src/VoxelSceneStats.cpp b/libraries/voxels/src/VoxelSceneStats.cpp index d199caf9ef..47e0beafba 100644 --- a/libraries/voxels/src/VoxelSceneStats.cpp +++ b/libraries/voxels/src/VoxelSceneStats.cpp @@ -17,23 +17,16 @@ const int samples = 100; VoxelSceneStats::VoxelSceneStats() : _elapsedAverage(samples), - _bitsPerVoxelAverage(samples) + _bitsPerVoxelAverage(samples), + _jurisdictionRoot(NULL) { reset(); _isReadyToSend = false; _isStarted = false; - _jurisdictionRoot = NULL; } VoxelSceneStats::~VoxelSceneStats() { - if (_jurisdictionRoot) { - delete[] _jurisdictionRoot; - } - for (int i=0; i < _jurisdictionEndNodes.size(); i++) { - if (_jurisdictionEndNodes[i]) { - delete[] _jurisdictionEndNodes[i]; - } - } + reset(); } void VoxelSceneStats::sceneStarted(bool isFullScene, bool isMoving, VoxelNode* root, JurisdictionMap* jurisdictionMap) { @@ -139,6 +132,17 @@ void VoxelSceneStats::reset() { _existsBitsWritten = 0; _existsInPacketBitsWritten = 0; _treesRemoved = 0; + + if (_jurisdictionRoot) { + delete[] _jurisdictionRoot; + _jurisdictionRoot = NULL; + } + for (int i=0; i < _jurisdictionEndNodes.size(); i++) { + if (_jurisdictionEndNodes[i]) { + delete[] _jurisdictionEndNodes[i]; + } + } + _jurisdictionEndNodes.clear(); } void VoxelSceneStats::packetSent(int bytes) { @@ -317,6 +321,21 @@ int VoxelSceneStats::packIntoMessage(unsigned char* destinationBuffer, int avail destinationBuffer += sizeof(bytes); memcpy(destinationBuffer, _jurisdictionRoot, bytes); destinationBuffer += bytes; + + // if and only if there's a root jurisdiction, also include the end nodes + int endNodeCount = _jurisdictionEndNodes.size(); + + memcpy(destinationBuffer, &endNodeCount, sizeof(endNodeCount)); + destinationBuffer += sizeof(endNodeCount); + + for (int i=0; i < endNodeCount; i++) { + unsigned char* endNodeCode = _jurisdictionEndNodes[i]; + int bytes = bytesRequiredForCodeLength(numberOfThreeBitSectionsInCode(endNodeCode)); + memcpy(destinationBuffer, &bytes, sizeof(bytes)); + destinationBuffer += sizeof(bytes); + memcpy(destinationBuffer, endNodeCode, bytes); + destinationBuffer += bytes; + } } else { int bytes = 0; memcpy(destinationBuffer, &bytes, sizeof(bytes)); @@ -424,6 +443,21 @@ int VoxelSceneStats::unpackFromMessage(unsigned char* sourceBuffer, int availabl _jurisdictionRoot = new unsigned char[bytes]; memcpy(_jurisdictionRoot, sourceBuffer, bytes); sourceBuffer += bytes; + + // if and only if there's a root jurisdiction, also include the end nodes + int endNodeCount = 0; + memcpy(&endNodeCount, sourceBuffer, sizeof(endNodeCount)); + sourceBuffer += sizeof(endNodeCount); + + for (int i=0; i < endNodeCount; i++) { + int bytes = 0; + memcpy(&bytes, sourceBuffer, sizeof(bytes)); + sourceBuffer += sizeof(bytes); + unsigned char* endNodeCode = new unsigned char[bytes]; + memcpy(endNodeCode, sourceBuffer, bytes); + sourceBuffer += bytes; + _jurisdictionEndNodes.push_back(endNodeCode); + } } // running averages From 01cd0d2a1f34a9884c5bad72e061e1c6a1bd15a0 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 13 Aug 2013 14:22:29 -0700 Subject: [PATCH 15/41] Have the glow effect add half of the original texture, too, and use it on the follow indicator. --- .../resources/shaders/vertical_blur.frag | 2 +- interface/src/Application.cpp | 23 ++++++++++++++----- interface/src/Application.h | 1 + interface/src/VoxelFade.cpp | 8 ++++++- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/interface/resources/shaders/vertical_blur.frag b/interface/resources/shaders/vertical_blur.frag index 5d136a29c9..012389622e 100644 --- a/interface/resources/shaders/vertical_blur.frag +++ b/interface/resources/shaders/vertical_blur.frag @@ -32,5 +32,5 @@ void main(void) { texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 11.5)) + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 13.5)) + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 15.5))) / 16.0; - gl_FragColor = blurred * blurred.a + texture2D(originalTexture, gl_TexCoord[0].st); + gl_FragColor = blurred * blurred.a + texture2D(originalTexture, gl_TexCoord[0].st) * (1.0 + blurred.a * 0.5); } diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index af103d4c28..7fae6ffbf9 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2334,13 +2334,21 @@ void Application::renderLookatIndicator(glm::vec3 pointOfInterest, Camera& which renderCircle(haloOrigin, INDICATOR_RADIUS, IDENTITY_UP, NUM_SEGMENTS); } +void maybeBeginFollowIndicator(bool& began) { + if (!began) { + Application::getInstance()->getGlowEffect()->begin(); + glLineWidth(5); + glBegin(GL_LINES); + began = true; + } +} + void Application::renderFollowIndicator() { NodeList* nodeList = NodeList::getInstance(); - _glowEffect.begin(); + // initialize lazily so that we don't enable the glow effect unnecessarily + bool began = false; - glLineWidth(5); - glBegin(GL_LINES); for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); ++node) { if (node->getLinkedData() != NULL && node->getType() == NODE_TYPE_AGENT) { Avatar* avatar = (Avatar *) node->getLinkedData(); @@ -2359,6 +2367,7 @@ void Application::renderFollowIndicator() { } if (leader != NULL) { + maybeBeginFollowIndicator(began); glColor3f(1.f, 0.f, 0.f); glVertex3f((avatar->getHead().getPosition().x + avatar->getPosition().x) / 2.f, (avatar->getHead().getPosition().y + avatar->getPosition().y) / 2.f, @@ -2373,6 +2382,7 @@ void Application::renderFollowIndicator() { } if (_myAvatar.getLeadingAvatar() != NULL) { + maybeBeginFollowIndicator(began); glColor3f(1.f, 0.f, 0.f); glVertex3f((_myAvatar.getHead().getPosition().x + _myAvatar.getPosition().x) / 2.f, (_myAvatar.getHead().getPosition().y + _myAvatar.getPosition().y) / 2.f, @@ -2383,9 +2393,10 @@ void Application::renderFollowIndicator() { (_myAvatar.getLeadingAvatar()->getHead().getPosition().z + _myAvatar.getLeadingAvatar()->getPosition().z) / 2.f); } - glEnd(); - - _glowEffect.end(); + if (began) { + glEnd(); + _glowEffect.end(); + } } void Application::update(float deltaTime) { diff --git a/interface/src/Application.h b/interface/src/Application.h index bbd1b3ed7b..862207302b 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -119,6 +119,7 @@ public: QNetworkAccessManager* getNetworkAccessManager() { return _networkAccessManager; } GeometryCache* getGeometryCache() { return &_geometryCache; } TextureCache* getTextureCache() { return &_textureCache; } + GlowEffect* getGlowEffect() { return &_glowEffect; } void resetSongMixMenuItem(); void setupWorldLight(Camera& whichCamera); diff --git a/interface/src/VoxelFade.cpp b/interface/src/VoxelFade.cpp index f7ec97ae30..8ce68c9724 100644 --- a/interface/src/VoxelFade.cpp +++ b/interface/src/VoxelFade.cpp @@ -10,6 +10,7 @@ #include +#include "Application.h" #include "VoxelFade.h" const float VoxelFade::FADE_OUT_START = 0.5f; @@ -32,6 +33,8 @@ VoxelFade::VoxelFade(FadeDirection direction, float red, float green, float blue } void VoxelFade::render() { + Application::getInstance()->getGlowEffect()->begin(); + glDisable(GL_LIGHTING); glPushMatrix(); glScalef(TREE_SCALE, TREE_SCALE, TREE_SCALE); @@ -45,6 +48,9 @@ void VoxelFade::render() { glPopMatrix(); glEnable(GL_LIGHTING); + + Application::getInstance()->getGlowEffect()->end(); + opacity *= (direction == FADE_OUT) ? FADE_OUT_STEP : FADE_IN_STEP; } @@ -55,4 +61,4 @@ bool VoxelFade::isDone() const { return opacity >= FADE_IN_END; } return true; // unexpected case, assume we're done -} \ No newline at end of file +} From 48bee0e52c05e2845d596ba804ce2c88c081bf1b Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 13 Aug 2013 14:29:49 -0700 Subject: [PATCH 16/41] Gonna access that vector from two threads? Need to lock it. --- interface/src/Application.cpp | 4 ++++ interface/src/Application.h | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 174dfc2527..f2e75ddb63 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -4045,11 +4045,13 @@ void* Application::processVoxels(void* args) { app->_wantToKillLocalVoxels = false; } + app->_voxelPacketMutex.lock(); while (app->_voxelPackets.size() > 0) { NetworkPacket& packet = app->_voxelPackets.front(); app->processVoxelPacket(packet.getSenderAddress(), packet.getData(), packet.getLength()); app->_voxelPackets.erase(app->_voxelPackets.begin()); } + app->_voxelPacketMutex.unlock(); if (!app->_enableProcessVoxelsThread) { break; @@ -4063,7 +4065,9 @@ void* Application::processVoxels(void* args) { } void Application::queueVoxelPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { + _voxelPacketMutex.lock(); _voxelPackets.push_back(NetworkPacket(senderAddress, packetData, packetLength)); + _voxelPacketMutex.unlock(); } void Application::processVoxelPacket(sockaddr& senderAddress, unsigned char* packetData, ssize_t packetLength) { diff --git a/interface/src/Application.h b/interface/src/Application.h index 29f7e8bea7..6ef5ef4c0a 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -460,7 +460,7 @@ private: pthread_t _processVoxelsThread; bool _stopProcessVoxelsThread; std::vector _voxelPackets; - + QMutex _voxelPacketMutex; unsigned char _incomingPacket[MAX_PACKET_SIZE]; int _packetCount; From d24e340c913f0ed621d81ac5dcd3afcb4b9eca19 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Tue, 13 Aug 2013 14:52:35 -0700 Subject: [PATCH 17/41] To keep the frame rate up, let's only sample eight points for the blur. --- interface/resources/shaders/horizontal_blur.frag | 12 ++---------- interface/resources/shaders/vertical_blur.frag | 14 +++----------- 2 files changed, 5 insertions(+), 21 deletions(-) diff --git a/interface/resources/shaders/horizontal_blur.frag b/interface/resources/shaders/horizontal_blur.frag index 2f31849815..695de1a538 100644 --- a/interface/resources/shaders/horizontal_blur.frag +++ b/interface/resources/shaders/horizontal_blur.frag @@ -13,20 +13,12 @@ uniform sampler2D originalTexture; void main(void) { float ds = dFdx(gl_TexCoord[0].s); - gl_FragColor = (texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -15.5, 0.0)) + - texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -13.5, 0.0)) + - texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -11.5, 0.0)) + - texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -9.5, 0.0)) + - texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -7.5, 0.0)) + + gl_FragColor = (texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -7.5, 0.0)) + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -5.5, 0.0)) + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -3.5, 0.0)) + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * -1.5, 0.0)) + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 1.5, 0.0)) + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 3.5, 0.0)) + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 5.5, 0.0)) + - texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 7.5, 0.0)) + - texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 9.5, 0.0)) + - texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 11.5, 0.0)) + - texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 13.5, 0.0)) + - texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 15.5, 0.0))) / 16.0; + texture2D(originalTexture, gl_TexCoord[0].st + vec2(ds * 7.5, 0.0))) / 8.0; } diff --git a/interface/resources/shaders/vertical_blur.frag b/interface/resources/shaders/vertical_blur.frag index 012389622e..6e49c5b553 100644 --- a/interface/resources/shaders/vertical_blur.frag +++ b/interface/resources/shaders/vertical_blur.frag @@ -16,21 +16,13 @@ uniform sampler2D horizontallyBlurredTexture; void main(void) { float dt = dFdy(gl_TexCoord[0].t); - vec4 blurred = (texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -15.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -13.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -11.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -9.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -7.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -4.5)) + + vec4 blurred = (texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -7.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -5.5)) + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -3.5)) + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -1.5)) + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 1.5)) + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 3.5)) + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 5.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 7.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 9.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 11.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 13.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 15.5))) / 16.0; + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 7.5))) / 8.0; gl_FragColor = blurred * blurred.a + texture2D(originalTexture, gl_TexCoord[0].st) * (1.0 + blurred.a * 0.5); } From 4477289501524bbbf983527715964c1a3916c2fd Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 14 Aug 2013 13:18:41 -0700 Subject: [PATCH 18/41] repair bad merge --- interface/src/Application.cpp | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 0b68887725..e0f8f87dbf 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -22,6 +22,9 @@ #include #include +// include this before QGLWidget, which includes an earlier version of OpenGL +#include "InterfaceConfig.h" + #include #include #include @@ -92,6 +95,8 @@ const int STARTUP_JITTER_SAMPLES = PACKET_LENGTH_SAMPLES_PER_CHANNEL / 2; // customized canvas that simply forwards requests/events to the singleton application class GLCanvas : public QGLWidget { +public: + GLCanvas(); protected: virtual void initializeGL(); @@ -110,6 +115,9 @@ protected: virtual void wheelEvent(QWheelEvent* event); }; +GLCanvas::GLCanvas() : QGLWidget(QGLFormat(QGL::NoDepthBuffer, QGL::NoStencilBuffer)) { +} + void GLCanvas::initializeGL() { Application::getInstance()->initializeGL(); setAttribute(Qt::WA_AcceptTouchEvents); @@ -2103,8 +2111,8 @@ void Application::runTests() { void Application::initDisplay() { glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glShadeModel (GL_SMOOTH); + glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE); + glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); @@ -2114,6 +2122,8 @@ void Application::init() { _voxels.init(); _environment.init(); + + _glowEffect.init(); _handControl.setScreenDimensions(_glWidget->width(), _glWidget->height()); @@ -2214,11 +2224,21 @@ void Application::renderLookatIndicator(glm::vec3 pointOfInterest, Camera& which renderCircle(haloOrigin, INDICATOR_RADIUS, IDENTITY_UP, NUM_SEGMENTS); } +void maybeBeginFollowIndicator(bool& began) { + if (!began) { + Application::getInstance()->getGlowEffect()->begin(); + glLineWidth(5); + glBegin(GL_LINES); + began = true; + } +} + void Application::renderFollowIndicator() { NodeList* nodeList = NodeList::getInstance(); - glLineWidth(5); - glBegin(GL_LINES); + // initialize lazily so that we don't enable the glow effect unnecessarily + bool began = false; + for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); ++node) { if (node->getLinkedData() != NULL && node->getType() == NODE_TYPE_AGENT) { Avatar* avatar = (Avatar *) node->getLinkedData(); @@ -2237,6 +2257,7 @@ void Application::renderFollowIndicator() { } if (leader != NULL) { + maybeBeginFollowIndicator(began); glColor3f(1.f, 0.f, 0.f); glVertex3f((avatar->getHead().getPosition().x + avatar->getPosition().x) / 2.f, (avatar->getHead().getPosition().y + avatar->getPosition().y) / 2.f, @@ -2251,6 +2272,7 @@ void Application::renderFollowIndicator() { } if (_myAvatar.getLeadingAvatar() != NULL) { + maybeBeginFollowIndicator(began); glColor3f(1.f, 0.f, 0.f); glVertex3f((_myAvatar.getHead().getPosition().x + _myAvatar.getPosition().x) / 2.f, (_myAvatar.getHead().getPosition().y + _myAvatar.getPosition().y) / 2.f, @@ -2261,7 +2283,10 @@ void Application::renderFollowIndicator() { (_myAvatar.getLeadingAvatar()->getHead().getPosition().z + _myAvatar.getLeadingAvatar()->getPosition().z) / 2.f); } - glEnd(); + if (began) { + glEnd(); + _glowEffect.end(); + } } void Application::update(float deltaTime) { From a43615e9dc16e13bd3f68a276fcbf71aa82b5b22 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 14 Aug 2013 13:20:22 -0700 Subject: [PATCH 19/41] repair bad merge --- interface/src/Application.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index e0f8f87dbf..828fd75a78 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2914,6 +2914,9 @@ void Application::displaySide(Camera& whichCamera) { // Setup 3D lights (after the camera transform, so that they are positioned in world space) setupWorldLight(whichCamera); + // prepare the glow effect + _glowEffect.prepare(); + if (_renderStarsOn->isChecked()) { if (!_stars.getFileLoaded()) { _stars.readInput(STAR_FILE, STAR_CACHE_FILE, 0); @@ -3042,6 +3045,7 @@ void Application::displaySide(Camera& whichCamera) { _myAvatar.getHead().setLookAtPosition(_myCamera.getPosition()); } _myAvatar.render(_lookingInMirror->isChecked(), _renderAvatarBalls->isChecked()); + _myAvatar.setDisplayingLookatVectors(_renderLookatOn->isChecked()); if (_renderLookatIndicatorOn->isChecked() && _isLookingAtOtherAvatar) { @@ -3076,6 +3080,9 @@ void Application::displaySide(Camera& whichCamera) { } renderFollowIndicator(); + + // render the glow effect + _glowEffect.render(); } void Application::displayOverlay() { From 15f129f32d3b4ac3d833104e6595b4137fca658b Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Wed, 14 Aug 2013 14:14:47 -0700 Subject: [PATCH 20/41] Added simple additive/blur-with-persist glow modes, means to cycle through modes. --- interface/resources/shaders/glow_add.frag | 17 ++++ .../resources/shaders/glow_add_separate.frag | 20 +++++ .../resources/shaders/vertical_blur.frag | 24 +++--- .../resources/shaders/vertical_blur_add.frag | 28 +++++++ interface/src/Application.cpp | 5 ++ interface/src/renderer/GlowEffect.cpp | 83 +++++++++++++++---- interface/src/renderer/GlowEffect.h | 18 +++- interface/src/renderer/TextureCache.cpp | 17 +++- interface/src/renderer/TextureCache.h | 4 +- 9 files changed, 185 insertions(+), 31 deletions(-) create mode 100644 interface/resources/shaders/glow_add.frag create mode 100644 interface/resources/shaders/glow_add_separate.frag create mode 100644 interface/resources/shaders/vertical_blur_add.frag diff --git a/interface/resources/shaders/glow_add.frag b/interface/resources/shaders/glow_add.frag new file mode 100644 index 0000000000..0947292109 --- /dev/null +++ b/interface/resources/shaders/glow_add.frag @@ -0,0 +1,17 @@ +#version 120 + +// +// glow_add.frag +// fragment shader +// +// Created by Andrzej Kapolka on 8/14/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// + +// the texture containing the original color +uniform sampler2D originalTexture; + +void main(void) { + vec4 color = texture2D(originalTexture, gl_TexCoord[0].st); + gl_FragColor = color * (1.0 + color.a); +} diff --git a/interface/resources/shaders/glow_add_separate.frag b/interface/resources/shaders/glow_add_separate.frag new file mode 100644 index 0000000000..7b7f538a03 --- /dev/null +++ b/interface/resources/shaders/glow_add_separate.frag @@ -0,0 +1,20 @@ +#version 120 + +// +// glow_add_separate.frag +// fragment shader +// +// Created by Andrzej Kapolka on 8/14/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// + +// the texture containing the original color +uniform sampler2D originalTexture; + +// the texture containing the blurred color +uniform sampler2D blurredTexture; + +void main(void) { + vec4 blurred = texture2D(blurredTexture, gl_TexCoord[0].st); + gl_FragColor = blurred * blurred.a + texture2D(originalTexture, gl_TexCoord[0].st) * (1.0 + blurred.a * 0.5); +} diff --git a/interface/resources/shaders/vertical_blur.frag b/interface/resources/shaders/vertical_blur.frag index 6e49c5b553..96ab95ea9e 100644 --- a/interface/resources/shaders/vertical_blur.frag +++ b/interface/resources/shaders/vertical_blur.frag @@ -4,25 +4,21 @@ // vertical_blur.frag // fragment shader // -// Created by Andrzej Kapolka on 8/8/13. +// Created by Andrzej Kapolka on 8/14/13. // Copyright (c) 2013 High Fidelity, Inc. All rights reserved. // -// the texture containing the original color -uniform sampler2D originalTexture; - // the texture containing the horizontally blurred color -uniform sampler2D horizontallyBlurredTexture; +uniform sampler2D originalTexture; void main(void) { float dt = dFdy(gl_TexCoord[0].t); - vec4 blurred = (texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -7.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -5.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -3.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -1.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 1.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 3.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 5.5)) + - texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 7.5))) / 8.0; - gl_FragColor = blurred * blurred.a + texture2D(originalTexture, gl_TexCoord[0].st) * (1.0 + blurred.a * 0.5); + gl_FragColor = (texture2D(originalTexture, gl_TexCoord[0].st + vec2(0.0, dt * -7.5)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(0.0, dt * -5.5)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(0.0, dt * -3.5)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(0.0, dt * -1.5)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(0.0, dt * 1.5)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(0.0, dt * 3.5)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(0.0, dt * 5.5)) + + texture2D(originalTexture, gl_TexCoord[0].st + vec2(0.0, dt * 7.5))) / 8.0; } diff --git a/interface/resources/shaders/vertical_blur_add.frag b/interface/resources/shaders/vertical_blur_add.frag new file mode 100644 index 0000000000..5cda2622b4 --- /dev/null +++ b/interface/resources/shaders/vertical_blur_add.frag @@ -0,0 +1,28 @@ +#version 120 + +// +// vertical_blur_add.frag +// fragment shader +// +// Created by Andrzej Kapolka on 8/8/13. +// Copyright (c) 2013 High Fidelity, Inc. All rights reserved. +// + +// the texture containing the original color +uniform sampler2D originalTexture; + +// the texture containing the horizontally blurred color +uniform sampler2D horizontallyBlurredTexture; + +void main(void) { + float dt = dFdy(gl_TexCoord[0].t); + vec4 blurred = (texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -7.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -5.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -3.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * -1.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 1.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 3.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 5.5)) + + texture2D(horizontallyBlurredTexture, gl_TexCoord[0].st + vec2(0.0, dt * 7.5))) / 8.0; + gl_FragColor = blurred * blurred.a + texture2D(originalTexture, gl_TexCoord[0].st) * (1.0 + blurred.a * 0.5); +} diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index eebb7ba744..65b2da4c24 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2011,6 +2011,7 @@ void Application::initMenu() { _renderAvatarBalls->setChecked(false); renderMenu->addAction("Cycle Voxel Mode", _myAvatar.getVoxels(), SLOT(cycleMode())); renderMenu->addAction("Cycle Face Mode", &_myAvatar.getHead().getFace(), SLOT(cycleRenderMode())); + renderMenu->addAction("Cycle Glow Mode", &_glowEffect, SLOT(cycleRenderMode())); (_renderFrameTimerOn = renderMenu->addAction("Show Timer"))->setCheckable(true); _renderFrameTimerOn->setChecked(false); (_renderLookatOn = renderMenu->addAction("Lookat Vectors"))->setCheckable(true); @@ -3162,7 +3163,9 @@ void Application::displaySide(Camera& whichCamera) { if (isLookingAtMyAvatar(avatar)) { avatar->getHead().setLookAtPosition(_myCamera.getPosition()); } + _glowEffect.begin(); avatar->render(false, _renderAvatarBalls->isChecked()); + _glowEffect.end(); avatar->setDisplayingLookatVectors(_renderLookatOn->isChecked()); } @@ -3173,7 +3176,9 @@ void Application::displaySide(Camera& whichCamera) { if (_myCamera.getMode() == CAMERA_MODE_MIRROR) { _myAvatar.getHead().setLookAtPosition(_myCamera.getPosition()); } + _glowEffect.begin(); _myAvatar.render(_lookingInMirror->isChecked(), _renderAvatarBalls->isChecked()); + _glowEffect.end(); _myAvatar.setDisplayingLookatVectors(_renderLookatOn->isChecked()); diff --git a/interface/src/renderer/GlowEffect.cpp b/interface/src/renderer/GlowEffect.cpp index 11c73dd12d..8233fe1984 100644 --- a/interface/src/renderer/GlowEffect.cpp +++ b/interface/src/renderer/GlowEffect.cpp @@ -14,9 +14,12 @@ #include "GlowEffect.h" #include "ProgramObject.h" -static ProgramObject* createBlurProgram(const QString& direction) { +GlowEffect::GlowEffect() : _renderMode(BLUR_ADD_MODE) { +} + +static ProgramObject* createProgram(const QString& name) { ProgramObject* program = new ProgramObject(); - program->addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/" + direction + "_blur.frag"); + program->addShaderFromSourceFile(QGLShader::Fragment, "resources/shaders/" + name + ".frag"); program->link(); program->bind(); @@ -28,12 +31,20 @@ static ProgramObject* createBlurProgram(const QString& direction) { void GlowEffect::init() { switchToResourcesParentIfRequired(); - _horizontalBlurProgram = createBlurProgram("horizontal"); - _verticalBlurProgram = createBlurProgram("vertical"); - _verticalBlurProgram->bind(); - _verticalBlurProgram->setUniformValue("horizontallyBlurredTexture", 1); - _verticalBlurProgram->release(); + _addProgram = createProgram("glow_add"); + _horizontalBlurProgram = createProgram("horizontal_blur"); + _verticalBlurAddProgram = createProgram("vertical_blur_add"); + _verticalBlurProgram = createProgram("vertical_blur"); + _addSeparateProgram = createProgram("glow_add_separate"); + + _verticalBlurAddProgram->bind(); + _verticalBlurAddProgram->setUniformValue("horizontallyBlurredTexture", 1); + _verticalBlurAddProgram->release(); + + _addSeparateProgram->bind(); + _addSeparateProgram->setUniformValue("blurredTexture", 1); + _addSeparateProgram->release(); } void GlowEffect::prepare() { @@ -93,7 +104,12 @@ void GlowEffect::render() { glDisable(GL_TEXTURE_2D); glEnable(GL_LIGHTING); } - } else { + } else if (_renderMode == ADD_MODE) { + _addProgram->bind(); + renderFullscreenQuad(); + _addProgram->release(); + + } else { // _renderMode == BLUR_ADD_MODE || _renderMode == BLUR_PERSIST_ADD_MODE // render the primary to the secondary with the horizontal blur QOpenGLFramebufferObject* secondaryFBO = Application::getInstance()->getTextureCache()->getSecondaryFramebufferObject(); @@ -105,13 +121,48 @@ void GlowEffect::render() { secondaryFBO->release(); - // render the secondary to the screen with the vertical blur - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, secondaryFBO->texture()); + if (_renderMode == BLUR_ADD_MODE) { + // render the secondary to the screen with the vertical blur + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, secondaryFBO->texture()); + + _verticalBlurAddProgram->bind(); + renderFullscreenQuad(); + _verticalBlurAddProgram->release(); + + } else { // _renderMode == BLUR_PERSIST_ADD_MODE + // render the secondary to the tertiary with horizontal blur and persistence + QOpenGLFramebufferObject* tertiaryFBO = + Application::getInstance()->getTextureCache()->getTertiaryFramebufferObject(); + tertiaryFBO->bind(); + + glEnable(GL_BLEND); + glBlendFunc(GL_ONE_MINUS_CONSTANT_ALPHA, GL_CONSTANT_ALPHA); + const float PERSISTENCE_SMOOTHING = 0.9f; + glBlendColor(0.0f, 0.0f, 0.0f, PERSISTENCE_SMOOTHING); + + glBindTexture(GL_TEXTURE_2D, secondaryFBO->texture()); + + _verticalBlurProgram->bind(); + renderFullscreenQuad(); + _verticalBlurProgram->release(); - _verticalBlurProgram->bind(); - renderFullscreenQuad(); - _verticalBlurProgram->release(); + glBlendColor(0.0f, 0.0f, 0.0f, 0.0f); + glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE); + glDisable(GL_BLEND); + + // now add the tertiary to the primary buffer + tertiaryFBO->release(); + + glBindTexture(GL_TEXTURE_2D, primaryFBO->texture()); + + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, tertiaryFBO->texture()); + + _addSeparateProgram->bind(); + renderFullscreenQuad(); + _addSeparateProgram->release(); + } glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE0); @@ -126,3 +177,7 @@ void GlowEffect::render() { glEnable(GL_DEPTH_TEST); glBindTexture(GL_TEXTURE_2D, 0); } + +void GlowEffect::cycleRenderMode() { + _renderMode = (RenderMode)((_renderMode + 1) % RENDER_MODE_COUNT); +} diff --git a/interface/src/renderer/GlowEffect.h b/interface/src/renderer/GlowEffect.h index 06ca6e5712..d8ea3d18c2 100644 --- a/interface/src/renderer/GlowEffect.h +++ b/interface/src/renderer/GlowEffect.h @@ -9,11 +9,17 @@ #ifndef __interface__GlowEffect__ #define __interface__GlowEffect__ +#include + class ProgramObject; -class GlowEffect { +class GlowEffect : public QObject { + Q_OBJECT + public: + GlowEffect(); + void init(); void prepare(); @@ -23,10 +29,20 @@ public: void render(); +public slots: + + void cycleRenderMode(); + private: + enum RenderMode { ADD_MODE, BLUR_ADD_MODE, BLUR_PERSIST_ADD_MODE, RENDER_MODE_COUNT }; + + RenderMode _renderMode; + ProgramObject* _addProgram; ProgramObject* _horizontalBlurProgram; + ProgramObject* _verticalBlurAddProgram; ProgramObject* _verticalBlurProgram; + ProgramObject* _addSeparateProgram; bool _isEmpty; }; diff --git a/interface/src/renderer/TextureCache.cpp b/interface/src/renderer/TextureCache.cpp index 8ff1673d18..d4cc97edbf 100644 --- a/interface/src/renderer/TextureCache.cpp +++ b/interface/src/renderer/TextureCache.cpp @@ -14,7 +14,7 @@ #include "TextureCache.h" TextureCache::TextureCache() : _permutationNormalTextureID(0), - _primaryFramebufferObject(NULL), _secondaryFramebufferObject(NULL) { + _primaryFramebufferObject(NULL), _secondaryFramebufferObject(NULL), _tertiaryFramebufferObject(NULL) { } TextureCache::~TextureCache() { @@ -27,6 +27,9 @@ TextureCache::~TextureCache() { if (_secondaryFramebufferObject != NULL) { delete _secondaryFramebufferObject; } + if (_tertiaryFramebufferObject != NULL) { + delete _tertiaryFramebufferObject; + } } GLuint TextureCache::getPermutationNormalTextureID() { @@ -72,6 +75,14 @@ QOpenGLFramebufferObject* TextureCache::getSecondaryFramebufferObject() { return _secondaryFramebufferObject; } +QOpenGLFramebufferObject* TextureCache::getTertiaryFramebufferObject() { + if (_tertiaryFramebufferObject == NULL) { + _tertiaryFramebufferObject = new QOpenGLFramebufferObject(Application::getInstance()->getGLWidget()->size()); + Application::getInstance()->getGLWidget()->installEventFilter(this); + } + return _tertiaryFramebufferObject; +} + bool TextureCache::eventFilter(QObject* watched, QEvent* event) { if (event->type() == QEvent::Resize) { QSize size = static_cast(event)->size(); @@ -83,6 +94,10 @@ bool TextureCache::eventFilter(QObject* watched, QEvent* event) { delete _secondaryFramebufferObject; _secondaryFramebufferObject = NULL; } + if (_tertiaryFramebufferObject != NULL && _tertiaryFramebufferObject->size() != size) { + delete _tertiaryFramebufferObject; + _tertiaryFramebufferObject = NULL; + } } return false; } diff --git a/interface/src/renderer/TextureCache.h b/interface/src/renderer/TextureCache.h index 29920e1f1f..716f087992 100644 --- a/interface/src/renderer/TextureCache.h +++ b/interface/src/renderer/TextureCache.h @@ -25,7 +25,8 @@ public: QOpenGLFramebufferObject* getPrimaryFramebufferObject(); QOpenGLFramebufferObject* getSecondaryFramebufferObject(); - + QOpenGLFramebufferObject* getTertiaryFramebufferObject(); + virtual bool eventFilter(QObject* watched, QEvent* event); private: @@ -34,6 +35,7 @@ private: QOpenGLFramebufferObject* _primaryFramebufferObject; QOpenGLFramebufferObject* _secondaryFramebufferObject; + QOpenGLFramebufferObject* _tertiaryFramebufferObject; }; #endif /* defined(__interface__TextureCache__) */ From 4305ad552d22fccf5695bec8cccea65d3c70a712 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 14 Aug 2013 14:19:06 -0700 Subject: [PATCH 21/41] fix issue with JurisdictionMap being passed across wire --- interface/src/Application.cpp | 2 +- interface/src/VoxelEditPacketSender.cpp | 7 ------- libraries/voxels/src/JurisdictionMap.cpp | 9 +-------- libraries/voxels/src/VoxelSceneStats.cpp | 4 ++-- 4 files changed, 4 insertions(+), 18 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 828fd75a78..2da4abc242 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1586,7 +1586,7 @@ bool Application::sendVoxelsOperation(VoxelNode* node, void* extraData) { codeColorBuffer[bytesInCode + RED_INDEX ] = node->getColor()[RED_INDEX ]; codeColorBuffer[bytesInCode + GREEN_INDEX] = node->getColor()[GREEN_INDEX]; codeColorBuffer[bytesInCode + BLUE_INDEX ] = node->getColor()[BLUE_INDEX ]; - + args->app->_voxelEditSender.queueVoxelEditMessage(PACKET_TYPE_SET_VOXEL_DESTRUCTIVE, codeColorBuffer, codeAndColorLength); delete[] codeColorBuffer; diff --git a/interface/src/VoxelEditPacketSender.cpp b/interface/src/VoxelEditPacketSender.cpp index 5155619b7e..c663977268 100644 --- a/interface/src/VoxelEditPacketSender.cpp +++ b/interface/src/VoxelEditPacketSender.cpp @@ -39,7 +39,6 @@ void VoxelEditPacketSender::sendVoxelEditMessage(PACKET_TYPE type, VoxelDetail& } void VoxelEditPacketSender::actuallySendMessage(uint16_t nodeID, unsigned char* bufferOut, ssize_t sizeOut) { - qDebug("VoxelEditPacketSender::actuallySendMessage() sizeOut=%lu target NodeID=%d\n", sizeOut, nodeID); NodeList* nodeList = NodeList::getInstance(); for (NodeList::iterator node = nodeList->begin(); node != nodeList->end(); node++) { // only send to the NodeTypes that are NODE_TYPE_VOXEL_SERVER @@ -65,13 +64,7 @@ void VoxelEditPacketSender::queueVoxelEditMessage(PACKET_TYPE type, unsigned cha // here we need to get the "pending packet" for this server uint16_t nodeID = node->getNodeID(); const JurisdictionMap& map = _app->_voxelServerJurisdictions[nodeID]; - if (map.isMyJurisdiction(codeColorBuffer, CHECK_NODE_ONLY) == JurisdictionMap::WITHIN) { - - // do I need this??? - //if (_pendingEditPackets.find(nodeID) == _pendingEditPackets.end()) { - // _pendingEditPackets[nodeID] = - //} EditPacketBuffer& packetBuffer = _pendingEditPackets[nodeID]; packetBuffer._nodeID = nodeID; diff --git a/libraries/voxels/src/JurisdictionMap.cpp b/libraries/voxels/src/JurisdictionMap.cpp index a89d12fa6b..5573bdf840 100644 --- a/libraries/voxels/src/JurisdictionMap.cpp +++ b/libraries/voxels/src/JurisdictionMap.cpp @@ -138,7 +138,7 @@ void JurisdictionMap::init(unsigned char* rootOctalCode, const std::vector