From 21f12c1d6c9d458c9c98376f323ff1581ac32f3b Mon Sep 17 00:00:00 2001 From: David Rowe Date: Thu, 26 Feb 2015 13:50:43 -0800 Subject: [PATCH] Use cache ID when rendering talk bubble sphere --- interface/src/avatar/Avatar.cpp | 10 ++- interface/src/avatar/Avatar.h | 2 + libraries/render-utils/src/GeometryCache.cpp | 84 +++++++++++++++++--- libraries/render-utils/src/GeometryCache.h | 15 ++-- 4 files changed, 91 insertions(+), 20 deletions(-) diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index 6dc3d4b339..e5f296399e 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -77,7 +77,8 @@ Avatar::Avatar() : _moving(false), _collisionGroups(0), _initialized(false), - _shouldRenderBillboard(true) + _shouldRenderBillboard(true), + _voiceSphereID(GeometryCache::UNKNOWN_ID) { // we may have been created in the network thread, but we live in the main thread moveToThread(Application::getInstance()->thread()); @@ -439,8 +440,13 @@ void Avatar::render(const glm::vec3& cameraPosition, RenderMode renderMode, bool glPushMatrix(); glTranslatef(_position.x, _position.y, _position.z); glScalef(height, height, height); + + if (_voiceSphereID == GeometryCache::UNKNOWN_ID) { + _voiceSphereID = DependencyManager::get()->allocateID(); + } DependencyManager::get()->renderSphere(sphereRadius, 15, 15, - glm::vec4(SPHERE_COLOR[0], SPHERE_COLOR[1], SPHERE_COLOR[2], 1.0f - angle / MAX_SPHERE_ANGLE)); + glm::vec4(SPHERE_COLOR[0], SPHERE_COLOR[1], SPHERE_COLOR[2], 1.0f - angle / MAX_SPHERE_ANGLE), true, + _voiceSphereID); glPopMatrix(); } diff --git a/interface/src/avatar/Avatar.h b/interface/src/avatar/Avatar.h index 72b7ecf93a..d6977afce6 100644 --- a/interface/src/avatar/Avatar.h +++ b/interface/src/avatar/Avatar.h @@ -243,6 +243,8 @@ private: float getBillboardSize() const; static int _jointConesID; + + int _voiceSphereID; }; #endif // hifi_Avatar_h diff --git a/libraries/render-utils/src/GeometryCache.cpp b/libraries/render-utils/src/GeometryCache.cpp index dffadc730f..317b3bd6b9 100644 --- a/libraries/render-utils/src/GeometryCache.cpp +++ b/libraries/render-utils/src/GeometryCache.cpp @@ -53,18 +53,33 @@ const int NUM_COORDS_PER_VERTEX = 3; const int NUM_BYTES_PER_VERTEX = NUM_COORDS_PER_VERTEX * sizeof(GLfloat); const int NUM_BYTES_PER_INDEX = sizeof(GLushort); -void GeometryCache::renderSphere(float radius, int slices, int stacks, const glm::vec4& color, bool solid) { +void GeometryCache::renderSphere(float radius, int slices, int stacks, const glm::vec4& color, bool solid, int id) { + bool registered = (id != UNKNOWN_ID); Vec2Pair radiusKey(glm::vec2(radius, slices), glm::vec2(stacks, 0)); IntPair slicesStacksKey(slices, stacks); - Vec3Pair colorKey(glm::vec3(color.x, color.y, slices), glm::vec3(color.z, color.y, stacks)); + Vec3Pair colorKey(glm::vec3(color.x, color.y, slices), glm::vec3(color.z, color.w, stacks)); int vertices = slices * (stacks - 1) + 2; int indices = slices * (stacks - 1) * NUM_VERTICES_PER_TRIANGULATED_QUAD; - if (!_sphereVertices.contains(radiusKey)) { + if (registered && (!_registeredSphereVertices.contains(id) || _lastRegisteredSphereVertices[id] != radiusKey) + || !registered && !_sphereVertices.contains(radiusKey)) { + + if (registered && _registeredSphereVertices.contains(id)) { + _registeredSphereVertices[id].clear(); + #ifdef WANT_DEBUG + qDebug() << "renderSphere()... RELEASING REGISTERED VERTICES BUFFER"; + #endif + } + gpu::BufferPointer verticesBuffer(new gpu::Buffer()); - _sphereVertices[radiusKey] = verticesBuffer; + if (registered) { + _registeredSphereVertices[id] = verticesBuffer; + _lastRegisteredSphereVertices[id] = radiusKey; + } else { + _sphereVertices[radiusKey] = verticesBuffer; + } GLfloat* vertexData = new GLfloat[vertices * NUM_COORDS_PER_VERTEX]; GLfloat* vertex = vertexData; @@ -106,10 +121,29 @@ void GeometryCache::renderSphere(float radius, int slices, int stacks, const glm qDebug() << " _sphereVertices.size():" << _sphereVertices.size(); #endif } + #ifdef WANT_DEBUG + else if (registered) { + qDebug() << "renderSphere()... REUSING PREVIOUSLY REGISTERED VERTICES BUFFER"; + } + #endif - if (!_sphereIndices.contains(slicesStacksKey)) { + if (registered && (!_registeredSphereIndices.contains(id) || _lastRegisteredSphereIndices[id] != slicesStacksKey) + || !registered && !_sphereIndices.contains(slicesStacksKey)) { + + if (registered && _registeredSphereIndices.contains(id)) { + _registeredSphereIndices[id].clear(); + #ifdef WANT_DEBUG + qDebug() << "renderSphere()... RELEASING REGISTERED INDICES BUFFER"; + #endif + } + gpu::BufferPointer indicesBuffer(new gpu::Buffer()); - _sphereIndices[slicesStacksKey] = indicesBuffer; + if (registered) { + _registeredSphereIndices[id] = indicesBuffer; + _lastRegisteredSphereIndices[id] = slicesStacksKey; + } else { + _sphereIndices[slicesStacksKey] = indicesBuffer; + } GLushort* indexData = new GLushort[indices]; GLushort* index = indexData; @@ -164,7 +198,7 @@ void GeometryCache::renderSphere(float radius, int slices, int stacks, const glm delete[] indexData; #ifdef WANT_DEBUG - qDebug() << "GeometryCache::renderSphere()... --- CREATING INDEX BUFFER"; + qDebug() << "GeometryCache::renderSphere()... --- CREATING INDICES BUFFER"; qDebug() << " radius:" << radius; qDebug() << " slices:" << slices; qDebug() << " stacks:" << stacks; @@ -173,10 +207,29 @@ void GeometryCache::renderSphere(float radius, int slices, int stacks, const glm qDebug() << " _sphereIndices.size():" << _sphereIndices.size(); #endif } + #ifdef WANT_DEBUG + else if (registered) { + qDebug() << "renderSphere()... REUSING PREVIOUSLY REGISTERED INDICES BUFFER"; + } + #endif + + if (registered && (!_registeredSphereColors.contains(id) || _lastRegisteredSphereColors[id] != colorKey) + || !registered && !_sphereColors.contains(colorKey)) { + + if (registered && _registeredSphereColors.contains(id)) { + _registeredSphereColors[id].clear(); + #ifdef WANT_DEBUG + qDebug() << "renderSphere()... RELEASING REGISTERED COLORS BUFFER"; + #endif + } - if (!_sphereColors.contains(colorKey)) { gpu::BufferPointer colorBuffer(new gpu::Buffer()); - _sphereColors[colorKey] = colorBuffer; + if (registered) { + _registeredSphereColors[id] = colorBuffer; + _lastRegisteredSphereColors[id] = colorKey; + } else { + _sphereColors[colorKey] = colorBuffer; + } int compactColor = ((int(color.x * 255.0f) & 0xFF)) | ((int(color.y * 255.0f) & 0xFF) << 8) | @@ -201,11 +254,16 @@ void GeometryCache::renderSphere(float radius, int slices, int stacks, const glm qDebug() << " stacks:" << stacks; qDebug() << " _sphereColors.size():" << _sphereColors.size(); #endif - } - gpu::BufferPointer verticesBuffer = _sphereVertices[radiusKey]; - gpu::BufferPointer indicesBuffer = _sphereIndices[slicesStacksKey]; - gpu::BufferPointer colorBuffer = _sphereColors[colorKey]; + #ifdef WANT_DEBUG + else if (registered) { + qDebug() << "renderSphere()... REUSING PREVIOUSLY REGISTERED COLORS BUFFER"; + } + #endif + + gpu::BufferPointer verticesBuffer = registered ? _registeredSphereVertices[id] : _sphereVertices[radiusKey]; + gpu::BufferPointer indicesBuffer = registered ? _registeredSphereIndices[id] : _sphereIndices[slicesStacksKey]; + gpu::BufferPointer colorBuffer = registered ? _registeredSphereColors[id] : _sphereColors[colorKey]; const int VERTICES_SLOT = 0; const int NORMALS_SLOT = 1; diff --git a/libraries/render-utils/src/GeometryCache.h b/libraries/render-utils/src/GeometryCache.h index 864c5ff24c..92b6d44b6c 100644 --- a/libraries/render-utils/src/GeometryCache.h +++ b/libraries/render-utils/src/GeometryCache.h @@ -134,10 +134,10 @@ public: void renderCone(float base, float height, int slices, int stacks); - void renderSphere(float radius, int slices, int stacks, const glm::vec3& color, bool solid = true) - { renderSphere(radius, slices, stacks, glm::vec4(color, 1.0f), solid); } + void renderSphere(float radius, int slices, int stacks, const glm::vec3& color, bool solid = true, int id = UNKNOWN_ID) + { renderSphere(radius, slices, stacks, glm::vec4(color, 1.0f), solid, id); } - void renderSphere(float radius, int slices, int stacks, const glm::vec4& color, bool solid = true); + void renderSphere(float radius, int slices, int stacks, const glm::vec4& color, bool solid = true, int id = UNKNOWN_ID); void renderGrid(int xDivisions, int yDivisions, const glm::vec4& color); void renderGrid(int x, int y, int width, int height, int rows, int cols, const glm::vec4& color, int id = UNKNOWN_ID); void renderSolidCube(float size, const glm::vec4& color); @@ -290,10 +290,15 @@ private: QHash _gridColors; QHash _sphereVertices; + QHash _registeredSphereVertices; + QHash _lastRegisteredSphereVertices; QHash _sphereIndices; + QHash _registeredSphereIndices; + QHash _lastRegisteredSphereIndices; QHash _sphereColors; - - + QHash _registeredSphereColors; + QHash _lastRegisteredSphereColors; + QHash > _networkGeometry; };