From 7147dec69f9069d300e53f5c59f88fe1d19c26fd Mon Sep 17 00:00:00 2001 From: John Maguire Date: Wed, 18 Aug 2010 16:26:36 +0000 Subject: [PATCH] Missing files! ^&%^#@ SVN. --- src/analyzers/analyzer.cpp | 15 ++++ src/analyzers/analyzer.h | 26 +++++++ src/analyzers/glblockanalyzer.cpp | 119 ++++++++++++++++++++++++++++++ src/analyzers/glblockanalyzer.h | 36 +++++++++ 4 files changed, 196 insertions(+) create mode 100644 src/analyzers/analyzer.cpp create mode 100644 src/analyzers/analyzer.h create mode 100644 src/analyzers/glblockanalyzer.cpp create mode 100644 src/analyzers/glblockanalyzer.h diff --git a/src/analyzers/analyzer.cpp b/src/analyzers/analyzer.cpp new file mode 100644 index 000000000..e9f36c934 --- /dev/null +++ b/src/analyzers/analyzer.cpp @@ -0,0 +1,15 @@ +#include "analyzer.h" + +#include "engines/enginebase.h" + +AnalyzerBase::AnalyzerBase(QWidget* parent) + : QGLWidget(parent), + engine_(NULL) { +} + +void AnalyzerBase::set_engine(Engine::Base* engine) { + disconnect(engine_); + engine_ = engine; + connect(engine_, SIGNAL(SpectrumAvailable(const QVector&)), + SLOT(SpectrumAvailable(const QVector&))); +} diff --git a/src/analyzers/analyzer.h b/src/analyzers/analyzer.h new file mode 100644 index 000000000..61c075af4 --- /dev/null +++ b/src/analyzers/analyzer.h @@ -0,0 +1,26 @@ +#ifndef ANALYZER_H +#define ANALYZER_H + +#include +#include + +#include "engines/engine_fwd.h" + +class AnalyzerBase : public QGLWidget { + Q_OBJECT + public: + AnalyzerBase(QWidget* parent = 0); + + public slots: + void set_engine(Engine::Base* engine); + + protected slots: + virtual void SpectrumAvailable(const QVector& spectrum) = 0; + + private: + + protected: + Engine::Base* engine_; +}; + +#endif diff --git a/src/analyzers/glblockanalyzer.cpp b/src/analyzers/glblockanalyzer.cpp new file mode 100644 index 000000000..2254a2184 --- /dev/null +++ b/src/analyzers/glblockanalyzer.cpp @@ -0,0 +1,119 @@ +#include "glblockanalyzer.h" + +#include + +const char* GLBlockAnalyzer::kName = "GL Block Analyzer"; + +GLBlockAnalyzer::GLBlockAnalyzer(QWidget* parent) + : AnalyzerBase(parent), + rectangles_size_(0), + shader_(this) { + qDebug() << Q_FUNC_INFO; +} + +void GLBlockAnalyzer::SpectrumAvailable(const QVector& spectrum) { + updateGL(); + current_spectrum_ = spectrum; +} + +void GLBlockAnalyzer::initializeGL() { + qDebug() << Q_FUNC_INFO; + glClearColor(0.0, 0.0, 0.0, 1.0); + glDisable(GL_DEPTH_TEST); + + shader_.addShaderFromSourceFile(QGLShader::Vertex, ":shaders/glblock_vert.glsl"); + shader_.addShaderFromSourceFile(QGLShader::Fragment, ":shaders/glblock_frag.glsl"); + shader_.link(); + if (!shader_.isLinked()) { + qWarning() << "Shader failed to link" + << shader_.log(); + } +} + +void GLBlockAnalyzer::resizeGL(int w, int h) { + qDebug() << Q_FUNC_INFO; + glViewport(0, 0, w, h); + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + +void GLBlockAnalyzer::paintGL() { + if (current_spectrum_.size() != rectangles_size_) { + rectangles_.reset(new float[current_spectrum_.size() * 3 * 4]); + rectangles_size_ = current_spectrum_.size(); + std::fill(rectangles_.get(), rectangles_.get() + rectangles_size_ * 3 * 4, 0.0f); + + tex_coords_.reset(new float[rectangles_size_ * 2 * 4]); + for (int i = 0; i < rectangles_size_ * 2 * 4; i += 2 * 4) { + tex_coords_[i] = 0.0; + tex_coords_[i+1] = 0.0; + + tex_coords_[i+2] = 1.0; + tex_coords_[i+3] = 0.0; + + tex_coords_[i+4] = 1.0; + tex_coords_[i+5] = 1.0; + + tex_coords_[i+6] = 0.0; + tex_coords_[i+7] = 1.0; + } + } + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glLoadIdentity(); + + const float width = 2.0 / current_spectrum_.size(); + + // Now we have x 0.0 -> 1.0 and y 0.0 -> 1.0. + glTranslatef(-1.0, -1.0, 0.0); + glScalef(2.0, 2.0, 1.0); + + + for (int i = 0; i < current_spectrum_.size(); ++i) { + const float x = width * i; + const float height = current_spectrum_[i]; + + float* current_rectangle = rectangles_.get() + i*4*3; // 4 points of size 3. + float* bottom_left = current_rectangle; + float* bottom_right = current_rectangle + 3; + float* top_right = current_rectangle + 6; + float* top_left = current_rectangle + 9; + + bottom_left[0] = x; + bottom_left[1] = 0.0; + + bottom_right[0] = x + width; + bottom_right[1] = 0.0; + + top_right[0] = x + width; + top_right[1] = height; + + top_left[0] = x; + top_left[1] = height; + } + + shader_.bind(); + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + + // Draw rectangles. + glVertexPointer(3, GL_FLOAT, 0, rectangles_.get()); + glTexCoordPointer(2, GL_FLOAT, 0, tex_coords_.get()); + + glColor3f(1.0, 0.0, 0.0); + glPolygonMode(GL_FRONT, GL_FILL); + glDrawArrays(GL_QUADS, 0, rectangles_size_ * 4); + + // Draw outlines. + glColor3f(1.0, 1.0, 1.0); + glPolygonMode(GL_FRONT, GL_LINE); + glDrawArrays(GL_QUADS, 0, rectangles_size_ * 4); + + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); +} diff --git a/src/analyzers/glblockanalyzer.h b/src/analyzers/glblockanalyzer.h new file mode 100644 index 000000000..832fe9703 --- /dev/null +++ b/src/analyzers/glblockanalyzer.h @@ -0,0 +1,36 @@ +#ifndef GLBLOCKANALYZER_H +#define GLBLOCKANALYZER_H + +#include "analyzer.h" + +#include + +#include + +class GLBlockAnalyzer : public AnalyzerBase { + Q_OBJECT + public: + Q_INVOKABLE GLBlockAnalyzer(QWidget* parent = 0); + + static const char* kName; + + protected: + void initializeGL(); + void resizeGL(int w, int h); + void paintGL(); + + protected slots: + // AnalyzerBase + void SpectrumAvailable(const QVector& spectrum); + + private: + QVector current_spectrum_; + + boost::scoped_array rectangles_; + boost::scoped_array tex_coords_; + size_t rectangles_size_; + + QGLShaderProgram shader_; +}; + +#endif