Project

General

Profile

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

examples / src / main / java / org / distorted / examples / olimpic / OlimpicRenderer.java @ 6ab5da9a

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 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 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
31 d218d64e leszek
import org.distorted.library.DistortedScreen;
32 8744aa41 Leszek Koltunski
import org.distorted.library.DistortedNode;
33 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
34 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
35 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static1D;
38 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
39 40eef4b9 Leszek Koltunski
import org.distorted.library.DistortedTexture;
40 427ab7bf Leszek Koltunski
41 722c6ed3 Leszek Koltunski
import android.content.Context;
42 427ab7bf Leszek Koltunski
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
45 427ab7bf Leszek Koltunski
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 e7a4ef16 Leszek Koltunski
   private static final int NUM_LEAVES  = 8;
54 427ab7bf Leszek Koltunski
   
55
   private GLSurfaceView mView;
56 8744aa41 Leszek Koltunski
   private DistortedNode mRoot;
57 40eef4b9 Leszek Koltunski
   private DistortedTexture mLeaf;
58 d218d64e leszek
   private DistortedScreen mScreen;
59 40eef4b9 Leszek Koltunski
   private int mScreenW, mScreenH;
60 2878c554 leszek
   private int mPrevRendered, mCurrRendered;
61 880bdf97 Leszek Koltunski
62 6ab5da9a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64
   void pressed(int color, int number)
65
     {
66
     android.util.Log.d("Olimpic","Color: "+color+" number:"+number+" pressed!");
67
     }
68
69 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71 508f22b5 Leszek Koltunski
   OlimpicRenderer(GLSurfaceView v)
72 427ab7bf Leszek Koltunski
      {     
73
      mView = v;
74 e8b6aa95 Leszek Koltunski
75 2878c554 leszek
      mPrevRendered = 0;
76
      mCurrRendered = 0;
77
78 7451c98a Leszek Koltunski
      mLeaf = new DistortedTexture(LEAF_SIZE,LEAF_SIZE);
79 b01acdaf Leszek Koltunski
      MeshFlat mesh = new MeshFlat(1,1);
80 d04a4886 Leszek Koltunski
      DistortedEffects effects = new DistortedEffects();
81 40eef4b9 Leszek Koltunski
82
      mScreenW = 9*LEAF_SIZE;
83
      mScreenH = 9*LEAF_SIZE;
84 8744aa41 Leszek Koltunski
      mRoot = new DistortedNode(new DistortedTexture(mScreenW,mScreenH), effects, mesh);
85 427ab7bf Leszek Koltunski
     
86 880bdf97 Leszek Koltunski
      Dynamic1D rot = new Dynamic1D(5000,0.0f);
87
      rot.setMode(Dynamic1D.MODE_JUMP);
88
      rot.add(new Static1D(  0));
89
      rot.add(new Static1D(360));
90 427ab7bf Leszek Koltunski
      
91 8744aa41 Leszek Koltunski
      DistortedNode[] mCircleNode = new DistortedNode[NUM_CIRCLES];
92 427ab7bf Leszek Koltunski
    
93
      int[] colors    = new int[] {0,0,1,  0,0,0,  1,0,0,  1,1,0,  0,1,0}; // blue, black, red, yellow, green  
94 40eef4b9 Leszek Koltunski
      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};
95 427ab7bf Leszek Koltunski
      
96 7589635e Leszek Koltunski
      Static3D center = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
97
      Static3D axis   = new Static3D(0,0,1);
98
      Static3D moveVector = new Static3D(0,LEAF_SIZE,0);
99
100 427ab7bf Leszek Koltunski
      for(int i=0; i<NUM_CIRCLES; i++)
101
        {
102 3c8b1903 Leszek Koltunski
        if( i<=1 )
103 427ab7bf Leszek Koltunski
          {
104 d04a4886 Leszek Koltunski
          effects = new DistortedEffects();
105 8744aa41 Leszek Koltunski
          mCircleNode[i] = new DistortedNode(new DistortedTexture(3*LEAF_SIZE,3*LEAF_SIZE), effects, mesh);
106 427ab7bf Leszek Koltunski
        
107 3c8b1903 Leszek Koltunski
          for(int j=0; j<NUM_LEAVES-i; j++)
108 427ab7bf Leszek Koltunski
            {
109 d04a4886 Leszek Koltunski
            effects = new DistortedEffects();
110 b01acdaf Leszek Koltunski
            mCircleNode[i].attach(mLeaf, effects, mesh);
111 392e16fd Leszek Koltunski
            effects.rotate( new Static1D(j*(360/NUM_LEAVES)), axis, center );
112
            effects.move(moveVector);
113 427ab7bf Leszek Koltunski
            }
114
          }
115
        else
116
          {
117 8744aa41 Leszek Koltunski
          mCircleNode[i] = new DistortedNode(mCircleNode[0], Distorted.CLONE_SURFACE |Distorted.CLONE_CHILDREN);
118 427ab7bf Leszek Koltunski
          }
119 3c8b1903 Leszek Koltunski
120 392e16fd Leszek Koltunski
        mRoot.attach(mCircleNode[i]);
121 d04a4886 Leszek Koltunski
        effects = mCircleNode[i].getEffects();
122 392e16fd Leszek Koltunski
        effects.move( new Static3D(positions[2*i], positions[2*i+1], 0) );
123
        effects.rotate( rot, axis, center );
124
        effects.chroma( new Static1D(0.5f), new Static3D(colors[3*i],colors[3*i+1], colors[3*i+2]) );
125 427ab7bf Leszek Koltunski
        }
126 392e16fd Leszek Koltunski
127 d218d64e leszek
      mScreen = new DistortedScreen();
128 fe59d375 Leszek Koltunski
      mScreen.attach(mRoot);
129 427ab7bf Leszek Koltunski
      }
130
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
   
133
    public void onDrawFrame(GL10 glUnused) 
134
      {
135 41a81a14 Leszek Koltunski
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
136 2878c554 leszek
      mCurrRendered = mScreen.render(System.currentTimeMillis());
137
138
      if( mCurrRendered!=mPrevRendered )
139
        {
140
        mPrevRendered = mCurrRendered;
141 722c6ed3 Leszek Koltunski
142
        final OlimpicActivity act = (OlimpicActivity)mView.getContext();
143
144
        act.runOnUiThread(new Runnable()
145
          {
146
          public void run()
147
            {
148
            act.setText("rendered: "+mCurrRendered+" objects");
149
            }
150
          });
151 2878c554 leszek
        }
152 427ab7bf Leszek Koltunski
      }
153
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
    
156
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
157
      {
158 d04a4886 Leszek Koltunski
      DistortedEffects effects = mRoot.getEffects();
159 40eef4b9 Leszek Koltunski
160 392e16fd Leszek Koltunski
      effects.abortEffects(EffectTypes.MATRIX);
161 427ab7bf Leszek Koltunski
      
162 40eef4b9 Leszek Koltunski
      if( (float)mScreenH/mScreenW > (float)height/width )
163 427ab7bf Leszek Koltunski
        {
164 40eef4b9 Leszek Koltunski
        int w = (height*mScreenW)/mScreenH;
165
        float factor = (float)height/mScreenH;
166 7589635e Leszek Koltunski
167 392e16fd Leszek Koltunski
        effects.move( new Static3D((width-w)/2 ,0, 0) );
168
        effects.scale( factor );
169 427ab7bf Leszek Koltunski
        }
170
      else
171
        {
172 40eef4b9 Leszek Koltunski
        int h = (width*mScreenH)/mScreenW;
173
        float factor = (float)width/mScreenW;
174 7589635e Leszek Koltunski
175 392e16fd Leszek Koltunski
        effects.move( new Static3D(0,(height-h)/2,0) );
176
        effects.scale( factor );
177 427ab7bf Leszek Koltunski
        }
178
      
179 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
180 427ab7bf Leszek Koltunski
      }
181
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
    
184
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
185
      {
186 41a81a14 Leszek Koltunski
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
187 e7a4ef16 Leszek Koltunski
188 427ab7bf Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
189
      Bitmap leaf;
190
      
191
      try 
192
        {
193
        leaf = BitmapFactory.decodeStream(is);
194
        } 
195
      finally 
196
        {
197
        try 
198
          {
199
          is.close();
200
          } 
201
        catch(IOException e) { }
202
        }  
203
      
204 e8b6aa95 Leszek Koltunski
      mLeaf.setTexture(leaf);
205 427ab7bf Leszek Koltunski
      
206
      try
207
        {
208 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
209 427ab7bf Leszek Koltunski
        }
210
      catch(Exception ex)
211
        {
212 ed1c0b33 Leszek Koltunski
        android.util.Log.e("Olympic", ex.getMessage() );
213 427ab7bf Leszek Koltunski
        }
214
      }
215
 
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217
    
218
}