Project

General

Profile

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

examples / src / main / java / org / distorted / examples / olimpic / OlimpicRenderer.java @ 77e66c58

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.effect.FragmentEffectChroma;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectRotate;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.main.DistortedEffects;
35
import org.distorted.library.main.DistortedLibrary;
36
import org.distorted.library.main.DistortedScreen;
37
import org.distorted.library.main.DistortedNode;
38
import org.distorted.library.mesh.MeshQuad;
39
import org.distorted.library.type.Dynamic1D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static1D;
42
import org.distorted.library.main.DistortedTexture;
43

    
44
import android.app.ActivityManager;
45
import android.content.Context;
46
import android.content.pm.ConfigurationInfo;
47
import android.content.res.Resources;
48
import android.graphics.Bitmap;
49
import android.graphics.BitmapFactory;
50
import android.opengl.GLSurfaceView;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
class OlimpicRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
55
{
56
   private static final int LEAF_SIZE = 100;
57
   private static final int NUM_CIRCLES = 5;
58
   static final int NUM_LEAVES  = 8;
59
   
60
   private final GLSurfaceView mView;
61
   private final DistortedNode mRoot;
62
   private final DistortedTexture mLeaf;
63
   private final DistortedScreen mScreen;
64
   private final MeshQuad mLeafMesh;
65
   private final Static3D mScale;
66
   private final DistortedNode[] mCircleNode = new DistortedNode[NUM_CIRCLES];
67
   private final DistortedEffects[] mEffects = new DistortedEffects[NUM_LEAVES];
68
   private final Resources mResources;
69

    
70
   private int mPrevRendered, mCurrRendered;
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
   void pressed(int color, int number, boolean checked)
75
     {
76
     if( color>=0 && color<NUM_CIRCLES )
77
       {
78
       if( number==0 )
79
         {
80
         if( checked ) mRoot.attach(mCircleNode[color]);
81
         else          mRoot.detach(mCircleNode[color]);
82
         }
83
       else if( number>0 && number<=NUM_LEAVES )
84
         {
85
         if( checked ) mCircleNode[color].attach( mLeaf, mEffects[number-1], mLeafMesh);
86
         else          mCircleNode[color].detach(mEffects[number-1]);
87
         }
88
       }
89
     }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
   OlimpicRenderer(GLSurfaceView v)
94
      {     
95
      mView = v;
96
      mResources = v.getResources();
97

    
98
      mPrevRendered = -1;
99
      mCurrRendered = -1;
100

    
101
      mLeaf = new DistortedTexture();
102
      DistortedTexture surface = new DistortedTexture();
103

    
104
      mLeafMesh = new MeshQuad();
105
      MeshQuad quad = new MeshQuad();
106

    
107
      mScale= new Static3D(1,1,1);
108

    
109
      DistortedEffects rootEffects = new DistortedEffects();
110
      rootEffects.apply(new MatrixEffectScale(mScale));
111

    
112
      mRoot = new DistortedNode(new DistortedTexture(), rootEffects, quad);
113
      mRoot.resizeFBO(9*LEAF_SIZE,9*LEAF_SIZE);
114

    
115
      Dynamic1D rot = new Dynamic1D(5000,0.0f);
116
      rot.setMode(Dynamic1D.MODE_JUMP);
117
      rot.add(new Static1D(  0));
118
      rot.add(new Static1D(360));
119

    
120
      int[] colors    = new int[] {0,0,1,  0,0,0,  1,0,0,  1,1,0,  0,1,0}; // blue, black, red, yellow, green  
121
      int[] positions = new int[] {-3*LEAF_SIZE  ,   LEAF_SIZE  ,
122
                                              0  ,   LEAF_SIZE  ,
123
                                    3*LEAF_SIZE  ,   LEAF_SIZE  ,
124
                                   -3*LEAF_SIZE/2,-3*LEAF_SIZE/2,
125
                                    3*LEAF_SIZE/2,-3*LEAF_SIZE/2 };
126
      
127
      Static3D center = new Static3D(0,0,0);
128
      Static3D axis   = new Static3D(0,0,1);
129
      Static3D moveVector = new Static3D(-LEAF_SIZE,0,0);
130

    
131
      for(int j=0; j<NUM_LEAVES; j++)
132
        {
133
        mEffects[j] = new DistortedEffects();
134
        mEffects[j].apply( new MatrixEffectScale(LEAF_SIZE) );
135
        mEffects[j].apply( new MatrixEffectMove(moveVector));
136
        mEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
137
        }
138

    
139
      for(int i=0; i<NUM_CIRCLES; i++)
140
        {
141
        DistortedEffects effects = new DistortedEffects();
142
        effects.apply( new MatrixEffectScale(3*LEAF_SIZE) );
143
        effects.apply( new MatrixEffectRotate(rot, axis, center) );
144
        effects.apply( new MatrixEffectMove(new Static3D(positions[2*i], positions[2*i+1], 0)) );
145
        effects.apply( new FragmentEffectChroma(new Static1D(0.5f), new Static3D(colors[3*i],colors[3*i+1], colors[3*i+2])) );
146

    
147
        mCircleNode[i] = new DistortedNode( surface, effects, quad);
148
        mCircleNode[i].resizeFBO(3*LEAF_SIZE, 3*LEAF_SIZE);
149
        mRoot.attach(mCircleNode[i]);
150

    
151
        for(int j=0; j<NUM_LEAVES; j++) mCircleNode[i].attach(mLeaf, mEffects[j], mLeafMesh);
152
        }
153

    
154
      mScreen = new DistortedScreen();
155
      mScreen.attach(mRoot);
156
      }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
   
160
   public void onDrawFrame(GL10 glUnused)
161
     {
162
     mCurrRendered = mScreen.render(System.currentTimeMillis());
163

    
164
     if( mCurrRendered!=mPrevRendered )
165
       {
166
       mPrevRendered = mCurrRendered;
167

    
168
       final OlimpicActivity act = (OlimpicActivity)mView.getContext();
169

    
170
       act.runOnUiThread(new Runnable()
171
          {
172
          public void run()
173
            {
174
           act.setText("rendered: "+mCurrRendered+" objects");
175
           }
176
          });
177
       }
178
     }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
    
182
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
183
     {
184
     float horiRatio = (float)width ;
185
     float vertRatio = (float)height;
186
     float factor    = Math.min(horiRatio,vertRatio);
187

    
188
     mScale.set(factor,factor,factor);
189
     mScreen.resize(width, height);
190
     }
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 ignored) { }
210
       }
211
      
212
     mLeaf.setTexture(leaf);
213

    
214
     FragmentEffectChroma.enable();
215

    
216
     DistortedLibrary.onSurfaceCreated(this);
217
     }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
   public void distortedException(Exception ex)
222
     {
223
     android.util.Log.e("Olympic", ex.getMessage() );
224
     }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
   public InputStream localFile(int fileID)
229
     {
230
     return mResources.openRawResource(fileID);
231
     }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
   public void logMessage(String message)
236
     {
237
     android.util.Log.e("Olympic", message );
238
     }
239
}
(2-2/3)