Project

General

Profile

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

examples / src / main / java / org / distorted / examples / olimpic / OlimpicRenderer.java @ 2878c554

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