Project

General

Profile

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

examples / src / main / java / org / distorted / examples / earth / EarthRenderer.java @ 1833b023

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