diff options
author | Matt Turner <mattst88@gmail.com> | 2012-10-24 13:09:59 -0700 |
---|---|---|
committer | Matt Turner <mattst88@gmail.com> | 2012-10-24 13:09:59 -0700 |
commit | a645d75861bd8db86172007be011df17d2c51fac (patch) | |
tree | 551b9daa3a6968b7c6be116cfd29c9bd522a023b | |
parent | 67050b3ae4b7b8878c38b9d4e70a50ff219f6766 (diff) |
Add rotate_x/y_axis functions and unit tests
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/check_rotation.cpp | 45 | ||||
-rw-r--r-- | src/main.cpp | 19 |
3 files changed, 65 insertions, 1 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 5fe0d86..20fd98c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,7 +21,7 @@ # USE OR OTHER DEALINGS IN THE SOFTWARE. bin_PROGRAMS = rotating_cubes -rotating_cubes_SOURCES = main.cpp shader.cpp +rotating_cubes_SOURCES = main.cpp shader.cpp check_rotation.cpp AM_CXXFLAGS= $(GLU3_CFLAGS) rotating_cubes_LDADD = $(FRAMEWORK_LIBS) $(GLU3_LIBS) -lEGL diff --git a/src/check_rotation.cpp b/src/check_rotation.cpp new file mode 100644 index 0000000..858bae3 --- /dev/null +++ b/src/check_rotation.cpp @@ -0,0 +1,45 @@ +#include <glu3.h> + +#include <cassert> +#include <cmath> +#include <cfloat> + +extern GLUmat4 rotate_x_axis(float theta); +extern GLUmat4 rotate_y_axis(float theta); + +#define EPSILONS 2 + +static inline bool equal(const float & a, const float & b) +{ + return (fabs(a - b) < EPSILONS * FLT_EPSILON); +} + +static inline bool equal(const GLUvec4 & a, const GLUvec4 & b) +{ + return equal(a.values[0], b.values[0]) && + equal(a.values[1], b.values[1]) && + equal(a.values[2], b.values[2]) && + equal(a.values[3], b.values[3]); +} + +static inline bool equal(const GLUmat4 & a, const GLUmat4 & b) +{ + return equal(a.col[0], b.col[0]) && + equal(a.col[1], b.col[1]) && + equal(a.col[2], b.col[2]) && + equal(a.col[3], b.col[3]); +} + +void check_rotation(void) +{ + // Rotation by 0 and 2PI are equal (within 2 epsilons) + assert(equal(rotate_x_axis(0.0f), rotate_x_axis(2.0f * M_PI))); + assert(equal(rotate_y_axis(0.0f), rotate_y_axis(2.0f * M_PI))); + assert(equal(rotate_x_axis(M_PI) * rotate_x_axis(M_PI), + rotate_x_axis(0.0f))); + assert(equal(rotate_y_axis(M_PI) * rotate_y_axis(M_PI), + rotate_y_axis(0.0f))); + + assert(equal(rotate_x_axis(M_PI) * rotate_y_axis(M_PI), + rotate_y_axis(M_PI) * rotate_x_axis(M_PI))); +} diff --git a/src/main.cpp b/src/main.cpp index 0cc151b..a9d4c74 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,6 +42,22 @@ static float orbit_angle = 0.0f; static float eye_distance = 15.0f; static float fov = 45.0f; +GLUmat4 rotate_x_axis(float theta) +{ + return GLUmat4 (GLUvec4 (1.0f, 0.0f, 0.0f, 0.0f), + GLUvec4 (0.0f, cosf(theta), sinf(theta), 0.0f), + GLUvec4 (0.0f, -sinf(theta), cosf(theta), 0.0f), + GLUvec4 (0.0f, 0.0f, 0.0f, 1.0f)); +} + +GLUmat4 rotate_y_axis(float theta) +{ + return GLUmat4 (GLUvec4 (cosf(theta), 0.0f, -sinf(theta), 0.0f), + GLUvec4 (0.0f, 1.0f, 0.0f, 0.0f), + GLUvec4 (sinf(theta), 0.0f, cosf(theta), 0.0f), + GLUvec4 (0.0f, 0.0f, 0.0f, 1.0f)); +} + static Uint32 t0; static GLuint bo; @@ -473,6 +489,7 @@ timer_callback(Uint32 interval, void *not_used) return interval; } +extern void check_rotation(void); int main(int argc, char **argv) @@ -480,6 +497,8 @@ main(int argc, char **argv) (void) argc; (void) argv; + check_rotation(); + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) { exit(1); } |