Add Util::map_[float|int].

Change Util for a namespace, not a class with static methods.
This commit is contained in:
Alexandre Quessy
2013-11-26 22:13:46 -05:00
parent ed1056527d
commit 83404dac47
2 changed files with 39 additions and 5 deletions
+33 -1
View File
@@ -18,8 +18,40 @@
*/
#include "Util.h"
#include <algorithm>
void Util::correctGlTexCoord(GLfloat x, GLfloat y)
namespace Util {
void correctGlTexCoord(GLfloat x, GLfloat y)
{
glTexCoord2f (x, 1.0f - y);
}
/**
* Convenience function to map a variable from one coordinate space
* to another.
* The result is clipped in the range [ostart, ostop]
* Make sure ostop is bigger than ostart.
*
* To map a MIDI control value into the [0,1] range:
* map(value, 0.0, 1.0, 0. 127.);
*
* Depends on: #include <algorithm>
*/
float map_float(float value, float istart, float istop, float ostart, float ostop)
{
float ret = ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
// In Processing, they don't do the following: (clipping)
return std::max(std::min(ret, ostop), ostart);
}
int map_int(int value, int istart, int istop, int ostart, int ostop)
{
float ret = ostart + (ostop - ostart) * ((value - istart) / float(istop - istart));
//g_print("%f = %d + (%d-%d) * ((%d-%d) / (%d-%d))", ret, ostart, ostop, ostart, value, istart, istop, istart);
// In Processing, they don't do the following: (clipping)
return std::max(std::min(int(ret), ostop), ostart);
}
} // end of namespace
+6 -4
View File
@@ -22,10 +22,12 @@
#include <GL/gl.h>
class Util {
public:
namespace Util {
static void correctGlTexCoord(GLfloat x, GLfloat y);
};
void correctGlTexCoord(GLfloat x, GLfloat y);
float map_float(float value, float istart, float istop, float ostart, float ostop);
int map_int(int value, int istart, int istop, int ostart, int ostop);
} // end of namespace
#endif /* UTIL_H_ */