Merge pull request #5940 from ericrius1/polyLineFix

Made polyline painting experience smoother- no more flickering
This commit is contained in:
Ryan Huffman 2015-09-29 18:22:01 -07:00
commit b4ca03ac3f
2 changed files with 65 additions and 65 deletions

View file

@ -43,7 +43,7 @@ void RenderablePolyLineEntityItem::createPipeline() {
static const int NORMAL_OFFSET = 12; static const int NORMAL_OFFSET = 12;
static const int COLOR_OFFSET = 24; static const int COLOR_OFFSET = 24;
static const int TEXTURE_OFFSET = 28; static const int TEXTURE_OFFSET = 28;
auto textureCache = DependencyManager::get<TextureCache>(); auto textureCache = DependencyManager::get<TextureCache>();
QString path = PathUtils::resourcesPath() + "images/paintStroke.png"; QString path = PathUtils::resourcesPath() + "images/paintStroke.png";
_texture = textureCache->getImageTexture(path); _texture = textureCache->getImageTexture(path);
@ -52,22 +52,22 @@ void RenderablePolyLineEntityItem::createPipeline() {
_format->setAttribute(gpu::Stream::NORMAL, 0, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), NORMAL_OFFSET); _format->setAttribute(gpu::Stream::NORMAL, 0, gpu::Element(gpu::VEC3, gpu::FLOAT, gpu::XYZ), NORMAL_OFFSET);
_format->setAttribute(gpu::Stream::COLOR, 0, gpu::Element::COLOR_RGBA_32, COLOR_OFFSET); _format->setAttribute(gpu::Stream::COLOR, 0, gpu::Element::COLOR_RGBA_32, COLOR_OFFSET);
_format->setAttribute(gpu::Stream::TEXCOORD, 0, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::UV), TEXTURE_OFFSET); _format->setAttribute(gpu::Stream::TEXCOORD, 0, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::UV), TEXTURE_OFFSET);
auto VS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(paintStroke_vert))); auto VS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(paintStroke_vert)));
auto PS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(paintStroke_frag))); auto PS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(paintStroke_frag)));
gpu::ShaderPointer program = gpu::ShaderPointer(gpu::Shader::createProgram(VS, PS)); gpu::ShaderPointer program = gpu::ShaderPointer(gpu::Shader::createProgram(VS, PS));
gpu::Shader::BindingSet slotBindings; gpu::Shader::BindingSet slotBindings;
PAINTSTROKE_GPU_SLOT = 0; PAINTSTROKE_GPU_SLOT = 0;
slotBindings.insert(gpu::Shader::Binding(std::string("paintStrokeTextureBinding"), PAINTSTROKE_GPU_SLOT)); slotBindings.insert(gpu::Shader::Binding(std::string("paintStrokeTextureBinding"), PAINTSTROKE_GPU_SLOT));
gpu::Shader::makeProgram(*program, slotBindings); gpu::Shader::makeProgram(*program, slotBindings);
gpu::StatePointer state = gpu::StatePointer(new gpu::State()); gpu::StatePointer state = gpu::StatePointer(new gpu::State());
state->setDepthTest(true, true, gpu::LESS_EQUAL); state->setDepthTest(true, true, gpu::LESS_EQUAL);
state->setBlendFunction(true, state->setBlendFunction(true,
gpu::State::SRC_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::INV_SRC_ALPHA, 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); gpu::State::FACTOR_ALPHA, gpu::State::BLEND_OP_ADD, gpu::State::ONE);
_pipeline = gpu::PipelinePointer(gpu::Pipeline::create(program, state)); _pipeline = gpu::PipelinePointer(gpu::Pipeline::create(program, state));
} }
void RenderablePolyLineEntityItem::updateGeometry() { void RenderablePolyLineEntityItem::updateGeometry() {
@ -78,30 +78,30 @@ void RenderablePolyLineEntityItem::updateGeometry() {
float tailStart = 0.0f; float tailStart = 0.0f;
float tailEnd = 0.25f; float tailEnd = 0.25f;
float tailLength = tailEnd - tailStart; float tailLength = tailEnd - tailStart;
float headStart = 0.76f; float headStart = 0.76f;
float headEnd = 1.0f; float headEnd = 1.0f;
float headLength = headEnd - headStart; float headLength = headEnd - headStart;
float uCoord, vCoord; float uCoord, vCoord;
int numTailStrips = 5; int numTailStrips = 5;
int numHeadStrips = 10; int numHeadStrips = 10;
int startHeadIndex = _normals.size() - numHeadStrips; int startHeadIndex = _vertices.size() / 2 - numHeadStrips;
for (int i = 0; i < _normals.size(); i++) { for (int i = 0; i < _vertices.size() / 2; i++) {
uCoord = 0.26f; uCoord = 0.26f;
vCoord = 0.0f; vCoord = 0.0f;
//tail //tail
if(i < numTailStrips) { if (i < numTailStrips) {
uCoord = float(i)/numTailStrips * tailLength + tailStart; uCoord = float(i) / numTailStrips * tailLength + tailStart;
} }
//head //head
if( i > startHeadIndex) { if (i > startHeadIndex) {
uCoord = float( (i+ 1) - startHeadIndex)/numHeadStrips * headLength + headStart; uCoord = float((i + 1) - startHeadIndex) / numHeadStrips * headLength + headStart;
} }
uv = vec2(uCoord, vCoord); uv = vec2(uCoord, vCoord);
_verticesBuffer->append(sizeof(glm::vec3), (const gpu::Byte*)&_vertices.at(vertexIndex)); _verticesBuffer->append(sizeof(glm::vec3), (const gpu::Byte*)&_vertices.at(vertexIndex));
_verticesBuffer->append(sizeof(glm::vec3), (const gpu::Byte*)&_normals.at(i)); _verticesBuffer->append(sizeof(glm::vec3), (const gpu::Byte*)&_normals.at(i));
_verticesBuffer->append(sizeof(int), (gpu::Byte*)&_color); _verticesBuffer->append(sizeof(int), (gpu::Byte*)&_color);
@ -114,32 +114,32 @@ void RenderablePolyLineEntityItem::updateGeometry() {
_verticesBuffer->append(sizeof(int), (gpu::Byte*)_color); _verticesBuffer->append(sizeof(int), (gpu::Byte*)_color);
_verticesBuffer->append(sizeof(glm::vec2), (const gpu::Byte*)&uv); _verticesBuffer->append(sizeof(glm::vec2), (const gpu::Byte*)&uv);
vertexIndex++; vertexIndex++;
_numVertices +=2; _numVertices += 2;
} }
_pointsChanged = false; _pointsChanged = false;
} }
void RenderablePolyLineEntityItem::render(RenderArgs* args) { void RenderablePolyLineEntityItem::render(RenderArgs* args) {
QWriteLocker lock(&_quadReadWriteLock); QWriteLocker lock(&_quadReadWriteLock);
if (_points.size() < 2 || _vertices.size() != _normals.size() * 2) { if (_points.size() < 2 || _normals.size () < 2 || _vertices.size() < 2) {
return; return;
} }
if (!_pipeline) { if (!_pipeline) {
createPipeline(); createPipeline();
} }
PerformanceTimer perfTimer("RenderablePolyLineEntityItem::render"); PerformanceTimer perfTimer("RenderablePolyLineEntityItem::render");
Q_ASSERT(getType() == EntityTypes::PolyLine); Q_ASSERT(getType() == EntityTypes::PolyLine);
Q_ASSERT(args->_batch); Q_ASSERT(args->_batch);
if (_pointsChanged) { if (_pointsChanged) {
updateGeometry(); updateGeometry();
} }
gpu::Batch& batch = *args->_batch; gpu::Batch& batch = *args->_batch;
Transform transform = Transform(); Transform transform = Transform();
transform.setTranslation(getPosition()); transform.setTranslation(getPosition());
@ -151,8 +151,8 @@ void RenderablePolyLineEntityItem::render(RenderArgs* args) {
batch.setInputFormat(_format); batch.setInputFormat(_format);
batch.setInputBuffer(0, _verticesBuffer, 0, _format->getChannels().at(0)._stride); batch.setInputBuffer(0, _verticesBuffer, 0, _format->getChannels().at(0)._stride);
batch.draw(gpu::TRIANGLE_STRIP, _numVertices, 0); batch.draw(gpu::TRIANGLE_STRIP, _numVertices, 0);
RenderableDebugableEntityItem::render(this, args); RenderableDebugableEntityItem::render(this, args);
}; };

View file

@ -27,12 +27,12 @@ const int PolyLineEntityItem::MAX_POINTS_PER_LINE = 70;
EntityItemPointer PolyLineEntityItem::factory(const EntityItemID& entityID, const EntityItemProperties& properties) { EntityItemPointer PolyLineEntityItem::factory(const EntityItemID& entityID, const EntityItemProperties& properties) {
EntityItemPointer result { new PolyLineEntityItem(entityID, properties) }; EntityItemPointer result{ new PolyLineEntityItem(entityID, properties) };
return result; return result;
} }
PolyLineEntityItem::PolyLineEntityItem(const EntityItemID& entityItemID, const EntityItemProperties& properties) : PolyLineEntityItem::PolyLineEntityItem(const EntityItemID& entityItemID, const EntityItemProperties& properties) :
EntityItem(entityItemID) , EntityItem(entityItemID),
_lineWidth(DEFAULT_LINE_WIDTH), _lineWidth(DEFAULT_LINE_WIDTH),
_pointsChanged(true), _pointsChanged(true),
_points(QVector<glm::vec3>(0.0f)), _points(QVector<glm::vec3>(0.0f)),
@ -48,16 +48,16 @@ _strokeWidths(QVector<float>(0.0f))
EntityItemProperties PolyLineEntityItem::getProperties(EntityPropertyFlags desiredProperties) const { EntityItemProperties PolyLineEntityItem::getProperties(EntityPropertyFlags desiredProperties) const {
QWriteLocker lock(&_quadReadWriteLock); QWriteLocker lock(&_quadReadWriteLock);
EntityItemProperties properties = EntityItem::getProperties(desiredProperties); // get the properties from our base class EntityItemProperties properties = EntityItem::getProperties(desiredProperties); // get the properties from our base class
properties._color = getXColor(); properties._color = getXColor();
properties._colorChanged = false; properties._colorChanged = false;
COPY_ENTITY_PROPERTY_TO_PROPERTIES(lineWidth, getLineWidth); COPY_ENTITY_PROPERTY_TO_PROPERTIES(lineWidth, getLineWidth);
COPY_ENTITY_PROPERTY_TO_PROPERTIES(linePoints, getLinePoints); COPY_ENTITY_PROPERTY_TO_PROPERTIES(linePoints, getLinePoints);
COPY_ENTITY_PROPERTY_TO_PROPERTIES(normals, getNormals); COPY_ENTITY_PROPERTY_TO_PROPERTIES(normals, getNormals);
COPY_ENTITY_PROPERTY_TO_PROPERTIES(strokeWidths, getStrokeWidths); COPY_ENTITY_PROPERTY_TO_PROPERTIES(strokeWidths, getStrokeWidths);
properties._glowLevel = getGlowLevel(); properties._glowLevel = getGlowLevel();
properties._glowLevelChanged = false; properties._glowLevelChanged = false;
return properties; return properties;
@ -67,20 +67,20 @@ bool PolyLineEntityItem::setProperties(const EntityItemProperties& properties) {
QWriteLocker lock(&_quadReadWriteLock); QWriteLocker lock(&_quadReadWriteLock);
bool somethingChanged = false; bool somethingChanged = false;
somethingChanged = EntityItem::setProperties(properties); // set the properties in our base class somethingChanged = EntityItem::setProperties(properties); // set the properties in our base class
SET_ENTITY_PROPERTY_FROM_PROPERTIES(color, setColor); SET_ENTITY_PROPERTY_FROM_PROPERTIES(color, setColor);
SET_ENTITY_PROPERTY_FROM_PROPERTIES(lineWidth, setLineWidth); SET_ENTITY_PROPERTY_FROM_PROPERTIES(lineWidth, setLineWidth);
SET_ENTITY_PROPERTY_FROM_PROPERTIES(linePoints, setLinePoints); SET_ENTITY_PROPERTY_FROM_PROPERTIES(linePoints, setLinePoints);
SET_ENTITY_PROPERTY_FROM_PROPERTIES(normals, setNormals); SET_ENTITY_PROPERTY_FROM_PROPERTIES(normals, setNormals);
SET_ENTITY_PROPERTY_FROM_PROPERTIES(strokeWidths, setStrokeWidths); SET_ENTITY_PROPERTY_FROM_PROPERTIES(strokeWidths, setStrokeWidths);
if (somethingChanged) { if (somethingChanged) {
bool wantDebug = false; bool wantDebug = false;
if (wantDebug) { if (wantDebug) {
uint64_t now = usecTimestampNow(); uint64_t now = usecTimestampNow();
int elapsed = now - getLastEdited(); int elapsed = now - getLastEdited();
qCDebug(entities) << "PolyLineEntityItem::setProperties() AFTER update... edited AGO=" << elapsed << qCDebug(entities) << "PolyLineEntityItem::setProperties() AFTER update... edited AGO=" << elapsed <<
"now=" << now << " getLastEdited()=" << getLastEdited(); "now=" << now << " getLastEdited()=" << getLastEdited();
} }
setLastEdited(properties._lastEdited); setLastEdited(properties._lastEdited);
} }
@ -93,7 +93,7 @@ bool PolyLineEntityItem::appendPoint(const glm::vec3& point) {
return false; return false;
} }
glm::vec3 halfBox = getDimensions() * 0.5f; glm::vec3 halfBox = getDimensions() * 0.5f;
if ( (point.x < - halfBox.x || point.x > halfBox.x) || (point.y < -halfBox.y || point.y > halfBox.y) || (point.z < - halfBox.z || point.z > halfBox.z) ) { if ((point.x < -halfBox.x || point.x > halfBox.x) || (point.y < -halfBox.y || point.y > halfBox.y) || (point.z < -halfBox.z || point.z > halfBox.z)) {
qDebug() << "Point is outside entity's bounding box"; qDebug() << "Point is outside entity's bounding box";
return false; return false;
} }
@ -102,17 +102,17 @@ bool PolyLineEntityItem::appendPoint(const glm::vec3& point) {
return true; return true;
} }
bool PolyLineEntityItem::setStrokeWidths(const QVector<float>& strokeWidths ) { bool PolyLineEntityItem::setStrokeWidths(const QVector<float>& strokeWidths) {
_strokeWidths = strokeWidths; _strokeWidths = strokeWidths;
return true; return true;
} }
bool PolyLineEntityItem::setNormals(const QVector<glm::vec3>& normals) { bool PolyLineEntityItem::setNormals(const QVector<glm::vec3>& normals) {
_normals = normals; _normals = normals;
if (_points.size () < 2 || _normals.size() < 2) { if (_points.size() < 2 || _normals.size() < 2) {
return false; return false;
} }
int minVectorSize = _normals.size(); int minVectorSize = _normals.size();
if (_points.size() < minVectorSize) { if (_points.size() < minVectorSize) {
minVectorSize = _points.size(); minVectorSize = _points.size();
@ -123,15 +123,15 @@ bool PolyLineEntityItem::setNormals(const QVector<glm::vec3>& normals) {
_vertices.clear(); _vertices.clear();
glm::vec3 v1, v2, tangent, binormal, point; glm::vec3 v1, v2, tangent, binormal, point;
for (int i = 0; i < minVectorSize-1; i++) { for (int i = 0; i < minVectorSize - 1; i++) {
float width = _strokeWidths.at(i); float width = _strokeWidths.at(i);
point = _points.at(i); point = _points.at(i);
tangent = _points.at(i+1) - point; tangent = _points.at(i + 1) - point;
glm::vec3 normal = normals.at(i); glm::vec3 normal = normals.at(i);
binormal = glm::normalize(glm::cross(tangent, normal)) * width; binormal = glm::normalize(glm::cross(tangent, normal)) * width;
//This checks to make sure binormal is not a NAN //This checks to make sure binormal is not a NAN
assert(binormal.x == binormal.x); assert(binormal.x == binormal.x);
v1 = point + binormal; v1 = point + binormal;
@ -139,11 +139,11 @@ bool PolyLineEntityItem::setNormals(const QVector<glm::vec3>& normals) {
_vertices << v1 << v2; _vertices << v1 << v2;
} }
//for last point we can just assume binormals are same since it represents last two vertices of quad //for last point we can just assume binormals are same since it represents last two vertices of quad
point = _points.at(_points.size() - 1); point = _points.at(minVectorSize - 1);
v1 = point + binormal; v1 = point + binormal;
v2 = point - binormal; v2 = point - binormal;
_vertices << v1 << v2; _vertices << v1 << v2;
return true; return true;
} }
@ -157,7 +157,7 @@ bool PolyLineEntityItem::setLinePoints(const QVector<glm::vec3>& points) {
//Check to see if points actually changed. If they haven't, return before doing anything else //Check to see if points actually changed. If they haven't, return before doing anything else
else if (points.size() == _points.size()) { else if (points.size() == _points.size()) {
//same number of points, so now compare every point //same number of points, so now compare every point
for (int i = 0; i < points.size(); i++ ) { for (int i = 0; i < points.size(); i++) {
if (points.at(i) != _points.at(i)){ if (points.at(i) != _points.at(i)){
_pointsChanged = true; _pointsChanged = true;
break; break;
@ -171,9 +171,9 @@ bool PolyLineEntityItem::setLinePoints(const QVector<glm::vec3>& points) {
for (int i = 0; i < points.size(); i++) { for (int i = 0; i < points.size(); i++) {
glm::vec3 point = points.at(i); glm::vec3 point = points.at(i);
glm::vec3 halfBox = getDimensions() * 0.5f; glm::vec3 halfBox = getDimensions() * 0.5f;
if ((point.x < - halfBox.x || point.x > halfBox.x) || if ((point.x < -halfBox.x || point.x > halfBox.x) ||
(point.y < -halfBox.y || point.y > halfBox.y) || (point.y < -halfBox.y || point.y > halfBox.y) ||
(point.z < - halfBox.z || point.z > halfBox.z)) { (point.z < -halfBox.z || point.z > halfBox.z)) {
qDebug() << "Point is outside entity's bounding box"; qDebug() << "Point is outside entity's bounding box";
return false; return false;
} }
@ -183,18 +183,18 @@ bool PolyLineEntityItem::setLinePoints(const QVector<glm::vec3>& points) {
} }
int PolyLineEntityItem::readEntitySubclassDataFromBuffer(const unsigned char* data, int bytesLeftToRead, int PolyLineEntityItem::readEntitySubclassDataFromBuffer(const unsigned char* data, int bytesLeftToRead,
ReadBitstreamToTreeParams& args, ReadBitstreamToTreeParams& args,
EntityPropertyFlags& propertyFlags, bool overwriteLocalData) { EntityPropertyFlags& propertyFlags, bool overwriteLocalData) {
QWriteLocker lock(&_quadReadWriteLock); QWriteLocker lock(&_quadReadWriteLock);
int bytesRead = 0; int bytesRead = 0;
const unsigned char* dataAt = data; const unsigned char* dataAt = data;
READ_ENTITY_PROPERTY(PROP_COLOR, rgbColor, setColor); READ_ENTITY_PROPERTY(PROP_COLOR, rgbColor, setColor);
READ_ENTITY_PROPERTY(PROP_LINE_WIDTH, float, setLineWidth); READ_ENTITY_PROPERTY(PROP_LINE_WIDTH, float, setLineWidth);
READ_ENTITY_PROPERTY(PROP_LINE_POINTS, QVector<glm::vec3>, setLinePoints); READ_ENTITY_PROPERTY(PROP_LINE_POINTS, QVector<glm::vec3>, setLinePoints);
READ_ENTITY_PROPERTY(PROP_NORMALS, QVector<glm::vec3>, setNormals); READ_ENTITY_PROPERTY(PROP_NORMALS, QVector<glm::vec3>, setNormals);
READ_ENTITY_PROPERTY(PROP_STROKE_WIDTHS, QVector<float>, setStrokeWidths); READ_ENTITY_PROPERTY(PROP_STROKE_WIDTHS, QVector<float>, setStrokeWidths);
return bytesRead; return bytesRead;
} }
@ -211,16 +211,16 @@ EntityPropertyFlags PolyLineEntityItem::getEntityProperties(EncodeBitstreamParam
} }
void PolyLineEntityItem::appendSubclassData(OctreePacketData* packetData, EncodeBitstreamParams& params, void PolyLineEntityItem::appendSubclassData(OctreePacketData* packetData, EncodeBitstreamParams& params,
EntityTreeElementExtraEncodeData* modelTreeElementExtraEncodeData, EntityTreeElementExtraEncodeData* modelTreeElementExtraEncodeData,
EntityPropertyFlags& requestedProperties, EntityPropertyFlags& requestedProperties,
EntityPropertyFlags& propertyFlags, EntityPropertyFlags& propertyFlags,
EntityPropertyFlags& propertiesDidntFit, EntityPropertyFlags& propertiesDidntFit,
int& propertyCount, int& propertyCount,
OctreeElement::AppendState& appendState) const { OctreeElement::AppendState& appendState) const {
QWriteLocker lock(&_quadReadWriteLock); QWriteLocker lock(&_quadReadWriteLock);
bool successPropertyFits = true; bool successPropertyFits = true;
APPEND_ENTITY_PROPERTY(PROP_COLOR, getColor()); APPEND_ENTITY_PROPERTY(PROP_COLOR, getColor());
APPEND_ENTITY_PROPERTY(PROP_LINE_WIDTH, getLineWidth()); APPEND_ENTITY_PROPERTY(PROP_LINE_WIDTH, getLineWidth());
APPEND_ENTITY_PROPERTY(PROP_LINE_POINTS, getLinePoints()); APPEND_ENTITY_PROPERTY(PROP_LINE_POINTS, getLinePoints());