Project

General

Profile

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

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

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.EffectTypes;
34
import org.distorted.library.MeshFlat;
35
import org.distorted.library.type.Dynamic1D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static1D;
38
import org.distorted.library.Distorted;
39
import org.distorted.library.DistortedTexture;
40

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

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

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

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

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
   void pressed(int color, int number, boolean checked)
68
     {
69
     android.util.Log.d("Olimpic","Color: "+color+" number:"+number+" checked: "+checked);
70

    
71
     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
         if( checked ) mCircleNode[color].attach( mLeaf, mEffects[number-1], mMesh);
81
         else          mCircleNode[color].detach(mEffects[number-1]);
82
         }
83
       }
84
     }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

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

    
92
      mPrevRendered = 0;
93
      mCurrRendered = 0;
94

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
197
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
198
      Bitmap leaf;
199
      
200
      try 
201
        {
202
        leaf = BitmapFactory.decodeStream(is);
203
        } 
204
      finally 
205
        {
206
        try 
207
          {
208
          is.close();
209
          } 
210
        catch(IOException e) { }
211
        }  
212
      
213
      mLeaf.setTexture(leaf);
214
      
215
      try
216
        {
217
        Distorted.onCreate(mView.getContext());
218
        }
219
      catch(Exception ex)
220
        {
221
        android.util.Log.e("Olympic", ex.getMessage() );
222
        }
223
      }
224
 
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226
    
227
}
(2-2/3)