Project

General

Profile

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

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

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 296d2e73 Leszek Koltunski
import org.distorted.library.effect.EffectName;
28
import org.distorted.library.effect.EffectType;
29 37b324c4 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.effect.MatrixEffectQuaternion;
31
import org.distorted.library.effect.MatrixEffectScale;
32 64558e4e Leszek Koltunski
import org.distorted.library.main.Distorted;
33 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36 57d7fdba Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
37 296d2e73 Leszek Koltunski
import org.distorted.library.mesh.MeshSphere;
38 7589635e Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
39
import org.distorted.library.type.Static3D;
40 64558e4e Leszek Koltunski
import org.distorted.library.type.Static4D;
41 427ab7bf Leszek Koltunski
42 9814e53c Leszek Koltunski
import java.io.IOException;
43
import java.io.InputStream;
44
45 64558e4e Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
46
import javax.microedition.khronos.opengles.GL10;
47 427ab7bf Leszek Koltunski
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50 64558e4e Leszek Koltunski
class EarthRenderer implements GLSurfaceView.Renderer
51 427ab7bf Leszek Koltunski
{
52 296d2e73 Leszek Koltunski
    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 64558e4e Leszek Koltunski
57 427ab7bf Leszek Koltunski
    private GLSurfaceView mView;
58 f6d884d5 Leszek Koltunski
    private DistortedTexture mTexture;
59 d04a4886 Leszek Koltunski
    private DistortedEffects mEffects;
60 57d7fdba Leszek Koltunski
    private MeshBase mMesh;
61 d218d64e leszek
    private DistortedScreen mScreen;
62 8a99c681 Leszek Koltunski
    private int mObjWidth, mObjHeight, mObjDepth;
63 37b324c4 Leszek Koltunski
    private Static3D mMove, mScale, mCenter;
64 261fe5bd Leszek Koltunski
65 7589635e Leszek Koltunski
    Static4D mQuat1, mQuat2;
66 427ab7bf Leszek Koltunski
    int mScreenMin;
67 9c3e749e Leszek Koltunski
68 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
69
70 64558e4e Leszek Koltunski
    EarthRenderer(GLSurfaceView v)
71 427ab7bf Leszek Koltunski
      {
72
      mView = v;
73 261fe5bd Leszek Koltunski
74 37b324c4 Leszek Koltunski
      mMove = new Static3D(0,0,0);
75
      mScale= new Static3D(1,1,1);
76
      mCenter=new Static3D(0,0,0);
77
78 296d2e73 Leszek Koltunski
      mMesh   = new MeshSphere(LEVEL);
79
      mTexture= new DistortedTexture(SIZE,SIZE);
80 261fe5bd Leszek Koltunski
81 f6d884d5 Leszek Koltunski
      mObjWidth = mTexture.getWidth();
82
      mObjHeight= mTexture.getHeight();
83 8a99c681 Leszek Koltunski
      mObjDepth = mTexture.getDepth(mMesh);
84 261fe5bd Leszek Koltunski
85 7589635e Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
86
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
87 427ab7bf Leszek Koltunski
      
88 37b324c4 Leszek Koltunski
      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 833685d0 Leszek Koltunski
94 37b324c4 Leszek Koltunski
      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 392e16fd Leszek Koltunski
100 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
101 64558e4e Leszek Koltunski
      mScreen.setProjection(FOV, NEAR);
102 427ab7bf Leszek Koltunski
      }
103
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
   
106
    public void onDrawFrame(GL10 glUnused) 
107
      {
108 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
109 427ab7bf Leszek Koltunski
      }
110
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
    
113
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
114
      {
115 64558e4e Leszek Koltunski
      final float SCALE = 0.75f;
116
117 427ab7bf Leszek Koltunski
      mScreenMin = width<height ? width:height;
118 64558e4e Leszek Koltunski
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (SCALE*height)/mObjHeight :  (SCALE*width)/mObjWidth;
119 37b324c4 Leszek Koltunski
      mMove.set( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0);
120
      mScale.set(factor,factor,factor);
121 b424b062 Leszek Koltunski
      mCenter.set( (float)mObjWidth/2, (float)mObjHeight/2, (float)mObjDepth/2 );
122 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
123 427ab7bf Leszek Koltunski
      }
124
125 9c3e749e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
126
127 9814e53c Leszek Koltunski
    void setLevel(int level)
128 9c3e749e Leszek Koltunski
      {
129 64558e4e Leszek Koltunski
      float inflateLevel = (level-50)/50.0f;
130
      mMesh.setInflate(inflateLevel);
131 9c3e749e Leszek Koltunski
      }
132
133 296d2e73 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
149
    
150
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
151
      {
152 9814e53c Leszek Koltunski
      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 64558e4e Leszek Koltunski
168 9814e53c Leszek Koltunski
      mTexture.setTexture(bitmap);
169 fe59d375 Leszek Koltunski
170
      mScreen.detachAll();
171
      mScreen.attach(mTexture,mEffects,mMesh);
172
173 427ab7bf Leszek Koltunski
      try
174
        {
175 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
176 427ab7bf Leszek Koltunski
        }
177
      catch(Exception ex)
178
        {
179 64558e4e Leszek Koltunski
        android.util.Log.e("Earth", ex.getMessage() );
180 427ab7bf Leszek Koltunski
        }
181
      }
182
}