|
From: Lasse Kärkkäi. <tr...@us...> - 2011-03-13 01:39:35
|
Author: Lasse Karkkainen <tro...@tr...>
Date: Sun Mar 13 02:39:14 2011 +0100
New glmath functions for vectors, fixed neckglow that was broken a few commits back (possibly a better implementation now)
---
game/glmath.hh | 8 ++++++++
game/guitargraph.cc | 39 ++++++++++++++++-----------------------
game/guitargraph.hh | 2 +-
3 files changed, 25 insertions(+), 24 deletions(-)
diff --git a/game/glmath.hh b/game/glmath.hh
index 2f1da54..b65e30d 100644
--- a/game/glmath.hh
+++ b/game/glmath.hh
@@ -9,6 +9,10 @@
namespace glmath {
+ template <typename T> T mix(T const& a, T const& b, double blend) {
+ return (1.0-blend) * a + blend * b;
+ }
+
struct vec4;
struct vec3 {
@@ -32,6 +36,10 @@ namespace glmath {
inline vec3::vec3(vec4 const& v): x(v.x), y(v.y), z(v.z) {}
static inline vec3 operator*(float k, vec3 const& v) { return vec3(k * v.x, k * v.y, k * v.z); }
+ static inline vec4 operator*(float k, vec4 const& v) { return vec4(k * v.x, k * v.y, k * v.z, k * v.w); }
+
+ static inline vec3 operator+(vec3 const& a, vec4 const& b) { return vec3(a.x + b.x, a.y + b.y, a.z + b.z); }
+ static inline vec4 operator+(vec4 const& a, vec4 const& b) { return vec4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); }
static inline float dot(vec3 const& a, vec3 const& b) {
return a.x * b.x + a.y * b.y + a.z * b.z;
diff --git a/game/guitargraph.cc b/game/guitargraph.cc
index 116e741..f3519e9 100644
--- a/game/guitargraph.cc
+++ b/game/guitargraph.cc
@@ -784,10 +784,6 @@ namespace {
}
}
- // EVIL GLOBALS, FIXME!
- float ng_r = 0, ng_g = 0, ng_b = 0; // neck glow color components
- int ng_ccnt = 0; // neck glow color count
-
}
void GuitarGraph::drawNotes(double time) {
@@ -805,6 +801,9 @@ void GuitarGraph::drawNotes(double time) {
}
}
if (time != time) return; // Check that time is not NaN
+
+ glmath::vec4 neckglow; // Used for calculating the average neck color
+
// Iterate chords
for (Chords::iterator it = m_chords.begin(); it != m_chords.end(); ++it) {
float tBeg = it->begin - time;
@@ -841,7 +840,7 @@ void GuitarGraph::drawNotes(double time) {
if (!joining(time)) {
// Get a color for the fret and adjust it if GodMode is on
c = colorize(color(fret), it->begin);
- if (glow > 0.1f) { ng_r+=c.r; ng_g+=c.g; ng_b+=c.b; ng_ccnt++; } // neck glow
+ if (glow > 0.1f) { neckglow = neckglow + glmath::vec4(c.r, c.g, c.b, 1.0); } // neck glow tracking
// Further adjust the color if the note is hit
c.r += glow * 0.2f;
c.g += glow * 0.2f;
@@ -873,6 +872,11 @@ void GuitarGraph::drawNotes(double time) {
}
}
}
+ // Mangle neck glow color as needed
+ // Convert sum into average and apply correctness as premultiplied alpha
+ if (neckglow.w > 0.0) neckglow = (correctness() / neckglow.w) * neckglow;
+ // Blend into use slowly
+ m_neckglowColor = glmath::mix(m_neckglowColor, neckglow, 0.05);
}
double GuitarGraph::neckWidth() const { return std::min(0.5, m_width.get()); }
@@ -1006,24 +1010,13 @@ void GuitarGraph::draw(double time) {
ViewTrans view(m_cx.get(), 0.0, 0.75); // Apply a per-player local perspective
drawNeckStuff(time);
-
- // Bottom neck glow
- if (ng_ccnt > 0) {
- // Mangle color
- if (m_neckglowColor.r > 0 || m_neckglowColor.g > 0 || m_neckglowColor.b > 0) {
- m_neckglowColor.r = blend(m_neckglowColor.r, ng_r / ng_ccnt, 0.95);
- m_neckglowColor.g = blend(m_neckglowColor.g, ng_g / ng_ccnt, 0.95);
- m_neckglowColor.b = blend(m_neckglowColor.b, ng_b / ng_ccnt, 0.95);
- } else { // We don't want to fade from black in the start
- m_neckglowColor.r = ng_r / ng_ccnt;
- m_neckglowColor.g = ng_g / ng_ccnt;
- m_neckglowColor.b = ng_b / ng_ccnt;
- }
- m_neckglowColor.a = correctness();
- }
- if (correctness() > 0) {
- // Glow drawing
- ColorTrans c(m_neckglowColor);
+
+ if (m_neckglowColor.w > 0.0) {
+ // Neck glow drawing
+ using namespace glmath;
+ double a = m_neckglowColor.w;
+ vec4 color((1.0 / a) * vec3(m_neckglowColor), a); // Convert into non-premultiplied
+ ColorTrans c(glmath::mat4::diagonal(color));
m_neckglow.dimensions.screenBottom(0.0).middle().fixedWidth(neckWidth());
m_neckglow.draw();
}
diff --git a/game/guitargraph.hh b/game/guitargraph.hh
index 2e68b0d..dd97ffb 100644
--- a/game/guitargraph.hh
+++ b/game/guitargraph.hh
@@ -88,7 +88,7 @@ class GuitarGraph: public InstrumentGraph {
Texture m_flame_godmode;
Surface m_tap; /// image for 2d HOPO note cap
Surface m_neckglow; /// image for the glow from the bottom of the neck
- Color m_neckglowColor;
+ glmath::vec4 m_neckglowColor;
Object3d m_fretObj; /// 3d object for regular note
Object3d m_tappableObj; /// 3d object for the HOPO note cap
std::vector<std::string> m_samples; /// sound effects
|