From f7ca1f608fd5721fec5980287dc1773685a7fb95 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Mon, 22 Jun 2015 14:06:14 +0200 Subject: [PATCH 001/107] Migrating the deferred lighting effect to Batch and in doing so, introducing the StandardShaderLib to gpu and the first 2 shaders --- libraries/gpu/src/gpu/Batch.cpp | 2 +- libraries/gpu/src/gpu/Config.slh | 4 +- libraries/gpu/src/gpu/DrawTexture.slf | 22 +++++++ .../gpu/src/gpu/DrawTransformUnitQuad.slv | 36 +++++++++++ libraries/gpu/src/gpu/StandardShaderLib.cpp | 35 +++++++++++ libraries/gpu/src/gpu/StandardShaderLib.h | 38 ++++++++++++ libraries/gpu/src/gpu/State.h | 1 + .../src/DeferredLightingEffect.cpp | 59 ++++++++++++++----- .../render-utils/src/DeferredLightingEffect.h | 2 + 9 files changed, 183 insertions(+), 16 deletions(-) create mode 100755 libraries/gpu/src/gpu/DrawTexture.slf create mode 100755 libraries/gpu/src/gpu/DrawTransformUnitQuad.slv create mode 100755 libraries/gpu/src/gpu/StandardShaderLib.cpp create mode 100755 libraries/gpu/src/gpu/StandardShaderLib.h diff --git a/libraries/gpu/src/gpu/Batch.cpp b/libraries/gpu/src/gpu/Batch.cpp index bff99e7ec3..973e5d01d2 100644 --- a/libraries/gpu/src/gpu/Batch.cpp +++ b/libraries/gpu/src/gpu/Batch.cpp @@ -201,7 +201,7 @@ void Batch::setUniformTexture(uint32 slot, const TextureView& view) { } void Batch::setFramebuffer(const FramebufferPointer& framebuffer) { - ADD_COMMAND(setUniformTexture); + ADD_COMMAND(setFramebuffer); _params.push_back(_framebuffers.cache(framebuffer)); diff --git a/libraries/gpu/src/gpu/Config.slh b/libraries/gpu/src/gpu/Config.slh index 28f447a696..6c5308c2ee 100644 --- a/libraries/gpu/src/gpu/Config.slh +++ b/libraries/gpu/src/gpu/Config.slh @@ -18,7 +18,9 @@ <@elif GLPROFILE == MAC_GL @> <@def GPU_FEATURE_PROFILE GPU_LEGACY@> <@def GPU_TRANSFORM_PROFILE GPU_LEGACY@> - <@def VERSION_HEADER #version 120@> + <@def VERSION_HEADER #version 120 +#extension GL_EXT_gpu_shader4 : enable@> +gpu_shader4 <@else@> <@def GPU_FEATURE_PROFILE GPU_LEGACY@> <@def GPU_TRANSFORM_PROFILE GPU_LEGACY@> diff --git a/libraries/gpu/src/gpu/DrawTexture.slf b/libraries/gpu/src/gpu/DrawTexture.slf new file mode 100755 index 0000000000..58671dcb78 --- /dev/null +++ b/libraries/gpu/src/gpu/DrawTexture.slf @@ -0,0 +1,22 @@ +<@include gpu/Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// Draw texture 0 fetched at texcoord.xy +// +// Created by Sam Gateau on 6/22/2015 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + + +uniform sampler2D colorMap; + +varying vec2 varTexcoord; + +void main(void) { + gl_FragColor = texture2D(colorMap, varTexcoord); + // gl_FragColor = vec4(varTexcoord, 0.0, 1.0); +} diff --git a/libraries/gpu/src/gpu/DrawTransformUnitQuad.slv b/libraries/gpu/src/gpu/DrawTransformUnitQuad.slv new file mode 100755 index 0000000000..2d1e4584a7 --- /dev/null +++ b/libraries/gpu/src/gpu/DrawTransformUnitQuad.slv @@ -0,0 +1,36 @@ +<@include gpu/Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// Draw and transform the unit quad [-1,-1 -> 1,1] +// Simply draw a Triangle_strip of 2 triangles, no input buffers or index buffer needed +// +// Created by Sam Gateau on 6/22/2015 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +<@include gpu/Transform.slh@> + +<$declareStandardTransform()$> + +varying vec2 varTexcoord; + +void main(void) { + const vec4 UNIT_QUAD[4] = vec4[4]( + vec4(-1.0, -1.0, 0.0, 1.0), + vec4(1.0, -1.0, 0.0, 1.0), + vec4(-1.0, 1.0, 0.0, 1.0), + vec4(1.0, 1.0, 0.0, 1.0) + ); + vec4 pos = UNIT_QUAD[gl_VertexID]; + + // standard transform + TransformCamera cam = getTransformCamera(); + TransformObject obj = getTransformObject(); + <$transformModelToClipPos(cam, obj, pos, gl_Position)$> + + varTexcoord = (pos.xy + 1) * 0.5; +} diff --git a/libraries/gpu/src/gpu/StandardShaderLib.cpp b/libraries/gpu/src/gpu/StandardShaderLib.cpp new file mode 100755 index 0000000000..fa56eb2051 --- /dev/null +++ b/libraries/gpu/src/gpu/StandardShaderLib.cpp @@ -0,0 +1,35 @@ +// +// StandardShaderLib.cpp +// libraries/gpu/src/gpu +// +// Collection of standard shaders that can be used all over the place +// +// Created by Sam Gateau on 6/22/2015. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +#include "StandardShaderLib.h" + +#include "DrawTransformUnitQuad_vert.h" +#include "DrawTexture_frag.h" + +using namespace gpu; + +ShaderPointer StandardShaderLib::_drawTransformUnitQuadVS; +ShaderPointer StandardShaderLib::_drawTexturePS; + +ShaderPointer StandardShaderLib::getDrawTransformUnitQuadVS() { + if (!_drawTransformUnitQuadVS) { + _drawTransformUnitQuadVS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(DrawTransformUnitQuad_vert))); + } + return _drawTransformUnitQuadVS; +} + +ShaderPointer StandardShaderLib::getDrawTexturePS() { + if (!_drawTexturePS) { + _drawTexturePS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(DrawTexture_frag))); + } + return _drawTexturePS; +} diff --git a/libraries/gpu/src/gpu/StandardShaderLib.h b/libraries/gpu/src/gpu/StandardShaderLib.h new file mode 100755 index 0000000000..cabdbad66b --- /dev/null +++ b/libraries/gpu/src/gpu/StandardShaderLib.h @@ -0,0 +1,38 @@ +// +// StandardShaderLib.h +// libraries/gpu/src/gpu +// +// Collection of standard shaders that can be used all over the place +// +// Created by Sam Gateau on 6/22/2015. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +#ifndef hifi_gpu_StandardShaderLib_h +#define hifi_gpu_StandardShaderLib_h + +#include + +#include "Shader.h" + +namespace gpu { + +class StandardShaderLib { +public: + + static ShaderPointer getDrawTransformUnitQuadVS(); + + static ShaderPointer getDrawTexturePS(); + +protected: + + static ShaderPointer _drawTransformUnitQuadVS; + static ShaderPointer _drawTexturePS; +}; + + +}; + +#endif diff --git a/libraries/gpu/src/gpu/State.h b/libraries/gpu/src/gpu/State.h index c9bd38efeb..07198d8162 100755 --- a/libraries/gpu/src/gpu/State.h +++ b/libraries/gpu/src/gpu/State.h @@ -341,6 +341,7 @@ public: // Color write mask void setColorWriteMask(uint8 mask) { SET_FIELD(COLOR_WRITE_MASK, DEFAULT.colorWriteMask, mask, _values.colorWriteMask); } + void setColorWriteMask(bool red, bool green, bool blue, bool alpha) { SET_FIELD(COLOR_WRITE_MASK, DEFAULT.colorWriteMask, (WRITE_RED * red) | (WRITE_GREEN * green) | (WRITE_BLUE * blue) | (WRITE_ALPHA * alpha), _values.colorWriteMask); } uint8 getColorWriteMask() const { return _values.colorWriteMask; } // All the possible fields diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index b49d1985bb..03dca38462 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -25,6 +25,7 @@ #include "gpu/Batch.h" #include "gpu/GLBackend.h" +#include "gpu/StandardShaderLib.h" #include "simple_vert.h" #include "simple_frag.h" @@ -104,6 +105,19 @@ void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) { loadLightProgram(point_light_frag, true, _pointLight, _pointLightLocations); loadLightProgram(spot_light_frag, true, _spotLight, _spotLightLocations); + { + auto VSFS = gpu::StandardShaderLib::getDrawTransformUnitQuadVS(); + auto PSBlit = gpu::StandardShaderLib::getDrawTexturePS(); + auto blitProgram = gpu::ShaderPointer(gpu::Shader::createProgram(VSFS, PSBlit)); + gpu::Shader::makeProgram(*blitProgram); + gpu::StatePointer blitState = gpu::StatePointer(new gpu::State()); + blitState->setBlendFunction(true, + gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA, + gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE); + blitState->setColorWriteMask(true, true, true, false); + _blitLightBuffer = gpu::PipelinePointer(gpu::Pipeline::create(blitProgram, blitState)); + } + // Allocate a global light representing the Global Directional light casting shadow (the sun) and the ambient light _globalLights.push_back(0); _allocatedLights.push_back(model::LightPointer(new model::Light())); @@ -515,36 +529,48 @@ void DeferredLightingEffect::render(RenderArgs* args) { } void DeferredLightingEffect::copyBack(RenderArgs* args) { + + gpu::Batch batch; + auto textureCache = DependencyManager::get(); QSize framebufferSize = textureCache->getFrameBufferSize(); auto freeFBO = DependencyManager::get()->getFreeFramebuffer(); //freeFBO->release(); - glBindFramebuffer(GL_FRAMEBUFFER, 0); + // glBindFramebuffer(GL_FRAMEBUFFER, 0); - glDisable(GL_CULL_FACE); + // glDisable(GL_CULL_FACE); // now transfer the lit region to the primary fbo - glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE); - glColorMask(true, true, true, false); + // glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_CONSTANT_ALPHA, GL_ONE); + // glColorMask(true, true, true, false); auto primaryFBO = gpu::GLBackend::getFramebufferID(textureCache->getPrimaryFramebuffer()); - glBindFramebuffer(GL_DRAW_FRAMEBUFFER, primaryFBO); +// glBindFramebuffer(GL_DRAW_FRAMEBUFFER, primaryFBO); + batch.setFramebuffer(textureCache->getPrimaryFramebuffer()); + batch.setPipeline(_blitLightBuffer); //primaryFBO->bind(); - glBindTexture(GL_TEXTURE_2D, gpu::GLBackend::getTextureID(freeFBO->getRenderBuffer(0))); - glEnable(GL_TEXTURE_2D); + // glBindTexture(GL_TEXTURE_2D, gpu::GLBackend::getTextureID(freeFBO->getRenderBuffer(0))); + // glEnable(GL_TEXTURE_2D); - glPushMatrix(); + batch.setUniformTexture(0, freeFBO->getRenderBuffer(0)); + + /* glPushMatrix(); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); - + */ + + batch.setProjectionTransform(glm::mat4()); + batch.setModelTransform(Transform()); + batch.setViewTransform(Transform()); + int viewport[4]; glGetIntegerv(GL_VIEWPORT, viewport); const int VIEWPORT_X_INDEX = 0; @@ -557,12 +583,17 @@ void DeferredLightingEffect::copyBack(RenderArgs* args) { float tMin = viewport[VIEWPORT_Y_INDEX] / (float)framebufferSize.height(); float tHeight = viewport[VIEWPORT_HEIGHT_INDEX] / (float)framebufferSize.height(); - renderFullscreenQuad(sMin, sMin + sWidth, tMin, tMin + tHeight); + batch.draw(gpu::TRIANGLE_STRIP, 4); + + + args->_context->syncCache(); + args->_context->render(batch); +// renderFullscreenQuad(sMin, sMin + sWidth, tMin, tMin + tHeight); - glBindTexture(GL_TEXTURE_2D, 0); - glDisable(GL_TEXTURE_2D); + // glBindTexture(GL_TEXTURE_2D, 0); + // glDisable(GL_TEXTURE_2D); - glColorMask(true, true, true, true); + /* glColorMask(true, true, true, true); glEnable(GL_LIGHTING); glEnable(GL_COLOR_MATERIAL); glEnable(GL_DEPTH_TEST); @@ -571,7 +602,7 @@ void DeferredLightingEffect::copyBack(RenderArgs* args) { glPopMatrix(); glMatrixMode(GL_MODELVIEW); - glPopMatrix(); + glPopMatrix();*/ } void DeferredLightingEffect::setupTransparent(RenderArgs* args, int lightBufferUnit) { diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 9d66bf08c0..a18add8585 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -104,6 +104,8 @@ private: gpu::PipelinePointer _simpleProgramTextured; gpu::PipelinePointer _simpleProgramTexturedCullNone; + gpu::PipelinePointer _blitLightBuffer; + ProgramObject _directionalSkyboxLight; LightLocations _directionalSkyboxLightLocations; ProgramObject _directionalSkyboxLightShadowMap; From ca652730b590b2103688c390e9b179b021cf9059 Mon Sep 17 00:00:00 2001 From: samcake Date: Mon, 22 Jun 2015 15:47:13 +0200 Subject: [PATCH 002/107] REfine the gpu/Context.slh to get gpu_shader4 and gl_VertexID --- libraries/gpu/src/gpu/Config.slh | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/gpu/src/gpu/Config.slh b/libraries/gpu/src/gpu/Config.slh index 6c5308c2ee..29c36ff477 100644 --- a/libraries/gpu/src/gpu/Config.slh +++ b/libraries/gpu/src/gpu/Config.slh @@ -20,7 +20,6 @@ <@def GPU_TRANSFORM_PROFILE GPU_LEGACY@> <@def VERSION_HEADER #version 120 #extension GL_EXT_gpu_shader4 : enable@> -gpu_shader4 <@else@> <@def GPU_FEATURE_PROFILE GPU_LEGACY@> <@def GPU_TRANSFORM_PROFILE GPU_LEGACY@> From 6444bcab1bfd229c99a2b7e51c215b5b65b53221 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Mon, 22 Jun 2015 20:00:06 +0200 Subject: [PATCH 003/107] commenting out --- libraries/gpu/src/gpu/DrawTexture.slf | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/gpu/src/gpu/DrawTexture.slf b/libraries/gpu/src/gpu/DrawTexture.slf index 58671dcb78..e456c49649 100755 --- a/libraries/gpu/src/gpu/DrawTexture.slf +++ b/libraries/gpu/src/gpu/DrawTexture.slf @@ -18,5 +18,4 @@ varying vec2 varTexcoord; void main(void) { gl_FragColor = texture2D(colorMap, varTexcoord); - // gl_FragColor = vec4(varTexcoord, 0.0, 1.0); } From c41ebcae788bcfb94bc784fc80af354c6e836b8d Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Fri, 26 Jun 2015 15:40:08 -0700 Subject: [PATCH 004/107] Covering all the case for rendering the light passes --- libraries/gpu/src/gpu/Batch.cpp | 28 + libraries/gpu/src/gpu/Batch.h | 21 +- libraries/gpu/src/gpu/Context.h | 2 +- libraries/gpu/src/gpu/Format.h | 17 + libraries/gpu/src/gpu/GLBackend.cpp | 32 +- libraries/gpu/src/gpu/GLBackend.h | 8 + libraries/gpu/src/gpu/GLBackendOutput.cpp | 11 +- libraries/gpu/src/gpu/GLBackendPipeline.cpp | 6 + libraries/gpu/src/gpu/GLBackendShader.cpp | 5 + libraries/gpu/src/gpu/GLBackendTransform.cpp | 20 +- libraries/gpu/src/gpu/Resource.cpp | 3 + libraries/gpu/src/gpu/StandardShaderLib.cpp | 9 + libraries/gpu/src/gpu/StandardShaderLib.h | 6 + libraries/gpu/src/gpu/State.h | 2 +- libraries/gpu/src/gpu/Transform.slh | 11 + libraries/model/src/model/Geometry.h | 2 + .../src/DeferredLightingEffect.cpp | 530 ++++++++++++++---- .../render-utils/src/DeferredLightingEffect.h | 36 +- libraries/render-utils/src/GeometryCache.cpp | 1 - libraries/render-utils/src/GeometryCache.h | 1 + .../render-utils/src/RenderDeferredTask.cpp | 3 +- .../src/deferred_light_limited.slv | 20 +- libraries/render-utils/src/spot_light.slf | 3 +- 23 files changed, 636 insertions(+), 141 deletions(-) diff --git a/libraries/gpu/src/gpu/Batch.cpp b/libraries/gpu/src/gpu/Batch.cpp index 973e5d01d2..ac4046503c 100644 --- a/libraries/gpu/src/gpu/Batch.cpp +++ b/libraries/gpu/src/gpu/Batch.cpp @@ -103,6 +103,23 @@ void Batch::clearFramebuffer(Framebuffer::Masks targets, const Vec4& color, floa _params.push_back(targets); } +void Batch::clearColorFramebuffer(Framebuffer::Masks targets, const Vec4& color) { + clearFramebuffer(targets & Framebuffer::BUFFER_COLORS, color, 1.0f, 0); +} + +void Batch::clearDepthFramebuffer(float depth) { + clearFramebuffer(Framebuffer::BUFFER_DEPTH, Vec4(0.0f), depth, 0); +} + +void Batch::clearStencilFramebuffer(int stencil) { + clearFramebuffer(Framebuffer::BUFFER_STENCIL, Vec4(0.0f), 1.0f, stencil); +} + +void Batch::clearDepthStencilFramebuffer(float depth, int stencil) { + clearFramebuffer(Framebuffer::BUFFER_DEPTHSTENCIL, Vec4(0.0f), depth, stencil); +} + + void Batch::setInputFormat(const Stream::FormatPointer& format) { ADD_COMMAND(setInputFormat); @@ -141,6 +158,10 @@ void Batch::setIndexBuffer(Type type, const BufferPointer& buffer, Offset offset _params.push_back(type); } +void Batch::setIndexBuffer(const BufferView& buffer) { + setIndexBuffer(buffer._element.getType(), buffer._buffer, buffer._offset); +} + void Batch::setModelTransform(const Transform& model) { ADD_COMMAND(setModelTransform); @@ -159,6 +180,13 @@ void Batch::setProjectionTransform(const Mat4& proj) { _params.push_back(cacheData(sizeof(Mat4), &proj)); } +void Batch::setViewportTransform(const Vec4i& viewport) { + ADD_COMMAND(setViewportTransform); + + _params.push_back(cacheData(sizeof(Vec4i), &viewport)); +} + + void Batch::setPipeline(const PipelinePointer& pipeline) { ADD_COMMAND(setPipeline); diff --git a/libraries/gpu/src/gpu/Batch.h b/libraries/gpu/src/gpu/Batch.h index 9c97db65ef..64e2f7c6ca 100644 --- a/libraries/gpu/src/gpu/Batch.h +++ b/libraries/gpu/src/gpu/Batch.h @@ -44,19 +44,6 @@ namespace gpu { -enum Primitive { - POINTS = 0, - LINES, - LINE_STRIP, - TRIANGLES, - TRIANGLE_STRIP, - TRIANGLE_FAN, - QUADS, - QUAD_STRIP, - - NUM_PRIMITIVES, -}; - enum ReservedSlot { /* TRANSFORM_OBJECT_SLOT = 6, TRANSFORM_CAMERA_SLOT = 7, @@ -82,7 +69,12 @@ public: void drawIndexedInstanced(uint32 nbInstances, Primitive primitiveType, uint32 nbIndices, uint32 startIndex = 0, uint32 startInstance = 0); // Clear framebuffer layers + // Targets can be any of the render buffers contained in the Framebuffer void clearFramebuffer(Framebuffer::Masks targets, const Vec4& color, float depth, int stencil); + void clearColorFramebuffer(Framebuffer::Masks targets, const Vec4& color); // not a command, just a shortcut for clearFramebuffer, mask out targets to make sure it touches only color targets + void clearDepthFramebuffer(float depth); // not a command, just a shortcut for clearFramebuffer, it touches only depth target + void clearStencilFramebuffer(int stencil); // not a command, just a shortcut for clearFramebuffer, it touches only stencil target + void clearDepthStencilFramebuffer(float depth, int stencil); // not a command, just a shortcut for clearFramebuffer, it touches depth and stencil target // Input Stage // InputFormat @@ -95,6 +87,7 @@ public: void setInputStream(Slot startChannel, const BufferStream& stream); // not a command, just unroll into a loop of setInputBuffer void setIndexBuffer(Type type, const BufferPointer& buffer, Offset offset); + void setIndexBuffer(const BufferView& buffer); // not a command, just a shortcut from a BufferView // Transform Stage // Vertex position is transformed by ModelTransform from object space to world space @@ -105,6 +98,7 @@ public: void setModelTransform(const Transform& model); void setViewTransform(const Transform& view); void setProjectionTransform(const Mat4& proj); + void setViewportTransform(const Vec4i& viewport); // Viewport is xy = low left corner in the framebuffer, zw = width height of the viewport // Pipeline Stage void setPipeline(const PipelinePointer& pipeline); @@ -177,6 +171,7 @@ public: COMMAND_setModelTransform, COMMAND_setViewTransform, COMMAND_setProjectionTransform, + COMMAND_setViewportTransform, COMMAND_setPipeline, COMMAND_setStateBlendFactor, diff --git a/libraries/gpu/src/gpu/Context.h b/libraries/gpu/src/gpu/Context.h index 98ddc7fb64..da4882ab65 100644 --- a/libraries/gpu/src/gpu/Context.h +++ b/libraries/gpu/src/gpu/Context.h @@ -42,7 +42,7 @@ public: Mat4 _projectionViewUntranslated; Mat4 _projection; Mat4 _projectionInverse; - Vec4 _viewport; + Vec4i _viewport; }; template< typename T > diff --git a/libraries/gpu/src/gpu/Format.h b/libraries/gpu/src/gpu/Format.h index 7cf913430d..981a560965 100644 --- a/libraries/gpu/src/gpu/Format.h +++ b/libraries/gpu/src/gpu/Format.h @@ -38,6 +38,7 @@ typedef uint32 Offset; typedef glm::mat4 Mat4; typedef glm::mat3 Mat3; typedef glm::vec4 Vec4; +typedef glm::ivec4 Vec4i; typedef glm::vec3 Vec3; typedef glm::vec2 Vec2; typedef glm::ivec2 Vec2i; @@ -181,6 +182,9 @@ public: } static const Element COLOR_RGBA_32; + static const Element VEC3F_XYZ; + static const Element INDEX_UINT16; + static const Element PART_DRAWCALL; protected: uint8 _semantic; @@ -202,6 +206,19 @@ enum ComparisonFunction { NUM_COMPARISON_FUNCS, }; +enum Primitive { + POINTS = 0, + LINES, + LINE_STRIP, + TRIANGLES, + TRIANGLE_STRIP, + TRIANGLE_FAN, + QUADS, + QUAD_STRIP, + + NUM_PRIMITIVES, +}; + }; diff --git a/libraries/gpu/src/gpu/GLBackend.cpp b/libraries/gpu/src/gpu/GLBackend.cpp index 9004c4a8fe..ec93e00117 100644 --- a/libraries/gpu/src/gpu/GLBackend.cpp +++ b/libraries/gpu/src/gpu/GLBackend.cpp @@ -29,6 +29,7 @@ GLBackend::CommandCall GLBackend::_commandCalls[Batch::NUM_COMMANDS] = (&::gpu::GLBackend::do_setModelTransform), (&::gpu::GLBackend::do_setViewTransform), (&::gpu::GLBackend::do_setProjectionTransform), + (&::gpu::GLBackend::do_setViewportTransform), (&::gpu::GLBackend::do_setPipeline), (&::gpu::GLBackend::do_setStateBlendFactor), @@ -219,13 +220,31 @@ void GLBackend::do_clearFramebuffer(Batch& batch, uint32 paramOffset) { glmask |= GL_DEPTH_BUFFER_BIT; } + std::vector drawBuffers; if (masks & Framebuffer::BUFFER_COLORS) { - glClearColor(color.x, color.y, color.z, color.w); - glmask |= GL_COLOR_BUFFER_BIT; + for (int i = 0; i < Framebuffer::MAX_NUM_RENDER_BUFFERS; i++) { + if (masks & (1 << i)) { + drawBuffers.push_back(GL_COLOR_ATTACHMENT0 + i); + } + } + + if (!drawBuffers.empty()) { + glDrawBuffers(drawBuffers.size(), drawBuffers.data()); + glClearColor(color.x, color.y, color.z, color.w); + glmask |= GL_COLOR_BUFFER_BIT; + } } glClear(glmask); + // Restore the color draw buffers only if a frmaebuffer is bound + if (_output._framebuffer && !drawBuffers.empty()) { + auto glFramebuffer = syncGPUObject(*_output._framebuffer); + if (glFramebuffer) { + glDrawBuffers(glFramebuffer->_colorBuffers.size(), glFramebuffer->_colorBuffers.data()); + } + } + (void) CHECK_GL_ERROR(); } @@ -584,10 +603,11 @@ void GLBackend::do_glUniform4fv(Batch& batch, uint32 paramOffset) { return; } updatePipeline(); - glUniform4fv( - batch._params[paramOffset + 2]._int, - batch._params[paramOffset + 1]._uint, - (const GLfloat*)batch.editData(batch._params[paramOffset + 0]._uint)); + + GLint location = batch._params[paramOffset + 2]._int; + GLsizei count = batch._params[paramOffset + 1]._uint; + const GLfloat* value = (const GLfloat*)batch.editData(batch._params[paramOffset + 0]._uint); + glUniform4fv(location, count, value); (void) CHECK_GL_ERROR(); } diff --git a/libraries/gpu/src/gpu/GLBackend.h b/libraries/gpu/src/gpu/GLBackend.h index 28236c68c9..939b43c968 100644 --- a/libraries/gpu/src/gpu/GLBackend.h +++ b/libraries/gpu/src/gpu/GLBackend.h @@ -96,6 +96,7 @@ public: #if (GPU_TRANSFORM_PROFILE == GPU_CORE) #else + GLuint _transformObject_model = -1; GLuint _transformCamera_viewInverse = -1; #endif @@ -179,6 +180,7 @@ public: class GLFramebuffer : public GPUObject { public: GLuint _fbo = 0; + std::vector _colorBuffers; GLFramebuffer(); ~GLFramebuffer(); @@ -267,6 +269,7 @@ protected: void do_setModelTransform(Batch& batch, uint32 paramOffset); void do_setViewTransform(Batch& batch, uint32 paramOffset); void do_setProjectionTransform(Batch& batch, uint32 paramOffset); + void do_setViewportTransform(Batch& batch, uint32 paramOffset); void initTransform(); void killTransform(); @@ -281,9 +284,11 @@ protected: Transform _model; Transform _view; Mat4 _projection; + Vec4i _viewport; bool _invalidModel; bool _invalidView; bool _invalidProj; + bool _invalidViewport; GLenum _lastMode; @@ -293,9 +298,11 @@ protected: _model(), _view(), _projection(), + _viewport(0,0,1,1), _invalidModel(true), _invalidView(true), _invalidProj(false), + _invalidViewport(false), _lastMode(GL_TEXTURE) {} } _transform; @@ -329,6 +336,7 @@ protected: #if (GPU_TRANSFORM_PROFILE == GPU_CORE) #else + GLint _program_transformObject_model = -1; GLint _program_transformCamera_viewInverse = -1; #endif diff --git a/libraries/gpu/src/gpu/GLBackendOutput.cpp b/libraries/gpu/src/gpu/GLBackendOutput.cpp index 903c97f45b..30b60ad183 100755 --- a/libraries/gpu/src/gpu/GLBackendOutput.cpp +++ b/libraries/gpu/src/gpu/GLBackendOutput.cpp @@ -40,8 +40,7 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe glBindFramebuffer(GL_FRAMEBUFFER, fbo); - unsigned int nbColorBuffers = 0; - GLenum colorBuffers[16]; + std::vector colorBuffers; if (framebuffer.hasColor()) { static const GLenum colorAttachments[] = { GL_COLOR_ATTACHMENT0, @@ -69,8 +68,7 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe if (gltexture) { glFramebufferTexture2D(GL_FRAMEBUFFER, colorAttachments[unit], GL_TEXTURE_2D, gltexture->_texture, 0); } - colorBuffers[nbColorBuffers] = colorAttachments[unit]; - nbColorBuffers++; + colorBuffers.push_back(colorAttachments[unit]); unit++; } } @@ -100,8 +98,8 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe } // Last but not least, define where we draw - if (nbColorBuffers > 0) { - glDrawBuffers(nbColorBuffers, colorBuffers); + if (!colorBuffers.empty()) { + glDrawBuffers(colorBuffers.size(), colorBuffers.data()); } else { glDrawBuffer( GL_NONE ); } @@ -139,6 +137,7 @@ GLBackend::GLFramebuffer* GLBackend::syncGPUObject(const Framebuffer& framebuffe // All is green, assign the gpuobject to the Framebuffer object = new GLFramebuffer(); object->_fbo = fbo; + object->_colorBuffers = colorBuffers; Backend::setGPUObject(framebuffer, object); } diff --git a/libraries/gpu/src/gpu/GLBackendPipeline.cpp b/libraries/gpu/src/gpu/GLBackendPipeline.cpp index f4449e9ea1..2ad8302d12 100755 --- a/libraries/gpu/src/gpu/GLBackendPipeline.cpp +++ b/libraries/gpu/src/gpu/GLBackendPipeline.cpp @@ -73,6 +73,7 @@ void GLBackend::do_setPipeline(Batch& batch, uint32 paramOffset) { #if (GPU_TRANSFORM_PROFILE == GPU_CORE) #else + _pipeline._program_transformObject_model = -1; _pipeline._program_transformCamera_viewInverse = -1; #endif @@ -91,6 +92,7 @@ void GLBackend::do_setPipeline(Batch& batch, uint32 paramOffset) { #if (GPU_TRANSFORM_PROFILE == GPU_CORE) #else + _pipeline._program_transformObject_model = pipelineObject->_program->_transformObject_model; _pipeline._program_transformCamera_viewInverse = pipelineObject->_program->_transformCamera_viewInverse; #endif } @@ -143,6 +145,10 @@ void GLBackend::updatePipeline() { #if (GPU_TRANSFORM_PROFILE == GPU_CORE) #else + // If shader program needs the model we need to provide it + if (_pipeline._program_transformObject_model >= 0) { + glUniformMatrix4fv(_pipeline._program_transformObject_model, 1, false, (const GLfloat*) &_transform._transformObject._model); + } // If shader program needs the inverseView we need to provide it if (_pipeline._program_transformCamera_viewInverse >= 0) { glUniformMatrix4fv(_pipeline._program_transformCamera_viewInverse, 1, false, (const GLfloat*) &_transform._transformCamera._viewInverse); diff --git a/libraries/gpu/src/gpu/GLBackendShader.cpp b/libraries/gpu/src/gpu/GLBackendShader.cpp index 45adbcdb3c..6058b8f8a9 100755 --- a/libraries/gpu/src/gpu/GLBackendShader.cpp +++ b/libraries/gpu/src/gpu/GLBackendShader.cpp @@ -111,6 +111,11 @@ void makeBindings(GLBackend::GLShader* shader) { shader->_transformCameraSlot = gpu::TRANSFORM_CAMERA_SLOT; } #else + loc = glGetUniformLocation(glprogram, "transformObject_model"); + if (loc >= 0) { + shader->_transformObject_model = loc; + } + loc = glGetUniformLocation(glprogram, "transformCamera_viewInverse"); if (loc >= 0) { shader->_transformCamera_viewInverse = loc; diff --git a/libraries/gpu/src/gpu/GLBackendTransform.cpp b/libraries/gpu/src/gpu/GLBackendTransform.cpp index 2e3c2dca70..faddacc176 100755 --- a/libraries/gpu/src/gpu/GLBackendTransform.cpp +++ b/libraries/gpu/src/gpu/GLBackendTransform.cpp @@ -15,7 +15,6 @@ using namespace gpu; // Transform Stage - void GLBackend::do_setModelTransform(Batch& batch, uint32 paramOffset) { _transform._model = batch._transforms.get(batch._params[paramOffset]._uint); _transform._invalidModel = true; @@ -31,6 +30,11 @@ void GLBackend::do_setProjectionTransform(Batch& batch, uint32 paramOffset) { _transform._invalidProj = true; } +void GLBackend::do_setViewportTransform(Batch& batch, uint32 paramOffset) { + memcpy(&_transform._viewport, batch.editData(batch._params[paramOffset]._uint), sizeof(Vec4i)); + _transform._invalidViewport = true; +} + void GLBackend::initTransform() { #if (GPU_TRANSFORM_PROFILE == GPU_CORE) glGenBuffers(1, &_transform._transformObjectBuffer); @@ -57,10 +61,13 @@ void GLBackend::killTransform() { } void GLBackend::syncTransformStateCache() { + _transform._invalidViewport = true; _transform._invalidProj = true; _transform._invalidView = true; _transform._invalidModel = true; + glGetIntegerv(GL_VIEWPORT, (GLint*) &_transform._viewport); + GLint currentMode; glGetIntegerv(GL_MATRIX_MODE, ¤tMode); _transform._lastMode = currentMode; @@ -78,6 +85,13 @@ void GLBackend::updateTransform() { GLint originalMatrixMode; glGetIntegerv(GL_MATRIX_MODE, &originalMatrixMode); // Check all the dirty flags and update the state accordingly + if (_transform._invalidViewport) { + _transform._transformCamera._viewport = _transform._viewport; + + // Where we assign the GL viewport + glViewport(_transform._viewport.x, _transform._viewport.y, _transform._viewport.z, _transform._viewport.w); + } + if (_transform._invalidProj) { _transform._transformCamera._projection = _transform._projection; _transform._transformCamera._projectionInverse = glm::inverse(_transform._projection); @@ -100,7 +114,7 @@ void GLBackend::updateTransform() { } #if (GPU_TRANSFORM_PROFILE == GPU_CORE) - if (_transform._invalidView || _transform._invalidProj) { + if (_transform._invalidView || _transform._invalidProj || _transform._invalidViewport) { glBindBufferBase(GL_UNIFORM_BUFFER, TRANSFORM_CAMERA_SLOT, 0); glBindBuffer(GL_ARRAY_BUFFER, _transform._transformCameraBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(_transform._transformCamera), (const void*) &_transform._transformCamera, GL_DYNAMIC_DRAW); @@ -162,7 +176,7 @@ void GLBackend::updateTransform() { #endif // Flags are clean - _transform._invalidView = _transform._invalidProj = _transform._invalidModel = false; + _transform._invalidView = _transform._invalidProj = _transform._invalidModel = _transform._invalidViewport = false; glMatrixMode(originalMatrixMode); } diff --git a/libraries/gpu/src/gpu/Resource.cpp b/libraries/gpu/src/gpu/Resource.cpp index 046cf9fe40..5498e24189 100644 --- a/libraries/gpu/src/gpu/Resource.cpp +++ b/libraries/gpu/src/gpu/Resource.cpp @@ -15,6 +15,9 @@ using namespace gpu; const Element Element::COLOR_RGBA_32 = Element(VEC4, UINT8, RGBA); +const Element Element::VEC3F_XYZ = Element(VEC3, FLOAT, XYZ); +const Element Element::INDEX_UINT16 = Element(SCALAR, UINT16, INDEX); +const Element Element::PART_DRAWCALL = Element(VEC4, UINT32, PART); Resource::Size Resource::Sysmem::allocateMemory(Byte** dataAllocated, Size size) { if ( !dataAllocated ) { diff --git a/libraries/gpu/src/gpu/StandardShaderLib.cpp b/libraries/gpu/src/gpu/StandardShaderLib.cpp index fa56eb2051..581ce47cde 100755 --- a/libraries/gpu/src/gpu/StandardShaderLib.cpp +++ b/libraries/gpu/src/gpu/StandardShaderLib.cpp @@ -13,11 +13,13 @@ #include "StandardShaderLib.h" #include "DrawTransformUnitQuad_vert.h" +#include "DrawViewportQuadTransformTexcoord_vert.h" #include "DrawTexture_frag.h" using namespace gpu; ShaderPointer StandardShaderLib::_drawTransformUnitQuadVS; +ShaderPointer StandardShaderLib::_drawViewportQuadTransformTexcoordVS; ShaderPointer StandardShaderLib::_drawTexturePS; ShaderPointer StandardShaderLib::getDrawTransformUnitQuadVS() { @@ -27,6 +29,13 @@ ShaderPointer StandardShaderLib::getDrawTransformUnitQuadVS() { return _drawTransformUnitQuadVS; } +ShaderPointer StandardShaderLib::getDrawViewportQuadTransformTexcoordVS() { + if (!_drawViewportQuadTransformTexcoordVS) { + _drawViewportQuadTransformTexcoordVS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(DrawViewportQuadTransformTexcoord_vert))); + } + return _drawViewportQuadTransformTexcoordVS; +} + ShaderPointer StandardShaderLib::getDrawTexturePS() { if (!_drawTexturePS) { _drawTexturePS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(DrawTexture_frag))); diff --git a/libraries/gpu/src/gpu/StandardShaderLib.h b/libraries/gpu/src/gpu/StandardShaderLib.h index cabdbad66b..a8fc5126f8 100755 --- a/libraries/gpu/src/gpu/StandardShaderLib.h +++ b/libraries/gpu/src/gpu/StandardShaderLib.h @@ -22,13 +22,19 @@ namespace gpu { class StandardShaderLib { public: + // Shader draw the unit quad objectPos = ([(-1,-1),(1,1)]) and transform it by the full model transform stack (Model, View, Proj). + // A texcoord attribute is also generated texcoord = [(0,0),(1,1)] static ShaderPointer getDrawTransformUnitQuadVS(); + // Shader draws the unit quad in the full viewport clipPos = ([(-1,-1),(1,1)]) and transform the texcoord = [(0,0),(1,1)] by the model transform. + static ShaderPointer getDrawViewportQuadTransformTexcoordVS(); + static ShaderPointer getDrawTexturePS(); protected: static ShaderPointer _drawTransformUnitQuadVS; + static ShaderPointer _drawViewportQuadTransformTexcoordVS; static ShaderPointer _drawTexturePS; }; diff --git a/libraries/gpu/src/gpu/State.h b/libraries/gpu/src/gpu/State.h index 07198d8162..59fbacc089 100755 --- a/libraries/gpu/src/gpu/State.h +++ b/libraries/gpu/src/gpu/State.h @@ -341,7 +341,7 @@ public: // Color write mask void setColorWriteMask(uint8 mask) { SET_FIELD(COLOR_WRITE_MASK, DEFAULT.colorWriteMask, mask, _values.colorWriteMask); } - void setColorWriteMask(bool red, bool green, bool blue, bool alpha) { SET_FIELD(COLOR_WRITE_MASK, DEFAULT.colorWriteMask, (WRITE_RED * red) | (WRITE_GREEN * green) | (WRITE_BLUE * blue) | (WRITE_ALPHA * alpha), _values.colorWriteMask); } + void setColorWriteMask(bool red, bool green, bool blue, bool alpha) { uint32 value = ((WRITE_RED * red) | (WRITE_GREEN * green) | (WRITE_BLUE * blue) | (WRITE_ALPHA * alpha)); SET_FIELD(COLOR_WRITE_MASK, DEFAULT.colorWriteMask, value, _values.colorWriteMask); } uint8 getColorWriteMask() const { return _values.colorWriteMask; } // All the possible fields diff --git a/libraries/gpu/src/gpu/Transform.slh b/libraries/gpu/src/gpu/Transform.slh index d01fe128ae..fad2e9ada8 100644 --- a/libraries/gpu/src/gpu/Transform.slh +++ b/libraries/gpu/src/gpu/Transform.slh @@ -86,6 +86,7 @@ TransformCamera getTransformCamera() { return camera; } +uniform mat4 transformObject_model; uniform mat4 transformCamera_viewInverse; <@endif@> @@ -122,6 +123,16 @@ uniform mat4 transformCamera_viewInverse; <@endif@> <@endfunc@> +<@func transformModelToWorldPos(objectTransform, modelPos, worldPos)@> +<@if GPU_TRANSFORM_PROFILE == GPU_CORE@> + { // transformModelToWorldPos + <$worldPos$> = (<$objectTransform$>._model * <$modelPos$>); + } +<@else@> + <$worldPos$> = vec3(transformObject_model * <$modelPos$>); +<@endif@> +<@endfunc@> + <@func transformModelToEyeDir(cameraTransform, objectTransform, modelDir, eyeDir)@> <@if GPU_TRANSFORM_PROFILE == GPU_CORE@> { // transformModelToEyeDir diff --git a/libraries/model/src/model/Geometry.h b/libraries/model/src/model/Geometry.h index ddefaf4e96..16ebb60b72 100755 --- a/libraries/model/src/model/Geometry.h +++ b/libraries/model/src/model/Geometry.h @@ -113,6 +113,8 @@ public: // Generate a BufferStream on the mesh vertices and attributes const gpu::BufferStream makeBufferStream() const; + static gpu::Primitive topologyToPrimitive(Topology topo) { return static_cast(topo); } + protected: gpu::Stream::FormatPointer _vertexFormat; diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 03dca38462..afcc116aea 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -106,7 +106,7 @@ void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) { loadLightProgram(spot_light_frag, true, _spotLight, _spotLightLocations); { - auto VSFS = gpu::StandardShaderLib::getDrawTransformUnitQuadVS(); + auto VSFS = gpu::StandardShaderLib::getDrawViewportQuadTransformTexcoordVS(); auto PSBlit = gpu::StandardShaderLib::getDrawTexturePS(); auto blitProgram = gpu::ShaderPointer(gpu::Shader::createProgram(VSFS, PSBlit)); gpu::Shader::makeProgram(*blitProgram); @@ -225,51 +225,73 @@ void DeferredLightingEffect::addSpotLight(const glm::vec3& position, float radiu } void DeferredLightingEffect::prepare(RenderArgs* args) { - // clear the normal and specular buffers + auto textureCache = DependencyManager::get(); + gpu::Batch batch; + // batch.setFramebuffer(textureCache->getPrimaryFramebuffer()); + + // clear the normal and specular buffers + batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLOR1, glm::vec4(0.0f, 0.0f, 0.0f, 0.0f)); + const float MAX_SPECULAR_EXPONENT = 128.0f; + batch.clearColorFramebuffer(gpu::Framebuffer::BUFFER_COLOR2, glm::vec4(0.0f, 0.0f, 0.0f, 1.0f / MAX_SPECULAR_EXPONENT)); + + args->_context->syncCache(); + args->_context->render(batch); +/* textureCache->setPrimaryDrawBuffers(false, true, false); glClear(GL_COLOR_BUFFER_BIT); textureCache->setPrimaryDrawBuffers(false, false, true); // clearing to zero alpha for specular causes problems on my Nvidia card; clear to lowest non-zero value instead const float MAX_SPECULAR_EXPONENT = 128.0f; glClearColor(0.0f, 0.0f, 0.0f, 1.0f / MAX_SPECULAR_EXPONENT); - glClear(GL_COLOR_BUFFER_BIT); - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glClear(GL_COLOR_BUFFER_BIT);*/ + /* glClearColor(0.0f, 0.0f, 0.0f, 0.0f); textureCache->setPrimaryDrawBuffers(true, false, false); -} +*/} void DeferredLightingEffect::render(RenderArgs* args) { + gpu::Batch batch; + // perform deferred lighting, rendering to free fbo - glDisable(GL_BLEND); + /* glDisable(GL_BLEND); glDisable(GL_LIGHTING); glDisable(GL_DEPTH_TEST); glDisable(GL_COLOR_MATERIAL); glDepthMask(false); - + */ auto textureCache = DependencyManager::get(); - glBindFramebuffer(GL_FRAMEBUFFER, 0 ); + // glBindFramebuffer(GL_FRAMEBUFFER, 0 ); QSize framebufferSize = textureCache->getFrameBufferSize(); // binding the first framebuffer auto freeFBO = DependencyManager::get()->getFreeFramebuffer(); - glBindFramebuffer(GL_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(freeFBO)); + + batch.setFramebuffer(freeFBO); + + //glBindFramebuffer(GL_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(freeFBO)); - glClear(GL_COLOR_BUFFER_BIT); - // glEnable(GL_FRAMEBUFFER_SRGB); + batch.clearColorFramebuffer(freeFBO->getBufferMask(), glm::vec4(0.0f, 0.0f, 0.0f, 0.0f)); + + // glClear(GL_COLOR_BUFFER_BIT); // glBindTexture(GL_TEXTURE_2D, primaryFBO->texture()); - glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryColorTextureID()); + // glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryColorTextureID()); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryNormalTextureID()); + batch.setUniformTexture(0, textureCache->getPrimaryColorTexture()); + + // glActiveTexture(GL_TEXTURE1); + // glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryNormalTextureID()); + batch.setUniformTexture(1, textureCache->getPrimaryNormalTexture()); - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, textureCache->getPrimarySpecularTextureID()); + // glActiveTexture(GL_TEXTURE2); + // glBindTexture(GL_TEXTURE_2D, textureCache->getPrimarySpecularTextureID()); + batch.setUniformTexture(2, textureCache->getPrimarySpecularTexture()); - glActiveTexture(GL_TEXTURE3); - glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryDepthTextureID()); + // glActiveTexture(GL_TEXTURE3); + // glBindTexture(GL_TEXTURE_2D, textureCache->getPrimaryDepthTextureID()); + batch.setUniformTexture(3, textureCache->getPrimaryDepthTexture()); // get the viewport side (left, right, both) int viewport[4]; @@ -290,50 +312,55 @@ void DeferredLightingEffect::render(RenderArgs* args) { glm::mat4 invViewMat; _viewState->getViewTransform().getMatrix(invViewMat); - ProgramObject* program = &_directionalLight; + // ProgramObject* program = &_directionalLight; + auto& program = _directionalLight; const LightLocations* locations = &_directionalLightLocations; bool shadowsEnabled = _viewState->getShadowsEnabled(); if (shadowsEnabled) { - glActiveTexture(GL_TEXTURE4); - glBindTexture(GL_TEXTURE_2D, textureCache->getShadowDepthTextureID()); - program = &_directionalLightShadowMap; + // glActiveTexture(GL_TEXTURE4); + // glBindTexture(GL_TEXTURE_2D, textureCache->getShadowDepthTextureID()); + batch.setUniformTexture(4, textureCache->getShadowFramebuffer()->getDepthStencilBuffer()); + + program = _directionalLightShadowMap; locations = &_directionalLightShadowMapLocations; if (_viewState->getCascadeShadowsEnabled()) { - program = &_directionalLightCascadedShadowMap; + program = _directionalLightCascadedShadowMap; locations = &_directionalLightCascadedShadowMapLocations; if (useSkyboxCubemap) { - program = &_directionalSkyboxLightCascadedShadowMap; + program = _directionalSkyboxLightCascadedShadowMap; locations = &_directionalSkyboxLightCascadedShadowMapLocations; } else if (_ambientLightMode > -1) { - program = &_directionalAmbientSphereLightCascadedShadowMap; + program = _directionalAmbientSphereLightCascadedShadowMap; locations = &_directionalAmbientSphereLightCascadedShadowMapLocations; } - program->bind(); - program->setUniform(locations->shadowDistances, _viewState->getShadowDistances()); + batch.setPipeline(program); + //program->bind(); + // program->setUniform(locations->shadowDistances, _viewState->getShadowDistances()); + batch._glUniform3fv(locations->shadowDistances, 1, (const GLfloat*) &_viewState->getShadowDistances()); } else { if (useSkyboxCubemap) { - program = &_directionalSkyboxLightShadowMap; + program = _directionalSkyboxLightShadowMap; locations = &_directionalSkyboxLightShadowMapLocations; } else if (_ambientLightMode > -1) { - program = &_directionalAmbientSphereLightShadowMap; + program = _directionalAmbientSphereLightShadowMap; locations = &_directionalAmbientSphereLightShadowMapLocations; } - program->bind(); + batch.setPipeline(program); } - program->setUniformValue(locations->shadowScale, - 1.0f / textureCache->getShadowFramebuffer()->getWidth()); + // program->setUniformValue(locations->shadowScale, 1.0f / textureCache->getShadowFramebuffer()->getWidth()); + batch._glUniform1f(locations->shadowScale, 1.0f / textureCache->getShadowFramebuffer()->getWidth()); } else { if (useSkyboxCubemap) { - program = &_directionalSkyboxLight; + program = _directionalSkyboxLight; locations = &_directionalSkyboxLightLocations; } else if (_ambientLightMode > -1) { - program = &_directionalAmbientSphereLight; + program = _directionalAmbientSphereLight; locations = &_directionalAmbientSphereLightLocations; } - program->bind(); + batch.setPipeline(program); } { @@ -344,71 +371,116 @@ void DeferredLightingEffect::render(RenderArgs* args) { if (useSkyboxCubemap && _skybox->getCubemap()->getIrradiance()) { sh = (*_skybox->getCubemap()->getIrradiance()); } - for (int i =0; i ambientSphere, gpu::SphericalHarmonics::NUM_COEFFICIENTS, (const GLfloat*) (&sh)); + + for (int i =0; i ambientSphere + i, 1, (const GLfloat*) (&sh) + i * 4); + } + /* for (int i =0; i setUniformValue(locations->ambientSphere + i, *(((QVector4D*) &sh) + i)); - } + }*/ } if (useSkyboxCubemap) { - glActiveTexture(GL_TEXTURE5); - glBindTexture(GL_TEXTURE_CUBE_MAP, gpu::GLBackend::getTextureID(_skybox->getCubemap())); + // glActiveTexture(GL_TEXTURE5); + // glBindTexture(GL_TEXTURE_CUBE_MAP, gpu::GLBackend::getTextureID(_skybox->getCubemap())); + batch.setUniformTexture(5, _skybox->getCubemap()); } if (locations->lightBufferUnit >= 0) { - gpu::Batch batch; + //gpu::Batch batch; batch.setUniformBuffer(locations->lightBufferUnit, globalLight->getSchemaBuffer()); - gpu::GLBackend::renderBatch(batch); + //gpu::GLBackend::renderBatch(batch); } if (_atmosphere && (locations->atmosphereBufferUnit >= 0)) { - gpu::Batch batch; + //gpu::Batch batch; batch.setUniformBuffer(locations->atmosphereBufferUnit, _atmosphere->getDataBuffer()); - gpu::GLBackend::renderBatch(batch); + //gpu::GLBackend::renderBatch(batch); } - glUniformMatrix4fv(locations->invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + // glUniformMatrix4fv(locations->invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + batch._glUniformMatrix4fv(locations->invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); } float left, right, bottom, top, nearVal, farVal; glm::vec4 nearClipPlane, farClipPlane; _viewState->computeOffAxisFrustum(left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane); - program->setUniformValue(locations->nearLocation, nearVal); + // program->setUniformValue(locations->nearLocation, nearVal); + batch._glUniform1f(locations->nearLocation, nearVal); + float depthScale = (farVal - nearVal) / farVal; - program->setUniformValue(locations->depthScale, depthScale); + // program->setUniformValue(locations->depthScale, depthScale); + batch._glUniform1f(locations->depthScale, depthScale); + float nearScale = -1.0f / nearVal; float depthTexCoordScaleS = (right - left) * nearScale / sWidth; float depthTexCoordScaleT = (top - bottom) * nearScale / tHeight; float depthTexCoordOffsetS = left * nearScale - sMin * depthTexCoordScaleS; float depthTexCoordOffsetT = bottom * nearScale - tMin * depthTexCoordScaleT; - program->setUniformValue(locations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); - program->setUniformValue(locations->depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); + // program->setUniformValue(locations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); + // program->setUniformValue(locations->depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); + batch._glUniform2f(locations->depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); + batch._glUniform2f(locations->depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); - renderFullscreenQuad(sMin, sMin + sWidth, tMin, tMin + tHeight); - - program->release(); + Transform model; + model.setTranslation(glm::vec3(sMin, tMin, 0.0)); + model.setScale(glm::vec3(sWidth, tHeight, 1.0)); + batch.setModelTransform(model); + + batch.setProjectionTransform(glm::mat4()); + batch.setViewTransform(Transform()); + + { + glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f); + glm::vec2 topLeft(-1.0f, -1.0f); + glm::vec2 bottomRight(1.0f, 1.0f); + glm::vec2 texCoordTopLeft(sMin, tMin); + glm::vec2 texCoordBottomRight(sMin + sWidth, tMin + tHeight); + + DependencyManager::get()->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color); + } + + // renderFullscreenQuad(sMin, sMin + sWidth, tMin, tMin + tHeight); + // batch.draw(gpu::TRIANGLE_STRIP, 4); // full screen quad + + // args->_context->syncCache(); + // args->_context->render(batch); + //program->release(); if (useSkyboxCubemap) { - glBindTexture(GL_TEXTURE_CUBE_MAP, 0); - if (!shadowsEnabled) { - glActiveTexture(GL_TEXTURE3); - } + batch.setUniformTexture(5, nullptr); + + // glBindTexture(GL_TEXTURE_CUBE_MAP, 0); + // if (!shadowsEnabled) { + // glActiveTexture(GL_TEXTURE3); + // } } if (shadowsEnabled) { - glBindTexture(GL_TEXTURE_2D, 0); - glActiveTexture(GL_TEXTURE3); + batch.setUniformTexture(4, nullptr); + // glBindTexture(GL_TEXTURE_2D, 0); + // glActiveTexture(GL_TEXTURE3); } - + + // additive blending - glEnable(GL_BLEND); - glBlendFunc(GL_ONE, GL_ONE); + // glEnable(GL_BLEND); + // glBlendFunc(GL_ONE, GL_ONE); - glEnable(GL_CULL_FACE); + // glEnable(GL_CULL_FACE); + glm::vec4 sCoefficients(sWidth / 2.0f, 0.0f, 0.0f, sMin + sWidth / 2.0f); glm::vec4 tCoefficients(0.0f, tHeight / 2.0f, 0.0f, tMin + tHeight / 2.0f); - glTexGenfv(GL_S, GL_OBJECT_PLANE, (const GLfloat*)&sCoefficients); - glTexGenfv(GL_T, GL_OBJECT_PLANE, (const GLfloat*)&tCoefficients); - + // glTexGenfv(GL_S, GL_OBJECT_PLANE, (const GLfloat*)&sCoefficients); + // glTexGenfv(GL_T, GL_OBJECT_PLANE, (const GLfloat*)&tCoefficients); + // texcoordMat + auto texcoordMat = glm::mat4(); + texcoordMat[0] = glm::vec4(sWidth / 2.0f, 0.0f, 0.0f, sMin + sWidth / 2.0f); + texcoordMat[1] = glm::vec4(0.0f, tHeight / 2.0f, 0.0f, tMin + tHeight / 2.0f); + texcoordMat[2] = glm::vec4(0.0f, 0.0f, 1.0f, 0.0f); + texcoordMat[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); + // enlarge the scales slightly to account for tesselation const float SCALE_EXPANSION = 0.05f; @@ -417,102 +489,199 @@ void DeferredLightingEffect::render(RenderArgs* args) { auto geometryCache = DependencyManager::get(); + glm::mat4 projMat; + Transform viewMat; + args->_viewFrustum->evalProjectionMatrix(projMat); + args->_viewFrustum->evalViewTransform(viewMat); + if (args->_renderMode == RenderArgs::MIRROR_RENDER_MODE) { + viewMat.postScale(glm::vec3(-1.0f, 1.0f, 1.0f)); + } + batch.setProjectionTransform(projMat); + batch.setViewTransform(viewMat); + if (!_pointLights.empty()) { - _pointLight.bind(); - _pointLight.setUniformValue(_pointLightLocations.nearLocation, nearVal); - _pointLight.setUniformValue(_pointLightLocations.depthScale, depthScale); - _pointLight.setUniformValue(_pointLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); - _pointLight.setUniformValue(_pointLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); + batch.setPipeline(_pointLight); + batch._glUniform1f(_pointLightLocations.nearLocation, nearVal); + batch._glUniform1f(_pointLightLocations.depthScale, depthScale); + batch._glUniform2f(_pointLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); + batch._glUniform2f(_pointLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); + + //_pointLight.bind(); + //_pointLight.setUniformValue(_pointLightLocations.nearLocation, nearVal); + //_pointLight.setUniformValue(_pointLightLocations.depthScale, depthScale); + //_pointLight.setUniformValue(_pointLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); + //_pointLight.setUniformValue(_pointLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); + + batch._glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + + //glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + batch._glUniformMatrix4fv(_pointLightLocations.texcoordMat, 1, false, reinterpret_cast< const GLfloat* >(&texcoordMat)); for (auto lightID : _pointLights) { - auto light = _allocatedLights[lightID]; - + auto& light = _allocatedLights[lightID]; + light->setShowContour(true); if (_pointLightLocations.lightBufferUnit >= 0) { - gpu::Batch batch; + // gpu::Batch batch; batch.setUniformBuffer(_pointLightLocations.lightBufferUnit, light->getSchemaBuffer()); - gpu::GLBackend::renderBatch(batch); + // gpu::GLBackend::renderBatch(batch); } - glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); - glPushMatrix(); + + // glPushMatrix(); float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius) { - glLoadIdentity(); + + /* glLoadIdentity(); glTranslatef(0.0f, 0.0f, -1.0f); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); - - renderFullscreenQuad(); - + */ + // renderFullscreenQuad(); + Transform model; + model.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f)); + batch.setModelTransform(model); + + { + glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f); + glm::vec2 topLeft(-1.0f, -1.0f); + glm::vec2 bottomRight(1.0f, 1.0f); + glm::vec2 texCoordTopLeft(sMin, tMin); + glm::vec2 texCoordBottomRight(sMin + sWidth, tMin + tHeight); + + DependencyManager::get()->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color); + } + /* glPopMatrix(); glMatrixMode(GL_MODELVIEW); - + */ } else { - glTranslatef(light->getPosition().x, light->getPosition().y, light->getPosition().z); - geometryCache->renderSphere(expandedRadius, 32, 32, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); + Transform model; + model.setTranslation(glm::vec3(light->getPosition().x, light->getPosition().y, light->getPosition().z)); + batch.setModelTransform(model); + // glTranslatef(light->getPosition().x, light->getPosition().y, light->getPosition().z); + + // geometryCache->renderSphere(expandedRadius, 32, 32, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); + geometryCache->renderSphere(batch, expandedRadius, 32, 32, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); } - glPopMatrix(); + // glPopMatrix(); } _pointLights.clear(); - _pointLight.release(); + // _pointLight.release(); } if (!_spotLights.empty()) { - _spotLight.bind(); + batch.setPipeline(_pointLight); + batch._glUniform1f(_spotLightLocations.nearLocation, nearVal); + batch._glUniform1f(_spotLightLocations.depthScale, depthScale); + batch._glUniform2f(_spotLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); + batch._glUniform2f(_spotLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); + +/* _spotLight.bind(); _spotLight.setUniformValue(_spotLightLocations.nearLocation, nearVal); _spotLight.setUniformValue(_spotLightLocations.depthScale, depthScale); _spotLight.setUniformValue(_spotLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); _spotLight.setUniformValue(_spotLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); - + */ + + + batch._glUniformMatrix4fv(_spotLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + + //glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + batch._glUniformMatrix4fv(_spotLightLocations.texcoordMat, 1, false, reinterpret_cast< const GLfloat* >(&texcoordMat)); + for (auto lightID : _spotLights) { auto light = _allocatedLights[lightID]; if (_spotLightLocations.lightBufferUnit >= 0) { - gpu::Batch batch; + // gpu::Batch batch; batch.setUniformBuffer(_spotLightLocations.lightBufferUnit, light->getSchemaBuffer()); - gpu::GLBackend::renderBatch(batch); + // gpu::GLBackend::renderBatch(batch); } - glUniformMatrix4fv(_spotLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); + // glUniformMatrix4fv(_spotLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); - glPushMatrix(); + // glPushMatrix(); float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); float edgeRadius = expandedRadius / glm::cos(light->getSpotAngle()); if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < edgeRadius + nearRadius) { - glLoadIdentity(); + /*glLoadIdentity(); glTranslatef(0.0f, 0.0f, -1.0f); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); - renderFullscreenQuad(); - - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); + renderFullscreenQuad();*/ + Transform model; + model.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f)); + batch.setModelTransform(model); + + { + glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f); + glm::vec2 topLeft(-1.0f, -1.0f); + glm::vec2 bottomRight(1.0f, 1.0f); + glm::vec2 texCoordTopLeft(sMin, tMin); + glm::vec2 texCoordBottomRight(sMin + sWidth, tMin + tHeight); + + DependencyManager::get()->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color); + } + + /* glPopMatrix(); + glMatrixMode(GL_MODELVIEW);*/ } else { + Transform model; + model.setTranslation(glm::vec3(light->getPosition().x, light->getPosition().y, light->getPosition().z)); + + glm::quat spotRotation = rotationBetween(glm::vec3(0.0f, 0.0f, -1.0f), light->getDirection()); + glm::vec3 axis = glm::axis(spotRotation); + + model.postRotate(spotRotation); + model.postTranslate(glm::vec3(0.0f, 0.0f, -light->getMaximumRadius() * (1.0f + SCALE_EXPANSION * 0.5f))); + + float base = expandedRadius * glm::tan(light->getSpotAngle()); + float height = expandedRadius; + model.postScale(glm::vec3(base, base, height)); + + batch.setModelTransform(model); + auto& mesh = getSpotLightMesh(); + + + batch.setIndexBuffer(mesh->getIndexBuffer()); + batch.setInputBuffer(0, mesh->getVertexBuffer()); + batch.setInputFormat(mesh->getVertexFormat()); + + + auto& part = mesh->getPartBuffer().get(); + + batch.drawIndexed(model::Mesh::topologyToPrimitive(part._topology), part._numIndices, part._startIndex); + + + //geometryCache->renderCone(batch, expandedRadius * glm::tan(light->getSpotAngle()), expandedRadius, 32, 1); + + /* glTranslatef(light->getPosition().x, light->getPosition().y, light->getPosition().z); glm::quat spotRotation = rotationBetween(glm::vec3(0.0f, 0.0f, -1.0f), light->getDirection()); glm::vec3 axis = glm::axis(spotRotation); glRotatef(glm::degrees(glm::angle(spotRotation)), axis.x, axis.y, axis.z); glTranslatef(0.0f, 0.0f, -light->getMaximumRadius() * (1.0f + SCALE_EXPANSION * 0.5f)); - geometryCache->renderCone(expandedRadius * glm::tan(light->getSpotAngle()), - expandedRadius, 32, 1); + geometryCache->renderCone(expandedRadius * glm::tan(light->getSpotAngle()), expandedRadius, 32, 1); + */ } glPopMatrix(); } _spotLights.clear(); - _spotLight.release(); + // _spotLight.release(); } - + +/* glBindTexture(GL_TEXTURE_2D, 0); glActiveTexture(GL_TEXTURE2); @@ -523,8 +692,16 @@ void DeferredLightingEffect::render(RenderArgs* args) { glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, 0); - // glDisable(GL_FRAMEBUFFER_SRGB); - +*/ + // Probably not necessary in the long run because the gpu layer would unbound this texture if used as render target + batch.setUniformTexture(0, nullptr); + batch.setUniformTexture(1, nullptr); + batch.setUniformTexture(2, nullptr); + batch.setUniformTexture(3, nullptr); + + args->_context->syncCache(); + args->_context->render(batch); + // End of the Lighting pass } @@ -568,7 +745,6 @@ void DeferredLightingEffect::copyBack(RenderArgs* args) { */ batch.setProjectionTransform(glm::mat4()); - batch.setModelTransform(Transform()); batch.setViewTransform(Transform()); int viewport[4]; @@ -583,6 +759,14 @@ void DeferredLightingEffect::copyBack(RenderArgs* args) { float tMin = viewport[VIEWPORT_Y_INDEX] / (float)framebufferSize.height(); float tHeight = viewport[VIEWPORT_HEIGHT_INDEX] / (float)framebufferSize.height(); + Transform model; + model.setTranslation(glm::vec3(sMin, tMin, 0.0)); + model.setScale(glm::vec3(sWidth, tHeight, 1.0)); + batch.setModelTransform(model); + + + batch.setViewportTransform(glm::ivec4(viewport[0], viewport[1], viewport[2], viewport[3])); + batch.draw(gpu::TRIANGLE_STRIP, 4); @@ -609,7 +793,7 @@ void DeferredLightingEffect::setupTransparent(RenderArgs* args, int lightBufferU auto globalLight = _allocatedLights[_globalLights.front()]; args->_batch->setUniformBuffer(lightBufferUnit, globalLight->getSchemaBuffer()); } - +/* void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations) { program.addShaderFromSourceCode(QGLShader::Vertex, (limited ? deferred_light_limited_vert : deferred_light_vert)); program.addShaderFromSourceCode(QGLShader::Fragment, fragSource); @@ -672,6 +856,60 @@ void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool limit program.release(); } +*/ + + +void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool lightVolume, gpu::PipelinePointer& pipeline, LightLocations& locations) { + auto VS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string((lightVolume ? deferred_light_limited_vert : deferred_light_vert)))); + auto PS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(fragSource))); + + gpu::ShaderPointer program = gpu::ShaderPointer(gpu::Shader::createProgram(VS, PS)); + + gpu::Shader::BindingSet slotBindings; + slotBindings.insert(gpu::Shader::Binding(std::string("diffuseMap"), 0)); + slotBindings.insert(gpu::Shader::Binding(std::string("normalMap"), 1)); + slotBindings.insert(gpu::Shader::Binding(std::string("specularMap"), 2)); + slotBindings.insert(gpu::Shader::Binding(std::string("depthMap"), 3)); + slotBindings.insert(gpu::Shader::Binding(std::string("shadowMap"), 4)); + slotBindings.insert(gpu::Shader::Binding(std::string("skyboxMap"), 5)); + const GLint LIGHT_GPU_SLOT = 3; + slotBindings.insert(gpu::Shader::Binding(std::string("lightBuffer"), LIGHT_GPU_SLOT)); + const GLint ATMOSPHERE_GPU_SLOT = 4; + slotBindings.insert(gpu::Shader::Binding(std::string("atmosphereBufferUnit"), ATMOSPHERE_GPU_SLOT)); + + gpu::Shader::makeProgram(*program, slotBindings); + + locations.shadowDistances = program->getUniforms().findLocation("shadowDistances"); + locations.shadowScale = program->getUniforms().findLocation("shadowScale"); + locations.nearLocation = program->getUniforms().findLocation("near"); + locations.depthScale = program->getUniforms().findLocation("depthScale"); + locations.depthTexCoordOffset = program->getUniforms().findLocation("depthTexCoordOffset"); + locations.depthTexCoordScale = program->getUniforms().findLocation("depthTexCoordScale"); + locations.radius = program->getUniforms().findLocation("radius"); + locations.ambientSphere = program->getUniforms().findLocation("ambientSphere.L00"); + locations.invViewMat = program->getUniforms().findLocation("invViewMat"); + locations.texcoordMat = program->getUniforms().findLocation("texcoordMat"); + +#if (GPU_FEATURE_PROFILE == GPU_CORE) + locations.lightBufferUnit = program->getBuffers().findLocation("lightBuffer"); + locations.atmosphereBufferUnit = program->getBuffers().findLocation("atmosphereBufferUnit"); +#else + locations.lightBufferUnit = program->getUniforms().findLocation("lightBuffer"); + locations.atmosphereBufferUnit = program->getUniforms().findLocation("atmosphereBufferUnit"); +#endif + + gpu::StatePointer state = gpu::StatePointer(new gpu::State()); + if (lightVolume) { + state->setCullMode(gpu::State::CULL_BACK); + + // additive blending + state->setBlendFunction(true, gpu::State::ONE, gpu::State::BLEND_OP_ADD, gpu::State::ONE); + } else { + state->setCullMode(gpu::State::CULL_BACK); + } + pipeline.reset(gpu::Pipeline::create(program, state)); + +} void DeferredLightingEffect::setAmbientLightMode(int preset) { if ((preset >= 0) && (preset < gpu::SphericalHarmonics::NUM_PRESET)) { @@ -695,3 +933,89 @@ void DeferredLightingEffect::setGlobalLight(const glm::vec3& direction, const gl void DeferredLightingEffect::setGlobalSkybox(const model::SkyboxPointer& skybox) { _skybox = skybox; } + +model::MeshPointer DeferredLightingEffect::getSpotLightMesh() { + if (!_spotLightMesh) { + _spotLightMesh.reset(new model::Mesh()); + + int slices = 32; + int stacks = 1; + int vertices = (stacks + 2) * slices; + int baseTriangles = slices - 2; + int indices = 6 * slices * stacks + 3 * baseTriangles; + + GLfloat* vertexData = new GLfloat[vertices * 3]; + GLfloat* vertex = vertexData; + // cap + for (int i = 0; i < slices; i++) { + float theta = TWO_PI * i / slices; + + //normals + /* *(vertex++) = 0.0f; + *(vertex++) = 0.0f; + *(vertex++) = -1.0f; + */ + // vertices + *(vertex++) = cosf(theta); + *(vertex++) = sinf(theta); + *(vertex++) = 0.0f; + } + // body + for (int i = 0; i <= stacks; i++) { + float z = (float)i / stacks; + float radius = 1.0f - z; + + for (int j = 0; j < slices; j++) { + float theta = TWO_PI * j / slices; + + //normals + /* *(vertex++) = cosf(theta) / SQUARE_ROOT_OF_2; + *(vertex++) = sinf(theta) / SQUARE_ROOT_OF_2; + *(vertex++) = 1.0f / SQUARE_ROOT_OF_2; + */ + // vertices + *(vertex++) = radius * cosf(theta); + *(vertex++) = radius * sinf(theta); + *(vertex++) = z; + } + } + + _spotLightMesh->setVertexBuffer(gpu::BufferView(new gpu::Buffer(sizeof(GLfloat) * vertices, (gpu::Byte*) vertexData), gpu::Element::VEC3F_XYZ)); + delete[] vertexData; + + + GLushort* indexData = new GLushort[indices]; + GLushort* index = indexData; + for (int i = 0; i < baseTriangles; i++) { + *(index++) = 0; + *(index++) = i + 2; + *(index++) = i + 1; + } + for (int i = 1; i <= stacks; i++) { + GLushort bottom = i * slices; + GLushort top = bottom + slices; + for (int j = 0; j < slices; j++) { + int next = (j + 1) % slices; + + *(index++) = bottom + j; + *(index++) = top + next; + *(index++) = top + j; + + *(index++) = bottom + j; + *(index++) = bottom + next; + *(index++) = top + next; + } + } + + _spotLightMesh->setIndexBuffer(gpu::BufferView(new gpu::Buffer(sizeof(GLushort) * indices, (gpu::Byte*) indexData), gpu::Element::INDEX_UINT16)); + delete[] indexData; + + model::Mesh::Part part(0, vertices - 1, 0, model::Mesh::TRIANGLES); + + _spotLightMesh->setPartBuffer(gpu::BufferView(new gpu::Buffer(sizeof(part), (gpu::Byte*) &part), gpu::Element::PART_DRAWCALL)); + + _spotLightMesh->makeBufferStream(); + } + return _spotLightMesh; +} + diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index a18add8585..040acd39d5 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -21,6 +21,7 @@ #include "model/Light.h" #include "model/Stage.h" +#include "model/Geometry.h" class AbstractViewStateInterface; class RenderArgs; @@ -95,9 +96,14 @@ private: int lightBufferUnit; int atmosphereBufferUnit; int invViewMat; + int texcoordMat; }; + + model::MeshPointer _spotLightMesh; + model::MeshPointer getSpotLightMesh(); - static void loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations); + // static void loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations); + static void loadLightProgram(const char* fragSource, bool lightVolume, gpu::PipelinePointer& program, LightLocations& locations); gpu::PipelinePointer _simpleProgram; gpu::PipelinePointer _simpleProgramCullNone; @@ -106,6 +112,32 @@ private: gpu::PipelinePointer _blitLightBuffer; + gpu::PipelinePointer _directionalSkyboxLight; + LightLocations _directionalSkyboxLightLocations; + gpu::PipelinePointer _directionalSkyboxLightShadowMap; + LightLocations _directionalSkyboxLightShadowMapLocations; + gpu::PipelinePointer _directionalSkyboxLightCascadedShadowMap; + LightLocations _directionalSkyboxLightCascadedShadowMapLocations; + + gpu::PipelinePointer _directionalAmbientSphereLight; + LightLocations _directionalAmbientSphereLightLocations; + gpu::PipelinePointer _directionalAmbientSphereLightShadowMap; + LightLocations _directionalAmbientSphereLightShadowMapLocations; + gpu::PipelinePointer _directionalAmbientSphereLightCascadedShadowMap; + LightLocations _directionalAmbientSphereLightCascadedShadowMapLocations; + + gpu::PipelinePointer _directionalLight; + LightLocations _directionalLightLocations; + gpu::PipelinePointer _directionalLightShadowMap; + LightLocations _directionalLightShadowMapLocations; + gpu::PipelinePointer _directionalLightCascadedShadowMap; + LightLocations _directionalLightCascadedShadowMapLocations; + + gpu::PipelinePointer _pointLight; + LightLocations _pointLightLocations; + gpu::PipelinePointer _spotLight; + LightLocations _spotLightLocations; +/* ProgramObject _directionalSkyboxLight; LightLocations _directionalSkyboxLightLocations; ProgramObject _directionalSkyboxLightShadowMap; @@ -131,7 +163,7 @@ private: LightLocations _pointLightLocations; ProgramObject _spotLight; LightLocations _spotLightLocations; - +*/ class PointLight { public: glm::vec4 position; diff --git a/libraries/render-utils/src/GeometryCache.cpp b/libraries/render-utils/src/GeometryCache.cpp index 3e7e9a0adf..b49721d9c7 100644 --- a/libraries/render-utils/src/GeometryCache.cpp +++ b/libraries/render-utils/src/GeometryCache.cpp @@ -401,7 +401,6 @@ void GeometryCache::renderCone(float base, float height, int slices, int stacks) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } - void GeometryCache::renderGrid(int xDivisions, int yDivisions, const glm::vec4& color) { gpu::Batch batch; renderGrid(batch, xDivisions, yDivisions, color); diff --git a/libraries/render-utils/src/GeometryCache.h b/libraries/render-utils/src/GeometryCache.h index f97ab8a773..83891bbf49 100644 --- a/libraries/render-utils/src/GeometryCache.h +++ b/libraries/render-utils/src/GeometryCache.h @@ -301,6 +301,7 @@ private: }; QHash _coneVBOs; + int _nextID; QHash _lastRegisteredQuad3DTexture; diff --git a/libraries/render-utils/src/RenderDeferredTask.cpp b/libraries/render-utils/src/RenderDeferredTask.cpp index ced83da5e9..9a5fdf69ce 100755 --- a/libraries/render-utils/src/RenderDeferredTask.cpp +++ b/libraries/render-utils/src/RenderDeferredTask.cpp @@ -38,8 +38,9 @@ void ResolveDeferred::run(const SceneContextPointer& sceneContext, const RenderC } RenderDeferredTask::RenderDeferredTask() : Task() { - _jobs.push_back(Job(new PrepareDeferred::JobModel("PrepareDeferred"))); _jobs.push_back(Job(new DrawBackground::JobModel("DrawBackground"))); + + _jobs.push_back(Job(new PrepareDeferred::JobModel("PrepareDeferred"))); _jobs.push_back(Job(new FetchItems::JobModel("FetchOpaque", FetchItems( [] (const RenderContextPointer& context, int count) { diff --git a/libraries/render-utils/src/deferred_light_limited.slv b/libraries/render-utils/src/deferred_light_limited.slv index e3051d43f7..e657f36598 100644 --- a/libraries/render-utils/src/deferred_light_limited.slv +++ b/libraries/render-utils/src/deferred_light_limited.slv @@ -12,9 +12,23 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +<@include gpu/Transform.slh@> + +<$declareStandardTransform()$> + +uniform mat4 texcoordMat; + void main(void) { - gl_Position = ftransform(); + // gl_Position = ftransform(); + + // standard transform + TransformCamera cam = getTransformCamera(); + TransformObject obj = getTransformObject(); + <$transformModelToClipPos(cam, obj, gl_Vertex, gl_Position)$>; + vec4 projected = gl_Position / gl_Position.w; - gl_TexCoord[0] = vec4(dot(projected, gl_ObjectPlaneS[3]) * gl_Position.w, - dot(projected, gl_ObjectPlaneT[3]) * gl_Position.w, 0.0, gl_Position.w); + // gl_TexCoord[0] = vec4(dot(projected, gl_ObjectPlaneS[3]) * gl_Position.w, + // dot(projected, gl_ObjectPlaneT[3]) * gl_Position.w, 0.0, gl_Position.w); + gl_TexCoord[0] = vec4(dot(projected, texcoordMat[0]) * gl_Position.w, + dot(projected, texcoordMat[1]) * gl_Position.w, 0.0, gl_Position.w); } diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index e6c3938b71..72aa41344c 100644 --- a/libraries/render-utils/src/spot_light.slf +++ b/libraries/render-utils/src/spot_light.slf @@ -28,7 +28,8 @@ void main(void) { // Grab the fragment data from the uv vec2 texCoord = gl_TexCoord[0].st / gl_TexCoord[0].q; DeferredFragment frag = unpackDeferredFragment(texCoord); - + + // Kill if in front of the light volume float depth = frag.depthVal; if (depth < gl_FragCoord.z) { From f73e56a93f9a76fb3cc07a6b52c4eb75435be3c4 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Fri, 26 Jun 2015 15:45:15 -0700 Subject: [PATCH 005/107] Covering all the case for rendering the light passes --- .../gpu/DrawViewportQuadTransformTexcoord.slv | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 libraries/gpu/src/gpu/DrawViewportQuadTransformTexcoord.slv diff --git a/libraries/gpu/src/gpu/DrawViewportQuadTransformTexcoord.slv b/libraries/gpu/src/gpu/DrawViewportQuadTransformTexcoord.slv new file mode 100755 index 0000000000..7fd6e816b3 --- /dev/null +++ b/libraries/gpu/src/gpu/DrawViewportQuadTransformTexcoord.slv @@ -0,0 +1,38 @@ +<@include gpu/Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// Draw the unit quad [-1,-1 -> 1,1] filling in +// Simply draw a Triangle_strip of 2 triangles, no input buffers or index buffer needed +// +// Created by Sam Gateau on 6/22/2015 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +<@include gpu/Transform.slh@> + +<$declareStandardTransform()$> + +varying vec2 varTexcoord; + +void main(void) { + const vec4 UNIT_QUAD[4] = vec4[4]( + vec4(-1.0, -1.0, 0.0, 1.0), + vec4(1.0, -1.0, 0.0, 1.0), + vec4(-1.0, 1.0, 0.0, 1.0), + vec4(1.0, 1.0, 0.0, 1.0) + ); + vec4 pos = UNIT_QUAD[gl_VertexID]; + + // standard transform but applied to the Texcoord + vec4 tc = vec4((pos.xy + 1) * 0.5, pos.zw); + + TransformObject obj = getTransformObject(); + <$transformModelToWorldPos(obj, tc, tc)$> + + gl_Position = pos; + varTexcoord = tc.xy; +} From 1b69b388350a48d2ec620cdaa34c960d452e270f Mon Sep 17 00:00:00 2001 From: samcake Date: Sat, 27 Jun 2015 06:34:58 -0700 Subject: [PATCH 006/107] fix shader issue on mac --- .../gpu/DrawViewportQuadTransformTexcoord.slv | 30 +++++++++---------- libraries/gpu/src/gpu/Transform.slh | 2 +- .../src/DeferredLightingEffect.cpp | 14 ++++----- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/libraries/gpu/src/gpu/DrawViewportQuadTransformTexcoord.slv b/libraries/gpu/src/gpu/DrawViewportQuadTransformTexcoord.slv index 7fd6e816b3..e91b8a7644 100755 --- a/libraries/gpu/src/gpu/DrawViewportQuadTransformTexcoord.slv +++ b/libraries/gpu/src/gpu/DrawViewportQuadTransformTexcoord.slv @@ -12,27 +12,27 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -<@include gpu/Transform.slh@> - +<@include gpu/Transform.slh@> + <$declareStandardTransform()$> varying vec2 varTexcoord; void main(void) { - const vec4 UNIT_QUAD[4] = vec4[4]( - vec4(-1.0, -1.0, 0.0, 1.0), - vec4(1.0, -1.0, 0.0, 1.0), - vec4(-1.0, 1.0, 0.0, 1.0), + const vec4 UNIT_QUAD[4] = vec4[4]( + vec4(-1.0, -1.0, 0.0, 1.0), + vec4(1.0, -1.0, 0.0, 1.0), + vec4(-1.0, 1.0, 0.0, 1.0), vec4(1.0, 1.0, 0.0, 1.0) ); vec4 pos = UNIT_QUAD[gl_VertexID]; - - // standard transform but applied to the Texcoord - vec4 tc = vec4((pos.xy + 1) * 0.5, pos.zw); - - TransformObject obj = getTransformObject(); - <$transformModelToWorldPos(obj, tc, tc)$> - - gl_Position = pos; - varTexcoord = tc.xy; + + // standard transform but applied to the Texcoord + vec4 tc = vec4((pos.xy + 1) * 0.5, pos.zw); + + TransformObject obj = getTransformObject(); + <$transformModelToWorldPos(obj, tc, tc)$> + + gl_Position = pos; + varTexcoord = tc.xy; } diff --git a/libraries/gpu/src/gpu/Transform.slh b/libraries/gpu/src/gpu/Transform.slh index fad2e9ada8..1802f585cd 100644 --- a/libraries/gpu/src/gpu/Transform.slh +++ b/libraries/gpu/src/gpu/Transform.slh @@ -129,7 +129,7 @@ uniform mat4 transformCamera_viewInverse; <$worldPos$> = (<$objectTransform$>._model * <$modelPos$>); } <@else@> - <$worldPos$> = vec3(transformObject_model * <$modelPos$>); + <$worldPos$> = (transformObject_model * <$modelPos$>); <@endif@> <@endfunc@> diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index afcc116aea..b742cd8ffe 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -644,12 +644,12 @@ void DeferredLightingEffect::render(RenderArgs* args) { model.postRotate(spotRotation); model.postTranslate(glm::vec3(0.0f, 0.0f, -light->getMaximumRadius() * (1.0f + SCALE_EXPANSION * 0.5f))); - float base = expandedRadius * glm::tan(light->getSpotAngle()); + float base = expandedRadius * glm::tan(light->getSpotAngle()); float height = expandedRadius; model.postScale(glm::vec3(base, base, height)); batch.setModelTransform(model); - auto& mesh = getSpotLightMesh(); + auto mesh = getSpotLightMesh(); batch.setIndexBuffer(mesh->getIndexBuffer()); @@ -935,11 +935,11 @@ void DeferredLightingEffect::setGlobalSkybox(const model::SkyboxPointer& skybox) } model::MeshPointer DeferredLightingEffect::getSpotLightMesh() { - if (!_spotLightMesh) { - _spotLightMesh.reset(new model::Mesh()); - - int slices = 32; - int stacks = 1; + if (!_spotLightMesh) { + _spotLightMesh.reset(new model::Mesh()); + + int slices = 32; + int stacks = 1; int vertices = (stacks + 2) * slices; int baseTriangles = slices - 2; int indices = 6 * slices * stacks + 3 * baseTriangles; From efbccc3a4e883e20a3010466186de83eb38b2fc2 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Mon, 29 Jun 2015 15:30:51 -0700 Subject: [PATCH 007/107] Fix storing Oculus eye positions --- interface/src/devices/OculusManager.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/interface/src/devices/OculusManager.cpp b/interface/src/devices/OculusManager.cpp index 9d7146cbe7..e5c00a3699 100644 --- a/interface/src/devices/OculusManager.cpp +++ b/interface/src/devices/OculusManager.cpp @@ -692,13 +692,13 @@ void OculusManager::display(QGLWidget * glCanvas, RenderArgs* renderArgs, const _eyeRenderPoses[eye] = eyePoses[eye]; // Set the camera rotation for this eye - vec3 eyePosition = toGlm(_eyeRenderPoses[eye].Position); - eyePosition = whichCamera.getRotation() * eyePosition; + _eyePositions[eye] = toGlm(_eyeRenderPoses[eye].Position); + _eyePositions[eye] = whichCamera.getRotation() * _eyePositions[eye]; quat eyeRotation = toGlm(_eyeRenderPoses[eye].Orientation); // Update our camera to what the application camera is doing _camera->setRotation(whichCamera.getRotation() * eyeRotation); - _camera->setPosition(whichCamera.getPosition() + eyePosition); + _camera->setPosition(whichCamera.getPosition() + _eyePositions[eye]); configureCamera(*_camera); _camera->update(1.0f / Application::getInstance()->getFps()); From 6a48b56e0f48381030fe51c333c6d808e49d6cf3 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Mon, 29 Jun 2015 15:31:15 -0700 Subject: [PATCH 008/107] In mirror mode look directly at the camera in both normal and HMD view --- interface/src/Application.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 2e2722aec2..6975f13291 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2282,17 +2282,14 @@ void Application::updateMyAvatarLookAtPosition() { bool isLookingAtSomeone = false; glm::vec3 lookAtSpot; if (_myCamera.getMode() == CAMERA_MODE_MIRROR) { - // When I am in mirror mode, just look right at the camera (myself) + // When I am in mirror mode, just look right at the camera (myself); don't switch gaze points because when physically + // looking in a mirror one's eyes appear steady. if (!OculusManager::isConnected()) { lookAtSpot = _myCamera.getPosition(); } else { - if (_myAvatar->isLookingAtLeftEye()) { - lookAtSpot = OculusManager::getLeftEyePosition(); - } else { - lookAtSpot = OculusManager::getRightEyePosition(); - } + lookAtSpot = _myCamera.getPosition() + + (OculusManager::getLeftEyePosition() + OculusManager::getRightEyePosition()) / 2.0f; } - } else { AvatarSharedPointer lookingAt = _myAvatar->getLookAtTargetAvatar().lock(); if (lookingAt && _myAvatar != lookingAt.get()) { From bc4c6351068e81c8d4171ac7e7f5c6a0aa260089 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Wed, 1 Jul 2015 18:31:16 -0700 Subject: [PATCH 009/107] Fix gazing at avatar (when no head tracking or HMD) Randomly look at avatar's left and right eyes if the face is visible. Otherwise just look at their head. --- interface/src/Application.cpp | 28 ++++++++++++++++++++-------- interface/src/avatar/FaceModel.cpp | 2 +- interface/src/avatar/Head.cpp | 4 ++-- interface/src/avatar/MyAvatar.cpp | 16 ---------------- 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 6975f13291..acc34cbc19 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2294,22 +2294,34 @@ void Application::updateMyAvatarLookAtPosition() { AvatarSharedPointer lookingAt = _myAvatar->getLookAtTargetAvatar().lock(); if (lookingAt && _myAvatar != lookingAt.get()) { isLookingAtSomeone = true; + Head* lookingAtHead = static_cast(lookingAt.get())->getHead(); + // If I am looking at someone else, look directly at one of their eyes if (tracker && !tracker->isMuted()) { // If a face tracker is active, look at the eye for the side my gaze is biased toward if (tracker->getEstimatedEyeYaw() > _myAvatar->getHead()->getFinalYaw()) { - // Look at their right eye - lookAtSpot = static_cast(lookingAt.get())->getHead()->getRightEyePosition(); + lookAtSpot = lookingAtHead->getRightEyePosition(); } else { - // Look at their left eye - lookAtSpot = static_cast(lookingAt.get())->getHead()->getLeftEyePosition(); + lookAtSpot = lookingAtHead->getLeftEyePosition(); } } else { - // Need to add randomly looking back and forth between left and right eye for case with no tracker - if (_myAvatar->isLookingAtLeftEye()) { - lookAtSpot = static_cast(lookingAt.get())->getHead()->getLeftEyePosition(); + + const float MAXIMUM_FACE_ANGLE = 65.0f * RADIANS_PER_DEGREE; + glm::vec3 lookingAtFaceOrientation = lookingAtHead->getFinalOrientationInWorldFrame() * IDENTITY_FRONT; + glm::vec3 fromLookingAtToMe = glm::normalize(_myAvatar->getHead()->getEyePosition() + - lookingAtHead->getEyePosition()); + float faceAngle = glm::angle(lookingAtFaceOrientation, fromLookingAtToMe); + + if (faceAngle < MAXIMUM_FACE_ANGLE) { + // Randomly look back and forth between left and right eyes + if (_myAvatar->isLookingAtLeftEye()) { + lookAtSpot = lookingAtHead->getLeftEyePosition(); + } else { + lookAtSpot = lookingAtHead->getRightEyePosition(); + } } else { - lookAtSpot = static_cast(lookingAt.get())->getHead()->getRightEyePosition(); + // Just look at their head (mid point between eyes) + lookAtSpot = lookingAtHead->getEyePosition(); } } } else { diff --git a/interface/src/avatar/FaceModel.cpp b/interface/src/avatar/FaceModel.cpp index 1501c52de5..7a582406a4 100644 --- a/interface/src/avatar/FaceModel.cpp +++ b/interface/src/avatar/FaceModel.cpp @@ -77,7 +77,7 @@ void FaceModel::maybeUpdateEyeRotation(Model* model, const JointState& parentSta glm::translate(state.getDefaultTranslationInConstrainedFrame()) * joint.preTransform * glm::mat4_cast(joint.preRotation * joint.rotation)); glm::vec3 front = glm::vec3(inverse * glm::vec4(_owningHead->getFinalOrientationInWorldFrame() * IDENTITY_FRONT, 0.0f)); - glm::vec3 lookAt = glm::vec3(inverse * glm::vec4(_owningHead->getCorrectedLookAtPosition() + + glm::vec3 lookAt = glm::vec3(inverse * glm::vec4(_owningHead->getLookAtPosition() + _owningHead->getSaccade() - model->getTranslation(), 1.0f)); glm::quat between = rotationBetween(front, lookAt); const float MAX_ANGLE = 30.0f * RADIANS_PER_DEGREE; diff --git a/interface/src/avatar/Head.cpp b/interface/src/avatar/Head.cpp index 02d16a1ca4..61f378c536 100644 --- a/interface/src/avatar/Head.cpp +++ b/interface/src/avatar/Head.cpp @@ -256,7 +256,7 @@ void Head::calculateMouthShapes() { void Head::applyEyelidOffset(glm::quat headOrientation) { // Adjusts the eyelid blendshape coefficients so that the eyelid follows the iris as the head pitches. - glm::quat eyeRotation = rotationBetween(headOrientation * IDENTITY_FRONT, getCorrectedLookAtPosition() - _eyePosition); + glm::quat eyeRotation = rotationBetween(headOrientation * IDENTITY_FRONT, getLookAtPosition() - _eyePosition); eyeRotation = eyeRotation * glm::angleAxis(safeEulerAngles(headOrientation).y, IDENTITY_UP); // Rotation w.r.t. head float eyePitch = safeEulerAngles(eyeRotation).x; @@ -295,7 +295,7 @@ void Head::relaxLean(float deltaTime) { void Head::render(RenderArgs* renderArgs, float alpha, ViewFrustum* renderFrustum, bool postLighting) { if (postLighting) { if (_renderLookatVectors) { - renderLookatVectors(renderArgs, _leftEyePosition, _rightEyePosition, getCorrectedLookAtPosition()); + renderLookatVectors(renderArgs, _leftEyePosition, _rightEyePosition, getLookAtPosition()); } } } diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index e4ed55601a..f0bba200fe 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -880,7 +880,6 @@ void MyAvatar::updateLookAtTargetAvatar() { const float KEEP_LOOKING_AT_CURRENT_ANGLE_FACTOR = 1.3f; const float GREATEST_LOOKING_AT_DISTANCE = 10.0f; - int howManyLookingAtMe = 0; foreach (const AvatarSharedPointer& avatarPointer, DependencyManager::get()->getAvatarHash()) { Avatar* avatar = static_cast(avatarPointer.get()); bool isCurrentTarget = avatar->getIsLookAtTarget(); @@ -893,21 +892,6 @@ void MyAvatar::updateLookAtTargetAvatar() { _targetAvatarPosition = avatarPointer->getPosition(); smallestAngleTo = angleTo; } - // Check if this avatar is looking at me, and fix their gaze on my camera if so - if (Application::getInstance()->isLookingAtMyAvatar(avatar)) { - howManyLookingAtMe++; - // Have that avatar look directly at my camera - // Philip TODO: correct to look at left/right eye - if (qApp->isHMDMode()) { - avatar->getHead()->setCorrectedLookAtPosition(Application::getInstance()->getViewFrustum()->getPosition()); - // FIXME what is the point of this? - // avatar->getHead()->setCorrectedLookAtPosition(OculusManager::getLeftEyePosition()); - } else { - avatar->getHead()->setCorrectedLookAtPosition(Application::getInstance()->getViewFrustum()->getPosition()); - } - } else { - avatar->getHead()->clearCorrectedLookAtPosition(); - } } } auto avatarPointer = _lookAtTargetAvatar.lock(); From fada70fe02f48358bb435935373cd431768ba673 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Wed, 1 Jul 2015 18:32:33 -0700 Subject: [PATCH 010/107] Remove redundant code --- interface/src/Application.cpp | 9 --------- interface/src/Application.h | 2 -- interface/src/avatar/Head.cpp | 13 ------------- interface/src/avatar/Head.h | 5 ----- 4 files changed, 29 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index acc34cbc19..f8fb2bb9a6 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2229,15 +2229,6 @@ void Application::shrinkMirrorView() { const float HEAD_SPHERE_RADIUS = 0.1f; -bool Application::isLookingAtMyAvatar(Avatar* avatar) { - glm::vec3 theirLookAt = avatar->getHead()->getLookAtPosition(); - glm::vec3 myEyePosition = _myAvatar->getHead()->getEyePosition(); - if (pointInSphere(theirLookAt, myEyePosition, HEAD_SPHERE_RADIUS * _myAvatar->getScale())) { - return true; - } - return false; -} - void Application::updateLOD() { PerformanceTimer perfTimer("LOD"); // adjust it unless we were asked to disable this feature, or if we're currently in throttleRendering mode diff --git a/interface/src/Application.h b/interface/src/Application.h index 0787cffbdc..375aded8ac 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -322,8 +322,6 @@ public: QStringList getRunningScripts() { return _scriptEnginesHash.keys(); } ScriptEngine* getScriptEngine(QString scriptHash) { return _scriptEnginesHash.contains(scriptHash) ? _scriptEnginesHash[scriptHash] : NULL; } - bool isLookingAtMyAvatar(Avatar* avatar); - float getRenderResolutionScale() const; int getRenderAmbientLight() const; diff --git a/interface/src/avatar/Head.cpp b/interface/src/avatar/Head.cpp index 61f378c536..27888b9d4e 100644 --- a/interface/src/avatar/Head.cpp +++ b/interface/src/avatar/Head.cpp @@ -315,19 +315,6 @@ glm::quat Head::getFinalOrientationInLocalFrame() const { return glm::quat(glm::radians(glm::vec3(getFinalPitch(), getFinalYaw(), getFinalRoll() ))); } -glm::vec3 Head::getCorrectedLookAtPosition() { - if (_isLookingAtMe) { - return _correctedLookAtPosition; - } else { - return getLookAtPosition(); - } -} - -void Head::setCorrectedLookAtPosition(glm::vec3 correctedLookAtPosition) { - _isLookingAtMe = true; - _correctedLookAtPosition = correctedLookAtPosition; -} - glm::quat Head::getCameraOrientation() const { // NOTE: Head::getCameraOrientation() is not used for orienting the camera "view" while in Oculus mode, so // you may wonder why this code is here. This method will be called while in Oculus mode to determine how diff --git a/interface/src/avatar/Head.h b/interface/src/avatar/Head.h index a208574c26..0b216e4a2e 100644 --- a/interface/src/avatar/Head.h +++ b/interface/src/avatar/Head.h @@ -56,9 +56,6 @@ public: /// \return orientationBody * orientationBasePitch glm::quat getCameraOrientation () const; - void setCorrectedLookAtPosition(glm::vec3 correctedLookAtPosition); - glm::vec3 getCorrectedLookAtPosition(); - void clearCorrectedLookAtPosition() { _isLookingAtMe = false; } bool getIsLookingAtMe() { return _isLookingAtMe; } float getScale() const { return _scale; } @@ -147,8 +144,6 @@ private: bool _isLookingAtMe; FaceModel _faceModel; - glm::vec3 _correctedLookAtPosition; - int _leftEyeLookAtID; int _rightEyeLookAtID; From 26cbb14f45c91f788d5f7ce89a6435bcd4e9c6bb Mon Sep 17 00:00:00 2001 From: David Rowe Date: Wed, 1 Jul 2015 20:09:36 -0700 Subject: [PATCH 011/107] Alternative look-at gaze left/right with face trackers too Instead of looking at one or other eye depending on look direction. --- interface/src/Application.cpp | 38 +++++++++++++---------------------- 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index f8fb2bb9a6..29bc9a4f9c 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2284,36 +2284,26 @@ void Application::updateMyAvatarLookAtPosition() { } else { AvatarSharedPointer lookingAt = _myAvatar->getLookAtTargetAvatar().lock(); if (lookingAt && _myAvatar != lookingAt.get()) { + // If I am looking at someone else, look directly at one of their eyes isLookingAtSomeone = true; Head* lookingAtHead = static_cast(lookingAt.get())->getHead(); - // If I am looking at someone else, look directly at one of their eyes - if (tracker && !tracker->isMuted()) { - // If a face tracker is active, look at the eye for the side my gaze is biased toward - if (tracker->getEstimatedEyeYaw() > _myAvatar->getHead()->getFinalYaw()) { - lookAtSpot = lookingAtHead->getRightEyePosition(); - } else { + const float MAXIMUM_FACE_ANGLE = 65.0f * RADIANS_PER_DEGREE; + glm::vec3 lookingAtFaceOrientation = lookingAtHead->getFinalOrientationInWorldFrame() * IDENTITY_FRONT; + glm::vec3 fromLookingAtToMe = glm::normalize(_myAvatar->getHead()->getEyePosition() + - lookingAtHead->getEyePosition()); + float faceAngle = glm::angle(lookingAtFaceOrientation, fromLookingAtToMe); + + if (faceAngle < MAXIMUM_FACE_ANGLE) { + // Randomly look back and forth between left and right eyes + if (_myAvatar->isLookingAtLeftEye()) { lookAtSpot = lookingAtHead->getLeftEyePosition(); + } else { + lookAtSpot = lookingAtHead->getRightEyePosition(); } } else { - - const float MAXIMUM_FACE_ANGLE = 65.0f * RADIANS_PER_DEGREE; - glm::vec3 lookingAtFaceOrientation = lookingAtHead->getFinalOrientationInWorldFrame() * IDENTITY_FRONT; - glm::vec3 fromLookingAtToMe = glm::normalize(_myAvatar->getHead()->getEyePosition() - - lookingAtHead->getEyePosition()); - float faceAngle = glm::angle(lookingAtFaceOrientation, fromLookingAtToMe); - - if (faceAngle < MAXIMUM_FACE_ANGLE) { - // Randomly look back and forth between left and right eyes - if (_myAvatar->isLookingAtLeftEye()) { - lookAtSpot = lookingAtHead->getLeftEyePosition(); - } else { - lookAtSpot = lookingAtHead->getRightEyePosition(); - } - } else { - // Just look at their head (mid point between eyes) - lookAtSpot = lookingAtHead->getEyePosition(); - } + // Just look at their head (mid point between eyes) + lookAtSpot = lookingAtHead->getEyePosition(); } } else { // I am not looking at anyone else, so just look forward From 4ced0dc6c441f926a7bb31e0d762bc69d87387e0 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Wed, 1 Jul 2015 20:24:47 -0700 Subject: [PATCH 012/107] Only deflect eyes for Faceshift; DDE doesn't provide eye pitch or yaw --- interface/src/Application.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 29bc9a4f9c..48ea822efc 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2311,14 +2311,13 @@ void Application::updateMyAvatarLookAtPosition() { (_myAvatar->getHead()->getFinalOrientationInWorldFrame() * glm::vec3(0.0f, 0.0f, -TREE_SCALE)); } } - // - // Deflect the eyes a bit to match the detected Gaze from 3D camera if active - // - if (tracker && !tracker->isMuted()) { + + // Deflect the eyes a bit to match the detected gaze from Faceshift if active. + // DDE doesn't track eyes. + if (tracker && typeid(*tracker) == typeid(Faceshift) && !tracker->isMuted()) { float eyePitch = tracker->getEstimatedEyePitch(); float eyeYaw = tracker->getEstimatedEyeYaw(); const float GAZE_DEFLECTION_REDUCTION_DURING_EYE_CONTACT = 0.1f; - // deflect using Faceshift gaze data glm::vec3 origin = _myAvatar->getHead()->getEyePosition(); float pitchSign = (_myCamera.getMode() == CAMERA_MODE_MIRROR) ? -1.0f : 1.0f; float deflection = DependencyManager::get()->getEyeDeflection(); From 026f6d3690165c1adca62a722cf158b6c533590c Mon Sep 17 00:00:00 2001 From: David Rowe Date: Wed, 1 Jul 2015 21:22:30 -0700 Subject: [PATCH 013/107] Add mouth as third gaze target in addition to left and right eyes --- interface/src/Application.cpp | 16 +++++++++++----- interface/src/avatar/Head.h | 6 +----- interface/src/avatar/MyAvatar.cpp | 24 ++++++++++++++++++------ interface/src/avatar/MyAvatar.h | 10 ++++++++-- 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 48ea822efc..d68a3813a9 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2295,11 +2295,17 @@ void Application::updateMyAvatarLookAtPosition() { float faceAngle = glm::angle(lookingAtFaceOrientation, fromLookingAtToMe); if (faceAngle < MAXIMUM_FACE_ANGLE) { - // Randomly look back and forth between left and right eyes - if (_myAvatar->isLookingAtLeftEye()) { - lookAtSpot = lookingAtHead->getLeftEyePosition(); - } else { - lookAtSpot = lookingAtHead->getRightEyePosition(); + // Randomly look back and forth between look targets + switch (_myAvatar->getEyeContactTarget()) { + case LEFT_EYE: + lookAtSpot = lookingAtHead->getLeftEyePosition(); + break; + case RIGHT_EYE: + lookAtSpot = lookingAtHead->getRightEyePosition(); + break; + case MOUTH: + lookAtSpot = lookingAtHead->getMouthPosition(); + break; } } else { // Just look at their head (mid point between eyes) diff --git a/interface/src/avatar/Head.h b/interface/src/avatar/Head.h index 0b216e4a2e..a053a5bd44 100644 --- a/interface/src/avatar/Head.h +++ b/interface/src/avatar/Head.h @@ -22,11 +22,6 @@ #include "InterfaceConfig.h" #include "world.h" -enum eyeContactTargets { - LEFT_EYE, - RIGHT_EYE, - MOUTH -}; const float EYE_EAR_GAP = 0.08f; @@ -74,6 +69,7 @@ public: const glm::vec3& getLeftEyePosition() const { return _leftEyePosition; } glm::vec3 getRightEarPosition() const { return _rightEyePosition + (getRightDirection() * EYE_EAR_GAP) + (getFrontDirection() * -EYE_EAR_GAP); } glm::vec3 getLeftEarPosition() const { return _leftEyePosition + (getRightDirection() * -EYE_EAR_GAP) + (getFrontDirection() * -EYE_EAR_GAP); } + glm::vec3 getMouthPosition() const { return _eyePosition - getUpDirection() * glm::length(_rightEyePosition - _leftEyePosition); } FaceModel& getFaceModel() { return _faceModel; } const FaceModel& getFaceModel() const { return _faceModel; } diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index f0bba200fe..170fc03d17 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -95,7 +95,7 @@ MyAvatar::MyAvatar() : _shouldRender(true), _billboardValid(false), _feetTouchFloor(true), - _isLookingAtLeftEye(true), + _eyeContactTarget(LEFT_EYE), _realWorldFieldOfView("realWorldFieldOfView", DEFAULT_REAL_WORLD_FIELD_OF_VIEW_DEGREES), _firstPersonSkeletonModel(this), @@ -904,12 +904,24 @@ void MyAvatar::clearLookAtTargetAvatar() { _lookAtTargetAvatar.reset(); } -bool MyAvatar::isLookingAtLeftEye() { - float const CHANCE_OF_CHANGING_EYE = 0.01f; - if (randFloat() < CHANCE_OF_CHANGING_EYE) { - _isLookingAtLeftEye = !_isLookingAtLeftEye; +eyeContactTarget MyAvatar::getEyeContactTarget() { + float const CHANCE_OF_CHANGING_TARGET = 0.01f; + if (randFloat() < CHANCE_OF_CHANGING_TARGET) { + float const FIFTY_FIFTY_CHANCE = 0.5f; + switch (_eyeContactTarget) { + case LEFT_EYE: + _eyeContactTarget = (randFloat() < FIFTY_FIFTY_CHANCE) ? MOUTH : RIGHT_EYE; + break; + case RIGHT_EYE: + _eyeContactTarget = (randFloat() < FIFTY_FIFTY_CHANCE) ? LEFT_EYE : MOUTH; + break; + case MOUTH: + _eyeContactTarget = (randFloat() < FIFTY_FIFTY_CHANCE) ? RIGHT_EYE : LEFT_EYE; + break; + } } - return _isLookingAtLeftEye; + + return _eyeContactTarget; } glm::vec3 MyAvatar::getDefaultEyePosition() const { diff --git a/interface/src/avatar/MyAvatar.h b/interface/src/avatar/MyAvatar.h index 2fea09ee27..daec7d3457 100644 --- a/interface/src/avatar/MyAvatar.h +++ b/interface/src/avatar/MyAvatar.h @@ -19,6 +19,12 @@ class ModelItemID; +enum eyeContactTarget { + LEFT_EYE, + RIGHT_EYE, + MOUTH +}; + class MyAvatar : public Avatar { Q_OBJECT Q_PROPERTY(bool shouldRenderLocally READ getShouldRenderLocally WRITE setShouldRenderLocally) @@ -93,7 +99,7 @@ public: bool isMyAvatar() const { return true; } - bool isLookingAtLeftEye(); + eyeContactTarget getEyeContactTarget(); virtual int parseDataAtOffset(const QByteArray& packet, int offset); @@ -245,7 +251,7 @@ private: QList _animationHandles; bool _feetTouchFloor; - bool _isLookingAtLeftEye; + eyeContactTarget _eyeContactTarget; RecorderPointer _recorder; From 3a92878544669199e2239188ae1080adb7b8e223 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Thu, 2 Jul 2015 13:47:52 -0700 Subject: [PATCH 014/107] Reinstate making someone looking at me look at my camera instead of face --- interface/src/Application.cpp | 9 +++++++++ interface/src/Application.h | 2 ++ interface/src/avatar/FaceModel.cpp | 2 +- interface/src/avatar/Head.cpp | 19 ++++++++++++++++--- interface/src/avatar/Head.h | 5 +++++ interface/src/avatar/MyAvatar.cpp | 6 ++++++ 6 files changed, 39 insertions(+), 4 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index ca264fd42f..01323d3e1d 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2248,6 +2248,15 @@ void Application::shrinkMirrorView() { const float HEAD_SPHERE_RADIUS = 0.1f; +bool Application::isLookingAtMyAvatar(Avatar* avatar) { + glm::vec3 theirLookAt = avatar->getHead()->getLookAtPosition(); + glm::vec3 myEyePosition = _myAvatar->getHead()->getEyePosition(); + if (pointInSphere(theirLookAt, myEyePosition, HEAD_SPHERE_RADIUS * _myAvatar->getScale())) { + return true; + } + return false; +} + void Application::updateLOD() { PerformanceTimer perfTimer("LOD"); // adjust it unless we were asked to disable this feature, or if we're currently in throttleRendering mode diff --git a/interface/src/Application.h b/interface/src/Application.h index 1d3c0dcc70..8dd987fbcd 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -322,6 +322,8 @@ public: QStringList getRunningScripts() { return _scriptEnginesHash.keys(); } ScriptEngine* getScriptEngine(QString scriptHash) { return _scriptEnginesHash.contains(scriptHash) ? _scriptEnginesHash[scriptHash] : NULL; } + bool isLookingAtMyAvatar(Avatar* avatar); + float getRenderResolutionScale() const; int getRenderAmbientLight() const; diff --git a/interface/src/avatar/FaceModel.cpp b/interface/src/avatar/FaceModel.cpp index 7a582406a4..1501c52de5 100644 --- a/interface/src/avatar/FaceModel.cpp +++ b/interface/src/avatar/FaceModel.cpp @@ -77,7 +77,7 @@ void FaceModel::maybeUpdateEyeRotation(Model* model, const JointState& parentSta glm::translate(state.getDefaultTranslationInConstrainedFrame()) * joint.preTransform * glm::mat4_cast(joint.preRotation * joint.rotation)); glm::vec3 front = glm::vec3(inverse * glm::vec4(_owningHead->getFinalOrientationInWorldFrame() * IDENTITY_FRONT, 0.0f)); - glm::vec3 lookAt = glm::vec3(inverse * glm::vec4(_owningHead->getLookAtPosition() + + glm::vec3 lookAt = glm::vec3(inverse * glm::vec4(_owningHead->getCorrectedLookAtPosition() + _owningHead->getSaccade() - model->getTranslation(), 1.0f)); glm::quat between = rotationBetween(front, lookAt); const float MAX_ANGLE = 30.0f * RADIANS_PER_DEGREE; diff --git a/interface/src/avatar/Head.cpp b/interface/src/avatar/Head.cpp index 911bd4f4a4..e5201a8923 100644 --- a/interface/src/avatar/Head.cpp +++ b/interface/src/avatar/Head.cpp @@ -258,7 +258,7 @@ void Head::calculateMouthShapes() { void Head::applyEyelidOffset(glm::quat headOrientation) { // Adjusts the eyelid blendshape coefficients so that the eyelid follows the iris as the head pitches. - glm::quat eyeRotation = rotationBetween(headOrientation * IDENTITY_FRONT, getLookAtPosition() - _eyePosition); + glm::quat eyeRotation = rotationBetween(headOrientation * IDENTITY_FRONT, getCorrectedLookAtPosition() - _eyePosition); eyeRotation = eyeRotation * glm::angleAxis(safeEulerAngles(headOrientation).y, IDENTITY_UP); // Rotation w.r.t. head float eyePitch = safeEulerAngles(eyeRotation).x; @@ -295,8 +295,8 @@ void Head::relaxLean(float deltaTime) { } void Head::render(RenderArgs* renderArgs, float alpha, ViewFrustum* renderFrustum, bool postLighting) { - if (_renderLookatVectors) { - renderLookatVectors(renderArgs, _leftEyePosition, _rightEyePosition, getLookAtPosition()); + if (_renderLookatVectors && _isLookingAtMe) { + renderLookatVectors(renderArgs, _leftEyePosition, _rightEyePosition, getCorrectedLookAtPosition()); } } @@ -315,6 +315,19 @@ glm::quat Head::getFinalOrientationInLocalFrame() const { return glm::quat(glm::radians(glm::vec3(getFinalPitch(), getFinalYaw(), getFinalRoll() ))); } +glm::vec3 Head::getCorrectedLookAtPosition() { + if (_isLookingAtMe) { + return _correctedLookAtPosition; + } else { + return getLookAtPosition(); + } +} + +void Head::setCorrectedLookAtPosition(glm::vec3 correctedLookAtPosition) { + _isLookingAtMe = true; + _correctedLookAtPosition = correctedLookAtPosition; +} + glm::quat Head::getCameraOrientation() const { // NOTE: Head::getCameraOrientation() is not used for orienting the camera "view" while in Oculus mode, so // you may wonder why this code is here. This method will be called while in Oculus mode to determine how diff --git a/interface/src/avatar/Head.h b/interface/src/avatar/Head.h index a053a5bd44..3f839d53bc 100644 --- a/interface/src/avatar/Head.h +++ b/interface/src/avatar/Head.h @@ -51,6 +51,9 @@ public: /// \return orientationBody * orientationBasePitch glm::quat getCameraOrientation () const; + void setCorrectedLookAtPosition(glm::vec3 correctedLookAtPosition); + glm::vec3 getCorrectedLookAtPosition(); + void clearCorrectedLookAtPosition() { _isLookingAtMe = false; } bool getIsLookingAtMe() { return _isLookingAtMe; } float getScale() const { return _scale; } @@ -140,6 +143,8 @@ private: bool _isLookingAtMe; FaceModel _faceModel; + glm::vec3 _correctedLookAtPosition; + int _leftEyeLookAtID; int _rightEyeLookAtID; diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 03b8a56526..9c46edc2d8 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -892,6 +892,12 @@ void MyAvatar::updateLookAtTargetAvatar() { _targetAvatarPosition = avatarPointer->getPosition(); smallestAngleTo = angleTo; } + if (Application::getInstance()->isLookingAtMyAvatar(avatar)) { + // Alter their gaze to look directly at my camera; this looks more natural than looking at my avatar's face + avatar->getHead()->setCorrectedLookAtPosition(Application::getInstance()->getViewFrustum()->getPosition()); + } else { + avatar->getHead()->clearCorrectedLookAtPosition(); + } } } auto avatarPointer = _lookAtTargetAvatar.lock(); From 9efeda9716d68513cb26e1b203a3cae184577ada Mon Sep 17 00:00:00 2001 From: David Rowe Date: Thu, 2 Jul 2015 15:43:23 -0700 Subject: [PATCH 015/107] Adjust gaze target for someone looking at me --- interface/src/Application.cpp | 3 +-- interface/src/avatar/MyAvatar.cpp | 22 +++++++++++++++++++--- interface/src/devices/OculusManager.cpp | 1 + interface/src/devices/OculusManager.h | 1 + 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 01323d3e1d..1a39999301 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -2306,8 +2306,7 @@ void Application::updateMyAvatarLookAtPosition() { if (!OculusManager::isConnected()) { lookAtSpot = _myCamera.getPosition(); } else { - lookAtSpot = _myCamera.getPosition() - + (OculusManager::getLeftEyePosition() + OculusManager::getRightEyePosition()) / 2.0f; + lookAtSpot = _myCamera.getPosition() + OculusManager::getMidEyePosition(); } } else { AvatarSharedPointer lookingAt = _myAvatar->getLookAtTargetAvatar().lock(); diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 9c46edc2d8..c3c8ddcd25 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -34,6 +34,9 @@ #include #include +#include "devices/Faceshift.h" +#include "devices/OculusManager.h" + #include "Application.h" #include "AvatarManager.h" #include "Environment.h" @@ -42,7 +45,6 @@ #include "MyAvatar.h" #include "Physics.h" #include "Recorder.h" -#include "devices/Faceshift.h" #include "Util.h" #include "InterfaceLogging.h" @@ -893,8 +895,22 @@ void MyAvatar::updateLookAtTargetAvatar() { smallestAngleTo = angleTo; } if (Application::getInstance()->isLookingAtMyAvatar(avatar)) { - // Alter their gaze to look directly at my camera; this looks more natural than looking at my avatar's face - avatar->getHead()->setCorrectedLookAtPosition(Application::getInstance()->getViewFrustum()->getPosition()); + // Alter their gaze to look directly at my camera; this looks more natural than looking at my avatar's face. + // Offset their gaze according to whether they're looking at one of my eyes or my mouth. + glm::vec3 gazeOffset = avatar->getHead()->getLookAtPosition() - getHead()->getEyePosition(); + const float HUMAN_EYE_SEPARATION = 0.065f; + float myEyeSeparation = glm::length(getHead()->getLeftEyePosition() - getHead()->getRightEyePosition()); + gazeOffset = gazeOffset * HUMAN_EYE_SEPARATION / myEyeSeparation; + + if (Application::getInstance()->isHMDMode()) { + //avatar->getHead()->setCorrectedLookAtPosition(Application::getInstance()->getCamera()->getPosition() + // + OculusManager::getMidEyePosition() + gazeOffset); + avatar->getHead()->setCorrectedLookAtPosition(Application::getInstance()->getViewFrustum()->getPosition() + + OculusManager::getMidEyePosition() + gazeOffset); + } else { + avatar->getHead()->setCorrectedLookAtPosition(Application::getInstance()->getViewFrustum()->getPosition() + + gazeOffset); + } } else { avatar->getHead()->clearCorrectedLookAtPosition(); } diff --git a/interface/src/devices/OculusManager.cpp b/interface/src/devices/OculusManager.cpp index e5c00a3699..eb19031156 100644 --- a/interface/src/devices/OculusManager.cpp +++ b/interface/src/devices/OculusManager.cpp @@ -283,6 +283,7 @@ static ovrVector3f _eyeOffsets[ovrEye_Count]; glm::vec3 OculusManager::getLeftEyePosition() { return _eyePositions[ovrEye_Left]; } glm::vec3 OculusManager::getRightEyePosition() { return _eyePositions[ovrEye_Right]; } +glm::vec3 OculusManager::getMidEyePosition() { return (_eyePositions[ovrEye_Left] + _eyePositions[ovrEye_Right]) / 2.0f; } void OculusManager::connect(QOpenGLContext* shareContext) { qCDebug(interfaceapp) << "Oculus SDK" << OVR_VERSION_STRING; diff --git a/interface/src/devices/OculusManager.h b/interface/src/devices/OculusManager.h index 9d9f091296..83ecbf0fb7 100644 --- a/interface/src/devices/OculusManager.h +++ b/interface/src/devices/OculusManager.h @@ -47,6 +47,7 @@ public: static glm::vec3 getLeftEyePosition(); static glm::vec3 getRightEyePosition(); + static glm::vec3 getMidEyePosition(); static int getHMDScreen(); From 4019a216d47146b16707081db892d94a9ef6a855 Mon Sep 17 00:00:00 2001 From: samcake Date: Fri, 3 Jul 2015 12:12:54 -0700 Subject: [PATCH 016/107] Fixed the viewport expressed in int not getting to the shader, needed to be convert to float --- libraries/gpu/src/gpu/Context.h | 2 +- libraries/gpu/src/gpu/GLBackendTransform.cpp | 2 +- libraries/model/src/model/Skybox.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/gpu/src/gpu/Context.h b/libraries/gpu/src/gpu/Context.h index da4882ab65..2207575274 100644 --- a/libraries/gpu/src/gpu/Context.h +++ b/libraries/gpu/src/gpu/Context.h @@ -42,7 +42,7 @@ public: Mat4 _projectionViewUntranslated; Mat4 _projection; Mat4 _projectionInverse; - Vec4i _viewport; + Vec4 _viewport; // Public value is int but float in the shader to stay in floats for all the transform computations. }; template< typename T > diff --git a/libraries/gpu/src/gpu/GLBackendTransform.cpp b/libraries/gpu/src/gpu/GLBackendTransform.cpp index 0d47bc4f33..5f4d5f4af6 100755 --- a/libraries/gpu/src/gpu/GLBackendTransform.cpp +++ b/libraries/gpu/src/gpu/GLBackendTransform.cpp @@ -86,7 +86,7 @@ void GLBackend::updateTransform() { glGetIntegerv(GL_MATRIX_MODE, &originalMatrixMode); // Check all the dirty flags and update the state accordingly if (_transform._invalidViewport) { - _transform._transformCamera._viewport = _transform._viewport; + _transform._transformCamera._viewport = glm::vec4(_transform._viewport); // Where we assign the GL viewport glViewport(_transform._viewport.x, _transform._viewport.y, _transform._viewport.z, _transform._viewport.w); diff --git a/libraries/model/src/model/Skybox.cpp b/libraries/model/src/model/Skybox.cpp index 10199adda3..a02c646668 100755 --- a/libraries/model/src/model/Skybox.cpp +++ b/libraries/model/src/model/Skybox.cpp @@ -51,7 +51,7 @@ void Skybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const Sky static gpu::BufferPointer theBuffer; static gpu::Stream::FormatPointer theFormat; static gpu::BufferPointer theConstants; - int SKYBOX_CONSTANTS_SLOT = 0; // need to be defined by the compilation of the shader + static int SKYBOX_CONSTANTS_SLOT = 0; // need to be defined by the compilation of the shader if (!thePipeline) { auto skyVS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(Skybox_vert))); auto skyFS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(Skybox_frag))); From 3f38a835d830a0adab8d444190ef1801c4176b65 Mon Sep 17 00:00:00 2001 From: David Rowe Date: Fri, 3 Jul 2015 13:49:10 -0700 Subject: [PATCH 017/107] Fix look-at vectors not rendering if not looking at me --- interface/src/avatar/Head.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/src/avatar/Head.cpp b/interface/src/avatar/Head.cpp index e5201a8923..98126c3a22 100644 --- a/interface/src/avatar/Head.cpp +++ b/interface/src/avatar/Head.cpp @@ -295,7 +295,7 @@ void Head::relaxLean(float deltaTime) { } void Head::render(RenderArgs* renderArgs, float alpha, ViewFrustum* renderFrustum, bool postLighting) { - if (_renderLookatVectors && _isLookingAtMe) { + if (_renderLookatVectors) { renderLookatVectors(renderArgs, _leftEyePosition, _rightEyePosition, getCorrectedLookAtPosition()); } } From a2f6693bd7c2b6561ff5dda1cbd02c2e086708b7 Mon Sep 17 00:00:00 2001 From: samcake Date: Mon, 6 Jul 2015 14:40:10 -0700 Subject: [PATCH 018/107] getting the deferred lighting to work --- libraries/gpu/src/gpu/GLBackend.h | 2 +- libraries/gpu/src/gpu/GLBackendState.cpp | 18 +- libraries/gpu/src/gpu/State.cpp | 50 ++-- libraries/gpu/src/gpu/State.h | 10 +- .../src/DeferredLightingEffect.cpp | 220 +++++------------- 5 files changed, 99 insertions(+), 201 deletions(-) diff --git a/libraries/gpu/src/gpu/GLBackend.h b/libraries/gpu/src/gpu/GLBackend.h index 765858dcf4..78b0f0838e 100644 --- a/libraries/gpu/src/gpu/GLBackend.h +++ b/libraries/gpu/src/gpu/GLBackend.h @@ -199,7 +199,7 @@ public: void do_setStateFillMode(int32 mode); void do_setStateCullMode(int32 mode); void do_setStateFrontFaceClockwise(bool isClockwise); - void do_setStateDepthClipEnable(bool enable); + void do_setStateDepthClampEnable(bool enable); void do_setStateScissorEnable(bool enable); void do_setStateMultisampleEnable(bool enable); void do_setStateAntialiasedLineEnable(bool enable); diff --git a/libraries/gpu/src/gpu/GLBackendState.cpp b/libraries/gpu/src/gpu/GLBackendState.cpp index ef272bb708..4c25a1c6fd 100644 --- a/libraries/gpu/src/gpu/GLBackendState.cpp +++ b/libraries/gpu/src/gpu/GLBackendState.cpp @@ -51,7 +51,7 @@ const GLBackend::GLState::Commands makeResetStateCommands() { CommandPointer(new Command1I(&GLBackend::do_setStateFillMode, DEFAULT.fillMode)), CommandPointer(new Command1I(&GLBackend::do_setStateCullMode, DEFAULT.cullMode)), CommandPointer(new Command1B(&GLBackend::do_setStateFrontFaceClockwise, DEFAULT.frontFaceClockwise)), - CommandPointer(new Command1B(&GLBackend::do_setStateDepthClipEnable, DEFAULT.depthClipEnable)), + CommandPointer(new Command1B(&GLBackend::do_setStateDepthClampEnable, DEFAULT.depthClampEnable)), CommandPointer(new Command1B(&GLBackend::do_setStateScissorEnable, DEFAULT.scissorEnable)), CommandPointer(new Command1B(&GLBackend::do_setStateMultisampleEnable, DEFAULT.multisampleEnable)), CommandPointer(new Command1B(&GLBackend::do_setStateAntialiasedLineEnable, DEFAULT.antialisedLineEnable)), @@ -89,8 +89,8 @@ void generateFrontFaceClockwise(GLBackend::GLState::Commands& commands, bool isC commands.push_back(CommandPointer(new Command1B(&GLBackend::do_setStateFrontFaceClockwise, isClockwise))); } -void generateDepthClipEnable(GLBackend::GLState::Commands& commands, bool enable) { - commands.push_back(CommandPointer(new Command1B(&GLBackend::do_setStateDepthClipEnable, enable))); +void generateDepthClampEnable(GLBackend::GLState::Commands& commands, bool enable) { + commands.push_back(CommandPointer(new Command1B(&GLBackend::do_setStateDepthClampEnable, enable))); } void generateScissorEnable(GLBackend::GLState::Commands& commands, bool enable) { @@ -176,8 +176,8 @@ GLBackend::GLState* GLBackend::syncGPUObject(const State& state) { generateFrontFaceClockwise(object->_commands, state.isFrontFaceClockwise()); break; } - case State::DEPTH_CLIP_ENABLE: { - generateDepthClipEnable(object->_commands, state.isDepthClipEnable()); + case State::DEPTH_CLAMP_ENABLE: { + generateDepthClampEnable(object->_commands, state.isDepthClampEnable()); break; } case State::SCISSOR_ENABLE: { @@ -373,7 +373,7 @@ void GLBackend::getCurrentGLState(State::Data& state) { GLint winding; glGetIntegerv(GL_FRONT_FACE, &winding); state.frontFaceClockwise = (winding == GL_CW); - state.depthClipEnable = glIsEnabled(GL_DEPTH_CLAMP); + state.depthClampEnable = glIsEnabled(GL_DEPTH_CLAMP); state.scissorEnable = glIsEnabled(GL_SCISSOR_TEST); state.multisampleEnable = glIsEnabled(GL_MULTISAMPLE); state.antialisedLineEnable = glIsEnabled(GL_LINE_SMOOTH); @@ -533,8 +533,8 @@ void GLBackend::do_setStateFrontFaceClockwise(bool isClockwise) { } } -void GLBackend::do_setStateDepthClipEnable(bool enable) { - if (_pipeline._stateCache.depthClipEnable != enable) { +void GLBackend::do_setStateDepthClampEnable(bool enable) { + if (_pipeline._stateCache.depthClampEnable != enable) { if (enable) { glEnable(GL_DEPTH_CLAMP); } else { @@ -542,7 +542,7 @@ void GLBackend::do_setStateDepthClipEnable(bool enable) { } (void) CHECK_GL_ERROR(); - _pipeline._stateCache.depthClipEnable = enable; + _pipeline._stateCache.depthClampEnable = enable; } } diff --git a/libraries/gpu/src/gpu/State.cpp b/libraries/gpu/src/gpu/State.cpp index ca254626e9..da3ab20c7b 100755 --- a/libraries/gpu/src/gpu/State.cpp +++ b/libraries/gpu/src/gpu/State.cpp @@ -24,20 +24,20 @@ State::~State() { // Please make sure to go check makeResetStateCommands() before modifying this value const State::Data State::DEFAULT = State::Data(); -State::Signature State::evalSignature(const Data& state) { +State::Signature State::evalSignature(const Data& state) { Signature signature(0); if (state.fillMode != State::DEFAULT.fillMode) { signature.set(State::FILL_MODE); - } + } if (state.cullMode != State::DEFAULT.cullMode) { signature.set(State::CULL_MODE); } if (state.frontFaceClockwise != State::DEFAULT.frontFaceClockwise) { signature.set(State::FRONT_FACE_CLOCKWISE); } - if (state.depthClipEnable != State::DEFAULT.depthClipEnable) { - signature.set(State::DEPTH_CLIP_ENABLE); + if (state.depthClampEnable != State::DEFAULT.depthClampEnable) { + signature.set(State::DEPTH_CLAMP_ENABLE); } if (state.scissorEnable != State::DEFAULT.scissorEnable) { signature.set(State::SCISSOR_ENABLE); @@ -47,16 +47,16 @@ State::Signature State::evalSignature(const Data& state) { } if (state.antialisedLineEnable != State::DEFAULT.antialisedLineEnable) { signature.set(State::ANTIALISED_LINE_ENABLE); - } + } if (state.depthBias != State::DEFAULT.depthBias) { signature.set(State::DEPTH_BIAS); } if (state.depthBiasSlopeScale != State::DEFAULT.depthBiasSlopeScale) { signature.set(State::DEPTH_BIAS_SLOPE_SCALE); - } + } if (state.depthTest != State::DEFAULT.depthTest) { signature.set(State::DEPTH_TEST); - } + } if (state.stencilActivation != State::DEFAULT.stencilActivation) { signature.set(State::STENCIL_ACTIVATION); } @@ -68,21 +68,21 @@ State::Signature State::evalSignature(const Data& state) { } if (state.sampleMask != State::DEFAULT.sampleMask) { signature.set(State::SAMPLE_MASK); - } - if (state.alphaToCoverageEnable != State::DEFAULT.alphaToCoverageEnable) { - signature.set(State::ALPHA_TO_COVERAGE_ENABLE); - } - if (state.blendFunction != State::DEFAULT.blendFunction) { - signature.set(State::BLEND_FUNCTION); - } - if (state.colorWriteMask != State::DEFAULT.colorWriteMask) { - signature.set(State::COLOR_WRITE_MASK); - } - - return signature; -} - -State::State(const Data& values) : - _values(values) { - _signature = evalSignature(_values); -} + } + if (state.alphaToCoverageEnable != State::DEFAULT.alphaToCoverageEnable) { + signature.set(State::ALPHA_TO_COVERAGE_ENABLE); + } + if (state.blendFunction != State::DEFAULT.blendFunction) { + signature.set(State::BLEND_FUNCTION); + } + if (state.colorWriteMask != State::DEFAULT.colorWriteMask) { + signature.set(State::COLOR_WRITE_MASK); + } + + return signature; +} + +State::State(const Data& values) : + _values(values) { + _signature = evalSignature(_values); +} diff --git a/libraries/gpu/src/gpu/State.h b/libraries/gpu/src/gpu/State.h index ffd0793d54..39cad1445f 100755 --- a/libraries/gpu/src/gpu/State.h +++ b/libraries/gpu/src/gpu/State.h @@ -249,7 +249,7 @@ public: uint8 colorWriteMask = WRITE_ALL; bool frontFaceClockwise : 1; - bool depthClipEnable : 1; + bool depthClampEnable : 1; bool scissorEnable : 1; bool multisampleEnable : 1; bool antialisedLineEnable : 1; @@ -257,7 +257,7 @@ public: Data() : frontFaceClockwise(false), - depthClipEnable(false), + depthClampEnable(false), scissorEnable(false), multisampleEnable(false), antialisedLineEnable(false), @@ -276,8 +276,8 @@ public: void setFrontFaceClockwise(bool isClockwise) { SET_FIELD(FRONT_FACE_CLOCKWISE, DEFAULT.frontFaceClockwise, isClockwise, _values.frontFaceClockwise); } bool isFrontFaceClockwise() const { return _values.frontFaceClockwise; } - void setDepthClipEnable(bool enable) { SET_FIELD(DEPTH_CLIP_ENABLE, DEFAULT.depthClipEnable, enable, _values.depthClipEnable); } - bool isDepthClipEnable() const { return _values.depthClipEnable; } + void setDepthClampEnable(bool enable) { SET_FIELD(DEPTH_CLAMP_ENABLE, DEFAULT.depthClampEnable, enable, _values.depthClampEnable); } + bool isDepthClampEnable() const { return _values.depthClampEnable; } void setScissorEnable(bool enable) { SET_FIELD(SCISSOR_ENABLE, DEFAULT.scissorEnable, enable, _values.scissorEnable); } bool isScissorEnable() const { return _values.scissorEnable; } @@ -349,7 +349,7 @@ public: FILL_MODE, CULL_MODE, FRONT_FACE_CLOCKWISE, - DEPTH_CLIP_ENABLE, + DEPTH_CLAMP_ENABLE, SCISSOR_ENABLE, MULTISAMPLE_ENABLE, ANTIALISED_LINE_ENABLE, diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index db7eb755b1..30decd3c7e 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -497,122 +497,73 @@ void DeferredLightingEffect::render(RenderArgs* args) { batch._glUniform1f(_pointLightLocations.depthScale, depthScale); batch._glUniform2f(_pointLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); batch._glUniform2f(_pointLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); - - //_pointLight.bind(); - //_pointLight.setUniformValue(_pointLightLocations.nearLocation, nearVal); - //_pointLight.setUniformValue(_pointLightLocations.depthScale, depthScale); - //_pointLight.setUniformValue(_pointLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); - //_pointLight.setUniformValue(_pointLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); - + batch._glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); - //glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); batch._glUniformMatrix4fv(_pointLightLocations.texcoordMat, 1, false, reinterpret_cast< const GLfloat* >(&texcoordMat)); for (auto lightID : _pointLights) { auto& light = _allocatedLights[lightID]; light->setShowContour(true); if (_pointLightLocations.lightBufferUnit >= 0) { - // gpu::Batch batch; batch.setUniformBuffer(_pointLightLocations.lightBufferUnit, light->getSchemaBuffer()); - // gpu::GLBackend::renderBatch(batch); } - - // glPushMatrix(); - float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius) { - - /* glLoadIdentity(); - glTranslatef(0.0f, 0.0f, -1.0f); - - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - */ - // renderFullscreenQuad(); Transform model; model.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f)); batch.setModelTransform(model); + batch.setViewTransform(Transform()); + batch.setProjectionTransform(glm::mat4()); - { - glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f); - glm::vec2 topLeft(-1.0f, -1.0f); - glm::vec2 bottomRight(1.0f, 1.0f); - glm::vec2 texCoordTopLeft(sMin, tMin); - glm::vec2 texCoordBottomRight(sMin + sWidth, tMin + tHeight); + glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f); + glm::vec2 topLeft(-1.0f, -1.0f); + glm::vec2 bottomRight(1.0f, 1.0f); + glm::vec2 texCoordTopLeft(sMin, tMin); + glm::vec2 texCoordBottomRight(sMin + sWidth, tMin + tHeight); - DependencyManager::get()->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color); - } - /* - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); - */ + DependencyManager::get()->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color); + + batch.setProjectionTransform(projMat); + batch.setViewTransform(viewMat); } else { Transform model; model.setTranslation(glm::vec3(light->getPosition().x, light->getPosition().y, light->getPosition().z)); batch.setModelTransform(model); - // glTranslatef(light->getPosition().x, light->getPosition().y, light->getPosition().z); - - // geometryCache->renderSphere(expandedRadius, 32, 32, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); geometryCache->renderSphere(batch, expandedRadius, 32, 32, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); } - - // glPopMatrix(); } _pointLights.clear(); - - // _pointLight.release(); } if (!_spotLights.empty()) { - batch.setPipeline(_pointLight); + batch.setPipeline(_spotLight); batch._glUniform1f(_spotLightLocations.nearLocation, nearVal); batch._glUniform1f(_spotLightLocations.depthScale, depthScale); batch._glUniform2f(_spotLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); batch._glUniform2f(_spotLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); - -/* _spotLight.bind(); - _spotLight.setUniformValue(_spotLightLocations.nearLocation, nearVal); - _spotLight.setUniformValue(_spotLightLocations.depthScale, depthScale); - _spotLight.setUniformValue(_spotLightLocations.depthTexCoordOffset, depthTexCoordOffsetS, depthTexCoordOffsetT); - _spotLight.setUniformValue(_spotLightLocations.depthTexCoordScale, depthTexCoordScaleS, depthTexCoordScaleT); - */ - - + batch._glUniformMatrix4fv(_spotLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); - //glUniformMatrix4fv(_pointLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); batch._glUniformMatrix4fv(_spotLightLocations.texcoordMat, 1, false, reinterpret_cast< const GLfloat* >(&texcoordMat)); for (auto lightID : _spotLights) { auto light = _allocatedLights[lightID]; - + light->setShowContour(true); if (_spotLightLocations.lightBufferUnit >= 0) { - // gpu::Batch batch; batch.setUniformBuffer(_spotLightLocations.lightBufferUnit, light->getSchemaBuffer()); - // gpu::GLBackend::renderBatch(batch); } - // glUniformMatrix4fv(_spotLightLocations.invViewMat, 1, false, reinterpret_cast< const GLfloat* >(&invViewMat)); - - // glPushMatrix(); float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); float edgeRadius = expandedRadius / glm::cos(light->getSpotAngle()); if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < edgeRadius + nearRadius) { - /*glLoadIdentity(); - glTranslatef(0.0f, 0.0f, -1.0f); - - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - - renderFullscreenQuad();*/ + Transform model; model.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f)); batch.setModelTransform(model); - + batch.setViewTransform(Transform()); + batch.setProjectionTransform(glm::mat4()); { glm::vec4 color(1.0f, 1.0f, 1.0f, 1.0f); glm::vec2 topLeft(-1.0f, -1.0f); @@ -621,10 +572,10 @@ void DeferredLightingEffect::render(RenderArgs* args) { glm::vec2 texCoordBottomRight(sMin + sWidth, tMin + tHeight); DependencyManager::get()->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, color); + + batch.setProjectionTransform(projMat); + batch.setViewTransform(viewMat); } - - /* glPopMatrix(); - glMatrixMode(GL_MODELVIEW);*/ } else { Transform model; @@ -634,7 +585,6 @@ void DeferredLightingEffect::render(RenderArgs* args) { glm::vec3 axis = glm::axis(spotRotation); model.postRotate(spotRotation); - model.postTranslate(glm::vec3(0.0f, 0.0f, -light->getMaximumRadius() * (1.0f + SCALE_EXPANSION * 0.5f))); float base = expandedRadius * glm::tan(light->getSpotAngle()); float height = expandedRadius; @@ -648,43 +598,14 @@ void DeferredLightingEffect::render(RenderArgs* args) { batch.setInputBuffer(0, mesh->getVertexBuffer()); batch.setInputFormat(mesh->getVertexFormat()); + auto& part = mesh->getPartBuffer().get(); - auto& part = mesh->getPartBuffer().get(); - - batch.drawIndexed(model::Mesh::topologyToPrimitive(part._topology), part._numIndices, part._startIndex); - - - //geometryCache->renderCone(batch, expandedRadius * glm::tan(light->getSpotAngle()), expandedRadius, 32, 1); - - /* - glTranslatef(light->getPosition().x, light->getPosition().y, light->getPosition().z); - glm::quat spotRotation = rotationBetween(glm::vec3(0.0f, 0.0f, -1.0f), light->getDirection()); - glm::vec3 axis = glm::axis(spotRotation); - glRotatef(glm::degrees(glm::angle(spotRotation)), axis.x, axis.y, axis.z); - glTranslatef(0.0f, 0.0f, -light->getMaximumRadius() * (1.0f + SCALE_EXPANSION * 0.5f)); - geometryCache->renderCone(expandedRadius * glm::tan(light->getSpotAngle()), expandedRadius, 32, 1); - */ + batch.drawIndexed(model::Mesh::topologyToPrimitive(part._topology), part._numIndices, part._startIndex); } - - glPopMatrix(); } _spotLights.clear(); - - // _spotLight.release(); } - -/* - glBindTexture(GL_TEXTURE_2D, 0); - - glActiveTexture(GL_TEXTURE2); - glBindTexture(GL_TEXTURE_2D, 0); - glActiveTexture(GL_TEXTURE1); - glBindTexture(GL_TEXTURE_2D, 0); - - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, 0); -*/ // Probably not necessary in the long run because the gpu layer would unbound this texture if used as render target batch.setUniformTexture(0, nullptr); batch.setUniformTexture(1, nullptr); @@ -893,7 +814,7 @@ void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool light gpu::StatePointer state = gpu::StatePointer(new gpu::State()); if (lightVolume) { state->setCullMode(gpu::State::CULL_BACK); - + // additive blending state->setBlendFunction(true, gpu::State::ONE, gpu::State::BLEND_OP_ADD, gpu::State::ONE); } else { @@ -930,79 +851,56 @@ model::MeshPointer DeferredLightingEffect::getSpotLightMesh() { if (!_spotLightMesh) { _spotLightMesh.reset(new model::Mesh()); - int slices = 32; - int stacks = 1; - int vertices = (stacks + 2) * slices; - int baseTriangles = slices - 2; - int indices = 6 * slices * stacks + 3 * baseTriangles; + int slices = 16; + int vertices = 2 + slices; + int originVertex = vertices - 2; + int capVertex = vertices - 1; + int verticesSize = vertices * 3 * sizeof(float); + int indices = 3 * slices * 2; - GLfloat* vertexData = new GLfloat[vertices * 3]; + GLfloat* vertexData = new GLfloat[verticesSize]; GLfloat* vertex = vertexData; - // cap + for (int i = 0; i < slices; i++) { float theta = TWO_PI * i / slices; - //normals - /* *(vertex++) = 0.0f; - *(vertex++) = 0.0f; - *(vertex++) = -1.0f; - */ - // vertices *(vertex++) = cosf(theta); *(vertex++) = sinf(theta); - *(vertex++) = 0.0f; + *(vertex++) = -1.0f; } - // body - for (int i = 0; i <= stacks; i++) { - float z = (float)i / stacks; - float radius = 1.0f - z; - - for (int j = 0; j < slices; j++) { - float theta = TWO_PI * j / slices; - - //normals - /* *(vertex++) = cosf(theta) / SQUARE_ROOT_OF_2; - *(vertex++) = sinf(theta) / SQUARE_ROOT_OF_2; - *(vertex++) = 1.0f / SQUARE_ROOT_OF_2; - */ - // vertices - *(vertex++) = radius * cosf(theta); - *(vertex++) = radius * sinf(theta); - *(vertex++) = z; - } - } - - _spotLightMesh->setVertexBuffer(gpu::BufferView(new gpu::Buffer(sizeof(GLfloat) * vertices, (gpu::Byte*) vertexData), gpu::Element::VEC3F_XYZ)); + + *(vertex++) = 0.0f; + *(vertex++) = 0.0f; + *(vertex++) = 0.0f; + + *(vertex++) = 0.0f; + *(vertex++) = 0.0f; + *(vertex++) = -1.0f; + + _spotLightMesh->setVertexBuffer(gpu::BufferView(new gpu::Buffer(verticesSize, (gpu::Byte*) vertexData), gpu::Element::VEC3F_XYZ)); delete[] vertexData; - - GLushort* indexData = new GLushort[indices]; - GLushort* index = indexData; - for (int i = 0; i < baseTriangles; i++) { - *(index++) = 0; - *(index++) = i + 2; - *(index++) = i + 1; - } - for (int i = 1; i <= stacks; i++) { - GLushort bottom = i * slices; - GLushort top = bottom + slices; - for (int j = 0; j < slices; j++) { - int next = (j + 1) % slices; - - *(index++) = bottom + j; - *(index++) = top + next; - *(index++) = top + j; - - *(index++) = bottom + j; - *(index++) = bottom + next; - *(index++) = top + next; - } + gpu::uint16* indexData = new gpu::uint16[indices]; + gpu::uint16* index = indexData; + for (int i = 0; i < slices; i++) { + *(index++) = originVertex; + + int s0 = i; + int s1 = ((i + 1) % slices); + *(index++) = s0; + *(index++) = s1; + *(index++) = s1; + *(index++) = s0; + + *(index++) = capVertex; } _spotLightMesh->setIndexBuffer(gpu::BufferView(new gpu::Buffer(sizeof(GLushort) * indices, (gpu::Byte*) indexData), gpu::Element::INDEX_UINT16)); delete[] indexData; - model::Mesh::Part part(0, vertices - 1, 0, model::Mesh::TRIANGLES); + model::Mesh::Part part(0, indices, 0, model::Mesh::TRIANGLES); + //model::Mesh::Part part(0, indices, 0, model::Mesh::LINE_STRIP); + _spotLightMesh->setPartBuffer(gpu::BufferView(new gpu::Buffer(sizeof(part), (gpu::Byte*) &part), gpu::Element::PART_DRAWCALL)); From 9649fe45e4ea9d1d98ed9ee3068eb657d24e15bb Mon Sep 17 00:00:00 2001 From: samcake Date: Mon, 6 Jul 2015 15:45:26 -0700 Subject: [PATCH 019/107] fix a bad paintRainbow function in the shader... --- libraries/render/src/render/drawItemStatus.slv | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/libraries/render/src/render/drawItemStatus.slv b/libraries/render/src/render/drawItemStatus.slv index 9e2b4919ff..106a5684bb 100644 --- a/libraries/render/src/render/drawItemStatus.slv +++ b/libraries/render/src/render/drawItemStatus.slv @@ -22,10 +22,10 @@ uniform vec3 inBoundPos; uniform vec3 inBoundDim; uniform ivec4 inStatus; -vec3 paintRainbow(float nv) { - float v = nv * 5.f; +vec3 paintRainbow(float normalizedHue) { + float v = normalizedHue * 6.f; if (v < 0.f) { - return vec3(0.f, 0.f, 0.f); + return vec3(1.f, 0.f, 0.f); } else if (v < 1.f) { return vec3(1.f, v, 0.f); } else if (v < 2.f) { @@ -36,8 +36,10 @@ vec3 paintRainbow(float nv) { return vec3(0.f, 1.f - (v-3.f), 1.f ); } else if (v < 5.f) { return vec3((v-4.f), 0.f, 1.f ); + } else if (v < 6.f) { + return vec3(1.f, 0.f, 1.f - (v-5.f)); } else { - return vec3(1.f, 1.f, 1.f); + return vec3(1.f, 0.f, 0.f); } } From a59fd4401473a9f059c38a25eece73c3da8bc42f Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 7 Jul 2015 09:04:20 -0700 Subject: [PATCH 020/107] quiet some log spam --- libraries/entities/src/EntityTree.cpp | 4 ---- 1 file changed, 4 deletions(-) diff --git a/libraries/entities/src/EntityTree.cpp b/libraries/entities/src/EntityTree.cpp index 3d7fcd8ce5..a951b2bf4f 100644 --- a/libraries/entities/src/EntityTree.cpp +++ b/libraries/entities/src/EntityTree.cpp @@ -92,13 +92,11 @@ void EntityTree::postAddEntity(EntityItemPointer entity) { bool EntityTree::updateEntity(const EntityItemID& entityID, const EntityItemProperties& properties, const SharedNodePointer& senderNode) { EntityTreeElement* containingElement = getContainingElement(entityID); if (!containingElement) { - qCDebug(entities) << "UNEXPECTED!!!! EntityTree::updateEntity() entityID doesn't exist!!! entityID=" << entityID; return false; } EntityItemPointer existingEntity = containingElement->getEntityWithEntityItemID(entityID); if (!existingEntity) { - qCDebug(entities) << "UNEXPECTED!!!! don't call updateEntity() on entity items that don't exist. entityID=" << entityID; return false; } @@ -108,8 +106,6 @@ bool EntityTree::updateEntity(const EntityItemID& entityID, const EntityItemProp bool EntityTree::updateEntity(EntityItemPointer entity, const EntityItemProperties& properties, const SharedNodePointer& senderNode) { EntityTreeElement* containingElement = getContainingElement(entity->getEntityItemID()); if (!containingElement) { - qCDebug(entities) << "UNEXPECTED!!!! EntityTree::updateEntity() entity-->element lookup failed!!! entityID=" - << entity->getEntityItemID(); return false; } return updateEntityWithElement(entity, properties, containingElement, senderNode); From 0a389a9daa628dbd2d28b9c8139da5b25920a91d Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Tue, 7 Jul 2015 11:11:52 -0700 Subject: [PATCH 021/107] Updating oglplus version and switching to url based dist --- cmake/externals/oglplus/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/externals/oglplus/CMakeLists.txt b/cmake/externals/oglplus/CMakeLists.txt index a0b91739f6..43730129a0 100644 --- a/cmake/externals/oglplus/CMakeLists.txt +++ b/cmake/externals/oglplus/CMakeLists.txt @@ -4,8 +4,8 @@ string(TOUPPER ${EXTERNAL_NAME} EXTERNAL_NAME_UPPER) include(ExternalProject) ExternalProject_Add( ${EXTERNAL_NAME} - GIT_REPOSITORY https://github.com/matus-chochlik/oglplus.git - GIT_TAG a2681383928b1166f176512cbe0f95e96fe68d08 + URL http://iweb.dl.sourceforge.net/project/oglplus/oglplus-0.63.x/oglplus-0.63.0.zip + URL_MD5 de984ab245b185b45c87415c0e052135 CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND "" From 1c2972bd7c5f2b0a037c4b9c033135168523566f Mon Sep 17 00:00:00 2001 From: Howard Stearns Date: Tue, 7 Jul 2015 11:49:55 -0700 Subject: [PATCH 022/107] Expose avatar collisions to scripts, and include velocityChange in exposed collision data. --- interface/src/avatar/AvatarManager.cpp | 2 +- interface/src/avatar/MyAvatar.h | 1 + libraries/shared/src/RegisteredMetaTypes.cpp | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/interface/src/avatar/AvatarManager.cpp b/interface/src/avatar/AvatarManager.cpp index dbd46cbfbd..944f16fd34 100644 --- a/interface/src/avatar/AvatarManager.cpp +++ b/interface/src/avatar/AvatarManager.cpp @@ -256,7 +256,6 @@ void AvatarManager::handleOutgoingChanges(VectorOfMotionStates& motionStates) { } void AvatarManager::handleCollisionEvents(CollisionEvents& collisionEvents) { - // TODO: expose avatar collision events to JS for (Collision collision : collisionEvents) { // TODO: Current physics uses null idA or idB for non-entities. The plan is to handle MOTIONSTATE_TYPE_AVATAR, // and then MOTIONSTATE_TYPE_MYAVATAR. As it is, this code only covers the case of my avatar (in which case one @@ -285,6 +284,7 @@ void AvatarManager::handleCollisionEvents(CollisionEvents& collisionEvents) { const float AVATAR_STRETCH_FACTOR = 1.0f; AudioInjector::playSound(collisionSoundURL, energyFactorOfFull, AVATAR_STRETCH_FACTOR, myAvatar->getPosition()); + myAvatar->collisionWithEntity(collision); } } } diff --git a/interface/src/avatar/MyAvatar.h b/interface/src/avatar/MyAvatar.h index 8410665ed5..f77eb81060 100644 --- a/interface/src/avatar/MyAvatar.h +++ b/interface/src/avatar/MyAvatar.h @@ -209,6 +209,7 @@ public slots: signals: void transformChanged(); void newCollisionSoundURL(const QUrl& url); + void collisionWithEntity(const Collision& collision); private: diff --git a/libraries/shared/src/RegisteredMetaTypes.cpp b/libraries/shared/src/RegisteredMetaTypes.cpp index 62f2be0512..dce31b2971 100644 --- a/libraries/shared/src/RegisteredMetaTypes.cpp +++ b/libraries/shared/src/RegisteredMetaTypes.cpp @@ -229,6 +229,7 @@ QScriptValue collisionToScriptValue(QScriptEngine* engine, const Collision& coll obj.setProperty("idB", quuidToScriptValue(engine, collision.idB)); obj.setProperty("penetration", vec3toScriptValue(engine, collision.penetration)); obj.setProperty("contactPoint", vec3toScriptValue(engine, collision.contactPoint)); + obj.setProperty("velocityChange", vec3toScriptValue(engine, collision.velocityChange)); return obj; } From 11e52c8ae034bfc02390ca213a8ba075b93c6451 Mon Sep 17 00:00:00 2001 From: Howard Stearns Date: Tue, 7 Jul 2015 14:41:03 -0700 Subject: [PATCH 023/107] Baseline versions of fight club scripts. --- examples/example/games/make-dummy.js | 70 ++++++++++++++++++ examples/example/games/sword.js | 104 +++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 examples/example/games/make-dummy.js create mode 100644 examples/example/games/sword.js diff --git a/examples/example/games/make-dummy.js b/examples/example/games/make-dummy.js new file mode 100644 index 0000000000..068a8b7f9a --- /dev/null +++ b/examples/example/games/make-dummy.js @@ -0,0 +1,70 @@ +// +// make-dummy.js +// examples +// +// Created by Seth Alves on 2015-6-10 +// Copyright 2015 High Fidelity, Inc. +// +// Makes a boxing-dummy that responds to collisions. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +// +"use strict"; +/*jslint vars: true*/ +var Overlays, Entities, Controller, Script, MyAvatar, Vec3; // Referenced globals provided by High Fidelity. + +var HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; + +var rezButton = Overlays.addOverlay("image", { + x: 100, + y: 350, + width: 32, + height: 32, + imageURL: HIFI_PUBLIC_BUCKET + "images/close.png", + color: { + red: 255, + green: 255, + blue: 255 + }, + alpha: 1 +}); + + +function mousePressEvent(event) { + var clickedOverlay = Overlays.getOverlayAtPoint({ + x: event.x, + y: event.y + }); + + if (clickedOverlay === rezButton) { + var boxId; + + var position = Vec3.sum(MyAvatar.position, {x: 1.0, y: 0.4, z: 0.0}); + boxId = Entities.addEntity({ + type: "Box", + name: "dummy", + position: position, + dimensions: {x: 0.3, y: 0.7, z: 0.3}, + gravity: {x: 0.0, y: -3.0, z: 0.0}, + damping: 0.2, + collisionsWillMove: true + }); + + var pointToOffsetFrom = Vec3.sum(position, {x: 0.0, y: 2.0, z: 0.0}); + Entities.addAction("offset", boxId, {pointToOffsetFrom: pointToOffsetFrom, + linearDistance: 2.0, + // linearTimeScale: 0.005 + linearTimeScale: 0.1 + }); + } +} + + +function scriptEnding() { + Overlays.deleteOverlay(rezButton); +} + +Controller.mousePressEvent.connect(mousePressEvent); +Script.scriptEnding.connect(scriptEnding); diff --git a/examples/example/games/sword.js b/examples/example/games/sword.js new file mode 100644 index 0000000000..9592c275f6 --- /dev/null +++ b/examples/example/games/sword.js @@ -0,0 +1,104 @@ +// stick.js +// examples +// +// Created by Seth Alves on 2015-6-10 +// Copyright 2015 High Fidelity, Inc. +// +// Allow avatar to hold a stick +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +"use strict"; +/*jslint vars: true*/ +var Script, Entities, MyAvatar, Window, Controller, Vec3, Quat; // Referenced globals provided by High Fidelity. + +var hand = "right"; +var nullActionID = "00000000-0000-0000-0000-000000000000"; +var controllerID; +var controllerActive; +var stickID = null; +var actionID = nullActionID; +var dimensions = { x: 0.3, y: 0.1, z: 2.0 }; + +var stickModel = "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx"; +var swordModel = "https://hifi-public.s3.amazonaws.com/ozan/props/sword/sword.fbx"; +var whichModel = "sword"; + +// sometimes if this is run immediately the stick doesn't get created? use a timer. +Script.setTimeout(function () { + stickID = Entities.addEntity({ + type: "Model", + modelURL: (whichModel === "sword") ? swordModel : stickModel, + //compoundShapeURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.obj", + shapeType: "box", + dimensions: dimensions, + position: MyAvatar.getRightPalmPosition(), // initial position doesn't matter, as long as it's close + rotation: MyAvatar.orientation, + damping: 0.1, + collisionSoundURL: "http://public.highfidelity.io/sounds/Collisions-hitsandslaps/swordStrike1.wav", + restitution: 0.01, + collisionsWillMove: true + }); + actionID = Entities.addAction("hold", stickID, {relativePosition: {x: 0.0, y: 0.0, z: -dimensions.z / 2}, + hand: hand, + timeScale: 0.15}); +}, 3000); + + +function cleanUp() { + Entities.deleteEntity(stickID); +} + + +function positionStick(stickOrientation) { + var baseOffset = {x: 0.0, y: 0.0, z: -dimensions.z / 2}; + var offset = Vec3.multiplyQbyV(stickOrientation, baseOffset); + Entities.updateAction(stickID, actionID, {relativePosition: offset, + relativeRotation: stickOrientation}); +} + + +function mouseMoveEvent(event) { + if (!stickID || actionID === nullActionID) { + return; + } + var windowCenterX = Window.innerWidth / 2; + var windowCenterY = Window.innerHeight / 2; + var mouseXCenterOffset = event.x - windowCenterX; + var mouseYCenterOffset = event.y - windowCenterY; + var mouseXRatio = mouseXCenterOffset / windowCenterX; + var mouseYRatio = mouseYCenterOffset / windowCenterY; + + var stickOrientation = Quat.fromPitchYawRollDegrees(mouseYRatio * -90, mouseXRatio * -90, 0); + positionStick(stickOrientation); +} + + +function initControls() { + if (hand === "right") { + controllerID = 3; // right handed + } else { + controllerID = 4; // left handed + } +} + + +function update() { + var palmPosition = Controller.getSpatialControlPosition(controllerID); + controllerActive = (Vec3.length(palmPosition) > 0); + if (!controllerActive) { + return; + } + + var stickOrientation = Controller.getSpatialControlRawRotation(controllerID); + var adjustment = Quat.fromPitchYawRollDegrees(180, 0, 0); + stickOrientation = Quat.multiply(stickOrientation, adjustment); + + positionStick(stickOrientation); +} + + +Script.scriptEnding.connect(cleanUp); +Controller.mouseMoveEvent.connect(mouseMoveEvent); +Script.update.connect(update); From 77bd7b2821162fa9c5602c7eb6bbdbbf7cb6e816 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Tue, 7 Jul 2015 15:56:16 -0700 Subject: [PATCH 024/107] some ObjectAction methods should be const --- assignment-client/src/AssignmentAction.cpp | 2 +- assignment-client/src/AssignmentAction.h | 4 ++-- interface/src/avatar/AvatarActionHold.cpp | 5 ++--- interface/src/avatar/AvatarActionHold.h | 2 +- libraries/entities/src/EntityActionInterface.h | 4 ++-- libraries/physics/src/ObjectAction.cpp | 8 ++++---- libraries/physics/src/ObjectAction.h | 4 ++-- libraries/physics/src/ObjectActionOffset.cpp | 2 +- libraries/physics/src/ObjectActionOffset.h | 4 ++-- libraries/physics/src/ObjectActionSpring.cpp | 2 +- libraries/physics/src/ObjectActionSpring.h | 4 ++-- 11 files changed, 20 insertions(+), 21 deletions(-) diff --git a/assignment-client/src/AssignmentAction.cpp b/assignment-client/src/AssignmentAction.cpp index 6cb3c06312..8be00f53bd 100644 --- a/assignment-client/src/AssignmentAction.cpp +++ b/assignment-client/src/AssignmentAction.cpp @@ -28,7 +28,7 @@ void AssignmentAction::removeFromSimulation(EntitySimulation* simulation) const simulation->removeAction(_id); } -QByteArray AssignmentAction::serialize() { +QByteArray AssignmentAction::serialize() const { return _data; } diff --git a/assignment-client/src/AssignmentAction.h b/assignment-client/src/AssignmentAction.h index cd72c1f277..cccecd5da5 100644 --- a/assignment-client/src/AssignmentAction.h +++ b/assignment-client/src/AssignmentAction.h @@ -25,14 +25,14 @@ public: virtual ~AssignmentAction(); const QUuid& getID() const { return _id; } - virtual EntityActionType getType() { return _type; } + virtual EntityActionType getType() const { return _type; } virtual void removeFromSimulation(EntitySimulation* simulation) const; virtual EntityItemWeakPointer getOwnerEntity() const { return _ownerEntity; } virtual void setOwnerEntity(const EntityItemPointer ownerEntity) { _ownerEntity = ownerEntity; } virtual bool updateArguments(QVariantMap arguments); virtual QVariantMap getArguments(); - virtual QByteArray serialize(); + virtual QByteArray serialize() const; virtual void deserialize(QByteArray serializedArguments); private: diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 918521c0da..85e7067611 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -166,8 +166,7 @@ QVariantMap AvatarActionHold::getArguments() { void AvatarActionHold::deserialize(QByteArray serializedArguments) { - if (_mine) { - return; + if (!_mine) { + ObjectActionSpring::deserialize(serializedArguments); } - ObjectActionSpring::deserialize(serializedArguments); } diff --git a/interface/src/avatar/AvatarActionHold.h b/interface/src/avatar/AvatarActionHold.h index a47f1ce05d..e5aa0f1ccf 100644 --- a/interface/src/avatar/AvatarActionHold.h +++ b/interface/src/avatar/AvatarActionHold.h @@ -22,7 +22,7 @@ public: AvatarActionHold(EntityActionType type, QUuid id, EntityItemPointer ownerEntity); virtual ~AvatarActionHold(); - virtual EntityActionType getType() { return ACTION_TYPE_HOLD; } + virtual EntityActionType getType() const { return ACTION_TYPE_HOLD; } virtual bool updateArguments(QVariantMap arguments); virtual QVariantMap getArguments(); diff --git a/libraries/entities/src/EntityActionInterface.h b/libraries/entities/src/EntityActionInterface.h index 5693e1fb6f..a543b65f40 100644 --- a/libraries/entities/src/EntityActionInterface.h +++ b/libraries/entities/src/EntityActionInterface.h @@ -32,7 +32,7 @@ public: EntityActionInterface() { } virtual ~EntityActionInterface() { } virtual const QUuid& getID() const = 0; - virtual EntityActionType getType() { assert(false); return ACTION_TYPE_NONE; } + virtual EntityActionType getType() const { assert(false); return ACTION_TYPE_NONE; } virtual void removeFromSimulation(EntitySimulation* simulation) const = 0; virtual EntityItemWeakPointer getOwnerEntity() const = 0; @@ -40,7 +40,7 @@ public: virtual bool updateArguments(QVariantMap arguments) = 0; virtual QVariantMap getArguments() = 0; - virtual QByteArray serialize() = 0; + virtual QByteArray serialize() const = 0; virtual void deserialize(QByteArray serializedArguments) = 0; static EntityActionType actionTypeFromString(QString actionTypeString); diff --git a/libraries/physics/src/ObjectAction.cpp b/libraries/physics/src/ObjectAction.cpp index ae29fe79d3..2dbfba0413 100644 --- a/libraries/physics/src/ObjectAction.cpp +++ b/libraries/physics/src/ObjectAction.cpp @@ -24,15 +24,15 @@ ObjectAction::~ObjectAction() { } void ObjectAction::updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep) { - if (!_active) { - return; - } if (_ownerEntity.expired()) { qDebug() << "warning -- action with no entity removing self from btCollisionWorld."; btDynamicsWorld* dynamicsWorld = static_cast(collisionWorld); dynamicsWorld->removeAction(this); return; } + if (!_active) { + return; + } updateActionWorker(deltaTimeStep); } @@ -129,7 +129,7 @@ void ObjectAction::setAngularVelocity(glm::vec3 angularVelocity) { rigidBody->activate(); } -QByteArray ObjectAction::serialize() { +QByteArray ObjectAction::serialize() const { assert(false); return QByteArray(); } diff --git a/libraries/physics/src/ObjectAction.h b/libraries/physics/src/ObjectAction.h index 0e982aaacf..928eb6d3a4 100644 --- a/libraries/physics/src/ObjectAction.h +++ b/libraries/physics/src/ObjectAction.h @@ -30,7 +30,7 @@ public: virtual ~ObjectAction(); const QUuid& getID() const { return _id; } - virtual EntityActionType getType() { assert(false); return ACTION_TYPE_NONE; } + virtual EntityActionType getType() const { assert(false); return ACTION_TYPE_NONE; } virtual void removeFromSimulation(EntitySimulation* simulation) const; virtual EntityItemWeakPointer getOwnerEntity() const { return _ownerEntity; } virtual void setOwnerEntity(const EntityItemPointer ownerEntity) { _ownerEntity = ownerEntity; } @@ -45,7 +45,7 @@ public: virtual void updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep); virtual void debugDraw(btIDebugDraw* debugDrawer); - virtual QByteArray serialize(); + virtual QByteArray serialize() const; virtual void deserialize(QByteArray serializedArguments); private: diff --git a/libraries/physics/src/ObjectActionOffset.cpp b/libraries/physics/src/ObjectActionOffset.cpp index 22c6b7e0d3..b03cd43876 100644 --- a/libraries/physics/src/ObjectActionOffset.cpp +++ b/libraries/physics/src/ObjectActionOffset.cpp @@ -127,7 +127,7 @@ QVariantMap ObjectActionOffset::getArguments() { return arguments; } -QByteArray ObjectActionOffset::serialize() { +QByteArray ObjectActionOffset::serialize() const { QByteArray ba; QDataStream dataStream(&ba, QIODevice::WriteOnly); dataStream << getType(); diff --git a/libraries/physics/src/ObjectActionOffset.h b/libraries/physics/src/ObjectActionOffset.h index 28a08c2efe..b58d943b2d 100644 --- a/libraries/physics/src/ObjectActionOffset.h +++ b/libraries/physics/src/ObjectActionOffset.h @@ -22,14 +22,14 @@ public: ObjectActionOffset(EntityActionType type, QUuid id, EntityItemPointer ownerEntity); virtual ~ObjectActionOffset(); - virtual EntityActionType getType() { return ACTION_TYPE_OFFSET; } + virtual EntityActionType getType() const { return ACTION_TYPE_OFFSET; } virtual bool updateArguments(QVariantMap arguments); virtual QVariantMap getArguments(); virtual void updateActionWorker(float deltaTimeStep); - virtual QByteArray serialize(); + virtual QByteArray serialize() const; virtual void deserialize(QByteArray serializedArguments); private: diff --git a/libraries/physics/src/ObjectActionSpring.cpp b/libraries/physics/src/ObjectActionSpring.cpp index fae593f3eb..e7d841664d 100644 --- a/libraries/physics/src/ObjectActionSpring.cpp +++ b/libraries/physics/src/ObjectActionSpring.cpp @@ -206,7 +206,7 @@ QVariantMap ObjectActionSpring::getArguments() { return arguments; } -QByteArray ObjectActionSpring::serialize() { +QByteArray ObjectActionSpring::serialize() const { QByteArray serializedActionArguments; QDataStream dataStream(&serializedActionArguments, QIODevice::WriteOnly); diff --git a/libraries/physics/src/ObjectActionSpring.h b/libraries/physics/src/ObjectActionSpring.h index c887a046bb..d45bc78f10 100644 --- a/libraries/physics/src/ObjectActionSpring.h +++ b/libraries/physics/src/ObjectActionSpring.h @@ -22,14 +22,14 @@ public: ObjectActionSpring(EntityActionType type, QUuid id, EntityItemPointer ownerEntity); virtual ~ObjectActionSpring(); - virtual EntityActionType getType() { return ACTION_TYPE_SPRING; } + virtual EntityActionType getType() const { return ACTION_TYPE_SPRING; } virtual bool updateArguments(QVariantMap arguments); virtual QVariantMap getArguments(); virtual void updateActionWorker(float deltaTimeStep); - virtual QByteArray serialize(); + virtual QByteArray serialize() const; virtual void deserialize(QByteArray serializedArguments); protected: From e9df8cacc754f7973da2906444977e4344de735a Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Tue, 7 Jul 2015 16:17:37 -0700 Subject: [PATCH 025/107] make some arguments const, cleanup some #includes --- assignment-client/src/AssignmentAction.cpp | 2 +- assignment-client/src/AssignmentAction.h | 2 +- assignment-client/src/AssignmentActionFactory.cpp | 4 ++-- assignment-client/src/AssignmentActionFactory.h | 2 +- interface/src/InterfaceActionFactory.cpp | 4 ++-- interface/src/InterfaceActionFactory.h | 2 +- interface/src/avatar/AvatarActionHold.cpp | 2 +- interface/src/avatar/AvatarActionHold.h | 2 +- libraries/entities/src/EntityActionFactoryInterface.h | 2 +- libraries/physics/src/ObjectAction.cpp | 2 +- libraries/physics/src/ObjectAction.h | 4 +--- libraries/physics/src/ObjectActionOffset.cpp | 2 +- libraries/physics/src/ObjectActionOffset.h | 2 +- libraries/physics/src/ObjectActionSpring.cpp | 2 +- libraries/physics/src/ObjectActionSpring.h | 5 +---- libraries/shared/src/QVariantGLM.cpp | 8 ++++---- libraries/shared/src/QVariantGLM.h | 8 ++++---- 17 files changed, 25 insertions(+), 30 deletions(-) diff --git a/assignment-client/src/AssignmentAction.cpp b/assignment-client/src/AssignmentAction.cpp index 8be00f53bd..58a6eda473 100644 --- a/assignment-client/src/AssignmentAction.cpp +++ b/assignment-client/src/AssignmentAction.cpp @@ -13,7 +13,7 @@ #include "AssignmentAction.h" -AssignmentAction::AssignmentAction(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) : +AssignmentAction::AssignmentAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : _id(id), _type(type), _data(QByteArray()), diff --git a/assignment-client/src/AssignmentAction.h b/assignment-client/src/AssignmentAction.h index cccecd5da5..77ccf52032 100644 --- a/assignment-client/src/AssignmentAction.h +++ b/assignment-client/src/AssignmentAction.h @@ -21,7 +21,7 @@ class AssignmentAction : public EntityActionInterface { public: - AssignmentAction(EntityActionType type, QUuid id, EntityItemPointer ownerEntity); + AssignmentAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); virtual ~AssignmentAction(); const QUuid& getID() const { return _id; } diff --git a/assignment-client/src/AssignmentActionFactory.cpp b/assignment-client/src/AssignmentActionFactory.cpp index ba2692c611..e1c5d3adff 100644 --- a/assignment-client/src/AssignmentActionFactory.cpp +++ b/assignment-client/src/AssignmentActionFactory.cpp @@ -12,14 +12,14 @@ #include "AssignmentActionFactory.h" -EntityActionPointer assignmentActionFactory(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) { +EntityActionPointer assignmentActionFactory(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) { return (EntityActionPointer) new AssignmentAction(type, id, ownerEntity); } EntityActionPointer AssignmentActionFactory::factory(EntitySimulation* simulation, EntityActionType type, - QUuid id, + const QUuid& id, EntityItemPointer ownerEntity, QVariantMap arguments) { EntityActionPointer action = assignmentActionFactory(type, id, ownerEntity); diff --git a/assignment-client/src/AssignmentActionFactory.h b/assignment-client/src/AssignmentActionFactory.h index f71d22c0dd..41245dac68 100644 --- a/assignment-client/src/AssignmentActionFactory.h +++ b/assignment-client/src/AssignmentActionFactory.h @@ -21,7 +21,7 @@ public: virtual ~AssignmentActionFactory() { } virtual EntityActionPointer factory(EntitySimulation* simulation, EntityActionType type, - QUuid id, + const QUuid& id, EntityItemPointer ownerEntity, QVariantMap arguments); virtual EntityActionPointer factoryBA(EntitySimulation* simulation, diff --git a/interface/src/InterfaceActionFactory.cpp b/interface/src/InterfaceActionFactory.cpp index ccff5b4dc6..b721b10af3 100644 --- a/interface/src/InterfaceActionFactory.cpp +++ b/interface/src/InterfaceActionFactory.cpp @@ -18,7 +18,7 @@ #include "InterfaceActionFactory.h" -EntityActionPointer interfaceActionFactory(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) { +EntityActionPointer interfaceActionFactory(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) { switch (type) { case ACTION_TYPE_NONE: return nullptr; @@ -37,7 +37,7 @@ EntityActionPointer interfaceActionFactory(EntityActionType type, QUuid id, Enti EntityActionPointer InterfaceActionFactory::factory(EntitySimulation* simulation, EntityActionType type, - QUuid id, + const QUuid& id, EntityItemPointer ownerEntity, QVariantMap arguments) { EntityActionPointer action = interfaceActionFactory(type, id, ownerEntity); diff --git a/interface/src/InterfaceActionFactory.h b/interface/src/InterfaceActionFactory.h index 944e2fb753..004c24163f 100644 --- a/interface/src/InterfaceActionFactory.h +++ b/interface/src/InterfaceActionFactory.h @@ -20,7 +20,7 @@ public: virtual ~InterfaceActionFactory() { } virtual EntityActionPointer factory(EntitySimulation* simulation, EntityActionType type, - QUuid id, + const QUuid& id, EntityItemPointer ownerEntity, QVariantMap arguments); virtual EntityActionPointer factoryBA(EntitySimulation* simulation, diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 85e7067611..b8c7621094 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -17,7 +17,7 @@ const uint16_t AvatarActionHold::holdVersion = 1; -AvatarActionHold::AvatarActionHold(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) : +AvatarActionHold::AvatarActionHold(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : ObjectActionSpring(type, id, ownerEntity), _relativePosition(glm::vec3(0.0f)), _relativeRotation(glm::quat()), diff --git a/interface/src/avatar/AvatarActionHold.h b/interface/src/avatar/AvatarActionHold.h index e5aa0f1ccf..970fb75c0d 100644 --- a/interface/src/avatar/AvatarActionHold.h +++ b/interface/src/avatar/AvatarActionHold.h @@ -19,7 +19,7 @@ class AvatarActionHold : public ObjectActionSpring { public: - AvatarActionHold(EntityActionType type, QUuid id, EntityItemPointer ownerEntity); + AvatarActionHold(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); virtual ~AvatarActionHold(); virtual EntityActionType getType() const { return ACTION_TYPE_HOLD; } diff --git a/libraries/entities/src/EntityActionFactoryInterface.h b/libraries/entities/src/EntityActionFactoryInterface.h index 5269405d55..9f4056cdff 100644 --- a/libraries/entities/src/EntityActionFactoryInterface.h +++ b/libraries/entities/src/EntityActionFactoryInterface.h @@ -25,7 +25,7 @@ class EntityActionFactoryInterface : public QObject, public Dependency { virtual ~EntityActionFactoryInterface() { } virtual EntityActionPointer factory(EntitySimulation* simulation, EntityActionType type, - QUuid id, + const QUuid& id, EntityItemPointer ownerEntity, QVariantMap arguments) { assert(false); return nullptr; } virtual EntityActionPointer factoryBA(EntitySimulation* simulation, diff --git a/libraries/physics/src/ObjectAction.cpp b/libraries/physics/src/ObjectAction.cpp index 2dbfba0413..3c38b084aa 100644 --- a/libraries/physics/src/ObjectAction.cpp +++ b/libraries/physics/src/ObjectAction.cpp @@ -13,7 +13,7 @@ #include "ObjectAction.h" -ObjectAction::ObjectAction(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) : +ObjectAction::ObjectAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : btActionInterface(), _id(id), _active(false), diff --git a/libraries/physics/src/ObjectAction.h b/libraries/physics/src/ObjectAction.h index 928eb6d3a4..1fb3ea9b10 100644 --- a/libraries/physics/src/ObjectAction.h +++ b/libraries/physics/src/ObjectAction.h @@ -17,8 +17,6 @@ #include -#include - #include "ObjectMotionState.h" #include "BulletUtil.h" #include "EntityActionInterface.h" @@ -26,7 +24,7 @@ class ObjectAction : public btActionInterface, public EntityActionInterface { public: - ObjectAction(EntityActionType type, QUuid id, EntityItemPointer ownerEntity); + ObjectAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); virtual ~ObjectAction(); const QUuid& getID() const { return _id; } diff --git a/libraries/physics/src/ObjectActionOffset.cpp b/libraries/physics/src/ObjectActionOffset.cpp index b03cd43876..90e148971a 100644 --- a/libraries/physics/src/ObjectActionOffset.cpp +++ b/libraries/physics/src/ObjectActionOffset.cpp @@ -15,7 +15,7 @@ const uint16_t ObjectActionOffset::offsetVersion = 1; -ObjectActionOffset::ObjectActionOffset(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) : +ObjectActionOffset::ObjectActionOffset(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : ObjectAction(type, id, ownerEntity) { #if WANT_DEBUG qDebug() << "ObjectActionOffset::ObjectActionOffset"; diff --git a/libraries/physics/src/ObjectActionOffset.h b/libraries/physics/src/ObjectActionOffset.h index b58d943b2d..ef79c8eb6b 100644 --- a/libraries/physics/src/ObjectActionOffset.h +++ b/libraries/physics/src/ObjectActionOffset.h @@ -19,7 +19,7 @@ class ObjectActionOffset : public ObjectAction { public: - ObjectActionOffset(EntityActionType type, QUuid id, EntityItemPointer ownerEntity); + ObjectActionOffset(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); virtual ~ObjectActionOffset(); virtual EntityActionType getType() const { return ACTION_TYPE_OFFSET; } diff --git a/libraries/physics/src/ObjectActionSpring.cpp b/libraries/physics/src/ObjectActionSpring.cpp index e7d841664d..8e55b8b857 100644 --- a/libraries/physics/src/ObjectActionSpring.cpp +++ b/libraries/physics/src/ObjectActionSpring.cpp @@ -17,7 +17,7 @@ const float SPRING_MAX_SPEED = 10.0f; const uint16_t ObjectActionSpring::springVersion = 1; -ObjectActionSpring::ObjectActionSpring(EntityActionType type, QUuid id, EntityItemPointer ownerEntity) : +ObjectActionSpring::ObjectActionSpring(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : ObjectAction(type, id, ownerEntity), _positionalTarget(glm::vec3(0.0f)), _linearTimeScale(0.2f), diff --git a/libraries/physics/src/ObjectActionSpring.h b/libraries/physics/src/ObjectActionSpring.h index d45bc78f10..6c576e4545 100644 --- a/libraries/physics/src/ObjectActionSpring.h +++ b/libraries/physics/src/ObjectActionSpring.h @@ -12,14 +12,11 @@ #ifndef hifi_ObjectActionSpring_h #define hifi_ObjectActionSpring_h -#include - -#include #include "ObjectAction.h" class ObjectActionSpring : public ObjectAction { public: - ObjectActionSpring(EntityActionType type, QUuid id, EntityItemPointer ownerEntity); + ObjectActionSpring(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); virtual ~ObjectActionSpring(); virtual EntityActionType getType() const { return ACTION_TYPE_SPRING; } diff --git a/libraries/shared/src/QVariantGLM.cpp b/libraries/shared/src/QVariantGLM.cpp index 7cebacee8e..7a3ab92cca 100644 --- a/libraries/shared/src/QVariantGLM.cpp +++ b/libraries/shared/src/QVariantGLM.cpp @@ -20,7 +20,7 @@ QVariantList glmToQList(const glm::quat& g) { return QVariantList() << g.x << g.y << g.z << g.w; } -QVariantList rgbColorToQList(rgbColor& v) { +QVariantList rgbColorToQList(const rgbColor& v) { return QVariantList() << (int)(v[0]) << (int)(v[1]) << (int)(v[2]); } @@ -42,12 +42,12 @@ QVariantMap glmToQMap(const glm::quat& glmQuat) { } -glm::vec3 qListToGlmVec3(const QVariant q) { +glm::vec3 qListToGlmVec3(const QVariant& q) { QVariantList qList = q.toList(); return glm::vec3(qList[RED_INDEX].toFloat(), qList[GREEN_INDEX].toFloat(), qList[BLUE_INDEX].toFloat()); } -glm::quat qListToGlmQuat(const QVariant q) { +glm::quat qListToGlmQuat(const QVariant& q) { QVariantList qList = q.toList(); float x = qList[0].toFloat(); float y = qList[1].toFloat(); @@ -56,7 +56,7 @@ glm::quat qListToGlmQuat(const QVariant q) { return glm::quat(w, x, y, z); } -void qListtoRgbColor(const QVariant q, rgbColor returnValue) { +void qListtoRgbColor(const QVariant& q, rgbColor& returnValue) { QVariantList qList = q.toList(); returnValue[RED_INDEX] = qList[RED_INDEX].toInt(); returnValue[GREEN_INDEX] = qList[GREEN_INDEX].toInt(); diff --git a/libraries/shared/src/QVariantGLM.h b/libraries/shared/src/QVariantGLM.h index 922aa7c4aa..3a91110250 100644 --- a/libraries/shared/src/QVariantGLM.h +++ b/libraries/shared/src/QVariantGLM.h @@ -19,11 +19,11 @@ QVariantList glmToQList(const glm::vec3& g); QVariantList glmToQList(const glm::quat& g); -QVariantList rgbColorToQList(rgbColor& v); +QVariantList rgbColorToQList(const rgbColor& v); QVariantMap glmToQMap(const glm::vec3& glmVector); QVariantMap glmToQMap(const glm::quat& glmQuat); -glm::vec3 qListToGlmVec3(const QVariant q); -glm::quat qListToGlmQuat(const QVariant q); -void qListtoRgbColor(const QVariant q, rgbColor returnValue); +glm::vec3 qListToGlmVec3(const QVariant& q); +glm::quat qListToGlmQuat(const QVariant& q); +void qListtoRgbColor(const QVariant& q, rgbColor& returnValue); From a0a1b01ea9f7855f3670ce34591090d150a653c0 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Tue, 7 Jul 2015 17:48:05 -0700 Subject: [PATCH 026/107] coalesce dupe action data members: _id and _type --- assignment-client/src/AssignmentAction.cpp | 3 +-- assignment-client/src/AssignmentAction.h | 4 ---- interface/src/InterfaceActionFactory.cpp | 6 +++--- interface/src/avatar/AvatarActionHold.cpp | 5 +++-- interface/src/avatar/AvatarActionHold.h | 4 +--- .../entities/src/EntityActionInterface.h | 8 +++++--- libraries/physics/src/ObjectAction.cpp | 10 +--------- libraries/physics/src/ObjectAction.h | 20 +++++++++---------- libraries/physics/src/ObjectActionOffset.cpp | 11 +++++----- libraries/physics/src/ObjectActionOffset.h | 4 +--- libraries/physics/src/ObjectActionSpring.cpp | 11 +++++----- libraries/physics/src/ObjectActionSpring.h | 4 +--- 12 files changed, 37 insertions(+), 53 deletions(-) diff --git a/assignment-client/src/AssignmentAction.cpp b/assignment-client/src/AssignmentAction.cpp index 58a6eda473..8b5650ee42 100644 --- a/assignment-client/src/AssignmentAction.cpp +++ b/assignment-client/src/AssignmentAction.cpp @@ -14,8 +14,7 @@ #include "AssignmentAction.h" AssignmentAction::AssignmentAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : - _id(id), - _type(type), + EntityActionInterface(type, id), _data(QByteArray()), _active(false), _ownerEntity(ownerEntity) { diff --git a/assignment-client/src/AssignmentAction.h b/assignment-client/src/AssignmentAction.h index 77ccf52032..23720bd465 100644 --- a/assignment-client/src/AssignmentAction.h +++ b/assignment-client/src/AssignmentAction.h @@ -24,8 +24,6 @@ public: AssignmentAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); virtual ~AssignmentAction(); - const QUuid& getID() const { return _id; } - virtual EntityActionType getType() const { return _type; } virtual void removeFromSimulation(EntitySimulation* simulation) const; virtual EntityItemWeakPointer getOwnerEntity() const { return _ownerEntity; } virtual void setOwnerEntity(const EntityItemPointer ownerEntity) { _ownerEntity = ownerEntity; } @@ -36,8 +34,6 @@ public: virtual void deserialize(QByteArray serializedArguments); private: - QUuid _id; - EntityActionType _type; QByteArray _data; protected: diff --git a/interface/src/InterfaceActionFactory.cpp b/interface/src/InterfaceActionFactory.cpp index b721b10af3..363fb66e76 100644 --- a/interface/src/InterfaceActionFactory.cpp +++ b/interface/src/InterfaceActionFactory.cpp @@ -23,11 +23,11 @@ EntityActionPointer interfaceActionFactory(EntityActionType type, const QUuid& i case ACTION_TYPE_NONE: return nullptr; case ACTION_TYPE_OFFSET: - return (EntityActionPointer) new ObjectActionOffset(type, id, ownerEntity); + return (EntityActionPointer) new ObjectActionOffset(id, ownerEntity); case ACTION_TYPE_SPRING: - return (EntityActionPointer) new ObjectActionSpring(type, id, ownerEntity); + return (EntityActionPointer) new ObjectActionSpring(id, ownerEntity); case ACTION_TYPE_HOLD: - return (EntityActionPointer) new AvatarActionHold(type, id, ownerEntity); + return (EntityActionPointer) new AvatarActionHold(id, ownerEntity); } assert(false); diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index b8c7621094..ca903a9771 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -17,13 +17,14 @@ const uint16_t AvatarActionHold::holdVersion = 1; -AvatarActionHold::AvatarActionHold(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : - ObjectActionSpring(type, id, ownerEntity), +AvatarActionHold::AvatarActionHold(const QUuid& id, EntityItemPointer ownerEntity) : + ObjectActionSpring(id, ownerEntity), _relativePosition(glm::vec3(0.0f)), _relativeRotation(glm::quat()), _hand("right"), _mine(false) { + _type = ACTION_TYPE_HOLD; #if WANT_DEBUG qDebug() << "AvatarActionHold::AvatarActionHold"; #endif diff --git a/interface/src/avatar/AvatarActionHold.h b/interface/src/avatar/AvatarActionHold.h index 970fb75c0d..3500b5dfa1 100644 --- a/interface/src/avatar/AvatarActionHold.h +++ b/interface/src/avatar/AvatarActionHold.h @@ -19,11 +19,9 @@ class AvatarActionHold : public ObjectActionSpring { public: - AvatarActionHold(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); + AvatarActionHold(const QUuid& id, EntityItemPointer ownerEntity); virtual ~AvatarActionHold(); - virtual EntityActionType getType() const { return ACTION_TYPE_HOLD; } - virtual bool updateArguments(QVariantMap arguments); virtual QVariantMap getArguments(); diff --git a/libraries/entities/src/EntityActionInterface.h b/libraries/entities/src/EntityActionInterface.h index a543b65f40..a4f1c8ea15 100644 --- a/libraries/entities/src/EntityActionInterface.h +++ b/libraries/entities/src/EntityActionInterface.h @@ -29,10 +29,10 @@ enum EntityActionType { class EntityActionInterface { public: - EntityActionInterface() { } + EntityActionInterface(EntityActionType type, const QUuid& id) : _id(id), _type(type) { } virtual ~EntityActionInterface() { } - virtual const QUuid& getID() const = 0; - virtual EntityActionType getType() const { assert(false); return ACTION_TYPE_NONE; } + const QUuid& getID() const { return _id; } + EntityActionType getType() const { return _type; } virtual void removeFromSimulation(EntitySimulation* simulation) const = 0; virtual EntityItemWeakPointer getOwnerEntity() const = 0; @@ -68,6 +68,8 @@ protected: static QString extractStringArgument(QString objectName, QVariantMap arguments, QString argumentName, bool& ok, bool required = true); + QUuid _id; + EntityActionType _type; }; diff --git a/libraries/physics/src/ObjectAction.cpp b/libraries/physics/src/ObjectAction.cpp index 3c38b084aa..101b69f03a 100644 --- a/libraries/physics/src/ObjectAction.cpp +++ b/libraries/physics/src/ObjectAction.cpp @@ -15,7 +15,7 @@ ObjectAction::ObjectAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : btActionInterface(), - _id(id), + EntityActionInterface(type, id), _active(false), _ownerEntity(ownerEntity) { } @@ -129,11 +129,3 @@ void ObjectAction::setAngularVelocity(glm::vec3 angularVelocity) { rigidBody->activate(); } -QByteArray ObjectAction::serialize() const { - assert(false); - return QByteArray(); -} - -void ObjectAction::deserialize(QByteArray serializedArguments) { - assert(false); -} diff --git a/libraries/physics/src/ObjectAction.h b/libraries/physics/src/ObjectAction.h index 1fb3ea9b10..9c7a2c9b4b 100644 --- a/libraries/physics/src/ObjectAction.h +++ b/libraries/physics/src/ObjectAction.h @@ -27,28 +27,22 @@ public: ObjectAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); virtual ~ObjectAction(); - const QUuid& getID() const { return _id; } - virtual EntityActionType getType() const { assert(false); return ACTION_TYPE_NONE; } virtual void removeFromSimulation(EntitySimulation* simulation) const; virtual EntityItemWeakPointer getOwnerEntity() const { return _ownerEntity; } virtual void setOwnerEntity(const EntityItemPointer ownerEntity) { _ownerEntity = ownerEntity; } - virtual bool updateArguments(QVariantMap arguments) { return false; } - virtual QVariantMap getArguments() { return QVariantMap(); } + virtual bool updateArguments(QVariantMap arguments) = 0; + virtual QVariantMap getArguments() = 0; // this is called from updateAction and should be overridden by subclasses - virtual void updateActionWorker(float deltaTimeStep) {} + virtual void updateActionWorker(float deltaTimeStep) = 0; // these are from btActionInterface virtual void updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep); virtual void debugDraw(btIDebugDraw* debugDrawer); - virtual QByteArray serialize() const; - virtual void deserialize(QByteArray serializedArguments); - -private: - QUuid _id; - QReadWriteLock _lock; + virtual QByteArray serialize() const = 0; + virtual void deserialize(QByteArray serializedArguments) = 0; protected: @@ -68,6 +62,10 @@ protected: bool tryLockForWrite() { return _lock.tryLockForWrite(); } void unlock() { _lock.unlock(); } +private: + QReadWriteLock _lock; + +protected: bool _active; EntityItemWeakPointer _ownerEntity; }; diff --git a/libraries/physics/src/ObjectActionOffset.cpp b/libraries/physics/src/ObjectActionOffset.cpp index 90e148971a..7cf35b27d7 100644 --- a/libraries/physics/src/ObjectActionOffset.cpp +++ b/libraries/physics/src/ObjectActionOffset.cpp @@ -15,8 +15,8 @@ const uint16_t ObjectActionOffset::offsetVersion = 1; -ObjectActionOffset::ObjectActionOffset(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : - ObjectAction(type, id, ownerEntity) { +ObjectActionOffset::ObjectActionOffset(const QUuid& id, EntityItemPointer ownerEntity) : + ObjectAction(ACTION_TYPE_OFFSET, id, ownerEntity) { #if WANT_DEBUG qDebug() << "ObjectActionOffset::ObjectActionOffset"; #endif @@ -146,13 +146,14 @@ void ObjectActionOffset::deserialize(QByteArray serializedArguments) { QDataStream dataStream(serializedArguments); EntityActionType type; - QUuid id; - uint16_t serializationVersion; - dataStream >> type; assert(type == getType()); + + QUuid id; dataStream >> id; assert(id == getID()); + + uint16_t serializationVersion; dataStream >> serializationVersion; if (serializationVersion != ObjectActionOffset::offsetVersion) { return; diff --git a/libraries/physics/src/ObjectActionOffset.h b/libraries/physics/src/ObjectActionOffset.h index ef79c8eb6b..1918da6996 100644 --- a/libraries/physics/src/ObjectActionOffset.h +++ b/libraries/physics/src/ObjectActionOffset.h @@ -19,11 +19,9 @@ class ObjectActionOffset : public ObjectAction { public: - ObjectActionOffset(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); + ObjectActionOffset(const QUuid& id, EntityItemPointer ownerEntity); virtual ~ObjectActionOffset(); - virtual EntityActionType getType() const { return ACTION_TYPE_OFFSET; } - virtual bool updateArguments(QVariantMap arguments); virtual QVariantMap getArguments(); diff --git a/libraries/physics/src/ObjectActionSpring.cpp b/libraries/physics/src/ObjectActionSpring.cpp index 8e55b8b857..cb1dd20472 100644 --- a/libraries/physics/src/ObjectActionSpring.cpp +++ b/libraries/physics/src/ObjectActionSpring.cpp @@ -17,8 +17,8 @@ const float SPRING_MAX_SPEED = 10.0f; const uint16_t ObjectActionSpring::springVersion = 1; -ObjectActionSpring::ObjectActionSpring(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity) : - ObjectAction(type, id, ownerEntity), +ObjectActionSpring::ObjectActionSpring(const QUuid& id, EntityItemPointer ownerEntity) : + ObjectAction(ACTION_TYPE_SPRING, id, ownerEntity), _positionalTarget(glm::vec3(0.0f)), _linearTimeScale(0.2f), _positionalTargetSet(false), @@ -229,13 +229,14 @@ void ObjectActionSpring::deserialize(QByteArray serializedArguments) { QDataStream dataStream(serializedArguments); EntityActionType type; - QUuid id; - uint16_t serializationVersion; - dataStream >> type; assert(type == getType()); + + QUuid id; dataStream >> id; assert(id == getID()); + + uint16_t serializationVersion; dataStream >> serializationVersion; if (serializationVersion != ObjectActionSpring::springVersion) { return; diff --git a/libraries/physics/src/ObjectActionSpring.h b/libraries/physics/src/ObjectActionSpring.h index 6c576e4545..caa64c3d3a 100644 --- a/libraries/physics/src/ObjectActionSpring.h +++ b/libraries/physics/src/ObjectActionSpring.h @@ -16,11 +16,9 @@ class ObjectActionSpring : public ObjectAction { public: - ObjectActionSpring(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); + ObjectActionSpring(const QUuid& id, EntityItemPointer ownerEntity); virtual ~ObjectActionSpring(); - virtual EntityActionType getType() const { return ACTION_TYPE_SPRING; } - virtual bool updateArguments(QVariantMap arguments); virtual QVariantMap getArguments(); From 7e59be619693631685a4ad25ec44f0497b385234 Mon Sep 17 00:00:00 2001 From: Niraj Venkat Date: Wed, 8 Jul 2015 10:00:58 -0700 Subject: [PATCH 027/107] First take oon the query for the gpu --- libraries/gpu/src/gpu/Batch.cpp | 17 ++++ libraries/gpu/src/gpu/Batch.h | 12 +++ libraries/gpu/src/gpu/Context.h | 9 ++ libraries/gpu/src/gpu/GLBackend.cpp | 4 +- libraries/gpu/src/gpu/GLBackend.h | 17 ++++ libraries/gpu/src/gpu/GLBackendOutput.cpp | 1 - libraries/gpu/src/gpu/GLBackendQuery.cpp | 93 +++++++++++++++++++ libraries/gpu/src/gpu/Query.cpp | 27 ++++++ libraries/gpu/src/gpu/Query.h | 45 +++++++++ .../render-utils/src/RenderDeferredTask.cpp | 24 +++++ .../render-utils/src/RenderDeferredTask.h | 3 + 11 files changed, 250 insertions(+), 2 deletions(-) create mode 100644 libraries/gpu/src/gpu/GLBackendQuery.cpp create mode 100644 libraries/gpu/src/gpu/Query.cpp create mode 100644 libraries/gpu/src/gpu/Query.h diff --git a/libraries/gpu/src/gpu/Batch.cpp b/libraries/gpu/src/gpu/Batch.cpp index ee028e79e6..c97c9603b6 100644 --- a/libraries/gpu/src/gpu/Batch.cpp +++ b/libraries/gpu/src/gpu/Batch.cpp @@ -213,3 +213,20 @@ void Batch::setFramebuffer(const FramebufferPointer& framebuffer) { } +void Batch::beginQuery(const QueryPointer& query) { + ADD_COMMAND(beginQuery); + + _params.push_back(_queries.cache(query)); +} + +void Batch::endQuery(const QueryPointer& query) { + ADD_COMMAND(endQuery); + + _params.push_back(_queries.cache(query)); +} + +void Batch::getQuery(const QueryPointer& query) { + ADD_COMMAND(getQuery); + + _params.push_back(_queries.cache(query)); +} diff --git a/libraries/gpu/src/gpu/Batch.h b/libraries/gpu/src/gpu/Batch.h index 835e872b4a..6b98ae7917 100644 --- a/libraries/gpu/src/gpu/Batch.h +++ b/libraries/gpu/src/gpu/Batch.h @@ -18,6 +18,7 @@ #include +#include "Query.h" #include "Stream.h" #include "Texture.h" @@ -121,6 +122,11 @@ public: // Framebuffer Stage void setFramebuffer(const FramebufferPointer& framebuffer); + // Query Section + void beginQuery(const QueryPointer& query); + void endQuery(const QueryPointer& query); + void getQuery(const QueryPointer& query); + // TODO: As long as we have gl calls explicitely issued from interface // code, we need to be able to record and batch these calls. THe long // term strategy is to get rid of any GL calls in favor of the HIFI GPU API @@ -189,6 +195,10 @@ public: COMMAND_setFramebuffer, + COMMAND_beginQuery, + COMMAND_endQuery, + COMMAND_getQuery, + // TODO: As long as we have gl calls explicitely issued from interface // code, we need to be able to record and batch these calls. THe long // term strategy is to get rid of any GL calls in favor of the HIFI GPU API @@ -292,6 +302,7 @@ public: typedef Cache::Vector TransformCaches; typedef Cache::Vector PipelineCaches; typedef Cache::Vector FramebufferCaches; + typedef Cache::Vector QueryCaches; // Cache Data in a byte array if too big to fit in Param // FOr example Mat4s are going there @@ -316,6 +327,7 @@ public: TransformCaches _transforms; PipelineCaches _pipelines; FramebufferCaches _framebuffers; + QueryCaches _queries; protected: }; diff --git a/libraries/gpu/src/gpu/Context.h b/libraries/gpu/src/gpu/Context.h index 98ddc7fb64..d34954f50c 100644 --- a/libraries/gpu/src/gpu/Context.h +++ b/libraries/gpu/src/gpu/Context.h @@ -99,6 +99,15 @@ public: return reinterpret_cast(framebuffer.getGPUObject()); } + template< typename T > + static void setGPUObject(const Query& query, T* object) { + query.setGPUObject(object); + } + template< typename T > + static T* getGPUObject(const Query& query) { + return reinterpret_cast(query.getGPUObject()); + } + protected: }; diff --git a/libraries/gpu/src/gpu/GLBackend.cpp b/libraries/gpu/src/gpu/GLBackend.cpp index 302dc0e8be..63b48cab31 100644 --- a/libraries/gpu/src/gpu/GLBackend.cpp +++ b/libraries/gpu/src/gpu/GLBackend.cpp @@ -39,6 +39,9 @@ GLBackend::CommandCall GLBackend::_commandCalls[Batch::NUM_COMMANDS] = (&::gpu::GLBackend::do_setFramebuffer), + (&::gpu::GLBackend::do_beginQuery), + (&::gpu::GLBackend::do_endQuery), + (&::gpu::GLBackend::do_getQuery), (&::gpu::GLBackend::do_glEnable), (&::gpu::GLBackend::do_glDisable), @@ -243,7 +246,6 @@ void GLBackend::do_clearFramebuffer(Batch& batch, uint32 paramOffset) { (void) CHECK_GL_ERROR(); } - // TODO: As long as we have gl calls explicitely issued from interface // code, we need to be able to record and batch these calls. THe long // term strategy is to get rid of any GL calls in favor of the HIFI GPU API diff --git a/libraries/gpu/src/gpu/GLBackend.h b/libraries/gpu/src/gpu/GLBackend.h index d798e9aaac..6313e8e7ac 100644 --- a/libraries/gpu/src/gpu/GLBackend.h +++ b/libraries/gpu/src/gpu/GLBackend.h @@ -188,6 +188,18 @@ public: static GLFramebuffer* syncGPUObject(const Framebuffer& framebuffer); static GLuint getFramebufferID(const FramebufferPointer& framebuffer); + class GLQuery : public GPUObject { + public: + GLuint _qo = 0; + GLuint64 _result = 0; + + GLQuery(); + ~GLQuery(); + }; + static GLQuery* syncGPUObject(const Query& query); + static GLuint getQueryID(const QueryPointer& query); + + static const int MAX_NUM_ATTRIBUTES = Stream::NUM_INPUT_SLOTS; static const int MAX_NUM_INPUT_BUFFERS = 16; @@ -367,6 +379,11 @@ protected: OutputStageState() {} } _output; + // Query section + void do_beginQuery(Batch& batch, uint32 paramOffset); + void do_endQuery(Batch& batch, uint32 paramOffset); + void do_getQuery(Batch& batch, uint32 paramOffset); + // TODO: As long as we have gl calls explicitely issued from interface // code, we need to be able to record and batch these calls. THe long // term strategy is to get rid of any GL calls in favor of the HIFI GPU API diff --git a/libraries/gpu/src/gpu/GLBackendOutput.cpp b/libraries/gpu/src/gpu/GLBackendOutput.cpp index 903c97f45b..ef0f7c7f34 100755 --- a/libraries/gpu/src/gpu/GLBackendOutput.cpp +++ b/libraries/gpu/src/gpu/GLBackendOutput.cpp @@ -167,4 +167,3 @@ void GLBackend::do_setFramebuffer(Batch& batch, uint32 paramOffset) { _output._framebuffer = framebuffer; } } - diff --git a/libraries/gpu/src/gpu/GLBackendQuery.cpp b/libraries/gpu/src/gpu/GLBackendQuery.cpp new file mode 100644 index 0000000000..2bc6a7896c --- /dev/null +++ b/libraries/gpu/src/gpu/GLBackendQuery.cpp @@ -0,0 +1,93 @@ +// +// GLBackendQuery.cpp +// libraries/gpu/src/gpu +// +// Created by Sam Gateau on 7/7/2015. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +#include "GPULogging.h" +#include "GLBackendShared.h" + +using namespace gpu; + +GLBackend::GLQuery::GLQuery() {} + +GLBackend::GLQuery::~GLQuery() { + if (_qo != 0) { + glDeleteQueries(1, &_qo); + } +} + +GLBackend::GLQuery* GLBackend::syncGPUObject(const Query& query) { + GLQuery* object = Backend::getGPUObject(query); + + // If GPU object already created and in sync + if (object) { + return object; + } + + // need to have a gpu object? + if (!object) { + GLuint qo; + glGenQueries(1, &qo); + (void) CHECK_GL_ERROR(); + GLuint64 result = -1; + + // All is green, assign the gpuobject to the Query + object = new GLQuery(); + object->_qo = qo; + object->_result = result; + Backend::setGPUObject(query, object); + } + + return object; +} + + + +GLuint GLBackend::getQueryID(const QueryPointer& query) { + if (!query) { + return 0; + } + GLQuery* object = GLBackend::syncGPUObject(*query); + if (object) { + return object->_qo; + } else { + return 0; + } +} + +void GLBackend::do_beginQuery(Batch& batch, uint32 paramOffset) { + auto& query = batch._queries.get(batch._params[paramOffset]._uint); + GLQuery* glquery = syncGPUObject(*query); + if (glquery) { + glBeginQuery(GL_TIME_ELAPSED, glquery->_qo); + (void)CHECK_GL_ERROR(); + } +} + +void GLBackend::do_endQuery(Batch& batch, uint32 paramOffset) { + auto& query = batch._queries.get(batch._params[paramOffset]._uint); + GLQuery* glquery = syncGPUObject(*query); + if (glquery) { + glEndQuery(GL_TIME_ELAPSED); + (void)CHECK_GL_ERROR(); + } +} + +void GLBackend::do_getQuery(Batch& batch, uint32 paramOffset) { + auto& query = batch._queries.get(batch._params[paramOffset]._uint); + GLQuery* glquery = syncGPUObject(*query); + if (glquery) { + GLint available = 0; + while (!available) { + glGetQueryObjectiv(glquery->_qo, GL_QUERY_RESULT_AVAILABLE, &available); + } + + glGetQueryObjectui64v(glquery->_qo, GL_QUERY_RESULT, &glquery->_result); + (void)CHECK_GL_ERROR(); + } +} diff --git a/libraries/gpu/src/gpu/Query.cpp b/libraries/gpu/src/gpu/Query.cpp new file mode 100644 index 0000000000..b8ed729c99 --- /dev/null +++ b/libraries/gpu/src/gpu/Query.cpp @@ -0,0 +1,27 @@ +// +// Query.cpp +// interface/src/gpu +// +// Created by Niraj Venkat on 7/7/2015. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +#include "Query.h" + +#include + +using namespace gpu; + +Query::Query() +{ +} + +Query::~Query() +{ +} + +double Query::getElapsedTime() { + return 0.0; +} diff --git a/libraries/gpu/src/gpu/Query.h b/libraries/gpu/src/gpu/Query.h new file mode 100644 index 0000000000..0a4d554e77 --- /dev/null +++ b/libraries/gpu/src/gpu/Query.h @@ -0,0 +1,45 @@ +// +// Query.h +// interface/src/gpu +// +// Created by Niraj Venkat on 7/7/2015. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// +#ifndef hifi_gpu_Query_h +#define hifi_gpu_Query_h + +#include +#include +#include +#include "GPUConfig.h" + +#include "Format.h" + +namespace gpu { + + class Query { + public: + Query(); + ~Query(); + + uint32 queryResult; + + double getElapsedTime(); + + protected: + + // This shouldn't be used by anything else than the Backend class with the proper casting. + mutable GPUObject* _gpuObject = NULL; + void setGPUObject(GPUObject* gpuObject) const { _gpuObject = gpuObject; } + GPUObject* getGPUObject() const { return _gpuObject; } + friend class Backend; + }; + + typedef std::shared_ptr QueryPointer; + typedef std::vector< QueryPointer > Queries; +}; + +#endif diff --git a/libraries/render-utils/src/RenderDeferredTask.cpp b/libraries/render-utils/src/RenderDeferredTask.cpp index 12a6d32ae5..56980058c8 100755 --- a/libraries/render-utils/src/RenderDeferredTask.cpp +++ b/libraries/render-utils/src/RenderDeferredTask.cpp @@ -75,6 +75,12 @@ RenderDeferredTask::RenderDeferredTask() : Task() { _jobs.push_back(Job(new DrawOverlay3D::JobModel("DrawOverlay3D"))); _jobs.push_back(Job(new ResetGLState::JobModel())); + + // Give ourselves 3 frmaes of timer queries + _timerQueries.push_back(gpu::QueryPointer(new gpu::Query())); + _timerQueries.push_back(gpu::QueryPointer(new gpu::Query())); + _timerQueries.push_back(gpu::QueryPointer(new gpu::Query())); + _currentTimerQueryIndex = 0; } RenderDeferredTask::~RenderDeferredTask() { @@ -98,9 +104,27 @@ void RenderDeferredTask::run(const SceneContextPointer& sceneContext, const Rend renderContext->args->_context->syncCache(); + // start the current timer query + auto& currentQuery = _timerQueries[_currentTimerQueryIndex]; + { + gpu::Batch batch; + batch.beginQuery(currentQuery); + renderContext->args->_context->render(batch); + } + for (auto job : _jobs) { job.run(sceneContext, renderContext); } + + // End the current timer query + { + gpu::Batch batch; + batch.endQuery(currentQuery); + batch.getQuery(currentQuery); + renderContext->args->_context->render(batch); + (_currentTimerQueryIndex++); + _currentTimerQueryIndex = _currentTimerQueryIndex% _timerQueries.size(); + } }; void DrawOpaqueDeferred::run(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext, const ItemIDsBounds& inItems) { diff --git a/libraries/render-utils/src/RenderDeferredTask.h b/libraries/render-utils/src/RenderDeferredTask.h index 3d11e97634..4040606c62 100755 --- a/libraries/render-utils/src/RenderDeferredTask.h +++ b/libraries/render-utils/src/RenderDeferredTask.h @@ -77,6 +77,9 @@ public: virtual void run(const render::SceneContextPointer& sceneContext, const render::RenderContextPointer& renderContext); + + gpu::Queries _timerQueries; + int _currentTimerQueryIndex = 0; }; From 16e79cc85fae5470545d541596cba65d4df47b09 Mon Sep 17 00:00:00 2001 From: Chris Collins Date: Wed, 8 Jul 2015 11:17:03 -0700 Subject: [PATCH 028/107] Remove lobby.js from the default scripts Lobby.js has some bugs that are not going to be fixed in the short term. Therefore we are removing it from default scripts. --- examples/defaultScripts.js | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/defaultScripts.js b/examples/defaultScripts.js index fb76b62dbe..922047b90c 100644 --- a/examples/defaultScripts.js +++ b/examples/defaultScripts.js @@ -12,7 +12,6 @@ Script.load("progress.js"); Script.load("edit.js"); Script.load("selectAudioDevice.js"); Script.load("inspect.js"); -Script.load("lobby.js"); Script.load("notifications.js"); Script.load("users.js"); Script.load("grab.js"); From 58c32dbf2066ec1e1eb084e8aa6927e64f48b746 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Wed, 8 Jul 2015 11:30:04 -0700 Subject: [PATCH 029/107] remove whitespace from end of lines --- libraries/physics/src/ObjectAction.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/physics/src/ObjectAction.h b/libraries/physics/src/ObjectAction.h index 9c7a2c9b4b..f619657e92 100644 --- a/libraries/physics/src/ObjectAction.h +++ b/libraries/physics/src/ObjectAction.h @@ -65,7 +65,7 @@ protected: private: QReadWriteLock _lock; -protected: +protected: bool _active; EntityItemWeakPointer _ownerEntity; }; From ef4620cabb29d3f1b71dfd6f80ce3290c2ebd650 Mon Sep 17 00:00:00 2001 From: Howard Stearns Date: Wed, 8 Jul 2015 11:52:37 -0700 Subject: [PATCH 030/107] Add a button to create/toggle-away sword. When brandished, display hit points (in overlay for this user). --- examples/example/games/sword.js | 143 ++++++++++++++++++++++++++------ 1 file changed, 117 insertions(+), 26 deletions(-) diff --git a/examples/example/games/sword.js b/examples/example/games/sword.js index 9592c275f6..995015b227 100644 --- a/examples/example/games/sword.js +++ b/examples/example/games/sword.js @@ -11,7 +11,7 @@ // "use strict"; /*jslint vars: true*/ -var Script, Entities, MyAvatar, Window, Controller, Vec3, Quat; // Referenced globals provided by High Fidelity. +var Script, Entities, MyAvatar, Window, Overlays, Controller, Vec3, Quat, print; // Referenced globals provided by High Fidelity. var hand = "right"; var nullActionID = "00000000-0000-0000-0000-000000000000"; @@ -20,39 +20,86 @@ var controllerActive; var stickID = null; var actionID = nullActionID; var dimensions = { x: 0.3, y: 0.1, z: 2.0 }; +var AWAY_ORIENTATION = Quat.fromPitchYawRollDegrees(-90, 0, 0); var stickModel = "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx"; var swordModel = "https://hifi-public.s3.amazonaws.com/ozan/props/sword/sword.fbx"; var whichModel = "sword"; +var rezButton = Overlays.addOverlay("image", { + x: 100, + y: 380, + width: 32, + height: 32, + imageURL: "http://s3.amazonaws.com/hifi-public/images/delete.png", + color: { + red: 255, + green: 255, + blue: 255 + }, + alpha: 1 +}); -// sometimes if this is run immediately the stick doesn't get created? use a timer. -Script.setTimeout(function () { - stickID = Entities.addEntity({ - type: "Model", - modelURL: (whichModel === "sword") ? swordModel : stickModel, - //compoundShapeURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.obj", - shapeType: "box", - dimensions: dimensions, - position: MyAvatar.getRightPalmPosition(), // initial position doesn't matter, as long as it's close - rotation: MyAvatar.orientation, - damping: 0.1, - collisionSoundURL: "http://public.highfidelity.io/sounds/Collisions-hitsandslaps/swordStrike1.wav", - restitution: 0.01, - collisionsWillMove: true - }); - actionID = Entities.addAction("hold", stickID, {relativePosition: {x: 0.0, y: 0.0, z: -dimensions.z / 2}, - hand: hand, - timeScale: 0.15}); -}, 3000); - - -function cleanUp() { - Entities.deleteEntity(stickID); +var health = 100; +var display; +var isAway = false; +function updateDisplay() { + var text = health.toString(); + if (!display) { + health = 100; + display = Overlays.addOverlay("text", { + text: text, + font: { size: 20 }, + color: {red: 0, green: 255, blue: 0}, + backgroundColor: {red: 100, green: 100, blue: 100}, // Why doesn't this and the next work? + backgroundAlpha: 0.9, + x: Window.innerWidth - 50, + y: 50 + }); + } else { + Overlays.editOverlay(display, {text: text}); + } +} +function removeDisplay() { + if (display) { + Overlays.deleteOverlay(display); + display = null; + } } +function cleanUp() { + if (stickID) { + Entities.deleteAction(stickID, actionID); + Entities.deleteEntity(stickID); + stickID = null; + actionID = null; + } + removeDisplay(); + Overlays.deleteOverlay(rezButton); +} + +function computeEnergy(collision, entityID) { + var id = entityID || collision.idA || collision.idB; + var entity = id && Entities.getEntityProperties(id); + var mass = entity ? (entity.density * entity.dimensions.x * entity.dimensions.y * entity.dimensions.z) : 1; + var linearVelocityChange = Vec3.length(collision.velocityChange); + var energy = 0.5 * mass * linearVelocityChange * linearVelocityChange; + return Math.min(Math.max(1.0, Math.round(energy)), 20); +} +function gotHit(collision) { + if (isAway) { return; } + var energy = computeEnergy(collision); + health -= energy; + updateDisplay(); +} +function scoreHit(idA, idB, collision) { + if (isAway) { return; } + var energy = computeEnergy(collision, idA); + health += energy; + updateDisplay(); +} function positionStick(stickOrientation) { - var baseOffset = {x: 0.0, y: 0.0, z: -dimensions.z / 2}; + var baseOffset = {x: 0.3, y: 0.0, z: -dimensions.z / 2}; // FIXME: don't move yourself by colliding with your own capsule. Fudge of 0.3 in x. var offset = Vec3.multiplyQbyV(stickOrientation, baseOffset); Entities.updateAction(stickID, actionID, {relativePosition: offset, relativeRotation: stickOrientation}); @@ -60,7 +107,7 @@ function positionStick(stickOrientation) { function mouseMoveEvent(event) { - if (!stickID || actionID === nullActionID) { + if (!stickID || actionID === nullActionID || isAway) { return; } var windowCenterX = Window.innerWidth / 2; @@ -98,7 +145,51 @@ function update() { positionStick(stickOrientation); } +function toggleAway() { + isAway = !isAway; + if (isAway) { + positionStick(AWAY_ORIENTATION); + removeDisplay(); + } else { + updateDisplay(); + } +} + +function onClick(event) { + switch (Overlays.getOverlayAtPoint({x: event.x, y: event.y})) { + case rezButton: + if (!stickID) { + stickID = Entities.addEntity({ + type: "Model", + modelURL: (whichModel === "sword") ? swordModel : stickModel, + //compoundShapeURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.obj", + shapeType: "box", + dimensions: dimensions, + position: MyAvatar.getRightPalmPosition(), // initial position doesn't matter, as long as it's close + rotation: MyAvatar.orientation, + damping: 0.1, + collisionSoundURL: "http://public.highfidelity.io/sounds/Collisions-hitsandslaps/swordStrike1.wav", + restitution: 0.01, + collisionsWillMove: true + }); + actionID = Entities.addAction("hold", stickID, {relativePosition: {x: 0.0, y: 0.0, z: -dimensions.z / 2}, + hand: hand, + timeScale: 0.15}); + if (actionID === nullActionID) { + print('*** FAILED TO MAKE SWORD ACTION ***'); + cleanUp(); + } + Script.addEventHandler(stickID, 'collisionWithEntity', scoreHit); + updateDisplay(); + } else { + toggleAway(); + } + break; + } +} Script.scriptEnding.connect(cleanUp); Controller.mouseMoveEvent.connect(mouseMoveEvent); +Controller.mousePressEvent.connect(onClick); Script.update.connect(update); +MyAvatar.collisionWithEntity.connect(gotHit); From a6ec668b2e5b116eaa0e82dbe5375ee141817a70 Mon Sep 17 00:00:00 2001 From: Niraj Venkat Date: Wed, 8 Jul 2015 14:24:24 -0700 Subject: [PATCH 031/107] attempt #1 to fix jenkins build error --- libraries/gpu/src/gpu/GLBackendQuery.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/libraries/gpu/src/gpu/GLBackendQuery.cpp b/libraries/gpu/src/gpu/GLBackendQuery.cpp index 2bc6a7896c..6e448871c4 100644 --- a/libraries/gpu/src/gpu/GLBackendQuery.cpp +++ b/libraries/gpu/src/gpu/GLBackendQuery.cpp @@ -61,7 +61,7 @@ GLuint GLBackend::getQueryID(const QueryPointer& query) { } void GLBackend::do_beginQuery(Batch& batch, uint32 paramOffset) { - auto& query = batch._queries.get(batch._params[paramOffset]._uint); + auto query = batch._queries.get(batch._params[paramOffset]._uint); GLQuery* glquery = syncGPUObject(*query); if (glquery) { glBeginQuery(GL_TIME_ELAPSED, glquery->_qo); @@ -70,7 +70,7 @@ void GLBackend::do_beginQuery(Batch& batch, uint32 paramOffset) { } void GLBackend::do_endQuery(Batch& batch, uint32 paramOffset) { - auto& query = batch._queries.get(batch._params[paramOffset]._uint); + auto query = batch._queries.get(batch._params[paramOffset]._uint); GLQuery* glquery = syncGPUObject(*query); if (glquery) { glEndQuery(GL_TIME_ELAPSED); @@ -79,14 +79,9 @@ void GLBackend::do_endQuery(Batch& batch, uint32 paramOffset) { } void GLBackend::do_getQuery(Batch& batch, uint32 paramOffset) { - auto& query = batch._queries.get(batch._params[paramOffset]._uint); + auto query = batch._queries.get(batch._params[paramOffset]._uint); GLQuery* glquery = syncGPUObject(*query); - if (glquery) { - GLint available = 0; - while (!available) { - glGetQueryObjectiv(glquery->_qo, GL_QUERY_RESULT_AVAILABLE, &available); - } - + if (glquery) { glGetQueryObjectui64v(glquery->_qo, GL_QUERY_RESULT, &glquery->_result); (void)CHECK_GL_ERROR(); } From f992e875b452c900d43037d35c3493f145b38230 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Wed, 8 Jul 2015 14:25:13 -0700 Subject: [PATCH 032/107] Adding intrsumentation for nsight and hiding the nsight specific code in cpp --- interface/src/Application.cpp | 2 +- interface/src/GLCanvas.cpp | 1 + .../src/RenderableModelEntityItem.cpp | 1 + libraries/gpu/src/gpu/Batch.cpp | 11 ++++++++++ libraries/gpu/src/gpu/Batch.h | 10 ++-------- libraries/gpu/src/gpu/Context.cpp | 2 ++ libraries/render-utils/src/Model.cpp | 20 +++++++++++++++---- libraries/render/CMakeLists.txt | 14 ++++++++++++- libraries/render/src/render/Scene.cpp | 2 ++ 9 files changed, 49 insertions(+), 14 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index c1397beb28..46e127b1b3 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1182,7 +1182,6 @@ bool Application::event(QEvent* event) { } bool Application::eventFilter(QObject* object, QEvent* event) { - if (event->type() == QEvent::ShortcutOverride) { if (DependencyManager::get()->shouldSwallowShortcut(event)) { event->accept(); @@ -1787,6 +1786,7 @@ void Application::checkFPS() { } void Application::idle() { + PROFILE_RANGE(__FUNCTION__); static SimpleAverage interIdleDurations; static uint64_t lastIdleEnd{ 0 }; diff --git a/interface/src/GLCanvas.cpp b/interface/src/GLCanvas.cpp index 0eda678fa9..aff7c75cc9 100644 --- a/interface/src/GLCanvas.cpp +++ b/interface/src/GLCanvas.cpp @@ -58,6 +58,7 @@ void GLCanvas::initializeGL() { } void GLCanvas::paintGL() { + PROFILE_RANGE(__FUNCTION__); if (!_throttleRendering && !Application::getInstance()->getWindow()->isMinimized()) { Application::getInstance()->paintGL(); } diff --git a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp index ae1c97f07f..85b7bafc78 100644 --- a/libraries/entities-renderer/src/RenderableModelEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableModelEntityItem.cpp @@ -161,6 +161,7 @@ namespace render { template <> void payloadRender(const RenderableModelEntityItemMeta::Pointer& payload, RenderArgs* args) { if (args) { if (payload && payload->entity) { + PROFILE_RANGE("MetaModelRender"); payload->entity->render(args); } } diff --git a/libraries/gpu/src/gpu/Batch.cpp b/libraries/gpu/src/gpu/Batch.cpp index ee028e79e6..98d7b70db8 100644 --- a/libraries/gpu/src/gpu/Batch.cpp +++ b/libraries/gpu/src/gpu/Batch.cpp @@ -12,6 +12,17 @@ #include +#if defined(NSIGHT_FOUND) +#include "nvToolsExt.h" + +ProfileRange::ProfileRange(const char *name) { + nvtxRangePush(name); +} +ProfileRange::~ProfileRange() { + nvtxRangePop(); +} +#endif + #define ADD_COMMAND(call) _commands.push_back(COMMAND_##call); _commandOffsets.push_back(_params.size()); using namespace gpu; diff --git a/libraries/gpu/src/gpu/Batch.h b/libraries/gpu/src/gpu/Batch.h index 835e872b4a..5583294aba 100644 --- a/libraries/gpu/src/gpu/Batch.h +++ b/libraries/gpu/src/gpu/Batch.h @@ -26,17 +26,11 @@ #include "Framebuffer.h" #if defined(NSIGHT_FOUND) - #include "nvToolsExt.h" class ProfileRange { public: - ProfileRange(const char *name) { - nvtxRangePush(name); - } - ~ProfileRange() { - nvtxRangePop(); - } + ProfileRange(const char *name); + ~ProfileRange(); }; - #define PROFILE_RANGE(name) ProfileRange profileRangeThis(name); #else #define PROFILE_RANGE(name) diff --git a/libraries/gpu/src/gpu/Context.cpp b/libraries/gpu/src/gpu/Context.cpp index 9cc6bb3cd7..51335f78df 100644 --- a/libraries/gpu/src/gpu/Context.cpp +++ b/libraries/gpu/src/gpu/Context.cpp @@ -33,9 +33,11 @@ bool Context::makeProgram(Shader& shader, const Shader::BindingSet& bindings) { } void Context::render(Batch& batch) { + PROFILE_RANGE(__FUNCTION__); _backend->render(batch); } void Context::syncCache() { + PROFILE_RANGE(__FUNCTION__); _backend->syncCache(); } \ No newline at end of file diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index 1b94c70e57..03140c4dfb 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -417,6 +417,7 @@ void Model::reset() { } bool Model::updateGeometry() { + PROFILE_RANGE(__FUNCTION__); bool needFullUpdate = false; bool needToRebuild = false; @@ -690,6 +691,7 @@ void Model::recalculateMeshPartOffsets() { // entity-scripts to call. I think it would be best to do the picking once-per-frame (in cpu, or gpu if possible) // and then the calls use the most recent such result. void Model::recalculateMeshBoxes(bool pickAgainstTriangles) { + PROFILE_RANGE(__FUNCTION__); bool calculatedMeshTrianglesNeeded = pickAgainstTriangles && !_calculatedMeshTrianglesValid; if (!_calculatedMeshBoxesValid || calculatedMeshTrianglesNeeded || (!_calculatedMeshPartBoxesValid && pickAgainstTriangles) ) { @@ -1281,6 +1283,7 @@ Blender::Blender(Model* model, int blendNumber, const QWeakPointer vertices, normals; if (!_model.isNull()) { int offset = 0; @@ -1392,6 +1395,7 @@ void Model::snapToRegistrationPoint() { } void Model::simulate(float deltaTime, bool fullUpdate) { + PROFILE_RANGE(__FUNCTION__); fullUpdate = updateGeometry() || fullUpdate || (_scaleToFit && !_scaledToFit) || (_snapModelToRegistrationPoint && !_snappedToRegistrationPoint); @@ -1829,6 +1833,7 @@ void Model::setupBatchTransform(gpu::Batch& batch, RenderArgs* args) { } AABox Model::getPartBounds(int meshIndex, int partIndex) { + if (meshIndex < _meshStates.size()) { const MeshState& state = _meshStates.at(meshIndex); bool isSkinned = state.clusterMatrices.size() > 1; @@ -1859,6 +1864,7 @@ AABox Model::getPartBounds(int meshIndex, int partIndex) { } void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool translucent) { + // PROFILE_RANGE(__FUNCTION__); PerformanceTimer perfTimer("Model::renderPart"); if (!_readyWhenAdded) { return; // bail asap @@ -1933,7 +1939,9 @@ void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool tran pickPrograms(batch, mode, translucentMesh, alphaThreshold, hasLightmap, hasTangents, hasSpecular, isSkinned, wireframe, args, locations); - updateVisibleJointStates(); + { + updateVisibleJointStates(); + } // if our index is ever out of range for either meshes or networkMeshes, then skip it, and set our _meshGroupsKnown // to false to rebuild out mesh groups. @@ -2076,9 +2084,13 @@ void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool tran } } - _mutex.lock(); - qint64 offset = _calculatedMeshPartOffset[QPair(meshIndex, partIndex)]; - _mutex.unlock(); + qint64 offset; + { + // FIXME_STUTTER: We should n't have any lock here + _mutex.lock(); + offset = _calculatedMeshPartOffset[QPair(meshIndex, partIndex)]; + _mutex.unlock(); + } if (part.quadIndices.size() > 0) { batch.drawIndexed(gpu::QUADS, part.quadIndices.size(), offset); diff --git a/libraries/render/CMakeLists.txt b/libraries/render/CMakeLists.txt index ee99eb00b9..4d2be949e6 100644 --- a/libraries/render/CMakeLists.txt +++ b/libraries/render/CMakeLists.txt @@ -9,4 +9,16 @@ add_dependency_external_projects(glm) find_package(GLM REQUIRED) target_include_directories(${TARGET_NAME} PUBLIC ${GLM_INCLUDE_DIRS}) -link_hifi_libraries(shared gpu model) \ No newline at end of file +link_hifi_libraries(shared gpu model) + +if (WIN32) + if (USE_NSIGHT) + # try to find the Nsight package and add it to the build if we find it + find_package(NSIGHT) + if (NSIGHT_FOUND) + include_directories(${NSIGHT_INCLUDE_DIRS}) + add_definitions(-DNSIGHT_FOUND) + target_link_libraries(${TARGET_NAME} "${NSIGHT_LIBRARIES}") + endif () + endif() +endif (WIN32) \ No newline at end of file diff --git a/libraries/render/src/render/Scene.cpp b/libraries/render/src/render/Scene.cpp index a7145af4b5..268f2b6841 100644 --- a/libraries/render/src/render/Scene.cpp +++ b/libraries/render/src/render/Scene.cpp @@ -11,6 +11,7 @@ #include "Scene.h" #include +#include "gpu/Batch.h" using namespace render; @@ -167,6 +168,7 @@ void consolidateChangeQueue(PendingChangesQueue& queue, PendingChanges& singleBa } void Scene::processPendingChangesQueue() { + PROFILE_RANGE(__FUNCTION__); _changeQueueMutex.lock(); PendingChanges consolidatedPendingChanges; consolidateChangeQueue(_changeQueue, consolidatedPendingChanges); From 6a9344a3f6b5f966a3d9b8d5c64db8f4008fe6ea Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 8 Jul 2015 15:14:24 -0700 Subject: [PATCH 033/107] fix bug in old version of decode --- libraries/shared/src/ByteCountCoding.h | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/libraries/shared/src/ByteCountCoding.h b/libraries/shared/src/ByteCountCoding.h index 2a39ee7a8c..efde19d45b 100644 --- a/libraries/shared/src/ByteCountCoding.h +++ b/libraries/shared/src/ByteCountCoding.h @@ -21,6 +21,8 @@ #include #include +#include + #include #include @@ -111,12 +113,12 @@ template inline QByteArray ByteCountCoded::encode() const { } template inline void ByteCountCoded::decode(const QByteArray& fromEncodedBytes) { - // first convert the ByteArray into a BitArray... QBitArray encodedBits; int bitCount = BITS_IN_BYTE * fromEncodedBytes.count(); encodedBits.resize(bitCount); + // copies the QByteArray into a QBitArray for(int byte = 0; byte < fromEncodedBytes.count(); byte++) { char originalByte = fromEncodedBytes.at(byte); for(int bit = 0; bit < BITS_IN_BYTE; bit++) { @@ -128,8 +130,8 @@ template inline void ByteCountCoded::decode(const QByteArray& fro } // next, read the leading bits to determine the correct number of bytes to decode (may not match the QByteArray) - int encodedByteCount = 0; - int leadBits = 1; + int encodedByteCount = 1; // there is at least 1 byte (after the leadBits) + int leadBits = 1; // there is always at least 1 lead bit int bitAt; for (bitAt = 0; bitAt < bitCount; bitAt++) { if (encodedBits.at(bitAt)) { @@ -139,16 +141,15 @@ template inline void ByteCountCoded::decode(const QByteArray& fro break; } } - encodedByteCount++; // always at least one byte - int expectedBitCount = encodedByteCount * BITS_IN_BYTE; + int expectedBitCount = (encodedByteCount * BITS_IN_BYTE) - leadBits; T value = 0; - + if (expectedBitCount <= (encodedBits.size() - leadBits)) { // Now, keep reading... int valueStartsAt = bitAt + 1; T bitValue = 1; - for (bitAt = valueStartsAt; bitAt < expectedBitCount; bitAt++) { + for (bitAt = valueStartsAt; bitAt < (expectedBitCount + leadBits); bitAt++) { if(encodedBits.at(bitAt)) { value += bitValue; } From 250d2e9e2c061916b5554729febc827eaea3e51a Mon Sep 17 00:00:00 2001 From: Niraj Venkat Date: Wed, 8 Jul 2015 15:14:28 -0700 Subject: [PATCH 034/107] attempt #2 to fix jenkins build error --- libraries/gpu/src/gpu/GLBackend.h | 2 +- libraries/gpu/src/gpu/GLBackendQuery.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/gpu/src/gpu/GLBackend.h b/libraries/gpu/src/gpu/GLBackend.h index 6313e8e7ac..b142e63e65 100644 --- a/libraries/gpu/src/gpu/GLBackend.h +++ b/libraries/gpu/src/gpu/GLBackend.h @@ -191,7 +191,7 @@ public: class GLQuery : public GPUObject { public: GLuint _qo = 0; - GLuint64 _result = 0; + GLuint _result = 0; GLQuery(); ~GLQuery(); diff --git a/libraries/gpu/src/gpu/GLBackendQuery.cpp b/libraries/gpu/src/gpu/GLBackendQuery.cpp index 6e448871c4..bcd291f7e7 100644 --- a/libraries/gpu/src/gpu/GLBackendQuery.cpp +++ b/libraries/gpu/src/gpu/GLBackendQuery.cpp @@ -82,7 +82,7 @@ void GLBackend::do_getQuery(Batch& batch, uint32 paramOffset) { auto query = batch._queries.get(batch._params[paramOffset]._uint); GLQuery* glquery = syncGPUObject(*query); if (glquery) { - glGetQueryObjectui64v(glquery->_qo, GL_QUERY_RESULT, &glquery->_result); + glGetQueryObjectuiv(glquery->_qo, GL_QUERY_RESULT, &glquery->_result); (void)CHECK_GL_ERROR(); } } From 6813a3afe134d4b22fc029fb1decf72c47c5fbe0 Mon Sep 17 00:00:00 2001 From: Howard Stearns Date: Wed, 8 Jul 2015 15:27:45 -0700 Subject: [PATCH 035/107] Sword: + init hands. + toolbar of brandishToggle/makeTarget/cleanup. + initial hit flash using overlays instead of ogl "fade". --- examples/example/games/sword.js | 102 +++++++++++++++++++++++++++----- 1 file changed, 86 insertions(+), 16 deletions(-) diff --git a/examples/example/games/sword.js b/examples/example/games/sword.js index 995015b227..115a407cc8 100644 --- a/examples/example/games/sword.js +++ b/examples/example/games/sword.js @@ -11,7 +11,8 @@ // "use strict"; /*jslint vars: true*/ -var Script, Entities, MyAvatar, Window, Overlays, Controller, Vec3, Quat, print; // Referenced globals provided by High Fidelity. +var Script, Entities, MyAvatar, Window, Overlays, Controller, Vec3, Quat, print, ToolBar; // Referenced globals provided by High Fidelity. +Script.include(["../../libraries/toolBars.js"]); var hand = "right"; var nullActionID = "00000000-0000-0000-0000-000000000000"; @@ -19,25 +20,61 @@ var controllerID; var controllerActive; var stickID = null; var actionID = nullActionID; +var targetIDs = []; var dimensions = { x: 0.3, y: 0.1, z: 2.0 }; var AWAY_ORIENTATION = Quat.fromPitchYawRollDegrees(-90, 0, 0); +var BUTTON_SIZE = 32; var stickModel = "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx"; var swordModel = "https://hifi-public.s3.amazonaws.com/ozan/props/sword/sword.fbx"; var whichModel = "sword"; -var rezButton = Overlays.addOverlay("image", { - x: 100, - y: 380, - width: 32, - height: 32, - imageURL: "http://s3.amazonaws.com/hifi-public/images/delete.png", - color: { - red: 255, - green: 255, - blue: 255 - }, +var toolBar = new ToolBar(0, 0, ToolBar.vertical, "highfidelity.sword.toolbar", function () { + return {x: 100, y: 380}; +}); + +var SWORD_IMAGE = "http://s3.amazonaws.com/hifi-public/images/billiardsReticle.png"; // Toggle between brandishing/sheathing sword (creating if necessary) +var TARGET_IMAGE = "http://s3.amazonaws.com/hifi-public/images/puck.png"; // Create a target dummy +var CLEANUP_IMAGE = "http://s3.amazonaws.com/hifi-public/images/delete.png"; // Remove sword and all target dummies.f +var swordButton = toolBar.addOverlay("image", { + width: BUTTON_SIZE, + height: BUTTON_SIZE, + imageURL: SWORD_IMAGE, alpha: 1 }); +var targetButton = toolBar.addOverlay("image", { + width: BUTTON_SIZE, + height: BUTTON_SIZE, + imageURL: TARGET_IMAGE, + alpha: 1 +}); +var cleanupButton = toolBar.addOverlay("image", { + width: BUTTON_SIZE, + height: BUTTON_SIZE, + imageURL: CLEANUP_IMAGE, + alpha: 1 +}); + +var flasher; +function clearFlash() { + if (!flasher) { + return; + } + Script.clearTimeout(flasher.timer); + Overlays.deleteOverlay(flasher.overlay); + flasher = null; +} +function flash(color) { + clearFlash(); + flasher = {}; + flasher.overlay = Overlays.addOverlay("text", { + backgroundColor: color, + backgroundAlpha: 0.7, + width: Window.innerWidth, + height: Window.innerHeight + }); + flasher.timer = Script.setTimeout(clearFlash, 500); +} + var health = 100; var display; @@ -66,15 +103,22 @@ function removeDisplay() { } } -function cleanUp() { +function cleanUp(leaveButtons) { if (stickID) { Entities.deleteAction(stickID, actionID); Entities.deleteEntity(stickID); stickID = null; actionID = null; } + targetIDs.forEach(function (id) { + Entities.deleteAction(id.entity, id.action); + Entities.deleteEntity(id.entity); + }); + targetIDs = []; removeDisplay(); - Overlays.deleteOverlay(rezButton); + if (!leaveButtons) { + toolBar.cleanup(); + } } function computeEnergy(collision, entityID) { @@ -89,12 +133,14 @@ function gotHit(collision) { if (isAway) { return; } var energy = computeEnergy(collision); health -= energy; + flash({red: 255, green: 0, blue: 0}); updateDisplay(); } function scoreHit(idA, idB, collision) { if (isAway) { return; } var energy = computeEnergy(collision, idA); health += energy; + flash({red: 0, green: 255, blue: 0}); updateDisplay(); } @@ -156,9 +202,10 @@ function toggleAway() { } function onClick(event) { - switch (Overlays.getOverlayAtPoint({x: event.x, y: event.y})) { - case rezButton: + switch (Overlays.getOverlayAtPoint(event)) { + case swordButton: if (!stickID) { + initControls(); stickID = Entities.addEntity({ type: "Model", modelURL: (whichModel === "sword") ? swordModel : stickModel, @@ -185,6 +232,29 @@ function onClick(event) { toggleAway(); } break; + case targetButton: + var position = Vec3.sum(MyAvatar.position, {x: 1.0, y: 0.4, z: 0.0}); + var boxId = Entities.addEntity({ + type: "Box", + name: "dummy", + position: position, + dimensions: {x: 0.3, y: 0.7, z: 0.3}, + gravity: {x: 0.0, y: -3.0, z: 0.0}, + damping: 0.2, + collisionsWillMove: true + }); + + var pointToOffsetFrom = Vec3.sum(position, {x: 0.0, y: 2.0, z: 0.0}); + var action = Entities.addAction("offset", boxId, {pointToOffsetFrom: pointToOffsetFrom, + linearDistance: 2.0, + // linearTimeScale: 0.005 + linearTimeScale: 0.1 + }); + targetIDs.push({entity: boxId, action: action}); + break; + case cleanupButton: + cleanUp('leaveButtons'); + break; } } From 4478d76618efb2b7ada6a2444e4990490639c754 Mon Sep 17 00:00:00 2001 From: Niraj Venkat Date: Wed, 8 Jul 2015 15:28:25 -0700 Subject: [PATCH 036/107] attempt #3 - fixing ubuntu build --- libraries/gpu/src/gpu/GLBackendQuery.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libraries/gpu/src/gpu/GLBackendQuery.cpp b/libraries/gpu/src/gpu/GLBackendQuery.cpp index bcd291f7e7..a6e34a90ca 100644 --- a/libraries/gpu/src/gpu/GLBackendQuery.cpp +++ b/libraries/gpu/src/gpu/GLBackendQuery.cpp @@ -11,6 +11,11 @@ #include "GPULogging.h" #include "GLBackendShared.h" +#ifndef GL_ARB_timer_query +#define GL_TIME_ELAPSED 0x88BF +#define GL_TIMESTAMP 0x8E28 +#endif + using namespace gpu; GLBackend::GLQuery::GLQuery() {} From 1894b42773745e8f46e142aef3ee397f82cd034d Mon Sep 17 00:00:00 2001 From: Niraj Venkat Date: Wed, 8 Jul 2015 16:11:36 -0700 Subject: [PATCH 037/107] Added EXT support for Mac GL Query calls --- libraries/gpu/src/gpu/GLBackendQuery.cpp | 24 ++++++++++++++----- .../render-utils/src/RenderDeferredTask.cpp | 17 ------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/libraries/gpu/src/gpu/GLBackendQuery.cpp b/libraries/gpu/src/gpu/GLBackendQuery.cpp index a6e34a90ca..585d7de5ff 100644 --- a/libraries/gpu/src/gpu/GLBackendQuery.cpp +++ b/libraries/gpu/src/gpu/GLBackendQuery.cpp @@ -11,10 +11,6 @@ #include "GPULogging.h" #include "GLBackendShared.h" -#ifndef GL_ARB_timer_query -#define GL_TIME_ELAPSED 0x88BF -#define GL_TIMESTAMP 0x8E28 -#endif using namespace gpu; @@ -70,6 +66,12 @@ void GLBackend::do_beginQuery(Batch& batch, uint32 paramOffset) { GLQuery* glquery = syncGPUObject(*query); if (glquery) { glBeginQuery(GL_TIME_ELAPSED, glquery->_qo); + #if (GPU_FEATURE_PROFILE == GPU_LEGACY) + // (EXT_TIMER_QUERY) + glBeginQuery(TIME_ELAPSED_EXT, glquery->_qo); + #else + glBeginQuery(GL_TIME_ELAPSED, glquery->_qo); + #endif (void)CHECK_GL_ERROR(); } } @@ -78,7 +80,12 @@ void GLBackend::do_endQuery(Batch& batch, uint32 paramOffset) { auto query = batch._queries.get(batch._params[paramOffset]._uint); GLQuery* glquery = syncGPUObject(*query); if (glquery) { - glEndQuery(GL_TIME_ELAPSED); + #if (GPU_FEATURE_PROFILE == GPU_LEGACY) + // (EXT_TIMER_QUERY) + glEndQuery(TIME_ELAPSED_EXT); + #else + glEndQuery(GL_TIME_ELAPSED); + #endif (void)CHECK_GL_ERROR(); } } @@ -87,7 +94,12 @@ void GLBackend::do_getQuery(Batch& batch, uint32 paramOffset) { auto query = batch._queries.get(batch._params[paramOffset]._uint); GLQuery* glquery = syncGPUObject(*query); if (glquery) { - glGetQueryObjectuiv(glquery->_qo, GL_QUERY_RESULT, &glquery->_result); + #if (GPU_FEATURE_PROFILE == GPU_LEGACY) + // (EXT_TIMER_QUERY) + GetQueryObjectui64vEXT(glquery->_qo, GL_QUERY_RESULT, &glquery->_result); + #else + glGetQueryObjectui64v(glquery->_qo, GL_QUERY_RESULT, &glquery->_result); + #endif (void)CHECK_GL_ERROR(); } } diff --git a/libraries/render-utils/src/RenderDeferredTask.cpp b/libraries/render-utils/src/RenderDeferredTask.cpp index 56980058c8..9ae0bd1b36 100755 --- a/libraries/render-utils/src/RenderDeferredTask.cpp +++ b/libraries/render-utils/src/RenderDeferredTask.cpp @@ -104,27 +104,10 @@ void RenderDeferredTask::run(const SceneContextPointer& sceneContext, const Rend renderContext->args->_context->syncCache(); - // start the current timer query - auto& currentQuery = _timerQueries[_currentTimerQueryIndex]; - { - gpu::Batch batch; - batch.beginQuery(currentQuery); - renderContext->args->_context->render(batch); - } - for (auto job : _jobs) { job.run(sceneContext, renderContext); } - // End the current timer query - { - gpu::Batch batch; - batch.endQuery(currentQuery); - batch.getQuery(currentQuery); - renderContext->args->_context->render(batch); - (_currentTimerQueryIndex++); - _currentTimerQueryIndex = _currentTimerQueryIndex% _timerQueries.size(); - } }; void DrawOpaqueDeferred::run(const SceneContextPointer& sceneContext, const RenderContextPointer& renderContext, const ItemIDsBounds& inItems) { From 25a1108b612e9d78a2de8a157f37b272b3491cce Mon Sep 17 00:00:00 2001 From: Bing Shearer Date: Wed, 8 Jul 2015 16:24:23 -0700 Subject: [PATCH 038/107] Fixed spelling error --- libraries/script-engine/src/ScriptEngine.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/script-engine/src/ScriptEngine.cpp b/libraries/script-engine/src/ScriptEngine.cpp index 55cf9e9890..2046fd622d 100644 --- a/libraries/script-engine/src/ScriptEngine.cpp +++ b/libraries/script-engine/src/ScriptEngine.cpp @@ -493,7 +493,7 @@ void ScriptEngine::evaluate() { QScriptValue result = evaluate(_scriptContents); - // TODO: why do we check this twice? It seems like the call to clearExcpetions() in the lower level evaluate call + // TODO: why do we check this twice? It seems like the call to clearExceptions() in the lower level evaluate call // will cause this code to never actually run... if (hasUncaughtException()) { int line = uncaughtExceptionLineNumber(); From 6080ec4509ae1292dc8db991fcbcf31ea262afc2 Mon Sep 17 00:00:00 2001 From: Bing Shearer Date: Wed, 8 Jul 2015 16:29:43 -0700 Subject: [PATCH 039/107] Air Hockey UI now follows window border --- examples/example/games/airHockey.js | 69 ++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 22 deletions(-) mode change 100644 => 100755 examples/example/games/airHockey.js diff --git a/examples/example/games/airHockey.js b/examples/example/games/airHockey.js old mode 100644 new mode 100755 index a703f379bc..f70fbc232f --- a/examples/example/games/airHockey.js +++ b/examples/example/games/airHockey.js @@ -83,35 +83,60 @@ var puck_name_index = 2; var light_name_index = 3; var floor_name_index = 4; +//Create Spawn and Del. Button Vars. + +function updateButtonPosition() { + Overlays.editOverlay(spawnButton, { + x: screenSize.x / 2 + PADDING, + y: screenSize.y - (BUTTON_SIZE * 2 + PADDING), + }); + Overlays.editOverlay(deleteButton, { + x: screenSize.x / 2 - BUTTON_SIZE, + y: screenSize.y - (BUTTON_SIZE * 2 + PADDING), + }); +} + +function onScriptUpdate() { + var oldScreenSize = screenSize; + + screenSize = Controller.getViewportDimensions(); + + if (screenSize.x !== oldScreenSize.x || screenSize.y !== oldScreenSize.y) { + updateButtonPosition(); + } +} + +screenSize = Controller.getViewportDimensions(); var deleteButton = Overlays.addOverlay("image", { - x: screenSize.x / 2 - BUTTON_SIZE, - y: screenSize.y - (BUTTON_SIZE * 2 + PADDING), - width: BUTTON_SIZE, - height: BUTTON_SIZE, - imageURL: HIFI_PUBLIC_BUCKET + "images/delete.png", - color: { - red: 255, - green: 255, - blue: 255 - }, - alpha: 1 + x: screenSize.x / 2 - BUTTON_SIZE, + y: screenSize.y - (BUTTON_SIZE * 2 + PADDING), + width: BUTTON_SIZE, + height: BUTTON_SIZE, + imageURL: HIFI_PUBLIC_BUCKET + "images/delete.png", + color: { + red: 255, + green: 255, + blue: 255 + }, + alpha: 1 }); var spawnButton = Overlays.addOverlay("image", { - x: screenSize.x / 2 + PADDING, - y: screenSize.y - (BUTTON_SIZE * 2 + PADDING), - width: BUTTON_SIZE, - height: BUTTON_SIZE, - imageURL: HIFI_PUBLIC_BUCKET + "images/puck.png", - color: { - red: 255, - green: 255, - blue: 255 - }, - alpha: 1 + x: screenSize.x / 2 + PADDING, + y: screenSize.y - (BUTTON_SIZE * 2 + PADDING), + width: BUTTON_SIZE, + height: BUTTON_SIZE, + imageURL: HIFI_PUBLIC_BUCKET + "images/puck.png", + color: { + red: 255, + green: 255, + blue: 255 + }, + alpha: 1 }); +Script.update.connect(onScriptUpdate); var floor, edge1, edge2, edge3a, edge3b, edge4a, edge4b, light; From ba4049754ae7f237e3d1d2a44884abf22b8641d4 Mon Sep 17 00:00:00 2001 From: Bing Shearer Date: Wed, 8 Jul 2015 16:53:55 -0700 Subject: [PATCH 040/107] Restored indentatioN --- examples/example/games/airHockey.js | 44 ++++++++++++++--------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/examples/example/games/airHockey.js b/examples/example/games/airHockey.js index f70fbc232f..ca7f007df6 100755 --- a/examples/example/games/airHockey.js +++ b/examples/example/games/airHockey.js @@ -109,31 +109,31 @@ function onScriptUpdate() { screenSize = Controller.getViewportDimensions(); var deleteButton = Overlays.addOverlay("image", { - x: screenSize.x / 2 - BUTTON_SIZE, - y: screenSize.y - (BUTTON_SIZE * 2 + PADDING), - width: BUTTON_SIZE, - height: BUTTON_SIZE, - imageURL: HIFI_PUBLIC_BUCKET + "images/delete.png", - color: { - red: 255, - green: 255, - blue: 255 - }, - alpha: 1 + x: screenSize.x / 2 - BUTTON_SIZE, + y: screenSize.y - (BUTTON_SIZE * 2 + PADDING), + width: BUTTON_SIZE, + height: BUTTON_SIZE, + imageURL: HIFI_PUBLIC_BUCKET + "images/delete.png", + color: { + red: 255, + green: 255, + blue: 255 + }, + alpha: 1 }); var spawnButton = Overlays.addOverlay("image", { - x: screenSize.x / 2 + PADDING, - y: screenSize.y - (BUTTON_SIZE * 2 + PADDING), - width: BUTTON_SIZE, - height: BUTTON_SIZE, - imageURL: HIFI_PUBLIC_BUCKET + "images/puck.png", - color: { - red: 255, - green: 255, - blue: 255 - }, - alpha: 1 + x: screenSize.x / 2 + PADDING, + y: screenSize.y - (BUTTON_SIZE * 2 + PADDING), + width: BUTTON_SIZE, + height: BUTTON_SIZE, + imageURL: HIFI_PUBLIC_BUCKET + "images/puck.png", + color: { + red: 255, + green: 255, + blue: 255 + }, + alpha: 1 }); Script.update.connect(onScriptUpdate); From c8da32521365d7ad0b293056d6b7b2ccc5141895 Mon Sep 17 00:00:00 2001 From: Howard Stearns Date: Wed, 8 Jul 2015 16:59:54 -0700 Subject: [PATCH 041/107] Fudge the offset so that operates only when using the mouse. --- examples/example/games/sword.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/example/games/sword.js b/examples/example/games/sword.js index 115a407cc8..66503b62aa 100644 --- a/examples/example/games/sword.js +++ b/examples/example/games/sword.js @@ -28,6 +28,8 @@ var BUTTON_SIZE = 32; var stickModel = "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx"; var swordModel = "https://hifi-public.s3.amazonaws.com/ozan/props/sword/sword.fbx"; var whichModel = "sword"; +var attachmentOffset, MOUSE_CONTROLLER_OFFSET = {x: 0.5, y: 0.4, z: 0.0}; // A fudge when using mouse rather than hand-controller, to hit yourself less often. + var toolBar = new ToolBar(0, 0, ToolBar.vertical, "highfidelity.sword.toolbar", function () { return {x: 100, y: 380}; }); @@ -104,6 +106,7 @@ function removeDisplay() { } function cleanUp(leaveButtons) { + attachmentOffset = {x: 0, y: 0, z: 0}; if (stickID) { Entities.deleteAction(stickID, actionID); Entities.deleteEntity(stickID); @@ -145,7 +148,7 @@ function scoreHit(idA, idB, collision) { } function positionStick(stickOrientation) { - var baseOffset = {x: 0.3, y: 0.0, z: -dimensions.z / 2}; // FIXME: don't move yourself by colliding with your own capsule. Fudge of 0.3 in x. + var baseOffset = Vec3.sum(attachmentOffset, {x: 0.0, y: 0.0, z: -dimensions.z / 2}); var offset = Vec3.multiplyQbyV(stickOrientation, baseOffset); Entities.updateAction(stickID, actionID, {relativePosition: offset, relativeRotation: stickOrientation}); @@ -153,6 +156,7 @@ function positionStick(stickOrientation) { function mouseMoveEvent(event) { + attachmentOffset = MOUSE_CONTROLLER_OFFSET; if (!stickID || actionID === nullActionID || isAway) { return; } From 58f127f29946f0ad0940219f0f2ba66842183973 Mon Sep 17 00:00:00 2001 From: Niraj Venkat Date: Wed, 8 Jul 2015 17:17:31 -0700 Subject: [PATCH 042/107] Fixing names of GL enums --- libraries/gpu/src/gpu/GLBackend.h | 2 +- libraries/gpu/src/gpu/GLBackendQuery.cpp | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/libraries/gpu/src/gpu/GLBackend.h b/libraries/gpu/src/gpu/GLBackend.h index b142e63e65..6313e8e7ac 100644 --- a/libraries/gpu/src/gpu/GLBackend.h +++ b/libraries/gpu/src/gpu/GLBackend.h @@ -191,7 +191,7 @@ public: class GLQuery : public GPUObject { public: GLuint _qo = 0; - GLuint _result = 0; + GLuint64 _result = 0; GLQuery(); ~GLQuery(); diff --git a/libraries/gpu/src/gpu/GLBackendQuery.cpp b/libraries/gpu/src/gpu/GLBackendQuery.cpp index 585d7de5ff..003f757450 100644 --- a/libraries/gpu/src/gpu/GLBackendQuery.cpp +++ b/libraries/gpu/src/gpu/GLBackendQuery.cpp @@ -65,10 +65,9 @@ void GLBackend::do_beginQuery(Batch& batch, uint32 paramOffset) { auto query = batch._queries.get(batch._params[paramOffset]._uint); GLQuery* glquery = syncGPUObject(*query); if (glquery) { - glBeginQuery(GL_TIME_ELAPSED, glquery->_qo); #if (GPU_FEATURE_PROFILE == GPU_LEGACY) // (EXT_TIMER_QUERY) - glBeginQuery(TIME_ELAPSED_EXT, glquery->_qo); + glBeginQuery(GL_TIME_ELAPSED_EXT, glquery->_qo); #else glBeginQuery(GL_TIME_ELAPSED, glquery->_qo); #endif @@ -82,7 +81,7 @@ void GLBackend::do_endQuery(Batch& batch, uint32 paramOffset) { if (glquery) { #if (GPU_FEATURE_PROFILE == GPU_LEGACY) // (EXT_TIMER_QUERY) - glEndQuery(TIME_ELAPSED_EXT); + glEndQuery(GL_TIME_ELAPSED_EXT); #else glEndQuery(GL_TIME_ELAPSED); #endif @@ -96,7 +95,7 @@ void GLBackend::do_getQuery(Batch& batch, uint32 paramOffset) { if (glquery) { #if (GPU_FEATURE_PROFILE == GPU_LEGACY) // (EXT_TIMER_QUERY) - GetQueryObjectui64vEXT(glquery->_qo, GL_QUERY_RESULT, &glquery->_result); + glGetQueryObjectui64vEXT(glquery->_qo, GL_QUERY_RESULT, &glquery->_result); #else glGetQueryObjectui64v(glquery->_qo, GL_QUERY_RESULT, &glquery->_result); #endif From 98e0688e9859c5ef51f9f08743f4925df990ac41 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Wed, 8 Jul 2015 17:21:38 -0700 Subject: [PATCH 043/107] Reducing heap allocation in network packet parsing --- libraries/entities/src/EntityItem.cpp | 160 +++++++++++++++++------- libraries/entities/src/EntityItemID.cpp | 7 +- libraries/shared/src/BufferParser.h | 123 ++++++++++++++++++ 3 files changed, 240 insertions(+), 50 deletions(-) create mode 100644 libraries/shared/src/BufferParser.h diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 6c27152a97..1165518097 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -12,9 +12,11 @@ #include "EntityItem.h" #include +#include #include +#include #include #include #include @@ -354,50 +356,78 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef // ~27-35 bytes... const int MINIMUM_HEADER_BYTES = 27; - int bytesRead = 0; if (bytesLeftToRead < MINIMUM_HEADER_BYTES) { return 0; } + int clockSkew = args.sourceNode ? args.sourceNode->getClockSkewUsec() : 0; + + BufferParser parser(data, bytesLeftToRead); + +#ifdef DEBUG +#define VALIDATE_ENTITY_ITEM_PARSER 1 +#endif + +#ifdef VALIDATE_ENTITY_ITEM_PARSER + int bytesRead = 0; int originalLength = bytesLeftToRead; // TODO: figure out a way to avoid the big deep copy below. QByteArray originalDataBuffer((const char*)data, originalLength); // big deep copy! - - int clockSkew = args.sourceNode ? args.sourceNode->getClockSkewUsec() : 0; - const unsigned char* dataAt = data; +#endif // id - QByteArray encodedID = originalDataBuffer.mid(bytesRead, NUM_BYTES_RFC4122_UUID); // maximum possible size - _id = QUuid::fromRfc4122(encodedID); - dataAt += encodedID.size(); - bytesRead += encodedID.size(); - + parser.readUuid(_id); +#ifdef VALIDATE_ENTITY_ITEM_PARSER + { + QByteArray encodedID = originalDataBuffer.mid(bytesRead, NUM_BYTES_RFC4122_UUID); // maximum possible size + QUuid id = QUuid::fromRfc4122(encodedID); + dataAt += encodedID.size(); + bytesRead += encodedID.size(); + Q_ASSERT(id == _id); + Q_ASSERT(parser.offset() == bytesRead); + } +#endif + // type + parser.readCompressedCount((quint32&)_type); +#ifdef VALIDATE_ENTITY_ITEM_PARSER QByteArray encodedType = originalDataBuffer.mid(bytesRead); // maximum possible size ByteCountCoded typeCoder = encodedType; encodedType = typeCoder; // determine true length dataAt += encodedType.size(); bytesRead += encodedType.size(); quint32 type = typeCoder; - _type = (EntityTypes::EntityType)type; + EntityTypes::EntityType oldType = (EntityTypes::EntityType)type; + Q_ASSERT(oldType == _type); + Q_ASSERT(parser.offset() == bytesRead); +#endif bool overwriteLocalData = true; // assume the new content overwrites our local data + quint64 now = usecTimestampNow(); // _created - quint64 createdFromBuffer = 0; - memcpy(&createdFromBuffer, dataAt, sizeof(createdFromBuffer)); - dataAt += sizeof(createdFromBuffer); - bytesRead += sizeof(createdFromBuffer); - - quint64 now = usecTimestampNow(); - if (_created == UNKNOWN_CREATED_TIME) { - // we don't yet have a _created timestamp, so we accept this one - createdFromBuffer -= clockSkew; - if (createdFromBuffer > now || createdFromBuffer == UNKNOWN_CREATED_TIME) { - createdFromBuffer = now; + { + quint64 createdFromBuffer = 0; + parser.readValue(createdFromBuffer); +#ifdef VALIDATE_ENTITY_ITEM_PARSER + { + quint64 createdFromBuffer2 = 0; + memcpy(&createdFromBuffer2, dataAt, sizeof(createdFromBuffer2)); + dataAt += sizeof(createdFromBuffer2); + bytesRead += sizeof(createdFromBuffer2); + Q_ASSERT(createdFromBuffer2 == createdFromBuffer); + Q_ASSERT(parser.offset() == bytesRead); + } +#endif + if (_created == UNKNOWN_CREATED_TIME) { + // we don't yet have a _created timestamp, so we accept this one + createdFromBuffer -= clockSkew; + if (createdFromBuffer > now || createdFromBuffer == UNKNOWN_CREATED_TIME) { + createdFromBuffer = now; + } + _created = createdFromBuffer; } - _created = createdFromBuffer; } #ifdef WANT_DEBUG @@ -417,15 +447,21 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef #endif quint64 lastEditedFromBuffer = 0; - quint64 lastEditedFromBufferAdjusted = 0; // TODO: we could make this encoded as a delta from _created // _lastEdited - memcpy(&lastEditedFromBuffer, dataAt, sizeof(lastEditedFromBuffer)); - dataAt += sizeof(lastEditedFromBuffer); - bytesRead += sizeof(lastEditedFromBuffer); - lastEditedFromBufferAdjusted = lastEditedFromBuffer - clockSkew; - + parser.readValue(lastEditedFromBuffer); +#ifdef VALIDATE_ENTITY_ITEM_PARSER + { + quint64 lastEditedFromBuffer2 = 0; + memcpy(&lastEditedFromBuffer2, dataAt, sizeof(lastEditedFromBuffer2)); + dataAt += sizeof(lastEditedFromBuffer2); + bytesRead += sizeof(lastEditedFromBuffer2); + Q_ASSERT(lastEditedFromBuffer2 == lastEditedFromBuffer); + Q_ASSERT(parser.offset() == bytesRead); + } +#endif + quint64 lastEditedFromBufferAdjusted = lastEditedFromBuffer - clockSkew; if (lastEditedFromBufferAdjusted > now) { lastEditedFromBufferAdjusted = now; } @@ -487,9 +523,21 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef } // last updated is stored as ByteCountCoded delta from lastEdited - QByteArray encodedUpdateDelta = originalDataBuffer.mid(bytesRead); // maximum possible size - ByteCountCoded updateDeltaCoder = encodedUpdateDelta; - quint64 updateDelta = updateDeltaCoder; + quint64 updateDelta; + parser.readCompressedCount(updateDelta); +#ifdef VALIDATE_ENTITY_ITEM_PARSER + { + QByteArray encodedUpdateDelta = originalDataBuffer.mid(bytesRead); // maximum possible size + ByteCountCoded updateDeltaCoder = encodedUpdateDelta; + quint64 updateDelta2 = updateDeltaCoder; + Q_ASSERT(updateDelta == updateDelta2); + encodedUpdateDelta = updateDeltaCoder; // determine true length + dataAt += encodedUpdateDelta.size(); + bytesRead += encodedUpdateDelta.size(); + Q_ASSERT(parser.offset() == bytesRead); + } +#endif + if (overwriteLocalData) { _lastUpdated = lastEditedFromBufferAdjusted + updateDelta; // don't adjust for clock skew since we already did that #ifdef WANT_DEBUG @@ -499,17 +547,25 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef #endif } - encodedUpdateDelta = updateDeltaCoder; // determine true length - dataAt += encodedUpdateDelta.size(); - bytesRead += encodedUpdateDelta.size(); - // Newer bitstreams will have a last simulated and a last updated value quint64 lastSimulatedFromBufferAdjusted = now; if (args.bitstreamVersion >= VERSION_ENTITIES_HAS_LAST_SIMULATED_TIME) { // last simulated is stored as ByteCountCoded delta from lastEdited - QByteArray encodedSimulatedDelta = originalDataBuffer.mid(bytesRead); // maximum possible size - ByteCountCoded simulatedDeltaCoder = encodedSimulatedDelta; - quint64 simulatedDelta = simulatedDeltaCoder; + quint64 simulatedDelta; + parser.readCompressedCount(simulatedDelta); +#ifdef VALIDATE_ENTITY_ITEM_PARSER + { + QByteArray encodedSimulatedDelta = originalDataBuffer.mid(bytesRead); // maximum possible size + ByteCountCoded simulatedDeltaCoder = encodedSimulatedDelta; + quint64 simulatedDelta2 = simulatedDeltaCoder; + Q_ASSERT(simulatedDelta2 == simulatedDelta); + encodedSimulatedDelta = simulatedDeltaCoder; // determine true length + dataAt += encodedSimulatedDelta.size(); + bytesRead += encodedSimulatedDelta.size(); + Q_ASSERT(parser.offset() == bytesRead); + } +#endif + if (overwriteLocalData) { lastSimulatedFromBufferAdjusted = lastEditedFromBufferAdjusted + simulatedDelta; // don't adjust for clock skew since we already did that if (lastSimulatedFromBufferAdjusted > now) { @@ -521,9 +577,6 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef qCDebug(entities) << " lastSimulatedFromBufferAdjusted:" << debugTime(lastSimulatedFromBufferAdjusted, now); #endif } - encodedSimulatedDelta = simulatedDeltaCoder; // determine true length - dataAt += encodedSimulatedDelta.size(); - bytesRead += encodedSimulatedDelta.size(); } #ifdef WANT_DEBUG @@ -537,10 +590,26 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef // Property Flags - QByteArray encodedPropertyFlags = originalDataBuffer.mid(bytesRead); // maximum possible size - EntityPropertyFlags propertyFlags = encodedPropertyFlags; - dataAt += propertyFlags.getEncodedLength(); - bytesRead += propertyFlags.getEncodedLength(); + EntityPropertyFlags propertyFlags; + parser.readFlags(propertyFlags); +#ifdef VALIDATE_ENTITY_ITEM_PARSER + { + QByteArray encodedPropertyFlags = originalDataBuffer.mid(bytesRead); // maximum possible size + EntityPropertyFlags propertyFlags2 = encodedPropertyFlags; + dataAt += propertyFlags.getEncodedLength(); + bytesRead += propertyFlags.getEncodedLength(); + Q_ASSERT(propertyFlags2 == propertyFlags); + Q_ASSERT(parser.offset() == bytesRead); + } +#endif + +#ifdef VALIDATE_ENTITY_ITEM_PARSER + Q_ASSERT(parser.data() + parser.offset() == dataAt); +#else + const unsigned char* dataAt = parser.data() + parser.offset(); + int bytesRead = parser.offset(); +#endif + if (args.bitstreamVersion >= VERSION_ENTITIES_HAVE_SIMULATION_OWNER_AND_ACTIONS_OVER_WIRE) { // pack SimulationOwner and terse update properties near each other @@ -549,6 +618,7 @@ int EntityItem::readEntityDataFromBuffer(const unsigned char* data, int bytesLef // even when we would otherwise ignore the rest of the packet. if (propertyFlags.getHasProperty(PROP_SIMULATION_OWNER)) { + QByteArray simOwnerData; int bytes = OctreePacketData::unpackDataFromBytes(dataAt, simOwnerData); SimulationOwner newSimOwner; diff --git a/libraries/entities/src/EntityItemID.cpp b/libraries/entities/src/EntityItemID.cpp index 4e66f5d156..d882559ee3 100644 --- a/libraries/entities/src/EntityItemID.cpp +++ b/libraries/entities/src/EntityItemID.cpp @@ -11,7 +11,7 @@ #include #include - +#include #include #include "RegisteredMetaTypes.h" @@ -33,11 +33,8 @@ EntityItemID::EntityItemID(const QUuid& id) : QUuid(id) EntityItemID EntityItemID::readEntityItemIDFromBuffer(const unsigned char* data, int bytesLeftToRead) { EntityItemID result; - if (bytesLeftToRead >= NUM_BYTES_RFC4122_UUID) { - // id - QByteArray encodedID((const char*)data, NUM_BYTES_RFC4122_UUID); - result = QUuid::fromRfc4122(encodedID); + BufferParser(data, bytesLeftToRead).readUuid(result); } return result; } diff --git a/libraries/shared/src/BufferParser.h b/libraries/shared/src/BufferParser.h new file mode 100644 index 0000000000..622d59d125 --- /dev/null +++ b/libraries/shared/src/BufferParser.h @@ -0,0 +1,123 @@ +// +// Created by Bradley Austin Davis on 2015/07/08 +// Copyright 2013-2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#pragma once +#ifndef hifi_BufferParser_h +#define hifi_BufferParser_h + +#include + +#include +#include + +#include "GLMHelpers.h" +#include "ByteCountCoding.h" +#include "PropertyFlags.h" + +class BufferParser { +public: + BufferParser(const uint8_t* data, size_t size, size_t offset = 0) : + _offset(offset), _data(data), _size(size) { + } + + template + void readValue(T& result) { + Q_ASSERT(remaining() >= sizeof(T)); + memcpy(&result, _data + _offset, sizeof(T)); + _offset += sizeof(T); + } + + template<> + void readValue(quat& result) { + size_t advance = unpackOrientationQuatFromBytes(_data + _offset, result); + _offset += advance; + } + + template<> + void readValue(QString& result) { + uint16_t length; readValue(length); + result = QString((const char*)_data + _offset); + } + + template<> + void readValue(QUuid& result) { + uint16_t length; readValue(length); + Q_ASSERT(16 == length); + readUuid(result); + } + + template<> + void readValue(xColor& result) { + readValue(result.red); + readValue(result.blue); + readValue(result.green); + } + + template<> + void readValue>(QVector& result) { + uint16_t length; readValue(length); + result.resize(length); + memcpy(result.data(), _data + _offset, sizeof(glm::vec3) * length); + _offset += sizeof(glm::vec3) * length; + } + + template<> + void readValue(QByteArray& result) { + uint16_t length; readValue(length); + result = QByteArray((char*)_data + _offset, (int)length); + _offset += length; + } + + void readUuid(QUuid& result) { + readValue(result.data1); + readValue(result.data2); + readValue(result.data3); + readValue(result.data4); + result.data1 = qFromBigEndian(result.data1); + result.data2 = qFromBigEndian(result.data2); + result.data3 = qFromBigEndian(result.data3); + } + + template + void readFlags(PropertyFlags& result) { + // FIXME doing heap allocation + QByteArray encoded((const char*)(_data + _offset), remaining()); + result.decode(encoded); + _offset += result.getEncodedLength(); + } + + template + void readCompressedCount(T& result) { + // FIXME switch to a heapless implementation as soon as Brad provides it. + QByteArray encoded((const char*)(_data + _offset), std::min(sizeof(T) << 1, remaining())); + ByteCountCoded codec = encoded; + result = codec.data; + encoded = codec; + _offset += encoded.size(); + } + + inline size_t remaining() const { + return _size - _offset; + } + + inline size_t offset() const { + return _offset; + } + + inline const uint8_t* data() const { + return _data; + } + +private: + + size_t _offset{ 0 }; + const uint8_t* const _data; + const size_t _size; +}; + +#endif From e885ac1821c26c7e3ebed8e92db38239e1770255 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 8 Jul 2015 17:32:51 -0700 Subject: [PATCH 044/107] improved performance of ByteCountCoding<> decode --- interface/src/Application.cpp | 1 + interface/src/Util.cpp | 72 +++++++++++++++++++++ interface/src/Util.h | 1 + libraries/shared/src/ByteCountCoding.h | 88 ++++++++++++++------------ 4 files changed, 123 insertions(+), 39 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 48fa53777d..cd15f2f4c7 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1014,6 +1014,7 @@ void Application::paintGL() { void Application::runTests() { runTimingTests(); + //runUnitTests(); } void Application::audioMuteToggled() { diff --git a/interface/src/Util.cpp b/interface/src/Util.cpp index bf4df3f3d2..5f04010b47 100644 --- a/interface/src/Util.cpp +++ b/interface/src/Util.cpp @@ -21,6 +21,7 @@ #include +#include #include #include @@ -229,6 +230,43 @@ void runTimingTests() { elapsedUsecs = (float)startTime.nsecsElapsed() * NSEC_TO_USEC; qCDebug(interfaceapp, "vec3 assign and dot() usecs: %f, last result:%f", (double)(elapsedUsecs / numTests), (double)result); + + + quint64 BYTE_CODE_MAX_VALUE = 99999999; + quint64 BYTE_CODE_TESTS_SKIP = 999; + + QByteArray extraJunk; + const int EXTRA_JUNK_SIZE = 200; + extraJunk.append((unsigned char)255); + for (int i = 0; i < EXTRA_JUNK_SIZE; i++) { + extraJunk.append(QString("junk")); + } + + { + startTime.start(); + quint64 tests = 0; + quint64 failed = 0; + for (quint64 value = 0; value < BYTE_CODE_MAX_VALUE; value += BYTE_CODE_TESTS_SKIP) { + quint64 valueA = value; // usecTimestampNow(); + ByteCountCoded codedValueA = valueA; + QByteArray codedValueABuffer = codedValueA; + codedValueABuffer.append(extraJunk); + ByteCountCoded decodedValueA; + decodedValueA.decode(codedValueABuffer); + quint64 valueADecoded = decodedValueA; + tests++; + if (valueA != valueADecoded) { + qDebug() << "FAILED! value:" << valueA << "decoded:" << valueADecoded; + failed++; + } + + } + elapsedUsecs = (float)startTime.nsecsElapsed() * NSEC_TO_USEC; + qCDebug(interfaceapp) << "ByteCountCoded usecs: " << elapsedUsecs + << "per test:" << (double) (elapsedUsecs / tests) + << "tests:" << tests + << "failed:" << failed; + } } bool rayIntersectsSphere(const glm::vec3& rayStarting, const glm::vec3& rayNormalizedDirection, @@ -271,3 +309,37 @@ bool pointInSphere(glm::vec3& point, glm::vec3& sphereCenter, double sphereRadiu } return false; } + +void runUnitTests() { + + quint64 LAST_TEST = 10; + quint64 SKIP_BY = 1; + + for (quint64 value = 0; value <= LAST_TEST; value += SKIP_BY) { + qDebug() << "value:" << value; + + ByteCountCoded codedValue = value; + + QByteArray codedValueBuffer = codedValue; + + codedValueBuffer.append((unsigned char)255); + codedValueBuffer.append(QString("junk")); + + qDebug() << "codedValueBuffer:"; + outputBufferBits((const unsigned char*)codedValueBuffer.constData(), codedValueBuffer.size()); + + ByteCountCoded valueDecoder = codedValueBuffer; + quint64 valueDecoded = valueDecoder; + qDebug() << "valueDecoded:" << valueDecoded; + + + if (value == valueDecoded) { + qDebug() << "SUCCESS!"; + } else { + qDebug() << "FAILED!"; + } + + } +} + + diff --git a/interface/src/Util.h b/interface/src/Util.h index ed05209747..d252c26bef 100644 --- a/interface/src/Util.h +++ b/interface/src/Util.h @@ -30,6 +30,7 @@ void drawText(int x, int y, float scale, float radians, int mono, void renderCollisionOverlay(int width, int height, float magnitude, float red = 0, float blue = 0, float green = 0); void runTimingTests(); +void runUnitTests(); bool rayIntersectsSphere(const glm::vec3& rayStarting, const glm::vec3& rayNormalizedDirection, const glm::vec3& sphereCenter, float sphereRadius, float& distance); diff --git a/libraries/shared/src/ByteCountCoding.h b/libraries/shared/src/ByteCountCoding.h index efde19d45b..88f07230be 100644 --- a/libraries/shared/src/ByteCountCoding.h +++ b/libraries/shared/src/ByteCountCoding.h @@ -26,6 +26,8 @@ #include #include +#include "SharedUtil.h" + #include "NumericalConstants.h" template class ByteCountCoded { @@ -40,6 +42,7 @@ public: QByteArray encode() const; void decode(const QByteArray& fromEncoded); + void decode(const char* encodedBuffer, int encodedSize); bool operator==(const ByteCountCoded& other) const { return data == other.data; } bool operator!=(const ByteCountCoded& other) const { return data != other.data; } @@ -113,50 +116,57 @@ template inline QByteArray ByteCountCoded::encode() const { } template inline void ByteCountCoded::decode(const QByteArray& fromEncodedBytes) { - // first convert the ByteArray into a BitArray... - QBitArray encodedBits; - int bitCount = BITS_IN_BYTE * fromEncodedBytes.count(); - encodedBits.resize(bitCount); - - // copies the QByteArray into a QBitArray - for(int byte = 0; byte < fromEncodedBytes.count(); byte++) { - char originalByte = fromEncodedBytes.at(byte); - for(int bit = 0; bit < BITS_IN_BYTE; bit++) { - int shiftBy = BITS_IN_BYTE - (bit + 1); - char maskBit = ( 1 << shiftBy); - bool bitValue = originalByte & maskBit; - encodedBits.setBit(byte * BITS_IN_BYTE + bit, bitValue); - } - } - - // next, read the leading bits to determine the correct number of bytes to decode (may not match the QByteArray) + decode(fromEncodedBytes.constData(), fromEncodedBytes.size()); +} + +template inline void ByteCountCoded::decode(const char* encodedBuffer, int encodedSize) { + data = 0; // reset data + int bitCount = BITS_IN_BYTE * encodedSize; + int encodedByteCount = 1; // there is at least 1 byte (after the leadBits) int leadBits = 1; // there is always at least 1 lead bit - int bitAt; - for (bitAt = 0; bitAt < bitCount; bitAt++) { - if (encodedBits.at(bitAt)) { - encodedByteCount++; - leadBits++; - } else { - break; - } - } - int expectedBitCount = (encodedByteCount * BITS_IN_BYTE) - leadBits; - - T value = 0; - - if (expectedBitCount <= (encodedBits.size() - leadBits)) { - // Now, keep reading... - int valueStartsAt = bitAt + 1; - T bitValue = 1; - for (bitAt = valueStartsAt; bitAt < (expectedBitCount + leadBits); bitAt++) { - if(encodedBits.at(bitAt)) { - value += bitValue; + bool inLeadBits = true; + int bitAt = 0; + int expectedBitCount; // unknown at this point + int lastValueBit; + T bitValue = 1; + + for(int byte = 0; byte < encodedSize; byte++) { + char originalByte = encodedBuffer[byte]; + unsigned char maskBit = 128; + for(int bit = 0; bit < BITS_IN_BYTE; bit++) { + //int shiftBy = BITS_IN_BYTE - (bit + 1); + //char maskBit = (1 << shiftBy); + bool bitIsSet = originalByte & maskBit; + + // Processing of the lead bits + if (inLeadBits) { + if (bitIsSet) { + encodedByteCount++; + leadBits++; + } else { + inLeadBits = false; // once we hit our first 0, we know we're out of the lead bits + expectedBitCount = (encodedByteCount * BITS_IN_BYTE) - leadBits; + lastValueBit = expectedBitCount + bitAt; + // check to see if the remainder of our buffer is sufficient + if (expectedBitCount > (bitCount - leadBits)) { + break; + } + } + } else { + if (bitAt > lastValueBit) { + break; + } + + if(bitIsSet) { + data += bitValue; + } + bitValue *= 2; } - bitValue *= 2; + bitAt++; + maskBit = maskBit >> 1; } } - data = value; } #endif // hifi_ByteCountCoding_h From c0cdf3256cca585fdc6f5d72304f45faf22c143f Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 8 Jul 2015 17:34:36 -0700 Subject: [PATCH 045/107] comment and dead code --- libraries/shared/src/ByteCountCoding.h | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libraries/shared/src/ByteCountCoding.h b/libraries/shared/src/ByteCountCoding.h index 88f07230be..735c51a783 100644 --- a/libraries/shared/src/ByteCountCoding.h +++ b/libraries/shared/src/ByteCountCoding.h @@ -133,10 +133,8 @@ template inline void ByteCountCoded::decode(const char* encodedBu for(int byte = 0; byte < encodedSize; byte++) { char originalByte = encodedBuffer[byte]; - unsigned char maskBit = 128; + unsigned char maskBit = 128; // LEFT MOST BIT set for(int bit = 0; bit < BITS_IN_BYTE; bit++) { - //int shiftBy = BITS_IN_BYTE - (bit + 1); - //char maskBit = (1 << shiftBy); bool bitIsSet = originalByte & maskBit; // Processing of the lead bits @@ -148,6 +146,7 @@ template inline void ByteCountCoded::decode(const char* encodedBu inLeadBits = false; // once we hit our first 0, we know we're out of the lead bits expectedBitCount = (encodedByteCount * BITS_IN_BYTE) - leadBits; lastValueBit = expectedBitCount + bitAt; + // check to see if the remainder of our buffer is sufficient if (expectedBitCount > (bitCount - leadBits)) { break; From 600e9cbf52197467d3f02c945e85c6dba42a1185 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Wed, 8 Jul 2015 18:17:03 -0700 Subject: [PATCH 046/107] Fixing template specialization compilation error on gcc/clang --- libraries/shared/src/BufferParser.h | 91 +++++++++++++++-------------- 1 file changed, 46 insertions(+), 45 deletions(-) diff --git a/libraries/shared/src/BufferParser.h b/libraries/shared/src/BufferParser.h index 622d59d125..5d388a6f5f 100644 --- a/libraries/shared/src/BufferParser.h +++ b/libraries/shared/src/BufferParser.h @@ -26,54 +26,13 @@ public: } template - void readValue(T& result) { + inline void readValue(T& result) { Q_ASSERT(remaining() >= sizeof(T)); memcpy(&result, _data + _offset, sizeof(T)); _offset += sizeof(T); } - template<> - void readValue(quat& result) { - size_t advance = unpackOrientationQuatFromBytes(_data + _offset, result); - _offset += advance; - } - - template<> - void readValue(QString& result) { - uint16_t length; readValue(length); - result = QString((const char*)_data + _offset); - } - - template<> - void readValue(QUuid& result) { - uint16_t length; readValue(length); - Q_ASSERT(16 == length); - readUuid(result); - } - - template<> - void readValue(xColor& result) { - readValue(result.red); - readValue(result.blue); - readValue(result.green); - } - - template<> - void readValue>(QVector& result) { - uint16_t length; readValue(length); - result.resize(length); - memcpy(result.data(), _data + _offset, sizeof(glm::vec3) * length); - _offset += sizeof(glm::vec3) * length; - } - - template<> - void readValue(QByteArray& result) { - uint16_t length; readValue(length); - result = QByteArray((char*)_data + _offset, (int)length); - _offset += length; - } - - void readUuid(QUuid& result) { + inline void readUuid(QUuid& result) { readValue(result.data1); readValue(result.data2); readValue(result.data3); @@ -84,7 +43,7 @@ public: } template - void readFlags(PropertyFlags& result) { + inline void readFlags(PropertyFlags& result) { // FIXME doing heap allocation QByteArray encoded((const char*)(_data + _offset), remaining()); result.decode(encoded); @@ -92,7 +51,7 @@ public: } template - void readCompressedCount(T& result) { + inline void readCompressedCount(T& result) { // FIXME switch to a heapless implementation as soon as Brad provides it. QByteArray encoded((const char*)(_data + _offset), std::min(sizeof(T) << 1, remaining())); ByteCountCoded codec = encoded; @@ -120,4 +79,46 @@ private: const size_t _size; }; + +template<> +inline void BufferParser::readValue(quat& result) { + size_t advance = unpackOrientationQuatFromBytes(_data + _offset, result); + _offset += advance; +} + +template<> +inline void BufferParser::readValue(QString& result) { + uint16_t length; readValue(length); + result = QString((const char*)_data + _offset); +} + +template<> +inline void BufferParser::readValue(QUuid& result) { + uint16_t length; readValue(length); + Q_ASSERT(16 == length); + readUuid(result); +} + +template<> +inline void BufferParser::readValue(xColor& result) { + readValue(result.red); + readValue(result.blue); + readValue(result.green); +} + +template<> +inline void BufferParser::readValue(QVector& result) { + uint16_t length; readValue(length); + result.resize(length); + memcpy(result.data(), _data + _offset, sizeof(glm::vec3) * length); + _offset += sizeof(glm::vec3) * length; +} + +template<> +inline void BufferParser::readValue(QByteArray& result) { + uint16_t length; readValue(length); + result = QByteArray((char*)_data + _offset, (int)length); + _offset += length; +} + #endif From 7790e35d2497ca6ff631413ba943f9bf8ea63b3d Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 8 Jul 2015 18:19:12 -0700 Subject: [PATCH 047/107] added bytes consumed result to decode, and exit early after bytes consumed --- interface/src/Application.cpp | 2 +- interface/src/Util.cpp | 8 +++++--- libraries/shared/src/ByteCountCoding.h | 16 +++++++++++----- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index cd15f2f4c7..a834ce1288 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1014,7 +1014,7 @@ void Application::paintGL() { void Application::runTests() { runTimingTests(); - //runUnitTests(); + runUnitTests(); } void Application::audioMuteToggled() { diff --git a/interface/src/Util.cpp b/interface/src/Util.cpp index 5f04010b47..82f7d55b5d 100644 --- a/interface/src/Util.cpp +++ b/interface/src/Util.cpp @@ -232,7 +232,7 @@ void runTimingTests() { (double)(elapsedUsecs / numTests), (double)result); - quint64 BYTE_CODE_MAX_VALUE = 99999999; + quint64 BYTE_CODE_MAX_TEST_VALUE = 99999999; quint64 BYTE_CODE_TESTS_SKIP = 999; QByteArray extraJunk; @@ -246,7 +246,7 @@ void runTimingTests() { startTime.start(); quint64 tests = 0; quint64 failed = 0; - for (quint64 value = 0; value < BYTE_CODE_MAX_VALUE; value += BYTE_CODE_TESTS_SKIP) { + for (quint64 value = 0; value < BYTE_CODE_MAX_TEST_VALUE; value += BYTE_CODE_TESTS_SKIP) { quint64 valueA = value; // usecTimestampNow(); ByteCountCoded codedValueA = valueA; QByteArray codedValueABuffer = codedValueA; @@ -328,9 +328,11 @@ void runUnitTests() { qDebug() << "codedValueBuffer:"; outputBufferBits((const unsigned char*)codedValueBuffer.constData(), codedValueBuffer.size()); - ByteCountCoded valueDecoder = codedValueBuffer; + ByteCountCoded valueDecoder; + size_t bytesConsumed = valueDecoder.decode(codedValueBuffer); quint64 valueDecoded = valueDecoder; qDebug() << "valueDecoded:" << valueDecoded; + qDebug() << "bytesConsumed:" << bytesConsumed; if (value == valueDecoded) { diff --git a/libraries/shared/src/ByteCountCoding.h b/libraries/shared/src/ByteCountCoding.h index 735c51a783..ce6f121ddb 100644 --- a/libraries/shared/src/ByteCountCoding.h +++ b/libraries/shared/src/ByteCountCoding.h @@ -41,8 +41,8 @@ public: ByteCountCoded(const QByteArray& fromEncoded) : data(0) { decode(fromEncoded); } QByteArray encode() const; - void decode(const QByteArray& fromEncoded); - void decode(const char* encodedBuffer, int encodedSize); + size_t decode(const QByteArray& fromEncoded); + size_t decode(const char* encodedBuffer, int encodedSize); bool operator==(const ByteCountCoded& other) const { return data == other.data; } bool operator!=(const ByteCountCoded& other) const { return data != other.data; } @@ -115,12 +115,13 @@ template inline QByteArray ByteCountCoded::encode() const { return output; } -template inline void ByteCountCoded::decode(const QByteArray& fromEncodedBytes) { - decode(fromEncodedBytes.constData(), fromEncodedBytes.size()); +template inline size_t ByteCountCoded::decode(const QByteArray& fromEncodedBytes) { + return decode(fromEncodedBytes.constData(), fromEncodedBytes.size()); } -template inline void ByteCountCoded::decode(const char* encodedBuffer, int encodedSize) { +template inline size_t ByteCountCoded::decode(const char* encodedBuffer, int encodedSize) { data = 0; // reset data + size_t bytesConsumed = 0; int bitCount = BITS_IN_BYTE * encodedSize; int encodedByteCount = 1; // there is at least 1 byte (after the leadBits) @@ -133,6 +134,7 @@ template inline void ByteCountCoded::decode(const char* encodedBu for(int byte = 0; byte < encodedSize; byte++) { char originalByte = encodedBuffer[byte]; + bytesConsumed++; unsigned char maskBit = 128; // LEFT MOST BIT set for(int bit = 0; bit < BITS_IN_BYTE; bit++) { bool bitIsSet = originalByte & maskBit; @@ -165,7 +167,11 @@ template inline void ByteCountCoded::decode(const char* encodedBu bitAt++; maskBit = maskBit >> 1; } + if (!inLeadBits && bitAt > lastValueBit) { + break; + } } + return bytesConsumed; } #endif // hifi_ByteCountCoding_h From f2beb79d232af270d5af3249116d730c7eb8e5e5 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Wed, 8 Jul 2015 18:54:38 -0700 Subject: [PATCH 048/107] Updating buffer parser to use new heapless API --- libraries/shared/src/BufferParser.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/libraries/shared/src/BufferParser.h b/libraries/shared/src/BufferParser.h index 5d388a6f5f..84bde2be31 100644 --- a/libraries/shared/src/BufferParser.h +++ b/libraries/shared/src/BufferParser.h @@ -53,11 +53,9 @@ public: template inline void readCompressedCount(T& result) { // FIXME switch to a heapless implementation as soon as Brad provides it. - QByteArray encoded((const char*)(_data + _offset), std::min(sizeof(T) << 1, remaining())); - ByteCountCoded codec = encoded; + ByteCountCoded codec; + _offset += codec.decode(reinterpret_cast(_data + _offset), remaining()); result = codec.data; - encoded = codec; - _offset += encoded.size(); } inline size_t remaining() const { From 1be0e7ebc32076ae496feb60cece8e265f121cdf Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Wed, 8 Jul 2015 19:25:16 -0700 Subject: [PATCH 049/107] fix for mirror rendering on windows * added preScale to Transform class. * preScale by -1 about the xAxis instead of post scale. I think this was working on Mac due to the different code paths in GLBackendTransform::updateTransform for core vs legacy gl profile. --- interface/src/Application.cpp | 11 ----------- libraries/render-utils/src/RenderDeferredTask.cpp | 2 +- libraries/shared/src/Transform.h | 10 ++++++++++ 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 48fa53777d..7bfa500f64 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3386,14 +3386,6 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se // load the view frustum loadViewFrustum(theCamera, _displayViewFrustum); - // flip x if in mirror mode (also requires reversing winding order for backface culling) - if (theCamera.getMode() == CAMERA_MODE_MIRROR) { - //glScalef(-1.0f, 1.0f, 1.0f); - //glFrontFace(GL_CW); - } else { - glFrontFace(GL_CCW); - } - // transform view according to theCamera // could be myCamera (if in normal mode) // or could be viewFrustumOffsetCamera if in offset mode @@ -3411,9 +3403,6 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se Transform viewTransform; viewTransform.setTranslation(theCamera.getPosition()); viewTransform.setRotation(rotation); - if (theCamera.getMode() == CAMERA_MODE_MIRROR) { -// viewTransform.setScale(Transform::Vec3(-1.0f, 1.0f, 1.0f)); - } if (renderArgs->_renderSide != RenderArgs::MONO) { glm::mat4 invView = glm::inverse(_untranslatedViewMatrix); diff --git a/libraries/render-utils/src/RenderDeferredTask.cpp b/libraries/render-utils/src/RenderDeferredTask.cpp index 12a6d32ae5..4929d4b5ae 100755 --- a/libraries/render-utils/src/RenderDeferredTask.cpp +++ b/libraries/render-utils/src/RenderDeferredTask.cpp @@ -118,7 +118,7 @@ void DrawOpaqueDeferred::run(const SceneContextPointer& sceneContext, const Rend args->_viewFrustum->evalProjectionMatrix(projMat); args->_viewFrustum->evalViewTransform(viewMat); if (args->_renderMode == RenderArgs::MIRROR_RENDER_MODE) { - viewMat.postScale(glm::vec3(-1.0f, 1.0f, 1.0f)); + viewMat.preScale(glm::vec3(-1.0f, 1.0f, 1.0f)); } batch.setProjectionTransform(projMat); batch.setViewTransform(viewMat); diff --git a/libraries/shared/src/Transform.h b/libraries/shared/src/Transform.h index e961f379c8..6f841151c4 100644 --- a/libraries/shared/src/Transform.h +++ b/libraries/shared/src/Transform.h @@ -97,6 +97,8 @@ public: const Vec3& getScale() const; void setScale(float scale); void setScale(const Vec3& scale); // [new this] = [this.translation] * [this.rotation] * [scale] + void preScale(float scale); + void preScale(const Vec3& scale); void postScale(float scale); // [new this] = [this] * [scale] equivalent to glScale void postScale(const Vec3& scale); // [new this] = [this] * [scale] equivalent to glScale @@ -322,6 +324,14 @@ inline void Transform::setScale(const Vec3& scale) { } } +inline void Transform::preScale(float scale) { + setScale(getScale() * scale); +} + +inline void Transform::preScale(const Vec3& scale) { + setScale(getScale() * scale); +} + inline void Transform::postScale(float scale) { if (!isValidScale(scale) || scale == 1.0f) { return; From 08d1364f81616093bec3700dd26b98aec008fadf Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Wed, 8 Jul 2015 22:35:23 -0700 Subject: [PATCH 050/107] Move file log persistence to a separate thread --- interface/src/AbstractLoggerInterface.h | 4 +- interface/src/FileLogger.cpp | 42 +++++++++---- interface/src/FileLogger.h | 10 ++-- libraries/shared/src/GenericQueueThread.h | 72 +++++++++++++++++++++++ libraries/shared/src/GenericThread.cpp | 3 +- libraries/shared/src/GenericThread.h | 2 +- 6 files changed, 115 insertions(+), 18 deletions(-) create mode 100644 libraries/shared/src/GenericQueueThread.h diff --git a/interface/src/AbstractLoggerInterface.h b/interface/src/AbstractLoggerInterface.h index fe45346e4c..3823060b13 100644 --- a/interface/src/AbstractLoggerInterface.h +++ b/interface/src/AbstractLoggerInterface.h @@ -24,7 +24,7 @@ public: inline bool extraDebugging() { return _extraDebugging; } inline void setExtraDebugging(bool debugging) { _extraDebugging = debugging; } - virtual void addMessage(QString) = 0; + virtual void addMessage(const QString&) = 0; virtual QString getLogData() = 0; virtual void locateLog() = 0; @@ -32,7 +32,7 @@ signals: void logReceived(QString message); private: - bool _extraDebugging; + bool _extraDebugging{ false }; }; #endif // hifi_AbstractLoggerInterface_h diff --git a/interface/src/FileLogger.cpp b/interface/src/FileLogger.cpp index 4808842036..bcae96b2ea 100644 --- a/interface/src/FileLogger.cpp +++ b/interface/src/FileLogger.cpp @@ -21,11 +21,35 @@ const QString FILENAME_FORMAT = "hifi-log_%1_%2.txt"; const QString DATETIME_FORMAT = "yyyy-MM-dd_hh.mm.ss"; const QString LOGS_DIRECTORY = "Logs"; +class FilePersistThread : public GenericQueueThread < QString > { +public: + FilePersistThread(FileLogger& logger) : _logger(logger) { + setObjectName("LogFileWriter"); + } + +protected: + virtual bool processQueueItems(const Queue& messages) { + QFile file(_logger._fileName); + if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) { + QTextStream out(&file); + foreach(const QString& message, messages) { + out << message; + } + } + return true; + } +private: + FileLogger& _logger; +}; + +static FilePersistThread* _persistThreadInstance; + FileLogger::FileLogger(QObject* parent) : AbstractLoggerInterface(parent), _logData("") { - setExtraDebugging(false); + _persistThreadInstance = new FilePersistThread(*this); + _persistThreadInstance->initialize(true); _fileName = FileUtils::standardPath(LOGS_DIRECTORY); QHostAddress clientAddress = getLocalAddress(); @@ -33,16 +57,14 @@ FileLogger::FileLogger(QObject* parent) : _fileName.append(QString(FILENAME_FORMAT).arg(clientAddress.toString(), now.toString(DATETIME_FORMAT))); } -void FileLogger::addMessage(QString message) { - QMutexLocker locker(&_mutex); - emit logReceived(message); - _logData += message; +FileLogger::~FileLogger() { + _persistThreadInstance->terminate(); +} - QFile file(_fileName); - if (file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text)) { - QTextStream out(&file); - out << message; - } +void FileLogger::addMessage(const QString& message) { + _persistThreadInstance->queueItem(message); + emit logReceived(message); + //_logData += message; } void FileLogger::locateLog() { diff --git a/interface/src/FileLogger.h b/interface/src/FileLogger.h index 3dbbfd26cd..72bffa6445 100644 --- a/interface/src/FileLogger.h +++ b/interface/src/FileLogger.h @@ -13,23 +13,25 @@ #define hifi_FileLogger_h #include "AbstractLoggerInterface.h" -#include +#include class FileLogger : public AbstractLoggerInterface { Q_OBJECT public: FileLogger(QObject* parent = NULL); + virtual ~FileLogger(); - virtual void addMessage(QString); + virtual void addMessage(const QString&); virtual QString getLogData() { return _logData; } virtual void locateLog(); private: QString _logData; QString _fileName; - QMutex _mutex; - + friend class FilePersistThread; }; + + #endif // hifi_FileLogger_h diff --git a/libraries/shared/src/GenericQueueThread.h b/libraries/shared/src/GenericQueueThread.h new file mode 100644 index 0000000000..2a48ff7418 --- /dev/null +++ b/libraries/shared/src/GenericQueueThread.h @@ -0,0 +1,72 @@ +// +// Created by Bradley Austin Davis 2015/07/08. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#pragma once +#ifndef hifi_GenericQueueThread_h +#define hifi_GenericQueueThread_h + +#include + +#include +#include +#include + +#include "GenericThread.h" +#include "NumericalConstants.h" + +template +class GenericQueueThread : public GenericThread { +public: + using Queue = QQueue; + GenericQueueThread(QObject* parent = nullptr) + : GenericThread(parent) {} + + virtual ~GenericQueueThread() {} + + void queueItem(const T& t) { + lock(); + queueItemInternal(t); + unlock(); + _hasItems.wakeAll(); + } + +protected: + virtual void queueItemInternal(const T& t) { + _items.push_back(t); + } + + virtual uint32_t getMaxWait() { + return MSECS_PER_SECOND; + } + + virtual bool process() { + if (!_items.size()) { + _hasItemsMutex.lock(); + _hasItems.wait(&_hasItemsMutex, getMaxWait()); + _hasItemsMutex.unlock(); + } + + if (!_items.size()) { + return isStillRunning(); + } + + Queue processItems; + lock(); + processItems.swap(_items); + unlock(); + return processQueueItems(processItems); + } + + virtual bool processQueueItems(const Queue& items) = 0; + + Queue _items; + QWaitCondition _hasItems; + QMutex _hasItemsMutex; +}; + +#endif // hifi_GenericQueueThread_h diff --git a/libraries/shared/src/GenericThread.cpp b/libraries/shared/src/GenericThread.cpp index 3068ca2ab6..a7e0f3456b 100644 --- a/libraries/shared/src/GenericThread.cpp +++ b/libraries/shared/src/GenericThread.cpp @@ -14,7 +14,8 @@ #include "GenericThread.h" -GenericThread::GenericThread() : +GenericThread::GenericThread(QObject* parent) : + QObject(parent), _stopThread(false), _isThreaded(false) // assume non-threaded, must call initialize() { diff --git a/libraries/shared/src/GenericThread.h b/libraries/shared/src/GenericThread.h index b2c0eb13db..6fe6b6ccb4 100644 --- a/libraries/shared/src/GenericThread.h +++ b/libraries/shared/src/GenericThread.h @@ -23,7 +23,7 @@ class GenericThread : public QObject { Q_OBJECT public: - GenericThread(); + GenericThread(QObject* parent = nullptr); virtual ~GenericThread(); /// Call to start the thread. From 63dfd570f198e243709bad1a54f0af1821fe3a49 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Wed, 8 Jul 2015 22:54:31 -0700 Subject: [PATCH 051/107] Adding priority support to GenericThread --- interface/src/FileLogger.cpp | 6 +++--- libraries/shared/src/GenericThread.cpp | 3 ++- libraries/shared/src/GenericThread.h | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/interface/src/FileLogger.cpp b/interface/src/FileLogger.cpp index bcae96b2ea..508586c23c 100644 --- a/interface/src/FileLogger.cpp +++ b/interface/src/FileLogger.cpp @@ -23,7 +23,7 @@ const QString LOGS_DIRECTORY = "Logs"; class FilePersistThread : public GenericQueueThread < QString > { public: - FilePersistThread(FileLogger& logger) : _logger(logger) { + FilePersistThread(const FileLogger& logger) : _logger(logger) { setObjectName("LogFileWriter"); } @@ -39,7 +39,7 @@ protected: return true; } private: - FileLogger& _logger; + const FileLogger& _logger; }; static FilePersistThread* _persistThreadInstance; @@ -49,7 +49,7 @@ FileLogger::FileLogger(QObject* parent) : _logData("") { _persistThreadInstance = new FilePersistThread(*this); - _persistThreadInstance->initialize(true); + _persistThreadInstance->initialize(true, QThread::LowestPriority); _fileName = FileUtils::standardPath(LOGS_DIRECTORY); QHostAddress clientAddress = getLocalAddress(); diff --git a/libraries/shared/src/GenericThread.cpp b/libraries/shared/src/GenericThread.cpp index a7e0f3456b..9c1c7c590c 100644 --- a/libraries/shared/src/GenericThread.cpp +++ b/libraries/shared/src/GenericThread.cpp @@ -28,13 +28,14 @@ GenericThread::~GenericThread() { } } -void GenericThread::initialize(bool isThreaded) { +void GenericThread::initialize(bool isThreaded, QThread::Priority priority) { _isThreaded = isThreaded; if (_isThreaded) { _thread = new QThread(this); // match the thread name to our object name _thread->setObjectName(objectName()); + _thread->setPriority(priority); // when the worker thread is started, call our engine's run.. connect(_thread, SIGNAL(started()), this, SLOT(threadRoutine())); diff --git a/libraries/shared/src/GenericThread.h b/libraries/shared/src/GenericThread.h index 6fe6b6ccb4..f261dc5b37 100644 --- a/libraries/shared/src/GenericThread.h +++ b/libraries/shared/src/GenericThread.h @@ -28,7 +28,7 @@ public: /// Call to start the thread. /// \param bool isThreaded true by default. false for non-threaded mode and caller must call threadRoutine() regularly. - void initialize(bool isThreaded = true); + void initialize(bool isThreaded = true, QThread::Priority priority = QThread::NormalPriority); /// Call to stop the thread void terminate(); From 8314fe8c7ff616018ddf2cdff16455696f55c0a6 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Thu, 9 Jul 2015 03:45:19 -0700 Subject: [PATCH 052/107] FInally getting a nice spot light volume working for all angles --- libraries/gpu/src/gpu/GLBackendState.cpp | 6 +- libraries/model/src/model/Light.cpp | 19 ++- libraries/model/src/model/Light.h | 3 +- .../src/DeferredLightingEffect.cpp | 127 +++++++++++++----- .../render-utils/src/DeferredLightingEffect.h | 4 +- .../render-utils/src/deferred_light_spot.slv | 47 +++++++ libraries/render-utils/src/spot_light.slf | 3 + 7 files changed, 161 insertions(+), 48 deletions(-) create mode 100755 libraries/render-utils/src/deferred_light_spot.slv diff --git a/libraries/gpu/src/gpu/GLBackendState.cpp b/libraries/gpu/src/gpu/GLBackendState.cpp index c1b6bfa765..e898a29245 100644 --- a/libraries/gpu/src/gpu/GLBackendState.cpp +++ b/libraries/gpu/src/gpu/GLBackendState.cpp @@ -536,11 +536,9 @@ void GLBackend::do_setStateFrontFaceClockwise(bool isClockwise) { void GLBackend::do_setStateDepthClampEnable(bool enable) { if (_pipeline._stateCache.depthClampEnable != enable) { if (enable) { - //glEnable(GL_DEPTH_CLAMP); - glEnable(GL_DEPTH_CLAMP_NV); + glEnable(GL_DEPTH_CLAMP); } else { - //glDisable(GL_DEPTH_CLAMP); - glDisable(GL_DEPTH_CLAMP_NV); + glDisable(GL_DEPTH_CLAMP); } (void) CHECK_GL_ERROR(); diff --git a/libraries/model/src/model/Light.cpp b/libraries/model/src/model/Light.cpp index 60c6f6b3af..1bad381137 100755 --- a/libraries/model/src/model/Light.cpp +++ b/libraries/model/src/model/Light.cpp @@ -77,18 +77,25 @@ void Light::setMaximumRadius(float radius) { editSchema()._attenuation = Vec4(surfaceRadius, 1.0f/surfaceRadius, CutOffIntensityRatio, radius); } +#include + void Light::setSpotAngle(float angle) { - if (angle <= 0.f) { - angle = 0.0f; + double dangle = angle; + if (dangle <= 0.0) { + dangle = 0.0; } - editSchema()._spot.x = cos(angle); - editSchema()._spot.y = sin(angle); - editSchema()._spot.z = angle; + if (dangle > glm::half_pi()) { + dangle = glm::half_pi(); + } + + editSchema()._spot.x = (float)abs(cos(dangle)); + editSchema()._spot.y = (float)abs(sin(dangle)); + editSchema()._spot.z = (float)dangle; } void Light::setSpotExponent(float exponent) { if (exponent <= 0.f) { - exponent = 1.0f; + exponent = 0.0f; } editSchema()._spot.w = exponent; } diff --git a/libraries/model/src/model/Light.h b/libraries/model/src/model/Light.h index 920549d0f9..1ed07a942c 100755 --- a/libraries/model/src/model/Light.h +++ b/libraries/model/src/model/Light.h @@ -81,6 +81,7 @@ public: bool isSpot() const { return getType() == SPOT; } void setSpotAngle(float angle); float getSpotAngle() const { return getSchema()._spot.z; } + glm::vec2 getSpotAngleCosSin() const { return glm::vec2(getSchema()._spot.x, getSchema()._spot.y); } void setSpotExponent(float exponent); float getSpotExponent() const { return getSchema()._spot.w; } @@ -107,7 +108,7 @@ public: Color _color{1.0f}; float _intensity{1.0f}; Vec4 _attenuation{1.0f}; - Vec4 _spot{0.0f, 0.0f, 0.0f, 3.0f}; + Vec4 _spot{0.0f, 0.0f, 0.0f, 0.0f}; Vec4 _shadow{0.0f}; Vec4 _control{0.0f, 0.0f, 0.0f, 0.0f}; diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 4c50d0a962..da1f383a15 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -33,6 +33,7 @@ #include "deferred_light_vert.h" #include "deferred_light_limited_vert.h" +#include "deferred_light_spot_vert.h" #include "directional_light_frag.h" #include "directional_light_shadow_map_frag.h" @@ -91,26 +92,27 @@ void DeferredLightingEffect::init(AbstractViewStateInterface* viewState) { gpu::Shader::makeProgram(*_emissiveShader, slotBindings); _viewState = viewState; - loadLightProgram(directional_light_frag, false, _directionalLight, _directionalLightLocations); - loadLightProgram(directional_light_shadow_map_frag, false, _directionalLightShadowMap, + loadLightProgram(deferred_light_vert, directional_light_frag, false, _directionalLight, _directionalLightLocations); + loadLightProgram(deferred_light_vert, directional_light_shadow_map_frag, false, _directionalLightShadowMap, _directionalLightShadowMapLocations); - loadLightProgram(directional_light_cascaded_shadow_map_frag, false, _directionalLightCascadedShadowMap, + loadLightProgram(deferred_light_vert, directional_light_cascaded_shadow_map_frag, false, _directionalLightCascadedShadowMap, _directionalLightCascadedShadowMapLocations); - loadLightProgram(directional_ambient_light_frag, false, _directionalAmbientSphereLight, _directionalAmbientSphereLightLocations); - loadLightProgram(directional_ambient_light_shadow_map_frag, false, _directionalAmbientSphereLightShadowMap, + loadLightProgram(deferred_light_vert, directional_ambient_light_frag, false, _directionalAmbientSphereLight, _directionalAmbientSphereLightLocations); + loadLightProgram(deferred_light_vert, directional_ambient_light_shadow_map_frag, false, _directionalAmbientSphereLightShadowMap, _directionalAmbientSphereLightShadowMapLocations); - loadLightProgram(directional_ambient_light_cascaded_shadow_map_frag, false, _directionalAmbientSphereLightCascadedShadowMap, + loadLightProgram(deferred_light_vert, directional_ambient_light_cascaded_shadow_map_frag, false, _directionalAmbientSphereLightCascadedShadowMap, _directionalAmbientSphereLightCascadedShadowMapLocations); - loadLightProgram(directional_skybox_light_frag, false, _directionalSkyboxLight, _directionalSkyboxLightLocations); - loadLightProgram(directional_skybox_light_shadow_map_frag, false, _directionalSkyboxLightShadowMap, + loadLightProgram(deferred_light_vert, directional_skybox_light_frag, false, _directionalSkyboxLight, _directionalSkyboxLightLocations); + loadLightProgram(deferred_light_vert, directional_skybox_light_shadow_map_frag, false, _directionalSkyboxLightShadowMap, _directionalSkyboxLightShadowMapLocations); - loadLightProgram(directional_skybox_light_cascaded_shadow_map_frag, false, _directionalSkyboxLightCascadedShadowMap, + loadLightProgram(deferred_light_vert, directional_skybox_light_cascaded_shadow_map_frag, false, _directionalSkyboxLightCascadedShadowMap, _directionalSkyboxLightCascadedShadowMapLocations); - loadLightProgram(point_light_frag, true, _pointLight, _pointLightLocations); - loadLightProgram(spot_light_frag, true, _spotLight, _spotLightLocations); + + loadLightProgram(deferred_light_limited_vert, point_light_frag, true, _pointLight, _pointLightLocations); + loadLightProgram(deferred_light_spot_vert, spot_light_frag, true, _spotLight, _spotLightLocations); { auto VSFS = gpu::StandardShaderLib::getDrawViewportQuadTransformTexcoordVS(); @@ -421,13 +423,15 @@ void DeferredLightingEffect::render(RenderArgs* args) { for (auto lightID : _pointLights) { auto& light = _allocatedLights[lightID]; - light->setShowContour(true); + // IN DEBUG: light->setShowContour(true); if (_pointLightLocations.lightBufferUnit >= 0) { batch.setUniformBuffer(_pointLightLocations.lightBufferUnit, light->getSchemaBuffer()); } float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); - if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius) { + // TODO: We shouldn;t have to do that test and use a different volume geometry for when inside the vlight volume, + // we should be able to draw thre same geometry use DepthClamp but for unknown reason it's s not working... + /* if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius) { Transform model; model.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f)); batch.setModelTransform(model); @@ -444,7 +448,7 @@ void DeferredLightingEffect::render(RenderArgs* args) { batch.setProjectionTransform(projMat); batch.setViewTransform(viewMat); - } else { + } else*/ { Transform model; model.setTranslation(glm::vec3(light->getPosition().x, light->getPosition().y, light->getPosition().z)); batch.setModelTransform(model); @@ -467,12 +471,22 @@ void DeferredLightingEffect::render(RenderArgs* args) { for (auto lightID : _spotLights) { auto light = _allocatedLights[lightID]; - // IN DEBUG: light->setShowContour(true); + // IN DEBUG: + light->setShowContour(true); batch.setUniformBuffer(_spotLightLocations.lightBufferUnit, light->getSchemaBuffer()); + auto eyeLightPos = eyePoint - light->getPosition(); + auto eyeHalfPlaneDistance = glm::dot(eyeLightPos, light->getDirection()); + + glm::vec4 coneParam(light->getSpotAngleCosSin(), 0.66f * tan(0.5 * light->getSpotAngle()), 1.0f); + float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); - float edgeRadius = expandedRadius / glm::cos(light->getSpotAngle()); - if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < edgeRadius + nearRadius) { + // TODO: We shouldn;t have to do that test and use a different volume geometry for when inside the vlight volume, + // we should be able to draw thre same geometry use DepthClamp but for unknown reason it's s not working... + if ((eyeHalfPlaneDistance > -nearRadius) && (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius)) { + coneParam.w = 0.0f; + batch._glUniform4fv(_spotLightLocations.coneParam, 1, reinterpret_cast< const GLfloat* >(&coneParam)); + Transform model; model.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f)); batch.setModelTransform(model); @@ -490,15 +504,19 @@ void DeferredLightingEffect::render(RenderArgs* args) { batch.setProjectionTransform(projMat); batch.setViewTransform(viewMat); } else { + coneParam.w = 1.0f; + batch._glUniform4fv(_spotLightLocations.coneParam, 1, reinterpret_cast< const GLfloat* >(&coneParam)); + Transform model; model.setTranslation(glm::vec3(light->getPosition().x, light->getPosition().y, light->getPosition().z)); glm::quat spotRotation = rotationBetween(glm::vec3(0.0f, 0.0f, -1.0f), light->getDirection()); + spotRotation = light->getOrientation(); model.postRotate(spotRotation); float base = expandedRadius * glm::tan(light->getSpotAngle()); float height = expandedRadius; - model.postScale(glm::vec3(base, base, height)); + model.postScale(glm::vec3(height, height, height)); batch.setModelTransform(model); auto mesh = getSpotLightMesh(); @@ -522,6 +540,7 @@ void DeferredLightingEffect::render(RenderArgs* args) { batch.setUniformTexture(2, nullptr); batch.setUniformTexture(3, nullptr); + glDepthRange(0.0, 1.0); args->_context->syncCache(); args->_context->render(batch); @@ -575,8 +594,8 @@ void DeferredLightingEffect::setupTransparent(RenderArgs* args, int lightBufferU args->_batch->setUniformBuffer(lightBufferUnit, globalLight->getSchemaBuffer()); } -void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool lightVolume, gpu::PipelinePointer& pipeline, LightLocations& locations) { - auto VS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string((lightVolume ? deferred_light_limited_vert : deferred_light_vert)))); +void DeferredLightingEffect::loadLightProgram(const char* vertSource, const char* fragSource, bool lightVolume, gpu::PipelinePointer& pipeline, LightLocations& locations) { + auto VS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(vertSource))); auto PS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(fragSource))); gpu::ShaderPointer program = gpu::ShaderPointer(gpu::Shader::createProgram(VS, PS)); @@ -605,6 +624,7 @@ void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool light locations.ambientSphere = program->getUniforms().findLocation("ambientSphere.L00"); locations.invViewMat = program->getUniforms().findLocation("invViewMat"); locations.texcoordMat = program->getUniforms().findLocation("texcoordMat"); + locations.coneParam = program->getUniforms().findLocation("coneParam"); #if (GPU_FEATURE_PROFILE == GPU_CORE) locations.lightBufferUnit = program->getBuffers().findLocation("lightBuffer"); @@ -621,7 +641,8 @@ void DeferredLightingEffect::loadLightProgram(const char* fragSource, bool light // No need for z test since the depth buffer is not bound state->setDepthTest(true, false, gpu::LESS_EQUAL); // TODO: We should bind the true depth buffer both as RT and texture for the depth test // TODO: We should use DepthClamp and avoid changing geometry for inside /outside cases - + state->setDepthClampEnable(true); + // additive blending state->setBlendFunction(true, gpu::State::ONE, gpu::State::BLEND_OP_ADD, gpu::State::ONE); } else { @@ -659,30 +680,44 @@ model::MeshPointer DeferredLightingEffect::getSpotLightMesh() { _spotLightMesh.reset(new model::Mesh()); int slices = 16; - int vertices = 2 + slices; + int rings = 3; + int vertices = 2 + rings * slices; int originVertex = vertices - 2; int capVertex = vertices - 1; int verticesSize = vertices * 3 * sizeof(float); - int indices = 3 * slices * 2; + int indices = 3 * slices * (1 + 1 + 2 * (rings -1)); + int ringFloatOffset = slices * 3; + GLfloat* vertexData = new GLfloat[verticesSize]; - GLfloat* vertex = vertexData; + GLfloat* vertexRing0 = vertexData; + GLfloat* vertexRing1 = vertexRing0 + ringFloatOffset; + GLfloat* vertexRing2 = vertexRing1 + ringFloatOffset; for (int i = 0; i < slices; i++) { float theta = TWO_PI * i / slices; - - *(vertex++) = cosf(theta); - *(vertex++) = sinf(theta); - *(vertex++) = -1.0f; + auto cosin = glm::vec2(cosf(theta), sinf(theta)); + + *(vertexRing0++) = cosin.x; + *(vertexRing0++) = cosin.y; + *(vertexRing0++) = 0.0f; + + *(vertexRing1++) = cosin.x; + *(vertexRing1++) = cosin.y; + *(vertexRing1++) = 0.33f; + + *(vertexRing2++) = cosin.x; + *(vertexRing2++) = cosin.y; + *(vertexRing2++) = 0.66f; } - *(vertex++) = 0.0f; - *(vertex++) = 0.0f; - *(vertex++) = 0.0f; + *(vertexRing2++) = 0.0f; + *(vertexRing2++) = 0.0f; + *(vertexRing2++) = -1.0f; - *(vertex++) = 0.0f; - *(vertex++) = 0.0f; - *(vertex++) = -1.0f; + *(vertexRing2++) = 0.0f; + *(vertexRing2++) = 0.0f; + *(vertexRing2++) = 1.0f; _spotLightMesh->setVertexBuffer(gpu::BufferView(new gpu::Buffer(verticesSize, (gpu::Byte*) vertexData), gpu::Element::VEC3F_XYZ)); delete[] vertexData; @@ -696,9 +731,30 @@ model::MeshPointer DeferredLightingEffect::getSpotLightMesh() { int s1 = ((i + 1) % slices); *(index++) = s0; *(index++) = s1; + + int s2 = s0 + slices; + int s3 = s1 + slices; *(index++) = s1; *(index++) = s0; - + *(index++) = s2; + + *(index++) = s1; + *(index++) = s2; + *(index++) = s3; + + int s4 = s2 + slices; + int s5 = s3 + slices; + *(index++) = s3; + *(index++) = s2; + *(index++) = s4; + + *(index++) = s3; + *(index++) = s4; + *(index++) = s5; + + + *(index++) = s5; + *(index++) = s4; *(index++) = capVertex; } @@ -706,6 +762,7 @@ model::MeshPointer DeferredLightingEffect::getSpotLightMesh() { delete[] indexData; model::Mesh::Part part(0, indices, 0, model::Mesh::TRIANGLES); + // model::Mesh::Part part(0, indices, 0, model::Mesh::LINE_STRIP); _spotLightMesh->setPartBuffer(gpu::BufferView(new gpu::Buffer(sizeof(part), (gpu::Byte*) &part), gpu::Element::PART_DRAWCALL)); diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 6c196858ca..0732ffea79 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -96,13 +96,13 @@ private: int atmosphereBufferUnit; int invViewMat; int texcoordMat; + int coneParam; }; model::MeshPointer _spotLightMesh; model::MeshPointer getSpotLightMesh(); - // static void loadLightProgram(const char* fragSource, bool limited, ProgramObject& program, LightLocations& locations); - static void loadLightProgram(const char* fragSource, bool lightVolume, gpu::PipelinePointer& program, LightLocations& locations); + static void loadLightProgram(const char* vertSource, const char* fragSource, bool lightVolume, gpu::PipelinePointer& program, LightLocations& locations); gpu::PipelinePointer getPipeline(SimpleProgramKey config); diff --git a/libraries/render-utils/src/deferred_light_spot.slv b/libraries/render-utils/src/deferred_light_spot.slv new file mode 100755 index 0000000000..d3ef18fd53 --- /dev/null +++ b/libraries/render-utils/src/deferred_light_spot.slv @@ -0,0 +1,47 @@ +<@include gpu/Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// deferred_light_spot.vert +// vertex shader +// +// Created by Sam Gateau on 7/8/15. +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +<@include gpu/Transform.slh@> + +<$declareStandardTransform()$> + +uniform mat4 texcoordMat; +uniform vec4 coneParam; + +void main(void) { + vec4 coneVertex = gl_Vertex; + if (coneParam.w != 0.0) { + if(coneVertex.z >= 0.0) { + // Evaluate the true position of the spot volume + vec2 dir = float(coneVertex.z < 0.5f) * (coneParam.xy + + vec2(coneParam.y, -coneParam.x) * coneParam.z * float(coneVertex.z > 0.0f)) + + float(coneVertex.z > 0.5f) * (vec2(1.0, 0.0) + + vec2(0.0, coneParam.z) * float(coneVertex.z < 1.0f)); + + coneVertex.xy *= dir.y; + coneVertex.z = -dir.x; + } else { + coneVertex.z = 0.0; + } + } + + // standard transform + TransformCamera cam = getTransformCamera(); + TransformObject obj = getTransformObject(); + <$transformModelToClipPos(cam, obj, coneVertex, gl_Position)$>; + + vec4 projected = gl_Position / gl_Position.w; + gl_TexCoord[0] = vec4(dot(projected, texcoordMat[0]) * gl_Position.w, + dot(projected, texcoordMat[1]) * gl_Position.w, 0.0, gl_Position.w); +} diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index cfb2a6da4b..298a8e1502 100644 --- a/libraries/render-utils/src/spot_light.slf +++ b/libraries/render-utils/src/spot_light.slf @@ -35,6 +35,9 @@ void main(void) { discard; } + //gl_FragColor = vec4(1.0, 1.0, 1.0, 0.0); + //return; + // Need the light now Light light = getLight(); From e901d5cad0c5bfdc31a0a14e160d99577d2e0bfe Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Thu, 9 Jul 2015 09:21:53 -0700 Subject: [PATCH 053/107] Uncommented a block left behind... --- libraries/render-utils/src/DeferredLightingEffect.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index da1f383a15..9839a30d8f 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -431,7 +431,7 @@ void DeferredLightingEffect::render(RenderArgs* args) { float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); // TODO: We shouldn;t have to do that test and use a different volume geometry for when inside the vlight volume, // we should be able to draw thre same geometry use DepthClamp but for unknown reason it's s not working... - /* if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius) { + if (glm::distance(eyePoint, glm::vec3(light->getPosition())) < expandedRadius + nearRadius) { Transform model; model.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f)); batch.setModelTransform(model); @@ -448,7 +448,7 @@ void DeferredLightingEffect::render(RenderArgs* args) { batch.setProjectionTransform(projMat); batch.setViewTransform(viewMat); - } else*/ { + } else { Transform model; model.setTranslation(glm::vec3(light->getPosition().x, light->getPosition().y, light->getPosition().z)); batch.setModelTransform(model); From fb37af3e043a94e091a41742f384745d9493bc64 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Thu, 9 Jul 2015 09:25:07 -0700 Subject: [PATCH 054/107] Removing dead code --- .../src/DeferredLightingEffect.cpp | 4 +-- .../render-utils/src/DeferredLightingEffect.h | 26 ------------------- 2 files changed, 1 insertion(+), 29 deletions(-) diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 9839a30d8f..7984c27da6 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -540,7 +540,6 @@ void DeferredLightingEffect::render(RenderArgs* args) { batch.setUniformTexture(2, nullptr); batch.setUniformTexture(3, nullptr); - glDepthRange(0.0, 1.0); args->_context->syncCache(); args->_context->render(batch); @@ -762,8 +761,7 @@ model::MeshPointer DeferredLightingEffect::getSpotLightMesh() { delete[] indexData; model::Mesh::Part part(0, indices, 0, model::Mesh::TRIANGLES); - // model::Mesh::Part part(0, indices, 0, model::Mesh::LINE_STRIP); - + _spotLightMesh->setPartBuffer(gpu::BufferView(new gpu::Buffer(sizeof(part), (gpu::Byte*) &part), gpu::Element::PART_DRAWCALL)); _spotLightMesh->makeBufferStream(); diff --git a/libraries/render-utils/src/DeferredLightingEffect.h b/libraries/render-utils/src/DeferredLightingEffect.h index 0732ffea79..2c5ef6bb83 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.h +++ b/libraries/render-utils/src/DeferredLightingEffect.h @@ -137,33 +137,7 @@ private: LightLocations _pointLightLocations; gpu::PipelinePointer _spotLight; LightLocations _spotLightLocations; -/* - ProgramObject _directionalSkyboxLight; - LightLocations _directionalSkyboxLightLocations; - ProgramObject _directionalSkyboxLightShadowMap; - LightLocations _directionalSkyboxLightShadowMapLocations; - ProgramObject _directionalSkyboxLightCascadedShadowMap; - LightLocations _directionalSkyboxLightCascadedShadowMapLocations; - ProgramObject _directionalAmbientSphereLight; - LightLocations _directionalAmbientSphereLightLocations; - ProgramObject _directionalAmbientSphereLightShadowMap; - LightLocations _directionalAmbientSphereLightShadowMapLocations; - ProgramObject _directionalAmbientSphereLightCascadedShadowMap; - LightLocations _directionalAmbientSphereLightCascadedShadowMapLocations; - - ProgramObject _directionalLight; - LightLocations _directionalLightLocations; - ProgramObject _directionalLightShadowMap; - LightLocations _directionalLightShadowMapLocations; - ProgramObject _directionalLightCascadedShadowMap; - LightLocations _directionalLightCascadedShadowMapLocations; - - ProgramObject _pointLight; - LightLocations _pointLightLocations; - ProgramObject _spotLight; - LightLocations _spotLightLocations; -*/ class PointLight { public: glm::vec4 position; From 8f4a10844195120cdaa19f9675f7fc9dac30a30d Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Thu, 9 Jul 2015 09:37:35 -0700 Subject: [PATCH 055/107] Dead code and disable debug info --- libraries/render-utils/src/DeferredLightingEffect.cpp | 4 ++-- libraries/render-utils/src/deferred_light_limited.slv | 4 ---- libraries/render-utils/src/spot_light.slf | 3 --- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 7984c27da6..b86f628265 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -471,8 +471,8 @@ void DeferredLightingEffect::render(RenderArgs* args) { for (auto lightID : _spotLights) { auto light = _allocatedLights[lightID]; - // IN DEBUG: - light->setShowContour(true); + // IN DEBUG: light->setShowContour(true); + batch.setUniformBuffer(_spotLightLocations.lightBufferUnit, light->getSchemaBuffer()); auto eyeLightPos = eyePoint - light->getPosition(); diff --git a/libraries/render-utils/src/deferred_light_limited.slv b/libraries/render-utils/src/deferred_light_limited.slv index e657f36598..d57b987b68 100644 --- a/libraries/render-utils/src/deferred_light_limited.slv +++ b/libraries/render-utils/src/deferred_light_limited.slv @@ -19,16 +19,12 @@ uniform mat4 texcoordMat; void main(void) { - // gl_Position = ftransform(); - // standard transform TransformCamera cam = getTransformCamera(); TransformObject obj = getTransformObject(); <$transformModelToClipPos(cam, obj, gl_Vertex, gl_Position)$>; vec4 projected = gl_Position / gl_Position.w; - // gl_TexCoord[0] = vec4(dot(projected, gl_ObjectPlaneS[3]) * gl_Position.w, - // dot(projected, gl_ObjectPlaneT[3]) * gl_Position.w, 0.0, gl_Position.w); gl_TexCoord[0] = vec4(dot(projected, texcoordMat[0]) * gl_Position.w, dot(projected, texcoordMat[1]) * gl_Position.w, 0.0, gl_Position.w); } diff --git a/libraries/render-utils/src/spot_light.slf b/libraries/render-utils/src/spot_light.slf index 298a8e1502..cfb2a6da4b 100644 --- a/libraries/render-utils/src/spot_light.slf +++ b/libraries/render-utils/src/spot_light.slf @@ -35,9 +35,6 @@ void main(void) { discard; } - //gl_FragColor = vec4(1.0, 1.0, 1.0, 0.0); - //return; - // Need the light now Light light = getLight(); From 2fff560ff9dadfcbb2d8b60eebd881676812cbae Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Thu, 9 Jul 2015 09:54:35 -0700 Subject: [PATCH 056/107] Fixing log file viewer --- interface/src/FileLogger.cpp | 13 ++++++++++--- interface/src/FileLogger.h | 7 +++---- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/interface/src/FileLogger.cpp b/interface/src/FileLogger.cpp index 508586c23c..00da80814b 100644 --- a/interface/src/FileLogger.cpp +++ b/interface/src/FileLogger.cpp @@ -45,8 +45,7 @@ private: static FilePersistThread* _persistThreadInstance; FileLogger::FileLogger(QObject* parent) : - AbstractLoggerInterface(parent), - _logData("") + AbstractLoggerInterface(parent) { _persistThreadInstance = new FilePersistThread(*this); _persistThreadInstance->initialize(true, QThread::LowestPriority); @@ -64,9 +63,17 @@ FileLogger::~FileLogger() { void FileLogger::addMessage(const QString& message) { _persistThreadInstance->queueItem(message); emit logReceived(message); - //_logData += message; } void FileLogger::locateLog() { FileUtils::locateFile(_fileName); } + +QString FileLogger::getLogData() { + QString result; + QFile f(_fileName); + if (f.open(QFile::ReadOnly | QFile::Text)) { + result = QTextStream(&f).readAll(); + } + return result; +} diff --git a/interface/src/FileLogger.h b/interface/src/FileLogger.h index 72bffa6445..549654ca5c 100644 --- a/interface/src/FileLogger.h +++ b/interface/src/FileLogger.h @@ -22,12 +22,11 @@ public: FileLogger(QObject* parent = NULL); virtual ~FileLogger(); - virtual void addMessage(const QString&); - virtual QString getLogData() { return _logData; } - virtual void locateLog(); + virtual void addMessage(const QString&) override; + virtual QString getLogData() override; + virtual void locateLog() override; private: - QString _logData; QString _fileName; friend class FilePersistThread; }; From 110b009aa4fc312c89cad93015fcd953ca303417 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Thu, 9 Jul 2015 09:55:25 -0700 Subject: [PATCH 057/107] Less magic in this world --- libraries/render-utils/src/DeferredLightingEffect.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index b86f628265..813127f349 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -478,7 +478,8 @@ void DeferredLightingEffect::render(RenderArgs* args) { auto eyeLightPos = eyePoint - light->getPosition(); auto eyeHalfPlaneDistance = glm::dot(eyeLightPos, light->getDirection()); - glm::vec4 coneParam(light->getSpotAngleCosSin(), 0.66f * tan(0.5 * light->getSpotAngle()), 1.0f); + const float TANGENT_LENGTH_SCALE = 0.666f; + glm::vec4 coneParam(light->getSpotAngleCosSin(), TANGENT_LENGTH_SCALE * tan(0.5 * light->getSpotAngle()), 1.0f); float expandedRadius = light->getMaximumRadius() * (1.0f + SCALE_EXPANSION); // TODO: We shouldn;t have to do that test and use a different volume geometry for when inside the vlight volume, From 08d63b7fd89839c1b34ccf5ff44fac70f1480800 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Thu, 9 Jul 2015 11:03:02 -0700 Subject: [PATCH 058/107] first attempt at palm rotation --- interface/src/avatar/AvatarActionHold.cpp | 4 ++++ interface/src/avatar/MyAvatar.cpp | 26 +++++++++++++++++++++++ interface/src/avatar/MyAvatar.h | 6 ++++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index ca903a9771..c28032ddde 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -52,15 +52,19 @@ void AvatarActionHold::updateActionWorker(float deltaTimeStep) { } glm::vec3 palmPosition; + glm::quat palmRotation; if (_hand == "right") { palmPosition = myAvatar->getRightPalmPosition(); + palmRotation = myAvatar->getRightPalmRotation(); } else { palmPosition = myAvatar->getLeftPalmPosition(); + palmRotation = myAvatar->getLeftPalmRotation(); } auto rotation = myAvatar->getWorldAlignedOrientation(); auto offset = rotation * _relativePosition; auto position = palmPosition + offset; + rotation *= palmRotation; rotation *= _relativeRotation; unlock(); diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index aa6b6afc66..1a900d6afd 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -371,6 +371,19 @@ glm::vec3 MyAvatar::getLeftPalmPosition() { return leftHandPosition; } +glm::quat MyAvatar::getLeftPalmRotation() { + unsigned int leftIndex = 0; + if (getHand()->getNumPalms() > leftIndex) { + PalmData& palm = getHand()->getPalms()[leftIndex]; + if (palm.isActive()) { + glm::vec3 tipDirection = glm::normalize(palm.getTipPosition() - palm.getPosition()); + glm::vec3 avatarDirection = glm::vec3(0.0f, 0.0f, 1.0f) * getOrientation(); + return rotationBetween(avatarDirection, tipDirection); + } + } + return glm::quat(); +} + glm::vec3 MyAvatar::getRightPalmPosition() { glm::vec3 rightHandPosition; getSkeletonModel().getRightHandPosition(rightHandPosition); @@ -380,6 +393,19 @@ glm::vec3 MyAvatar::getRightPalmPosition() { return rightHandPosition; } +glm::quat MyAvatar::getRightPalmRotation() { + unsigned int rightIndex = 1; + if (getHand()->getNumPalms() > rightIndex) { + PalmData& palm = getHand()->getPalms()[rightIndex]; + if (palm.isActive()) { + glm::vec3 tipDirection = glm::normalize(palm.getTipPosition() - palm.getPosition()); + glm::vec3 avatarDirection = glm::vec3(0.0f, 0.0f, 1.0f) * getOrientation(); + return rotationBetween(avatarDirection, tipDirection); + } + } + return glm::quat(); +} + void MyAvatar::clearReferential() { changeReferential(NULL); } diff --git a/interface/src/avatar/MyAvatar.h b/interface/src/avatar/MyAvatar.h index 4e76143774..34dfcad2ad 100644 --- a/interface/src/avatar/MyAvatar.h +++ b/interface/src/avatar/MyAvatar.h @@ -195,10 +195,12 @@ public slots: void setThrust(glm::vec3 newThrust) { _thrust = newThrust; } void updateMotionBehavior(); - + glm::vec3 getLeftPalmPosition(); + glm::quat getLeftPalmRotation(); glm::vec3 getRightPalmPosition(); - + glm::quat getRightPalmRotation(); + void clearReferential(); bool setModelReferential(const QUuid& id); bool setJointReferential(const QUuid& id, int jointIndex); From 46bca30698138ff45b865199defbe01d161f2b1a Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Thu, 9 Jul 2015 11:29:53 -0700 Subject: [PATCH 059/107] MotionState reports zero velocity for slow objects This reduces the number of updates for an Entity that is effectively at rest due to a balance between some spring action and gravity, but nevertheless has a slight non-zero velocity at the end of each simulation step. --- libraries/physics/src/EntityMotionState.cpp | 6 +++--- libraries/physics/src/ObjectMotionState.cpp | 12 +++++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index a975d21c4d..8d77f815b7 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -248,7 +248,7 @@ bool EntityMotionState::remoteSimulationOutOfSync(uint32_t simulationStep) { btTransform xform = _body->getWorldTransform(); _serverPosition = bulletToGLM(xform.getOrigin()); _serverRotation = bulletToGLM(xform.getRotation()); - _serverVelocity = bulletToGLM(_body->getLinearVelocity()); + _serverVelocity = getBodyLinearVelocity(); _serverAngularVelocity = bulletToGLM(_body->getAngularVelocity()); _lastStep = simulationStep; _serverActionData = _entity->getActionData(); @@ -536,7 +536,7 @@ void EntityMotionState::bump(quint8 priority) { void EntityMotionState::resetMeasuredBodyAcceleration() { _lastMeasureStep = ObjectMotionState::getWorldSimulationStep(); if (_body) { - _lastVelocity = bulletToGLM(_body->getLinearVelocity()); + _lastVelocity = getBodyLinearVelocity(); } else { _lastVelocity = glm::vec3(0.0f); } @@ -555,7 +555,7 @@ void EntityMotionState::measureBodyAcceleration() { // Note: the integration equation for velocity uses damping: v1 = (v0 + a * dt) * (1 - D)^dt // hence the equation for acceleration is: a = (v1 / (1 - D)^dt - v0) / dt - glm::vec3 velocity = bulletToGLM(_body->getLinearVelocity()); + glm::vec3 velocity = getBodyLinearVelocity(); _measuredAcceleration = (velocity / powf(1.0f - _body->getLinearDamping(), dt) - _lastVelocity) * invDt; _lastVelocity = velocity; if (numSubsteps > PHYSICS_ENGINE_MAX_NUM_SUBSTEPS) { diff --git a/libraries/physics/src/ObjectMotionState.cpp b/libraries/physics/src/ObjectMotionState.cpp index 7ff119fb79..8e4bbdae1c 100644 --- a/libraries/physics/src/ObjectMotionState.cpp +++ b/libraries/physics/src/ObjectMotionState.cpp @@ -80,7 +80,17 @@ void ObjectMotionState::setBodyGravity(const glm::vec3& gravity) const { } glm::vec3 ObjectMotionState::getBodyLinearVelocity() const { - return bulletToGLM(_body->getLinearVelocity()); + // returns the body's velocity unless it is moving too slow in which case returns zero + btVector3 velocity = _body->getLinearVelocity(); + + // NOTE: the threshold to use here relates to the linear displacement threshold (dX) for sending updates + // to objects that are tracked server-side (e.g. entities which use dX = 2mm). Hence an object moving + // just under this velocity threshold would trigger an update about V/dX times per second. + const float MIN_LINEAR_SPEED_SQUARED = 0.0036f; // 6 mm/sec + if (velocity.length2() < MIN_LINEAR_SPEED_SQUARED) { + velocity *= 0.0f; + } + return bulletToGLM(velocity); } glm::vec3 ObjectMotionState::getObjectLinearVelocityChange() const { return glm::vec3(0.0f); // Subclasses override where meaningful. From 612e9649d8ab85c64cbab526f6f0d5849b323df5 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Thu, 9 Jul 2015 11:32:30 -0700 Subject: [PATCH 060/107] fix update logic of ObjectActionOffset --- libraries/physics/src/ObjectActionOffset.cpp | 104 +++++++++++-------- libraries/physics/src/ObjectActionOffset.h | 1 + 2 files changed, 61 insertions(+), 44 deletions(-) diff --git a/libraries/physics/src/ObjectActionOffset.cpp b/libraries/physics/src/ObjectActionOffset.cpp index 7cf35b27d7..ff789dfefe 100644 --- a/libraries/physics/src/ObjectActionOffset.cpp +++ b/libraries/physics/src/ObjectActionOffset.cpp @@ -16,7 +16,11 @@ const uint16_t ObjectActionOffset::offsetVersion = 1; ObjectActionOffset::ObjectActionOffset(const QUuid& id, EntityItemPointer ownerEntity) : - ObjectAction(ACTION_TYPE_OFFSET, id, ownerEntity) { + ObjectAction(ACTION_TYPE_OFFSET, id, ownerEntity), + _pointToOffsetFrom(0.f), + _linearDistance(0.0f), + _linearTimeScale(FLT_MAX), + _positionalTargetSet(false) { #if WANT_DEBUG qDebug() << "ObjectActionOffset::ObjectActionOffset"; #endif @@ -44,6 +48,7 @@ void ObjectActionOffset::updateActionWorker(btScalar deltaTimeStep) { unlock(); return; } + ObjectMotionState* motionState = static_cast(physicsInfo); btRigidBody* rigidBody = motionState->getRigidBody(); if (!rigidBody) { @@ -52,21 +57,32 @@ void ObjectActionOffset::updateActionWorker(btScalar deltaTimeStep) { return; } - if (_positionalTargetSet) { - glm::vec3 offset = _pointToOffsetFrom - bulletToGLM(rigidBody->getCenterOfMassPosition()); - float offsetLength = glm::length(offset); - float offsetError = _linearDistance - offsetLength; - - // if (glm::abs(offsetError) > IGNORE_POSITION_DELTA) { - if (glm::abs(offsetError) > 0.0f) { - float offsetErrorAbs = glm::abs(offsetError); - float offsetErrorDirection = - offsetError / offsetErrorAbs; - glm::vec3 previousVelocity = bulletToGLM(rigidBody->getLinearVelocity()); - - glm::vec3 velocityAdjustment = glm::normalize(offset) * offsetErrorDirection * offsetErrorAbs / _linearTimeScale; - rigidBody->setLinearVelocity(glmToBullet(previousVelocity + velocityAdjustment)); - // rigidBody->setLinearVelocity(glmToBullet(velocityAdjustment)); + const float MAX_LINEAR_TIMESCALE = 600.0f; // 10 minutes is a long time + if (_positionalTargetSet && _linearTimeScale < MAX_LINEAR_TIMESCALE) { + if (_needsActivation) { rigidBody->activate(); + _needsActivation = false; + } + glm::vec3 objectPosition = bulletToGLM(rigidBody->getCenterOfMassPosition()); + glm::vec3 springAxis = objectPosition - _pointToOffsetFrom; // from anchor to object + float distance = glm::length(springAxis); + if (distance > FLT_EPSILON) { + springAxis /= distance; // normalize springAxis + + // compute (critically damped) target velocity of spring relaxation + glm::vec3 offset = (distance - _linearDistance) * springAxis; + glm::vec3 targetVelocity = (-1.0f / _linearTimeScale) * offset; + + // compute current velocity and its parallel component + glm::vec3 currentVelocity = bulletToGLM(rigidBody->getLinearVelocity()); + glm::vec3 parallelVelocity = glm::dot(currentVelocity, springAxis) * springAxis; + + // we blend the parallel component with the spring's target velocity to get the new velocity + float blend = deltaTimeStep / _linearTimeScale; + if (blend > 1.0f) { + blend = 1.0f; + } + rigidBody->setLinearVelocity(glmToBullet(currentVelocity + blend * (targetVelocity - parallelVelocity)); } } @@ -75,45 +91,45 @@ void ObjectActionOffset::updateActionWorker(btScalar deltaTimeStep) { bool ObjectActionOffset::updateArguments(QVariantMap arguments) { - bool pOk0 = true; + bool ok = true; glm::vec3 pointToOffsetFrom = - EntityActionInterface::extractVec3Argument("offset action", arguments, "pointToOffsetFrom", pOk0, true); + EntityActionInterface::extractVec3Argument("offset action", arguments, "pointToOffsetFrom", ok, true); + if (!ok) { + return false; + } - bool pOk1 = true; + ok = true; float linearTimeScale = - EntityActionInterface::extractFloatArgument("offset action", arguments, "linearTimeScale", pOk1, false); + EntityActionInterface::extractFloatArgument("offset action", arguments, "linearTimeScale", ok, false); + if (ok) { + if (linearTimeScale <= 0.0f) { + qDebug() << "offset action -- linearTimeScale must be greater than zero."; + return false; + } + } else { + linearTimeScale = 0.1f; + } - bool pOk2 = true; + ok = true; float linearDistance = - EntityActionInterface::extractFloatArgument("offset action", arguments, "linearDistance", pOk2, false); - - if (!pOk0) { - return false; - } - if (pOk1 && linearTimeScale <= 0.0f) { - qDebug() << "offset action -- linearTimeScale must be greater than zero."; - return false; + EntityActionInterface::extractFloatArgument("offset action", arguments, "linearDistance", ok, false); + if (!ok) { + linearDistance = 0.0f; } - lockForWrite(); - - _pointToOffsetFrom = pointToOffsetFrom; - _positionalTargetSet = true; - - if (pOk1) { + // only change stuff if something actually changed + if (_pointToOffsetFrom != pointToOffsetFrom + || _linearTimeScale != linearTimeScale + || _linearDistance != linearDistance) { + lockForWrite(); + _pointToOffsetFrom = pointToOffsetFrom; _linearTimeScale = linearTimeScale; - } else { - _linearTimeScale = 0.1f; - } - - if (pOk2) { _linearDistance = linearDistance; - } else { - _linearDistance = 1.0f; + _positionalTargetSet = true; + _active = true; + _needsActivation = true; + unlock(); } - - _active = true; - unlock(); return true; } diff --git a/libraries/physics/src/ObjectActionOffset.h b/libraries/physics/src/ObjectActionOffset.h index 1918da6996..a0190f3832 100644 --- a/libraries/physics/src/ObjectActionOffset.h +++ b/libraries/physics/src/ObjectActionOffset.h @@ -36,6 +36,7 @@ public: float _linearDistance; float _linearTimeScale; bool _positionalTargetSet; + bool _needsActivation = true; }; #endif // hifi_ObjectActionOffset_h From ccf14b2e50123907743fce7487233e477d95276d Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Thu, 9 Jul 2015 11:43:57 -0700 Subject: [PATCH 061/107] fix syntax error typo --- libraries/physics/src/ObjectActionOffset.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/physics/src/ObjectActionOffset.cpp b/libraries/physics/src/ObjectActionOffset.cpp index ff789dfefe..6189128997 100644 --- a/libraries/physics/src/ObjectActionOffset.cpp +++ b/libraries/physics/src/ObjectActionOffset.cpp @@ -82,7 +82,7 @@ void ObjectActionOffset::updateActionWorker(btScalar deltaTimeStep) { if (blend > 1.0f) { blend = 1.0f; } - rigidBody->setLinearVelocity(glmToBullet(currentVelocity + blend * (targetVelocity - parallelVelocity)); + rigidBody->setLinearVelocity(glmToBullet(currentVelocity + blend * (targetVelocity - parallelVelocity))); } } From 55207a8fb1ce7da208cf0bd1d22b578dc1729661 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Thu, 9 Jul 2015 11:59:28 -0700 Subject: [PATCH 062/107] enforce coding standards --- libraries/physics/src/ObjectActionOffset.cpp | 2 +- libraries/physics/src/ObjectMotionState.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/physics/src/ObjectActionOffset.cpp b/libraries/physics/src/ObjectActionOffset.cpp index 6189128997..2391ded13d 100644 --- a/libraries/physics/src/ObjectActionOffset.cpp +++ b/libraries/physics/src/ObjectActionOffset.cpp @@ -17,7 +17,7 @@ const uint16_t ObjectActionOffset::offsetVersion = 1; ObjectActionOffset::ObjectActionOffset(const QUuid& id, EntityItemPointer ownerEntity) : ObjectAction(ACTION_TYPE_OFFSET, id, ownerEntity), - _pointToOffsetFrom(0.f), + _pointToOffsetFrom(0.0f), _linearDistance(0.0f), _linearTimeScale(FLT_MAX), _positionalTargetSet(false) { diff --git a/libraries/physics/src/ObjectMotionState.cpp b/libraries/physics/src/ObjectMotionState.cpp index 8e4bbdae1c..aeec85f7ea 100644 --- a/libraries/physics/src/ObjectMotionState.cpp +++ b/libraries/physics/src/ObjectMotionState.cpp @@ -92,6 +92,7 @@ glm::vec3 ObjectMotionState::getBodyLinearVelocity() const { } return bulletToGLM(velocity); } + glm::vec3 ObjectMotionState::getObjectLinearVelocityChange() const { return glm::vec3(0.0f); // Subclasses override where meaningful. } From d197bf2a8519d69bf90ae8f1f6d6975197afa576 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Thu, 9 Jul 2015 12:18:32 -0700 Subject: [PATCH 063/107] use palm rotation data in hold action --- examples/stick-hydra.js | 73 +++++++++++++++++++++++ interface/src/avatar/AvatarActionHold.cpp | 4 +- interface/src/avatar/MyAvatar.cpp | 26 ++------ 3 files changed, 80 insertions(+), 23 deletions(-) create mode 100644 examples/stick-hydra.js diff --git a/examples/stick-hydra.js b/examples/stick-hydra.js new file mode 100644 index 0000000000..42ac6c6cda --- /dev/null +++ b/examples/stick-hydra.js @@ -0,0 +1,73 @@ +// stick.js +// examples +// +// Created by Seth Alves on 2015-6-10 +// Copyright 2015 High Fidelity, Inc. +// +// Allow avatar to hold a stick +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +var hand = "left"; +var nullActionID = "00000000-0000-0000-0000-000000000000"; +var controllerID; +var controllerActive; +var stickID = null; +var actionID = nullActionID; +var makingNewStick = false; + +function makeNewStick() { + if (makingNewStick) { + return; + } + makingNewStick = true; + cleanUp(); + // sometimes if this is run immediately the stick doesn't get created? use a timer. + Script.setTimeout(function() { + stickID = Entities.addEntity({ + type: "Model", + name: "stick", + modelURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx", + compoundShapeURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.obj", + dimensions: {x: .11, y: .11, z: 1.0}, + position: MyAvatar.getRightPalmPosition(), // initial position doesn't matter, as long as it's close + rotation: MyAvatar.orientation, + damping: .1, + collisionSoundURL: "http://public.highfidelity.io/sounds/Collisions-hitsandslaps/67LCollision07.wav", + restitution: 0.01, + collisionsWillMove: true + }); + actionID = Entities.addAction("hold", stickID, + {relativePosition: {x: 0.0, y: 0.0, z: -0.5}, + relativeRotation: Quat.fromVec3Degrees({x: 0.0, y: 90.0, z: 0.0}), + hand: hand, + timeScale: 0.15}); + if (actionID == nullActionID) { + cleanUp(); + } + makingNewStick = false; + }, 3000); +} + + +function cleanUp() { + if (stickID) { + Entities.deleteEntity(stickID); + stickID = null; + } +} + + +function initControls(){ + if (hand == "right") { + controllerID = 3; // right handed + } else { + controllerID = 4; // left handed + } +} + + +Script.scriptEnding.connect(cleanUp); +makeNewStick(); diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index c28032ddde..97d679f94b 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -61,11 +61,9 @@ void AvatarActionHold::updateActionWorker(float deltaTimeStep) { palmRotation = myAvatar->getLeftPalmRotation(); } - auto rotation = myAvatar->getWorldAlignedOrientation(); + auto rotation = palmRotation * _relativeRotation; auto offset = rotation * _relativePosition; auto position = palmPosition + offset; - rotation *= palmRotation; - rotation *= _relativeRotation; unlock(); if (!tryLockForWrite()) { diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 1a900d6afd..79c600b7ec 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -372,16 +372,9 @@ glm::vec3 MyAvatar::getLeftPalmPosition() { } glm::quat MyAvatar::getLeftPalmRotation() { - unsigned int leftIndex = 0; - if (getHand()->getNumPalms() > leftIndex) { - PalmData& palm = getHand()->getPalms()[leftIndex]; - if (palm.isActive()) { - glm::vec3 tipDirection = glm::normalize(palm.getTipPosition() - palm.getPosition()); - glm::vec3 avatarDirection = glm::vec3(0.0f, 0.0f, 1.0f) * getOrientation(); - return rotationBetween(avatarDirection, tipDirection); - } - } - return glm::quat(); + glm::quat leftRotation; + getSkeletonModel().getJointRotationInWorldFrame(getSkeletonModel().getLeftHandJointIndex(), leftRotation); + return leftRotation; } glm::vec3 MyAvatar::getRightPalmPosition() { @@ -394,16 +387,9 @@ glm::vec3 MyAvatar::getRightPalmPosition() { } glm::quat MyAvatar::getRightPalmRotation() { - unsigned int rightIndex = 1; - if (getHand()->getNumPalms() > rightIndex) { - PalmData& palm = getHand()->getPalms()[rightIndex]; - if (palm.isActive()) { - glm::vec3 tipDirection = glm::normalize(palm.getTipPosition() - palm.getPosition()); - glm::vec3 avatarDirection = glm::vec3(0.0f, 0.0f, 1.0f) * getOrientation(); - return rotationBetween(avatarDirection, tipDirection); - } - } - return glm::quat(); + glm::quat rightRotation; + getSkeletonModel().getJointRotationInWorldFrame(getSkeletonModel().getRightHandJointIndex(), rightRotation); + return rightRotation; } void MyAvatar::clearReferential() { From 85f206d49d36e6ab7d13355c3e8b3b938f430c56 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Thu, 9 Jul 2015 14:13:01 -0700 Subject: [PATCH 064/107] if an action is updating its own arguments, cause updates to go out over network --- libraries/physics/src/EntityMotionState.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/physics/src/EntityMotionState.cpp b/libraries/physics/src/EntityMotionState.cpp index 8d77f815b7..b06c6f264b 100644 --- a/libraries/physics/src/EntityMotionState.cpp +++ b/libraries/physics/src/EntityMotionState.cpp @@ -287,6 +287,7 @@ bool EntityMotionState::remoteSimulationOutOfSync(uint32_t simulationStep) { } if (_serverActionData != _entity->getActionData()) { + setOutgoingPriority(SCRIPT_EDIT_SIMULATION_PRIORITY); return true; } From 25708678f6eed2ed69b6dbaaac358fb30bb0b0e0 Mon Sep 17 00:00:00 2001 From: Bing Shearer Date: Thu, 9 Jul 2015 14:18:53 -0700 Subject: [PATCH 065/107] Fixed shutdown procedure for Datagrams so that there is no crash on exit --- interface/src/DatagramProcessor.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/interface/src/DatagramProcessor.cpp b/interface/src/DatagramProcessor.cpp index f691527186..99308a922f 100644 --- a/interface/src/DatagramProcessor.cpp +++ b/interface/src/DatagramProcessor.cpp @@ -29,12 +29,13 @@ DatagramProcessor::DatagramProcessor(QObject* parent) : } void DatagramProcessor::processDatagrams() { - PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), - "DatagramProcessor::processDatagrams()"); if (_isShuttingDown) { return; // bail early... we're shutting down. } + PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), + "DatagramProcessor::processDatagrams()"); + HifiSockAddr senderSockAddr; From 85f6fdb8cfedd09f73fc66de9223df983b8c1de2 Mon Sep 17 00:00:00 2001 From: Bing Shearer Date: Thu, 9 Jul 2015 14:20:10 -0700 Subject: [PATCH 066/107] Fixed bug which would cause program to crash if there was no image associated with an overlay in windows build (see checkbox in sunLightExample) --- interface/src/ui/overlays/ImageOverlay.cpp | 67 +++++++++++----------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/interface/src/ui/overlays/ImageOverlay.cpp b/interface/src/ui/overlays/ImageOverlay.cpp index 399e8a459a..e80c6f584b 100644 --- a/interface/src/ui/overlays/ImageOverlay.cpp +++ b/interface/src/ui/overlays/ImageOverlay.cpp @@ -75,46 +75,49 @@ void ImageOverlay::render(RenderArgs* args) { glm::vec2 topLeft(left, top); glm::vec2 bottomRight(right, bottom); - float imageWidth = _texture->getWidth(); - float imageHeight = _texture->getHeight(); + // if for some reason our image is not over 0 width or height, don't attempt to render the image - if (_renderImage && imageWidth > 0 && imageHeight > 0) { + if (_renderImage) { + float imageWidth = _texture->getWidth(); + float imageHeight = _texture->getHeight(); + if (imageWidth > 0 && imageHeight > 0) { + QRect fromImage; + if (_wantClipFromImage) { + float scaleX = imageWidth / _texture->getOriginalWidth(); + float scaleY = imageHeight / _texture->getOriginalHeight(); - QRect fromImage; - if (_wantClipFromImage) { - float scaleX = imageWidth / _texture->getOriginalWidth(); - float scaleY = imageHeight / _texture->getOriginalHeight(); + fromImage.setX(scaleX * _fromImage.x()); + fromImage.setY(scaleY * _fromImage.y()); + fromImage.setWidth(scaleX * _fromImage.width()); + fromImage.setHeight(scaleY * _fromImage.height()); + } + else { + fromImage.setX(0); + fromImage.setY(0); + fromImage.setWidth(imageWidth); + fromImage.setHeight(imageHeight); + } - fromImage.setX(scaleX * _fromImage.x()); - fromImage.setY(scaleY * _fromImage.y()); - fromImage.setWidth(scaleX * _fromImage.width()); - fromImage.setHeight(scaleY * _fromImage.height()); - } else { - fromImage.setX(0); - fromImage.setY(0); - fromImage.setWidth(imageWidth); - fromImage.setHeight(imageHeight); + float x = fromImage.x() / imageWidth; + float y = fromImage.y() / imageHeight; + float w = fromImage.width() / imageWidth; // ?? is this what we want? not sure + float h = fromImage.height() / imageHeight; + + glm::vec2 texCoordTopLeft(x, y); + glm::vec2 texCoordBottomRight(x + w, y + h); + + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, quadColor); + } + else { + DependencyManager::get()->renderQuad(topLeft, bottomRight, quadColor); } - float x = fromImage.x() / imageWidth; - float y = fromImage.y() / imageHeight; - float w = fromImage.width() / imageWidth; // ?? is this what we want? not sure - float h = fromImage.height() / imageHeight; - - glm::vec2 texCoordTopLeft(x, y); - glm::vec2 texCoordBottomRight(x + w, y + h); - - DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, quadColor); - } else { - DependencyManager::get()->renderQuad(topLeft, bottomRight, quadColor); - } - - if (_renderImage) { - glDisable(GL_TEXTURE_2D); + if (_renderImage) { + glDisable(GL_TEXTURE_2D); + } } } - void ImageOverlay::setProperties(const QScriptValue& properties) { Overlay2D::setProperties(properties); From a01fb6f1dc89712df6fd03aedc3323242257987b Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Thu, 9 Jul 2015 14:32:34 -0700 Subject: [PATCH 067/107] if an action changes its own arguments, cause reserialisation --- interface/src/avatar/AvatarActionHold.cpp | 4 ++++ libraries/entities/src/EntityItem.cpp | 8 ++++++++ libraries/entities/src/EntityItem.h | 2 ++ 3 files changed, 14 insertions(+) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 97d679f94b..cdb59ebb96 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -147,6 +147,10 @@ bool AvatarActionHold::updateArguments(QVariantMap arguments) { _rotationalTargetSet = true; _active = true; unlock(); + auto ownerEntity = _ownerEntity.lock(); + if (ownerEntity) { + ownerEntity->setActionDataDirty(true); + } return true; } diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 1165518097..3aeaf4829f 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -1714,6 +1714,14 @@ QByteArray EntityItem::serializeActions(bool& success) const { } const QByteArray EntityItem::getActionData() const { + if (_actionDataDirty) { + bool success; + QByteArray newDataCache = serializeActions(success); + if (success) { + _allActionsDataCache = newDataCache; + } + _actionDataDirty = false; + } return _allActionsDataCache; } diff --git a/libraries/entities/src/EntityItem.h b/libraries/entities/src/EntityItem.h index bc8901c6b1..7893818267 100644 --- a/libraries/entities/src/EntityItem.h +++ b/libraries/entities/src/EntityItem.h @@ -395,6 +395,7 @@ public: bool hasActions() { return !_objectActions.empty(); } QList getActionIDs() { return _objectActions.keys(); } QVariantMap getActionArguments(const QUuid& actionID) const; + void setActionDataDirty(bool value) const { _actionDataDirty = value; } protected: @@ -482,6 +483,7 @@ protected: void checkWaitingToRemove(EntitySimulation* simulation = nullptr); mutable QByteArray _waitingActionData; mutable QSet _actionsToRemove; + mutable bool _actionDataDirty = false; }; #endif // hifi_EntityItem_h From 06563264e219c09ee2a56e8dc57d7a8263d068da Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Thu, 9 Jul 2015 14:34:33 -0700 Subject: [PATCH 068/107] Improve batched font rendering smoothing --- libraries/render-utils/src/TextRenderer3D.cpp | 2 +- libraries/render-utils/src/sdf_text3D.slf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/render-utils/src/TextRenderer3D.cpp b/libraries/render-utils/src/TextRenderer3D.cpp index d202d89aba..a429719b8b 100644 --- a/libraries/render-utils/src/TextRenderer3D.cpp +++ b/libraries/render-utils/src/TextRenderer3D.cpp @@ -343,7 +343,7 @@ void Font3D::setupGPU() { gpu::StatePointer state = gpu::StatePointer(new gpu::State()); state->setCullMode(gpu::State::CULL_BACK); state->setDepthTest(true, true, gpu::LESS_EQUAL); - state->setBlendFunction(false, + state->setBlendFunction(true, gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA, gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE); _pipeline = gpu::PipelinePointer(gpu::Pipeline::create(program, state)); diff --git a/libraries/render-utils/src/sdf_text3D.slf b/libraries/render-utils/src/sdf_text3D.slf index d9972417ba..425d6f9b08 100644 --- a/libraries/render-utils/src/sdf_text3D.slf +++ b/libraries/render-utils/src/sdf_text3D.slf @@ -18,7 +18,7 @@ uniform vec4 Color; varying vec4 interpolatedNormal; const float gamma = 2.2; -const float smoothing = 64.0; +const float smoothing = 256.0; const float interiorCutoff = 0.8; const float outlineExpansion = 0.2; From 5312beeaf4905e1f99695da2a26c4a8fef26067b Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Thu, 9 Jul 2015 14:50:31 -0700 Subject: [PATCH 069/107] if an action changes its own arguments, cause reserialisation --- interface/src/avatar/AvatarActionHold.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index cdb59ebb96..72019ecf28 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -89,6 +89,11 @@ void AvatarActionHold::updateActionWorker(float deltaTimeStep) { _rotationalTarget = rotation; unlock(); + auto ownerEntity = _ownerEntity.lock(); + if (ownerEntity) { + ownerEntity->setActionDataDirty(true); + } + ObjectActionSpring::updateActionWorker(deltaTimeStep); } From a8a4c995290b9f6fad6c133f6a39fa74e8c789da Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 9 Jul 2015 15:01:16 -0700 Subject: [PATCH 070/107] on script shutdown, wait to process all pending edit messages before ending the script engine --- libraries/script-engine/src/ScriptEngine.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/libraries/script-engine/src/ScriptEngine.cpp b/libraries/script-engine/src/ScriptEngine.cpp index 2046fd622d..863cc28a94 100644 --- a/libraries/script-engine/src/ScriptEngine.cpp +++ b/libraries/script-engine/src/ScriptEngine.cpp @@ -710,7 +710,13 @@ void ScriptEngine::run() { // since we're in non-threaded mode, call process so that the packets are sent if (!entityScriptingInterface->getEntityPacketSender()->isThreaded()) { - entityScriptingInterface->getEntityPacketSender()->process(); + // wait here till the edit packet sender is completely done sending + while (entityScriptingInterface->getEntityPacketSender()->hasPacketsToSend()) { + entityScriptingInterface->getEntityPacketSender()->process(); + QCoreApplication::processEvents(); + } + } else { + // FIXME - do we need to have a similar "wait here" loop for non-threaded packet senders? } } From 1f13106f5167b947c177e45be6c3d97450c575c6 Mon Sep 17 00:00:00 2001 From: samcake Date: Thu, 9 Jul 2015 15:05:03 -0700 Subject: [PATCH 071/107] FIxed the issue on mac due to the abs function not correctly executing --- libraries/model/src/model/Light.cpp | 8 +++++--- .../render-utils/src/DeferredLightingEffect.cpp | 17 ++++++----------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/libraries/model/src/model/Light.cpp b/libraries/model/src/model/Light.cpp index 1bad381137..b7635b4af3 100755 --- a/libraries/model/src/model/Light.cpp +++ b/libraries/model/src/model/Light.cpp @@ -88,9 +88,11 @@ void Light::setSpotAngle(float angle) { dangle = glm::half_pi(); } - editSchema()._spot.x = (float)abs(cos(dangle)); - editSchema()._spot.y = (float)abs(sin(dangle)); - editSchema()._spot.z = (float)dangle; + auto cosAngle = cos(dangle); + auto sinAngle = sin(dangle); + editSchema()._spot.x = (float) std::abs(cosAngle); + editSchema()._spot.y = (float) std::abs(sinAngle); + editSchema()._spot.z = (float) angle; } void Light::setSpotExponent(float exponent) { diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index 813127f349..a721e0cad3 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -509,15 +509,9 @@ void DeferredLightingEffect::render(RenderArgs* args) { batch._glUniform4fv(_spotLightLocations.coneParam, 1, reinterpret_cast< const GLfloat* >(&coneParam)); Transform model; - model.setTranslation(glm::vec3(light->getPosition().x, light->getPosition().y, light->getPosition().z)); - - glm::quat spotRotation = rotationBetween(glm::vec3(0.0f, 0.0f, -1.0f), light->getDirection()); - spotRotation = light->getOrientation(); - model.postRotate(spotRotation); - - float base = expandedRadius * glm::tan(light->getSpotAngle()); - float height = expandedRadius; - model.postScale(glm::vec3(height, height, height)); + model.setTranslation(light->getPosition()); + model.postRotate(light->getOrientation()); + model.postScale(glm::vec3(expandedRadius, expandedRadius, expandedRadius)); batch.setModelTransform(model); auto mesh = getSpotLightMesh(); @@ -679,7 +673,7 @@ model::MeshPointer DeferredLightingEffect::getSpotLightMesh() { if (!_spotLightMesh) { _spotLightMesh.reset(new model::Mesh()); - int slices = 16; + int slices = 32; int rings = 3; int vertices = 2 + rings * slices; int originVertex = vertices - 2; @@ -762,7 +756,8 @@ model::MeshPointer DeferredLightingEffect::getSpotLightMesh() { delete[] indexData; model::Mesh::Part part(0, indices, 0, model::Mesh::TRIANGLES); - + //DEBUG: model::Mesh::Part part(0, indices, 0, model::Mesh::LINE_STRIP); + _spotLightMesh->setPartBuffer(gpu::BufferView(new gpu::Buffer(sizeof(part), (gpu::Byte*) &part), gpu::Element::PART_DRAWCALL)); _spotLightMesh->makeBufferStream(); From 2dc4922da35f9d5674caf15169c498bfae4fdce1 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Thu, 9 Jul 2015 15:23:05 -0700 Subject: [PATCH 072/107] bring over code from entity-level-locking branch --- .../src/AssignmentActionFactory.cpp | 7 +- .../src/AssignmentActionFactory.h | 7 +- interface/src/InterfaceActionFactory.cpp | 7 +- interface/src/InterfaceActionFactory.h | 6 +- interface/src/avatar/AvatarActionHold.cpp | 16 +- .../src/EntityActionFactoryInterface.h | 6 +- libraries/entities/src/EntityItem.cpp | 272 ++++++++++++------ libraries/entities/src/EntityItem.h | 39 ++- .../entities/src/EntityScriptingInterface.cpp | 2 +- libraries/entities/src/EntitySimulation.cpp | 1 + 10 files changed, 243 insertions(+), 120 deletions(-) diff --git a/assignment-client/src/AssignmentActionFactory.cpp b/assignment-client/src/AssignmentActionFactory.cpp index e1c5d3adff..7c404cbd97 100644 --- a/assignment-client/src/AssignmentActionFactory.cpp +++ b/assignment-client/src/AssignmentActionFactory.cpp @@ -17,8 +17,7 @@ EntityActionPointer assignmentActionFactory(EntityActionType type, const QUuid& } -EntityActionPointer AssignmentActionFactory::factory(EntitySimulation* simulation, - EntityActionType type, +EntityActionPointer AssignmentActionFactory::factory(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity, QVariantMap arguments) { @@ -33,9 +32,7 @@ EntityActionPointer AssignmentActionFactory::factory(EntitySimulation* simulatio } -EntityActionPointer AssignmentActionFactory::factoryBA(EntitySimulation* simulation, - EntityItemPointer ownerEntity, - QByteArray data) { +EntityActionPointer AssignmentActionFactory::factoryBA(EntityItemPointer ownerEntity, QByteArray data) { QDataStream serializedActionDataStream(data); EntityActionType type; QUuid id; diff --git a/assignment-client/src/AssignmentActionFactory.h b/assignment-client/src/AssignmentActionFactory.h index 41245dac68..e2d58f3e6a 100644 --- a/assignment-client/src/AssignmentActionFactory.h +++ b/assignment-client/src/AssignmentActionFactory.h @@ -19,14 +19,11 @@ class AssignmentActionFactory : public EntityActionFactoryInterface { public: AssignmentActionFactory() : EntityActionFactoryInterface() { } virtual ~AssignmentActionFactory() { } - virtual EntityActionPointer factory(EntitySimulation* simulation, - EntityActionType type, + virtual EntityActionPointer factory(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity, QVariantMap arguments); - virtual EntityActionPointer factoryBA(EntitySimulation* simulation, - EntityItemPointer ownerEntity, - QByteArray data); + virtual EntityActionPointer factoryBA(EntityItemPointer ownerEntity, QByteArray data); }; #endif // hifi_AssignmentActionFactory_h diff --git a/interface/src/InterfaceActionFactory.cpp b/interface/src/InterfaceActionFactory.cpp index 363fb66e76..dca1015ecc 100644 --- a/interface/src/InterfaceActionFactory.cpp +++ b/interface/src/InterfaceActionFactory.cpp @@ -35,8 +35,7 @@ EntityActionPointer interfaceActionFactory(EntityActionType type, const QUuid& i } -EntityActionPointer InterfaceActionFactory::factory(EntitySimulation* simulation, - EntityActionType type, +EntityActionPointer InterfaceActionFactory::factory(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity, QVariantMap arguments) { @@ -51,9 +50,7 @@ EntityActionPointer InterfaceActionFactory::factory(EntitySimulation* simulation } -EntityActionPointer InterfaceActionFactory::factoryBA(EntitySimulation* simulation, - EntityItemPointer ownerEntity, - QByteArray data) { +EntityActionPointer InterfaceActionFactory::factoryBA(EntityItemPointer ownerEntity, QByteArray data) { QDataStream serializedArgumentStream(data); EntityActionType type; QUuid id; diff --git a/interface/src/InterfaceActionFactory.h b/interface/src/InterfaceActionFactory.h index 004c24163f..2031f5c57a 100644 --- a/interface/src/InterfaceActionFactory.h +++ b/interface/src/InterfaceActionFactory.h @@ -18,13 +18,11 @@ class InterfaceActionFactory : public EntityActionFactoryInterface { public: InterfaceActionFactory() : EntityActionFactoryInterface() { } virtual ~InterfaceActionFactory() { } - virtual EntityActionPointer factory(EntitySimulation* simulation, - EntityActionType type, + virtual EntityActionPointer factory(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity, QVariantMap arguments); - virtual EntityActionPointer factoryBA(EntitySimulation* simulation, - EntityItemPointer ownerEntity, + virtual EntityActionPointer factoryBA(EntityItemPointer ownerEntity, QByteArray data); }; diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 72019ecf28..ba37112fe1 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -85,15 +85,17 @@ void AvatarActionHold::updateActionWorker(float deltaTimeStep) { return; } + if (_positionalTarget != position || _rotationalTarget != rotation) { + auto ownerEntity = _ownerEntity.lock(); + if (ownerEntity) { + ownerEntity->setActionDataDirty(true); + } + } + _positionalTarget = position; _rotationalTarget = rotation; unlock(); - auto ownerEntity = _ownerEntity.lock(); - if (ownerEntity) { - ownerEntity->setActionDataDirty(true); - } - ObjectActionSpring::updateActionWorker(deltaTimeStep); } @@ -152,10 +154,6 @@ bool AvatarActionHold::updateArguments(QVariantMap arguments) { _rotationalTargetSet = true; _active = true; unlock(); - auto ownerEntity = _ownerEntity.lock(); - if (ownerEntity) { - ownerEntity->setActionDataDirty(true); - } return true; } diff --git a/libraries/entities/src/EntityActionFactoryInterface.h b/libraries/entities/src/EntityActionFactoryInterface.h index 9f4056cdff..adff1a53ba 100644 --- a/libraries/entities/src/EntityActionFactoryInterface.h +++ b/libraries/entities/src/EntityActionFactoryInterface.h @@ -23,13 +23,11 @@ class EntityActionFactoryInterface : public QObject, public Dependency { public: EntityActionFactoryInterface() { } virtual ~EntityActionFactoryInterface() { } - virtual EntityActionPointer factory(EntitySimulation* simulation, - EntityActionType type, + virtual EntityActionPointer factory(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity, QVariantMap arguments) { assert(false); return nullptr; } - virtual EntityActionPointer factoryBA(EntitySimulation* simulation, - EntityItemPointer ownerEntity, + virtual EntityActionPointer factoryBA(EntityItemPointer ownerEntity, QByteArray data) { assert(false); return nullptr; } }; diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 3aeaf4829f..e408ce3afa 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -1489,20 +1489,22 @@ void EntityItem::clearSimulationOwnership() { } + bool EntityItem::addAction(EntitySimulation* simulation, EntityActionPointer action) { + lockForWrite(); checkWaitingToRemove(simulation); - if (!checkWaitingActionData(simulation)) { - return false; - } bool result = addActionInternal(simulation, action); if (!result) { removeAction(simulation, action->getID()); } + + unlock(); return result; } bool EntityItem::addActionInternal(EntitySimulation* simulation, EntityActionPointer action) { + assertLocked(); assert(action); assert(simulation); auto actionOwnerEntity = action->getOwnerEntity().lock(); @@ -1523,36 +1525,37 @@ bool EntityItem::addActionInternal(EntitySimulation* simulation, EntityActionPoi } bool EntityItem::updateAction(EntitySimulation* simulation, const QUuid& actionID, const QVariantMap& arguments) { + lockForWrite(); checkWaitingToRemove(simulation); - if (!checkWaitingActionData(simulation)) { - return false; - } if (!_objectActions.contains(actionID)) { + unlock(); return false; } EntityActionPointer action = _objectActions[actionID]; - bool success = action->updateArguments(arguments); + bool success = action->updateArguments(arguments); if (success) { _allActionsDataCache = serializeActions(success); } else { qDebug() << "EntityItem::updateAction failed"; } + unlock(); return success; } bool EntityItem::removeAction(EntitySimulation* simulation, const QUuid& actionID) { + lockForWrite(); checkWaitingToRemove(simulation); - if (!checkWaitingActionData(simulation)) { - return false;; - } - return removeActionInternal(actionID); + bool success = removeActionInternal(actionID); + unlock(); + return success; } bool EntityItem::removeActionInternal(const QUuid& actionID, EntitySimulation* simulation) { + assertWriteLocked(); if (_objectActions.contains(actionID)) { if (!simulation) { EntityTree* entityTree = _element ? _element->getTree() : nullptr; @@ -1575,7 +1578,7 @@ bool EntityItem::removeActionInternal(const QUuid& actionID, EntitySimulation* s } bool EntityItem::clearActions(EntitySimulation* simulation) { - _waitingActionData.clear(); + lockForWrite(); QHash::iterator i = _objectActions.begin(); while (i != _objectActions.end()) { const QUuid id = i.key(); @@ -1584,85 +1587,84 @@ bool EntityItem::clearActions(EntitySimulation* simulation) { action->setOwnerEntity(nullptr); action->removeFromSimulation(simulation); } + // empty _serializedActions means no actions for the EntityItem _actionsToRemove.clear(); _allActionsDataCache.clear(); + unlock(); return true; } -bool EntityItem::deserializeActions(QByteArray allActionsData, EntitySimulation* simulation) const { - bool success = true; - QVector serializedActions; - if (allActionsData.size() > 0) { - QDataStream serializedActionsStream(allActionsData); - serializedActionsStream >> serializedActions; + +void EntityItem::deserializeActions() { + assertUnlocked(); + lockForWrite(); + deserializeActionsInternal(); + unlock(); +} + + +void EntityItem::deserializeActionsInternal() { + assertWriteLocked(); + + if (!_element) { + return; } // Keep track of which actions got added or updated by the new actionData - QSet updated; EntityTree* entityTree = _element ? _element->getTree() : nullptr; - if (!simulation) { - simulation = entityTree ? entityTree->getSimulation() : nullptr; + assert(entityTree); + EntitySimulation* simulation = entityTree ? entityTree->getSimulation() : nullptr; + assert(simulation); + + QVector serializedActions; + if (_allActionsDataCache.size() > 0) { + QDataStream serializedActionsStream(_allActionsDataCache); + serializedActionsStream >> serializedActions; } - if (simulation && entityTree) { - foreach(QByteArray serializedAction, serializedActions) { - QDataStream serializedActionStream(serializedAction); - EntityActionType actionType; - QUuid actionID; - serializedActionStream >> actionType; - serializedActionStream >> actionID; - updated << actionID; + QSet updated; - if (_objectActions.contains(actionID)) { - EntityActionPointer action = _objectActions[actionID]; - // TODO: make sure types match? there isn't currently a way to - // change the type of an existing action. - action->deserialize(serializedAction); - } else { - auto actionFactory = DependencyManager::get(); - if (simulation) { - EntityItemPointer entity = entityTree->findEntityByEntityItemID(_id); - EntityActionPointer action = actionFactory->factoryBA(simulation, entity, serializedAction); - if (action) { - entity->addActionInternal(simulation, action); - } - } else { - // we can't yet add the action. This method will be called later. - success = false; - } + foreach(QByteArray serializedAction, serializedActions) { + QDataStream serializedActionStream(serializedAction); + EntityActionType actionType; + QUuid actionID; + serializedActionStream >> actionType; + serializedActionStream >> actionID; + updated << actionID; + + if (_objectActions.contains(actionID)) { + EntityActionPointer action = _objectActions[actionID]; + // TODO: make sure types match? there isn't currently a way to + // change the type of an existing action. + action->deserialize(serializedAction); + } else { + auto actionFactory = DependencyManager::get(); + + // EntityItemPointer entity = entityTree->findEntityByEntityItemID(_id, false); + EntityItemPointer entity = shared_from_this(); + EntityActionPointer action = actionFactory->factoryBA(entity, serializedAction); + if (action) { + entity->addActionInternal(simulation, action); } } + } - // remove any actions that weren't included in the new data. - QHash::const_iterator i = _objectActions.begin(); - while (i != _objectActions.end()) { - const QUuid id = i.key(); - if (!updated.contains(id)) { - _actionsToRemove << id; - } - i++; + // remove any actions that weren't included in the new data. + QHash::const_iterator i = _objectActions.begin(); + while (i != _objectActions.end()) { + const QUuid id = i.key(); + if (!updated.contains(id)) { + _actionsToRemove << id; } - } else { - // no simulation - success = false; + i++; } - return success; -} - -bool EntityItem::checkWaitingActionData(EntitySimulation* simulation) const { - if (_waitingActionData.size() == 0) { - return true; - } - bool success = deserializeActions(_waitingActionData, simulation); - if (success) { - _waitingActionData.clear(); - } - return success; + return; } void EntityItem::checkWaitingToRemove(EntitySimulation* simulation) { + assertLocked(); foreach(QUuid actionID, _actionsToRemove) { removeActionInternal(actionID, simulation); } @@ -1670,21 +1672,22 @@ void EntityItem::checkWaitingToRemove(EntitySimulation* simulation) { } void EntityItem::setActionData(QByteArray actionData) { + assertUnlocked(); + lockForWrite(); + setActionDataInternal(actionData); + unlock(); +} + +void EntityItem::setActionDataInternal(QByteArray actionData) { + assertWriteLocked(); checkWaitingToRemove(); - bool success = deserializeActions(actionData); _allActionsDataCache = actionData; - if (success) { - _waitingActionData.clear(); - } else { - _waitingActionData = actionData; - } + deserializeActionsInternal(); } QByteArray EntityItem::serializeActions(bool& success) const { + assertLocked(); QByteArray result; - if (!checkWaitingActionData()) { - return _waitingActionData; - } if (_objectActions.size() == 0) { success = true; @@ -1713,7 +1716,7 @@ QByteArray EntityItem::serializeActions(bool& success) const { return result; } -const QByteArray EntityItem::getActionData() const { +const QByteArray EntityItem::getActionDataInternal() const { if (_actionDataDirty) { bool success; QByteArray newDataCache = serializeActions(success); @@ -1725,17 +1728,120 @@ const QByteArray EntityItem::getActionData() const { return _allActionsDataCache; } +const QByteArray EntityItem::getActionData() const { + assertUnlocked(); + lockForRead(); + auto result = getActionDataInternal(); + unlock(); + return result; +} + QVariantMap EntityItem::getActionArguments(const QUuid& actionID) const { QVariantMap result; - - if (!checkWaitingActionData()) { - return result; - } + lockForRead(); if (_objectActions.contains(actionID)) { EntityActionPointer action = _objectActions[actionID]; result = action->getArguments(); result["type"] = EntityActionInterface::actionTypeToString(action->getType()); } + unlock(); return result; } + + + +#define ENABLE_LOCKING 1 + +#ifdef ENABLE_LOCKING +void EntityItem::lockForRead() const { + _lock.lockForRead(); +} + +bool EntityItem::tryLockForRead() const { + return _lock.tryLockForRead(); +} + +void EntityItem::lockForWrite() const { + _lock.lockForWrite(); +} + +bool EntityItem::tryLockForWrite() const { + return _lock.tryLockForWrite(); +} + +void EntityItem::unlock() const { + _lock.unlock(); +} + +bool EntityItem::isLocked() const { + bool readSuccess = tryLockForRead(); + if (readSuccess) { + unlock(); + } + bool writeSuccess = tryLockForWrite(); + if (writeSuccess) { + unlock(); + } + if (readSuccess && writeSuccess) { + return false; // if we can take both kinds of lock, there was no previous lock + } + return true; // either read or write failed, so there is some lock in place. +} + + +bool EntityItem::isWriteLocked() const { + bool readSuccess = tryLockForRead(); + if (readSuccess) { + unlock(); + return false; + } + bool writeSuccess = tryLockForWrite(); + if (writeSuccess) { + unlock(); + return false; + } + return true; // either read or write failed, so there is some lock in place. +} + + +bool EntityItem::isUnlocked() const { + // this can't be sure -- this may get unlucky and hit locks from other threads. what we're actually trying + // to discover is if *this* thread hasn't locked the EntityItem. Try repeatedly to take both kinds of lock. + bool readSuccess = false; + for (int i=0; i<80; i++) { + readSuccess = tryLockForRead(); + if (readSuccess) { + unlock(); + break; + } + QThread::usleep(200); + } + + bool writeSuccess = false; + if (readSuccess) { + for (int i=0; i<80; i++) { + writeSuccess = tryLockForWrite(); + if (writeSuccess) { + unlock(); + break; + } + QThread::usleep(300); + } + } + + if (readSuccess && writeSuccess) { + return true; // if we can take both kinds of lock, there was no previous lock + } + return false; +} +#else +void EntityItem::lockForRead() const { } +bool EntityItem::tryLockForRead() const { return true; } +void EntityItem::lockForWrite() const { } +bool EntityItem::tryLockForWrite() const { return true; } +void EntityItem::unlock() const { } +bool EntityItem::isLocked() const { return true; } +bool EntityItem::isWriteLocked() const { return true; } +bool EntityItem::isUnlocked() const { return true; } +#endif diff --git a/libraries/entities/src/EntityItem.h b/libraries/entities/src/EntityItem.h index 7893818267..de431446e8 100644 --- a/libraries/entities/src/EntityItem.h +++ b/libraries/entities/src/EntityItem.h @@ -68,10 +68,28 @@ const float ACTIVATION_ANGULAR_VELOCITY_DELTA = 0.03f; #define debugTimeOnly(T) qPrintable(QString("%1").arg(T, 16, 10)) #define debugTreeVector(V) V << "[" << V << " in meters ]" +#if DEBUG + #define assertLocked() assert(isLocked()) +#else + #define assertLocked() +#endif + +#if DEBUG + #define assertWriteLocked() assert(isWriteLocked()) +#else + #define assertWriteLocked() +#endif + +#if DEBUG + #define assertUnlocked() assert(isUnlocked()) +#else + #define assertUnlocked() +#endif + /// EntityItem class this is the base class for all entity types. It handles the basic properties and functionality available /// to all other entity types. In particular: postion, size, rotation, age, lifetime, velocity, gravity. You can not instantiate /// one directly, instead you must only construct one of it's derived classes with additional features. -class EntityItem { +class EntityItem : public std::enable_shared_from_this { // These two classes manage lists of EntityItem pointers and must be able to cleanup pointers when an EntityItem is deleted. // To make the cleanup robust each EntityItem has backpointers to its manager classes (which are only ever set/cleared by // the managers themselves, hence they are fiends) whose NULL status can be used to determine which managers still need to @@ -395,10 +413,14 @@ public: bool hasActions() { return !_objectActions.empty(); } QList getActionIDs() { return _objectActions.keys(); } QVariantMap getActionArguments(const QUuid& actionID) const; + void deserializeActions(); void setActionDataDirty(bool value) const { _actionDataDirty = value; } protected: + const QByteArray getActionDataInternal() const; + void setActionDataInternal(QByteArray actionData); + static bool _sendPhysicsUpdates; EntityTypes::EntityType _type; QUuid _id; @@ -471,19 +493,28 @@ protected: bool addActionInternal(EntitySimulation* simulation, EntityActionPointer action); bool removeActionInternal(const QUuid& actionID, EntitySimulation* simulation = nullptr); - bool deserializeActions(QByteArray allActionsData, EntitySimulation* simulation = nullptr) const; + void deserializeActionsInternal(); QByteArray serializeActions(bool& success) const; QHash _objectActions; + static int _maxActionsDataSize; mutable QByteArray _allActionsDataCache; // when an entity-server starts up, EntityItem::setActionData is called before the entity-tree is // ready. This means we can't find our EntityItemPointer or add the action to the simulation. These // are used to keep track of and work around this situation. - bool checkWaitingActionData(EntitySimulation* simulation = nullptr) const; void checkWaitingToRemove(EntitySimulation* simulation = nullptr); - mutable QByteArray _waitingActionData; mutable QSet _actionsToRemove; mutable bool _actionDataDirty = false; + + mutable QReadWriteLock _lock; + void lockForRead() const; + bool tryLockForRead() const; + void lockForWrite() const; + bool tryLockForWrite() const; + void unlock() const; + bool isLocked() const; + bool isWriteLocked() const; + bool isUnlocked() const; }; #endif // hifi_EntityItem_h diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index 7cc2c03dfc..f1c6157694 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -574,7 +574,7 @@ QUuid EntityScriptingInterface::addAction(const QString& actionTypeString, if (actionType == ACTION_TYPE_NONE) { return false; } - EntityActionPointer action = actionFactory->factory(simulation, actionType, actionID, entity, arguments); + EntityActionPointer action = actionFactory->factory(actionType, actionID, entity, arguments); if (action) { entity->addAction(simulation, action); auto nodeList = DependencyManager::get(); diff --git a/libraries/entities/src/EntitySimulation.cpp b/libraries/entities/src/EntitySimulation.cpp index a2d20fe5d5..f2bd1e873e 100644 --- a/libraries/entities/src/EntitySimulation.cpp +++ b/libraries/entities/src/EntitySimulation.cpp @@ -146,6 +146,7 @@ void EntitySimulation::sortEntitiesThatMoved() { void EntitySimulation::addEntity(EntityItemPointer entity) { assert(entity); + entity->deserializeActions(); if (entity->isMortal()) { _mortalEntities.insert(entity); quint64 expiry = entity->getExpiry(); From 99c563602f34349614b409e7b96e6663213485d6 Mon Sep 17 00:00:00 2001 From: Bing Shearer Date: Thu, 9 Jul 2015 15:25:37 -0700 Subject: [PATCH 073/107] Fixed Else case where _renderImage is true but the texture has 0 size, else case did not previously exist. --- interface/src/ui/overlays/ImageOverlay.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/interface/src/ui/overlays/ImageOverlay.cpp b/interface/src/ui/overlays/ImageOverlay.cpp index e80c6f584b..7a0c3c00c3 100644 --- a/interface/src/ui/overlays/ImageOverlay.cpp +++ b/interface/src/ui/overlays/ImageOverlay.cpp @@ -75,8 +75,6 @@ void ImageOverlay::render(RenderArgs* args) { glm::vec2 topLeft(left, top); glm::vec2 bottomRight(right, bottom); - - // if for some reason our image is not over 0 width or height, don't attempt to render the image if (_renderImage) { float imageWidth = _texture->getWidth(); @@ -108,16 +106,15 @@ void ImageOverlay::render(RenderArgs* args) { glm::vec2 texCoordBottomRight(x + w, y + h); DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, quadColor); - } - else { + } else { DependencyManager::get()->renderQuad(topLeft, bottomRight, quadColor); } - - if (_renderImage) { - glDisable(GL_TEXTURE_2D); - } + glDisable(GL_TEXTURE_2D); + } else { + DependencyManager::get()->renderQuad(topLeft, bottomRight, quadColor); } } + void ImageOverlay::setProperties(const QScriptValue& properties) { Overlay2D::setProperties(properties); From fa06f50841b138eb5ad74165505342cee4948a4c Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Thu, 9 Jul 2015 15:34:03 -0700 Subject: [PATCH 074/107] fix header comment --- examples/stick-hydra.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/stick-hydra.js b/examples/stick-hydra.js index 42ac6c6cda..a74f7954bb 100644 --- a/examples/stick-hydra.js +++ b/examples/stick-hydra.js @@ -1,10 +1,10 @@ -// stick.js +// stick-hydra.js // examples // -// Created by Seth Alves on 2015-6-10 +// Created by Seth Alves on 2015-7-9 // Copyright 2015 High Fidelity, Inc. // -// Allow avatar to hold a stick +// Allow avatar to hold a stick and control it with a hand-tracker // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html From 446f800aeed371fe3eb9a3c96ce4d814b1a6f002 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Thu, 9 Jul 2015 15:40:43 -0700 Subject: [PATCH 075/107] More profiling ranges and updated performance testing scripts --- examples/animationPerfTest.js | 81 +++++++++++----------- examples/cubePerfTest.js | 58 ++++++++++++++++ interface/src/Application.cpp | 13 ++-- interface/src/ui/ApplicationCompositor.cpp | 2 + interface/src/ui/ApplicationOverlay.cpp | 4 ++ interface/src/ui/overlays/Overlays.cpp | 1 + 6 files changed, 112 insertions(+), 47 deletions(-) create mode 100644 examples/cubePerfTest.js diff --git a/examples/animationPerfTest.js b/examples/animationPerfTest.js index 6bf310db23..b832d2e61f 100644 --- a/examples/animationPerfTest.js +++ b/examples/animationPerfTest.js @@ -9,9 +9,7 @@ var NUM_MOONS = 20; // 1 = 60Hz, 2 = 30Hz, 3 = 20Hz, etc var UPDATE_FREQUENCY_DIVISOR = 2; - var MAX_RANGE = 75.0; -var LIFETIME = 600; var SCALE = 0.1; var center = Vec3.sum(MyAvatar.position, @@ -22,44 +20,47 @@ var PARTICLE_MIN_SIZE = 2.50; var PARTICLE_MAX_SIZE = 2.50; -var planet = Entities.addEntity({ - type: "Sphere", - position: center, - dimensions: { x: 10 * SCALE, y: 10 * SCALE, z: 10 * SCALE }, - color: { red: 0, green: 0, blue: 255 }, - ignoreCollisions: true, - collisionsWillMove: false, - lifetime: LIFETIME -}); +function deleteAnimationTestEntitites() { + var ids = Entities.findEntities(MyAvatar.position, 50); + for (var i = 0; i < ids.length; i++) { + var id = ids[i]; + var properties = Entities.getEntityProperties(id); + if (properties.name == "AnimationTest") { + Entities.deleteEntity(id); + } + } +} + +deleteAnimationTestEntitites(); var moons = []; // Create initial test particles that will move according to gravity from the planets for (var i = 0; i < NUM_MOONS; i++) { - var radius = PARTICLE_MIN_SIZE + Math.random() * PARTICLE_MAX_SIZE; - radius *= SCALE; - var gray = Math.random() * 155; - var position = { x: 10 , y: i * 3, z: 0 }; - var color = { red: 100 + gray, green: 100 + gray, blue: 100 + gray }; - if (i == 0) { - color = { red: 255, green: 0, blue: 0 }; - radius = 6 * SCALE - } - moons.push(Entities.addEntity({ - type: "Sphere", - position: Vec3.sum(center, position), - dimensions: { x: radius, y: radius, z: radius }, - color: color, - ignoreCollisions: true, - lifetime: LIFETIME, - collisionsWillMove: false - })); + var radius = PARTICLE_MIN_SIZE + Math.random() * PARTICLE_MAX_SIZE; + radius *= SCALE; + var gray = Math.random() * 155; + var position = { x: 10 , y: i * 3, z: 0 }; + var color = { red: 100 + gray, green: 100 + gray, blue: 100 + gray }; + if (i == 0) { + color = { red: 255, green: 0, blue: 0 }; + radius = 6 * SCALE + } + moons.push(Entities.addEntity({ + type: "Sphere", + name: "AnimationTest", + position: Vec3.sum(center, position), + dimensions: { x: radius, y: radius, z: radius }, + color: color, + ignoreCollisions: true, + collisionsWillMove: false + + })); } Script.update.connect(update); function scriptEnding() { - Entities.deleteEntity(planet); for (var i = 0; i < moons.length; i++) { Entities.deleteEntity(moons[i]); } @@ -70,22 +71,20 @@ var updateCount = 0; function update(deltaTime) { // Apply gravitational force from planets totalTime += deltaTime; - updateCount++; - if (0 != updateCount % UPDATE_FREQUENCY_DIVISOR) { - return; - } - - var planetProperties = Entities.getEntityProperties(planet); - var center = planetProperties.position; + updateCount++; + if (0 != updateCount % UPDATE_FREQUENCY_DIVISOR) { + return; + } + var particlePos = Entities.getEntityProperties(moons[0]).position; var relativePos = Vec3.subtract(particlePos.position, center); for (var t = 0; t < moons.length; t++) { - var thetaDelta = (Math.PI * 2.0 / NUM_MOONS) * t; - var y = Math.sin(totalTime + thetaDelta) * 10.0 * SCALE; - var x = Math.cos(totalTime + thetaDelta) * 10.0 * SCALE; + var thetaDelta = (Math.PI * 2.0 / NUM_MOONS) * t; + var y = Math.sin(totalTime + thetaDelta) * 10.0 * SCALE; + var x = Math.cos(totalTime + thetaDelta) * 10.0 * SCALE; var newBasePos = Vec3.sum({ x: 0, y: y, z: x }, center); Entities.editEntity(moons[t], { position: newBasePos}); } } -Script.scriptEnding.connect(scriptEnding); +Script.scriptEnding.connect(deleteAnimationTestEntitites); diff --git a/examples/cubePerfTest.js b/examples/cubePerfTest.js new file mode 100644 index 0000000000..e349b7add7 --- /dev/null +++ b/examples/cubePerfTest.js @@ -0,0 +1,58 @@ +// +// Created by Bradley Austin Davis on 2015/07/01 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +var SIDE_SIZE = 10; + +var center = { x: 0, y: 0, z: 0 }; + +var DEGREES_TO_RADIANS = Math.PI / 180.0; +var PARTICLE_MIN_SIZE = 2.50; +var PARTICLE_MAX_SIZE = 2.50; +var LIFETIME = 600; +var boxes = []; + +var ids = Entities.findEntities({ x: 512, y: 512, z: 512 }, 50); +for (var i = 0; i < ids.length; i++) { + var id = ids[i]; + var properties = Entities.getEntityProperties(id); + if (properties.name == "PerfTest") { + Entities.deleteEntity(id); + } +} + + +// Create initial test particles that will move according to gravity from the planets +for (var x = 0; x < SIDE_SIZE; x++) { + for (var y = 0; y < SIDE_SIZE; y++) { + for (var z = 0; z < SIDE_SIZE; z++) { + var gray = Math.random() * 155; + var cube = Math.random() > 0.5; + var color = { red: 100 + gray, green: 100 + gray, blue: 100 + gray }; + var position = { x: 512 + x * 0.2, y: 512 + y * 0.2, z: 512 + z * 0.2}; + var radius = Math.random() * 0.1; + boxes.push(Entities.addEntity({ + type: cube ? "Box" : "Sphere", + name: "PerfTest", + position: position, + dimensions: { x: radius, y: radius, z: radius }, + color: color, + ignoreCollisions: true, + collisionsWillMove: false, + lifetime: LIFETIME + })); + } + } +} + + +function scriptEnding() { + for (var i = 0; i < boxes.length; i++) { + //Entities.deleteEntity(boxes[i]); + } +} +Script.scriptEnding.connect(scriptEnding); diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index a30df36159..21acadd3be 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -887,12 +887,6 @@ void Application::paintGL() { { PerformanceTimer perfTimer("renderOverlay"); - /* - gpu::Context context(new gpu::GLBackend()); - RenderArgs renderArgs(&context, nullptr, getViewFrustum(), lodManager->getOctreeSizeScale(), - lodManager->getBoundaryLevelAdjust(), RenderArgs::DEFAULT_RENDER_MODE, - RenderArgs::MONO, RenderArgs::RENDER_DEBUG_NONE); - */ _applicationOverlay.renderOverlay(&renderArgs); } @@ -968,6 +962,8 @@ void Application::paintGL() { } else if (TV3DManager::isConnected()) { TV3DManager::display(&renderArgs, _myCamera); } else { + PROFILE_RANGE(__function__ "/mainRender"); + DependencyManager::get()->prepare(&renderArgs); // Viewport is assigned to the size of the framebuffer @@ -1002,6 +998,7 @@ void Application::paintGL() { if (!OculusManager::isConnected() || OculusManager::allowSwap()) { + PROFILE_RANGE(__FUNCTION__ "/bufferSwap"); _glWidget->swapBuffers(); } @@ -1051,6 +1048,7 @@ void Application::resetCameras(Camera& camera, const glm::uvec2& size) { } void Application::resizeGL() { + PROFILE_RANGE(__FUNCTION__); // Set the desired FBO texture size. If it hasn't changed, this does nothing. // Otherwise, it must rebuild the FBOs QSize renderSize; @@ -1526,6 +1524,7 @@ void Application::focusOutEvent(QFocusEvent* event) { } void Application::mouseMoveEvent(QMouseEvent* event, unsigned int deviceID) { + PROFILE_RANGE(__FUNCTION__); // Used by application overlay to determine how to draw cursor(s) _lastMouseMoveWasSimulated = deviceID > 0; if (!_lastMouseMoveWasSimulated) { @@ -1829,6 +1828,7 @@ void Application::idle() { PerformanceTimer perfTimer("update"); PerformanceWarning warn(showWarnings, "Application::idle()... update()"); const float BIGGEST_DELTA_TIME_SECS = 0.25f; + PROFILE_RANGE(__FUNCTION__ "/idleUpdate"); update(glm::clamp((float)timeSinceLastUpdate / 1000.0f, 0.0f, BIGGEST_DELTA_TIME_SECS)); } { @@ -2954,6 +2954,7 @@ QRect Application::getDesirableApplicationGeometry() { // or the "myCamera". // void Application::loadViewFrustum(Camera& camera, ViewFrustum& viewFrustum) { + PROFILE_RANGE(__FUNCTION__); // We will use these below, from either the camera or head vectors calculated above viewFrustum.setProjection(camera.getProjection()); diff --git a/interface/src/ui/ApplicationCompositor.cpp b/interface/src/ui/ApplicationCompositor.cpp index dafa332d53..2c1c800f94 100644 --- a/interface/src/ui/ApplicationCompositor.cpp +++ b/interface/src/ui/ApplicationCompositor.cpp @@ -184,6 +184,7 @@ void ApplicationCompositor::bindCursorTexture(gpu::Batch& batch, uint8_t cursorI // Draws the FBO texture for the screen void ApplicationCompositor::displayOverlayTexture(RenderArgs* renderArgs) { + PROFILE_RANGE(__FUNCTION__); if (_alpha == 0.0f) { return; } @@ -252,6 +253,7 @@ vec2 getPolarCoordinates(const PalmData& palm) { // Draws the FBO texture for Oculus rift. void ApplicationCompositor::displayOverlayTextureHmd(RenderArgs* renderArgs, int eye) { + PROFILE_RANGE(__FUNCTION__); if (_alpha == 0.0f) { return; } diff --git a/interface/src/ui/ApplicationOverlay.cpp b/interface/src/ui/ApplicationOverlay.cpp index e7d220893f..cadd78942c 100644 --- a/interface/src/ui/ApplicationOverlay.cpp +++ b/interface/src/ui/ApplicationOverlay.cpp @@ -74,6 +74,7 @@ ApplicationOverlay::~ApplicationOverlay() { // Renders the overlays either to a texture or to the screen void ApplicationOverlay::renderOverlay(RenderArgs* renderArgs) { + PROFILE_RANGE(__FUNCTION__); CHECK_GL_ERROR(); PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), "ApplicationOverlay::displayOverlay()"); @@ -98,6 +99,7 @@ void ApplicationOverlay::renderOverlay(RenderArgs* renderArgs) { } void ApplicationOverlay::renderQmlUi(RenderArgs* renderArgs) { + PROFILE_RANGE(__FUNCTION__); if (_uiTexture) { gpu::Batch batch; auto geometryCache = DependencyManager::get(); @@ -112,6 +114,7 @@ void ApplicationOverlay::renderQmlUi(RenderArgs* renderArgs) { } void ApplicationOverlay::renderOverlays(RenderArgs* renderArgs) { + PROFILE_RANGE(__FUNCTION__); glm::vec2 size = qApp->getCanvasSize(); mat4 legacyProjection = glm::ortho(0, size.x, size.y, 0, ORTHO_NEAR_CLIP, ORTHO_FAR_CLIP); @@ -219,6 +222,7 @@ GLuint ApplicationOverlay::getOverlayTexture() { } void ApplicationOverlay::buildFramebufferObject() { + PROFILE_RANGE(__FUNCTION__); QSize fboSize = qApp->getDeviceSize(); if (_overlayFramebuffer && fboSize == _overlayFramebuffer->size()) { // Already built diff --git a/interface/src/ui/overlays/Overlays.cpp b/interface/src/ui/overlays/Overlays.cpp index fb0a095e13..db1bc2185a 100644 --- a/interface/src/ui/overlays/Overlays.cpp +++ b/interface/src/ui/overlays/Overlays.cpp @@ -96,6 +96,7 @@ void Overlays::cleanupOverlaysToDelete() { } void Overlays::renderHUD(RenderArgs* renderArgs) { + PROFILE_RANGE(__FUNCTION__); QReadLocker lock(&_lock); gpu::Batch batch; renderArgs->_batch = &batch; From 408ecb9735870f0788c9f447750322f06597e6c2 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 9 Jul 2015 15:59:32 -0700 Subject: [PATCH 076/107] move domain server check in timer to node thread --- interface/src/Application.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index c1397beb28..07ddfe1377 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -394,6 +394,11 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : static_cast(dependency)->deleteLater(); }); + // setup a timer for domain-server check ins + QTimer* domainCheckInTimer = new QTimer(nodeList); + connect(domainCheckInTimer, &QTimer::timeout, nodeList, &NodeList::sendDomainServerCheckIn); + domainCheckInTimer->start(DOMAIN_SERVER_CHECK_IN_MSECS); + // put the NodeList and datagram processing on the node thread nodeList->moveToThread(nodeThread); @@ -1781,9 +1786,6 @@ void Application::checkFPS() { _frameCount = 0; _datagramProcessor->resetCounters(); _timerStart.start(); - - // ask the node list to check in with the domain server - DependencyManager::get()->sendDomainServerCheckIn(); } void Application::idle() { From a4f1145f067376681a9aab75fe48635f529d5016 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 9 Jul 2015 16:01:41 -0700 Subject: [PATCH 077/107] fix QTimer connection to nodeList --- interface/src/Application.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 07ddfe1377..48af04b031 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -396,7 +396,7 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : // setup a timer for domain-server check ins QTimer* domainCheckInTimer = new QTimer(nodeList); - connect(domainCheckInTimer, &QTimer::timeout, nodeList, &NodeList::sendDomainServerCheckIn); + connect(domainCheckInTimer, &QTimer::timeout, nodeList.data(), &NodeList::sendDomainServerCheckIn); domainCheckInTimer->start(DOMAIN_SERVER_CHECK_IN_MSECS); // put the NodeList and datagram processing on the node thread @@ -987,7 +987,7 @@ void Application::paintGL() { renderArgs._renderMode = RenderArgs::MIRROR_RENDER_MODE; if (Menu::getInstance()->isOptionChecked(MenuOption::Mirror)) { - renderRearViewMirror(&renderArgs, _mirrorViewRect); + renderRearViewMirror(&renderArgs, _mirrorViewRect); } renderArgs._renderMode = RenderArgs::NORMAL_RENDER_MODE; @@ -1853,7 +1853,7 @@ void Application::idle() { // After finishing all of the above work, ensure the idle timer is set to the proper interval, // depending on whether we're throttling or not idleTimer->start(_glWidget->isThrottleRendering() ? THROTTLED_IDLE_TIMER_DELAY : 0); - } + } // check for any requested background downloads. emit checkBackgroundDownloads(); @@ -3308,7 +3308,7 @@ namespace render { const float APPROXIMATE_DISTANCE_FROM_HORIZON = 0.1f; const float DOUBLE_APPROXIMATE_DISTANCE_FROM_HORIZON = 0.2f; - glm::vec3 sunDirection = (args->_viewFrustum->getPosition()/*getAvatarPosition()*/ - closestData.getSunLocation()) + glm::vec3 sunDirection = (args->_viewFrustum->getPosition()/*getAvatarPosition()*/ - closestData.getSunLocation()) / closestData.getAtmosphereOuterRadius(); float height = glm::distance(args->_viewFrustum->getPosition()/*theCamera.getPosition()*/, closestData.getAtmosphereCenter()); if (height < closestData.getAtmosphereInnerRadius()) { @@ -3316,8 +3316,8 @@ namespace render { alpha = 0.0f; if (sunDirection.y > -APPROXIMATE_DISTANCE_FROM_HORIZON) { - float directionY = glm::clamp(sunDirection.y, - -APPROXIMATE_DISTANCE_FROM_HORIZON, APPROXIMATE_DISTANCE_FROM_HORIZON) + float directionY = glm::clamp(sunDirection.y, + -APPROXIMATE_DISTANCE_FROM_HORIZON, APPROXIMATE_DISTANCE_FROM_HORIZON) + APPROXIMATE_DISTANCE_FROM_HORIZON; alpha = (directionY / DOUBLE_APPROXIMATE_DISTANCE_FROM_HORIZON); } @@ -3328,8 +3328,8 @@ namespace render { (closestData.getAtmosphereOuterRadius() - closestData.getAtmosphereInnerRadius()); if (sunDirection.y > -APPROXIMATE_DISTANCE_FROM_HORIZON) { - float directionY = glm::clamp(sunDirection.y, - -APPROXIMATE_DISTANCE_FROM_HORIZON, APPROXIMATE_DISTANCE_FROM_HORIZON) + float directionY = glm::clamp(sunDirection.y, + -APPROXIMATE_DISTANCE_FROM_HORIZON, APPROXIMATE_DISTANCE_FROM_HORIZON) + APPROXIMATE_DISTANCE_FROM_HORIZON; alpha = (directionY / DOUBLE_APPROXIMATE_DISTANCE_FROM_HORIZON); } @@ -3506,8 +3506,8 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se pendingChanges.resetItem(WorldBoxRenderData::_item, worldBoxRenderPayload); } else { - pendingChanges.updateItem(WorldBoxRenderData::_item, - [](WorldBoxRenderData& payload) { + pendingChanges.updateItem(WorldBoxRenderData::_item, + [](WorldBoxRenderData& payload) { payload._val++; }); } @@ -3526,7 +3526,7 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se } { - PerformanceTimer perfTimer("SceneProcessPendingChanges"); + PerformanceTimer perfTimer("SceneProcessPendingChanges"); _main3DScene->enqueuePendingChanges(pendingChanges); _main3DScene->processPendingChangesQueue(); @@ -4835,7 +4835,7 @@ qreal Application::getDevicePixelRatio() { mat4 Application::getEyeProjection(int eye) const { if (isHMDMode()) { return OculusManager::getEyeProjection(eye); - } + } return _viewFrustum.getProjection(); } From efb89c281f0b771c92948ea4a2b47853d56d3f03 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Thu, 9 Jul 2015 16:02:29 -0700 Subject: [PATCH 078/107] fix creation of QTimer parented by NodeList --- interface/src/Application.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 48af04b031..e662263958 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -395,7 +395,7 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : }); // setup a timer for domain-server check ins - QTimer* domainCheckInTimer = new QTimer(nodeList); + QTimer* domainCheckInTimer = new QTimer(nodeList.data()); connect(domainCheckInTimer, &QTimer::timeout, nodeList.data(), &NodeList::sendDomainServerCheckIn); domainCheckInTimer->start(DOMAIN_SERVER_CHECK_IN_MSECS); From 214405a0e5012bc68ef895e0566413bb60e54842 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Thu, 9 Jul 2015 16:06:27 -0700 Subject: [PATCH 079/107] Fixing typo --- interface/src/Application.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 21acadd3be..69e8710333 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -962,7 +962,7 @@ void Application::paintGL() { } else if (TV3DManager::isConnected()) { TV3DManager::display(&renderArgs, _myCamera); } else { - PROFILE_RANGE(__function__ "/mainRender"); + PROFILE_RANGE(__FUNCTION__ "/mainRender"); DependencyManager::get()->prepare(&renderArgs); From 1dce5f80cc68501ea8c72f99ce4450e2a856dfc2 Mon Sep 17 00:00:00 2001 From: Niraj Venkat Date: Thu, 9 Jul 2015 16:17:58 -0700 Subject: [PATCH 080/107] Entities are now added/removed from rendering based on Scene.shouldRenderEntities --- interface/src/Application.cpp | 20 ++++++++++++++----- .../src/EntityTreeRenderer.cpp | 19 ++++++++++++++++++ .../src/EntityTreeRenderer.h | 6 ++++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index a30df36159..fb1aa737a7 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1014,7 +1014,6 @@ void Application::paintGL() { void Application::runTests() { runTimingTests(); - runUnitTests(); } void Application::audioMuteToggled() { @@ -1183,6 +1182,7 @@ bool Application::event(QEvent* event) { } bool Application::eventFilter(QObject* object, QEvent* event) { + if (event->type() == QEvent::ShortcutOverride) { if (DependencyManager::get()->shouldSwallowShortcut(event)) { event->accept(); @@ -1787,7 +1787,6 @@ void Application::checkFPS() { } void Application::idle() { - PROFILE_RANGE(__FUNCTION__); static SimpleAverage interIdleDurations; static uint64_t lastIdleEnd{ 0 }; @@ -3387,6 +3386,14 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se // load the view frustum loadViewFrustum(theCamera, _displayViewFrustum); + // flip x if in mirror mode (also requires reversing winding order for backface culling) + if (theCamera.getMode() == CAMERA_MODE_MIRROR) { + //glScalef(-1.0f, 1.0f, 1.0f); + //glFrontFace(GL_CW); + } else { + glFrontFace(GL_CCW); + } + // transform view according to theCamera // could be myCamera (if in normal mode) // or could be viewFrustumOffsetCamera if in offset mode @@ -3404,6 +3411,9 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se Transform viewTransform; viewTransform.setTranslation(theCamera.getPosition()); viewTransform.setRotation(rotation); + if (theCamera.getMode() == CAMERA_MODE_MIRROR) { +// viewTransform.setScale(Transform::Vec3(-1.0f, 1.0f, 1.0f)); + } if (renderArgs->_renderSide != RenderArgs::MONO) { glm::mat4 invView = glm::inverse(_untranslatedViewMatrix); @@ -3458,9 +3468,9 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se // Assuming nothing get's rendered through that if (!selfAvatarOnly) { - + auto var = DependencyManager::get()->shouldRenderEntities(); // render models... - if (DependencyManager::get()->shouldRenderEntities()) { + //if (DependencyManager::get()->shouldRenderEntities()) { PerformanceTimer perfTimer("entities"); PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), "Application::displaySide() ... entities..."); @@ -3476,7 +3486,7 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se } renderArgs->_debugFlags = renderDebugFlags; _entities.render(renderArgs); - } + //} // render the ambient occlusion effect if enabled if (Menu::getInstance()->isOptionChecked(MenuOption::AmbientOcclusion)) { diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.cpp b/libraries/entities-renderer/src/EntityTreeRenderer.cpp index 03d88200c5..58068e7557 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.cpp +++ b/libraries/entities-renderer/src/EntityTreeRenderer.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include @@ -802,6 +803,8 @@ void EntityTreeRenderer::connectSignalsToSlots(EntityScriptingInterface* entityS connect(this, &EntityTreeRenderer::enterEntity, entityScriptingInterface, &EntityScriptingInterface::enterEntity); connect(this, &EntityTreeRenderer::leaveEntity, entityScriptingInterface, &EntityScriptingInterface::leaveEntity); connect(this, &EntityTreeRenderer::collisionWithEntity, entityScriptingInterface, &EntityScriptingInterface::collisionWithEntity); + + connect(&(*DependencyManager::get()), &SceneScriptingInterface::shouldRenderEntitiesChanged, this, &EntityTreeRenderer::updateEntityTree, Qt::QueuedConnection); } QScriptValueList EntityTreeRenderer::createMouseEventArgs(const EntityItemID& entityID, QMouseEvent* event, unsigned int deviceID) { @@ -1152,3 +1155,19 @@ void EntityTreeRenderer::entityCollisionWithEntity(const EntityItemID& idA, cons entityScriptB.property("collisionWithEntity").call(entityScriptA, args); } } + +void EntityTreeRenderer::updateEntityTree(bool shouldRenderEntities) { + if (DependencyManager::get()->shouldRenderEntities()) { + qDebug() << "SHOULD RENDER ENTITIES NOW"; + for (auto entityID : _entityIDsLastInScene) { + addingEntity(entityID); + } + _entityIDsLastInScene.clear(); + } else { + qDebug() << "SHOULD NOT RENDER ENTITIES"; + _entityIDsLastInScene = _entitiesInScene.keys(); + for (auto entityID : _entityIDsLastInScene) { + deletingEntity(entityID); + } + } +} diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.h b/libraries/entities-renderer/src/EntityTreeRenderer.h index bd6044516f..aae9c63d9a 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.h +++ b/libraries/entities-renderer/src/EntityTreeRenderer.h @@ -90,6 +90,9 @@ public: virtual void scriptContentsAvailable(const QUrl& url, const QString& scriptContents); virtual void errorInLoadingScript(const QUrl& url); + // For Scene.shouldRenderEntities + QList& getEntitiesLastInScene() { return _entityIDsLastInScene; } + signals: void mousePressOnEntity(const RayToEntityIntersectionResult& entityItemID, const QMouseEvent* event, unsigned int deviceId); void mouseMoveOnEntity(const RayToEntityIntersectionResult& entityItemID, const QMouseEvent* event, unsigned int deviceId); @@ -112,6 +115,7 @@ public slots: void deletingEntity(const EntityItemID& entityID); void entitySciptChanging(const EntityItemID& entityID, const bool reload); void entityCollisionWithEntity(const EntityItemID& idA, const EntityItemID& idB, const Collision& collision); + void updateEntityTree(bool shouldRenderEntities); // optional slots that can be wired to menu items void setDisplayElementChildProxies(bool value) { _displayElementChildProxies = value; } @@ -188,6 +192,8 @@ private: int _previousStageDay; QHash _entitiesInScene; + // For Scene.shouldRenderEntities + QList _entityIDsLastInScene; }; From 52095fd38d9e8dfbdc1c853f4129f4011a2cc3d8 Mon Sep 17 00:00:00 2001 From: Niraj Venkat Date: Thu, 9 Jul 2015 16:23:40 -0700 Subject: [PATCH 081/107] Fixing merge --- interface/src/Application.cpp | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index fb1aa737a7..0ee65b56f4 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -1014,6 +1014,7 @@ void Application::paintGL() { void Application::runTests() { runTimingTests(); + runUnitTests(); } void Application::audioMuteToggled() { @@ -1182,7 +1183,6 @@ bool Application::event(QEvent* event) { } bool Application::eventFilter(QObject* object, QEvent* event) { - if (event->type() == QEvent::ShortcutOverride) { if (DependencyManager::get()->shouldSwallowShortcut(event)) { event->accept(); @@ -1787,6 +1787,7 @@ void Application::checkFPS() { } void Application::idle() { + PROFILE_RANGE(__FUNCTION__); static SimpleAverage interIdleDurations; static uint64_t lastIdleEnd{ 0 }; @@ -3386,14 +3387,6 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se // load the view frustum loadViewFrustum(theCamera, _displayViewFrustum); - // flip x if in mirror mode (also requires reversing winding order for backface culling) - if (theCamera.getMode() == CAMERA_MODE_MIRROR) { - //glScalef(-1.0f, 1.0f, 1.0f); - //glFrontFace(GL_CW); - } else { - glFrontFace(GL_CCW); - } - // transform view according to theCamera // could be myCamera (if in normal mode) // or could be viewFrustumOffsetCamera if in offset mode @@ -3411,9 +3404,6 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se Transform viewTransform; viewTransform.setTranslation(theCamera.getPosition()); viewTransform.setRotation(rotation); - if (theCamera.getMode() == CAMERA_MODE_MIRROR) { -// viewTransform.setScale(Transform::Vec3(-1.0f, 1.0f, 1.0f)); - } if (renderArgs->_renderSide != RenderArgs::MONO) { glm::mat4 invView = glm::inverse(_untranslatedViewMatrix); From adf5b95835df4a65ffc6c1da9c3d81820ccbfa5c Mon Sep 17 00:00:00 2001 From: Niraj Venkat Date: Thu, 9 Jul 2015 16:25:29 -0700 Subject: [PATCH 082/107] Changing formatting --- interface/src/Application.cpp | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 0ee65b56f4..9482ed8586 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3460,23 +3460,21 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se if (!selfAvatarOnly) { auto var = DependencyManager::get()->shouldRenderEntities(); // render models... - //if (DependencyManager::get()->shouldRenderEntities()) { - PerformanceTimer perfTimer("entities"); - PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), - "Application::displaySide() ... entities..."); + PerformanceTimer perfTimer("entities"); + PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), + "Application::displaySide() ... entities..."); - RenderArgs::DebugFlags renderDebugFlags = RenderArgs::RENDER_DEBUG_NONE; + RenderArgs::DebugFlags renderDebugFlags = RenderArgs::RENDER_DEBUG_NONE; - if (Menu::getInstance()->isOptionChecked(MenuOption::PhysicsShowHulls)) { - renderDebugFlags = (RenderArgs::DebugFlags) (renderDebugFlags | (int) RenderArgs::RENDER_DEBUG_HULLS); - } - if (Menu::getInstance()->isOptionChecked(MenuOption::PhysicsShowOwned)) { - renderDebugFlags = - (RenderArgs::DebugFlags) (renderDebugFlags | (int) RenderArgs::RENDER_DEBUG_SIMULATION_OWNERSHIP); - } - renderArgs->_debugFlags = renderDebugFlags; - _entities.render(renderArgs); - //} + if (Menu::getInstance()->isOptionChecked(MenuOption::PhysicsShowHulls)) { + renderDebugFlags = (RenderArgs::DebugFlags) (renderDebugFlags | (int) RenderArgs::RENDER_DEBUG_HULLS); + } + if (Menu::getInstance()->isOptionChecked(MenuOption::PhysicsShowOwned)) { + renderDebugFlags = + (RenderArgs::DebugFlags) (renderDebugFlags | (int) RenderArgs::RENDER_DEBUG_SIMULATION_OWNERSHIP); + } + renderArgs->_debugFlags = renderDebugFlags; + _entities.render(renderArgs); // render the ambient occlusion effect if enabled if (Menu::getInstance()->isOptionChecked(MenuOption::AmbientOcclusion)) { From bd91daf712c9e4893d59db4785fe97890a978a10 Mon Sep 17 00:00:00 2001 From: Niraj Venkat Date: Thu, 9 Jul 2015 16:27:23 -0700 Subject: [PATCH 083/107] Removing debug code --- libraries/entities-renderer/src/EntityTreeRenderer.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.cpp b/libraries/entities-renderer/src/EntityTreeRenderer.cpp index 58068e7557..a2f6bc3289 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.cpp +++ b/libraries/entities-renderer/src/EntityTreeRenderer.cpp @@ -16,7 +16,6 @@ #include #include -#include #include #include @@ -1158,13 +1157,11 @@ void EntityTreeRenderer::entityCollisionWithEntity(const EntityItemID& idA, cons void EntityTreeRenderer::updateEntityTree(bool shouldRenderEntities) { if (DependencyManager::get()->shouldRenderEntities()) { - qDebug() << "SHOULD RENDER ENTITIES NOW"; for (auto entityID : _entityIDsLastInScene) { addingEntity(entityID); } _entityIDsLastInScene.clear(); } else { - qDebug() << "SHOULD NOT RENDER ENTITIES"; _entityIDsLastInScene = _entitiesInScene.keys(); for (auto entityID : _entityIDsLastInScene) { deletingEntity(entityID); From bf6fce4e454d03720f1faf0858d4307a744594ce Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Thu, 9 Jul 2015 17:05:15 -0700 Subject: [PATCH 084/107] remove unneeded const --- libraries/entities/src/EntityItem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index e408ce3afa..794d0752a1 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -1653,7 +1653,7 @@ void EntityItem::deserializeActionsInternal() { // remove any actions that weren't included in the new data. QHash::const_iterator i = _objectActions.begin(); while (i != _objectActions.end()) { - const QUuid id = i.key(); + QUuid id = i.key(); if (!updated.contains(id)) { _actionsToRemove << id; } From 706f85e4ce81985752d71950dadb9f2daa959d06 Mon Sep 17 00:00:00 2001 From: Niraj Venkat Date: Thu, 9 Jul 2015 17:31:38 -0700 Subject: [PATCH 085/107] Fixes to comply with coding standard --- interface/src/Application.cpp | 1 - libraries/entities-renderer/src/EntityTreeRenderer.cpp | 4 ++-- libraries/entities-renderer/src/EntityTreeRenderer.h | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 9482ed8586..91baa8c8b5 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3458,7 +3458,6 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se // Assuming nothing get's rendered through that if (!selfAvatarOnly) { - auto var = DependencyManager::get()->shouldRenderEntities(); // render models... PerformanceTimer perfTimer("entities"); PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.cpp b/libraries/entities-renderer/src/EntityTreeRenderer.cpp index a2f6bc3289..11d24c6d9d 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.cpp +++ b/libraries/entities-renderer/src/EntityTreeRenderer.cpp @@ -803,7 +803,7 @@ void EntityTreeRenderer::connectSignalsToSlots(EntityScriptingInterface* entityS connect(this, &EntityTreeRenderer::leaveEntity, entityScriptingInterface, &EntityScriptingInterface::leaveEntity); connect(this, &EntityTreeRenderer::collisionWithEntity, entityScriptingInterface, &EntityScriptingInterface::collisionWithEntity); - connect(&(*DependencyManager::get()), &SceneScriptingInterface::shouldRenderEntitiesChanged, this, &EntityTreeRenderer::updateEntityTree, Qt::QueuedConnection); + connect(&(*DependencyManager::get()), &SceneScriptingInterface::shouldRenderEntitiesChanged, this, &EntityTreeRenderer::updateEntityRenderStatus, Qt::QueuedConnection); } QScriptValueList EntityTreeRenderer::createMouseEventArgs(const EntityItemID& entityID, QMouseEvent* event, unsigned int deviceID) { @@ -1155,7 +1155,7 @@ void EntityTreeRenderer::entityCollisionWithEntity(const EntityItemID& idA, cons } } -void EntityTreeRenderer::updateEntityTree(bool shouldRenderEntities) { +void EntityTreeRenderer::updateEntityRenderStatus(bool shouldRenderEntities) { if (DependencyManager::get()->shouldRenderEntities()) { for (auto entityID : _entityIDsLastInScene) { addingEntity(entityID); diff --git a/libraries/entities-renderer/src/EntityTreeRenderer.h b/libraries/entities-renderer/src/EntityTreeRenderer.h index aae9c63d9a..28a6a3172c 100644 --- a/libraries/entities-renderer/src/EntityTreeRenderer.h +++ b/libraries/entities-renderer/src/EntityTreeRenderer.h @@ -115,7 +115,7 @@ public slots: void deletingEntity(const EntityItemID& entityID); void entitySciptChanging(const EntityItemID& entityID, const bool reload); void entityCollisionWithEntity(const EntityItemID& idA, const EntityItemID& idB, const Collision& collision); - void updateEntityTree(bool shouldRenderEntities); + void updateEntityRenderStatus(bool shouldRenderEntities); // optional slots that can be wired to menu items void setDisplayElementChildProxies(bool value) { _displayElementChildProxies = value; } From 11f2d740308df31ed56ccd2c2d36771e6b1268bc Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Thu, 9 Jul 2015 17:33:25 -0700 Subject: [PATCH 086/107] Simple cleaning to remove dependancies on the viewStateInterface in the deferredLightingEffect --- .../render-utils/src/DeferredLightingEffect.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index a721e0cad3..9bda5b8048 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -9,7 +9,6 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -// include this before QOpenGLFramebufferObject, which includes an earlier version of OpenGL #include #include @@ -24,7 +23,7 @@ #include "TextureCache.h" #include "gpu/Batch.h" -#include "gpu/GLBackend.h" +#include "gpu/Context.h" #include "gpu/StandardShaderLib.h" #include "simple_vert.h" @@ -269,7 +268,7 @@ void DeferredLightingEffect::render(RenderArgs* args) { // Fetch the ViewMatrix; glm::mat4 invViewMat; - _viewState->getViewTransform().getMatrix(invViewMat); + invViewMat = args->_viewFrustum->getView(); auto& program = _directionalLight; const LightLocations* locations = &_directionalLightLocations; @@ -344,7 +343,8 @@ void DeferredLightingEffect::render(RenderArgs* args) { float left, right, bottom, top, nearVal, farVal; glm::vec4 nearClipPlane, farClipPlane; - _viewState->computeOffAxisFrustum(left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane); + args->_viewFrustum->computeOffAxisFrustum(left, right, bottom, top, nearVal, farVal, nearClipPlane, farClipPlane); + batch._glUniform1f(locations->nearLocation, nearVal); float depthScale = (farVal - nearVal) / farVal; @@ -394,9 +394,9 @@ void DeferredLightingEffect::render(RenderArgs* args) { // enlarge the scales slightly to account for tesselation const float SCALE_EXPANSION = 0.05f; - - const glm::vec3& eyePoint = _viewState->getCurrentViewFrustum()->getPosition(); - float nearRadius = glm::distance(eyePoint, _viewState->getCurrentViewFrustum()->getNearTopLeft()); + + auto eyePoint = args->_viewFrustum->getPosition(); + float nearRadius = glm::distance(eyePoint, args->_viewFrustum->getNearTopLeft()); auto geometryCache = DependencyManager::get(); From 007d662db1dcf6fdb6721b0160e9819baf395772 Mon Sep 17 00:00:00 2001 From: Niraj Venkat Date: Thu, 9 Jul 2015 17:40:14 -0700 Subject: [PATCH 087/107] Re-added for shouldRenderEntities --- interface/src/Application.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 91baa8c8b5..5188e19c1a 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3458,22 +3458,24 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se // Assuming nothing get's rendered through that if (!selfAvatarOnly) { - // render models... - PerformanceTimer perfTimer("entities"); - PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), - "Application::displaySide() ... entities..."); + if (DependencyManager::get()->shouldRenderEntities()) { + // render models... + PerformanceTimer perfTimer("entities"); + PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), + "Application::displaySide() ... entities..."); - RenderArgs::DebugFlags renderDebugFlags = RenderArgs::RENDER_DEBUG_NONE; + RenderArgs::DebugFlags renderDebugFlags = RenderArgs::RENDER_DEBUG_NONE; - if (Menu::getInstance()->isOptionChecked(MenuOption::PhysicsShowHulls)) { - renderDebugFlags = (RenderArgs::DebugFlags) (renderDebugFlags | (int) RenderArgs::RENDER_DEBUG_HULLS); + if (Menu::getInstance()->isOptionChecked(MenuOption::PhysicsShowHulls)) { + renderDebugFlags = (RenderArgs::DebugFlags) (renderDebugFlags | (int)RenderArgs::RENDER_DEBUG_HULLS); + } + if (Menu::getInstance()->isOptionChecked(MenuOption::PhysicsShowOwned)) { + renderDebugFlags = + (RenderArgs::DebugFlags) (renderDebugFlags | (int)RenderArgs::RENDER_DEBUG_SIMULATION_OWNERSHIP); + } + renderArgs->_debugFlags = renderDebugFlags; + _entities.render(renderArgs); } - if (Menu::getInstance()->isOptionChecked(MenuOption::PhysicsShowOwned)) { - renderDebugFlags = - (RenderArgs::DebugFlags) (renderDebugFlags | (int) RenderArgs::RENDER_DEBUG_SIMULATION_OWNERSHIP); - } - renderArgs->_debugFlags = renderDebugFlags; - _entities.render(renderArgs); // render the ambient occlusion effect if enabled if (Menu::getInstance()->isOptionChecked(MenuOption::AmbientOcclusion)) { From a92a7980d7e5e094b608662408153798dc176b3b Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Thu, 9 Jul 2015 17:49:25 -0700 Subject: [PATCH 088/107] Restore audio scope --- interface/src/audio/AudioScope.cpp | 41 ++++++++++++++----------- interface/src/audio/AudioScope.h | 13 ++++---- interface/src/ui/ApplicationOverlay.cpp | 3 +- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/interface/src/audio/AudioScope.cpp b/interface/src/audio/AudioScope.cpp index 8cc27341d6..7a93be80f1 100644 --- a/interface/src/audio/AudioScope.cpp +++ b/interface/src/audio/AudioScope.cpp @@ -16,6 +16,9 @@ #include #include #include +#include +#include +#include #include "AudioScope.h" @@ -104,7 +107,7 @@ void AudioScope::freeScope() { } } -void AudioScope::render(int width, int height) { +void AudioScope::render(RenderArgs* renderArgs, int width, int height) { if (!_isEnabled) { return; @@ -122,24 +125,26 @@ void AudioScope::render(int width, int height) { int y = (height - (int)SCOPE_HEIGHT) / 2; int w = (int)SCOPE_WIDTH; int h = (int)SCOPE_HEIGHT; - - renderBackground(backgroundColor, x, y, w, h); - renderGrid(gridColor, x, y, w, h, gridRows, gridCols); - - renderLineStrip(_inputID, inputColor, x, y, _samplesPerScope, _scopeInputOffset, _scopeInput); - renderLineStrip(_outputLeftID, outputLeftColor, x, y, _samplesPerScope, _scopeOutputOffset, _scopeOutputLeft); - renderLineStrip(_outputRightD, outputRightColor, x, y, _samplesPerScope, _scopeOutputOffset, _scopeOutputRight); + + gpu::Batch batch; + auto geometryCache = DependencyManager::get(); + geometryCache->useSimpleDrawPipeline(batch); + auto textureCache = DependencyManager::get(); + batch.setUniformTexture(0, textureCache->getWhiteTexture()); + mat4 legacyProjection = glm::ortho(0, width, height, 0, -1000, 1000); + batch.setProjectionTransform(legacyProjection); + batch.setModelTransform(Transform()); + batch.setViewTransform(Transform()); + geometryCache->renderQuad(batch, x, y, w, h, backgroundColor); + geometryCache->renderGrid(batch, x, y, w, h, gridRows, gridCols, gridColor, _audioScopeGrid); + renderLineStrip(batch, _inputID, inputColor, x, y, _samplesPerScope, _scopeInputOffset, _scopeInput); + renderLineStrip(batch, _outputLeftID, outputLeftColor, x, y, _samplesPerScope, _scopeOutputOffset, _scopeOutputLeft); + renderLineStrip(batch, _outputRightD, outputRightColor, x, y, _samplesPerScope, _scopeOutputOffset, _scopeOutputRight); + renderArgs->_context->syncCache(); + renderArgs->_context->render(batch); } -void AudioScope::renderBackground(const glm::vec4& color, int x, int y, int width, int height) { - DependencyManager::get()->renderQuad(x, y, width, height, color); -} - -void AudioScope::renderGrid(const glm::vec4& color, int x, int y, int width, int height, int rows, int cols) { - DependencyManager::get()->renderGrid(x, y, width, height, rows, cols, color, _audioScopeGrid); -} - -void AudioScope::renderLineStrip(int id, const glm::vec4& color, int x, int y, int n, int offset, const QByteArray* byteArray) { +void AudioScope::renderLineStrip(gpu::Batch& batch, int id, const glm::vec4& color, int x, int y, int n, int offset, const QByteArray* byteArray) { int16_t sample; int16_t* samples = ((int16_t*) byteArray->data()) + offset; @@ -194,7 +199,7 @@ void AudioScope::renderLineStrip(int id, const glm::vec4& color, int x, int y, i geometryCache->updateVertices(id, points, color); - geometryCache->renderVertices(gpu::LINE_STRIP, id); + geometryCache->renderVertices(batch, gpu::LINE_STRIP, id); } int AudioScope::addBufferToScope(QByteArray* byteArray, int frameOffset, const int16_t* source, int sourceSamplesPerChannel, diff --git a/interface/src/audio/AudioScope.h b/interface/src/audio/AudioScope.h index cc9367e2d5..4ff4b55c29 100644 --- a/interface/src/audio/AudioScope.h +++ b/interface/src/audio/AudioScope.h @@ -14,11 +14,14 @@ #include -#include - #include #include +#include +#include +#include + + class AudioScope : public QObject, public Dependency { Q_OBJECT SINGLETON_DEPENDENCY @@ -28,7 +31,7 @@ public: void freeScope(); void reallocateScope(int frames); - void render(int width, int height); + void render(RenderArgs* renderArgs, int width, int height); public slots: void toggle(); @@ -48,9 +51,7 @@ private slots: private: // Audio scope methods for rendering - static void renderBackground(const glm::vec4& color, int x, int y, int width, int height); - void renderGrid(const glm::vec4& color, int x, int y, int width, int height, int rows, int cols); - void renderLineStrip(int id, const glm::vec4& color, int x, int y, int n, int offset, const QByteArray* byteArray); + void renderLineStrip(gpu::Batch& batch, int id, const glm::vec4& color, int x, int y, int n, int offset, const QByteArray* byteArray); // Audio scope methods for data acquisition int addBufferToScope(QByteArray* byteArray, int frameOffset, const int16_t* source, int sourceSamples, diff --git a/interface/src/ui/ApplicationOverlay.cpp b/interface/src/ui/ApplicationOverlay.cpp index e7d220893f..c58d450126 100644 --- a/interface/src/ui/ApplicationOverlay.cpp +++ b/interface/src/ui/ApplicationOverlay.cpp @@ -129,11 +129,12 @@ void ApplicationOverlay::renderOverlays(RenderArgs* renderArgs) { emit qApp->renderingOverlay(); qApp->getOverlays().renderHUD(renderArgs); + DependencyManager::get()->render(renderArgs, _overlayFramebuffer->size().width(), _overlayFramebuffer->size().height()); + glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); - renderArgs->_context->syncCache(); fboViewport(_overlayFramebuffer); } From 6c5d7b87891d909500325425a6a1ede7e35da63c Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Thu, 9 Jul 2015 17:53:26 -0700 Subject: [PATCH 089/107] Performance optimizations in transform buffers and geometry cache --- libraries/gpu/src/gpu/GLBackendTransform.cpp | 4 +- libraries/render-utils/src/GeometryCache.cpp | 60 +++++++++++++------- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/libraries/gpu/src/gpu/GLBackendTransform.cpp b/libraries/gpu/src/gpu/GLBackendTransform.cpp index 5f4d5f4af6..48a42fe5f1 100755 --- a/libraries/gpu/src/gpu/GLBackendTransform.cpp +++ b/libraries/gpu/src/gpu/GLBackendTransform.cpp @@ -117,7 +117,7 @@ void GLBackend::updateTransform() { if (_transform._invalidView || _transform._invalidProj || _transform._invalidViewport) { glBindBufferBase(GL_UNIFORM_BUFFER, TRANSFORM_CAMERA_SLOT, 0); glBindBuffer(GL_ARRAY_BUFFER, _transform._transformCameraBuffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(_transform._transformCamera), (const void*) &_transform._transformCamera, GL_DYNAMIC_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(_transform._transformCamera), (const void*)&_transform._transformCamera); glBindBuffer(GL_ARRAY_BUFFER, 0); CHECK_GL_ERROR(); } @@ -125,7 +125,7 @@ void GLBackend::updateTransform() { if (_transform._invalidModel) { glBindBufferBase(GL_UNIFORM_BUFFER, TRANSFORM_OBJECT_SLOT, 0); glBindBuffer(GL_ARRAY_BUFFER, _transform._transformObjectBuffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(_transform._transformObject), (const void*) &_transform._transformObject, GL_DYNAMIC_DRAW); + glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(_transform._transformObject), (const void*) &_transform._transformObject); glBindBuffer(GL_ARRAY_BUFFER, 0); CHECK_GL_ERROR(); } diff --git a/libraries/render-utils/src/GeometryCache.cpp b/libraries/render-utils/src/GeometryCache.cpp index 35f946c55c..02e59bfa3a 100644 --- a/libraries/render-utils/src/GeometryCache.cpp +++ b/libraries/render-utils/src/GeometryCache.cpp @@ -279,14 +279,21 @@ void GeometryCache::renderSphere(gpu::Batch& batch, float radius, int slices, in const int VERTICES_SLOT = 0; const int NORMALS_SLOT = 1; const int COLOR_SLOT = 2; - gpu::Stream::FormatPointer streamFormat(new gpu::Stream::Format()); // 1 for everyone - streamFormat->setAttribute(gpu::Stream::POSITION, VERTICES_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0); - streamFormat->setAttribute(gpu::Stream::NORMAL, NORMALS_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ)); - streamFormat->setAttribute(gpu::Stream::COLOR, COLOR_SLOT, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + static gpu::Stream::FormatPointer streamFormat; + static gpu::Element positionElement, normalElement, colorElement; + if (!streamFormat) { + streamFormat.reset(new gpu::Stream::Format()); // 1 for everyone + streamFormat->setAttribute(gpu::Stream::POSITION, VERTICES_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0); + streamFormat->setAttribute(gpu::Stream::NORMAL, NORMALS_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ)); + streamFormat->setAttribute(gpu::Stream::COLOR, COLOR_SLOT, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + positionElement = streamFormat->getAttributes().at(gpu::Stream::POSITION)._element; + normalElement = streamFormat->getAttributes().at(gpu::Stream::NORMAL)._element; + colorElement = streamFormat->getAttributes().at(gpu::Stream::COLOR)._element; + } - gpu::BufferView verticesView(verticesBuffer, streamFormat->getAttributes().at(gpu::Stream::POSITION)._element); - gpu::BufferView normalsView(verticesBuffer, streamFormat->getAttributes().at(gpu::Stream::NORMAL)._element); - gpu::BufferView colorView(colorBuffer, streamFormat->getAttributes().at(gpu::Stream::COLOR)._element); + gpu::BufferView verticesView(verticesBuffer, positionElement); + gpu::BufferView normalsView(verticesBuffer, normalElement); + gpu::BufferView colorView(colorBuffer, colorElement); batch.setInputFormat(streamFormat); batch.setInputBuffer(VERTICES_SLOT, verticesView); @@ -899,14 +906,21 @@ void GeometryCache::renderSolidCube(gpu::Batch& batch, float size, const glm::ve const int VERTICES_SLOT = 0; const int NORMALS_SLOT = 1; const int COLOR_SLOT = 2; - gpu::Stream::FormatPointer streamFormat(new gpu::Stream::Format()); // 1 for everyone + static gpu::Stream::FormatPointer streamFormat; + static gpu::Element positionElement, normalElement, colorElement; + if (!streamFormat) { + streamFormat.reset(new gpu::Stream::Format()); // 1 for everyone + streamFormat->setAttribute(gpu::Stream::POSITION, VERTICES_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0); + streamFormat->setAttribute(gpu::Stream::NORMAL, NORMALS_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ)); + streamFormat->setAttribute(gpu::Stream::COLOR, COLOR_SLOT, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + positionElement = streamFormat->getAttributes().at(gpu::Stream::POSITION)._element; + normalElement = streamFormat->getAttributes().at(gpu::Stream::NORMAL)._element; + colorElement = streamFormat->getAttributes().at(gpu::Stream::COLOR)._element; + } - streamFormat->setAttribute(gpu::Stream::POSITION, VERTICES_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0); - streamFormat->setAttribute(gpu::Stream::NORMAL, NORMALS_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ)); - streamFormat->setAttribute(gpu::Stream::COLOR, COLOR_SLOT, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); - - gpu::BufferView verticesView(verticesBuffer, 0, verticesBuffer->getSize(), VERTEX_STRIDE, streamFormat->getAttributes().at(gpu::Stream::POSITION)._element); - gpu::BufferView normalsView(verticesBuffer, NORMALS_OFFSET, verticesBuffer->getSize(), VERTEX_STRIDE, streamFormat->getAttributes().at(gpu::Stream::NORMAL)._element); + + gpu::BufferView verticesView(verticesBuffer, 0, verticesBuffer->getSize(), VERTEX_STRIDE, positionElement); + gpu::BufferView normalsView(verticesBuffer, NORMALS_OFFSET, verticesBuffer->getSize(), VERTEX_STRIDE, normalElement); gpu::BufferView colorView(colorBuffer, streamFormat->getAttributes().at(gpu::Stream::COLOR)._element); batch.setInputFormat(streamFormat); @@ -986,12 +1000,18 @@ void GeometryCache::renderWireCube(gpu::Batch& batch, float size, const glm::vec const int VERTICES_SLOT = 0; const int COLOR_SLOT = 1; - gpu::Stream::FormatPointer streamFormat(new gpu::Stream::Format()); // 1 for everyone - streamFormat->setAttribute(gpu::Stream::POSITION, VERTICES_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0); - streamFormat->setAttribute(gpu::Stream::COLOR, COLOR_SLOT, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); - - gpu::BufferView verticesView(verticesBuffer, streamFormat->getAttributes().at(gpu::Stream::POSITION)._element); - gpu::BufferView colorView(colorBuffer, streamFormat->getAttributes().at(gpu::Stream::COLOR)._element); + static gpu::Stream::FormatPointer streamFormat; + static gpu::Element positionElement, colorElement; + if (!streamFormat) { + streamFormat.reset(new gpu::Stream::Format()); // 1 for everyone + streamFormat->setAttribute(gpu::Stream::POSITION, VERTICES_SLOT, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), 0); + streamFormat->setAttribute(gpu::Stream::COLOR, COLOR_SLOT, gpu::Element(gpu::VEC4, gpu::UINT8, gpu::RGBA)); + positionElement = streamFormat->getAttributes().at(gpu::Stream::POSITION)._element; + colorElement = streamFormat->getAttributes().at(gpu::Stream::COLOR)._element; + } + + gpu::BufferView verticesView(verticesBuffer, positionElement); + gpu::BufferView colorView(colorBuffer, colorElement); batch.setInputFormat(streamFormat); batch.setInputBuffer(VERTICES_SLOT, verticesView); From 474a6436110a40286eb8d0a4b5c06abd56db2f62 Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Thu, 9 Jul 2015 18:53:12 -0700 Subject: [PATCH 090/107] Added unit test for shared/transform. The getInverseMat is currently failing, it shows the issue that was affecting the mirror rendering. --- tests/shared/src/TransformTests.cpp | 84 +++++++++++++++++++++++++++++ tests/shared/src/TransformTests.h | 52 ++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 tests/shared/src/TransformTests.cpp create mode 100644 tests/shared/src/TransformTests.h diff --git a/tests/shared/src/TransformTests.cpp b/tests/shared/src/TransformTests.cpp new file mode 100644 index 0000000000..93b0583aa6 --- /dev/null +++ b/tests/shared/src/TransformTests.cpp @@ -0,0 +1,84 @@ +// +// TransformTests.cpp +// tests/shared/src +// +// Copyright 2013-2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include + +#include "TransformTests.h" +#include "SharedLogging.h" + +using namespace glm; + +const vec3 xAxis(1.0f, 0.0f, 0.0f); +const vec3 yAxis(0.0f, 1.0f, 0.0f); +const vec3 zAxis(0.0f, 0.0f, 1.0f); +const quat rot90 = angleAxis((float)M_PI / 2.0f, yAxis); + +QTEST_MAIN(TransformTests) + +const float EPSILON = 0.001f; + +void TransformTests::getMatrix() { + + const vec3 t(0.0f, 0.0f, 10.0f); + + // create a matrix that is composed of a PI/2 rotation followed by a small z translation + const mat4 m(vec4(rot90 * xAxis, 0.0f), + vec4(rot90 * yAxis, 0.0f), + vec4(rot90 * zAxis, 0.0f), + vec4(vec4(t, 1.0f))); + + // postScale by a mirror about the x axis. + const mat4 mirrorX(vec4(-1.0f, 0.0f, 0.0f, 0.0f), + vec4( 0.0f, 1.0f, 0.0f, 0.0f), + vec4( 0.0f, 0.0f, 1.0f, 0.0f), + vec4( 0.0f, 0.0f, 0.0f, 1.0f)); + const mat4 result_a = m * mirrorX; + + Transform xform; + xform.setRotation(rot90); + xform.setTranslation(t); + xform.postScale(vec3(-1.0f, 1.0f, 1.0f)); + mat4 result_b; + xform.getMatrix(result_b); + + QCOMPARE_WITH_ABS_ERROR(result_a, result_b, EPSILON); +} + +void TransformTests::getInverseMatrix() { + + const vec3 t(0.0f, 0.0f, 10.0f); + + // create a matrix that is composed of a PI/2 rotation followed by a small z translation + const mat4 m(vec4(rot90 * xAxis, 0.0f), + vec4(rot90 * yAxis, 0.0f), + vec4(rot90 * zAxis, 0.0f), + vec4(vec4(t, 1.0f))); + + // mirror about the x axis. + const mat4 mirrorX(vec4(-1.0f, 0.0f, 0.0f, 0.0f), + vec4( 0.0f, 1.0f, 0.0f, 0.0f), + vec4( 0.0f, 0.0f, 1.0f, 0.0f), + vec4( 0.0f, 0.0f, 0.0f, 1.0f)); + const mat4 result_a = inverse(m * mirrorX); + + Transform xform; + xform.setTranslation(t); + xform.setRotation(rot90); + + // + // change postScale to preScale and the test will pass... + // + + xform.postScale(vec3(-1.0f, 1.0f, 1.0f)); + mat4 result_b; + xform.getInverseMatrix(result_b); + + QCOMPARE_WITH_ABS_ERROR(result_a, result_b, EPSILON); +} diff --git a/tests/shared/src/TransformTests.h b/tests/shared/src/TransformTests.h new file mode 100644 index 0000000000..ab5c8cf144 --- /dev/null +++ b/tests/shared/src/TransformTests.h @@ -0,0 +1,52 @@ +// +// TransformTests.h +// tests/shared/src +// +// Copyright 2013-2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_TransformTests_h +#define hifi_TransformTests_h + +#include +#include +#include + +inline float getErrorDifference(const glm::mat4& a, const glm::mat4& b) { + float maxDiff = 0; + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 4; ++j) { + float diff = fabs(a[i][j] - b[i][j]); + maxDiff = std::max(diff, maxDiff); + } + } + return maxDiff; +} + +inline QTextStream& operator<< (QTextStream& stream, const glm::mat4& matrix) { + stream << "[\n\t\t"; + stream.setFieldWidth(15); + for (int r = 0; r < 4; ++r) { + for (int c = 0; c < 4; ++c) { + stream << matrix[c][r]; + } + stream << "\n\t\t"; + } + stream.setFieldWidth(0); + stream << "]\n\t"; // hacky as hell, but this should work... + return stream; +} + +#include <../QTestExtensions.h> + +class TransformTests : public QObject { + Q_OBJECT +private slots: + void getMatrix(); + void getInverseMatrix(); +}; + +#endif // hifi_TransformTests_h From 7fc9fd97ff11076fd0b4e75e4cb1282f499be759 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Fri, 10 Jul 2015 11:03:12 -0700 Subject: [PATCH 091/107] Replacing setUniformTexture by setResourceTexture in order to differenciate the 2 types of binding points for the BUffers. --- interface/src/avatar/Avatar.cpp | 2 +- interface/src/ui/ApplicationCompositor.cpp | 2 +- interface/src/ui/ApplicationOverlay.cpp | 2 +- .../src/ui/overlays/BillboardOverlay.cpp | 4 +-- .../RenderableParticleEffectEntityItem.cpp | 2 +- libraries/gpu/src/gpu/Batch.cpp | 8 +++--- libraries/gpu/src/gpu/Batch.h | 6 ++-- libraries/gpu/src/gpu/GLBackend.cpp | 2 +- libraries/gpu/src/gpu/GLBackend.h | 2 +- libraries/gpu/src/gpu/GLBackendPipeline.cpp | 2 +- libraries/model/src/model/Skybox.cpp | 2 +- .../src/DeferredLightingEffect.cpp | 28 +++++++++---------- libraries/render-utils/src/Model.cpp | 10 +++---- .../render-utils/src/RenderDeferredTask.cpp | 2 +- libraries/render-utils/src/TextRenderer3D.cpp | 2 +- 15 files changed, 38 insertions(+), 38 deletions(-) diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index 3d4c158a0b..0dde96c7ce 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -644,7 +644,7 @@ void Avatar::renderBillboard(RenderArgs* renderArgs) { glm::vec2 texCoordBottomRight(1.0f, 1.0f); gpu::Batch& batch = *renderArgs->_batch; - batch.setUniformTexture(0, _billboardTexture->getGPUTexture()); + batch.setResourceTexture(0, _billboardTexture->getGPUTexture()); DependencyManager::get()->bindSimpleProgram(batch, true); DependencyManager::get()->renderQuad(batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, glm::vec4(1.0f, 1.0f, 1.0f, 1.0f)); diff --git a/interface/src/ui/ApplicationCompositor.cpp b/interface/src/ui/ApplicationCompositor.cpp index dafa332d53..a1420e3b6a 100644 --- a/interface/src/ui/ApplicationCompositor.cpp +++ b/interface/src/ui/ApplicationCompositor.cpp @@ -179,7 +179,7 @@ void ApplicationCompositor::bindCursorTexture(gpu::Batch& batch, uint8_t cursorI _cursors[iconId] = DependencyManager::get()-> getImageTexture(iconPath); } - batch.setUniformTexture(0, _cursors[iconId]); + batch.setResourceTexture(0, _cursors[iconId]); } // Draws the FBO texture for the screen diff --git a/interface/src/ui/ApplicationOverlay.cpp b/interface/src/ui/ApplicationOverlay.cpp index e7d220893f..4c3d082bdb 100644 --- a/interface/src/ui/ApplicationOverlay.cpp +++ b/interface/src/ui/ApplicationOverlay.cpp @@ -196,7 +196,7 @@ void ApplicationOverlay::renderDomainConnectionStatusBorder(RenderArgs* renderAr geometryCache->useSimpleDrawPipeline(batch); batch.setProjectionTransform(mat4()); batch.setModelTransform(mat4()); - batch.setUniformTexture(0, DependencyManager::get()->getWhiteTexture()); + batch.setResourceTexture(0, DependencyManager::get()->getWhiteTexture()); batch._glLineWidth(CONNECTION_STATUS_BORDER_LINE_WIDTH); // TODO animate the disconnect border for some excitement while not connected? diff --git a/interface/src/ui/overlays/BillboardOverlay.cpp b/interface/src/ui/overlays/BillboardOverlay.cpp index 988223765a..1bf4f2a9c7 100644 --- a/interface/src/ui/overlays/BillboardOverlay.cpp +++ b/interface/src/ui/overlays/BillboardOverlay.cpp @@ -87,12 +87,12 @@ void BillboardOverlay::render(RenderArgs* args) { transform.postScale(glm::vec3(getDimensions(), 1.0f)); batch->setModelTransform(transform); - batch->setUniformTexture(0, _texture->getGPUTexture()); + batch->setResourceTexture(0, _texture->getGPUTexture()); DependencyManager::get()->renderQuad(*batch, topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight, glm::vec4(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha)); - batch->setUniformTexture(0, args->_whiteTexture); // restore default white color after me + batch->setResourceTexture(0, args->_whiteTexture); // restore default white color after me } } diff --git a/libraries/entities-renderer/src/RenderableParticleEffectEntityItem.cpp b/libraries/entities-renderer/src/RenderableParticleEffectEntityItem.cpp index d00728a9eb..2eb95d1bef 100644 --- a/libraries/entities-renderer/src/RenderableParticleEffectEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderableParticleEffectEntityItem.cpp @@ -50,7 +50,7 @@ void RenderableParticleEffectEntityItem::render(RenderArgs* args) { Q_ASSERT(args->_batch); gpu::Batch& batch = *args->_batch; if (textured) { - batch.setUniformTexture(0, _texture->getGPUTexture()); + batch.setResourceTexture(0, _texture->getGPUTexture()); } batch.setModelTransform(getTransformToCenter()); DependencyManager::get()->bindSimpleProgram(batch, textured); diff --git a/libraries/gpu/src/gpu/Batch.cpp b/libraries/gpu/src/gpu/Batch.cpp index b3e6b6117d..b643ba66b8 100644 --- a/libraries/gpu/src/gpu/Batch.cpp +++ b/libraries/gpu/src/gpu/Batch.cpp @@ -227,15 +227,15 @@ void Batch::setUniformBuffer(uint32 slot, const BufferView& view) { } -void Batch::setUniformTexture(uint32 slot, const TexturePointer& texture) { - ADD_COMMAND(setUniformTexture); +void Batch::setResourceTexture(uint32 slot, const TexturePointer& texture) { + ADD_COMMAND(setResourceTexture); _params.push_back(_textures.cache(texture)); _params.push_back(slot); } -void Batch::setUniformTexture(uint32 slot, const TextureView& view) { - setUniformTexture(slot, view._texture); +void Batch::setResourceTexture(uint32 slot, const TextureView& view) { + setResourceTexture(slot, view._texture); } void Batch::setFramebuffer(const FramebufferPointer& framebuffer) { diff --git a/libraries/gpu/src/gpu/Batch.h b/libraries/gpu/src/gpu/Batch.h index 0cc1a6bee3..6841edb3ef 100644 --- a/libraries/gpu/src/gpu/Batch.h +++ b/libraries/gpu/src/gpu/Batch.h @@ -102,8 +102,8 @@ public: void setUniformBuffer(uint32 slot, const BufferPointer& buffer, Offset offset, Offset size); void setUniformBuffer(uint32 slot, const BufferView& view); // not a command, just a shortcut from a BufferView - void setUniformTexture(uint32 slot, const TexturePointer& view); - void setUniformTexture(uint32 slot, const TextureView& view); // not a command, just a shortcut from a TextureView + void setResourceTexture(uint32 slot, const TexturePointer& view); + void setResourceTexture(uint32 slot, const TextureView& view); // not a command, just a shortcut from a TextureView // Framebuffer Stage void setFramebuffer(const FramebufferPointer& framebuffer); @@ -172,7 +172,7 @@ public: COMMAND_setStateBlendFactor, COMMAND_setUniformBuffer, - COMMAND_setUniformTexture, + COMMAND_setResourceTexture, COMMAND_setFramebuffer, diff --git a/libraries/gpu/src/gpu/GLBackend.cpp b/libraries/gpu/src/gpu/GLBackend.cpp index 54a54ce2a5..a0320cea1b 100644 --- a/libraries/gpu/src/gpu/GLBackend.cpp +++ b/libraries/gpu/src/gpu/GLBackend.cpp @@ -35,7 +35,7 @@ GLBackend::CommandCall GLBackend::_commandCalls[Batch::NUM_COMMANDS] = (&::gpu::GLBackend::do_setStateBlendFactor), (&::gpu::GLBackend::do_setUniformBuffer), - (&::gpu::GLBackend::do_setUniformTexture), + (&::gpu::GLBackend::do_setResourceTexture), (&::gpu::GLBackend::do_setFramebuffer), diff --git a/libraries/gpu/src/gpu/GLBackend.h b/libraries/gpu/src/gpu/GLBackend.h index 78b0f0838e..2f511ca8d4 100644 --- a/libraries/gpu/src/gpu/GLBackend.h +++ b/libraries/gpu/src/gpu/GLBackend.h @@ -309,7 +309,7 @@ protected: // Uniform Stage void do_setUniformBuffer(Batch& batch, uint32 paramOffset); - void do_setUniformTexture(Batch& batch, uint32 paramOffset); + void do_setResourceTexture(Batch& batch, uint32 paramOffset); struct UniformStageState { diff --git a/libraries/gpu/src/gpu/GLBackendPipeline.cpp b/libraries/gpu/src/gpu/GLBackendPipeline.cpp index 3b16c58b20..51a3a24e9b 100755 --- a/libraries/gpu/src/gpu/GLBackendPipeline.cpp +++ b/libraries/gpu/src/gpu/GLBackendPipeline.cpp @@ -188,7 +188,7 @@ void GLBackend::do_setUniformBuffer(Batch& batch, uint32 paramOffset) { (void) CHECK_GL_ERROR(); } -void GLBackend::do_setUniformTexture(Batch& batch, uint32 paramOffset) { +void GLBackend::do_setResourceTexture(Batch& batch, uint32 paramOffset) { GLuint slot = batch._params[paramOffset + 1]._uint; TexturePointer uniformTexture = batch._textures.get(batch._params[paramOffset + 0]._uint); diff --git a/libraries/model/src/model/Skybox.cpp b/libraries/model/src/model/Skybox.cpp index a02c646668..0fb2458f01 100755 --- a/libraries/model/src/model/Skybox.cpp +++ b/libraries/model/src/model/Skybox.cpp @@ -103,7 +103,7 @@ void Skybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const Sky batch.setInputBuffer(gpu::Stream::POSITION, theBuffer, 0, 8); batch.setUniformBuffer(SKYBOX_CONSTANTS_SLOT, theConstants, 0, theConstants->getSize()); batch.setInputFormat(theFormat); - batch.setUniformTexture(0, skybox.getCubemap()); + batch.setResourceTexture(0, skybox.getCubemap()); batch.draw(gpu::TRIANGLE_STRIP, 4); } diff --git a/libraries/render-utils/src/DeferredLightingEffect.cpp b/libraries/render-utils/src/DeferredLightingEffect.cpp index a721e0cad3..c8ae000479 100644 --- a/libraries/render-utils/src/DeferredLightingEffect.cpp +++ b/libraries/render-utils/src/DeferredLightingEffect.cpp @@ -147,7 +147,7 @@ void DeferredLightingEffect::bindSimpleProgram(gpu::Batch& batch, bool textured, if (!config.isTextured()) { // If it is not textured, bind white texture and keep using textured pipeline - batch.setUniformTexture(0, DependencyManager::get()->getWhiteTexture()); + batch.setResourceTexture(0, DependencyManager::get()->getWhiteTexture()); } } @@ -244,13 +244,13 @@ void DeferredLightingEffect::render(RenderArgs* args) { batch.clearColorFramebuffer(freeFBO->getBufferMask(), glm::vec4(0.0f, 0.0f, 0.0f, 0.0f)); - batch.setUniformTexture(0, textureCache->getPrimaryColorTexture()); + batch.setResourceTexture(0, textureCache->getPrimaryColorTexture()); - batch.setUniformTexture(1, textureCache->getPrimaryNormalTexture()); + batch.setResourceTexture(1, textureCache->getPrimaryNormalTexture()); - batch.setUniformTexture(2, textureCache->getPrimarySpecularTexture()); + batch.setResourceTexture(2, textureCache->getPrimarySpecularTexture()); - batch.setUniformTexture(3, textureCache->getPrimaryDepthTexture()); + batch.setResourceTexture(3, textureCache->getPrimaryDepthTexture()); // get the viewport side (left, right, both) int viewport[4]; @@ -275,7 +275,7 @@ void DeferredLightingEffect::render(RenderArgs* args) { const LightLocations* locations = &_directionalLightLocations; bool shadowsEnabled = _viewState->getShadowsEnabled(); if (shadowsEnabled) { - batch.setUniformTexture(4, textureCache->getShadowFramebuffer()->getDepthStencilBuffer()); + batch.setResourceTexture(4, textureCache->getShadowFramebuffer()->getDepthStencilBuffer()); program = _directionalLightShadowMap; locations = &_directionalLightShadowMapLocations; @@ -329,7 +329,7 @@ void DeferredLightingEffect::render(RenderArgs* args) { } if (useSkyboxCubemap) { - batch.setUniformTexture(5, _skybox->getCubemap()); + batch.setResourceTexture(5, _skybox->getCubemap()); } if (locations->lightBufferUnit >= 0) { @@ -377,11 +377,11 @@ void DeferredLightingEffect::render(RenderArgs* args) { } if (useSkyboxCubemap) { - batch.setUniformTexture(5, nullptr); + batch.setResourceTexture(5, nullptr); } if (shadowsEnabled) { - batch.setUniformTexture(4, nullptr); + batch.setResourceTexture(4, nullptr); } glm::vec4 sCoefficients(sWidth / 2.0f, 0.0f, 0.0f, sMin + sWidth / 2.0f); @@ -530,10 +530,10 @@ void DeferredLightingEffect::render(RenderArgs* args) { } // Probably not necessary in the long run because the gpu layer would unbound this texture if used as render target - batch.setUniformTexture(0, nullptr); - batch.setUniformTexture(1, nullptr); - batch.setUniformTexture(2, nullptr); - batch.setUniformTexture(3, nullptr); + batch.setResourceTexture(0, nullptr); + batch.setResourceTexture(1, nullptr); + batch.setResourceTexture(2, nullptr); + batch.setResourceTexture(3, nullptr); args->_context->syncCache(); args->_context->render(batch); @@ -551,7 +551,7 @@ void DeferredLightingEffect::copyBack(RenderArgs* args) { batch.setFramebuffer(textureCache->getPrimaryFramebuffer()); batch.setPipeline(_blitLightBuffer); - batch.setUniformTexture(0, freeFBO->getRenderBuffer(0)); + batch.setResourceTexture(0, freeFBO->getRenderBuffer(0)); batch.setProjectionTransform(glm::mat4()); batch.setViewTransform(Transform()); diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index 03140c4dfb..ce325d23cb 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -2031,10 +2031,10 @@ void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool tran } static bool showDiffuse = true; if (showDiffuse && diffuseMap) { - batch.setUniformTexture(0, diffuseMap->getGPUTexture()); + batch.setResourceTexture(0, diffuseMap->getGPUTexture()); } else { - batch.setUniformTexture(0, textureCache->getWhiteTexture()); + batch.setResourceTexture(0, textureCache->getWhiteTexture()); } if (locations->texcoordMatrices >= 0) { @@ -2050,14 +2050,14 @@ void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool tran if (!mesh.tangents.isEmpty()) { Texture* normalMap = networkPart.normalTexture.data(); - batch.setUniformTexture(1, !normalMap ? + batch.setResourceTexture(1, !normalMap ? textureCache->getBlueTexture() : normalMap->getGPUTexture()); } if (locations->specularTextureUnit >= 0) { Texture* specularMap = networkPart.specularTexture.data(); - batch.setUniformTexture(locations->specularTextureUnit, !specularMap ? + batch.setResourceTexture(locations->specularTextureUnit, !specularMap ? textureCache->getWhiteTexture() : specularMap->getGPUTexture()); } @@ -2074,7 +2074,7 @@ void Model::renderPart(RenderArgs* args, int meshIndex, int partIndex, bool tran GLBATCH(glUniform2f)(locations->emissiveParams, emissiveOffset, emissiveScale); Texture* emissiveMap = networkPart.emissiveTexture.data(); - batch.setUniformTexture(locations->emissiveTextureUnit, !emissiveMap ? + batch.setResourceTexture(locations->emissiveTextureUnit, !emissiveMap ? textureCache->getWhiteTexture() : emissiveMap->getGPUTexture()); } diff --git a/libraries/render-utils/src/RenderDeferredTask.cpp b/libraries/render-utils/src/RenderDeferredTask.cpp index 8de5c8af01..cf60c42f98 100755 --- a/libraries/render-utils/src/RenderDeferredTask.cpp +++ b/libraries/render-utils/src/RenderDeferredTask.cpp @@ -236,7 +236,7 @@ void DrawOverlay3D::run(const SceneContextPointer& sceneContext, const RenderCon batch.setViewTransform(viewMat); batch.setPipeline(getOpaquePipeline()); - batch.setUniformTexture(0, args->_whiteTexture); + batch.setResourceTexture(0, args->_whiteTexture); if (!inItems.empty()) { batch.clearFramebuffer(gpu::Framebuffer::BUFFER_DEPTH, glm::vec4(), 1.f, 0); diff --git a/libraries/render-utils/src/TextRenderer3D.cpp b/libraries/render-utils/src/TextRenderer3D.cpp index a429719b8b..973cddc4d7 100644 --- a/libraries/render-utils/src/TextRenderer3D.cpp +++ b/libraries/render-utils/src/TextRenderer3D.cpp @@ -423,7 +423,7 @@ void Font3D::drawString(gpu::Batch& batch, float x, float y, const QString& str, setupGPU(); batch.setPipeline(_pipeline); - batch.setUniformTexture(_fontLoc, _texture); + batch.setResourceTexture(_fontLoc, _texture); batch._glUniform1i(_outlineLoc, (effectType == TextRenderer3D::OUTLINE_EFFECT)); batch._glUniform4fv(_colorLoc, 1, (const GLfloat*)color); From 2e5fd04d9e121cb5256d84e7d446169668c34a0b Mon Sep 17 00:00:00 2001 From: Niraj Venkat Date: Fri, 10 Jul 2015 11:27:34 -0700 Subject: [PATCH 092/107] Fix to get build working on Linux --- libraries/gpu/src/gpu/GLBackendQuery.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libraries/gpu/src/gpu/GLBackendQuery.cpp b/libraries/gpu/src/gpu/GLBackendQuery.cpp index 003f757450..39db19dafd 100644 --- a/libraries/gpu/src/gpu/GLBackendQuery.cpp +++ b/libraries/gpu/src/gpu/GLBackendQuery.cpp @@ -95,7 +95,9 @@ void GLBackend::do_getQuery(Batch& batch, uint32 paramOffset) { if (glquery) { #if (GPU_FEATURE_PROFILE == GPU_LEGACY) // (EXT_TIMER_QUERY) + #if !defined(Q_OS_LINUX) glGetQueryObjectui64vEXT(glquery->_qo, GL_QUERY_RESULT, &glquery->_result); + #endif #else glGetQueryObjectui64v(glquery->_qo, GL_QUERY_RESULT, &glquery->_result); #endif From 71440dfbc551f9a33278bb3e4b25c0286094684c Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Fri, 10 Jul 2015 11:30:11 -0700 Subject: [PATCH 093/107] add checks for NaN --- .../entities/src/EntityActionInterface.cpp | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/libraries/entities/src/EntityActionInterface.cpp b/libraries/entities/src/EntityActionInterface.cpp index 2b723d4e15..ba7f3afea4 100644 --- a/libraries/entities/src/EntityActionInterface.cpp +++ b/libraries/entities/src/EntityActionInterface.cpp @@ -127,21 +127,21 @@ glm::vec3 EntityActionInterface::extractVec3Argument(QString objectName, QVarian qDebug() << objectName << "requires argument:" << argumentName; } ok = false; - return glm::vec3(); + return glm::vec3(0.0f); } QVariant resultV = arguments[argumentName]; if (resultV.type() != (QVariant::Type) QMetaType::QVariantMap) { qDebug() << objectName << "argument" << argumentName << "must be a map"; ok = false; - return glm::vec3(); + return glm::vec3(0.0f); } QVariantMap resultVM = resultV.toMap(); if (!resultVM.contains("x") || !resultVM.contains("y") || !resultVM.contains("z")) { - qDebug() << objectName << "argument" << argumentName << "must be a map with keys of x, y, z"; + qDebug() << objectName << "argument" << argumentName << "must be a map with keys: x, y, z"; ok = false; - return glm::vec3(); + return glm::vec3(0.0f); } QVariant xV = resultVM["x"]; @@ -155,9 +155,15 @@ glm::vec3 EntityActionInterface::extractVec3Argument(QString objectName, QVarian float y = yV.toFloat(&yOk); float z = zV.toFloat(&zOk); if (!xOk || !yOk || !zOk) { - qDebug() << objectName << "argument" << argumentName << "must be a map with keys of x, y, z and values of type float."; + qDebug() << objectName << "argument" << argumentName << "must be a map with keys: x, y, and z of type float."; ok = false; - return glm::vec3(); + return glm::vec3(0.0f); + } + + if (x != x || y != y || z != z) { + // at least one of the values is NaN + ok = false; + return glm::vec3(0.0f); } return glm::vec3(x, y, z); @@ -181,8 +187,8 @@ glm::quat EntityActionInterface::extractQuatArgument(QString objectName, QVarian } QVariantMap resultVM = resultV.toMap(); - if (!resultVM.contains("x") || !resultVM.contains("y") || !resultVM.contains("z")) { - qDebug() << objectName << "argument" << argumentName << "must be a map with keys of x, y, z"; + if (!resultVM.contains("x") || !resultVM.contains("y") || !resultVM.contains("z") || !resultVM.contains("w")) { + qDebug() << objectName << "argument" << argumentName << "must be a map with keys: x, y, z, and w"; ok = false; return glm::quat(); } @@ -202,12 +208,18 @@ glm::quat EntityActionInterface::extractQuatArgument(QString objectName, QVarian float w = wV.toFloat(&wOk); if (!xOk || !yOk || !zOk || !wOk) { qDebug() << objectName << "argument" << argumentName - << "must be a map with keys of x, y, z, w and values of type float."; + << "must be a map with keys: x, y, z, and w of type float."; ok = false; return glm::quat(); } - return glm::quat(w, x, y, z); + if (x != x || y != y || z != z || w != w) { + // at least one of the components is NaN! + ok = false; + return glm::quat(); + } + + return glm::normalize(glm::quat(w, x, y, z)); } float EntityActionInterface::extractFloatArgument(QString objectName, QVariantMap arguments, @@ -224,7 +236,7 @@ float EntityActionInterface::extractFloatArgument(QString objectName, QVariantMa bool vOk = true; float v = vV.toFloat(&vOk); - if (!vOk) { + if (!vOk || v != v) { ok = false; return 0.0f; } From 90ae1fea8b2aef2bfc1a0d3d288917202e3aa657 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Fri, 10 Jul 2015 11:30:24 -0700 Subject: [PATCH 094/107] handle case where action params are missing --- libraries/physics/src/ObjectActionOffset.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/libraries/physics/src/ObjectActionOffset.cpp b/libraries/physics/src/ObjectActionOffset.cpp index 2391ded13d..00b7fe6734 100644 --- a/libraries/physics/src/ObjectActionOffset.cpp +++ b/libraries/physics/src/ObjectActionOffset.cpp @@ -95,26 +95,21 @@ bool ObjectActionOffset::updateArguments(QVariantMap arguments) { glm::vec3 pointToOffsetFrom = EntityActionInterface::extractVec3Argument("offset action", arguments, "pointToOffsetFrom", ok, true); if (!ok) { - return false; + pointToOffsetFrom = _pointToOffsetFrom; } ok = true; float linearTimeScale = EntityActionInterface::extractFloatArgument("offset action", arguments, "linearTimeScale", ok, false); - if (ok) { - if (linearTimeScale <= 0.0f) { - qDebug() << "offset action -- linearTimeScale must be greater than zero."; - return false; - } - } else { - linearTimeScale = 0.1f; + if (!ok) { + linearTimeScale = _linearTimeScale; } ok = true; float linearDistance = EntityActionInterface::extractFloatArgument("offset action", arguments, "linearDistance", ok, false); if (!ok) { - linearDistance = 0.0f; + linearDistance = _linearDistance; } // only change stuff if something actually changed From 56f00a526d1403a1844156794e78704222f88fda Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Fri, 10 Jul 2015 11:31:37 -0700 Subject: [PATCH 095/107] cleanup of ObjectActionSpring --- libraries/physics/src/ObjectActionSpring.cpp | 189 ++++++++----------- libraries/physics/src/ObjectActionSpring.h | 1 + 2 files changed, 78 insertions(+), 112 deletions(-) diff --git a/libraries/physics/src/ObjectActionSpring.cpp b/libraries/physics/src/ObjectActionSpring.cpp index cb1dd20472..21e3c19bea 100644 --- a/libraries/physics/src/ObjectActionSpring.cpp +++ b/libraries/physics/src/ObjectActionSpring.cpp @@ -17,14 +17,16 @@ const float SPRING_MAX_SPEED = 10.0f; const uint16_t ObjectActionSpring::springVersion = 1; + ObjectActionSpring::ObjectActionSpring(const QUuid& id, EntityItemPointer ownerEntity) : ObjectAction(ACTION_TYPE_SPRING, id, ownerEntity), _positionalTarget(glm::vec3(0.0f)), - _linearTimeScale(0.2f), - _positionalTargetSet(false), + _linearTimeScale(FLT_MAX), + _positionalTargetSet(true), _rotationalTarget(glm::quat()), - _angularTimeScale(0.2f), - _rotationalTargetSet(false) { + _angularTimeScale(FLT_MAX), + _rotationalTargetSet(true), + _needsActivation(true) { #if WANT_DEBUG qDebug() << "ObjectActionSpring::ObjectActionSpring"; #endif @@ -61,130 +63,97 @@ void ObjectActionSpring::updateActionWorker(btScalar deltaTimeStep) { return; } - // handle the linear part - if (_positionalTargetSet) { - // check for NaN - if (_positionalTarget.x != _positionalTarget.x || - _positionalTarget.y != _positionalTarget.y || - _positionalTarget.z != _positionalTarget.z) { - qDebug() << "ObjectActionSpring::updateActionWorker -- target position includes NaN"; - unlock(); - lockForWrite(); - _active = false; - unlock(); - return; - } - glm::vec3 offset = _positionalTarget - bulletToGLM(rigidBody->getCenterOfMassPosition()); - float offsetLength = glm::length(offset); - float speed = offsetLength / _linearTimeScale; - - // cap speed - if (speed > SPRING_MAX_SPEED) { - speed = SPRING_MAX_SPEED; - } - - if (offsetLength > IGNORE_POSITION_DELTA) { - glm::vec3 newVelocity = glm::normalize(offset) * speed; - rigidBody->setLinearVelocity(glmToBullet(newVelocity)); - rigidBody->activate(); - } else { - rigidBody->setLinearVelocity(glmToBullet(glm::vec3(0.0f))); - } + const float MAX_TIMESCALE = 600.0f; // 10 min is a long time + if (_linearTimeScale < MAX_TIMESCALE) { + btVector3 offset = rigidBody->getCenterOfMassPosition() - glmToBullet(_positionalTarget); + float offsetLength = offset.length(); + //float blend = glm::min(1.0f, deltaTimeStep / _linearTimeScale); + float blend = 1.0f; + float speed = (offsetLength > FLT_EPSILON) ? glm::min(offsetLength / _linearTimeScale, SPRING_MAX_SPEED) : 0.0f; + rigidBody->setLinearVelocity((1.0f - blend) * rigidBody->getLinearVelocity() - (blend * speed / (offsetLength * _linearTimeScale)) * offset); } - // handle rotation - if (_rotationalTargetSet) { - if (_rotationalTarget.x != _rotationalTarget.x || - _rotationalTarget.y != _rotationalTarget.y || - _rotationalTarget.z != _rotationalTarget.z || - _rotationalTarget.w != _rotationalTarget.w) { - qDebug() << "AvatarActionHold::updateActionWorker -- target rotation includes NaN"; - unlock(); - lockForWrite(); - _active = false; - unlock(); - return; - } + if (_angularTimeScale < MAX_TIMESCALE) { + btVector3 targetVelocity(0.0f, 0.0f, 0.0f); - glm::quat bodyRotation = bulletToGLM(rigidBody->getOrientation()); - // if qZero and qOne are too close to each other, we can get NaN for angle. - auto alignmentDot = glm::dot(bodyRotation, _rotationalTarget); - const float almostOne = 0.99999f; - if (glm::abs(alignmentDot) < almostOne) { - glm::quat target = _rotationalTarget; - if (alignmentDot < 0) { + btQuaternion bodyRotation = rigidBody->getOrientation(); + auto alignmentDot = bodyRotation.dot(glmToBullet(_rotationalTarget)); + const float ALMOST_ONE = 0.99999f; + if (glm::abs(alignmentDot) < ALMOST_ONE) { + btQuaternion target = glmToBullet(_rotationalTarget); + if (alignmentDot < 0.0f) { target = -target; } - glm::quat qZeroInverse = glm::inverse(bodyRotation); - glm::quat deltaQ = target * qZeroInverse; - glm::vec3 axis = glm::axis(deltaQ); - float angle = glm::angle(deltaQ); - assert(!isNaN(angle)); - glm::vec3 newAngularVelocity = (angle / _angularTimeScale) * glm::normalize(axis); - rigidBody->setAngularVelocity(glmToBullet(newAngularVelocity)); - rigidBody->activate(); - } else { - rigidBody->setAngularVelocity(glmToBullet(glm::vec3(0.0f))); + // if dQ is the incremental rotation that gets an object from Q0 to Q1 then: + // + // Q1 = dQ * Q0 + // + // solving for dQ gives: + // + // dQ = Q1 * Q0^ + btQuaternion deltaQ = target * bodyRotation.inverse(); + float angle = deltaQ.getAngle(); + const float MIN_ANGLE = 1.0e-4; + if (angle > MIN_ANGLE) { + targetVelocity = (angle / _angularTimeScale) * deltaQ.getAxis(); + } } + //float blend = glm::min(1.0f, deltaTimeStep / _angularTimeScale); + float blend = 1.0f; + rigidBody->setAngularVelocity((1.0f - blend) * rigidBody->getAngularVelocity() + blend * targetVelocity); + } + if (_needsActivation) { + rigidBody->activate(); + _needsActivation = false; } - unlock(); } +const float MIN_TIMESCALE = 0.1f; bool ObjectActionSpring::updateArguments(QVariantMap arguments) { // targets are required, spring-constants are optional - bool ptOk = true; + bool ok = true; glm::vec3 positionalTarget = - EntityActionInterface::extractVec3Argument("spring action", arguments, "targetPosition", ptOk, false); - bool pscOk = true; + EntityActionInterface::extractVec3Argument("spring action", arguments, "targetPosition", ok, false); + if (!ok) { + positionalTarget = _positionalTarget; + } + ok = true; float linearTimeScale = - EntityActionInterface::extractFloatArgument("spring action", arguments, "linearTimeScale", pscOk, false); - if (ptOk && pscOk && linearTimeScale <= 0.0f) { - qDebug() << "spring action -- linearTimeScale must be greater than zero."; - return false; + EntityActionInterface::extractFloatArgument("spring action", arguments, "linearTimeScale", ok, false); + if (!ok || linearTimeScale <= 0.0f) { + linearTimeScale = _linearTimeScale; } - bool rtOk = true; + ok = true; glm::quat rotationalTarget = - EntityActionInterface::extractQuatArgument("spring action", arguments, "targetRotation", rtOk, false); - bool rscOk = true; + EntityActionInterface::extractQuatArgument("spring action", arguments, "targetRotation", ok, false); + if (!ok) { + rotationalTarget = _rotationalTarget; + } + + ok = true; float angularTimeScale = - EntityActionInterface::extractFloatArgument("spring action", arguments, "angularTimeScale", rscOk, false); - - if (!ptOk && !rtOk) { - qDebug() << "spring action requires at least one of targetPosition or targetRotation argument"; - return false; + EntityActionInterface::extractFloatArgument("spring action", arguments, "angularTimeScale", ok, false); + if (!ok) { + angularTimeScale = _angularTimeScale; } - lockForWrite(); - - _positionalTargetSet = _rotationalTargetSet = false; - - if (ptOk) { + if (positionalTarget != _positionalTarget + || linearTimeScale != _linearTimeScale + || rotationalTarget != _rotationalTarget + || angularTimeScale != _angularTimeScale) { + // something changed + lockForWrite(); _positionalTarget = positionalTarget; - _positionalTargetSet = true; - - if (pscOk) { - _linearTimeScale = linearTimeScale; - } else { - _linearTimeScale = 0.1f; - } - } - - if (rtOk) { + _linearTimeScale = glm::max(MIN_TIMESCALE, glm::abs(linearTimeScale)); _rotationalTarget = rotationalTarget; - _rotationalTargetSet = true; - - if (rscOk) { - _angularTimeScale = angularTimeScale; - } else { - _angularTimeScale = 0.1f; - } + _angularTimeScale = glm::max(MIN_TIMESCALE, glm::abs(angularTimeScale)); + _active = true; + _needsActivation = true; + unlock(); } - - _active = true; - unlock(); return true; } @@ -192,15 +161,11 @@ QVariantMap ObjectActionSpring::getArguments() { QVariantMap arguments; lockForRead(); - if (_positionalTargetSet) { - arguments["linearTimeScale"] = _linearTimeScale; - arguments["targetPosition"] = glmToQMap(_positionalTarget); - } + arguments["linearTimeScale"] = _linearTimeScale; + arguments["targetPosition"] = glmToQMap(_positionalTarget); - if (_rotationalTargetSet) { - arguments["targetRotation"] = glmToQMap(_rotationalTarget); - arguments["angularTimeScale"] = _angularTimeScale; - } + arguments["targetRotation"] = glmToQMap(_rotationalTarget); + arguments["angularTimeScale"] = _angularTimeScale; unlock(); return arguments; diff --git a/libraries/physics/src/ObjectActionSpring.h b/libraries/physics/src/ObjectActionSpring.h index caa64c3d3a..a0a829bdab 100644 --- a/libraries/physics/src/ObjectActionSpring.h +++ b/libraries/physics/src/ObjectActionSpring.h @@ -37,6 +37,7 @@ protected: glm::quat _rotationalTarget; float _angularTimeScale; bool _rotationalTargetSet; + bool _needsActivation; }; #endif // hifi_ObjectActionSpring_h From 294c1ba367fe5de853bd9f3b91de62a60150ae17 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Fri, 10 Jul 2015 12:17:21 -0700 Subject: [PATCH 096/107] always serialize ACTION_TYPE_SPRING --- libraries/physics/src/ObjectActionSpring.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/physics/src/ObjectActionSpring.cpp b/libraries/physics/src/ObjectActionSpring.cpp index 21e3c19bea..c1ef94296a 100644 --- a/libraries/physics/src/ObjectActionSpring.cpp +++ b/libraries/physics/src/ObjectActionSpring.cpp @@ -175,7 +175,7 @@ QByteArray ObjectActionSpring::serialize() const { QByteArray serializedActionArguments; QDataStream dataStream(&serializedActionArguments, QIODevice::WriteOnly); - dataStream << getType(); + dataStream << ACTION_TYPE_SPRING; dataStream << getID(); dataStream << ObjectActionSpring::springVersion; @@ -195,7 +195,7 @@ void ObjectActionSpring::deserialize(QByteArray serializedArguments) { EntityActionType type; dataStream >> type; - assert(type == getType()); + assert(type == ACTION_TYPE_SPRING); QUuid id; dataStream >> id; From 9cb7e86877f297944e845a919f4d5b3362dc28d0 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Fri, 10 Jul 2015 12:18:09 -0700 Subject: [PATCH 097/107] cleanup of AvatarActionHold deserialization --- interface/src/avatar/AvatarActionHold.cpp | 100 ++++++++-------------- 1 file changed, 38 insertions(+), 62 deletions(-) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index ba37112fe1..1e75250ee2 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -70,30 +70,14 @@ void AvatarActionHold::updateActionWorker(float deltaTimeStep) { return; } - // check for NaNs - if (position.x != position.x || - position.y != position.y || - position.z != position.z) { - qDebug() << "AvatarActionHold::updateActionWorker -- target position includes NaN"; - return; - } - if (rotation.x != rotation.x || - rotation.y != rotation.y || - rotation.z != rotation.z || - rotation.w != rotation.w) { - qDebug() << "AvatarActionHold::updateActionWorker -- target rotation includes NaN"; - return; - } - if (_positionalTarget != position || _rotationalTarget != rotation) { auto ownerEntity = _ownerEntity.lock(); if (ownerEntity) { ownerEntity->setActionDataDirty(true); } + _positionalTarget = position; + _rotationalTarget = rotation; } - - _positionalTarget = position; - _rotationalTarget = rotation; unlock(); ObjectActionSpring::updateActionWorker(deltaTimeStep); @@ -101,59 +85,51 @@ void AvatarActionHold::updateActionWorker(float deltaTimeStep) { bool AvatarActionHold::updateArguments(QVariantMap arguments) { - bool rPOk = true; + bool ok = true; glm::vec3 relativePosition = - EntityActionInterface::extractVec3Argument("hold", arguments, "relativePosition", rPOk, false); - bool rROk = true; + EntityActionInterface::extractVec3Argument("hold", arguments, "relativePosition", ok, false); + if (!ok) { + relativePosition = _relativePosition; + } + + ok = true; glm::quat relativeRotation = - EntityActionInterface::extractQuatArgument("hold", arguments, "relativeRotation", rROk, false); - bool tSOk = true; + EntityActionInterface::extractQuatArgument("hold", arguments, "relativeRotation", ok, false); + if (!ok) { + relativeRotation = _relativeRotation; + } + + ok = true; float timeScale = - EntityActionInterface::extractFloatArgument("hold", arguments, "timeScale", tSOk, false); - bool hOk = true; + EntityActionInterface::extractFloatArgument("hold", arguments, "timeScale", ok, false); + if (!ok) { + timeScale = _linearTimeScale; + } + + ok = true; QString hand = - EntityActionInterface::extractStringArgument("hold", arguments, "hand", hOk, false); + EntityActionInterface::extractStringArgument("hold", arguments, "hand", ok, false); + if (!ok || !(hand == "left" || hand == "right")) { + hand = _hand; + } - lockForWrite(); - if (rPOk) { + if (relativePosition != _relativePosition + || relativeRotation != _relativeRotation + || timeScale != _linearTimeScale + || hand != _hand) { + lockForWrite(); _relativePosition = relativePosition; - } else { - _relativePosition = glm::vec3(0.0f, 0.0f, 1.0f); - } - - if (rROk) { _relativeRotation = relativeRotation; - } else { - _relativeRotation = glm::quat(0.0f, 0.0f, 0.0f, 1.0f); - } + const float MIN_TIMESCALE = 0.1f; + _linearTimeScale = glm::min(MIN_TIMESCALE, timeScale); + _angularTimeScale = _linearTimeScale; + _hand = hand; - if (tSOk) { - _linearTimeScale = timeScale; - _angularTimeScale = timeScale; - } else { - _linearTimeScale = 0.2f; - _angularTimeScale = 0.2f; + _mine = true; + _active = true; + _needsActivation = true; + unlock(); } - - if (hOk) { - hand = hand.toLower(); - if (hand == "left") { - _hand = "left"; - } else if (hand == "right") { - _hand = "right"; - } else { - qDebug() << "hold action -- invalid hand argument:" << hand; - _hand = "right"; - } - } else { - _hand = "right"; - } - - _mine = true; - _positionalTargetSet = true; - _rotationalTargetSet = true; - _active = true; - unlock(); return true; } From 1dd40af16242e4343dba5a728e5d7a3b7917415e Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Fri, 10 Jul 2015 12:43:35 -0700 Subject: [PATCH 098/107] fix AnimationLoop::setRunning() to not constantly reset the frame when the running state doesn't actually change --- libraries/animation/src/AnimationLoop.cpp | 10 +++++----- libraries/entities/src/EntityItemProperties.cpp | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/libraries/animation/src/AnimationLoop.cpp b/libraries/animation/src/AnimationLoop.cpp index e60df1eaf9..43e049f851 100644 --- a/libraries/animation/src/AnimationLoop.cpp +++ b/libraries/animation/src/AnimationLoop.cpp @@ -84,14 +84,14 @@ void AnimationLoop::setStartAutomatically(bool startAutomatically) { } void AnimationLoop::setRunning(bool running) { - if (_running == running) { + // don't do anything if the new value is the same as the value we already have + if (_running != running) { + _running = running; + + // If we just set running to true, then also reset the frame to the first frame if (running) { // move back to the beginning _frameIndex = _firstFrame; } - return; - } - if ((_running = running)) { - _frameIndex = _firstFrame; } } diff --git a/libraries/entities/src/EntityItemProperties.cpp b/libraries/entities/src/EntityItemProperties.cpp index 9a1a5494b7..106e0a6b8b 100644 --- a/libraries/entities/src/EntityItemProperties.cpp +++ b/libraries/entities/src/EntityItemProperties.cpp @@ -128,6 +128,10 @@ void EntityItemProperties::setSittingPoints(const QVector& sitting } } +bool EntityItemProperties::animationSettingsChanged() const { + return _animationSettingsChanged; +} + void EntityItemProperties::setAnimationSettings(const QString& value) { // the animations setting is a JSON string that may contain various animation settings. // if it includes fps, frameIndex, or running, those values will be parsed out and From 2ce8dba8193222f6a48176e7623a5d0133c886a6 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Fri, 10 Jul 2015 15:06:38 -0700 Subject: [PATCH 099/107] Removing heap allocation from property flag parsing, adding some manual tests --- libraries/shared/src/BufferParser.h | 5 +- libraries/shared/src/PropertyFlags.h | 92 ++++++++------- tests/entities/CMakeLists.txt | 12 ++ tests/entities/packet.bin | Bin 0 -> 1387 bytes tests/entities/src/main.cpp | 165 +++++++++++++++++++++++++++ 5 files changed, 231 insertions(+), 43 deletions(-) create mode 100644 tests/entities/CMakeLists.txt create mode 100644 tests/entities/packet.bin create mode 100644 tests/entities/src/main.cpp diff --git a/libraries/shared/src/BufferParser.h b/libraries/shared/src/BufferParser.h index 84bde2be31..d60e7127cd 100644 --- a/libraries/shared/src/BufferParser.h +++ b/libraries/shared/src/BufferParser.h @@ -44,10 +44,7 @@ public: template inline void readFlags(PropertyFlags& result) { - // FIXME doing heap allocation - QByteArray encoded((const char*)(_data + _offset), remaining()); - result.decode(encoded); - _offset += result.getEncodedLength(); + _offset += result.decode(_data + _offset, remaining()); } template diff --git a/libraries/shared/src/PropertyFlags.h b/libraries/shared/src/PropertyFlags.h index de05edc076..0202784c77 100644 --- a/libraries/shared/src/PropertyFlags.h +++ b/libraries/shared/src/PropertyFlags.h @@ -25,6 +25,7 @@ #include #include +#include "ByteCountCoding.h" #include templateclass PropertyFlags { @@ -51,7 +52,8 @@ public: void setHasProperty(Enum flag, bool value = true); bool getHasProperty(Enum flag) const; QByteArray encode(); - void decode(const QByteArray& fromEncoded); + size_t decode(const uint8_t* data, size_t length); + size_t decode(const QByteArray& fromEncoded); operator QByteArray() { return encode(); }; @@ -193,51 +195,63 @@ template inline QByteArray PropertyFlags::encode() { return output; } -template inline void PropertyFlags::decode(const QByteArray& fromEncodedBytes) { +template +inline size_t PropertyFlags::decode(const uint8_t* data, size_t size) { + clear(); + //clear(); // we are cleared out! - clear(); // we are cleared out! + size_t bytesConsumed = 0; + int bitCount = BITS_IN_BYTE * size; - // first convert the ByteArray into a BitArray... - QBitArray encodedBits; - int bitCount = BITS_PER_BYTE * fromEncodedBytes.count(); - encodedBits.resize(bitCount); - - for(int byte = 0; byte < fromEncodedBytes.count(); byte++) { - char originalByte = fromEncodedBytes.at(byte); - for(int bit = 0; bit < BITS_PER_BYTE; bit++) { - int shiftBy = BITS_PER_BYTE - (bit + 1); - char maskBit = ( 1 << shiftBy); - bool bitValue = originalByte & maskBit; - encodedBits.setBit(byte * BITS_PER_BYTE + bit, bitValue); + int encodedByteCount = 1; // there is at least 1 byte (after the leadBits) + int leadBits = 1; // there is always at least 1 lead bit + bool inLeadBits = true; + int bitAt = 0; + int expectedBitCount; // unknown at this point + int lastValueBit; + for (int byte = 0; byte < size; byte++) { + char originalByte = data[byte]; + bytesConsumed++; + unsigned char maskBit = 0x80; // LEFT MOST BIT set + for (int bit = 0; bit < BITS_IN_BYTE; bit++) { + bool bitIsSet = originalByte & maskBit; + // Processing of the lead bits + if (inLeadBits) { + if (bitIsSet) { + encodedByteCount++; + leadBits++; + } else { + inLeadBits = false; // once we hit our first 0, we know we're out of the lead bits + expectedBitCount = (encodedByteCount * BITS_IN_BYTE) - leadBits; + lastValueBit = expectedBitCount + bitAt; + + // check to see if the remainder of our buffer is sufficient + if (expectedBitCount > (bitCount - leadBits)) { + break; + } + } + } else { + if (bitAt > lastValueBit) { + break; + } + + if (bitIsSet) { + setHasProperty(static_cast(bitAt - leadBits), true); + } + } + bitAt++; + maskBit >>= 1; } - } - - // next, read the leading bits to determine the correct number of bytes to decode (may not match the QByteArray) - int encodedByteCount = 0; - int leadBits = 1; - int bitAt; - for (bitAt = 0; bitAt < bitCount; bitAt++) { - if (encodedBits.at(bitAt)) { - encodedByteCount++; - leadBits++; - } else { + if (!inLeadBits && bitAt > lastValueBit) { break; } } - encodedByteCount++; // always at least one byte - _encodedLength = encodedByteCount; + _encodedLength = bytesConsumed; + return bytesConsumed; +} - int expectedBitCount = encodedByteCount * BITS_PER_BYTE; - - // Now, keep reading... - if (expectedBitCount <= (encodedBits.size() - leadBits)) { - int flagsStartAt = bitAt + 1; - for (bitAt = flagsStartAt; bitAt < expectedBitCount; bitAt++) { - if (encodedBits.at(bitAt)) { - setHasProperty((Enum)(bitAt - flagsStartAt)); - } - } - } +template inline size_t PropertyFlags::decode(const QByteArray& fromEncodedBytes) { + return decode(reinterpret_cast(fromEncodedBytes.data()), fromEncodedBytes.size()); } template inline void PropertyFlags::debugDumpBits() { diff --git a/tests/entities/CMakeLists.txt b/tests/entities/CMakeLists.txt new file mode 100644 index 0000000000..44b84dea43 --- /dev/null +++ b/tests/entities/CMakeLists.txt @@ -0,0 +1,12 @@ + +set(TARGET_NAME "entities-test") + +# This is not a testcase -- just set it up as a regular hifi project +setup_hifi_project() + +set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "Tests/manual-tests/") + +# link in the shared libraries +link_hifi_libraries(entities avatars shared octree gpu model fbx networking animation environment) + +copy_dlls_beside_windows_executable() \ No newline at end of file diff --git a/tests/entities/packet.bin b/tests/entities/packet.bin new file mode 100644 index 0000000000000000000000000000000000000000..295117172ded11f54ff48a869fbb449ee8672154 GIT binary patch literal 1387 zcmcJPPiWIn9LIlgRCiF(IjM@c65L_1G;PyvX+g6!VK`@DZU=W6E=!XwlIC?us#|AW zhl*b2pD8jucu-Nni<^Rf4h}(Z!`A89iiqe$chG|}a2iRs)(tylh~MPF%kTFB@B90G z-s>O#(wr%3NT1o=b>}%byv~t)wW)Z>ZiCG5z5#Qi4WP{|n<;}GMblMlp^!L8nk)ct zW?%L(3oZIxW5IMPwGCh_2oUGy=ieFQ+W~-?9m!E_eliyZ##)8}VAcRw4s{Abnl+t$ z0USwlBslez8?Mh>c=Gx1&Sx`iHOY@-Et7T|Y#Etz*J74^GvVNhg6<|OFfP6oICRrw zm*TF8!kF(}40HDAA?DxQS!T*1T+LIoH@r<{C*k0X^opgRQ=AtNX(Dmod}P*f*Fa+noWBc}C) zP*~K|UGjce?Px+MLe{H>sL4npSrm!J3{i7R?cMoGf2z&42m|PcUR;tj-Kf*}h=>PV zmADku@c?B_tWdog7Q3_YPNanKfW#}hScxlq{uj5)QYEe+6wT+gn5xQZNB&zkc+IGd z87RUVvcM}!&s-{z-N_({mrgwhRS%qROk~*8ZyYuEZ@{SC21B{NH~xw0BNZ5@Oqguz zwWaml^*F@f7YCWEt=IE?Pp39NwDvShdk){fQN5s>IiR8R*mUB>$^M4mi7R_cV)V!( zJ`eP&TXR6K$pWnu271qX;JPMEwj^a4drkl7Q$8#>U5hCFwupHeJH=coU7a_U)6$Ns zWu_deyk5y8y4?irrYOQIcm#r$B#{t!pV!L^g5aaw3rYb04d@`RH5&*iivVqD!^-1{ qdoS74x!Y7n^Tf@JBe_0!_p;pvy}6>d&2Uf*u&m$>8Rsxa0>DqVPf;uY literal 0 HcmV?d00001 diff --git a/tests/entities/src/main.cpp b/tests/entities/src/main.cpp new file mode 100644 index 0000000000..8d16d98103 --- /dev/null +++ b/tests/entities/src/main.cpp @@ -0,0 +1,165 @@ +// +// main.cpp +// tests/render-utils/src +// +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +const QString& getTestResourceDir() { + static QString dir; + if (dir.isEmpty()) { + QDir path(__FILE__); + path.cdUp(); + dir = path.cleanPath(path.absoluteFilePath("../")) + "/"; + qDebug() << "Qml Test Path: " << dir; + } + return dir; +} + +class StopWatch { +public: + void start() { + Q_ASSERT(_start == 0); + _start = usecTimestampNow(); + } + + void stop() { + Q_ASSERT(_start != 0); + _last = usecTimestampNow() - _start; + _start = 0; + _total += _last; + _count++; + } + + quint64 getLast() { + return _last; + } + + quint64 getTotal() { + return _total; + } + + float getAverage() { + return (float)_total / (float)_count; + } + + void reset() { + _last = _start = _total = _count = 0; + } + +private: + size_t _count{ 0 }; + quint64 _total{ 0 }; + quint64 _start{ 0 }; + quint64 _last{ 0 }; +}; + +template +void testByteCountCodedStable(const T& value) { + ByteCountCoded coder((T)value); + auto encoded = coder.encode(); + auto originalEncodedSize = encoded.size(); + for (int i = 0; i < 10; ++i) { + encoded.append(qrand()); + } + ByteCountCoded decoder; + decoder.decode(encoded); + Q_ASSERT(decoder.data == coder.data); + auto consumed = decoder.decode(encoded.data(), encoded.size()); + Q_ASSERT(consumed == originalEncodedSize); + +} + +template +void testByteCountCoded() { + testByteCountCodedStable(0); + testByteCountCodedStable(1); + testByteCountCodedStable(1 << 16); + testByteCountCodedStable(std::numeric_limits::max() >> 16); + testByteCountCodedStable(std::numeric_limits::max() >> 8); + testByteCountCodedStable(std::numeric_limits::max() >> 1); + testByteCountCodedStable(std::numeric_limits::max()); +} + +void testPropertyFlags(uint32_t value) { + EntityPropertyFlags original; + original.clear(); + auto enumSize = sizeof(EntityPropertyList); + for (size_t i = 0; i < sizeof(EntityPropertyList) * 8; ++i) { + original.setHasProperty((EntityPropertyList)i); + } + QByteArray encoded = original.encode(); + auto originalSize = encoded.size(); + for (size_t i = 0; i < sizeof(EntityPropertyList); ++i) { + encoded.append(qrand()); + } + + EntityPropertyFlags decodeOld, decodeNew; + { + decodeOld.decode(encoded); + Q_ASSERT(decodeOld == original); + } + + { + auto decodeSize = decodeNew.decode((const uint8_t*)encoded.data(), encoded.size()); + Q_ASSERT(originalSize == decodeSize); + Q_ASSERT(decodeNew == original); + } +} + +void testPropertyFlags() { + testPropertyFlags(0); + testPropertyFlags(1); + testPropertyFlags(1 << 16); + testPropertyFlags(0xFFFF); +} + +int main(int argc, char** argv) { + QCoreApplication app(argc, argv); + { + auto start = usecTimestampNow(); + for (int i = 0; i < 1000; ++i) { + testPropertyFlags(); + testByteCountCoded(); + testByteCountCoded(); + testByteCountCoded(); + testByteCountCoded(); + } + auto duration = usecTimestampNow() - start; + qDebug() << duration; + + } + DependencyManager::set(NodeType::Unassigned); + + QFile file(getTestResourceDir() + "packet.bin"); + if (!file.open(QIODevice::ReadOnly)) return -1; + QByteArray packet = file.readAll(); + EntityItemPointer item = BoxEntityItem::factory(EntityItemID(), EntityItemProperties()); + ReadBitstreamToTreeParams params; + params.bitstreamVersion = 33; + + auto start = usecTimestampNow(); + for (int i = 0; i < 1000; ++i) { + item->readEntityDataFromBuffer(reinterpret_cast(packet.constData()), packet.size(), params); + } + float duration = (usecTimestampNow() - start); + qDebug() << (duration / 1000.0f); + return 0; +} + +#include "main.moc" From 98f10e72ec4f5662604d2b93c5357e538e002c34 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Fri, 10 Jul 2015 15:20:49 -0700 Subject: [PATCH 100/107] fix the wrong function name that happened missing a merge --- interface/src/audio/AudioScope.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interface/src/audio/AudioScope.cpp b/interface/src/audio/AudioScope.cpp index 7a93be80f1..4b4e86e7f4 100644 --- a/interface/src/audio/AudioScope.cpp +++ b/interface/src/audio/AudioScope.cpp @@ -130,7 +130,7 @@ void AudioScope::render(RenderArgs* renderArgs, int width, int height) { auto geometryCache = DependencyManager::get(); geometryCache->useSimpleDrawPipeline(batch); auto textureCache = DependencyManager::get(); - batch.setUniformTexture(0, textureCache->getWhiteTexture()); + batch.setResourceTexture(0, textureCache->getWhiteTexture()); mat4 legacyProjection = glm::ortho(0, width, height, 0, -1000, 1000); batch.setProjectionTransform(legacyProjection); batch.setModelTransform(Transform()); From 3db3676fae7d6b794ab55b58198998a4de47facd Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Fri, 10 Jul 2015 15:26:22 -0700 Subject: [PATCH 101/107] Fixing compile failure --- tests/entities/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/entities/CMakeLists.txt b/tests/entities/CMakeLists.txt index 44b84dea43..23c02a97f8 100644 --- a/tests/entities/CMakeLists.txt +++ b/tests/entities/CMakeLists.txt @@ -2,7 +2,7 @@ set(TARGET_NAME "entities-test") # This is not a testcase -- just set it up as a regular hifi project -setup_hifi_project() +setup_hifi_project(Script) set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "Tests/manual-tests/") From c340d336dcfb319dee2b84b528687a0c95658f1b Mon Sep 17 00:00:00 2001 From: Howard Stearns Date: Fri, 10 Jul 2015 15:43:56 -0700 Subject: [PATCH 102/107] Functional sword script: Mouse and hydra. Switchable hands. Scores above buttons (2d) and above head in-world. Adds avatar hit sound while sword is brandished. --- examples/example/games/sword.js | 259 +++++++++++++++++++------------- 1 file changed, 156 insertions(+), 103 deletions(-) diff --git a/examples/example/games/sword.js b/examples/example/games/sword.js index 66503b62aa..18d6911f0b 100644 --- a/examples/example/games/sword.js +++ b/examples/example/games/sword.js @@ -11,24 +11,26 @@ // "use strict"; /*jslint vars: true*/ -var Script, Entities, MyAvatar, Window, Overlays, Controller, Vec3, Quat, print, ToolBar; // Referenced globals provided by High Fidelity. -Script.include(["../../libraries/toolBars.js"]); +var Script, Entities, MyAvatar, Window, Overlays, Controller, Vec3, Quat, print, ToolBar, Settings; // Referenced globals provided by High Fidelity. +Script.include("http://s3.amazonaws.com/hifi-public/scripts/libraries/toolBars.js"); -var hand = "right"; +var hand = Settings.getValue("highfidelity.sword.hand", "right"); var nullActionID = "00000000-0000-0000-0000-000000000000"; var controllerID; var controllerActive; var stickID = null; var actionID = nullActionID; var targetIDs = []; -var dimensions = { x: 0.3, y: 0.1, z: 2.0 }; -var AWAY_ORIENTATION = Quat.fromPitchYawRollDegrees(-90, 0, 0); +var dimensions = { x: 0.3, y: 0.15, z: 2.0 }; var BUTTON_SIZE = 32; var stickModel = "https://hifi-public.s3.amazonaws.com/eric/models/stick.fbx"; var swordModel = "https://hifi-public.s3.amazonaws.com/ozan/props/sword/sword.fbx"; +var swordCollisionShape = "https://hifi-public.s3.amazonaws.com/ozan/props/sword/sword.obj"; +var swordCollisionSoundURL = "http://public.highfidelity.io/sounds/Collisions-hitsandslaps/swordStrike1.wav"; +var avatarCollisionSoundURL = "https://s3.amazonaws.com/hifi-public/sounds/Collisions-hitsandslaps/airhockey_hit1.wav"; var whichModel = "sword"; -var attachmentOffset, MOUSE_CONTROLLER_OFFSET = {x: 0.5, y: 0.4, z: 0.0}; // A fudge when using mouse rather than hand-controller, to hit yourself less often. +var originalAvatarCollisionSound; var toolBar = new ToolBar(0, 0, ToolBar.vertical, "highfidelity.sword.toolbar", function () { return {x: 100, y: 380}; @@ -37,6 +39,7 @@ var toolBar = new ToolBar(0, 0, ToolBar.vertical, "highfidelity.sword.toolbar", var SWORD_IMAGE = "http://s3.amazonaws.com/hifi-public/images/billiardsReticle.png"; // Toggle between brandishing/sheathing sword (creating if necessary) var TARGET_IMAGE = "http://s3.amazonaws.com/hifi-public/images/puck.png"; // Create a target dummy var CLEANUP_IMAGE = "http://s3.amazonaws.com/hifi-public/images/delete.png"; // Remove sword and all target dummies.f +var SWITCH_HANDS_IMAGE = "http://s3.amazonaws.com/hifi-public/images/up-arrow.svg"; // Toggle left vs right hand. Persists in settings. var swordButton = toolBar.addOverlay("image", { width: BUTTON_SIZE, height: BUTTON_SIZE, @@ -49,6 +52,12 @@ var targetButton = toolBar.addOverlay("image", { imageURL: TARGET_IMAGE, alpha: 1 }); +var switchHandsButton = toolBar.addOverlay("image", { + width: BUTTON_SIZE, + height: BUTTON_SIZE, + imageURL: SWITCH_HANDS_IMAGE, + alpha: 1 +}); var cleanupButton = toolBar.addOverlay("image", { width: BUTTON_SIZE, height: BUTTON_SIZE, @@ -77,53 +86,51 @@ function flash(color) { flasher.timer = Script.setTimeout(clearFlash, 500); } - var health = 100; -var display; -var isAway = false; +var display2d, display3d; +function trackAvatarWithText() { + Entities.editEntity(display3d, { + position: Vec3.sum(MyAvatar.position, {x: 0, y: 1.5, z: 0}), + rotation: Quat.multiply(MyAvatar.orientation, Quat.fromPitchYawRollDegrees(0, 180, 0)) + }); +} function updateDisplay() { var text = health.toString(); - if (!display) { + if (!display2d) { health = 100; - display = Overlays.addOverlay("text", { + display2d = Overlays.addOverlay("text", { text: text, font: { size: 20 }, color: {red: 0, green: 255, blue: 0}, backgroundColor: {red: 100, green: 100, blue: 100}, // Why doesn't this and the next work? backgroundAlpha: 0.9, - x: Window.innerWidth - 50, - y: 50 + x: toolBar.x - 5, // I'd like to add the score to the toolBar and have it drag with it, but toolBar doesn't support text (just buttons). + y: toolBar.y - 30 // So next best thing is to position it each time as if it were on top. }); + display3d = Entities.addEntity({ + name: MyAvatar.displayName + " score", + textColor: {red: 255, green: 255, blue: 255}, + type: "Text", + text: text, + lineHeight: 0.14, + backgroundColor: {red: 64, green: 64, blue: 64}, + dimensions: {x: 0.3, y: 0.2, z: 0.01}, + }); + Script.update.connect(trackAvatarWithText); } else { - Overlays.editOverlay(display, {text: text}); + Overlays.editOverlay(display2d, {text: text}); + Entities.editEntity(display3d, {text: text}); } } function removeDisplay() { - if (display) { - Overlays.deleteOverlay(display); - display = null; + if (display2d) { + Overlays.deleteOverlay(display2d); + display2d = null; + Script.update.disconnect(trackAvatarWithText); + Entities.deleteEntity(display3d); + display3d = null; } } - -function cleanUp(leaveButtons) { - attachmentOffset = {x: 0, y: 0, z: 0}; - if (stickID) { - Entities.deleteAction(stickID, actionID); - Entities.deleteEntity(stickID); - stickID = null; - actionID = null; - } - targetIDs.forEach(function (id) { - Entities.deleteAction(id.entity, id.action); - Entities.deleteEntity(id.entity); - }); - targetIDs = []; - removeDisplay(); - if (!leaveButtons) { - toolBar.cleanup(); - } -} - function computeEnergy(collision, entityID) { var id = entityID || collision.idA || collision.idB; var entity = id && Entities.getEntityProperties(id); @@ -133,31 +140,67 @@ function computeEnergy(collision, entityID) { return Math.min(Math.max(1.0, Math.round(energy)), 20); } function gotHit(collision) { - if (isAway) { return; } var energy = computeEnergy(collision); + print("Got hit - " + energy + " from " + collision.idA + " " + collision.idB); health -= energy; flash({red: 255, green: 0, blue: 0}); updateDisplay(); } function scoreHit(idA, idB, collision) { - if (isAway) { return; } var energy = computeEnergy(collision, idA); + print("Score + " + energy + " from " + JSON.stringify(idA) + " " + JSON.stringify(idB)); health += energy; flash({red: 0, green: 255, blue: 0}); updateDisplay(); } -function positionStick(stickOrientation) { - var baseOffset = Vec3.sum(attachmentOffset, {x: 0.0, y: 0.0, z: -dimensions.z / 2}); - var offset = Vec3.multiplyQbyV(stickOrientation, baseOffset); - Entities.updateAction(stickID, actionID, {relativePosition: offset, - relativeRotation: stickOrientation}); +function isFighting() { + return stickID && (actionID !== nullActionID); } - +function initControls() { + print("Sword hand is " + hand); + if (hand === "right") { + controllerID = 3; // right handed + } else { + controllerID = 4; // left handed + } +} +var inHand = false; +function positionStick(stickOrientation) { + var reorient = Quat.fromPitchYawRollDegrees(0, -90, 0); + var baseOffset = {x: -dimensions.z * 0.8, y: 0, z: 0}; + var offset = Vec3.multiplyQbyV(reorient, baseOffset); + stickOrientation = Quat.multiply(reorient, stickOrientation); + inHand = false; + Entities.updateAction(stickID, actionID, { + relativePosition: offset, + relativeRotation: stickOrientation + }); +} +function resetToHand() { // Maybe coordinate with positionStick? + if (inHand) { // Optimization: bail if we're already inHand. + return; + } + print('Reset to hand'); + Entities.updateAction(stickID, actionID, { + relativePosition: {x: 0.0, y: 0.0, z: -dimensions.z * 0.5}, + relativeRotation: Quat.fromVec3Degrees({x: 45.0, y: 0.0, z: 0.0}), + hand: hand, // It should not be necessary to repeat these two, but there seems to be a bug in that that + timeScale: 0.05 // they do not retain their earlier values if you don't repeat them. + }); + inHand = true; +} function mouseMoveEvent(event) { - attachmentOffset = MOUSE_CONTROLLER_OFFSET; - if (!stickID || actionID === nullActionID || isAway) { + if (event.deviceID) { // Not a MOUSE mouse event, but a (e.g., hydra) mouse event, with x/y that is not meaningful for us. + resetToHand(); // Can only happen when controller is uncradled, so let's drive with that, resetting our attachement. + return; + } + controllerActive = (Vec3.length(Controller.getSpatialControlPosition(controllerID)) > 0); + //print("Mouse move with hand controller " + (controllerActive ? "active" : "inactive") + JSON.stringify(event)); + if (controllerActive || !isFighting()) { + print('Attempting attachment reset'); + resetToHand(); return; } var windowCenterX = Window.innerWidth / 2; @@ -167,73 +210,80 @@ function mouseMoveEvent(event) { var mouseXRatio = mouseXCenterOffset / windowCenterX; var mouseYRatio = mouseYCenterOffset / windowCenterY; - var stickOrientation = Quat.fromPitchYawRollDegrees(mouseYRatio * -90, mouseXRatio * -90, 0); + var stickOrientation = Quat.fromPitchYawRollDegrees(mouseYRatio * 90, mouseXRatio * 90, 0); positionStick(stickOrientation); } - -function initControls() { - if (hand === "right") { - controllerID = 3; // right handed - } else { - controllerID = 4; // left handed +function removeSword() { + if (stickID) { + print('deleting action ' + actionID + ' and entity ' + stickID); + Entities.deleteAction(stickID, actionID); + Entities.deleteEntity(stickID); + stickID = null; + actionID = nullActionID; + Controller.mouseMoveEvent.disconnect(mouseMoveEvent); + MyAvatar.collisionWithEntity.disconnect(gotHit); + // removeEventhHandler happens automatically when the entity is deleted. + } + inHand = false; + if (originalAvatarCollisionSound !== undefined) { + MyAvatar.collisionSoundURL = originalAvatarCollisionSound; + } + removeDisplay(); +} +function cleanUp(leaveButtons) { + removeSword(); + targetIDs.forEach(function (id) { + Entities.deleteAction(id.entity, id.action); + Entities.deleteEntity(id.entity); + }); + targetIDs = []; + if (!leaveButtons) { + toolBar.cleanup(); } } - - -function update() { - var palmPosition = Controller.getSpatialControlPosition(controllerID); - controllerActive = (Vec3.length(palmPosition) > 0); - if (!controllerActive) { - return; +function makeSword() { + initControls(); + stickID = Entities.addEntity({ + type: "Model", + modelURL: swordModel, + compoundShapeURL: swordCollisionShape, + dimensions: dimensions, + position: (hand === 'right') ? MyAvatar.getRightPalmPosition() : MyAvatar.getLeftPalmPosition(), // initial position doesn't matter, as long as it's close + rotation: MyAvatar.orientation, + damping: 0.1, + collisionSoundURL: swordCollisionSoundURL, + restitution: 0.01, + collisionsWillMove: true + }); + actionID = Entities.addAction("hold", stickID, { + relativePosition: {x: 0.0, y: 0.0, z: -dimensions.z * 0.5}, + relativeRotation: Quat.fromVec3Degrees({x: 45.0, y: 0.0, z: 0.0}), + hand: hand, + timeScale: 0.05 + }); + if (actionID === nullActionID) { + print('*** FAILED TO MAKE SWORD ACTION ***'); + cleanUp(); } - - var stickOrientation = Controller.getSpatialControlRawRotation(controllerID); - var adjustment = Quat.fromPitchYawRollDegrees(180, 0, 0); - stickOrientation = Quat.multiply(stickOrientation, adjustment); - - positionStick(stickOrientation); -} - -function toggleAway() { - isAway = !isAway; - if (isAway) { - positionStick(AWAY_ORIENTATION); - removeDisplay(); - } else { - updateDisplay(); + if (originalAvatarCollisionSound === undefined) { + originalAvatarCollisionSound = MyAvatar.collisionSoundURL; // We won't get MyAvatar.collisionWithEntity unless there's a sound URL. (Bug.) + SoundCache.getSound(avatarCollisionSoundURL); // Interface does not currently "preload" this? (Bug?) } + MyAvatar.collisionSoundURL = avatarCollisionSoundURL; + Controller.mouseMoveEvent.connect(mouseMoveEvent); + MyAvatar.collisionWithEntity.connect(gotHit); + Script.addEventHandler(stickID, 'collisionWithEntity', scoreHit); + updateDisplay(); } function onClick(event) { switch (Overlays.getOverlayAtPoint(event)) { case swordButton: if (!stickID) { - initControls(); - stickID = Entities.addEntity({ - type: "Model", - modelURL: (whichModel === "sword") ? swordModel : stickModel, - //compoundShapeURL: "https://hifi-public.s3.amazonaws.com/eric/models/stick.obj", - shapeType: "box", - dimensions: dimensions, - position: MyAvatar.getRightPalmPosition(), // initial position doesn't matter, as long as it's close - rotation: MyAvatar.orientation, - damping: 0.1, - collisionSoundURL: "http://public.highfidelity.io/sounds/Collisions-hitsandslaps/swordStrike1.wav", - restitution: 0.01, - collisionsWillMove: true - }); - actionID = Entities.addAction("hold", stickID, {relativePosition: {x: 0.0, y: 0.0, z: -dimensions.z / 2}, - hand: hand, - timeScale: 0.15}); - if (actionID === nullActionID) { - print('*** FAILED TO MAKE SWORD ACTION ***'); - cleanUp(); - } - Script.addEventHandler(stickID, 'collisionWithEntity', scoreHit); - updateDisplay(); + makeSword(); } else { - toggleAway(); + removeSword(); } break; case targetButton: @@ -256,6 +306,12 @@ function onClick(event) { }); targetIDs.push({entity: boxId, action: action}); break; + case switchHandsButton: + cleanUp('leaveButtons'); + hand = hand === "right" ? "left" : "right"; + Settings.setValue("highfidelity.sword.hand", hand); + makeSword(); + break; case cleanupButton: cleanUp('leaveButtons'); break; @@ -263,7 +319,4 @@ function onClick(event) { } Script.scriptEnding.connect(cleanUp); -Controller.mouseMoveEvent.connect(mouseMoveEvent); Controller.mousePressEvent.connect(onClick); -Script.update.connect(update); -MyAvatar.collisionWithEntity.connect(gotHit); From c1b72db18dbf750f63fb6eec86c1a455cb86ff93 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Fri, 10 Jul 2015 16:00:51 -0700 Subject: [PATCH 103/107] Still trying to get jenkins builds to work --- libraries/shared/src/PropertyFlags.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libraries/shared/src/PropertyFlags.h b/libraries/shared/src/PropertyFlags.h index 0202784c77..b0ac9e76d8 100644 --- a/libraries/shared/src/PropertyFlags.h +++ b/libraries/shared/src/PropertyFlags.h @@ -197,8 +197,7 @@ template inline QByteArray PropertyFlags::encode() { template inline size_t PropertyFlags::decode(const uint8_t* data, size_t size) { - clear(); - //clear(); // we are cleared out! + clear(); // we are cleared out! size_t bytesConsumed = 0; int bitCount = BITS_IN_BYTE * size; From eb9d52fbb32291d7cef7abf379b697253f57af77 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Fri, 10 Jul 2015 16:11:56 -0700 Subject: [PATCH 104/107] Helps to actually commit the fix --- tests/entities/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/entities/CMakeLists.txt b/tests/entities/CMakeLists.txt index 23c02a97f8..0077549100 100644 --- a/tests/entities/CMakeLists.txt +++ b/tests/entities/CMakeLists.txt @@ -2,7 +2,7 @@ set(TARGET_NAME "entities-test") # This is not a testcase -- just set it up as a regular hifi project -setup_hifi_project(Script) +setup_hifi_project(Network Script) set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "Tests/manual-tests/") From 9ce9c541defef086bf9c24c0e3252a0ced649e71 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Fri, 10 Jul 2015 18:45:40 -0700 Subject: [PATCH 105/107] fix activation of object when action changes --- interface/src/avatar/AvatarActionHold.cpp | 2 +- libraries/entities/src/EntityItem.cpp | 6 +++++- .../entities/src/EntityScriptingInterface.cpp | 3 +++ libraries/physics/src/ObjectAction.cpp | 7 +++++++ libraries/physics/src/ObjectAction.h | 1 + libraries/physics/src/ObjectActionOffset.cpp | 6 +----- libraries/physics/src/ObjectActionOffset.h | 1 - libraries/physics/src/ObjectActionSpring.cpp | 20 +++++++------------ libraries/physics/src/ObjectActionSpring.h | 1 - 9 files changed, 25 insertions(+), 22 deletions(-) diff --git a/interface/src/avatar/AvatarActionHold.cpp b/interface/src/avatar/AvatarActionHold.cpp index 1e75250ee2..0c5145b596 100644 --- a/interface/src/avatar/AvatarActionHold.cpp +++ b/interface/src/avatar/AvatarActionHold.cpp @@ -127,7 +127,7 @@ bool AvatarActionHold::updateArguments(QVariantMap arguments) { _mine = true; _active = true; - _needsActivation = true; + activateBody(); unlock(); } return true; diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index 794d0752a1..024e54dbaa 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -1496,7 +1496,7 @@ bool EntityItem::addAction(EntitySimulation* simulation, EntityActionPointer act bool result = addActionInternal(simulation, action); if (!result) { - removeAction(simulation, action->getID()); + removeActionInternal(action->getID()); } unlock(); @@ -1520,6 +1520,7 @@ bool EntityItem::addActionInternal(EntitySimulation* simulation, EntityActionPoi QByteArray newDataCache = serializeActions(success); if (success) { _allActionsDataCache = newDataCache; + _dirtyFlags |= EntityItem::DIRTY_PHYSICS_ACTIVATION; } return success; } @@ -1537,6 +1538,7 @@ bool EntityItem::updateAction(EntitySimulation* simulation, const QUuid& actionI bool success = action->updateArguments(arguments); if (success) { _allActionsDataCache = serializeActions(success); + _dirtyFlags |= EntityItem::DIRTY_PHYSICS_ACTIVATION; } else { qDebug() << "EntityItem::updateAction failed"; } @@ -1572,6 +1574,7 @@ bool EntityItem::removeActionInternal(const QUuid& actionID, EntitySimulation* s bool success = true; _allActionsDataCache = serializeActions(success); + _dirtyFlags |= EntityItem::DIRTY_PHYSICS_ACTIVATION; return success; } return false; @@ -1590,6 +1593,7 @@ bool EntityItem::clearActions(EntitySimulation* simulation) { // empty _serializedActions means no actions for the EntityItem _actionsToRemove.clear(); _allActionsDataCache.clear(); + _dirtyFlags |= EntityItem::DIRTY_PHYSICS_ACTIVATION; unlock(); return true; } diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index f1c6157694..18175da1f6 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -543,6 +543,9 @@ bool EntityScriptingInterface::actionWorker(const QUuid& entityID, } bool success = actor(simulation, entity); + if (success) { + _entityTree->entityChanged(entity); + } _entityTree->unlock(); // transmit the change diff --git a/libraries/physics/src/ObjectAction.cpp b/libraries/physics/src/ObjectAction.cpp index 101b69f03a..5205e08c62 100644 --- a/libraries/physics/src/ObjectAction.cpp +++ b/libraries/physics/src/ObjectAction.cpp @@ -129,3 +129,10 @@ void ObjectAction::setAngularVelocity(glm::vec3 angularVelocity) { rigidBody->activate(); } +void ObjectAction::activateBody() { + auto rigidBody = getRigidBody(); + if (rigidBody) { + rigidBody->activate(); + } +} + diff --git a/libraries/physics/src/ObjectAction.h b/libraries/physics/src/ObjectAction.h index f619657e92..f27ed9ab07 100644 --- a/libraries/physics/src/ObjectAction.h +++ b/libraries/physics/src/ObjectAction.h @@ -55,6 +55,7 @@ protected: virtual void setLinearVelocity(glm::vec3 linearVelocity); virtual glm::vec3 getAngularVelocity(); virtual void setAngularVelocity(glm::vec3 angularVelocity); + virtual void activateBody(); void lockForRead() { _lock.lockForRead(); } bool tryLockForRead() { return _lock.tryLockForRead(); } diff --git a/libraries/physics/src/ObjectActionOffset.cpp b/libraries/physics/src/ObjectActionOffset.cpp index 00b7fe6734..a00bbbd418 100644 --- a/libraries/physics/src/ObjectActionOffset.cpp +++ b/libraries/physics/src/ObjectActionOffset.cpp @@ -59,10 +59,6 @@ void ObjectActionOffset::updateActionWorker(btScalar deltaTimeStep) { const float MAX_LINEAR_TIMESCALE = 600.0f; // 10 minutes is a long time if (_positionalTargetSet && _linearTimeScale < MAX_LINEAR_TIMESCALE) { - if (_needsActivation) { - rigidBody->activate(); - _needsActivation = false; - } glm::vec3 objectPosition = bulletToGLM(rigidBody->getCenterOfMassPosition()); glm::vec3 springAxis = objectPosition - _pointToOffsetFrom; // from anchor to object float distance = glm::length(springAxis); @@ -122,7 +118,7 @@ bool ObjectActionOffset::updateArguments(QVariantMap arguments) { _linearDistance = linearDistance; _positionalTargetSet = true; _active = true; - _needsActivation = true; + activateBody(); unlock(); } return true; diff --git a/libraries/physics/src/ObjectActionOffset.h b/libraries/physics/src/ObjectActionOffset.h index a0190f3832..1918da6996 100644 --- a/libraries/physics/src/ObjectActionOffset.h +++ b/libraries/physics/src/ObjectActionOffset.h @@ -36,7 +36,6 @@ public: float _linearDistance; float _linearTimeScale; bool _positionalTargetSet; - bool _needsActivation = true; }; #endif // hifi_ObjectActionOffset_h diff --git a/libraries/physics/src/ObjectActionSpring.cpp b/libraries/physics/src/ObjectActionSpring.cpp index c1ef94296a..dde47f7c4e 100644 --- a/libraries/physics/src/ObjectActionSpring.cpp +++ b/libraries/physics/src/ObjectActionSpring.cpp @@ -25,8 +25,7 @@ ObjectActionSpring::ObjectActionSpring(const QUuid& id, EntityItemPointer ownerE _positionalTargetSet(true), _rotationalTarget(glm::quat()), _angularTimeScale(FLT_MAX), - _rotationalTargetSet(true), - _needsActivation(true) { + _rotationalTargetSet(true) { #if WANT_DEBUG qDebug() << "ObjectActionSpring::ObjectActionSpring"; #endif @@ -67,10 +66,10 @@ void ObjectActionSpring::updateActionWorker(btScalar deltaTimeStep) { if (_linearTimeScale < MAX_TIMESCALE) { btVector3 offset = rigidBody->getCenterOfMassPosition() - glmToBullet(_positionalTarget); float offsetLength = offset.length(); - //float blend = glm::min(1.0f, deltaTimeStep / _linearTimeScale); - float blend = 1.0f; float speed = (offsetLength > FLT_EPSILON) ? glm::min(offsetLength / _linearTimeScale, SPRING_MAX_SPEED) : 0.0f; - rigidBody->setLinearVelocity((1.0f - blend) * rigidBody->getLinearVelocity() - (blend * speed / (offsetLength * _linearTimeScale)) * offset); + + // this action is aggresively critically damped and defeats the current velocity + rigidBody->setLinearVelocity((- speed / offsetLength) * offset); } if (_angularTimeScale < MAX_TIMESCALE) { @@ -98,13 +97,8 @@ void ObjectActionSpring::updateActionWorker(btScalar deltaTimeStep) { targetVelocity = (angle / _angularTimeScale) * deltaQ.getAxis(); } } - //float blend = glm::min(1.0f, deltaTimeStep / _angularTimeScale); - float blend = 1.0f; - rigidBody->setAngularVelocity((1.0f - blend) * rigidBody->getAngularVelocity() + blend * targetVelocity); - } - if (_needsActivation) { - rigidBody->activate(); - _needsActivation = false; + // this action is aggresively critically damped and defeats the current velocity + rigidBody->setAngularVelocity(targetVelocity); } unlock(); } @@ -151,7 +145,7 @@ bool ObjectActionSpring::updateArguments(QVariantMap arguments) { _rotationalTarget = rotationalTarget; _angularTimeScale = glm::max(MIN_TIMESCALE, glm::abs(angularTimeScale)); _active = true; - _needsActivation = true; + activateBody(); unlock(); } return true; diff --git a/libraries/physics/src/ObjectActionSpring.h b/libraries/physics/src/ObjectActionSpring.h index a0a829bdab..caa64c3d3a 100644 --- a/libraries/physics/src/ObjectActionSpring.h +++ b/libraries/physics/src/ObjectActionSpring.h @@ -37,7 +37,6 @@ protected: glm::quat _rotationalTarget; float _angularTimeScale; bool _rotationalTargetSet; - bool _needsActivation; }; #endif // hifi_ObjectActionSpring_h From c56b2918a405cb066924134a44a8ae47f3a501b5 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Sun, 12 Jul 2015 10:35:44 -0700 Subject: [PATCH 106/107] allow voxels.js to work on an empty voxel-space --- examples/voxels.js | 49 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/examples/voxels.js b/examples/voxels.js index 799af04bef..e110f15260 100644 --- a/examples/voxels.js +++ b/examples/voxels.js @@ -2,6 +2,32 @@ var controlHeld = false; var shiftHeld = false; +function attemptVoxelChange(intersection) { + var ids = Entities.findEntities(intersection.intersection, 10); + var success = false; + for (var i = 0; i < ids.length; i++) { + var id = ids[i]; + if (controlHeld) { + // hold control to erase a sphere + if (Entities.setVoxelSphere(id, intersection.intersection, 1.0, 0)) { + success = true; + } + } else if (shiftHeld) { + // hold shift to set all voxels to 255 + if (Entities.setAllVoxels(id, 255)) { + success = true; + } + } else { + // no modifier key means to add a sphere + if (Entities.setVoxelSphere(id, intersection.intersection, 1.0, 255)) { + success = true; + } + } + } + return success; +} + + function mousePressEvent(event) { if (!event.isLeftButton) { return; @@ -9,20 +35,21 @@ function mousePressEvent(event) { var pickRay = Camera.computePickRay(event.x, event.y); var intersection = Entities.findRayIntersection(pickRay, true); // accurate picking - // var props = Entities.getEntityProperties(intersection.entityID); + + // we've used a picking ray to decide where to add the new sphere of voxels. If we pick nothing + // or if we pick a non-PolyVox entity, we fall through to the next picking attempt. if (intersection.intersects) { - var ids = Entities.findEntities(intersection.intersection, 10); - for (var i = 0; i < ids.length; i++) { - var id = ids[i]; - if (controlHeld) { - Entities.setVoxelSphere(id, intersection.intersection, 1.0, 0); - } else if (shiftHeld) { - Entities.setAllVoxels(id, 255); - } else { - Entities.setVoxelSphere(id, intersection.intersection, 1.0, 255); - } + if (attemptVoxelChange(intersection)) { + return; } } + + // if the PolyVox entity is empty, we can't pick against its voxel. try picking against its + // bounding box, instead. + intersection = Entities.findRayIntersection(pickRay, false); // bounding box picking + if (intersection.intersects) { + attemptVoxelChange(intersection); + } } From 848e5bcebf2a1a925c1a53b3e4a49fc43baed69c Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Sun, 12 Jul 2015 13:43:21 -0700 Subject: [PATCH 107/107] this code can be called for hold actions if they end up in an svo save. revert a line from a previous commit --- libraries/physics/src/ObjectActionSpring.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/physics/src/ObjectActionSpring.cpp b/libraries/physics/src/ObjectActionSpring.cpp index dde47f7c4e..196cc8d3ea 100644 --- a/libraries/physics/src/ObjectActionSpring.cpp +++ b/libraries/physics/src/ObjectActionSpring.cpp @@ -189,7 +189,7 @@ void ObjectActionSpring::deserialize(QByteArray serializedArguments) { EntityActionType type; dataStream >> type; - assert(type == ACTION_TYPE_SPRING); + assert(type == getType()); QUuid id; dataStream >> id;