From a92a7980d7e5e094b608662408153798dc176b3b Mon Sep 17 00:00:00 2001
From: Brad Davis <bdavis@saintandreas.org>
Date: Thu, 9 Jul 2015 17:49:25 -0700
Subject: [PATCH] Restore audio scope

---
 interface/src/audio/AudioScope.cpp      | 41 ++++++++++++++-----------
 interface/src/audio/AudioScope.h        | 13 ++++----
 interface/src/ui/ApplicationOverlay.cpp |  3 +-
 3 files changed, 32 insertions(+), 25 deletions(-)

diff --git a/interface/src/audio/AudioScope.cpp b/interface/src/audio/AudioScope.cpp
index 8cc27341d6..7a93be80f1 100644
--- a/interface/src/audio/AudioScope.cpp
+++ b/interface/src/audio/AudioScope.cpp
@@ -16,6 +16,9 @@
 #include <AudioClient.h>
 #include <AudioConstants.h>
 #include <GeometryCache.h>
+#include <TextureCache.h>
+#include <gpu/Context.h>
+#include <GLMHelpers.h>
 
 #include "AudioScope.h"
 
@@ -104,7 +107,7 @@ void AudioScope::freeScope() {
     }
 }
 
-void AudioScope::render(int width, int height) {
+void AudioScope::render(RenderArgs* renderArgs, int width, int height) {
     
     if (!_isEnabled) {
         return;
@@ -122,24 +125,26 @@ void AudioScope::render(int width, int height) {
     int y = (height - (int)SCOPE_HEIGHT) / 2;
     int w = (int)SCOPE_WIDTH;
     int h = (int)SCOPE_HEIGHT;
-    
-    renderBackground(backgroundColor, x, y, w, h);
-    renderGrid(gridColor, x, y, w, h, gridRows, gridCols);
-    
-    renderLineStrip(_inputID, inputColor, x, y, _samplesPerScope, _scopeInputOffset, _scopeInput);
-    renderLineStrip(_outputLeftID, outputLeftColor, x, y, _samplesPerScope, _scopeOutputOffset, _scopeOutputLeft);
-    renderLineStrip(_outputRightD, outputRightColor, x, y, _samplesPerScope, _scopeOutputOffset, _scopeOutputRight);
+
+    gpu::Batch batch;
+    auto geometryCache = DependencyManager::get<GeometryCache>();
+    geometryCache->useSimpleDrawPipeline(batch);
+    auto textureCache = DependencyManager::get<TextureCache>();
+    batch.setUniformTexture(0, textureCache->getWhiteTexture());
+    mat4 legacyProjection = glm::ortho<float>(0, width, height, 0, -1000, 1000);
+    batch.setProjectionTransform(legacyProjection);
+    batch.setModelTransform(Transform());
+    batch.setViewTransform(Transform());
+    geometryCache->renderQuad(batch, x, y, w, h, backgroundColor);
+    geometryCache->renderGrid(batch, x, y, w, h, gridRows, gridCols, gridColor, _audioScopeGrid);
+    renderLineStrip(batch, _inputID, inputColor, x, y, _samplesPerScope, _scopeInputOffset, _scopeInput);
+    renderLineStrip(batch, _outputLeftID, outputLeftColor, x, y, _samplesPerScope, _scopeOutputOffset, _scopeOutputLeft);
+    renderLineStrip(batch, _outputRightD, outputRightColor, x, y, _samplesPerScope, _scopeOutputOffset, _scopeOutputRight);
+    renderArgs->_context->syncCache();
+    renderArgs->_context->render(batch);
 }
 
-void AudioScope::renderBackground(const glm::vec4& color, int x, int y, int width, int height) {
-    DependencyManager::get<GeometryCache>()->renderQuad(x, y, width, height, color);
-}
-
-void AudioScope::renderGrid(const glm::vec4& color, int x, int y, int width, int height, int rows, int cols) {
-    DependencyManager::get<GeometryCache>()->renderGrid(x, y, width, height, rows, cols, color, _audioScopeGrid);
-}
-
-void AudioScope::renderLineStrip(int id, const glm::vec4& color, int x, int y, int n, int offset, const QByteArray* byteArray) {
+void AudioScope::renderLineStrip(gpu::Batch& batch, int id, const glm::vec4& color, int x, int y, int n, int offset, const QByteArray* byteArray) {
 
     int16_t sample;
     int16_t* samples = ((int16_t*) byteArray->data()) + offset;
@@ -194,7 +199,7 @@ void AudioScope::renderLineStrip(int id, const glm::vec4& color, int x, int y, i
     
     
     geometryCache->updateVertices(id, points, color);
-    geometryCache->renderVertices(gpu::LINE_STRIP, id);
+    geometryCache->renderVertices(batch, gpu::LINE_STRIP, id);
 }
 
 int AudioScope::addBufferToScope(QByteArray* byteArray, int frameOffset, const int16_t* source, int sourceSamplesPerChannel,
diff --git a/interface/src/audio/AudioScope.h b/interface/src/audio/AudioScope.h
index cc9367e2d5..4ff4b55c29 100644
--- a/interface/src/audio/AudioScope.h
+++ b/interface/src/audio/AudioScope.h
@@ -14,11 +14,14 @@
 
 #include <glm/glm.hpp>
 
-#include <DependencyManager.h>
-
 #include <QByteArray>
 #include <QObject>
 
+#include <DependencyManager.h>
+#include <gpu/Batch.h>
+#include <RenderArgs.h>
+
+
 class AudioScope : public QObject, public Dependency {
     Q_OBJECT
     SINGLETON_DEPENDENCY
@@ -28,7 +31,7 @@ public:
     void freeScope();
     void reallocateScope(int frames);
     
-    void render(int width, int height);
+    void render(RenderArgs* renderArgs, int width, int height);
     
 public slots:
     void toggle();
@@ -48,9 +51,7 @@ private slots:
     
 private:
     // Audio scope methods for rendering
-    static void renderBackground(const glm::vec4& color, int x, int y, int width, int height);
-    void renderGrid(const glm::vec4& color, int x, int y, int width, int height, int rows, int cols);
-    void renderLineStrip(int id, const glm::vec4& color, int x, int  y, int n, int offset, const QByteArray* byteArray);
+    void renderLineStrip(gpu::Batch& batch, int id, const glm::vec4& color, int x, int  y, int n, int offset, const QByteArray* byteArray);
     
     // Audio scope methods for data acquisition
     int addBufferToScope(QByteArray* byteArray, int frameOffset, const int16_t* source, int sourceSamples,
diff --git a/interface/src/ui/ApplicationOverlay.cpp b/interface/src/ui/ApplicationOverlay.cpp
index e7d220893f..c58d450126 100644
--- a/interface/src/ui/ApplicationOverlay.cpp
+++ b/interface/src/ui/ApplicationOverlay.cpp
@@ -129,11 +129,12 @@ void ApplicationOverlay::renderOverlays(RenderArgs* renderArgs) {
     emit qApp->renderingOverlay();
     qApp->getOverlays().renderHUD(renderArgs);
 
+    DependencyManager::get<AudioScope>()->render(renderArgs, _overlayFramebuffer->size().width(), _overlayFramebuffer->size().height());
+
     glMatrixMode(GL_PROJECTION);
     glPopMatrix();
     glMatrixMode(GL_MODELVIEW);
 
-    renderArgs->_context->syncCache();
     fboViewport(_overlayFramebuffer);
 }