Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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

    
20
package org.distorted.examples.earth;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25

    
26
import org.distorted.examples.R;
27
import org.distorted.library.effect.Effect;
28
import org.distorted.library.effect.EffectName;
29
import org.distorted.library.effect.EffectType;
30
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
import org.distorted.library.effect.MatrixEffectMove;
36
import org.distorted.library.effect.MatrixEffectQuaternion;
37
import org.distorted.library.effect.MatrixEffectScale;
38
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
import org.distorted.library.main.Distorted;
45
import org.distorted.library.main.DistortedEffects;
46
import org.distorted.library.main.DistortedScreen;
47
import org.distorted.library.main.DistortedTexture;
48
import org.distorted.library.mesh.MeshBase;
49
import org.distorted.library.mesh.MeshSphere;
50
import org.distorted.library.type.DynamicQuat;
51
import org.distorted.library.type.Static1D;
52
import org.distorted.library.type.Static3D;
53
import org.distorted.library.type.Static4D;
54

    
55
import java.io.IOException;
56
import java.io.InputStream;
57

    
58
import javax.microedition.khronos.egl.EGLConfig;
59
import javax.microedition.khronos.opengles.GL10;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
class EarthRenderer implements GLSurfaceView.Renderer
64
{
65
    private static final float D      = (float)Math.sqrt(2.0)/2.0f;
66

    
67
    private static final int   SIZE   =   500;
68
    private static final int   RADIUS =   (int)(SIZE*D);
69
    private static final int   LEVEL  =    32;
70
    private static final float FOV    = 30.0f;
71
    private static final float NEAR   =  0.1f;
72

    
73
    private GLSurfaceView mView;
74
    private DistortedTexture mTexture;
75
    private DistortedEffects mEffects;
76
    private MeshBase mMesh;
77
    private DistortedScreen mScreen;
78
    private int mObjWidth, mObjHeight, mObjDepth;
79
    private Static3D mMove, mScale, mCenter;
80

    
81
    Static4D mQuat1, mQuat2;
82
    int mScreenMin;
83

    
84
    private Static3D mColor;
85
    private Static3D mRegionF;
86
    private Static1D mStrength;
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
    EarthRenderer(GLSurfaceView v)
91
      {
92
      mView = v;
93

    
94
      mStrength = new Static1D(0.5f);
95
      mColor    = new Static3D(255,0,0);
96
      mRegionF  = new Static3D(RADIUS,RADIUS,RADIUS);
97
      mMove     = new Static3D(0,0,0);
98
      mScale    = new Static3D(1,1,1);
99
      mCenter   = new Static3D(0,0,0);
100

    
101
      mMesh     = new MeshSphere(LEVEL);
102
      mTexture  = new DistortedTexture(SIZE,SIZE);
103

    
104
      mObjWidth = mTexture.getWidth();
105
      mObjHeight= mTexture.getHeight();
106
      mObjDepth = mTexture.getDepth(mMesh);
107

    
108
      mQuat1 = new Static4D(0,0,0,1);  // unity
109
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
110
      
111
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
112
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
113

    
114
      quatInt1.add(mQuat1);
115
      quatInt2.add(mQuat2);
116

    
117
      mEffects = new DistortedEffects();
118
      mEffects.apply( new MatrixEffectMove(mMove) );
119
      mEffects.apply( new MatrixEffectScale(mScale));
120
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, mCenter) );
121
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, mCenter) );
122

    
123

    
124
      Static3D center = new Static3D(0.5f*mObjWidth,0.5f*mObjHeight,0.5f*mObjDepth);
125
      mEffects.apply( new FragmentEffectChroma(mStrength, mColor,center, mRegionF, false) );
126

    
127
      mScreen = new DistortedScreen();
128
      mScreen.setProjection(FOV, NEAR);
129
      }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
   
133
    public void onDrawFrame(GL10 glUnused) 
134
      {
135
      mScreen.render( System.currentTimeMillis() );
136
      }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
    
140
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
141
      {
142
      final float SCALE = 0.75f;
143

    
144
      mScreenMin = width<height ? width:height;
145
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (SCALE*height)/mObjHeight :  (SCALE*width)/mObjWidth;
146
      mMove.set( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0);
147
      mScale.set(factor,factor,factor);
148
      mCenter.set( (float)mObjWidth/2, (float)mObjHeight/2, (float)mObjDepth/2 );
149
      mScreen.resize(width, height);
150
      }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
    void setLevel(int level)
155
      {
156
      float inflateLevel = (level-50)/50.0f;
157
      mMesh.setInflate(inflateLevel);
158
      }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
    void addNewPoint(float longitude, float latitude, EffectName name)
163
      {
164
      Effect effect =null;
165

    
166
      double sinLON = Math.sin(longitude);
167
      double cosLON = Math.cos(longitude);
168
      double sinLAT = Math.sin(latitude);
169
      double cosLAT = Math.cos(latitude);
170

    
171
      float x = 0.7071f*(float)(sinLON*cosLAT);
172
      float y = 0.7071f*(float)(cosLON*cosLAT);
173
      float z = 0.7071f*(float)        sinLAT ;
174

    
175
      Static3D center = new Static3D(x*mObjWidth,y*mObjHeight,z*mObjDepth);
176

    
177
      android.util.Log.e("earth", "center "+(x*mObjWidth)+" "+(y*mObjHeight)+" "+(z*mObjDepth));
178
      android.util.Log.e("earth", "longitude: "+longitude+" latitude:"+latitude);
179

    
180
      switch(name)
181
        {
182
        /*
183
        case DISTORT          : effect = new VertexEffectDistort(mDyn3, center, mRegionV); break;
184
        case DEFORM           : effect = new VertexEffectDeform (mDyn3, center, mRegionV); break;
185
        case SINK             : effect = new VertexEffectSink   (mDyn1, center, mRegionV); break;
186
        case PINCH            : effect = new VertexEffectPinch  (mDyn2, center, mRegionV); break;
187
        case SWIRL            : effect = new VertexEffectSwirl  (mDyn1, center, mRegionV); break;
188
        case WAVE             : effect = new VertexEffectWave   (mDyn5, center, mRegionV); break;
189
        */
190
        case ALPHA            : effect = new FragmentEffectAlpha     (mStrength,        center, mRegionF, false); break;
191
        case SMOOTH_ALPHA     : effect = new FragmentEffectAlpha     (mStrength,        center, mRegionF, true ); break;
192
        case CHROMA           : effect = new FragmentEffectChroma    (mStrength, mColor,center, mRegionF, false); break;
193
        case SMOOTH_CHROMA    : effect = new FragmentEffectChroma    (mStrength, mColor,center, mRegionF, true ); break;
194
        case BRIGHTNESS       : effect = new FragmentEffectBrightness(mStrength,        center, mRegionF, false); break;
195
        case SMOOTH_BRIGHTNESS: effect = new FragmentEffectBrightness(mStrength,        center, mRegionF, true ); break;
196
        case SATURATION       : effect = new FragmentEffectSaturation(mStrength,        center, mRegionF, false); break;
197
        case SMOOTH_SATURATION: effect = new FragmentEffectSaturation(mStrength,        center, mRegionF, true ); break;
198
        case CONTRAST         : effect = new FragmentEffectContrast  (mStrength,        center, mRegionF, false); break;
199
        case SMOOTH_CONTRAST  : effect = new FragmentEffectContrast  (mStrength,        center, mRegionF, true ); break;
200

    
201
        default               : android.util.Log.e("EarthRenderer", "unexpected effect: "+name.toString() );
202
        }
203

    
204
      if( effect!=null )
205
        {
206
        mEffects.apply(effect);
207
        }
208
      }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
    void removeAll()
213
      {
214
      mEffects.abortByType(EffectType.VERTEX);
215
      mEffects.abortByType(EffectType.FRAGMENT);
216
      }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
    
220
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
221
      {
222
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.world);
223
      Bitmap bitmap;
224

    
225
      try
226
        {
227
        bitmap = BitmapFactory.decodeStream(is);
228
        }
229
      finally
230
        {
231
        try
232
          {
233
          is.close();
234
          }
235
        catch(IOException e) { }
236
        }
237

    
238
      mTexture.setTexture(bitmap);
239

    
240
      mScreen.detachAll();
241
      mScreen.attach(mTexture,mEffects,mMesh);
242

    
243
      Effect.enableEffects(EffectType.FRAGMENT);
244
      Effect.enableEffects(EffectType.VERTEX);
245

    
246
      try
247
        {
248
        Distorted.onCreate(mView.getContext());
249
        }
250
      catch(Exception ex)
251
        {
252
        android.util.Log.e("Earth", ex.getMessage() );
253
        }
254
      }
255
}
(2-2/4)