Project

General

Profile

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

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

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