Project

General

Profile

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

examples / src / main / java / org / distorted / examples / olimpic / OlimpicRenderer.java @ 7589635e

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.examples.olimpic;
21

    
22
import java.io.IOException;
23
import java.io.InputStream;
24

    
25
import javax.microedition.khronos.egl.EGLConfig;
26
import javax.microedition.khronos.opengles.GL10;
27

    
28
import org.distorted.examples.R;
29

    
30
import org.distorted.library.EffectTypes;
31
import org.distorted.library.type.Dynamic1D;
32
import org.distorted.library.DistortedNode;
33
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static1D;
35
import org.distorted.library.Distorted;
36
import org.distorted.library.DistortedBitmap;
37

    
38
import android.graphics.Bitmap;
39
import android.graphics.BitmapFactory;
40
import android.opengl.GLES20;
41
import android.opengl.GLSurfaceView;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
class OlimpicRenderer implements GLSurfaceView.Renderer 
46
{
47
   private static final int LEAF_SIZE = 100;
48
   private static final int NUM_CIRCLES = 5;
49
   private static final int NUM_LEAVES = 8;  
50
   
51
   private GLSurfaceView mView;
52
   private DistortedNode[] mCircleNode;
53
   private DistortedNode mScreen;
54
   private DistortedBitmap mLeaf;
55
   private Dynamic1D mRot;
56
    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
   public OlimpicRenderer(GLSurfaceView v) 
60
      {     
61
      mView = v;
62
    
63
      mLeaf = new DistortedBitmap(LEAF_SIZE,LEAF_SIZE,1);
64
      
65
      mScreen = new DistortedNode(new DistortedBitmap(9*LEAF_SIZE,9*LEAF_SIZE,1));
66
     
67
      mRot = new Dynamic1D();
68
      mRot.setMode(Dynamic1D.MODE_JUMP);
69
      mRot.setCount(0.0f);
70
      mRot.setDuration(5000);
71
      mRot.add(new Static1D(  0));
72
      mRot.add(new Static1D(360));
73
      
74
      mCircleNode = new DistortedNode[NUM_CIRCLES];
75
    
76
      int[] colors    = new int[] {0,0,1,  0,0,0,  1,0,0,  1,1,0,  0,1,0}; // blue, black, red, yellow, green  
77
      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};
78
      
79
      DistortedBitmap tmp;
80
      Static3D center = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
81
      Static3D axis   = new Static3D(0,0,1);
82
      Static3D moveVector = new Static3D(0,LEAF_SIZE,0);
83

    
84
      for(int i=0; i<NUM_CIRCLES; i++)
85
        {
86
        if( i==0 )
87
          {
88
          mCircleNode[i] = new DistortedNode(new DistortedBitmap(3*LEAF_SIZE,3*LEAF_SIZE,1));
89
        
90
          for(int j=0; j<NUM_LEAVES; j++)
91
            {
92
            tmp = new DistortedBitmap(mLeaf, Distorted.CLONE_BITMAP);
93
            mCircleNode[i].attach(tmp);
94
            tmp.rotate(center, new Static1D(j*(360/NUM_LEAVES)), axis);
95
            tmp.move(moveVector);
96
            }
97
          }
98
        else
99
          {
100
          mCircleNode[i] = new DistortedNode(mCircleNode[0], Distorted.CLONE_BITMAP|Distorted.CLONE_CHILDREN); 
101
          }
102
      
103
        mScreen.attach(mCircleNode[i]);
104
        tmp = (DistortedBitmap)mCircleNode[i].getObject();
105
        tmp.move( new Static3D(positions[2*i], positions[2*i+1], 0) );
106
        tmp.rotate( center, mRot, axis );
107
        tmp.chroma(0.8f, new Static3D(colors[3*i],colors[3*i+1], colors[3*i+2]));
108
        }
109
      }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
   
113
    public void onDrawFrame(GL10 glUnused) 
114
      {
115
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
116
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
117
      
118
      mScreen.draw(System.currentTimeMillis());
119
      }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
    
123
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
124
      {
125
      DistortedBitmap bmp = (DistortedBitmap)mScreen.getObject();
126
      int bmpWidth  = bmp.getWidth();
127
      int bmpHeight = bmp.getHeight();
128
     
129
      bmp.abortEffects(EffectTypes.MATRIX);
130
      
131
      if( bmpHeight/bmpWidth > height/width )
132
        {
133
        int w = (height*bmpWidth)/bmpHeight;
134
        float factor = (float)height/bmpHeight;
135

    
136
        bmp.move( new Static3D((width-w)/2 ,0, 0) );
137
        bmp.scale( new Static3D(factor,factor,factor) );
138
        }
139
      else
140
        {
141
        int h = (width*bmpHeight)/bmpWidth;
142
        float factor = (float)width/bmpWidth;
143

    
144
        bmp.move( new Static3D(0,(height-h)/2,0) );
145
        bmp.scale( new Static3D(factor,factor,factor) );
146
        }
147
      
148
      Distorted.onSurfaceChanged(width, height); 
149
      }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
    
153
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
154
      {
155
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
156
      Bitmap leaf;
157
      
158
      try 
159
        {
160
        leaf = BitmapFactory.decodeStream(is);
161
        } 
162
      finally 
163
        {
164
        try 
165
          {
166
          is.close();
167
          } 
168
        catch(IOException e) { }
169
        }  
170
      
171
      mLeaf.setBitmap(leaf);
172
      
173
      try
174
        {
175
        Distorted.onSurfaceCreated(mView.getContext());
176
        }
177
      catch(Exception ex)
178
        {
179
        android.util.Log.e("Olympic", ex.getMessage() );
180
        }
181
      }
182
 
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
    
185
}
(2-2/3)