Project

General

Profile

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

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

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