From 3d42d532e4ab2db7915826f85637d835bbeaa4dd Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Mon, 22 Dec 2014 17:43:02 -0800 Subject: [PATCH] more work on removing immediate mode GL_QUADS --- interface/src/Audio.cpp | 41 +++------ interface/src/avatar/Avatar.cpp | 19 ++--- interface/src/ui/ApplicationOverlay.cpp | 29 ++++--- interface/src/ui/RearMirrorTools.cpp | 24 ++---- .../src/ui/overlays/BillboardOverlay.cpp | 23 ++--- interface/src/ui/overlays/ImageOverlay.cpp | 32 +++---- libraries/render-utils/src/GeometryCache.cpp | 84 ++++++++++++++++--- libraries/render-utils/src/GeometryCache.h | 8 +- libraries/render-utils/src/RenderUtil.cpp | 19 ++--- 9 files changed, 147 insertions(+), 132 deletions(-) diff --git a/interface/src/Audio.cpp b/interface/src/Audio.cpp index a43bda34fa..5bcd9bb647 100644 --- a/interface/src/Audio.cpp +++ b/interface/src/Audio.cpp @@ -1306,21 +1306,13 @@ void Audio::renderToolBox(int x, int y, bool boxed) { } else { glColor3f(0.41f, 0.41f, 0.41f); } - glBegin(GL_QUADS); - - glTexCoord2f(1, 1); - glVertex2f(boxBounds.left(), boxBounds.top()); - - glTexCoord2f(0, 1); - glVertex2f(boxBounds.right(), boxBounds.top()); - - glTexCoord2f(0, 0); - glVertex2f(boxBounds.right(), boxBounds.bottom()); - glTexCoord2f(1, 0); - glVertex2f(boxBounds.left(), boxBounds.bottom()); - - glEnd(); + glm::vec2 topLeft(boxBounds.left(), boxBounds.top()); + glm::vec2 bottomRight(boxBounds.right(), boxBounds.bottom()); + glm::vec2 texCoordTopLeft(1,1); + glm::vec2 texCoordBottomRight(0,0); + + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); } _iconBounds = QRect(x, y, MUTE_ICON_SIZE, MUTE_ICON_SIZE); @@ -1345,21 +1337,14 @@ void Audio::renderToolBox(int x, int y, bool boxed) { } glColor3f(_iconColor, _iconColor, _iconColor); - glBegin(GL_QUADS); + + glm::vec2 topLeft(_iconBounds.left(), _iconBounds.top()); + glm::vec2 bottomRight(_iconBounds.right(), _iconBounds.bottom()); + glm::vec2 texCoordTopLeft(1,1); + glm::vec2 texCoordBottomRight(0,0); - glTexCoord2f(1.0f, 1.0f); - glVertex2f(_iconBounds.left(), _iconBounds.top()); - - glTexCoord2f(0.0f, 1.0f); - glVertex2f(_iconBounds.right(), _iconBounds.top()); - - glTexCoord2f(0.0f, 0.0f); - glVertex2f(_iconBounds.right(), _iconBounds.bottom()); - - glTexCoord2f(1.0f, 0.0f); - glVertex2f(_iconBounds.left(), _iconBounds.bottom()); - - glEnd(); + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); + glDisable(GL_TEXTURE_2D); } diff --git a/interface/src/avatar/Avatar.cpp b/interface/src/avatar/Avatar.cpp index 4c309b2c8c..25c5010b7b 100644 --- a/interface/src/avatar/Avatar.cpp +++ b/interface/src/avatar/Avatar.cpp @@ -633,17 +633,14 @@ void Avatar::renderBillboard() { glScalef(size, size, size); glColor3f(1.0f, 1.0f, 1.0f); - - glBegin(GL_QUADS); - glTexCoord2f(0.0f, 0.0f); - glVertex2f(-1.0f, -1.0f); - glTexCoord2f(1.0f, 0.0f); - glVertex2f(1.0f, -1.0f); - glTexCoord2f(1.0f, 1.0f); - glVertex2f(1.0f, 1.0f); - glTexCoord2f(0.0f, 1.0f); - glVertex2f(-1.0f, 1.0f); - glEnd(); + + glm::vec2 topLeft(-1.0f, -1.0f); + glm::vec2 bottomRight(1.0f, 1.0f); + glm::vec2 texCoordTopLeft(0.0f, 0.0f); + glm::vec2 texCoordBottomRight(1.0f, 1.0f); + + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); + glPopMatrix(); diff --git a/interface/src/ui/ApplicationOverlay.cpp b/interface/src/ui/ApplicationOverlay.cpp index 5267659ec2..a34584b24c 100644 --- a/interface/src/ui/ApplicationOverlay.cpp +++ b/interface/src/ui/ApplicationOverlay.cpp @@ -229,13 +229,14 @@ void ApplicationOverlay::displayOverlayTexture() { glDisable(GL_LIGHTING); glEnable(GL_BLEND); - glBegin(GL_QUADS); { - glColor4f(1.0f, 1.0f, 1.0f, _alpha); - glTexCoord2f(0, 0); glVertex2i(0, glCanvas->getDeviceHeight()); - glTexCoord2f(1, 0); glVertex2i(glCanvas->getDeviceWidth(), glCanvas->getDeviceHeight()); - glTexCoord2f(1, 1); glVertex2i(glCanvas->getDeviceWidth(), 0); - glTexCoord2f(0, 1); glVertex2i(0, 0); - } glEnd(); + glColor4f(1.0f, 1.0f, 1.0f, _alpha); + glm::vec2 topLeft(0.0f, 0.0f); + glm::vec2 bottomRight(glCanvas->getDeviceWidth(), glCanvas->getDeviceHeight()); + glm::vec2 texCoordTopLeft(0.0f, 1.0f); + glm::vec2 texCoordBottomRight(1.0f, 0.0f); + + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); + } glPopMatrix(); glDisable(GL_TEXTURE_2D); @@ -658,16 +659,16 @@ void ApplicationOverlay::renderControllerPointers() { mouseX -= reticleSize / 2.0f; mouseY += reticleSize / 2.0f; - glBegin(GL_QUADS); glColor3f(RETICLE_COLOR[0], RETICLE_COLOR[1], RETICLE_COLOR[2]); - glTexCoord2d(0.0f, 0.0f); glVertex2i(mouseX, mouseY); - glTexCoord2d(1.0f, 0.0f); glVertex2i(mouseX + reticleSize, mouseY); - glTexCoord2d(1.0f, 1.0f); glVertex2i(mouseX + reticleSize, mouseY - reticleSize); - glTexCoord2d(0.0f, 1.0f); glVertex2i(mouseX, mouseY - reticleSize); + glm::vec2 topLeft(mouseX, mouseY); + glm::vec2 bottomRight(mouseX + reticleSize, mouseY - reticleSize); + glm::vec2 texCoordTopLeft(0.0f, 0.0f); + glm::vec2 texCoordBottomRight(1.0f, 1.0f); - glEnd(); + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); + } } @@ -862,8 +863,6 @@ void ApplicationOverlay::renderAudioMeter() { audioLevel = AUDIO_RED_START; } - //glBegin(GL_QUADS); - if (audioLevel > AUDIO_GREEN_START) { if (!isClipping) { glColor3fv(AUDIO_METER_GREEN); diff --git a/interface/src/ui/RearMirrorTools.cpp b/interface/src/ui/RearMirrorTools.cpp index 37f7c6ae23..29c8e67f61 100644 --- a/interface/src/ui/RearMirrorTools.cpp +++ b/interface/src/ui/RearMirrorTools.cpp @@ -140,22 +140,14 @@ void RearMirrorTools::displayIcon(QRect bounds, QRect iconBounds, GLuint texture } glBindTexture(GL_TEXTURE_2D, textureId); - glBegin(GL_QUADS); - { - glTexCoord2f(0, 0); - glVertex2f(iconBounds.left(), iconBounds.bottom()); - - glTexCoord2f(0, 1); - glVertex2f(iconBounds.left(), iconBounds.top()); - - glTexCoord2f(1, 1); - glVertex2f(iconBounds.right(), iconBounds.top()); - - glTexCoord2f(1, 0); - glVertex2f(iconBounds.right(), iconBounds.bottom()); - - } - glEnd(); + + glm::vec2 topLeft(iconBounds.left(), iconBounds.top()); + glm::vec2 bottomRight(iconBounds.right(), iconBounds.bottom()); + glm::vec2 texCoordTopLeft(0.0f, 1.0f); + glm::vec2 texCoordBottomRight(1.0f, 0.0f); + + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); + glPopMatrix(); glMatrixMode(GL_MODELVIEW); diff --git a/interface/src/ui/overlays/BillboardOverlay.cpp b/interface/src/ui/overlays/BillboardOverlay.cpp index f9ad7fdb38..bcb69bdce3 100644 --- a/interface/src/ui/overlays/BillboardOverlay.cpp +++ b/interface/src/ui/overlays/BillboardOverlay.cpp @@ -96,21 +96,16 @@ void BillboardOverlay::render(RenderArgs* args) { xColor color = getColor(); float alpha = getAlpha(); glColor4f(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); - glBegin(GL_QUADS); { - glTexCoord2f((float)_fromImage.x() / (float)_size.width(), - (float)_fromImage.y() / (float)_size.height()); + + glm::vec2 topLeft(-x, -y); + glm::vec2 bottomRight(x, y); + glm::vec2 texCoordTopLeft((float)_fromImage.x() / (float)_size.width(), + (float)_fromImage.y() / (float)_size.height()); + glm::vec2 texCoordBottomRight(((float)_fromImage.x() + (float)_fromImage.width()) / (float)_size.width(), + ((float)_fromImage.y() + (float)_fromImage.height()) / _size.height()); - glVertex2f(-x, -y); - glTexCoord2f(((float)_fromImage.x() + (float)_fromImage.width()) / (float)_size.width(), - (float)_fromImage.y() / (float)_size.height()); - glVertex2f(x, -y); - glTexCoord2f(((float)_fromImage.x() + (float)_fromImage.width()) / (float)_size.width(), - ((float)_fromImage.y() + (float)_fromImage.height()) / _size.height()); - glVertex2f(x, y); - glTexCoord2f((float)_fromImage.x() / (float)_size.width(), - ((float)_fromImage.y() + (float)_fromImage.height()) / (float)_size.height()); - glVertex2f(-x, y); - } glEnd(); + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); + } } glPopMatrix(); diff --git a/interface/src/ui/overlays/ImageOverlay.cpp b/interface/src/ui/overlays/ImageOverlay.cpp index b14f49737f..d7a6889c05 100644 --- a/interface/src/ui/overlays/ImageOverlay.cpp +++ b/interface/src/ui/overlays/ImageOverlay.cpp @@ -14,6 +14,9 @@ #include #include #include + +#include +#include #include #include "ImageOverlay.h" @@ -105,27 +108,16 @@ void ImageOverlay::render(RenderArgs* args) { int top = _bounds.top(); int bottom = _bounds.bottom() + 1; - glBegin(GL_QUADS); - if (_renderImage) { - glTexCoord2f(x, 1.0f - y); - } - glVertex2f(left, top); + glm::vec2 topLeft(left, top); + glm::vec2 bottomRight(right, bottom); + glm::vec2 texCoordTopLeft(x, 1.0f - y); + glm::vec2 texCoordBottomRight(x + w, 1.0f - (y + h)); - if (_renderImage) { - glTexCoord2f(x + w, 1.0f - y); - } - glVertex2f(right, top); - - if (_renderImage) { - glTexCoord2f(x + w, 1.0f - (y + h)); - } - glVertex2f(right, bottom); - - if (_renderImage) { - glTexCoord2f(x, 1.0f - (y + h)); - } - glVertex2f(left, bottom); - glEnd(); + if (_renderImage) { + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); + } else { + DependencyManager::get()->renderQuad(topLeft, bottomRight); + } if (_renderImage) { glDisable(GL_TEXTURE_2D); diff --git a/libraries/render-utils/src/GeometryCache.cpp b/libraries/render-utils/src/GeometryCache.cpp index 65069b773f..f6ea9247ce 100644 --- a/libraries/render-utils/src/GeometryCache.cpp +++ b/libraries/render-utils/src/GeometryCache.cpp @@ -665,22 +665,15 @@ void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottom Vec2Pair key(topLeft, bottomRight); VerticesIndices& vbo = _quad2DVBOs[key]; const int FLOATS_PER_VERTEX = 2; + const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); const int vertices = 4; const int indices = 4; if (vbo.first == 0) { int vertexPoints = vertices * FLOATS_PER_VERTEX; GLfloat* vertexData = new GLfloat[vertexPoints]; // only vertices, no normals because we're a 2D quad GLfloat* vertex = vertexData; - // index array of vertex array for glDrawRangeElement() as a GL_LINES for each edge static GLubyte cannonicalIndices[indices] = { 0, 1, 2, 3 }; - //glBegin(GL_QUADS); - // glVertex2f(left, top); - // glVertex2f(right, top); - // glVertex2f(right, bottom); - // glVertex2f(left, bottom); - //glEnd(); - vertex[0] = topLeft.x; vertex[1] = topLeft.y; vertex[2] = bottomRight.x; @@ -722,12 +715,77 @@ void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottom } -/* -void GeometryCache::renderQuad(cont glm::vec2& topLeft, cont glm::vec2& bottomRight, - cont glm::vec2& texCoordTopLeft, cont glm::vec2& texCoordBottomRight) { -} -*/ +void GeometryCache::renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight, + const glm::vec2& texCoordTopLeft, const glm::vec2& texCoordBottomRight) { + Vec2PairPair key(Vec2Pair(topLeft, bottomRight), Vec2Pair(texCoordTopLeft, texCoordBottomRight)); + + VerticesIndices& vbo = _quad2DTextureVBOs[key]; + const int FLOATS_PER_VERTEX = 2 * 2; // text coords & vertices + const int NUM_BYTES_PER_VERTEX = FLOATS_PER_VERTEX * sizeof(GLfloat); + const int vertices = 4; + const int indices = 4; + if (vbo.first == 0) { + int vertexPoints = vertices * FLOATS_PER_VERTEX; + GLfloat* vertexData = new GLfloat[vertexPoints]; // text coords & vertices + GLfloat* vertex = vertexData; + static GLubyte cannonicalIndices[indices] = { 0, 1, 2, 3 }; + int v = 0; + vertex[v++] = topLeft.x; + vertex[v++] = topLeft.y; + vertex[v++] = texCoordTopLeft.x; + vertex[v++] = texCoordTopLeft.y; + + vertex[v++] = bottomRight.x; + vertex[v++] = topLeft.y; + vertex[v++] = texCoordBottomRight.x; + vertex[v++] = texCoordTopLeft.y; + + vertex[v++] = bottomRight.x; + vertex[v++] = bottomRight.y; + vertex[v++] = texCoordBottomRight.x; + vertex[v++] = texCoordBottomRight.y; + + vertex[v++] = topLeft.x; + vertex[v++] = bottomRight.y; + vertex[v++] = texCoordTopLeft.x; + vertex[v++] = texCoordBottomRight.y; + + glGenBuffers(1, &vbo.first); + glBindBuffer(GL_ARRAY_BUFFER, vbo.first); + glBufferData(GL_ARRAY_BUFFER, vertices * NUM_BYTES_PER_VERTEX, vertexData, GL_STATIC_DRAW); + delete[] vertexData; + + GLushort* indexData = new GLushort[indices]; + GLushort* index = indexData; + for (int i = 0; i < indices; i++) { + index[i] = cannonicalIndices[i]; + } + + glGenBuffers(1, &vbo.second); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices * NUM_BYTES_PER_INDEX, indexData, GL_STATIC_DRAW); + delete[] indexData; + + qDebug() << "new quad + texture VBO made -- _quad2DTextureVBOs.size():" << _quad2DTextureVBOs.size(); + } else { + glBindBuffer(GL_ARRAY_BUFFER, vbo.first); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo.second); + } + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glVertexPointer(2, GL_FLOAT, NUM_BYTES_PER_VERTEX, 0); + glTexCoordPointer(2, GL_FLOAT, NUM_BYTES_PER_VERTEX, (const void *)(2 * sizeof(float))); + + glDrawRangeElementsEXT(GL_QUADS, 0, vertices - 1, indices, GL_UNSIGNED_SHORT, 0); + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); +} QSharedPointer GeometryCache::getGeometry(const QUrl& url, const QUrl& fallback, bool delayLoad) { return getResource(url, fallback, delayLoad).staticCast(); diff --git a/libraries/render-utils/src/GeometryCache.h b/libraries/render-utils/src/GeometryCache.h index 1c3f2114d6..f7971e78d6 100644 --- a/libraries/render-utils/src/GeometryCache.h +++ b/libraries/render-utils/src/GeometryCache.h @@ -70,11 +70,9 @@ public: void renderWireCube(float size); - void renderQuad(int x, int y, int width, int height) { - renderQuad(glm::vec2(x,y), glm::vec2(x + width, y + height)); - }; - + void renderQuad(int x, int y, int width, int height) { renderQuad(glm::vec2(x,y), glm::vec2(x + width, y + height)); } void renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight); + void renderQuad(const glm::vec2& topLeft, const glm::vec2& bottomRight, const glm::vec2& texCoordTopLeft, const glm::vec2& texCoordBottomRight); @@ -104,7 +102,7 @@ private: QHash _wireCubeVBOs; QHash _solidCubeVBOs; QHash _quad2DVBOs; - //QHash _quad2DTextureVBOs; + QHash _quad2DTextureVBOs; QHash _gridBuffers; diff --git a/libraries/render-utils/src/RenderUtil.cpp b/libraries/render-utils/src/RenderUtil.cpp index d70c972c87..140ced4c52 100644 --- a/libraries/render-utils/src/RenderUtil.cpp +++ b/libraries/render-utils/src/RenderUtil.cpp @@ -10,17 +10,16 @@ // #include + +#include +#include "GeometryCache.h" + #include "RenderUtil.h" void renderFullscreenQuad(float sMin, float sMax, float tMin, float tMax) { - glBegin(GL_QUADS); - glTexCoord2f(sMin, tMin); - glVertex2f(-1.0f, -1.0f); - glTexCoord2f(sMax, tMin); - glVertex2f(1.0f, -1.0f); - glTexCoord2f(sMax, tMax); - glVertex2f(1.0f, 1.0f); - glTexCoord2f(sMin, tMax); - glVertex2f(-1.0f, 1.0f); - glEnd(); + glm::vec2 topLeft(-1.0f, -1.0f); + glm::vec2 bottomRight(1.0f, 1.0f); + glm::vec2 texCoordTopLeft(sMin, tMin); + glm::vec2 texCoordBottomRight(sMax, tMax); + DependencyManager::get()->renderQuad(topLeft, bottomRight, texCoordTopLeft, texCoordBottomRight); }