Project

General

Profile

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

examples / src / main / java / org / distorted / examples / earth / EarthRenderer.java @ 296d2e73

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.EffectName;
28
import org.distorted.library.effect.EffectType;
29
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.effect.MatrixEffectQuaternion;
31
import org.distorted.library.effect.MatrixEffectScale;
32
import org.distorted.library.main.Distorted;
33
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.mesh.MeshBase;
37
import org.distorted.library.mesh.MeshSphere;
38
import org.distorted.library.type.DynamicQuat;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41

    
42
import java.io.IOException;
43
import java.io.InputStream;
44

    
45
import javax.microedition.khronos.egl.EGLConfig;
46
import javax.microedition.khronos.opengles.GL10;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
class EarthRenderer implements GLSurfaceView.Renderer
51
{
52
    private static final int   SIZE =   100;
53
    private static final int   LEVEL=    32;
54
    private static final float FOV  = 30.0f;
55
    private static final float NEAR =  0.1f;
56

    
57
    private GLSurfaceView mView;
58
    private DistortedTexture mTexture;
59
    private DistortedEffects mEffects;
60
    private MeshBase mMesh;
61
    private DistortedScreen mScreen;
62
    private int mObjWidth, mObjHeight, mObjDepth;
63
    private Static3D mMove, mScale, mCenter;
64

    
65
    Static4D mQuat1, mQuat2;
66
    int mScreenMin;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
    EarthRenderer(GLSurfaceView v)
71
      {
72
      mView = v;
73

    
74
      mMove = new Static3D(0,0,0);
75
      mScale= new Static3D(1,1,1);
76
      mCenter=new Static3D(0,0,0);
77

    
78
      mMesh   = new MeshSphere(LEVEL);
79
      mTexture= new DistortedTexture(SIZE,SIZE);
80

    
81
      mObjWidth = mTexture.getWidth();
82
      mObjHeight= mTexture.getHeight();
83
      mObjDepth = mTexture.getDepth(mMesh);
84

    
85
      mQuat1 = new Static4D(0,0,0,1);  // unity
86
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
87
      
88
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
89
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
90

    
91
      quatInt1.add(mQuat1);
92
      quatInt2.add(mQuat2);
93

    
94
      mEffects = new DistortedEffects();
95
      mEffects.apply( new MatrixEffectMove(mMove) );
96
      mEffects.apply( new MatrixEffectScale(mScale));
97
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, mCenter) );
98
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, mCenter) );
99

    
100
      mScreen = new DistortedScreen();
101
      mScreen.setProjection(FOV, NEAR);
102
      }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
   
106
    public void onDrawFrame(GL10 glUnused) 
107
      {
108
      mScreen.render( System.currentTimeMillis() );
109
      }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
    
113
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
114
      {
115
      final float SCALE = 0.75f;
116

    
117
      mScreenMin = width<height ? width:height;
118
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (SCALE*height)/mObjHeight :  (SCALE*width)/mObjWidth;
119
      mMove.set( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0);
120
      mScale.set(factor,factor,factor);
121
      mCenter.set( (float)mObjWidth/2, (float)mObjHeight/2, (float)mObjDepth/2 );
122
      mScreen.resize(width, height);
123
      }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
    void setLevel(int level)
128
      {
129
      float inflateLevel = (level-50)/50.0f;
130
      mMesh.setInflate(inflateLevel);
131
      }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
    void addNewPoint(float x, float y, EffectName effect)
136
      {
137

    
138
      }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
    void removeAll()
143
      {
144
      mEffects.abortByType(EffectType.VERTEX);
145
      mEffects.abortByType(EffectType.FRAGMENT);
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
    
150
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
151
      {
152
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.world);
153
      Bitmap bitmap;
154

    
155
      try
156
        {
157
        bitmap = BitmapFactory.decodeStream(is);
158
        }
159
      finally
160
        {
161
        try
162
          {
163
          is.close();
164
          }
165
        catch(IOException e) { }
166
        }
167

    
168
      mTexture.setTexture(bitmap);
169

    
170
      mScreen.detachAll();
171
      mScreen.attach(mTexture,mEffects,mMesh);
172

    
173
      try
174
        {
175
        Distorted.onCreate(mView.getContext());
176
        }
177
      catch(Exception ex)
178
        {
179
        android.util.Log.e("Earth", ex.getMessage() );
180
        }
181
      }
182
}
(2-2/4)