Project

General

Profile

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

examples / src / main / java / org / distorted / examples / olimpic / OlimpicRenderer.java @ 59759251

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
import org.distorted.library.type.Static4D;
38

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

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

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

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

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

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

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

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

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

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