Project

General

Profile

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

examples / src / main / java / org / distorted / examples / olimpic / OlimpicRenderer.java @ 7451c98a

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