Project

General

Profile

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

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

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 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26 9814e53c Leszek Koltunski
import android.graphics.Bitmap;
27
import android.graphics.BitmapFactory;
28 64558e4e Leszek Koltunski
import android.opengl.GLSurfaceView;
29 427ab7bf Leszek Koltunski
30 9814e53c Leszek Koltunski
import org.distorted.examples.R;
31 1585ba24 Leszek Koltunski
import org.distorted.library.effect.Effect;
32 296d2e73 Leszek Koltunski
import org.distorted.library.effect.EffectName;
33
import org.distorted.library.effect.EffectType;
34 1585ba24 Leszek Koltunski
import org.distorted.library.effect.FragmentEffectAlpha;
35
import org.distorted.library.effect.FragmentEffectBrightness;
36
import org.distorted.library.effect.FragmentEffectChroma;
37
import org.distorted.library.effect.FragmentEffectContrast;
38
import org.distorted.library.effect.FragmentEffectSaturation;
39 37b324c4 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
40
import org.distorted.library.effect.MatrixEffectQuaternion;
41
import org.distorted.library.effect.MatrixEffectScale;
42 1585ba24 Leszek Koltunski
import org.distorted.library.effect.VertexEffectDeform;
43
import org.distorted.library.effect.VertexEffectDistort;
44
import org.distorted.library.effect.VertexEffectPinch;
45
import org.distorted.library.effect.VertexEffectSink;
46
import org.distorted.library.effect.VertexEffectSwirl;
47
import org.distorted.library.effect.VertexEffectWave;
48 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
49 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
50
import org.distorted.library.main.DistortedScreen;
51
import org.distorted.library.main.DistortedTexture;
52 57d7fdba Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
53 296d2e73 Leszek Koltunski
import org.distorted.library.mesh.MeshSphere;
54 1833b023 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
55 7589635e Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
56 1585ba24 Leszek Koltunski
import org.distorted.library.type.Static1D;
57 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
58 64558e4e Leszek Koltunski
import org.distorted.library.type.Static4D;
59 edc4a0e4 Leszek Koltunski
import org.distorted.library.type.Static5D;
60 427ab7bf Leszek Koltunski
61 9814e53c Leszek Koltunski
import java.io.IOException;
62
import java.io.InputStream;
63
64 64558e4e Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
65
import javax.microedition.khronos.opengles.GL10;
66 427ab7bf Leszek Koltunski
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69 dc10a48d Leszek Koltunski
class EarthRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
70 427ab7bf Leszek Koltunski
{
71 355c0a07 Leszek Koltunski
    private static final float RADIUS_V= 1.0f/ 8;
72
    private static final float RADIUS_F= 1.0f/15;
73 1c1df934 Leszek Koltunski
    private static final int   LEVEL   =      40;
74 823f63f7 Leszek Koltunski
    private static final float FOV     =   30.0f;
75
    private static final float NEAR    =    0.1f;
76
    private static final float SCALE   =   1.10f;
77 64558e4e Leszek Koltunski
78 dc10a48d Leszek Koltunski
    private final GLSurfaceView mView;
79
    private final Resources mResources;
80
    private final DistortedTexture mTexture;
81
    private final DistortedEffects mEffects;
82
    private final MeshBase mMesh;
83
    private final DistortedScreen mScreen;
84
    private final Static1D mStrength, mSwirl;
85
    private final Static3D mMove, mScaleFactor, mColor,mRegionF,mPinch;
86
    private final Static4D mRegionV;
87
    private final float mObjWidth, mObjHeight, mObjDepth;
88 261fe5bd Leszek Koltunski
89 1833b023 Leszek Koltunski
    private int mScrWidth, mScrHeight;
90
    private float mLevel;
91 9c3e749e Leszek Koltunski
92 1833b023 Leszek Koltunski
    Static4D mQuat1, mQuat2;
93
    int mScreenMin;
94
95 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
96
97 64558e4e Leszek Koltunski
    EarthRenderer(GLSurfaceView v)
98 427ab7bf Leszek Koltunski
      {
99
      mView = v;
100 dc10a48d Leszek Koltunski
      mResources = v.getResources();
101 261fe5bd Leszek Koltunski
102 1833b023 Leszek Koltunski
      mLevel = SCALE;
103
104
      mStrength   = new Static1D(0.5f);
105
      mColor      = new Static3D(255,0,0);
106 823f63f7 Leszek Koltunski
      mRegionF    = new Static3D(RADIUS_F,RADIUS_F,RADIUS_F);
107
      mRegionV    = new Static4D(0,0,0,RADIUS_V);
108 1833b023 Leszek Koltunski
      mMove       = new Static3D(0,0,0);
109
      mScaleFactor= new Static3D(1,1,1);
110 386fd702 Leszek Koltunski
      mPinch      = new Static3D(0.5f,0.0f,0.0f);
111 edc4a0e4 Leszek Koltunski
      mSwirl      = new Static1D(45.0f);
112 1833b023 Leszek Koltunski
113 16b22aab Leszek Koltunski
      Static3D center = new Static3D(0,0,0);
114
115 823f63f7 Leszek Koltunski
      Dynamic3D scale = new Dynamic3D(0,0.5f);
116
      scale.add(mScaleFactor);
117 37b324c4 Leszek Koltunski
118 b88ec561 Leszek Koltunski
      mMesh     = new MeshSphere(LEVEL);
119 687263cc Leszek Koltunski
      mTexture  = new DistortedTexture();
120 261fe5bd Leszek Koltunski
121 355c0a07 Leszek Koltunski
      mObjWidth = mObjHeight = mObjDepth = 1.0f;
122 261fe5bd Leszek Koltunski
123 7589635e Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
124
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
125 427ab7bf Leszek Koltunski
      
126 37b324c4 Leszek Koltunski
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
127
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
128
129
      quatInt1.add(mQuat1);
130
      quatInt2.add(mQuat2);
131 833685d0 Leszek Koltunski
132 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
133 16b22aab Leszek Koltunski
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
134 ba9ae2c8 Leszek Koltunski
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
135
      mEffects.apply( new MatrixEffectScale(scale));
136
      mEffects.apply( new MatrixEffectMove(mMove) );
137 392e16fd Leszek Koltunski
138 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
139 64558e4e Leszek Koltunski
      mScreen.setProjection(FOV, NEAR);
140 427ab7bf Leszek Koltunski
      }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
   
144
    public void onDrawFrame(GL10 glUnused) 
145
      {
146 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
147 427ab7bf Leszek Koltunski
      }
148
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
    
151
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
152
      {
153 1833b023 Leszek Koltunski
      mScrWidth = width;
154
      mScrHeight= height;
155 355c0a07 Leszek Koltunski
      mScreenMin= Math.min(width, height);
156 1833b023 Leszek Koltunski
157
      adjustFactor();
158 64558e4e Leszek Koltunski
159 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
160 427ab7bf Leszek Koltunski
      }
161
162 9c3e749e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
163
164 9814e53c Leszek Koltunski
    void setLevel(int level)
165 9c3e749e Leszek Koltunski
      {
166 ba9ae2c8 Leszek Koltunski
      final float N=2.0f;
167 1833b023 Leszek Koltunski
168
      // MAP: 0--> SCALE/N   50-->SCALE   100->SCALE*N
169
      mLevel = SCALE* (1.0f+  (level>=50 ? N-1 : (N-1)/N) * (level-50)/50.0f);
170
171
      adjustFactor();
172
      }
173
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
176
    private void adjustFactor()
177
      {
178
      float xW = (float)mScrWidth /mObjWidth;
179
      float xH = (float)mScrHeight/mObjHeight;
180
181
      float factor = xW>xH ? mLevel*xH : mLevel*xW;
182
183 16b22aab Leszek Koltunski
      mMove.set( 0, 0, -factor*mObjDepth );
184 1833b023 Leszek Koltunski
      mScaleFactor.set(factor,factor,factor);
185 9c3e749e Leszek Koltunski
      }
186
187 296d2e73 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
188
189 1585ba24 Leszek Koltunski
    void addNewPoint(float longitude, float latitude, EffectName name)
190 296d2e73 Leszek Koltunski
      {
191 823f63f7 Leszek Koltunski
      final float CORR = 10.0F;
192
      Static3D vector;
193 1585ba24 Leszek Koltunski
      Effect effect =null;
194
195
      double sinLON = Math.sin(longitude);
196
      double cosLON = Math.cos(longitude);
197
      double sinLAT = Math.sin(latitude);
198
      double cosLAT = Math.cos(latitude);
199
200 e83350b4 Leszek Koltunski
      float x = (float)(sinLON*cosLAT)/2.0f;
201 0ec011a7 Leszek Koltunski
      float y = (float)        sinLAT /2.0f;
202 e83350b4 Leszek Koltunski
      float z = (float)(cosLON*cosLAT)/2.0f;
203 1585ba24 Leszek Koltunski
204 e50f49b6 Leszek Koltunski
      Static3D center = new Static3D( (x)*mObjWidth, (y)*mObjHeight, (z)*mObjDepth);
205 b88ec561 Leszek Koltunski
206 1585ba24 Leszek Koltunski
      switch(name)
207
        {
208 dc10a48d Leszek Koltunski
        case DISTORT          : vector = new Static3D( x*mObjWidth/CORR, y*mObjHeight/CORR, z*mObjDepth/CORR);
209 edc4a0e4 Leszek Koltunski
                                effect = new VertexEffectDistort(   vector, center, mRegionV); break;
210 dc10a48d Leszek Koltunski
        case DEFORM           : vector = new Static3D( x*mObjWidth/CORR, y*mObjHeight/CORR, z*mObjDepth/CORR);
211 513b2e9c Leszek Koltunski
                                Static1D radius = new Static1D(mObjWidth/2);
212
                                effect = new VertexEffectDeform (   vector, radius, center, mRegionV); break;
213 edc4a0e4 Leszek Koltunski
        case SINK             : effect = new VertexEffectSink   (mStrength, center, mRegionV); break;
214 0ec011a7 Leszek Koltunski
        case PINCH            : float lat = latitude>0 ? (float)(-Math.PI/2 + latitude) : (float)(Math.PI/2 + latitude);
215
                                float lon = longitude;
216
                                float latInDegrees = (float)(lat*180.0f/Math.PI);
217
                                float lonInDegrees = (float)(lon*180.0f/Math.PI);
218 bcbd5b45 Leszek Koltunski
                                mPinch.set1(latInDegrees);
219
                                mPinch.set2(lonInDegrees);
220 5e23b17b Leszek Koltunski
                                effect = new VertexEffectPinch  (mPinch   , center, mRegionV); break;
221
        case SWIRL            : effect = new VertexEffectSwirl  (mSwirl   , center, mRegionV); break;      // SWIRL & WAVE are not really fully
222
        case WAVE             : Static5D wave = new Static5D( RADIUS_V/2, RADIUS_V/2, 0, 90-latitude, 90); // 3D effects. They will not look
223
                                effect = new VertexEffectWave   (wave     , center, mRegionV); break;      // good everywhere on the sphere.
224 edc4a0e4 Leszek Koltunski
225 b88ec561 Leszek Koltunski
        case ALPHA            : effect = new FragmentEffectAlpha     (mStrength,        center, mRegionF, false); break;
226
        case SMOOTH_ALPHA     : effect = new FragmentEffectAlpha     (mStrength,        center, mRegionF, true ); break;
227
        case CHROMA           : effect = new FragmentEffectChroma    (mStrength, mColor,center, mRegionF, false); break;
228
        case SMOOTH_CHROMA    : effect = new FragmentEffectChroma    (mStrength, mColor,center, mRegionF, true ); break;
229
        case BRIGHTNESS       : effect = new FragmentEffectBrightness(mStrength,        center, mRegionF, false); break;
230
        case SMOOTH_BRIGHTNESS: effect = new FragmentEffectBrightness(mStrength,        center, mRegionF, true ); break;
231
        case SATURATION       : effect = new FragmentEffectSaturation(mStrength,        center, mRegionF, false); break;
232
        case SMOOTH_SATURATION: effect = new FragmentEffectSaturation(mStrength,        center, mRegionF, true ); break;
233
        case CONTRAST         : effect = new FragmentEffectContrast  (mStrength,        center, mRegionF, false); break;
234
        case SMOOTH_CONTRAST  : effect = new FragmentEffectContrast  (mStrength,        center, mRegionF, true ); break;
235
236 dc10a48d Leszek Koltunski
        default               : android.util.Log.e("EarthRenderer", "unexpected effect: "+name );
237 1585ba24 Leszek Koltunski
        }
238
239
      if( effect!=null )
240
        {
241
        mEffects.apply(effect);
242
        }
243 296d2e73 Leszek Koltunski
      }
244
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246
247
    void removeAll()
248
      {
249
      mEffects.abortByType(EffectType.VERTEX);
250
      mEffects.abortByType(EffectType.FRAGMENT);
251
      }
252
253 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
254
    
255
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
256
      {
257 9814e53c Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.world);
258
      Bitmap bitmap;
259
260
      try
261
        {
262
        bitmap = BitmapFactory.decodeStream(is);
263
        }
264
      finally
265
        {
266
        try
267
          {
268
          is.close();
269
          }
270 6979a0e0 Leszek Koltunski
        catch(IOException ignored) { }
271 9814e53c Leszek Koltunski
        }
272 64558e4e Leszek Koltunski
273 9814e53c Leszek Koltunski
      mTexture.setTexture(bitmap);
274 fe59d375 Leszek Koltunski
275
      mScreen.detachAll();
276
      mScreen.attach(mTexture,mEffects,mMesh);
277
278 b88ec561 Leszek Koltunski
      Effect.enableEffects(EffectType.FRAGMENT);
279
      Effect.enableEffects(EffectType.VERTEX);
280 736a0b00 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.FRAGMENT, 20);
281 dc10a48d Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(this);
282 061449ed Leszek Koltunski
      }
283
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285
286
    public void distortedException(Exception ex)
287
      {
288
      android.util.Log.e("Earth", ex.getMessage() );
289 427ab7bf Leszek Koltunski
      }
290 dc10a48d Leszek Koltunski
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292
293
    public int openGlVersion()
294
      {
295
      Context context = mView.getContext();
296
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
297
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
298
      int glESversion = configurationInfo.reqGlEsVersion;
299
      int major = glESversion >> 16;
300
      int minor = glESversion & 0xff;
301
302
      return 100*major + 10*minor;
303
      }
304
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306
307
    public InputStream localFile(int fileID)
308
      {
309
      return mResources.openRawResource(fileID);
310
      }
311
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313
314
    public void logMessage(String message)
315
      {
316
      android.util.Log.e("Earth", message );
317
      }
318 427ab7bf Leszek Koltunski
}