Project

General

Profile

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

examples / src / main / java / org / distorted / examples / olimpic / OlimpicRenderer.java @ 6637d0f2

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.DistortedEffects;
31
import org.distorted.library.DistortedScreen;
32
import org.distorted.library.DistortedNode;
33
import org.distorted.library.EffectNames;
34
import org.distorted.library.EffectTypes;
35
import org.distorted.library.MeshFlat;
36
import org.distorted.library.type.Dynamic1D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static1D;
39
import org.distorted.library.Distorted;
40
import org.distorted.library.DistortedTexture;
41

    
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44
import android.opengl.GLES30;
45
import android.opengl.GLSurfaceView;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
class OlimpicRenderer implements GLSurfaceView.Renderer 
50
{
51
   private static final int LEAF_SIZE = 100;
52
   private static final int NUM_CIRCLES = 5;
53
   private static final int NUM_LEAVES  = 8;
54
   
55
   private GLSurfaceView mView;
56
   private DistortedNode mRoot;
57
   private DistortedTexture mLeaf;
58
   private DistortedScreen mScreen;
59
   private MeshFlat mMesh;
60
   private int mScreenW, mScreenH;
61
   private int mPrevRendered, mCurrRendered;
62

    
63
   private DistortedNode[] mCircleNode = new DistortedNode[NUM_CIRCLES];
64
   private DistortedEffects[] mEffects = new DistortedEffects[NUM_LEAVES];
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
   void pressed(int color, int number, boolean checked)
69
     {
70
     if( color>=0 && color<NUM_CIRCLES )
71
       {
72
       if( number==0 )
73
         {
74
         if( checked ) mRoot.attach(mCircleNode[color]);
75
         else          mRoot.detach(mCircleNode[color]);
76
         }
77
       else if( number>0 && number<=NUM_LEAVES )
78
         {
79
         if( checked ) mCircleNode[color].attach( mLeaf, mEffects[number-1], mMesh);
80
         else          mCircleNode[color].detach(mEffects[number-1]);
81
         }
82
       }
83
     }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
   OlimpicRenderer(GLSurfaceView v)
88
      {     
89
      mView = v;
90

    
91
      mPrevRendered = 0;
92
      mCurrRendered = 0;
93

    
94
      mLeaf = new DistortedTexture(LEAF_SIZE,LEAF_SIZE);
95
      DistortedTexture surface = new DistortedTexture(3*LEAF_SIZE,3*LEAF_SIZE);
96
      mMesh = new MeshFlat(1,1);
97
      DistortedEffects effects = new DistortedEffects();
98

    
99
      mScreenW = 9*LEAF_SIZE;
100
      mScreenH = 9*LEAF_SIZE;
101
      mRoot = new DistortedNode(new DistortedTexture(mScreenW,mScreenH), effects, mMesh);
102
     
103
      Dynamic1D rot = new Dynamic1D(5000,0.0f);
104
      rot.setMode(Dynamic1D.MODE_JUMP);
105
      rot.add(new Static1D(  0));
106
      rot.add(new Static1D(360));
107

    
108
      int[] colors    = new int[] {0,0,1,  0,0,0,  1,0,0,  1,1,0,  0,1,0}; // blue, black, red, yellow, green  
109
      int[] positions = new int[] {0,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};
110
      
111
      Static3D center = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
112
      Static3D axis   = new Static3D(0,0,1);
113
      Static3D moveVector = new Static3D(0,LEAF_SIZE,0);
114

    
115
      for(int j=0; j<NUM_LEAVES; j++)
116
        {
117
        mEffects[j] = new DistortedEffects();
118
        mEffects[j].rotate( new Static1D(j*(360/NUM_LEAVES)), axis, center );
119
        mEffects[j].move(moveVector);
120
        }
121

    
122
      for(int i=0; i<NUM_CIRCLES; i++)
123
        {
124
        effects = new DistortedEffects();
125
        effects.move( new Static3D(positions[2*i], positions[2*i+1], 0) );
126
        effects.rotate( rot, axis, center );
127
        effects.chroma( new Static1D(0.5f), new Static3D(colors[3*i],colors[3*i+1], colors[3*i+2]) );
128

    
129
        mCircleNode[i] = new DistortedNode( surface, effects, mMesh);
130
        mRoot.attach(mCircleNode[i]);
131

    
132
        for(int j=0; j<NUM_LEAVES; j++) mCircleNode[i].attach(mLeaf, mEffects[j], mMesh);
133
        }
134

    
135
      mScreen = new DistortedScreen();
136
      mScreen.attach(mRoot);
137
      }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
   
141
    public void onDrawFrame(GL10 glUnused) 
142
      {
143
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
144
      mCurrRendered = mScreen.render(System.currentTimeMillis());
145

    
146
      if( mCurrRendered!=mPrevRendered )
147
        {
148
        mPrevRendered = mCurrRendered;
149

    
150
        final OlimpicActivity act = (OlimpicActivity)mView.getContext();
151

    
152
        act.runOnUiThread(new Runnable()
153
          {
154
          public void run()
155
            {
156
            act.setText("rendered: "+mCurrRendered+" objects");
157
            }
158
          });
159
        }
160
      }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
    
164
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
165
      {
166
      DistortedEffects effects = mRoot.getEffects();
167

    
168
      effects.abortEffects(EffectTypes.MATRIX);
169
      
170
      if( (float)mScreenH/mScreenW > (float)height/width )
171
        {
172
        int w = (height*mScreenW)/mScreenH;
173
        float factor = (float)height/mScreenH;
174

    
175
        effects.move( new Static3D((width-w)/2 ,0, 0) );
176
        effects.scale( factor );
177
        }
178
      else
179
        {
180
        int h = (width*mScreenH)/mScreenW;
181
        float factor = (float)width/mScreenW;
182

    
183
        effects.move( new Static3D(0,(height-h)/2,0) );
184
        effects.scale( factor );
185
        }
186
      
187
      mScreen.resize(width, height);
188
      }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191
    
192
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
193
      {
194
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
195

    
196
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
197
      Bitmap leaf;
198
      
199
      try 
200
        {
201
        leaf = BitmapFactory.decodeStream(is);
202
        } 
203
      finally 
204
        {
205
        try 
206
          {
207
          is.close();
208
          } 
209
        catch(IOException e) { }
210
        }  
211
      
212
      mLeaf.setTexture(leaf);
213

    
214
      DistortedEffects.enableEffect(EffectNames.CHROMA);
215

    
216
      try
217
        {
218
        Distorted.onCreate(mView.getContext());
219
        }
220
      catch(Exception ex)
221
        {
222
        android.util.Log.e("Olympic", ex.getMessage() );
223
        }
224
      }
225
 
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
    
228
}
(2-2/3)