Home | History | Annotate | Download | only in kube
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.graphics.kube;
     18 
     19 public class Layer {
     20 
     21 	public Layer(int axis) {
     22 		// start with identity matrix for transformation
     23 		mAxis = axis;
     24 		mTransform.setIdentity();
     25 	}
     26 
     27 	public void startAnimation() {
     28 		for (int i = 0; i < mShapes.length; i++) {
     29 			GLShape shape = mShapes[i];
     30 			if (shape != null) {
     31 				shape.startAnimation();
     32 			}
     33 		}
     34 	}
     35 
     36 	public void endAnimation() {
     37 		for (int i = 0; i < mShapes.length; i++) {
     38 			GLShape shape = mShapes[i];
     39 			if (shape != null) {
     40 				shape.endAnimation();
     41 			}
     42 		}
     43 	}
     44 
     45 	public void setAngle(float angle) {
     46 		// normalize the angle
     47 		float twopi = (float)Math.PI *2f;
     48 		while (angle >= twopi) angle -= twopi;
     49 		while (angle < 0f) angle += twopi;
     50 //		mAngle = angle;
     51 
     52 		float sin = (float)Math.sin(angle);
     53 		float cos = (float)Math.cos(angle);
     54 
     55 		float[][] m = mTransform.m;
     56 		switch (mAxis) {
     57 			case kAxisX:
     58 				m[1][1] = cos;
     59 				m[1][2] = sin;
     60 				m[2][1] = -sin;
     61 				m[2][2] = cos;
     62 				m[0][0] = 1f;
     63 				m[0][1] = m[0][2] = m[1][0] = m[2][0] = 0f;
     64 				break;
     65 			case kAxisY:
     66 				m[0][0] = cos;
     67 				m[0][2] = sin;
     68 				m[2][0] = -sin;
     69 				m[2][2] = cos;
     70 				m[1][1] = 1f;
     71 				m[0][1] = m[1][0] = m[1][2] = m[2][1] = 0f;
     72 				break;
     73 			case kAxisZ:
     74 				m[0][0] = cos;
     75 				m[0][1] = sin;
     76 				m[1][0] = -sin;
     77 				m[1][1] = cos;
     78 				m[2][2] = 1f;
     79 				m[2][0] = m[2][1] = m[0][2] = m[1][2] = 0f;
     80 				break;
     81 		}
     82 
     83 		for (int i = 0; i < mShapes.length; i++) {
     84 			GLShape shape = mShapes[i];
     85 			if (shape != null) {
     86 				shape.animateTransform(mTransform);
     87 			}
     88 		}
     89 	}
     90 
     91 	GLShape[] mShapes = new GLShape[9];
     92 	M4 mTransform = new M4();
     93 //	float mAngle;
     94 
     95 	// which axis do we rotate around?
     96 	// 0 for X, 1 for Y, 2 for Z
     97 	int mAxis;
     98 	static public final int kAxisX = 0;
     99 	static public final int kAxisY = 1;
    100 	static public final int kAxisZ = 2;
    101 }
    102