Project

General

Profile

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

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

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