Project

General

Profile

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

examples / src / main / java / org / distorted / examples / olimpic / OlimpicRenderer.java @ 10b7e588

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