Project

General

Profile

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

examples / src / main / java / org / distorted / examples / glow / GlowRenderer.java @ 08602667

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.glow;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25

    
26
import org.distorted.examples.R;
27
import org.distorted.library.Distorted;
28
import org.distorted.library.DistortedEffects;
29
import org.distorted.library.DistortedNode;
30
import org.distorted.library.DistortedScreen;
31
import org.distorted.library.DistortedTexture;
32
import org.distorted.library.EffectNames;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.MeshFlat;
35
import org.distorted.library.MeshObject;
36
import org.distorted.library.type.Dynamic1D;
37
import org.distorted.library.type.Static1D;
38
import org.distorted.library.type.Static3D;
39

    
40
import java.io.IOException;
41
import java.io.InputStream;
42

    
43
import javax.microedition.khronos.egl.EGLConfig;
44
import javax.microedition.khronos.opengles.GL10;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
class GlowRenderer implements GLSurfaceView.Renderer
49
{
50
   private static final int[] colors  = new int[] {0,0,1,  0,0,0,  1,0,0,  1,1,0,  0,1,0,  1,1,1}; // blue, black, red, yellow, green, white
51
   private static final int LEAF_SIZE = 100;
52
   private static final int NUM_LEAVES= colors.length/3;
53
   
54
   private GLSurfaceView mView;
55
   private DistortedNode mRoot;
56
   private DistortedTexture mLeaf;
57
   private DistortedScreen mScreen;
58
   private int mRootW, mRootH;
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
   GlowRenderer(GLSurfaceView v)
63
      {     
64
      mView = v;
65

    
66
      mRootW = 3*LEAF_SIZE;
67
      mRootH = 3*LEAF_SIZE;
68

    
69
      mLeaf = new DistortedTexture(LEAF_SIZE,LEAF_SIZE);
70
      DistortedTexture surface = new DistortedTexture(mRootW,mRootH);
71
      MeshObject mesh = new MeshFlat(1,1);
72
      DistortedEffects[] leafEffects = new DistortedEffects[NUM_LEAVES];
73

    
74
      mRoot = new DistortedNode(surface, new DistortedEffects(), mesh);
75
     
76
      Static3D moveVector = new Static3D(0,LEAF_SIZE,0);
77
      Static1D chromaLevel= new Static1D(0.5f);
78
      Static3D center     = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
79
      Static3D axis       = new Static3D(0,0,1);
80

    
81
      for(int j=0; j<NUM_LEAVES; j++)
82
        {
83
        leafEffects[j] = new DistortedEffects();
84
        leafEffects[j].rotate( new Static1D(j*(360/NUM_LEAVES)), axis, center );
85
        leafEffects[j].move(moveVector);
86
        leafEffects[j].chroma( chromaLevel, new Static3D(colors[3*j],colors[3*j+1], colors[3*j+2]) );
87

    
88
        mRoot.attach( mLeaf, leafEffects[j], mesh);
89
        }
90

    
91
      mScreen = new DistortedScreen(mView);
92
      mScreen.attach(mRoot);
93
      }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
   
97
    public void onDrawFrame(GL10 glUnused) 
98
      {
99
      mScreen.render(System.currentTimeMillis());
100
      }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
    
104
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
105
      {
106
      float qw = (float)width /mRootW;
107
      float qh = (float)height/mRootH;
108
      float factor = 0.6f* (qw<qh ? qw:qh);
109
      int w = (int)(factor*mRootW);
110
      int h = (int)(factor*mRootH);
111

    
112
      Dynamic1D rot = new Dynamic1D(5000,0.0f);
113
      rot.setMode(Dynamic1D.MODE_JUMP);
114
      rot.add(new Static1D(  0));
115
      rot.add(new Static1D(360));
116

    
117
      Static3D center = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
118
      Static3D axis   = new Static3D(0,0,1);
119

    
120
      DistortedEffects effects = mRoot.getEffects();
121
      effects.abortEffects(EffectTypes.MATRIX);
122
      effects.move( new Static3D((width-w)/2 ,(height-h)/2, 0) );
123
      effects.scale( factor );
124
      effects.rotate( rot, axis, center );
125

    
126
      mScreen.resize(width, height);
127
      }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
    
131
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
132
      {
133
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
134
      Bitmap leaf;
135
      
136
      try 
137
        {
138
        leaf = BitmapFactory.decodeStream(is);
139
        } 
140
      finally 
141
        {
142
        try 
143
          {
144
          is.close();
145
          } 
146
        catch(IOException e) { }
147
        }  
148
      
149
      mLeaf.setTexture(leaf);
150

    
151
      DistortedEffects.enableEffect(EffectNames.CHROMA);
152
      DistortedEffects.enableEffect(EffectNames.GLOW);
153

    
154
      try
155
        {
156
        Distorted.onCreate(mView.getContext());
157
        }
158
      catch(Exception ex)
159
        {
160
        android.util.Log.e("Glow", ex.getMessage() );
161
        }
162
      }
163
 
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
    
166
}
(2-2/3)