Project

General

Profile

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

examples / src / main / java / org / distorted / examples / olimpic / OlimpicRenderer.java @ 722c6ed3

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.content.Context;
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44
import android.opengl.GLES30;
45
import android.opengl.GLSurfaceView;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
   OlimpicRenderer(GLSurfaceView v)
65
      {     
66
      mView = v;
67

    
68
      mPrevRendered = 0;
69
      mCurrRendered = 0;
70

    
71
      mLeaf = new DistortedTexture(LEAF_SIZE,LEAF_SIZE);
72
      MeshFlat mesh = new MeshFlat(1,1);
73
      DistortedEffects effects = new DistortedEffects();
74

    
75
      mScreenW = 9*LEAF_SIZE;
76
      mScreenH = 9*LEAF_SIZE;
77
      mRoot = new DistortedNode(new DistortedTexture(mScreenW,mScreenH), effects, mesh);
78
     
79
      Dynamic1D rot = new Dynamic1D(5000,0.0f);
80
      rot.setMode(Dynamic1D.MODE_JUMP);
81
      rot.add(new Static1D(  0));
82
      rot.add(new Static1D(360));
83
      
84
      DistortedNode[] mCircleNode = new DistortedNode[NUM_CIRCLES];
85
    
86
      int[] colors    = new int[] {0,0,1,  0,0,0,  1,0,0,  1,1,0,  0,1,0}; // blue, black, red, yellow, green  
87
      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};
88
      
89
      Static3D center = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
90
      Static3D axis   = new Static3D(0,0,1);
91
      Static3D moveVector = new Static3D(0,LEAF_SIZE,0);
92

    
93
      for(int i=0; i<NUM_CIRCLES; i++)
94
        {
95
        if( i<=1 )
96
          {
97
          effects = new DistortedEffects();
98
          mCircleNode[i] = new DistortedNode(new DistortedTexture(3*LEAF_SIZE,3*LEAF_SIZE), effects, mesh);
99
        
100
          for(int j=0; j<NUM_LEAVES-i; j++)
101
            {
102
            effects = new DistortedEffects();
103
            mCircleNode[i].attach(mLeaf, effects, mesh);
104
            effects.rotate( new Static1D(j*(360/NUM_LEAVES)), axis, center );
105
            effects.move(moveVector);
106
            }
107
          }
108
        else
109
          {
110
          mCircleNode[i] = new DistortedNode(mCircleNode[0], Distorted.CLONE_SURFACE |Distorted.CLONE_CHILDREN);
111
          }
112

    
113
        mRoot.attach(mCircleNode[i]);
114
        effects = mCircleNode[i].getEffects();
115
        effects.move( new Static3D(positions[2*i], positions[2*i+1], 0) );
116
        effects.rotate( rot, axis, center );
117
        effects.chroma( new Static1D(0.5f), new Static3D(colors[3*i],colors[3*i+1], colors[3*i+2]) );
118
        }
119

    
120
      mScreen = new DistortedScreen();
121
      mScreen.attach(mRoot);
122
      }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
   
126
    public void onDrawFrame(GL10 glUnused) 
127
      {
128
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
129
      mCurrRendered = mScreen.render(System.currentTimeMillis());
130

    
131
      if( mCurrRendered!=mPrevRendered )
132
        {
133
        mPrevRendered = mCurrRendered;
134

    
135
        final OlimpicActivity act = (OlimpicActivity)mView.getContext();
136

    
137
        act.runOnUiThread(new Runnable()
138
          {
139
          public void run()
140
            {
141
            act.setText("rendered: "+mCurrRendered+" objects");
142
            }
143
          });
144
        }
145
      }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
    
149
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
150
      {
151
      DistortedEffects effects = mRoot.getEffects();
152

    
153
      effects.abortEffects(EffectTypes.MATRIX);
154
      
155
      if( (float)mScreenH/mScreenW > (float)height/width )
156
        {
157
        int w = (height*mScreenW)/mScreenH;
158
        float factor = (float)height/mScreenH;
159

    
160
        effects.move( new Static3D((width-w)/2 ,0, 0) );
161
        effects.scale( factor );
162
        }
163
      else
164
        {
165
        int h = (width*mScreenH)/mScreenW;
166
        float factor = (float)width/mScreenW;
167

    
168
        effects.move( new Static3D(0,(height-h)/2,0) );
169
        effects.scale( factor );
170
        }
171
      
172
      mScreen.resize(width, height);
173
      }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
    
177
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
178
      {
179
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
180

    
181
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
182
      Bitmap leaf;
183
      
184
      try 
185
        {
186
        leaf = BitmapFactory.decodeStream(is);
187
        } 
188
      finally 
189
        {
190
        try 
191
          {
192
          is.close();
193
          } 
194
        catch(IOException e) { }
195
        }  
196
      
197
      mLeaf.setTexture(leaf);
198
      
199
      try
200
        {
201
        Distorted.onCreate(mView.getContext());
202
        }
203
      catch(Exception ex)
204
        {
205
        android.util.Log.e("Olympic", ex.getMessage() );
206
        }
207
      }
208
 
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210
    
211
}
(2-2/3)