Project

General

Profile

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

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

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