Project

General

Profile

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

examples / src / main / java / org / distorted / examples / earth / EarthRenderer.java @ 676c14da

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.earth;
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.effect.Effect;
28
import org.distorted.library.effect.EffectName;
29
import org.distorted.library.effect.EffectType;
30
import org.distorted.library.effect.FragmentEffectAlpha;
31
import org.distorted.library.effect.FragmentEffectBrightness;
32
import org.distorted.library.effect.FragmentEffectChroma;
33
import org.distorted.library.effect.FragmentEffectContrast;
34
import org.distorted.library.effect.FragmentEffectSaturation;
35
import org.distorted.library.effect.MatrixEffectMove;
36
import org.distorted.library.effect.MatrixEffectQuaternion;
37
import org.distorted.library.effect.MatrixEffectScale;
38
import org.distorted.library.effect.VertexEffectDeform;
39
import org.distorted.library.effect.VertexEffectDistort;
40
import org.distorted.library.effect.VertexEffectPinch;
41
import org.distorted.library.effect.VertexEffectSink;
42
import org.distorted.library.effect.VertexEffectSwirl;
43
import org.distorted.library.effect.VertexEffectWave;
44
import org.distorted.library.main.Distorted;
45
import org.distorted.library.main.DistortedEffects;
46
import org.distorted.library.main.DistortedScreen;
47
import org.distorted.library.main.DistortedTexture;
48
import org.distorted.library.mesh.MeshBase;
49
import org.distorted.library.mesh.MeshSphere;
50
import org.distorted.library.type.Dynamic3D;
51
import org.distorted.library.type.DynamicQuat;
52
import org.distorted.library.type.Static1D;
53
import org.distorted.library.type.Static2D;
54
import org.distorted.library.type.Static3D;
55
import org.distorted.library.type.Static4D;
56
import org.distorted.library.type.Static5D;
57

    
58
import java.io.IOException;
59
import java.io.InputStream;
60

    
61
import javax.microedition.khronos.egl.EGLConfig;
62
import javax.microedition.khronos.opengles.GL10;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
class EarthRenderer implements GLSurfaceView.Renderer
67
{
68
    private static final int   SIZE    =     500;
69
    private static final int   RADIUS_V= SIZE/ 8;
70
    private static final int   RADIUS_F= SIZE/15;
71
    private static final int   LEVEL   =      32;
72
    private static final float FOV     =   30.0f;
73
    private static final float NEAR    =    0.1f;
74
    private static final int   MAX_EFF =      20;
75
    private static final float SCALE   =   1.10f;
76

    
77
    private GLSurfaceView mView;
78
    private DistortedTexture mTexture;
79
    private DistortedEffects mEffects;
80
    private MeshBase mMesh;
81
    private DistortedScreen mScreen;
82
    private int mObjWidth, mObjHeight, mObjDepth;
83
    private Static3D mMove, mScaleFactor, mCenter;
84

    
85
    private int mScrWidth, mScrHeight;
86
    private float mLevel;
87

    
88
    private Static3D mColor;
89
    private Static3D mRegionF;
90
    private Static4D mRegionV;
91
    private Static1D mStrength;
92
    private Static2D mPinch;
93
    private Static1D mSwirl;
94

    
95
    Static4D mQuat1, mQuat2;
96
    int mScreenMin;
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
    EarthRenderer(GLSurfaceView v)
101
      {
102
      mView = v;
103

    
104
      mLevel = SCALE;
105

    
106
      mStrength   = new Static1D(0.5f);
107
      mColor      = new Static3D(255,0,0);
108
      mRegionF    = new Static3D(RADIUS_F,RADIUS_F,RADIUS_F);
109
      mRegionV    = new Static4D(0,0,0,RADIUS_V);
110
      mMove       = new Static3D(0,0,0);
111
      mScaleFactor= new Static3D(1,1,1);
112
      mCenter     = new Static3D(0,0,0);
113
      mPinch      = new Static2D(0.5f,0.0f);
114
      mSwirl      = new Static1D(45.0f);
115

    
116
      Dynamic3D scale = new Dynamic3D(0,0.5f);
117
      scale.add(mScaleFactor);
118

    
119
      mMesh     = new MeshSphere(LEVEL);
120
      mTexture  = new DistortedTexture(SIZE,SIZE);
121

    
122
      mObjWidth = mTexture.getWidth();
123
      mObjHeight= mTexture.getHeight();
124
      mObjDepth = mTexture.getDepth(mMesh);
125

    
126
      mQuat1 = new Static4D(0,0,0,1);  // unity
127
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
128
      
129
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
130
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
131

    
132
      quatInt1.add(mQuat1);
133
      quatInt2.add(mQuat2);
134

    
135
      mEffects = new DistortedEffects();
136
      mEffects.apply( new MatrixEffectMove(mMove) );
137
      mEffects.apply( new MatrixEffectScale(scale));
138
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, mCenter) );
139
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, mCenter) );
140

    
141
      mScreen = new DistortedScreen();
142
      mScreen.setProjection(FOV, NEAR);
143
      }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
   
147
    public void onDrawFrame(GL10 glUnused) 
148
      {
149
      mScreen.render( System.currentTimeMillis() );
150
      }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
    
154
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
155
      {
156
      mScrWidth = width;
157
      mScrHeight= height;
158
      mScreenMin= width<height ? width:height;
159

    
160
      adjustFactor();
161

    
162
      mCenter.set( (float)mObjWidth/2, (float)mObjHeight/2, (float)mObjDepth/2 );
163
      mScreen.resize(width, height);
164
      }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
    void setLevel(int level)
169
      {
170
      float N=3.0f;
171

    
172
      // MAP: 0--> SCALE/N   50-->SCALE   100->SCALE*N
173
      mLevel = SCALE* (1.0f+  (level>=50 ? N-1 : (N-1)/N) * (level-50)/50.0f);
174

    
175
      adjustFactor();
176
      }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
    private void adjustFactor()
181
      {
182
      float xW = (float)mScrWidth /mObjWidth;
183
      float xH = (float)mScrHeight/mObjHeight;
184

    
185
      float factor = xW>xH ? mLevel*xH : mLevel*xW;
186

    
187
      mMove.set( (mScrWidth-factor*mObjWidth)/2 , (mScrHeight-factor*mObjHeight)/2 , -factor*mObjDepth );
188
      mScaleFactor.set(factor,factor,factor);
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
    void addNewPoint(float longitude, float latitude, EffectName name)
194
      {
195
      final float CORR = 10.0F;
196
      Static3D vector;
197
      Effect effect =null;
198

    
199
      double sinLON = Math.sin(longitude);
200
      double cosLON = Math.cos(longitude);
201
      double sinLAT = Math.sin(latitude);
202
      double cosLAT = Math.cos(latitude);
203

    
204
      float x = (float)(sinLON*cosLAT)/2.0f;
205
      float y = (float)       -sinLAT /2.0f;
206
      float z = (float)(cosLON*cosLAT)/2.0f;
207

    
208
      Static3D center = new Static3D( (0.5f+x)*mObjWidth, (0.5f+y)*mObjHeight, (0.5f+z)*mObjDepth);
209

    
210
      switch(name)
211
        {
212
        case DISTORT          : vector = new Static3D( +x*mObjWidth/CORR, +y*mObjHeight/CORR, +z*mObjDepth/CORR);
213
                                effect = new VertexEffectDistort(   vector, center, mRegionV); break;
214
        case DEFORM           : vector = new Static3D( +x*mObjWidth/CORR, +y*mObjHeight/CORR, +z*mObjDepth/CORR);
215
                                effect = new VertexEffectDeform (   vector, center, mRegionV); break;
216
        case SINK             : effect = new VertexEffectSink   (mStrength, center, mRegionV); break;
217
        case PINCH            : effect = new VertexEffectPinch  (mPinch   , center, mRegionV); break;      // Those 3 are not really
218
        case SWIRL            : effect = new VertexEffectSwirl  (mSwirl   , center, mRegionV); break;      // fully 3D effects. They
219
        case WAVE             : Static5D wave = new Static5D( RADIUS_V/2, RADIUS_V/2, 0, 90-latitude, 90); // will not look good
220
                                effect = new VertexEffectWave   (wave     , center, mRegionV); break;      // everywhere on the sphere.
221

    
222
        case ALPHA            : effect = new FragmentEffectAlpha     (mStrength,        center, mRegionF, false); break;
223
        case SMOOTH_ALPHA     : effect = new FragmentEffectAlpha     (mStrength,        center, mRegionF, true ); break;
224
        case CHROMA           : effect = new FragmentEffectChroma    (mStrength, mColor,center, mRegionF, false); break;
225
        case SMOOTH_CHROMA    : effect = new FragmentEffectChroma    (mStrength, mColor,center, mRegionF, true ); break;
226
        case BRIGHTNESS       : effect = new FragmentEffectBrightness(mStrength,        center, mRegionF, false); break;
227
        case SMOOTH_BRIGHTNESS: effect = new FragmentEffectBrightness(mStrength,        center, mRegionF, true ); break;
228
        case SATURATION       : effect = new FragmentEffectSaturation(mStrength,        center, mRegionF, false); break;
229
        case SMOOTH_SATURATION: effect = new FragmentEffectSaturation(mStrength,        center, mRegionF, true ); break;
230
        case CONTRAST         : effect = new FragmentEffectContrast  (mStrength,        center, mRegionF, false); break;
231
        case SMOOTH_CONTRAST  : effect = new FragmentEffectContrast  (mStrength,        center, mRegionF, true ); break;
232

    
233
        default               : android.util.Log.e("EarthRenderer", "unexpected effect: "+name.toString() );
234
        }
235

    
236
      if( effect!=null )
237
        {
238
        mEffects.apply(effect);
239
        }
240
      }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
    void removeAll()
245
      {
246
      mEffects.abortByType(EffectType.VERTEX);
247
      mEffects.abortByType(EffectType.FRAGMENT);
248
      }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251
    
252
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
253
      {
254
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.world);
255
      Bitmap bitmap;
256

    
257
      try
258
        {
259
        bitmap = BitmapFactory.decodeStream(is);
260
        }
261
      finally
262
        {
263
        try
264
          {
265
          is.close();
266
          }
267
        catch(IOException e) { }
268
        }
269

    
270
      mTexture.setTexture(bitmap);
271

    
272
      mScreen.detachAll();
273
      mScreen.attach(mTexture,mEffects,mMesh);
274

    
275
      Effect.enableEffects(EffectType.FRAGMENT);
276
      Effect.enableEffects(EffectType.VERTEX);
277

    
278
      DistortedEffects.setMax(EffectType.VERTEX  , MAX_EFF);
279
      DistortedEffects.setMax(EffectType.FRAGMENT, MAX_EFF);
280

    
281
      try
282
        {
283
        Distorted.onCreate(mView.getContext());
284
        }
285
      catch(Exception ex)
286
        {
287
        android.util.Log.e("Earth", ex.getMessage() );
288
        }
289
      }
290
}
(2-2/4)