Project

General

Profile

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

examples / src / main / java / org / distorted / examples / olimpic / OlimpicRenderer.java @ 061449ed

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 6173632c Leszek Koltunski
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 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
35 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
36 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
37
import org.distorted.library.main.DistortedNode;
38 698ad0a8 Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
39 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static1D;
42 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
43 427ab7bf Leszek Koltunski
44
import android.graphics.Bitmap;
45
import android.graphics.BitmapFactory;
46
import android.opengl.GLSurfaceView;
47
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50 061449ed Leszek Koltunski
class OlimpicRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
51 427ab7bf Leszek Koltunski
{
52
   private static final int LEAF_SIZE = 100;
53 6eb5eec2 Leszek Koltunski
   private static final int NUM_CIRCLES = 5;
54 411fca0c leszek
   static final int NUM_LEAVES  = 8;
55 427ab7bf Leszek Koltunski
   
56
   private GLSurfaceView mView;
57 8744aa41 Leszek Koltunski
   private DistortedNode mRoot;
58 40eef4b9 Leszek Koltunski
   private DistortedTexture mLeaf;
59 d218d64e leszek
   private DistortedScreen mScreen;
60 698ad0a8 Leszek Koltunski
   private MeshQuad mLeafMesh;
61 2878c554 leszek
   private int mPrevRendered, mCurrRendered;
62 16b22aab Leszek Koltunski
   private Static3D mScale;
63 880bdf97 Leszek Koltunski
64 44771819 Leszek Koltunski
   private DistortedNode[] mCircleNode = new DistortedNode[NUM_CIRCLES];
65
   private DistortedEffects[] mEffects = new DistortedEffects[NUM_LEAVES];
66
67 6ab5da9a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69 44771819 Leszek Koltunski
   void pressed(int color, int number, boolean checked)
70 6ab5da9a Leszek Koltunski
     {
71 44771819 Leszek Koltunski
     if( color>=0 && color<NUM_CIRCLES )
72
       {
73
       if( number==0 )
74
         {
75
         if( checked ) mRoot.attach(mCircleNode[color]);
76
         else          mRoot.detach(mCircleNode[color]);
77
         }
78
       else if( number>0 && number<=NUM_LEAVES )
79
         {
80 698ad0a8 Leszek Koltunski
         if( checked ) mCircleNode[color].attach( mLeaf, mEffects[number-1], mLeafMesh);
81 44771819 Leszek Koltunski
         else          mCircleNode[color].detach(mEffects[number-1]);
82
         }
83
       }
84 6ab5da9a Leszek Koltunski
     }
85
86 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
87
88 508f22b5 Leszek Koltunski
   OlimpicRenderer(GLSurfaceView v)
89 427ab7bf Leszek Koltunski
      {     
90
      mView = v;
91 e8b6aa95 Leszek Koltunski
92 f6654822 Leszek Koltunski
      mPrevRendered = -1;
93
      mCurrRendered = -1;
94 2878c554 leszek
95 687263cc Leszek Koltunski
      mLeaf = new DistortedTexture();
96
      DistortedTexture surface = new DistortedTexture();
97 698ad0a8 Leszek Koltunski
98
      mLeafMesh = new MeshQuad();
99 5a683295 Leszek Koltunski
      MeshQuad quad = new MeshQuad();
100 698ad0a8 Leszek Koltunski
101 0c5fa440 Leszek Koltunski
      mScale= new Static3D(1,1,1);
102
103 698ad0a8 Leszek Koltunski
      DistortedEffects rootEffects = new DistortedEffects();
104 687263cc Leszek Koltunski
      rootEffects.apply(new MatrixEffectScale(mScale));
105 40eef4b9 Leszek Koltunski
106 5a683295 Leszek Koltunski
      mRoot = new DistortedNode(new DistortedTexture(), rootEffects, quad);
107
      mRoot.resizeFBO(9*LEAF_SIZE,9*LEAF_SIZE);
108
109 880bdf97 Leszek Koltunski
      Dynamic1D rot = new Dynamic1D(5000,0.0f);
110
      rot.setMode(Dynamic1D.MODE_JUMP);
111
      rot.add(new Static1D(  0));
112
      rot.add(new Static1D(360));
113 44771819 Leszek Koltunski
114 67c3a83b leszek
      int[] colors    = new int[] {0,0,1,  0,0,0,  1,0,0,  1,1,0,  0,1,0}; // blue, black, red, yellow, green  
115 16b22aab Leszek Koltunski
      int[] positions = new int[] {-3*LEAF_SIZE  ,   LEAF_SIZE  ,
116
                                              0  ,   LEAF_SIZE  ,
117
                                    3*LEAF_SIZE  ,   LEAF_SIZE  ,
118
                                   -3*LEAF_SIZE/2,-3*LEAF_SIZE/2,
119
                                    3*LEAF_SIZE/2,-3*LEAF_SIZE/2 };
120 427ab7bf Leszek Koltunski
      
121 16b22aab Leszek Koltunski
      Static3D center = new Static3D(0,0,0);
122 7589635e Leszek Koltunski
      Static3D axis   = new Static3D(0,0,1);
123 16b22aab Leszek Koltunski
      Static3D moveVector = new Static3D(-LEAF_SIZE,0,0);
124 7589635e Leszek Koltunski
125 44771819 Leszek Koltunski
      for(int j=0; j<NUM_LEAVES; j++)
126 427ab7bf Leszek Koltunski
        {
127 698ad0a8 Leszek Koltunski
        mEffects[j] = new DistortedEffects();
128 5a683295 Leszek Koltunski
        mEffects[j].apply( new MatrixEffectScale(LEAF_SIZE) );
129
        mEffects[j].apply( new MatrixEffectMove(moveVector));
130 e8e54972 Leszek Koltunski
        mEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
131 44771819 Leszek Koltunski
        }
132 3c8b1903 Leszek Koltunski
133 44771819 Leszek Koltunski
      for(int i=0; i<NUM_CIRCLES; i++)
134
        {
135 698ad0a8 Leszek Koltunski
        DistortedEffects effects = new DistortedEffects();
136 5a683295 Leszek Koltunski
        effects.apply( new MatrixEffectScale(3*LEAF_SIZE) );
137 6173632c Leszek Koltunski
        effects.apply( new MatrixEffectRotate(rot, axis, center) );
138 e8e54972 Leszek Koltunski
        effects.apply( new MatrixEffectMove(new Static3D(positions[2*i], positions[2*i+1], 0)) );
139 6173632c Leszek Koltunski
        effects.apply( new FragmentEffectChroma(new Static1D(0.5f), new Static3D(colors[3*i],colors[3*i+1], colors[3*i+2])) );
140 44771819 Leszek Koltunski
141 5a683295 Leszek Koltunski
        mCircleNode[i] = new DistortedNode( surface, effects, quad);
142
        mCircleNode[i].resizeFBO(3*LEAF_SIZE, 3*LEAF_SIZE);
143 44771819 Leszek Koltunski
        mRoot.attach(mCircleNode[i]);
144
145 698ad0a8 Leszek Koltunski
        for(int j=0; j<NUM_LEAVES; j++) mCircleNode[i].attach(mLeaf, mEffects[j], mLeafMesh);
146 427ab7bf Leszek Koltunski
        }
147 392e16fd Leszek Koltunski
148 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
149 fe59d375 Leszek Koltunski
      mScreen.attach(mRoot);
150 427ab7bf Leszek Koltunski
      }
151
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
   
154 6173632c Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
155
     {
156
     mCurrRendered = mScreen.render(System.currentTimeMillis());
157 2878c554 leszek
158 6173632c Leszek Koltunski
     if( mCurrRendered!=mPrevRendered )
159
       {
160
       mPrevRendered = mCurrRendered;
161 722c6ed3 Leszek Koltunski
162 6173632c Leszek Koltunski
       final OlimpicActivity act = (OlimpicActivity)mView.getContext();
163 722c6ed3 Leszek Koltunski
164 6173632c Leszek Koltunski
       act.runOnUiThread(new Runnable()
165 722c6ed3 Leszek Koltunski
          {
166
          public void run()
167
            {
168 6173632c Leszek Koltunski
           act.setText("rendered: "+mCurrRendered+" objects");
169
           }
170 722c6ed3 Leszek Koltunski
          });
171 6173632c Leszek Koltunski
       }
172
     }
173 427ab7bf Leszek Koltunski
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
    
176 6173632c Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
177
     {
178 5a683295 Leszek Koltunski
     float horiRatio = (float)width ;
179
     float vertRatio = (float)height;
180 16b22aab Leszek Koltunski
     float factor    = horiRatio > vertRatio ? vertRatio : horiRatio;
181
182
     mScale.set(factor,factor,factor);
183 6173632c Leszek Koltunski
     mScreen.resize(width, height);
184
     }
185 427ab7bf Leszek Koltunski
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
    
188 6173632c Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
189
     {
190
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
191
     Bitmap leaf;
192 427ab7bf Leszek Koltunski
      
193 6173632c Leszek Koltunski
     try
194
       {
195
       leaf = BitmapFactory.decodeStream(is);
196
       }
197
     finally
198
       {
199
       try
200
         {
201
         is.close();
202
         }
203
       catch(IOException e) { }
204
       }
205 427ab7bf Leszek Koltunski
      
206 6173632c Leszek Koltunski
     mLeaf.setTexture(leaf);
207 6637d0f2 Leszek Koltunski
208 885b9cca leszek
     FragmentEffectChroma.enable();
209 6637d0f2 Leszek Koltunski
210 061449ed Leszek Koltunski
     DistortedLibrary.onCreate(mView.getContext(), this);
211
     }
212
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
215
   public void distortedException(Exception ex)
216
     {
217
     android.util.Log.e("Olympic", ex.getMessage() );
218 6173632c Leszek Koltunski
     }
219 427ab7bf Leszek Koltunski
}