From 91ca13c66daef7863c99e96ecf0487a6201b47e7 Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Sat, 1 Aug 2015 12:43:03 -0700 Subject: [PATCH] Added AnimDebugDraw to render-utils --- interface/src/Application.cpp | 4 + libraries/render-utils/src/AnimDebugDraw.cpp | 144 +++++++++++++++++++ libraries/render-utils/src/AnimDebugDraw.h | 36 +++++ libraries/render-utils/src/animdebugdraw.slf | 17 +++ libraries/render-utils/src/animdebugdraw.slv | 24 ++++ tests/animation/src/AnimClipTests.cpp | 1 - tests/animation/src/AnimClipTests.h | 2 +- 7 files changed, 226 insertions(+), 2 deletions(-) create mode 100644 libraries/render-utils/src/AnimDebugDraw.cpp create mode 100644 libraries/render-utils/src/AnimDebugDraw.h create mode 100644 libraries/render-utils/src/animdebugdraw.slf create mode 100644 libraries/render-utils/src/animdebugdraw.slv diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index acdfd8cfc9..e43465ad75 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -142,6 +142,8 @@ #include "ui/AddressBarDialog.h" #include "ui/UpdateDialog.h" +#include "AnimDebugDraw.h" + // ON WIndows PC, NVidia Optimus laptop, we want to enable NVIDIA GPU #if defined(Q_OS_WIN) @@ -645,6 +647,8 @@ Application::Application(int& argc, char** argv, QElapsedTimer &startup_time) : auto& packetReceiver = nodeList->getPacketReceiver(); packetReceiver.registerListener(PacketType::DomainConnectionDenied, this, "handleDomainConnectionDeniedPacket"); + + AnimDebugDraw& add = AnimDebugDraw::getInstance(); } void Application::aboutToQuit() { diff --git a/libraries/render-utils/src/AnimDebugDraw.cpp b/libraries/render-utils/src/AnimDebugDraw.cpp new file mode 100644 index 0000000000..80a8824d5a --- /dev/null +++ b/libraries/render-utils/src/AnimDebugDraw.cpp @@ -0,0 +1,144 @@ +// +// AnimDebugDraw.cpp +// +// 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 "animdebugdraw_vert.h" +#include "animdebugdraw_frag.h" +#include + +#include "AnimDebugDraw.h" +#include "AbstractViewStateInterface.h" + +struct Vertex { + glm::vec3 pos; + uint32_t rgba; +}; + +class AnimDebugDrawData { +public: + typedef render::Payload Payload; + typedef Payload::DataPointer Pointer; + + AnimDebugDrawData() { + + _vertexFormat = std::make_shared(); + _vertexBuffer = std::make_shared(); + _indexBuffer = std::make_shared(); + + _vertexFormat->setAttribute(gpu::Stream::POSITION, 0, gpu::Element::VEC3F_XYZ, 0); + _vertexFormat->setAttribute(gpu::Stream::COLOR, 0, gpu::Element::COLOR_RGBA_32, offsetof(Vertex, rgba)); + } + + void render(RenderArgs* args) { + auto& batch = *args->_batch; + batch.setPipeline(_pipeline); + auto transform = Transform{}; + batch.setModelTransform(transform); + + batch.setInputFormat(_vertexFormat); + batch.setInputBuffer(0, _vertexBuffer, 0, sizeof(Vertex)); + batch.setIndexBuffer(gpu::UINT16, _indexBuffer, 0); + + auto numIndices = _indexBuffer->getSize() / sizeof(uint16_t); + batch.drawIndexed(gpu::LINES, numIndices); + } + + gpu::PipelinePointer _pipeline; + render::ItemID _item; + gpu::Stream::FormatPointer _vertexFormat; + gpu::BufferPointer _vertexBuffer; + gpu::BufferPointer _indexBuffer; +}; + +typedef render::Payload AnimDebugDrawPayload; + +namespace render { + template <> const ItemKey payloadGetKey(const AnimDebugDrawData::Pointer& data) { return ItemKey::Builder::transparentShape(); } + template <> const Item::Bound payloadGetBound(const AnimDebugDrawData::Pointer& data) { return Item::Bound(); } + template <> void payloadRender(const AnimDebugDrawData::Pointer& data, RenderArgs* args) { + data->render(args); + } +} + +static AnimDebugDraw* instance = nullptr; + +AnimDebugDraw& AnimDebugDraw::getInstance() { + if (!instance) { + instance = new AnimDebugDraw(); + } + return *instance; +} + +static uint32_t toRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { + return ((uint32_t)r | (uint32_t)g << 8 | (uint32_t)b << 16 | (uint32_t)a << 24); +} + +gpu::PipelinePointer AnimDebugDraw::_pipeline; + +AnimDebugDraw::AnimDebugDraw() : + _itemID(0) { + + auto state = std::make_shared(); + state->setCullMode(gpu::State::CULL_BACK); + state->setDepthTest(true, true, gpu::LESS_EQUAL); + state->setBlendFunction(false, 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); + auto vertShader = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(animdebugdraw_vert))); + auto fragShader = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(animdebugdraw_frag))); + auto program = gpu::ShaderPointer(gpu::Shader::createProgram(vertShader, fragShader)); + _pipeline = gpu::PipelinePointer(gpu::Pipeline::create(program, state)); + + _animDebugDrawData = std::make_shared(); + _animDebugDrawPayload = std::make_shared(_animDebugDrawData); + + _animDebugDrawData->_pipeline = _pipeline; + + render::ScenePointer scene = AbstractViewStateInterface::instance()->getMain3DScene(); + if (scene) { + _itemID = scene->allocateID(); + render::PendingChanges pendingChanges; + pendingChanges.resetItem(_itemID, _animDebugDrawPayload); + scene->enqueuePendingChanges(pendingChanges); + } + + // HACK: add red, green and blue axis at (1,1,1) + _animDebugDrawData->_vertexBuffer->resize(sizeof(Vertex) * 6); + Vertex* data = (Vertex*)_animDebugDrawData->_vertexBuffer->editData(); + + data[0].pos = glm::vec3(1.0, 1.0f, 1.0f); + data[0].rgba = toRGBA(255, 0, 0, 255); + data[1].pos = glm::vec3(2.0, 1.0f, 1.0f); + data[1].rgba = toRGBA(255, 0, 0, 255); + + data[2].pos = glm::vec3(1.0, 1.0f, 1.0f); + data[2].rgba = toRGBA(0, 255, 0, 255); + data[3].pos = glm::vec3(1.0, 2.0f, 1.0f); + data[3].rgba = toRGBA(0, 255, 0, 255); + + data[4].pos = glm::vec3(1.0, 1.0f, 1.0f); + data[4].rgba = toRGBA(0, 0, 255, 255); + data[5].pos = glm::vec3(1.0, 1.0f, 2.0f); + data[5].rgba = toRGBA(0, 0, 255, 255); + + _animDebugDrawData->_indexBuffer->resize(sizeof(uint16_t) * 6); + uint16_t* indices = (uint16_t*)_animDebugDrawData->_indexBuffer->editData(); + for (int i = 0; i < 6; i++) { + indices[i] = i; + } + +} + +AnimDebugDraw::~AnimDebugDraw() { + render::ScenePointer scene = AbstractViewStateInterface::instance()->getMain3DScene(); + if (scene && _itemID) { + render::PendingChanges pendingChanges; + pendingChanges.removeItem(_itemID); + scene->enqueuePendingChanges(pendingChanges); + } +} diff --git a/libraries/render-utils/src/AnimDebugDraw.h b/libraries/render-utils/src/AnimDebugDraw.h new file mode 100644 index 0000000000..ea3ac9ced1 --- /dev/null +++ b/libraries/render-utils/src/AnimDebugDraw.h @@ -0,0 +1,36 @@ +// +// AnimDebugDraw.h +// +// 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_AnimDebugDraw_h +#define hifi_AnimDebugDraw_h + +#include "AnimNode.h" +#include +#include + +class AnimDebugDrawData; +typedef render::Payload AnimDebugDrawPayload; + + +class AnimDebugDraw { +public: + static AnimDebugDraw& getInstance(); + + AnimDebugDraw(); + ~AnimDebugDraw(); + +protected: + std::shared_ptr _animDebugDrawData; + std::shared_ptr _animDebugDrawPayload; + render::ItemID _itemID; + + static gpu::PipelinePointer _pipeline; +}; + +#endif // hifi_AnimDebugDraw diff --git a/libraries/render-utils/src/animdebugdraw.slf b/libraries/render-utils/src/animdebugdraw.slf new file mode 100644 index 0000000000..aa34c9bfba --- /dev/null +++ b/libraries/render-utils/src/animdebugdraw.slf @@ -0,0 +1,17 @@ +<@include gpu/Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// unlit untextured fragment shader +// +// 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 +// + +varying vec4 varColor; + +void main(void) { + gl_FragColor = varColor; +} diff --git a/libraries/render-utils/src/animdebugdraw.slv b/libraries/render-utils/src/animdebugdraw.slv new file mode 100644 index 0000000000..68749ec7cc --- /dev/null +++ b/libraries/render-utils/src/animdebugdraw.slv @@ -0,0 +1,24 @@ +<@include gpu/Config.slh@> +<$VERSION_HEADER$> +// Generated on <$_SCRIBE_DATE$> +// +// unlit untextured vertex shader +// +// 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 vec4 varColor; + +void main(void) { + // pass along the diffuse color + varColor = gl_Color; + + TransformCamera cam = getTransformCamera(); + TransformObject obj = getTransformObject(); + <$transformModelToClipPos(cam, obj, gl_Vertex, gl_Position)$> +} diff --git a/tests/animation/src/AnimClipTests.cpp b/tests/animation/src/AnimClipTests.cpp index aab49aa30d..58c7311b2d 100644 --- a/tests/animation/src/AnimClipTests.cpp +++ b/tests/animation/src/AnimClipTests.cpp @@ -1,6 +1,5 @@ // // AnimClipTests.cpp -// tests/rig/src // // Copyright 2015 High Fidelity, Inc. // diff --git a/tests/animation/src/AnimClipTests.h b/tests/animation/src/AnimClipTests.h index f70ee31aa1..d4590ef27d 100644 --- a/tests/animation/src/AnimClipTests.h +++ b/tests/animation/src/AnimClipTests.h @@ -27,4 +27,4 @@ private slots: void testLoader(); }; -#endif // hifi_TransformTests_h +#endif // hifi_AnimClipTests_h