Project

General

Profile

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

examples / src / main / java / org / distorted / examples / olimpic / OlimpicRenderer.java @ 5068fa06

1

    
2
package org.distorted.examples.olimpic;
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.Interpolator1D;
13
import org.distorted.library.DistortedNode;
14
import org.distorted.library.Float3D;
15
import org.distorted.library.Float1D;
16
import org.distorted.library.Distorted;
17
import org.distorted.library.DistortedBitmap;
18

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

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

    
26
class OlimpicRenderer implements GLSurfaceView.Renderer 
27
{
28
   private static final int LEAF_SIZE = 100;
29
   private static final int NUM_CIRCLES = 5;
30
   private static final int NUM_LEAVES = 8;  
31
   
32
   private GLSurfaceView mView;
33
   private DistortedNode[] mCircleNode;
34
   private DistortedNode mScreen;
35
   private DistortedBitmap mLeaf;
36
   private Interpolator1D mRot;
37
    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
   public OlimpicRenderer(GLSurfaceView v) 
41
      {     
42
      mView = v;
43
    
44
      mLeaf = new DistortedBitmap(LEAF_SIZE,LEAF_SIZE,1);
45
      
46
      mScreen = new DistortedNode(new DistortedBitmap(9*LEAF_SIZE,9*LEAF_SIZE,1));
47
     
48
      mRot = new Interpolator1D();
49
      mRot.setMode(Interpolator1D.MODE_JUMP);
50
      mRot.setCount(0.0f);
51
      mRot.setDuration(5000);
52
      mRot.add(new Float1D(  0));
53
      mRot.add(new Float1D(360));
54
      
55
      mCircleNode = new DistortedNode[NUM_CIRCLES];
56
    
57
      int[] colors    = new int[] {0,0,1,  0,0,0,  1,0,0,  1,1,0,  0,1,0}; // blue, black, red, yellow, green  
58
      int[] positions = new int[] {0*LEAF_SIZE,2*LEAF_SIZE,  3*LEAF_SIZE,2*LEAF_SIZE,  6*LEAF_SIZE,2*LEAF_SIZE,  3*LEAF_SIZE/2,9*LEAF_SIZE/2,  9*LEAF_SIZE/2,9*LEAF_SIZE/2};
59
      
60
      DistortedBitmap tmp;
61
      Float3D center = new Float3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
62
      
63
      for(int i=0; i<NUM_CIRCLES; i++)
64
        {
65
        if( i==0 )
66
          {
67
          mCircleNode[i] = new DistortedNode(new DistortedBitmap(3*LEAF_SIZE,3*LEAF_SIZE,1));
68
        
69
          for(int j=0; j<NUM_LEAVES; j++)
70
            {
71
            tmp = new DistortedBitmap(mLeaf, Distorted.CLONE_BITMAP);
72
            mCircleNode[i].attach(tmp);
73
            tmp.rotate(center, j*(360/NUM_LEAVES)); 
74
            tmp.move(0,LEAF_SIZE,0);
75
            }
76
          }
77
        else
78
          {
79
          mCircleNode[i] = new DistortedNode(mCircleNode[0], Distorted.CLONE_BITMAP|Distorted.CLONE_CHILDREN); 
80
          }
81
      
82
        mScreen.attach(mCircleNode[i]);
83
        tmp = mCircleNode[i].getBitmap();
84
        tmp.move(positions[2*i], positions[2*i+1], 0);
85
        tmp.rotate( center, mRot, 0.0f, 0.0f, 1.0f);
86
        tmp.chroma(0.8f, new Float3D(colors[3*i],colors[3*i+1], colors[3*i+2]));
87
        }
88
      }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
   
92
    public void onDrawFrame(GL10 glUnused) 
93
      {
94
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
95
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
96
      
97
      mScreen.draw(System.currentTimeMillis());
98
      }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
    
102
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
103
      {
104
      DistortedBitmap bmp = mScreen.getBitmap();
105
      int bmpWidth  = bmp.getWidth();
106
      int bmpHeight = bmp.getHeight();
107
     
108
      bmp.abortAllEffects(Distorted.TYPE_MATR); 
109
      
110
      if( bmpHeight/bmpWidth > height/width )
111
        {
112
        int w = (height*bmpWidth)/bmpHeight;
113
        bmp.move((width-w)/2 ,0, 0);
114
        bmp.scale((float)height/bmpHeight);
115
        }
116
      else
117
        {
118
        int h = (width*bmpHeight)/bmpWidth;
119
        bmp.move(0 ,(height-h)/2, 0);
120
        bmp.scale((float)width/bmpWidth);
121
        }
122
      
123
      Distorted.onSurfaceChanged(width, height); 
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
129
      {
130
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
131
      Bitmap leaf;
132
      
133
      try 
134
        {
135
        leaf = BitmapFactory.decodeStream(is);
136
        } 
137
      finally 
138
        {
139
        try 
140
          {
141
          is.close();
142
          } 
143
        catch(IOException e) { }
144
        }  
145
      
146
      mLeaf.setBitmap(leaf);
147
      
148
      try
149
        {
150
        Distorted.onSurfaceCreated(mView);
151
        }
152
      catch(Exception ex)
153
        {
154
        android.util.Log.e("DifferentEffectsRenderer", ex.getMessage() );
155
        }
156
      }
157
 
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
    
160
}
(2-2/3)