|
From: Tapio V. <aa...@us...> - 2010-11-06 20:15:48
|
Module: performous
Branch: opengl2
Commit: 02e56deb7340342349093461e95c82f8f10ec73d
Author: Tapio Vierros <tap...@gm...>
Date: Sat Nov 6 20:26:27 2010 +0200
Expand VerexArray class and use it to draw notegraph waves.
Improves consistency and avoids "re-inventing the wheel".
---
game/fbo.hh | 3 ++-
game/glutil.hh | 40 ++++++++++++++++++++++------------------
game/notegraph.cc | 41 +++++++++++++++++------------------------
3 files changed, 41 insertions(+), 43 deletions(-)
diff --git a/game/fbo.hh b/game/fbo.hh
index 4199393..ded4f84 100644
--- a/game/fbo.hh
+++ b/game/fbo.hh
@@ -1,9 +1,10 @@
#pragma once
+#include <boost/noncopyable.hpp>
#include "video_driver.hh"
/// Frame Buffer Object class
-class FBO {
+class FBO: public boost::noncopyable {
public:
/// Generate the FBO and attach a fresh texture to it
FBO(): m_texture() {
diff --git a/game/glutil.hh b/game/glutil.hh
index c8e3a06..f25abe1 100644
--- a/game/glutil.hh
+++ b/game/glutil.hh
@@ -11,20 +11,6 @@
namespace glutil {
- struct Point {
- float vx;
- float vy;
- Point(float vx_, float vy_): vx(vx_), vy(vy_) {}
- };
-
- struct tPoint {
- float tx;
- float ty;
- float vx;
- float vy;
- tPoint(float tx_, float ty_, float vx_, float vy_): tx(tx_), ty(ty_), vx(vx_), vy(vy_) {}
- };
-
/// wrapper struct for RAII
struct PushMatrix {
PushMatrix() { glPushMatrix(); }
@@ -75,6 +61,7 @@ namespace glutil {
operator float const*() const { return reinterpret_cast<float const*>(this); }
};
+ /// handy vertex array capable of drawing itself
class VertexArray {
private:
int m_dimension;
@@ -84,6 +71,8 @@ namespace glutil {
std::vector<float> m_colors;
public:
+ VertexArray(): m_dimension(1) {}
+
VertexArray& Vertex(float x, float y) {
m_dimension = 2;
m_vertices.push_back(x);
@@ -120,7 +109,7 @@ namespace glutil {
return *this;
}
- VertexArray& Color(glutil::Color& c) {
+ VertexArray& Color(const glutil::Color& c) {
m_colors.push_back(c.r);
m_colors.push_back(c.g);
m_colors.push_back(c.b);
@@ -128,7 +117,7 @@ namespace glutil {
return *this;
}
- void Draw(GLint mode = GL_TRIANGLE_STRIP) {
+ void Draw(GLint mode = GL_TRIANGLE_STRIP) const {
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(m_dimension, GL_FLOAT, 0, &m_vertices.front());
if (m_texcoords.size()) {
@@ -143,7 +132,7 @@ namespace glutil {
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_FLOAT, 0, &m_colors.front());
}
- glDrawArrays(mode, 0, m_vertices.size()/m_dimension);
+ glDrawArrays(mode, 0, size());
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
@@ -151,7 +140,22 @@ namespace glutil {
glDisableClientState(GL_VERTEX_ARRAY);
}
- VertexArray() {}
+ bool empty() const {
+ return m_vertices.empty() && m_normals.empty() && m_texcoords.empty() && m_colors.empty();
+ }
+
+ unsigned size() const {
+ return m_vertices.size() / m_dimension;
+ }
+
+ void clear() {
+ m_vertices.clear(); m_normals.clear(); m_texcoords.clear(); m_colors.clear();
+ }
+
+ std::vector<float>& getVertices() { return m_vertices; }
+ std::vector<float>& getNormals() { return m_normals; }
+ std::vector<float>& getTexCoords() { return m_texcoords; }
+ std::vector<float>& getColors() { return m_colors; }
};
/// easy line
diff --git a/game/notegraph.cc b/game/notegraph.cc
index 2ab1e6a..dc00199 100644
--- a/game/notegraph.cc
+++ b/game/notegraph.cc
@@ -183,26 +183,19 @@ void NoteGraph::drawNotes() {
}
namespace {
- void strip(std::vector<glutil::tPoint>& points) {
- size_t s = points.size();
- if (s > 3) {
+ void strip(glutil::VertexArray& va) {
+ if (va.size() > 3) {
// Combine the two last points into a terminating point
- {
- glutil::tPoint& p = points[s-2];
- p.ty = 0.5f;
- p.vy = 0.5f * (p.vy + points[s-1].vy);
- }
- points.pop_back();
- // Render them as a triangle stripe
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- glTexCoordPointer(2, GL_FLOAT, sizeof(glutil::tPoint), &points.front().tx);
- glVertexPointer(2, GL_FLOAT, sizeof(glutil::tPoint), &points.front().vx);
- glDrawArrays(GL_TRIANGLE_STRIP, 0, points.size());
- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
- glDisableClientState(GL_VERTEX_ARRAY);
+ size_t s = va.getVertices().size();
+ va.getTexCoords()[s-3] = 0.5f; // y of second-to-last texcoord
+ float& vy = va.getVertices()[s-3]; // y of second-to-last vertex
+ vy = 0.5f * (vy + va.getVertices()[s-1]); // here is y of last vertex
+ va.getVertices().pop_back();
+ va.getTexCoords().pop_back();
+ // Now ready to draw
+ va.Draw();
}
- points.clear();
+ va.clear();
}
}
@@ -222,7 +215,7 @@ void NoteGraph::drawWaves(Database const& database) {
float tex = texOffset;
double t = idx * Engine::TIMESTEP;
double oldval = getNaN();
- std::vector<glutil::tPoint> points;
+ glutil::VertexArray va;
Notes::const_iterator noteIt = m_vocal.notes.begin();
glutil::Color c(Color(p->m_color.r, p->m_color.g, p->m_color.b, m_notealpha));
for (; idx < endIdx; ++idx, t += Engine::TIMESTEP) {
@@ -250,16 +243,16 @@ void NoteGraph::drawWaves(Database const& database) {
thickness *= 1.0 + 0.2 * std::sin(tex - 2.0 * texOffset); // Further animation :)
thickness *= -m_noteUnit;
// If there has been a break or if the pitch change is too fast, terminate and begin a new one
- if (oldval != oldval || std::abs(oldval - val) > 1) strip(points);
+ if (oldval != oldval || std::abs(oldval - val) > 1) strip(va);
// Add a point or a pair of points
- if (points.empty()) points.push_back(glutil::tPoint(tex, 0.5f, x, y));
+ if (!va.size()) va.TexCoord(tex, 0.5f).Vertex(x, y);
else {
- points.push_back(glutil::tPoint(tex, 0.0f, x, y - thickness));
- points.push_back(glutil::tPoint(tex, 1.0f, x, y + thickness));
+ va.TexCoord(tex, 0.0f).Vertex(x, y - thickness);
+ va.TexCoord(tex, 1.0f).Vertex(x, y + thickness);
}
oldval = val;
}
- strip(points);
+ strip(va);
}
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
|