Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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

    
20
package org.distorted.examples.olimpic;
21

    
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
import org.distorted.examples.R;
29

    
30
import org.distorted.library.EffectTypes;
31
import org.distorted.library.type.Dynamic1D;
32
import org.distorted.library.DistortedNode;
33
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static1D;
35
import org.distorted.library.Distorted;
36
import org.distorted.library.DistortedBitmap;
37
import org.distorted.library.type.Static4D;
38

    
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
   private static final int NUM_LEAVES = 8;  
51
   
52
   private GLSurfaceView mView;
53
   private DistortedNode[] mCircleNode;
54
   private DistortedNode mScreen;
55
   private DistortedBitmap mLeaf;
56
   private Dynamic1D mRot;
57
    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
   public OlimpicRenderer(GLSurfaceView v) 
61
      {     
62
      mView = v;
63
    
64
      mLeaf = new DistortedBitmap(LEAF_SIZE,LEAF_SIZE,1);
65
      
66
      mScreen = new DistortedNode(new DistortedBitmap(9*LEAF_SIZE,9*LEAF_SIZE,1));
67
     
68
      mRot = new Dynamic1D(5000,0.0f);
69
      mRot.setMode(Dynamic1D.MODE_JUMP);
70
      mRot.add(new Static1D(  0));
71
      mRot.add(new Static1D(360));
72
      
73
      mCircleNode = new DistortedNode[NUM_CIRCLES];
74
    
75
      int[] colors    = new int[] {0,0,1,  0,0,0,  1,0,0,  1,1,0,  0,1,0}; // blue, black, red, yellow, green  
76
      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};
77
      
78
      DistortedBitmap tmp;
79
      Static3D center = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
80
      Static3D axis   = new Static3D(0,0,1);
81
      Static3D moveVector = new Static3D(0,LEAF_SIZE,0);
82

    
83
      for(int i=0; i<NUM_CIRCLES; i++)
84
        {
85
        if( i==0 )
86
          {
87
          mCircleNode[i] = new DistortedNode(new DistortedBitmap(3*LEAF_SIZE,3*LEAF_SIZE,1));
88
        
89
          for(int j=0; j<NUM_LEAVES; j++)
90
            {
91
            tmp = new DistortedBitmap(mLeaf, Distorted.CLONE_BITMAP);
92
            mCircleNode[i].attach(tmp);
93
            tmp.rotate(center, new Static1D(j*(360/NUM_LEAVES)), axis);
94
            tmp.move(moveVector);
95
            }
96
          }
97
        else
98
          {
99
          mCircleNode[i] = new DistortedNode(mCircleNode[0], Distorted.CLONE_BITMAP|Distorted.CLONE_CHILDREN); 
100
          }
101
      
102
        mScreen.attach(mCircleNode[i]);
103
        tmp = (DistortedBitmap)mCircleNode[i].getObject();
104
        tmp.move( new Static3D(positions[2*i], positions[2*i+1], 0) );
105
        tmp.rotate( center, mRot, axis );
106
        tmp.chroma( new Static1D(0.8f), new Static3D(colors[3*i],colors[3*i+1], colors[3*i+2]) );
107
        }
108
      }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
   
112
    public void onDrawFrame(GL10 glUnused) 
113
      {
114
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
115
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
116
      
117
      mScreen.draw(System.currentTimeMillis());
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
    
122
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
123
      {
124
      DistortedBitmap bmp = (DistortedBitmap)mScreen.getObject();
125
      int bmpWidth  = bmp.getWidth();
126
      int bmpHeight = bmp.getHeight();
127
     
128
      bmp.abortEffects(EffectTypes.MATRIX);
129
      
130
      if( bmpHeight/bmpWidth > height/width )
131
        {
132
        int w = (height*bmpWidth)/bmpHeight;
133
        float factor = (float)height/bmpHeight;
134

    
135
        bmp.move( new Static3D((width-w)/2 ,0, 0) );
136
        bmp.scale( factor );
137
        }
138
      else
139
        {
140
        int h = (width*bmpHeight)/bmpWidth;
141
        float factor = (float)width/bmpWidth;
142

    
143
        bmp.move( new Static3D(0,(height-h)/2,0) );
144
        bmp.scale( factor );
145
        }
146
      
147
      Distorted.onSurfaceChanged(width, height); 
148
      }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
    
152
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
153
      {
154
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
155
      Bitmap leaf;
156
      
157
      try 
158
        {
159
        leaf = BitmapFactory.decodeStream(is);
160
        } 
161
      finally 
162
        {
163
        try 
164
          {
165
          is.close();
166
          } 
167
        catch(IOException e) { }
168
        }  
169
      
170
      mLeaf.setBitmap(leaf);
171
      
172
      try
173
        {
174
        Distorted.onSurfaceCreated(mView.getContext());
175
        }
176
      catch(Exception ex)
177
        {
178
        android.util.Log.e("Olympic", ex.getMessage() );
179
        }
180
      }
181
 
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
    
184
}
(2-2/3)