rename parameters and reuse previously registered quad if geometry didn't change

This commit is contained in:
ZappoMan 2014-12-30 13:24:31 -08:00
parent be137534b5
commit 28569836bb
2 changed files with 107 additions and 67 deletions

View file

@ -667,20 +667,28 @@ void GeometryCache::renderWireCube(float size) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
} }
void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight, int quadID) { void GeometryCache::renderQuad(const glm::vec2& minCorner, const glm::vec2& maxCorner, int quadID) {
bool registeredQuad = (quadID != UNKNOWN_QUAD_ID); bool registeredQuad = (quadID != UNKNOWN_QUAD_ID);
Vec2Pair key(topLeft, bottomRight); Vec2Pair key(minCorner, maxCorner);
VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad2DVBOs[key]; VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad2DVBOs[key];
// if this is a registered quad, and we have buffers, then clear them and rebuild // if this is a registered quad, and we have buffers, then clear them and rebuild
// TODO: would be nice to only rebuild if the geometry changed from last time. // TODO: would be nice to only rebuild if the geometry changed from last time.
if (registeredQuad && vbo.first != 0) { if (registeredQuad && vbo.first != 0) {
glDeleteBuffers(1, &vbo.first); Vec2Pair& lastKey = _lastRegisteredQuad2D[quadID];
glDeleteBuffers(1, &vbo.second); if (lastKey != key) {
vbo.first = vbo.second = 0; glDeleteBuffers(1, &vbo.first);
glDeleteBuffers(1, &vbo.second);
vbo.first = vbo.second = 0;
#ifdef WANT_DEBUG
qDebug() << "renderQuad() vec2... RELEASING REGISTERED QUAD";
#endif // def WANT_DEBUG
}
#ifdef WANT_DEBUG #ifdef WANT_DEBUG
qDebug() << "renderQuad() vec2... RELEASING REGISTERED QUAD"; else {
qDebug() << "renderQuad() vec2... REUSING PREVIOUSLY REGISTERED QUAD";
}
#endif // def WANT_DEBUG #endif // def WANT_DEBUG
} }
@ -688,20 +696,21 @@ void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottom
const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat);
const int vertices = 4; const int vertices = 4;
const int indices = 4; const int indices = 4;
if (vbo.first == 0) { if (vbo.first == 0) {
_lastRegisteredQuad2D[quadID] = key;
int vertexPoints = vertices * FLOATS_PER_VERTEX; int vertexPoints = vertices * FLOATS_PER_VERTEX;
GLfloat* vertexData = new GLfloat[vertexPoints]; // only vertices, no normals because we're a 2D quad GLfloat* vertexData = new GLfloat[vertexPoints]; // only vertices, no normals because we're a 2D quad
GLfloat* vertex = vertexData; GLfloat* vertex = vertexData;
static GLubyte cannonicalIndices[indices] = {0, 1, 2, 3}; static GLubyte cannonicalIndices[indices] = {0, 1, 2, 3};
vertex[0] = topLeft.x; vertex[0] = minCorner.x;
vertex[1] = topLeft.y; vertex[1] = minCorner.y;
vertex[2] = bottomRight.x; vertex[2] = maxCorner.x;
vertex[3] = topLeft.y; vertex[3] = minCorner.y;
vertex[4] = bottomRight.x; vertex[4] = maxCorner.x;
vertex[5] = bottomRight.y; vertex[5] = maxCorner.y;
vertex[6] = topLeft.x; vertex[6] = minCorner.x;
vertex[7] = bottomRight.y; vertex[7] = maxCorner.y;
glGenBuffers(1, &vbo.first); glGenBuffers(1, &vbo.first);
glBindBuffer(GL_ARRAY_BUFFER, vbo.first); glBindBuffer(GL_ARRAY_BUFFER, vbo.first);
@ -741,21 +750,28 @@ void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottom
} }
void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight, void GeometryCache::renderQuad(const glm::vec2& minCorner, const glm::vec2& maxCorner,
const glm::vec2& texCoordTopLeft, const glm::vec2& texCoordBottomRight, int quadID) { const glm::vec2& texCoordMinCorner, const glm::vec2& texCoordMaxCorner, int quadID) {
bool registeredQuad = (quadID != UNKNOWN_QUAD_ID); bool registeredQuad = (quadID != UNKNOWN_QUAD_ID);
Vec2PairPair key(Vec2Pair(topLeft, bottomRight), Vec2Pair(texCoordTopLeft, texCoordBottomRight)); Vec2PairPair key(Vec2Pair(minCorner, maxCorner), Vec2Pair(texCoordMinCorner, texCoordMaxCorner));
VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad2DTextureVBOs[key]; VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad2DTextureVBOs[key];
// if this is a registered quad, and we have buffers, then clear them and rebuild // if this is a registered quad, and we have buffers, then clear them and rebuild
// TODO: would be nice to only rebuild if the geometry changed from last time.
if (registeredQuad && vbo.first != 0) { if (registeredQuad && vbo.first != 0) {
glDeleteBuffers(1, &vbo.first); Vec2PairPair& lastKey = _lastRegisteredQuad2DTexture[quadID];
glDeleteBuffers(1, &vbo.second); if (lastKey != key) {
vbo.first = vbo.second = 0; glDeleteBuffers(1, &vbo.first);
glDeleteBuffers(1, &vbo.second);
vbo.first = vbo.second = 0;
#ifdef WANT_DEBUG
qDebug() << "renderQuad() vec2 + texture... RELEASING REGISTERED QUAD";
#endif // def WANT_DEBUG
}
#ifdef WANT_DEBUG #ifdef WANT_DEBUG
qDebug() << "renderQuad() vec2 + texture... RELEASING REGISTERED QUAD"; else {
qDebug() << "renderQuad() vec2 + texture... REUSING PREVIOUSLY REGISTERED QUAD";
}
#endif // def WANT_DEBUG #endif // def WANT_DEBUG
} }
@ -763,32 +779,33 @@ void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottom
const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat);
const int vertices = 4; const int vertices = 4;
const int indices = 4; const int indices = 4;
if (vbo.first == 0) { if (vbo.first == 0) {
_lastRegisteredQuad2DTexture[quadID] = key;
int vertexPoints = vertices * FLOATS_PER_VERTEX; int vertexPoints = vertices * FLOATS_PER_VERTEX;
GLfloat* vertexData = new GLfloat[vertexPoints]; // text coords & vertices GLfloat* vertexData = new GLfloat[vertexPoints]; // text coords & vertices
GLfloat* vertex = vertexData; GLfloat* vertex = vertexData;
static GLubyte cannonicalIndices[indices] = {0, 1, 2, 3}; static GLubyte cannonicalIndices[indices] = {0, 1, 2, 3};
int v = 0; int v = 0;
vertex[v++] = topLeft.x; vertex[v++] = minCorner.x;
vertex[v++] = topLeft.y; vertex[v++] = minCorner.y;
vertex[v++] = texCoordTopLeft.x; vertex[v++] = texCoordMinCorner.x;
vertex[v++] = texCoordTopLeft.y; vertex[v++] = texCoordMinCorner.y;
vertex[v++] = bottomRight.x; vertex[v++] = maxCorner.x;
vertex[v++] = topLeft.y; vertex[v++] = minCorner.y;
vertex[v++] = texCoordBottomRight.x; vertex[v++] = texCoordMaxCorner.x;
vertex[v++] = texCoordTopLeft.y; vertex[v++] = texCoordMinCorner.y;
vertex[v++] = bottomRight.x; vertex[v++] = maxCorner.x;
vertex[v++] = bottomRight.y; vertex[v++] = maxCorner.y;
vertex[v++] = texCoordBottomRight.x; vertex[v++] = texCoordMaxCorner.x;
vertex[v++] = texCoordBottomRight.y; vertex[v++] = texCoordMaxCorner.y;
vertex[v++] = topLeft.x; vertex[v++] = minCorner.x;
vertex[v++] = bottomRight.y; vertex[v++] = maxCorner.y;
vertex[v++] = texCoordTopLeft.x; vertex[v++] = texCoordMinCorner.x;
vertex[v++] = texCoordBottomRight.y; vertex[v++] = texCoordMaxCorner.y;
glGenBuffers(1, &vbo.first); glGenBuffers(1, &vbo.first);
glBindBuffer(GL_ARRAY_BUFFER, vbo.first); glBindBuffer(GL_ARRAY_BUFFER, vbo.first);
@ -832,20 +849,28 @@ void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottom
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
} }
void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottomRight, int quadID) { void GeometryCache::renderQuad(const glm::vec3& minCorner, const glm::vec3& maxCorner, int quadID) {
bool registeredQuad = (quadID != UNKNOWN_QUAD_ID); bool registeredQuad = (quadID != UNKNOWN_QUAD_ID);
Vec3Pair key(topLeft, bottomRight); Vec3Pair key(minCorner, maxCorner);
VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad3DVBOs[key]; VerticesIndices& vbo = registeredQuad ? _registeredQuadVBOs[quadID] : _quad3DVBOs[key];
// if this is a registered quad, and we have buffers, then clear them and rebuild // if this is a registered quad, and we have buffers, then clear them and rebuild
// TODO: would be nice to only rebuild if the geometry changed from last time. // TODO: would be nice to only rebuild if the geometry changed from last time.
if (registeredQuad && vbo.first != 0) { if (registeredQuad && vbo.first != 0) {
glDeleteBuffers(1, &vbo.first); Vec3Pair& lastKey = _lastRegisteredQuad3D[quadID];
glDeleteBuffers(1, &vbo.second); if (lastKey != key) {
vbo.first = vbo.second = 0; glDeleteBuffers(1, &vbo.first);
glDeleteBuffers(1, &vbo.second);
vbo.first = vbo.second = 0;
#ifdef WANT_DEBUG
qDebug() << "renderQuad() vec3... RELEASING REGISTERED QUAD";
#endif // def WANT_DEBUG
}
#ifdef WANT_DEBUG #ifdef WANT_DEBUG
qDebug() << "renderQuad() vec3... RELEASING REGISTERED QUAD"; else {
qDebug() << "renderQuad() vec3... REUSING PREVIOUSLY REGISTERED QUAD";
}
#endif // def WANT_DEBUG #endif // def WANT_DEBUG
} }
@ -854,27 +879,28 @@ void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottom
const int vertices = 4; const int vertices = 4;
const int indices = 4; const int indices = 4;
if (vbo.first == 0) { if (vbo.first == 0) {
_lastRegisteredQuad3D[quadID] = key;
int vertexPoints = vertices * FLOATS_PER_VERTEX; int vertexPoints = vertices * FLOATS_PER_VERTEX;
GLfloat* vertexData = new GLfloat[vertexPoints]; // only vertices GLfloat* vertexData = new GLfloat[vertexPoints]; // only vertices
GLfloat* vertex = vertexData; GLfloat* vertex = vertexData;
static GLubyte cannonicalIndices[indices] = {0, 1, 2, 3}; static GLubyte cannonicalIndices[indices] = {0, 1, 2, 3};
int v = 0; int v = 0;
vertex[v++] = topLeft.x; vertex[v++] = minCorner.x;
vertex[v++] = topLeft.y; vertex[v++] = minCorner.y;
vertex[v++] = topLeft.z; vertex[v++] = minCorner.z;
vertex[v++] = bottomRight.x; vertex[v++] = maxCorner.x;
vertex[v++] = topLeft.y; vertex[v++] = minCorner.y;
vertex[v++] = topLeft.z; vertex[v++] = minCorner.z;
vertex[v++] = bottomRight.x; vertex[v++] = maxCorner.x;
vertex[v++] = bottomRight.y; vertex[v++] = maxCorner.y;
vertex[v++] = bottomRight.z; vertex[v++] = maxCorner.z;
vertex[v++] = topLeft.x; vertex[v++] = minCorner.x;
vertex[v++] = bottomRight.y; vertex[v++] = maxCorner.y;
vertex[v++] = bottomRight.z; vertex[v++] = maxCorner.z;
glGenBuffers(1, &vbo.first); glGenBuffers(1, &vbo.first);
glBindBuffer(GL_ARRAY_BUFFER, vbo.first); glBindBuffer(GL_ARRAY_BUFFER, vbo.first);
@ -936,11 +962,19 @@ void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottom
// if this is a registered quad, and we have buffers, then clear them and rebuild // if this is a registered quad, and we have buffers, then clear them and rebuild
// TODO: would be nice to only rebuild if the geometry changed from last time. // TODO: would be nice to only rebuild if the geometry changed from last time.
if (registeredQuad && vbo.first != 0) { if (registeredQuad && vbo.first != 0) {
glDeleteBuffers(1, &vbo.first); Vec3PairVec2Pair& lastKey = _lastRegisteredQuad3DTexture[quadID];
glDeleteBuffers(1, &vbo.second); if (lastKey != key) {
vbo.first = vbo.second = 0; glDeleteBuffers(1, &vbo.first);
glDeleteBuffers(1, &vbo.second);
vbo.first = vbo.second = 0;
#ifdef WANT_DEBUG
qDebug() << "renderQuad() vec3 + texture VBO... RELEASING REGISTERED QUAD";
#endif // def WANT_DEBUG
}
#ifdef WANT_DEBUG #ifdef WANT_DEBUG
qDebug() << "renderQuad() vec3 + texture VBO... RELEASING REGISTERED QUAD"; else {
qDebug() << "renderQuad() vec3 + texture... REUSING PREVIOUSLY REGISTERED QUAD";
}
#endif // def WANT_DEBUG #endif // def WANT_DEBUG
} }
@ -948,7 +982,8 @@ void GeometryCache::renderQuad(const glm::vec3& topLeft, const glm::vec3& bottom
const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat);
const int vertices = 4; const int vertices = 4;
const int indices = 4; const int indices = 4;
if (vbo.first == 0) { if (vbo.first == 0) {
_lastRegisteredQuad3DTexture[quadID] = key;
int vertexPoints = vertices * FLOATS_PER_VERTEX; int vertexPoints = vertices * FLOATS_PER_VERTEX;
GLfloat* vertexData = new GLfloat[vertexPoints]; // text coords & vertices GLfloat* vertexData = new GLfloat[vertexPoints]; // text coords & vertices
GLfloat* vertex = vertexData; GLfloat* vertex = vertexData;

View file

@ -96,12 +96,12 @@ public:
void renderQuad(int x, int y, int width, int height, int quadID = UNKNOWN_QUAD_ID) void renderQuad(int x, int y, int width, int height, int quadID = UNKNOWN_QUAD_ID)
{ renderQuad(glm::vec2(x,y), glm::vec2(x + width, y + height), quadID); } { renderQuad(glm::vec2(x,y), glm::vec2(x + width, y + height), quadID); }
void renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight, int quadID = UNKNOWN_QUAD_ID); void renderQuad(const glm::vec2& minCorner, const glm::vec2& maxCorner, int quadID = UNKNOWN_QUAD_ID);
void renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight, void renderQuad(const glm::vec2& minCorner, const glm::vec2& maxCorner,
const glm::vec2& texCoordTopLeft, const glm::vec2& texCoordBottomRight, int quadID = UNKNOWN_QUAD_ID); const glm::vec2& texCoordMinCorner, const glm::vec2& texCoordMaxCorner, int quadID = UNKNOWN_QUAD_ID);
void renderQuad(const glm::vec3& topLeft, const glm::vec3& bottomRight, int quadID = UNKNOWN_QUAD_ID); void renderQuad(const glm::vec3& minCorner, const glm::vec3& maxCorner, int quadID = UNKNOWN_QUAD_ID);
void renderQuad(const glm::vec3& topLeft, const glm::vec3& bottomLeft, void renderQuad(const glm::vec3& topLeft, const glm::vec3& bottomLeft,
const glm::vec3& bottomRight, const glm::vec3& topRight, const glm::vec3& bottomRight, const glm::vec3& topRight,
@ -139,6 +139,11 @@ private:
QHash<int, VerticesIndices> _registeredQuadVBOs; QHash<int, VerticesIndices> _registeredQuadVBOs;
int _nextQuadID; int _nextQuadID;
QHash<int, Vec2Pair> _lastRegisteredQuad2D;
QHash<int, Vec2PairPair> _lastRegisteredQuad2DTexture;
QHash<int, Vec3Pair> _lastRegisteredQuad3D;
QHash<int, Vec3PairVec2Pair> _lastRegisteredQuad3DTexture;
QHash<IntPair, QOpenGLBuffer> _gridBuffers; QHash<IntPair, QOpenGLBuffer> _gridBuffers;
QHash<QUrl, QWeakPointer<NetworkGeometry> > _networkGeometry; QHash<QUrl, QWeakPointer<NetworkGeometry> > _networkGeometry;