diff --git a/examples/painting/whiteboard/whiteboardSpawner.js b/examples/painting/whiteboard/whiteboardSpawner.js index 56ead143e4..f3005495ec 100644 --- a/examples/painting/whiteboard/whiteboardSpawner.js +++ b/examples/painting/whiteboard/whiteboardSpawner.js @@ -247,4 +247,4 @@ function cleanup() { // Uncomment this line to delete whiteboard and all associated entity on script close -// Script.scriptEnding.connect(cleanup); \ No newline at end of file +//Script.scriptEnding.connect(cleanup); diff --git a/libraries/entities-renderer/src/RenderablePolyLineEntityItem.cpp b/libraries/entities-renderer/src/RenderablePolyLineEntityItem.cpp index 76cf4fac3d..036d37a95b 100644 --- a/libraries/entities-renderer/src/RenderablePolyLineEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderablePolyLineEntityItem.cpp @@ -31,6 +31,7 @@ EntityItemPointer RenderablePolyLineEntityItem::factory(const EntityItemID& enti RenderablePolyLineEntityItem::RenderablePolyLineEntityItem(const EntityItemID& entityItemID, const EntityItemProperties& properties) : PolyLineEntityItem(entityItemID, properties) { _numVertices = 0; + _vertices = QVector(0.0f); } @@ -114,13 +115,56 @@ void RenderablePolyLineEntityItem::updateGeometry() { _numVertices += 2; } _pointsChanged = false; + _normalsChanged = false; + _strokeWidthsChanged = false; +} + +void RenderablePolyLineEntityItem::updateVertices() { + // Calculate the minimum vector size out of normals, points, and stroke widths + int minVectorSize = _normals.size(); + if (_points.size() < minVectorSize) { + minVectorSize = _points.size(); + } + if (_strokeWidths.size() < minVectorSize) { + minVectorSize = _strokeWidths.size(); + } + + _vertices.clear(); + glm::vec3 v1, v2, tangent, binormal, point; + + int finalIndex = minVectorSize - 1; + for (int i = 0; i < finalIndex; i++) { + float width = _strokeWidths.at(i); + point = _points.at(i); + + tangent = _points.at(i); + + tangent = _points.at(i + 1) - point; + glm::vec3 normal = _normals.at(i); + binormal = glm::normalize(glm::cross(tangent, normal)) * width; + + // Check to make sure binormal is not a NAN. If it is, don't add to vertices vector + if (binormal.x != binormal.x) { + continue; + } + v1 = point + binormal; + v2 = point - binormal; + _vertices << v1 << v2; + } + + // For last point we can assume binormals are the same since it represents the last two vertices of quad + point = _points.at(finalIndex); + v1 = point + binormal; + v2 = point - binormal; + _vertices << v1 << v2; + } void RenderablePolyLineEntityItem::render(RenderArgs* args) { QWriteLocker lock(&_quadReadWriteLock); - if (_points.size() < 2 || _normals.size () < 2 || _vertices.size() < 2) { + if (_points.size() < 2 || _normals.size () < 2 || _strokeWidths.size() < 2) { return; } @@ -139,7 +183,8 @@ void RenderablePolyLineEntityItem::render(RenderArgs* args) { Q_ASSERT(getType() == EntityTypes::PolyLine); Q_ASSERT(args->_batch); - if (_pointsChanged) { + if (_pointsChanged || _strokeWidthsChanged || _normalsChanged) { + updateVertices(); updateGeometry(); } diff --git a/libraries/entities-renderer/src/RenderablePolyLineEntityItem.h b/libraries/entities-renderer/src/RenderablePolyLineEntityItem.h index 618f8c66a6..c49777cfa3 100644 --- a/libraries/entities-renderer/src/RenderablePolyLineEntityItem.h +++ b/libraries/entities-renderer/src/RenderablePolyLineEntityItem.h @@ -40,8 +40,10 @@ public: protected: void updateGeometry(); + void updateVertices(); gpu::BufferPointer _verticesBuffer; unsigned int _numVertices; + QVector _vertices; }; diff --git a/libraries/entities/src/PolyLineEntityItem.cpp b/libraries/entities/src/PolyLineEntityItem.cpp index 45c967f78d..012ec3ca4a 100644 --- a/libraries/entities/src/PolyLineEntityItem.cpp +++ b/libraries/entities/src/PolyLineEntityItem.cpp @@ -34,8 +34,9 @@ PolyLineEntityItem::PolyLineEntityItem(const EntityItemID& entityItemID, const E EntityItem(entityItemID), _lineWidth(DEFAULT_LINE_WIDTH), _pointsChanged(true), +_normalsChanged(true), +_strokeWidthsChanged(true), _points(QVector(0.0f)), -_vertices(QVector(0.0f)), _normals(QVector(0.0f)), _strokeWidths(QVector(0.0f)), _textures("") @@ -106,47 +107,13 @@ bool PolyLineEntityItem::appendPoint(const glm::vec3& point) { bool PolyLineEntityItem::setStrokeWidths(const QVector& strokeWidths) { _strokeWidths = strokeWidths; + _strokeWidthsChanged = true; return true; } bool PolyLineEntityItem::setNormals(const QVector& normals) { _normals = normals; - if (_points.size() < 2 || _normals.size() < 2 || _strokeWidths.size() < 2) { - return false; - } - - int minVectorSize = _normals.size(); - if (_points.size() < minVectorSize) { - minVectorSize = _points.size(); - } - if (_strokeWidths.size() < minVectorSize) { - minVectorSize = _strokeWidths.size(); - } - - _vertices.clear(); - glm::vec3 v1, v2, tangent, binormal, point; - - int finalIndex = minVectorSize -1; - for (int i = 0; i < finalIndex; i++) { - float width = _strokeWidths.at(i); - point = _points.at(i); - - tangent = _points.at(i + 1) - point; - glm::vec3 normal = normals.at(i); - binormal = glm::normalize(glm::cross(tangent, normal)) * width; - - //This checks to make sure binormal is not a NAN - assert(binormal.x == binormal.x); - v1 = point + binormal; - v2 = point - binormal; - _vertices << v1 << v2; - } - //for last point we can just assume binormals are same since it represents last two vertices of quad - point = _points.at(finalIndex); - v1 = point + binormal; - v2 = point - binormal; - _vertices << v1 << v2; - + _normalsChanged = true; return true; } diff --git a/libraries/entities/src/PolyLineEntityItem.h b/libraries/entities/src/PolyLineEntityItem.h index 9e9d3f9013..4da52f3f21 100644 --- a/libraries/entities/src/PolyLineEntityItem.h +++ b/libraries/entities/src/PolyLineEntityItem.h @@ -93,8 +93,9 @@ class PolyLineEntityItem : public EntityItem { rgbColor _color; float _lineWidth; bool _pointsChanged; + bool _normalsChanged; + bool _strokeWidthsChanged; QVector _points; - QVector _vertices; QVector _normals; QVector _strokeWidths; QString _textures;