Project

General

Profile

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

examples / src / main / java / org / distorted / examples / earth / EarthRenderer.java @ e8e54972

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19 427ab7bf Leszek Koltunski
20 64558e4e Leszek Koltunski
package org.distorted.examples.earth;
21 427ab7bf Leszek Koltunski
22 9814e53c Leszek Koltunski
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24 64558e4e Leszek Koltunski
import android.opengl.GLSurfaceView;
25 427ab7bf Leszek Koltunski
26 9814e53c Leszek Koltunski
import org.distorted.examples.R;
27 1585ba24 Leszek Koltunski
import org.distorted.library.effect.Effect;
28 296d2e73 Leszek Koltunski
import org.distorted.library.effect.EffectName;
29
import org.distorted.library.effect.EffectType;
30 1585ba24 Leszek Koltunski
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 37b324c4 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
36
import org.distorted.library.effect.MatrixEffectQuaternion;
37
import org.distorted.library.effect.MatrixEffectScale;
38 1585ba24 Leszek Koltunski
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 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
45 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
46
import org.distorted.library.main.DistortedScreen;
47
import org.distorted.library.main.DistortedTexture;
48 57d7fdba Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
49 296d2e73 Leszek Koltunski
import org.distorted.library.mesh.MeshSphere;
50 1833b023 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
51 7589635e Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
52 1585ba24 Leszek Koltunski
import org.distorted.library.type.Static1D;
53 edc4a0e4 Leszek Koltunski
import org.distorted.library.type.Static2D;
54 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
55 64558e4e Leszek Koltunski
import org.distorted.library.type.Static4D;
56 edc4a0e4 Leszek Koltunski
import org.distorted.library.type.Static5D;
57 427ab7bf Leszek Koltunski
58 9814e53c Leszek Koltunski
import java.io.IOException;
59
import java.io.InputStream;
60
61 64558e4e Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
62
import javax.microedition.khronos.opengles.GL10;
63 427ab7bf Leszek Koltunski
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66 64558e4e Leszek Koltunski
class EarthRenderer implements GLSurfaceView.Renderer
67 427ab7bf Leszek Koltunski
{
68 823f63f7 Leszek Koltunski
    private static final int   SIZE    =     500;
69 edc4a0e4 Leszek Koltunski
    private static final int   RADIUS_V= SIZE/ 8;
70 823f63f7 Leszek Koltunski
    private static final int   RADIUS_F= SIZE/15;
71 1c1df934 Leszek Koltunski
    private static final int   LEVEL   =      40;
72 823f63f7 Leszek Koltunski
    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 64558e4e Leszek Koltunski
77 427ab7bf Leszek Koltunski
    private GLSurfaceView mView;
78 f6d884d5 Leszek Koltunski
    private DistortedTexture mTexture;
79 d04a4886 Leszek Koltunski
    private DistortedEffects mEffects;
80 57d7fdba Leszek Koltunski
    private MeshBase mMesh;
81 d218d64e leszek
    private DistortedScreen mScreen;
82 8a99c681 Leszek Koltunski
    private int mObjWidth, mObjHeight, mObjDepth;
83 16b22aab Leszek Koltunski
    private Static3D mMove, mScaleFactor;
84 261fe5bd Leszek Koltunski
85 1833b023 Leszek Koltunski
    private int mScrWidth, mScrHeight;
86
    private float mLevel;
87 9c3e749e Leszek Koltunski
88 1585ba24 Leszek Koltunski
    private Static3D mColor;
89 b88ec561 Leszek Koltunski
    private Static3D mRegionF;
90 823f63f7 Leszek Koltunski
    private Static4D mRegionV;
91 1585ba24 Leszek Koltunski
    private Static1D mStrength;
92 5e23b17b Leszek Koltunski
    private Static3D mPinch;
93 edc4a0e4 Leszek Koltunski
    private Static1D mSwirl;
94 1585ba24 Leszek Koltunski
95 1833b023 Leszek Koltunski
    Static4D mQuat1, mQuat2;
96
    int mScreenMin;
97
98 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
99
100 64558e4e Leszek Koltunski
    EarthRenderer(GLSurfaceView v)
101 427ab7bf Leszek Koltunski
      {
102
      mView = v;
103 261fe5bd Leszek Koltunski
104 1833b023 Leszek Koltunski
      mLevel = SCALE;
105
106
      mStrength   = new Static1D(0.5f);
107
      mColor      = new Static3D(255,0,0);
108 823f63f7 Leszek Koltunski
      mRegionF    = new Static3D(RADIUS_F,RADIUS_F,RADIUS_F);
109
      mRegionV    = new Static4D(0,0,0,RADIUS_V);
110 1833b023 Leszek Koltunski
      mMove       = new Static3D(0,0,0);
111
      mScaleFactor= new Static3D(1,1,1);
112 386fd702 Leszek Koltunski
      mPinch      = new Static3D(0.5f,0.0f,0.0f);
113 5e23b17b Leszek Koltunski
      mPinch      = new Static3D(0.5f,0.0f,0.0f);
114 edc4a0e4 Leszek Koltunski
      mSwirl      = new Static1D(45.0f);
115 1833b023 Leszek Koltunski
116 16b22aab Leszek Koltunski
      Static3D center = new Static3D(0,0,0);
117
118 823f63f7 Leszek Koltunski
      Dynamic3D scale = new Dynamic3D(0,0.5f);
119
      scale.add(mScaleFactor);
120 37b324c4 Leszek Koltunski
121 b88ec561 Leszek Koltunski
      mMesh     = new MeshSphere(LEVEL);
122 698ad0a8 Leszek Koltunski
      mMesh.setStretch(SIZE,SIZE,SIZE);
123 687263cc Leszek Koltunski
      mTexture  = new DistortedTexture();
124 261fe5bd Leszek Koltunski
125 687263cc Leszek Koltunski
      mObjWidth = mObjHeight = mObjDepth = SIZE;
126 261fe5bd Leszek Koltunski
127 7589635e Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
128
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
129 427ab7bf Leszek Koltunski
      
130 37b324c4 Leszek Koltunski
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
131
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
132
133
      quatInt1.add(mQuat1);
134
      quatInt2.add(mQuat2);
135 833685d0 Leszek Koltunski
136 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
137 dae661e9 Leszek Koltunski
      mEffects.apply( new MatrixEffectMove(mMove) );
138 823f63f7 Leszek Koltunski
      mEffects.apply( new MatrixEffectScale(scale));
139 16b22aab Leszek Koltunski
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
140
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
141 392e16fd Leszek Koltunski
142 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
143 64558e4e Leszek Koltunski
      mScreen.setProjection(FOV, NEAR);
144 427ab7bf Leszek Koltunski
      }
145
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
   
148
    public void onDrawFrame(GL10 glUnused) 
149
      {
150 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
151 427ab7bf Leszek Koltunski
      }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
    
155
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
156
      {
157 1833b023 Leszek Koltunski
      mScrWidth = width;
158
      mScrHeight= height;
159
      mScreenMin= width<height ? width:height;
160
161
      adjustFactor();
162 64558e4e Leszek Koltunski
163 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
164 427ab7bf Leszek Koltunski
      }
165
166 9c3e749e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
167
168 9814e53c Leszek Koltunski
    void setLevel(int level)
169 9c3e749e Leszek Koltunski
      {
170 16b22aab Leszek Koltunski
      final float N=2.2f;
171 1833b023 Leszek Koltunski
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 16b22aab Leszek Koltunski
      mMove.set( 0, 0, -factor*mObjDepth );
188 1833b023 Leszek Koltunski
      mScaleFactor.set(factor,factor,factor);
189 9c3e749e Leszek Koltunski
      }
190
191 296d2e73 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
192
193 1585ba24 Leszek Koltunski
    void addNewPoint(float longitude, float latitude, EffectName name)
194 296d2e73 Leszek Koltunski
      {
195 823f63f7 Leszek Koltunski
      final float CORR = 10.0F;
196
      Static3D vector;
197 1585ba24 Leszek Koltunski
      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 e83350b4 Leszek Koltunski
      float x = (float)(sinLON*cosLAT)/2.0f;
205 0ec011a7 Leszek Koltunski
      float y = (float)        sinLAT /2.0f;
206 e83350b4 Leszek Koltunski
      float z = (float)(cosLON*cosLAT)/2.0f;
207 1585ba24 Leszek Koltunski
208 823f63f7 Leszek Koltunski
      Static3D center = new Static3D( (0.5f+x)*mObjWidth, (0.5f+y)*mObjHeight, (0.5f+z)*mObjDepth);
209 b88ec561 Leszek Koltunski
210 1585ba24 Leszek Koltunski
      switch(name)
211
        {
212 823f63f7 Leszek Koltunski
        case DISTORT          : vector = new Static3D( +x*mObjWidth/CORR, +y*mObjHeight/CORR, +z*mObjDepth/CORR);
213 edc4a0e4 Leszek Koltunski
                                effect = new VertexEffectDistort(   vector, center, mRegionV); break;
214 823f63f7 Leszek Koltunski
        case DEFORM           : vector = new Static3D( +x*mObjWidth/CORR, +y*mObjHeight/CORR, +z*mObjDepth/CORR);
215 edc4a0e4 Leszek Koltunski
                                effect = new VertexEffectDeform (   vector, center, mRegionV); break;
216
        case SINK             : effect = new VertexEffectSink   (mStrength, center, mRegionV); break;
217 0ec011a7 Leszek Koltunski
        case PINCH            : float lat = latitude>0 ? (float)(-Math.PI/2 + latitude) : (float)(Math.PI/2 + latitude);
218
                                float lon = longitude;
219
                                float latInDegrees = (float)(lat*180.0f/Math.PI);
220
                                float lonInDegrees = (float)(lon*180.0f/Math.PI);
221 bcbd5b45 Leszek Koltunski
                                mPinch.set1(latInDegrees);
222
                                mPinch.set2(lonInDegrees);
223 5e23b17b Leszek Koltunski
                                effect = new VertexEffectPinch  (mPinch   , center, mRegionV); break;
224
        case SWIRL            : effect = new VertexEffectSwirl  (mSwirl   , center, mRegionV); break;      // SWIRL & WAVE are not really fully
225
        case WAVE             : Static5D wave = new Static5D( RADIUS_V/2, RADIUS_V/2, 0, 90-latitude, 90); // 3D effects. They will not look
226
                                effect = new VertexEffectWave   (wave     , center, mRegionV); break;      // good everywhere on the sphere.
227 edc4a0e4 Leszek Koltunski
228 b88ec561 Leszek Koltunski
        case ALPHA            : effect = new FragmentEffectAlpha     (mStrength,        center, mRegionF, false); break;
229
        case SMOOTH_ALPHA     : effect = new FragmentEffectAlpha     (mStrength,        center, mRegionF, true ); break;
230
        case CHROMA           : effect = new FragmentEffectChroma    (mStrength, mColor,center, mRegionF, false); break;
231
        case SMOOTH_CHROMA    : effect = new FragmentEffectChroma    (mStrength, mColor,center, mRegionF, true ); break;
232
        case BRIGHTNESS       : effect = new FragmentEffectBrightness(mStrength,        center, mRegionF, false); break;
233
        case SMOOTH_BRIGHTNESS: effect = new FragmentEffectBrightness(mStrength,        center, mRegionF, true ); break;
234
        case SATURATION       : effect = new FragmentEffectSaturation(mStrength,        center, mRegionF, false); break;
235
        case SMOOTH_SATURATION: effect = new FragmentEffectSaturation(mStrength,        center, mRegionF, true ); break;
236
        case CONTRAST         : effect = new FragmentEffectContrast  (mStrength,        center, mRegionF, false); break;
237
        case SMOOTH_CONTRAST  : effect = new FragmentEffectContrast  (mStrength,        center, mRegionF, true ); break;
238
239
        default               : android.util.Log.e("EarthRenderer", "unexpected effect: "+name.toString() );
240 1585ba24 Leszek Koltunski
        }
241
242
      if( effect!=null )
243
        {
244
        mEffects.apply(effect);
245
        }
246 296d2e73 Leszek Koltunski
      }
247
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249
250
    void removeAll()
251
      {
252
      mEffects.abortByType(EffectType.VERTEX);
253
      mEffects.abortByType(EffectType.FRAGMENT);
254
      }
255
256 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
257
    
258
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
259
      {
260 9814e53c Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.world);
261
      Bitmap bitmap;
262
263
      try
264
        {
265
        bitmap = BitmapFactory.decodeStream(is);
266
        }
267
      finally
268
        {
269
        try
270
          {
271
          is.close();
272
          }
273
        catch(IOException e) { }
274
        }
275 64558e4e Leszek Koltunski
276 9814e53c Leszek Koltunski
      mTexture.setTexture(bitmap);
277 fe59d375 Leszek Koltunski
278
      mScreen.detachAll();
279
      mScreen.attach(mTexture,mEffects,mMesh);
280
281 b88ec561 Leszek Koltunski
      Effect.enableEffects(EffectType.FRAGMENT);
282
      Effect.enableEffects(EffectType.VERTEX);
283
284 e3900503 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX  , MAX_EFF);
285
      DistortedLibrary.setMax(EffectType.FRAGMENT, MAX_EFF);
286 676c14da Leszek Koltunski
287 427ab7bf Leszek Koltunski
      try
288
        {
289 e3900503 Leszek Koltunski
        DistortedLibrary.onCreate(mView.getContext());
290 427ab7bf Leszek Koltunski
        }
291
      catch(Exception ex)
292
        {
293 64558e4e Leszek Koltunski
        android.util.Log.e("Earth", ex.getMessage() );
294 427ab7bf Leszek Koltunski
        }
295
      }
296
}