Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesRenderer.java @ ed1c0b33

1

    
2
package org.distorted.examples.cubes;
3

    
4
import java.io.IOException;
5
import java.io.InputStream;
6

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

    
10
import org.distorted.examples.R;
11

    
12
import org.distorted.library.InterpolatorQuat;
13
import org.distorted.library.DistortedCubes;
14
import org.distorted.library.Float4D;
15
import org.distorted.library.Float3D;
16
import org.distorted.library.Distorted;
17

    
18
import android.graphics.Bitmap;
19
import android.graphics.BitmapFactory;
20
import android.opengl.GLES20;
21
import android.opengl.GLSurfaceView;
22

    
23
///////////////////////////////////////////////////////////////////////////////////////////////////
24

    
25
class CubesRenderer implements GLSurfaceView.Renderer 
26
{
27
	private static final int SIZE = 100;
28
	
29
	private int mCols, mRows;
30
	
31
    private GLSurfaceView mView;
32
    private DistortedCubes mCubes;
33
    private InterpolatorQuat mQuatInt1, mQuatInt2;
34
    
35
    Float4D mQuat1, mQuat2;
36
    int mScreenMin;
37
    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
    public CubesRenderer(GLSurfaceView v) 
41
      {
42
      mView = v;
43
      
44
      String shape = CubesActivity.getShape();
45
      
46
      mCols = CubesActivity.getCols();
47
      mRows = shape.length() / mCols;
48
      
49
      mCubes = new DistortedCubes( mCols, shape, SIZE);
50
      
51
      mQuat1 = new Float4D(0,0,0,1);  // unity
52
      mQuat2 = new Float4D(0,0,0,1);  // quaternions
53
      
54
      mQuatInt1 = new InterpolatorQuat();
55
      mQuatInt2 = new InterpolatorQuat();
56
      mQuatInt1.setDuration(0);
57
      mQuatInt2.setDuration(0);
58
      mQuatInt1.setCount(0.5f);
59
      mQuatInt2.setCount(0.5f);
60
      
61
      mQuatInt1.add(mQuat1);
62
      mQuatInt2.add(mQuat2);
63
      }
64

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

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
    
77
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
78
      {
79
      mScreenMin = width<height ? width:height;
80
    	
81
      mCubes.abortAllEffects(Distorted.TYPE_MATR);  
82

    
83
      if( mRows/mCols > height/width )
84
        {
85
        int w = (height*mRows)/mCols;
86
        mCubes.move((width-w)/2 ,0, 0);
87
        mCubes.scale((float)height/(mRows*SIZE));
88
        }  
89
      else
90
        {   
91
        int h = (width*mRows)/mCols;
92
        mCubes.move(0 ,(height-h)/2, 0);
93
        mCubes.scale((float)width/(mCols*SIZE));
94
        }
95
    
96
      Float3D center = new Float3D(mCols*SIZE/2,mRows*SIZE/2, 0);
97
      
98
      mCubes.quaternion(center, mQuatInt1);
99
      mCubes.quaternion(center, mQuatInt2);
100
       
101
      Distorted.onSurfaceChanged(width, height); 
102
      }
103

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