Project

General

Profile

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

examples / src / main / java / org / distorted / examples / olimpic / OlimpicRenderer.java @ 687263cc

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 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
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
class OlimpicRenderer implements GLSurfaceView.Renderer 
51
{
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 f3ca895f Leszek Koltunski
   private MeshRectangles mMesh;
61 67c3a83b leszek
   private int mScreenW, mScreenH;
62 2878c554 leszek
   private int mPrevRendered, mCurrRendered;
63 6173632c Leszek Koltunski
   private Static3D mMove, mScale;
64 880bdf97 Leszek Koltunski
65 44771819 Leszek Koltunski
   private DistortedNode[] mCircleNode = new DistortedNode[NUM_CIRCLES];
66
   private DistortedEffects[] mEffects = new DistortedEffects[NUM_LEAVES];
67
68 6ab5da9a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
69
70 44771819 Leszek Koltunski
   void pressed(int color, int number, boolean checked)
71 6ab5da9a Leszek Koltunski
     {
72 44771819 Leszek Koltunski
     if( color>=0 && color<NUM_CIRCLES )
73
       {
74
       if( number==0 )
75
         {
76
         if( checked ) mRoot.attach(mCircleNode[color]);
77
         else          mRoot.detach(mCircleNode[color]);
78
         }
79
       else if( number>0 && number<=NUM_LEAVES )
80
         {
81
         if( checked ) mCircleNode[color].attach( mLeaf, mEffects[number-1], mMesh);
82
         else          mCircleNode[color].detach(mEffects[number-1]);
83
         }
84
       }
85 6ab5da9a Leszek Koltunski
     }
86
87 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
88
89 508f22b5 Leszek Koltunski
   OlimpicRenderer(GLSurfaceView v)
90 427ab7bf Leszek Koltunski
      {     
91
      mView = v;
92 e8b6aa95 Leszek Koltunski
93 f6654822 Leszek Koltunski
      mPrevRendered = -1;
94
      mCurrRendered = -1;
95 2878c554 leszek
96 687263cc Leszek Koltunski
      mScreenW = 9*LEAF_SIZE;
97
      mScreenH = 9*LEAF_SIZE;
98
99
      mLeaf = new DistortedTexture();
100
      DistortedTexture surface = new DistortedTexture();
101 f3ca895f Leszek Koltunski
      mMesh = new MeshRectangles(1,1);
102 0c5fa440 Leszek Koltunski
      mMove = new Static3D(0,0,0);
103
      mScale= new Static3D(1,1,1);
104
105 687263cc Leszek Koltunski
      DistortedEffects rootEffects = new DistortedEffects(mScreenW,mScreenH,0);
106
      rootEffects.apply(new MatrixEffectScale(mScale));
107
      rootEffects.apply(new MatrixEffectMove(mMove));
108 40eef4b9 Leszek Koltunski
109 687263cc Leszek Koltunski
      mRoot = new DistortedNode(new DistortedTexture(), rootEffects, mMesh);
110 427ab7bf Leszek Koltunski
     
111 880bdf97 Leszek Koltunski
      Dynamic1D rot = new Dynamic1D(5000,0.0f);
112
      rot.setMode(Dynamic1D.MODE_JUMP);
113
      rot.add(new Static1D(  0));
114
      rot.add(new Static1D(360));
115 44771819 Leszek Koltunski
116 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  
117 a4d59c0b Leszek Koltunski
      int[] positions = new int[] {0,4*LEAF_SIZE,  3*LEAF_SIZE,4*LEAF_SIZE,  6*LEAF_SIZE,4*LEAF_SIZE,  3*LEAF_SIZE/2,3*LEAF_SIZE/2,  9*LEAF_SIZE/2,3*LEAF_SIZE/2};
118 427ab7bf Leszek Koltunski
      
119 67c3a83b leszek
      Static3D center = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
120 7589635e Leszek Koltunski
      Static3D axis   = new Static3D(0,0,1);
121 67c3a83b leszek
      Static3D moveVector = new Static3D(0,LEAF_SIZE,0);
122 7589635e Leszek Koltunski
123 44771819 Leszek Koltunski
      for(int j=0; j<NUM_LEAVES; j++)
124 427ab7bf Leszek Koltunski
        {
125 687263cc Leszek Koltunski
        mEffects[j] = new DistortedEffects(LEAF_SIZE,LEAF_SIZE,0);
126 6173632c Leszek Koltunski
        mEffects[j].apply(new MatrixEffectMove(moveVector));
127 dae661e9 Leszek Koltunski
        mEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
128 44771819 Leszek Koltunski
        }
129 3c8b1903 Leszek Koltunski
130 44771819 Leszek Koltunski
      for(int i=0; i<NUM_CIRCLES; i++)
131
        {
132 687263cc Leszek Koltunski
        DistortedEffects effects = new DistortedEffects(3*LEAF_SIZE,3*LEAF_SIZE,0);
133 6173632c Leszek Koltunski
        effects.apply( new MatrixEffectRotate(rot, axis, center) );
134 dae661e9 Leszek Koltunski
        effects.apply( new MatrixEffectMove(new Static3D(positions[2*i], positions[2*i+1], 0)) );
135 6173632c Leszek Koltunski
        effects.apply( new FragmentEffectChroma(new Static1D(0.5f), new Static3D(colors[3*i],colors[3*i+1], colors[3*i+2])) );
136 44771819 Leszek Koltunski
137
        mCircleNode[i] = new DistortedNode( surface, effects, mMesh);
138
        mRoot.attach(mCircleNode[i]);
139
140
        for(int j=0; j<NUM_LEAVES; j++) mCircleNode[i].attach(mLeaf, mEffects[j], mMesh);
141 427ab7bf Leszek Koltunski
        }
142 392e16fd Leszek Koltunski
143 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
144 fe59d375 Leszek Koltunski
      mScreen.attach(mRoot);
145 427ab7bf Leszek Koltunski
      }
146
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
   
149 6173632c Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
150
     {
151
     mCurrRendered = mScreen.render(System.currentTimeMillis());
152 2878c554 leszek
153 6173632c Leszek Koltunski
     if( mCurrRendered!=mPrevRendered )
154
       {
155
       mPrevRendered = mCurrRendered;
156 722c6ed3 Leszek Koltunski
157 6173632c Leszek Koltunski
       final OlimpicActivity act = (OlimpicActivity)mView.getContext();
158 722c6ed3 Leszek Koltunski
159 6173632c Leszek Koltunski
       act.runOnUiThread(new Runnable()
160 722c6ed3 Leszek Koltunski
          {
161
          public void run()
162
            {
163 6173632c Leszek Koltunski
           act.setText("rendered: "+mCurrRendered+" objects");
164
           }
165 722c6ed3 Leszek Koltunski
          });
166 6173632c Leszek Koltunski
       }
167
     }
168 427ab7bf Leszek Koltunski
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
    
171 6173632c Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
172
     {
173
     if( (float)mScreenH/mScreenW > (float)height/width )
174
       {
175
       int w = (height*mScreenW)/mScreenH;
176
       float factor = (float)height/mScreenH;
177
       mMove.set((width-w)/2 ,0, 0);
178
       mScale.set(factor,factor,factor);
179
       }
180
     else
181
       {
182
       int h = (width*mScreenH)/mScreenW;
183
       float factor = (float)width/mScreenW;
184
       mMove.set(0,(height-h)/2,0);
185
       mScale.set(factor,factor,factor);
186
       }
187 67c3a83b leszek
      
188 6173632c Leszek Koltunski
     mScreen.resize(width, height);
189
     }
190 427ab7bf Leszek Koltunski
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
    
193 6173632c Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
194
     {
195
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
196
     Bitmap leaf;
197 427ab7bf Leszek Koltunski
      
198 6173632c Leszek Koltunski
     try
199
       {
200
       leaf = BitmapFactory.decodeStream(is);
201
       }
202
     finally
203
       {
204
       try
205
         {
206
         is.close();
207
         }
208
       catch(IOException e) { }
209
       }
210 427ab7bf Leszek Koltunski
      
211 6173632c Leszek Koltunski
     mLeaf.setTexture(leaf);
212 6637d0f2 Leszek Koltunski
213 885b9cca leszek
     FragmentEffectChroma.enable();
214 6637d0f2 Leszek Koltunski
215 6173632c Leszek Koltunski
     try
216
       {
217 e3900503 Leszek Koltunski
       DistortedLibrary.onCreate(mView.getContext());
218 6173632c Leszek Koltunski
       }
219
     catch(Exception ex)
220
       {
221
       android.util.Log.e("Olympic", ex.getMessage() );
222
       }
223
     }
224 427ab7bf Leszek Koltunski
}