Project

General

Profile

Download (3.51 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / quaternion / QuaternionRenderer.java @ a8c3ada7

1

    
2
package org.distorted.examples.quaternion;
3

    
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.util.Random;
7

    
8
import javax.microedition.khronos.egl.EGLConfig;
9
import javax.microedition.khronos.opengles.GL10;
10

    
11
import org.distorted.examples.R;
12

    
13
import org.distorted.library.EffectTypes;
14
import org.distorted.library.Interpolator;
15
import org.distorted.library.InterpolatorQuat;
16
import org.distorted.library.DistortedCubes;
17
import org.distorted.library.Float4D;
18
import org.distorted.library.Float3D;
19
import org.distorted.library.Distorted;
20

    
21
import android.graphics.Bitmap;
22
import android.graphics.BitmapFactory;
23
import android.opengl.GLES20;
24
import android.opengl.GLSurfaceView;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

    
28
class QuaternionRenderer implements GLSurfaceView.Renderer 
29
{
30
	private static final int NUM_QUATERNIONS = 5;
31
	private static final int SIZE = 100;
32
	
33
    private GLSurfaceView mView;
34
    private DistortedCubes mCube;
35
    private Random mRnd = new Random(System.currentTimeMillis());
36
    private InterpolatorQuat mRot;
37
    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
    public QuaternionRenderer(GLSurfaceView v) 
41
      {
42
      mView = v;
43
      mCube = new DistortedCubes( 3, "000010000", SIZE);
44
      
45
      mRot = new InterpolatorQuat();
46
      
47
      float x,y,z,w, len;
48
      
49
      for(int i=0; i<NUM_QUATERNIONS; i++)
50
        {
51
        x = 2*mRnd.nextFloat()-1;  
52
        y = 2*mRnd.nextFloat()-1;  
53
        z = 2*mRnd.nextFloat()-1;  
54
        w = 2*mRnd.nextFloat()-1;  
55
    	 
56
        len = (float)Math.sqrt( x*x+y*y+z*z+w*w );
57
    		
58
        mRot.add(new Float4D(x/len,y/len,z/len,w/len));
59
        }
60
    
61
      mRot.setCount(0);
62
      mRot.setDuration(8000);
63
      mRot.setMode(Interpolator.MODE_LOOP);
64
      }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
   
68
    public void onDrawFrame(GL10 glUnused) 
69
      {
70
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
71
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
72
      
73
      mCube.draw(System.currentTimeMillis());
74
      }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
    
78
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
79
      {
80
      mCube.abortEffects(EffectTypes.MATRIX);
81

    
82
      if( width > height )
83
        {
84
        mCube.move((width-height)/2 ,0, 0);
85
        mCube.scale(height/(3.0f*SIZE));
86
        }  
87
      else
88
        {   
89
        mCube.move(0 ,(height-width)/2, 0);
90
        mCube.scale(width/(3.0f*SIZE));
91
        }
92
     
93
      mCube.quaternion( new Float3D(3*SIZE/2,3*SIZE/2,0), mRot);
94
       
95
      Distorted.onSurfaceChanged(width, height); 
96
      }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
    
100
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
101
      {
102
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
103
      Bitmap bitmap;
104
        
105
      try 
106
        {
107
        bitmap = BitmapFactory.decodeStream(is);
108
        } 
109
      finally 
110
        {
111
        try 
112
          {
113
          is.close();
114
          } 
115
        catch(IOException e) { }
116
        }  
117
      
118
      mCube.setBitmap(bitmap);
119
      
120
      try
121
        {
122
        Distorted.onSurfaceCreated(mView.getContext());
123
        }
124
      catch(Exception ex)
125
        {
126
        android.util.Log.e("Quaternion", ex.getMessage() );
127
        }
128
      }
129
}
(2-2/3)