Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.olimpic;
21 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
29 427ab7bf Leszek Koltunski
30 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
31 d218d64e leszek
import org.distorted.library.DistortedScreen;
32 8744aa41 Leszek Koltunski
import org.distorted.library.DistortedNode;
33 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
34 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
35 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
36 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static1D;
39 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
40 40eef4b9 Leszek Koltunski
import org.distorted.library.DistortedTexture;
41 427ab7bf Leszek Koltunski
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44
import android.opengl.GLSurfaceView;
45
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48
class OlimpicRenderer implements GLSurfaceView.Renderer 
49
{
50
   private static final int LEAF_SIZE = 100;
51 6eb5eec2 Leszek Koltunski
   private static final int NUM_CIRCLES = 5;
52 411fca0c leszek
   static final int NUM_LEAVES  = 8;
53 427ab7bf Leszek Koltunski
   
54
   private GLSurfaceView mView;
55 8744aa41 Leszek Koltunski
   private DistortedNode mRoot;
56 40eef4b9 Leszek Koltunski
   private DistortedTexture mLeaf;
57 d218d64e leszek
   private DistortedScreen mScreen;
58 44771819 Leszek Koltunski
   private MeshFlat mMesh;
59 40eef4b9 Leszek Koltunski
   private int mScreenW, mScreenH;
60 2878c554 leszek
   private int mPrevRendered, mCurrRendered;
61 880bdf97 Leszek Koltunski
62 44771819 Leszek Koltunski
   private DistortedNode[] mCircleNode = new DistortedNode[NUM_CIRCLES];
63
   private DistortedEffects[] mEffects = new DistortedEffects[NUM_LEAVES];
64
65 6ab5da9a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67 44771819 Leszek Koltunski
   void pressed(int color, int number, boolean checked)
68 6ab5da9a Leszek Koltunski
     {
69 44771819 Leszek Koltunski
     if( color>=0 && color<NUM_CIRCLES )
70
       {
71
       if( number==0 )
72
         {
73 c90f5dbb leszek
         android.util.Log.e("renderer", "pressed: root "+checked);
74
75 44771819 Leszek Koltunski
         if( checked ) mRoot.attach(mCircleNode[color]);
76
         else          mRoot.detach(mCircleNode[color]);
77
         }
78
       else if( number>0 && number<=NUM_LEAVES )
79
         {
80 c90f5dbb leszek
         android.util.Log.e("renderer", "pressed: circle: "+color+" leaf: "+number+" :"+checked);
81
82 44771819 Leszek Koltunski
         if( checked ) mCircleNode[color].attach( mLeaf, mEffects[number-1], mMesh);
83
         else          mCircleNode[color].detach(mEffects[number-1]);
84
         }
85
       }
86 6ab5da9a Leszek Koltunski
     }
87
88 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
89
90 508f22b5 Leszek Koltunski
   OlimpicRenderer(GLSurfaceView v)
91 427ab7bf Leszek Koltunski
      {     
92
      mView = v;
93 e8b6aa95 Leszek Koltunski
94 f6654822 Leszek Koltunski
      mPrevRendered = -1;
95
      mCurrRendered = -1;
96 2878c554 leszek
97 7451c98a Leszek Koltunski
      mLeaf = new DistortedTexture(LEAF_SIZE,LEAF_SIZE);
98 44771819 Leszek Koltunski
      DistortedTexture surface = new DistortedTexture(3*LEAF_SIZE,3*LEAF_SIZE);
99
      mMesh = new MeshFlat(1,1);
100 d04a4886 Leszek Koltunski
      DistortedEffects effects = new DistortedEffects();
101 40eef4b9 Leszek Koltunski
102
      mScreenW = 9*LEAF_SIZE;
103
      mScreenH = 9*LEAF_SIZE;
104 44771819 Leszek Koltunski
      mRoot = new DistortedNode(new DistortedTexture(mScreenW,mScreenH), effects, mMesh);
105 427ab7bf Leszek Koltunski
     
106 880bdf97 Leszek Koltunski
      Dynamic1D rot = new Dynamic1D(5000,0.0f);
107
      rot.setMode(Dynamic1D.MODE_JUMP);
108
      rot.add(new Static1D(  0));
109
      rot.add(new Static1D(360));
110 44771819 Leszek Koltunski
111 427ab7bf Leszek Koltunski
      int[] colors    = new int[] {0,0,1,  0,0,0,  1,0,0,  1,1,0,  0,1,0}; // blue, black, red, yellow, green  
112 40eef4b9 Leszek Koltunski
      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};
113 427ab7bf Leszek Koltunski
      
114 7589635e Leszek Koltunski
      Static3D center = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
115
      Static3D axis   = new Static3D(0,0,1);
116
      Static3D moveVector = new Static3D(0,LEAF_SIZE,0);
117
118 44771819 Leszek Koltunski
      for(int j=0; j<NUM_LEAVES; j++)
119 427ab7bf Leszek Koltunski
        {
120 44771819 Leszek Koltunski
        mEffects[j] = new DistortedEffects();
121
        mEffects[j].rotate( new Static1D(j*(360/NUM_LEAVES)), axis, center );
122
        mEffects[j].move(moveVector);
123
        }
124 3c8b1903 Leszek Koltunski
125 44771819 Leszek Koltunski
      for(int i=0; i<NUM_CIRCLES; i++)
126
        {
127
        effects = new DistortedEffects();
128 392e16fd Leszek Koltunski
        effects.move( new Static3D(positions[2*i], positions[2*i+1], 0) );
129
        effects.rotate( rot, axis, center );
130
        effects.chroma( new Static1D(0.5f), new Static3D(colors[3*i],colors[3*i+1], colors[3*i+2]) );
131 44771819 Leszek Koltunski
132
        mCircleNode[i] = new DistortedNode( surface, effects, mMesh);
133
        mRoot.attach(mCircleNode[i]);
134
135
        for(int j=0; j<NUM_LEAVES; j++) mCircleNode[i].attach(mLeaf, mEffects[j], mMesh);
136 427ab7bf Leszek Koltunski
        }
137 392e16fd Leszek Koltunski
138 d218d64e leszek
      mScreen = new DistortedScreen();
139 fe59d375 Leszek Koltunski
      mScreen.attach(mRoot);
140 427ab7bf Leszek Koltunski
      }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
   
144
    public void onDrawFrame(GL10 glUnused) 
145
      {
146 2878c554 leszek
      mCurrRendered = mScreen.render(System.currentTimeMillis());
147
148
      if( mCurrRendered!=mPrevRendered )
149
        {
150
        mPrevRendered = mCurrRendered;
151 722c6ed3 Leszek Koltunski
152
        final OlimpicActivity act = (OlimpicActivity)mView.getContext();
153
154
        act.runOnUiThread(new Runnable()
155
          {
156
          public void run()
157
            {
158
            act.setText("rendered: "+mCurrRendered+" objects");
159
            }
160
          });
161 2878c554 leszek
        }
162 427ab7bf Leszek Koltunski
      }
163
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
    
166
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
167
      {
168 d04a4886 Leszek Koltunski
      DistortedEffects effects = mRoot.getEffects();
169 40eef4b9 Leszek Koltunski
170 392e16fd Leszek Koltunski
      effects.abortEffects(EffectTypes.MATRIX);
171 427ab7bf Leszek Koltunski
      
172 40eef4b9 Leszek Koltunski
      if( (float)mScreenH/mScreenW > (float)height/width )
173 427ab7bf Leszek Koltunski
        {
174 40eef4b9 Leszek Koltunski
        int w = (height*mScreenW)/mScreenH;
175
        float factor = (float)height/mScreenH;
176 7589635e Leszek Koltunski
177 392e16fd Leszek Koltunski
        effects.move( new Static3D((width-w)/2 ,0, 0) );
178
        effects.scale( factor );
179 427ab7bf Leszek Koltunski
        }
180
      else
181
        {
182 40eef4b9 Leszek Koltunski
        int h = (width*mScreenH)/mScreenW;
183
        float factor = (float)width/mScreenW;
184 7589635e Leszek Koltunski
185 392e16fd Leszek Koltunski
        effects.move( new Static3D(0,(height-h)/2,0) );
186
        effects.scale( factor );
187 427ab7bf Leszek Koltunski
        }
188
      
189 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
190 427ab7bf Leszek Koltunski
      }
191
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
    
194
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
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 e8b6aa95 Leszek Koltunski
      mLeaf.setTexture(leaf);
213 6637d0f2 Leszek Koltunski
214
      DistortedEffects.enableEffect(EffectNames.CHROMA);
215
216 427ab7bf Leszek Koltunski
      try
217
        {
218 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
219 427ab7bf Leszek Koltunski
        }
220
      catch(Exception ex)
221
        {
222 ed1c0b33 Leszek Koltunski
        android.util.Log.e("Olympic", ex.getMessage() );
223 427ab7bf Leszek Koltunski
        }
224
      }
225
 
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227
    
228
}