Project

General

Profile

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

examples / src / main / java / org / distorted / examples / earth / EarthRenderer.java @ 736a0b00

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 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 061449ed Leszek Koltunski
class EarthRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
66 427ab7bf Leszek Koltunski
{
67 355c0a07 Leszek Koltunski
    private static final float RADIUS_V= 1.0f/ 8;
68
    private static final float RADIUS_F= 1.0f/15;
69 1c1df934 Leszek Koltunski
    private static final int   LEVEL   =      40;
70 823f63f7 Leszek Koltunski
    private static final float FOV     =   30.0f;
71
    private static final float NEAR    =    0.1f;
72
    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 355c0a07 Leszek Koltunski
    private float mObjWidth, mObjHeight, mObjDepth;
80 16b22aab Leszek Koltunski
    private Static3D mMove, mScaleFactor;
81 261fe5bd Leszek Koltunski
82 1833b023 Leszek Koltunski
    private int mScrWidth, mScrHeight;
83
    private float mLevel;
84 9c3e749e Leszek Koltunski
85 1585ba24 Leszek Koltunski
    private Static3D mColor;
86 b88ec561 Leszek Koltunski
    private Static3D mRegionF;
87 823f63f7 Leszek Koltunski
    private Static4D mRegionV;
88 1585ba24 Leszek Koltunski
    private Static1D mStrength;
89 5e23b17b Leszek Koltunski
    private Static3D mPinch;
90 edc4a0e4 Leszek Koltunski
    private Static1D mSwirl;
91 1585ba24 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 261fe5bd Leszek Koltunski
101 1833b023 Leszek Koltunski
      mLevel = SCALE;
102
103
      mStrength   = new Static1D(0.5f);
104
      mColor      = new Static3D(255,0,0);
105 823f63f7 Leszek Koltunski
      mRegionF    = new Static3D(RADIUS_F,RADIUS_F,RADIUS_F);
106
      mRegionV    = new Static4D(0,0,0,RADIUS_V);
107 1833b023 Leszek Koltunski
      mMove       = new Static3D(0,0,0);
108
      mScaleFactor= new Static3D(1,1,1);
109 386fd702 Leszek Koltunski
      mPinch      = new Static3D(0.5f,0.0f,0.0f);
110 edc4a0e4 Leszek Koltunski
      mSwirl      = new Static1D(45.0f);
111 1833b023 Leszek Koltunski
112 16b22aab Leszek Koltunski
      Static3D center = new Static3D(0,0,0);
113
114 823f63f7 Leszek Koltunski
      Dynamic3D scale = new Dynamic3D(0,0.5f);
115
      scale.add(mScaleFactor);
116 37b324c4 Leszek Koltunski
117 b88ec561 Leszek Koltunski
      mMesh     = new MeshSphere(LEVEL);
118 687263cc Leszek Koltunski
      mTexture  = new DistortedTexture();
119 261fe5bd Leszek Koltunski
120 355c0a07 Leszek Koltunski
      mObjWidth = mObjHeight = mObjDepth = 1.0f;
121 261fe5bd Leszek Koltunski
122 7589635e Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
123
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
124 427ab7bf Leszek Koltunski
      
125 37b324c4 Leszek Koltunski
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
126
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
127
128
      quatInt1.add(mQuat1);
129
      quatInt2.add(mQuat2);
130 833685d0 Leszek Koltunski
131 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
132 16b22aab Leszek Koltunski
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
133 ba9ae2c8 Leszek Koltunski
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
134
      mEffects.apply( new MatrixEffectScale(scale));
135
      mEffects.apply( new MatrixEffectMove(mMove) );
136 392e16fd Leszek Koltunski
137 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
138 64558e4e Leszek Koltunski
      mScreen.setProjection(FOV, NEAR);
139 427ab7bf Leszek Koltunski
      }
140
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
   
143
    public void onDrawFrame(GL10 glUnused) 
144
      {
145 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
146 427ab7bf Leszek Koltunski
      }
147
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
    
150
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
151
      {
152 1833b023 Leszek Koltunski
      mScrWidth = width;
153
      mScrHeight= height;
154 355c0a07 Leszek Koltunski
      mScreenMin= Math.min(width, height);
155 1833b023 Leszek Koltunski
156
      adjustFactor();
157 64558e4e Leszek Koltunski
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 ba9ae2c8 Leszek Koltunski
      final float N=2.0f;
166 1833b023 Leszek Koltunski
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 16b22aab Leszek Koltunski
      mMove.set( 0, 0, -factor*mObjDepth );
183 1833b023 Leszek Koltunski
      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 823f63f7 Leszek Koltunski
      final float CORR = 10.0F;
191
      Static3D vector;
192 1585ba24 Leszek Koltunski
      Effect effect =null;
193
194
      double sinLON = Math.sin(longitude);
195
      double cosLON = Math.cos(longitude);
196
      double sinLAT = Math.sin(latitude);
197
      double cosLAT = Math.cos(latitude);
198
199 e83350b4 Leszek Koltunski
      float x = (float)(sinLON*cosLAT)/2.0f;
200 0ec011a7 Leszek Koltunski
      float y = (float)        sinLAT /2.0f;
201 e83350b4 Leszek Koltunski
      float z = (float)(cosLON*cosLAT)/2.0f;
202 1585ba24 Leszek Koltunski
203 e50f49b6 Leszek Koltunski
      Static3D center = new Static3D( (x)*mObjWidth, (y)*mObjHeight, (z)*mObjDepth);
204 b88ec561 Leszek Koltunski
205 1585ba24 Leszek Koltunski
      switch(name)
206
        {
207 823f63f7 Leszek Koltunski
        case DISTORT          : vector = new Static3D( +x*mObjWidth/CORR, +y*mObjHeight/CORR, +z*mObjDepth/CORR);
208 edc4a0e4 Leszek Koltunski
                                effect = new VertexEffectDistort(   vector, center, mRegionV); break;
209 823f63f7 Leszek Koltunski
        case DEFORM           : vector = new Static3D( +x*mObjWidth/CORR, +y*mObjHeight/CORR, +z*mObjDepth/CORR);
210 513b2e9c Leszek Koltunski
                                Static1D radius = new Static1D(mObjWidth/2);
211
                                effect = new VertexEffectDeform (   vector, radius, center, mRegionV); break;
212 edc4a0e4 Leszek Koltunski
        case SINK             : effect = new VertexEffectSink   (mStrength, center, mRegionV); break;
213 0ec011a7 Leszek Koltunski
        case PINCH            : float lat = latitude>0 ? (float)(-Math.PI/2 + latitude) : (float)(Math.PI/2 + latitude);
214
                                float lon = longitude;
215
                                float latInDegrees = (float)(lat*180.0f/Math.PI);
216
                                float lonInDegrees = (float)(lon*180.0f/Math.PI);
217 bcbd5b45 Leszek Koltunski
                                mPinch.set1(latInDegrees);
218
                                mPinch.set2(lonInDegrees);
219 5e23b17b Leszek Koltunski
                                effect = new VertexEffectPinch  (mPinch   , center, mRegionV); break;
220
        case SWIRL            : effect = new VertexEffectSwirl  (mSwirl   , center, mRegionV); break;      // SWIRL & WAVE are not really fully
221
        case WAVE             : Static5D wave = new Static5D( RADIUS_V/2, RADIUS_V/2, 0, 90-latitude, 90); // 3D effects. They will not look
222
                                effect = new VertexEffectWave   (wave     , center, mRegionV); break;      // good everywhere on the sphere.
223 edc4a0e4 Leszek Koltunski
224 b88ec561 Leszek Koltunski
        case ALPHA            : effect = new FragmentEffectAlpha     (mStrength,        center, mRegionF, false); break;
225
        case SMOOTH_ALPHA     : effect = new FragmentEffectAlpha     (mStrength,        center, mRegionF, true ); break;
226
        case CHROMA           : effect = new FragmentEffectChroma    (mStrength, mColor,center, mRegionF, false); break;
227
        case SMOOTH_CHROMA    : effect = new FragmentEffectChroma    (mStrength, mColor,center, mRegionF, true ); break;
228
        case BRIGHTNESS       : effect = new FragmentEffectBrightness(mStrength,        center, mRegionF, false); break;
229
        case SMOOTH_BRIGHTNESS: effect = new FragmentEffectBrightness(mStrength,        center, mRegionF, true ); break;
230
        case SATURATION       : effect = new FragmentEffectSaturation(mStrength,        center, mRegionF, false); break;
231
        case SMOOTH_SATURATION: effect = new FragmentEffectSaturation(mStrength,        center, mRegionF, true ); break;
232
        case CONTRAST         : effect = new FragmentEffectContrast  (mStrength,        center, mRegionF, false); break;
233
        case SMOOTH_CONTRAST  : effect = new FragmentEffectContrast  (mStrength,        center, mRegionF, true ); break;
234
235
        default               : android.util.Log.e("EarthRenderer", "unexpected effect: "+name.toString() );
236 1585ba24 Leszek Koltunski
        }
237
238
      if( effect!=null )
239
        {
240
        mEffects.apply(effect);
241
        }
242 296d2e73 Leszek Koltunski
      }
243
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245
246
    void removeAll()
247
      {
248
      mEffects.abortByType(EffectType.VERTEX);
249
      mEffects.abortByType(EffectType.FRAGMENT);
250
      }
251
252 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
253
    
254
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
255
      {
256 9814e53c Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.world);
257
      Bitmap bitmap;
258
259
      try
260
        {
261
        bitmap = BitmapFactory.decodeStream(is);
262
        }
263
      finally
264
        {
265
        try
266
          {
267
          is.close();
268
          }
269 6979a0e0 Leszek Koltunski
        catch(IOException ignored) { }
270 9814e53c Leszek Koltunski
        }
271 64558e4e Leszek Koltunski
272 9814e53c Leszek Koltunski
      mTexture.setTexture(bitmap);
273 fe59d375 Leszek Koltunski
274
      mScreen.detachAll();
275
      mScreen.attach(mTexture,mEffects,mMesh);
276
277 b88ec561 Leszek Koltunski
      Effect.enableEffects(EffectType.FRAGMENT);
278
      Effect.enableEffects(EffectType.VERTEX);
279
280 736a0b00 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.FRAGMENT, 20);
281
282 7c72e798 Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
283 061449ed Leszek Koltunski
      }
284
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286
287
    public void distortedException(Exception ex)
288
      {
289
      android.util.Log.e("Earth", ex.getMessage() );
290 427ab7bf Leszek Koltunski
      }
291
}