Project

General

Profile

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

examples / src / main / java / org / distorted / examples / earth / EarthRenderer.java @ 9814e53c

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

    
39
import java.io.IOException;
40
import java.io.InputStream;
41

    
42
import javax.microedition.khronos.egl.EGLConfig;
43
import javax.microedition.khronos.opengles.GL10;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
class EarthRenderer implements GLSurfaceView.Renderer
48
{
49
    private static final float FOV = 30.0f;
50
    private static final float NEAR = 0.1f;
51

    
52
    private GLSurfaceView mView;
53
    private DistortedTexture mTexture;
54
    private DistortedEffects mEffects;
55
    private MeshBase mMesh;
56
    private DistortedScreen mScreen;
57
    private int mObjWidth, mObjHeight, mObjDepth;
58
    private Static3D mMove, mScale, mCenter;
59

    
60
    Static4D mQuat1, mQuat2;
61
    int mScreenMin;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
    EarthRenderer(GLSurfaceView v)
66
      {
67
      mView = v;
68

    
69
      mMove = new Static3D(0,0,0);
70
      mScale= new Static3D(1,1,1);
71
      mCenter=new Static3D(0,0,0);
72

    
73
      EarthActivity act = (EarthActivity)v.getContext();
74

    
75
      mTexture = act.getTexture();
76
      mMesh    = act.getMesh();
77

    
78
      mObjWidth = mTexture.getWidth();
79
      mObjHeight= mTexture.getHeight();
80
      mObjDepth = mTexture.getDepth(mMesh);
81

    
82
      mQuat1 = new Static4D(0,0,0,1);  // unity
83
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
84
      
85
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
86
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
87

    
88
      quatInt1.add(mQuat1);
89
      quatInt2.add(mQuat2);
90

    
91
      mEffects = new DistortedEffects();
92
      mEffects.apply( new MatrixEffectMove(mMove) );
93
      mEffects.apply( new MatrixEffectScale(mScale));
94
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, mCenter) );
95
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, mCenter) );
96

    
97
      mScreen = new DistortedScreen();
98
      mScreen.setProjection(FOV, NEAR);
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
   
103
    public void onDrawFrame(GL10 glUnused) 
104
      {
105
      mScreen.render( System.currentTimeMillis() );
106
      }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
    
110
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
111
      {
112
      final float SCALE = 0.75f;
113

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

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
    void setLevel(int level)
125
      {
126
      float inflateLevel = (level-50)/50.0f;
127
      mMesh.setInflate(inflateLevel);
128
      }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
    
132
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
133
      {
134
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.world);
135
      Bitmap bitmap;
136

    
137
      try
138
        {
139
        bitmap = BitmapFactory.decodeStream(is);
140
        }
141
      finally
142
        {
143
        try
144
          {
145
          is.close();
146
          }
147
        catch(IOException e) { }
148
        }
149

    
150
      mTexture.setTexture(bitmap);
151

    
152
      mScreen.detachAll();
153
      mScreen.attach(mTexture,mEffects,mMesh);
154

    
155
      try
156
        {
157
        Distorted.onCreate(mView.getContext());
158
        }
159
      catch(Exception ex)
160
        {
161
        android.util.Log.e("Earth", ex.getMessage() );
162
        }
163
      }
164
}
(2-2/3)