diff --git a/example/WiiMoteOpenGLDemo/Classes/EAGLView.h b/example/WiiMoteOpenGLDemo/Classes/EAGLView.h index 63ab9a34d..9d05d2a69 100644 --- a/example/WiiMoteOpenGLDemo/Classes/EAGLView.h +++ b/example/WiiMoteOpenGLDemo/Classes/EAGLView.h @@ -39,13 +39,20 @@ Note that setting the view non-opaque will only work if the EAGL surface has an GLuint textures[1]; GLfloat rota; + + BOOL useRotationMatrix; + // use rotation matrix + GLfloat rotationMatrix[16]; + + // use euler angles int rotateX; int rotateY; int rotateZ; } @property NSTimeInterval animationInterval; +@property BOOL useRotationMatrix; - (void)startAnimation; - (void)stopAnimation; @@ -54,6 +61,7 @@ Note that setting the view non-opaque will only work if the EAGL surface has an - (void)setupView; - (void)checkGLError:(BOOL)visibleCheck; +- (void)setRotationMatrix:(float[3][3]) matrix; - (void)setRotationX:(int)X Y:(int)Y Z:(int)Z; - (void)loadTexture; diff --git a/example/WiiMoteOpenGLDemo/Classes/EAGLView.m b/example/WiiMoteOpenGLDemo/Classes/EAGLView.m index c34bce3f5..f1d4bfa1d 100644 --- a/example/WiiMoteOpenGLDemo/Classes/EAGLView.m +++ b/example/WiiMoteOpenGLDemo/Classes/EAGLView.m @@ -34,7 +34,7 @@ @synthesize context; @synthesize animationTimer; @synthesize animationInterval; - +@synthesize useRotationMatrix; // You must implement this method + (Class)layerClass { @@ -160,15 +160,20 @@ glTranslatef(0.0, 0.0, -2.0); #ifdef USE_BLUETOOTH - glRotatef(rotateX, 1.0f, 0.0f, 0.0f); - glRotatef(rotateY, 0.0f, 1.0f, 0.0f); - glRotatef(rotateZ, 0.0f, 0.0f, 1.0f); - // glRotatef(1.0f, rotateX, rotateY, rotateZ); + if (self.useRotationMatrix) { + glMultMatrixf(rotationMatrix); + } else { + glRotatef(rotateX, 1.0f, 0.0f, 0.0f); + glRotatef(rotateY, 0.0f, 1.0f, 0.0f); + glRotatef(rotateZ, 0.0f, 0.0f, 1.0f); + // glRotatef(1.0f, rotateX, rotateY, rotateZ); + } #else rota += 1; glRotatef(rota, 0.0, 0.5, 0.0); glRotatef(rota, 0.0, 0.0, 1.0); #endif + glVertexPointer(3, GL_FLOAT, 0, cubeVertices); glEnableClientState(GL_VERTEX_ARRAY); @@ -357,8 +362,35 @@ } } +- (void)setRotationMatrix:(float[3][3]) matrix{ + useRotationMatrix = YES; + + // extend 3 by 3 matrix to 4 by 4 + int pos = 0; + rotationMatrix[pos++] = matrix[0][0]; + rotationMatrix[pos++] = matrix[0][1]; + rotationMatrix[pos++] = matrix[0][2]; + rotationMatrix[pos++] = 0; + + rotationMatrix[pos++] = matrix[1][0]; + rotationMatrix[pos++] = matrix[1][1]; + rotationMatrix[pos++] = matrix[1][2]; + rotationMatrix[pos++] = 0; + + rotationMatrix[pos++] = matrix[2][0]; + rotationMatrix[pos++] = matrix[2][1]; + rotationMatrix[pos++] = matrix[2][2]; + rotationMatrix[pos++] = 0; + + rotationMatrix[pos++] = 0; + rotationMatrix[pos++] = 0; + rotationMatrix[pos++] = 0; + rotationMatrix[pos++] = 1; +} - (void)setRotationX:(int)x Y:(int)y Z:(int)z{ + useRotationMatrix = NO; + // NSLog(@"BT data: %u %u %u", x , y ,z); rotateX = x; rotateY = y; diff --git a/example/WiiMoteOpenGLDemo/Classes/WiiMoteOpenGLDemoAppDelegate.m b/example/WiiMoteOpenGLDemo/Classes/WiiMoteOpenGLDemoAppDelegate.m index 9492ded73..62822faa8 100644 --- a/example/WiiMoteOpenGLDemo/Classes/WiiMoteOpenGLDemoAppDelegate.m +++ b/example/WiiMoteOpenGLDemo/Classes/WiiMoteOpenGLDemoAppDelegate.m @@ -45,6 +45,11 @@ #import #import +// quaternion rotation library +float norm(float *vector, int dim); +void normalizeVector(float *vector, int dim); +void getRotationMatrixFromVectors(float vin[3], float vout[3], float matrix[3][3]); + BTDevice *device; uint16_t wiiMoteConHandle = 0; WiiMoteOpenGLDemoAppDelegate * theMainApp; @@ -59,120 +64,38 @@ WiiMoteOpenGLDemoAppDelegate * theMainApp; #define SIZE 5 int counter; + +// define the rest position +static float restPosition[3] = {0,0,1}; +#define histSize 5 +int histX[histSize]; +int histY[histSize]; +int histZ[histSize]; + +static float addToHistory(int history[histSize], int value){ + int i; + float sum = 0; + for (i=0; i= 90 || roll <= -90) { - pitch = 360 - pitch; - } - // if ( (++counter & 15) == 0) - // NSLog(@"BT data: %f %f %f: pitch %i, roll %i, yaw %i", ax , ay ,az, pitch, roll, theta); -#endif - pitch = 0; - -#if 1 - static int lastPitch; - static int lastRoll; - - if (abs(lastPitch - pitch) > 180) { - if (pitch > lastPitch) { - pitch -= 360; - } else { - pitch += 360; - } - } - - if (abs(lastRoll - roll) > 180) { - if (roll > lastRoll) { - roll -= 360; - } else { - roll += 360; - } - } - - // moving average of size SIZE - static int pos = 0; - static int historyRoll[SIZE]; - static int historyPitch[SIZE]; - static int historyTheta[SIZE]; - - - historyRoll[pos] = roll; - historyPitch[pos] = pitch; - historyTheta[pos] = theta; - pos++; - if (pos==SIZE) pos = 0; - - pitch = roll = 0; - int i; - for (i=0;i +#include +#include + +#define rad M_PI/180.0 +#define deg 180.0/M_PI + +// define the rest position +static float restPosition[3] = {1,0,0}; + +float norm(float *vector, int dim){ + float result = 0; + int i; + + for (i=0; i