Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericRenderer.java @ 77e66c58

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.generic;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import android.graphics.Bitmap;
27
import android.graphics.BitmapFactory;
28
import android.opengl.GLSurfaceView;
29

    
30
import org.distorted.examples.R;
31
import org.distorted.library.effect.Effect;
32
import org.distorted.library.effect.EffectType;
33
import org.distorted.library.effect.MatrixEffectMove;
34
import org.distorted.library.effect.MatrixEffectQuaternion;
35
import org.distorted.library.effect.MatrixEffectScale;
36
import org.distorted.library.effect.VertexEffectScale;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.main.DistortedNode;
40
import org.distorted.library.main.DistortedScreen;
41
import org.distorted.library.main.DistortedTexture;
42
import org.distorted.library.mesh.MeshBase;
43
import org.distorted.library.mesh.MeshQuad;
44
import org.distorted.library.mesh.MeshSphere;
45
import org.distorted.library.type.Static3D;
46
import org.distorted.library.type.Static4D;
47

    
48
import java.io.IOException;
49
import java.io.InputStream;
50

    
51
import javax.microedition.khronos.egl.EGLConfig;
52
import javax.microedition.khronos.opengles.GL10;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
class GenericRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
57
{
58
    private static final float FOV = 30.0f;
59
    private static final float NEAR = 0.1f;
60

    
61
    private final GLSurfaceView mView;
62
    private final Resources mResources;
63
    private final DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
64
    private final DistortedEffects mObjectEffects;
65
    private final DistortedScreen mScreen;
66
    private final DistortedNode mCenterNode, mRegionNode;
67
    private final Static3D mCenterPoint, mRegionPoint, mScaleRegion;
68
    private final Static3D mMoveObject, mScaleObject, mScaleCenter, mMoveBackground, mScaleBackground;
69
    private final float mObjWidth, mObjHeight, mObjDepth, mFactor;
70

    
71
    private boolean mShowingCenter=false;
72
    private boolean mShowingRegion=false;
73
    private float mFactorObj;
74
    private int mWidth;
75

    
76
    Static4D mQuat1, mQuat2;
77
    int mScreenMin;
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
    GenericRenderer(GLSurfaceView v)
82
      {
83
      mView = v;
84
      mResources = v.getResources();
85

    
86
      GenericActivity2 act = (GenericActivity2)v.getContext();
87

    
88
      mMoveObject     = new Static3D(0,0,0);
89
      mScaleObject    = new Static3D(1,1,1);
90
      mScaleCenter    = new Static3D(1,1,1);
91
      mMoveBackground = new Static3D(0,0,0);
92
      mScaleBackground= new Static3D(1,1,1);
93

    
94
      mObjectTexture     = act.getTexture();
95
      mBackgroundTexture = new DistortedTexture();
96
      mCenterTexture     = new DistortedTexture();
97
      mRegionTexture     = new DistortedTexture();
98

    
99
      mFactorObj = 1.0f;
100

    
101
      mObjectEffects   = act.getEffects();
102
      DistortedEffects backgroundEffects = new DistortedEffects();
103
      DistortedEffects centerEffects     = new DistortedEffects();
104
      DistortedEffects regionEffects     = new DistortedEffects();
105

    
106
      MeshBase mesh   = act.getMesh();
107
      MeshQuad quad   = new MeshQuad();
108

    
109
      mFactor = mesh instanceof MeshSphere ? 1.2f : 0.7f;
110

    
111
      mObjWidth = act.getWidth();
112
      mObjHeight= act.getHeight();
113
      mObjDepth = act.getDepth();
114

    
115
      mQuat1 = new Static4D(0,0,0,1);  // unity
116
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
117

    
118
      mCenterPoint= new Static3D(0,0,0);
119
      mRegionPoint= new Static3D(0,0,0);
120
      mScaleRegion = new Static3D(1,1,1);
121

    
122
      mCenterNode = new DistortedNode(mCenterTexture, centerEffects, quad);
123
      mRegionNode = new DistortedNode(mRegionTexture, regionEffects, quad);
124

    
125
      mScreen = new DistortedScreen();
126
      mScreen.setProjection(FOV, NEAR);
127
      mScreen.attach(mBackgroundTexture, backgroundEffects, quad );
128
      mScreen.attach(mObjectTexture    , mObjectEffects   , mesh );
129

    
130
      Static3D rotateCen = new Static3D(0,0,0);
131

    
132
      MatrixEffectQuaternion quat1cen = new MatrixEffectQuaternion(mQuat1, rotateCen);
133
      MatrixEffectQuaternion quat2cen = new MatrixEffectQuaternion(mQuat2, rotateCen);
134
      MatrixEffectMove centerMove     = new MatrixEffectMove(mCenterPoint);
135

    
136
      centerEffects.apply( new MatrixEffectScale(mScaleCenter) );
137
      centerEffects.apply( centerMove );
138
      centerEffects.apply( new MatrixEffectMove(new Static3D(0,0,12)) );
139
      centerEffects.apply(quat2cen);
140
      centerEffects.apply(quat1cen);
141

    
142
      regionEffects.apply( new MatrixEffectScale(mScaleRegion) );
143
      regionEffects.apply( centerMove );
144
      regionEffects.apply( new MatrixEffectMove(mRegionPoint) );
145
      regionEffects.apply( new MatrixEffectMove(new Static3D(0,0,10)) );
146
      regionEffects.apply(quat2cen);
147
      regionEffects.apply(quat1cen);
148

    
149
      resetMatrixEffects();
150
      resetVertexEffects();
151

    
152
      // quite tricky: move the background exactly to the FAR plane! (see InternalOutputSurface.setProjection() )
153
      backgroundEffects.apply(new MatrixEffectScale(mScaleBackground) );
154
      backgroundEffects.apply(new MatrixEffectMove(mMoveBackground) );
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
    void resetMatrixEffects()
160
      {
161
      Static3D rotateObj = new Static3D(0,0,0);
162

    
163
      MatrixEffectQuaternion quat1obj = new MatrixEffectQuaternion(mQuat1,  rotateObj);
164
      MatrixEffectQuaternion quat2obj = new MatrixEffectQuaternion(mQuat2,  rotateObj);
165

    
166
      mObjectEffects.apply(quat2obj);
167
      mObjectEffects.apply(quat1obj);
168
      mObjectEffects.apply( new MatrixEffectScale(mScaleObject) );
169
      mObjectEffects.apply( new MatrixEffectMove(mMoveObject));
170

    
171
      mQuat1.set(0,0,0,1);
172
      mQuat2.set(0,0,0,1);
173
      }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
    void resetVertexEffects()
178
      {
179
      Static3D scale = new Static3D(mObjWidth, mObjHeight, mObjDepth);
180
      mObjectEffects.apply( new VertexEffectScale(scale) );
181
      }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
    void showRegionAndCenter(boolean showRegion, boolean showCenter)
186
      {
187
      if( mShowingCenter!=showCenter  )
188
        {
189
        if( showCenter )
190
          {
191
          if( mShowingRegion ) mScreen.detach(mRegionNode);
192
          mScreen.attach(mCenterNode);
193
          if( mShowingRegion ) mScreen.attach(mRegionNode);
194
          }
195
        else
196
          {
197
          mScreen.detach(mCenterNode);
198
          }
199

    
200
        mShowingCenter = showCenter;
201
        }
202

    
203
      if( mShowingRegion!=showRegion  )
204
        {
205
        if( showRegion ) mScreen.attach(mRegionNode);
206
        else             mScreen.detach(mRegionNode);
207

    
208
        mShowingRegion = showRegion;
209
        }
210
      }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
    public int getWidth()
215
      {
216
      return mWidth;
217
      }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
    float getScaleFactor()
222
      {
223
      return mFactorObj;
224
      }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
    void setCenter(float x, float y, float z)
229
      {
230
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
231
      }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
    void setRegion(float x, float y, float z, float r)
236
      {
237
      float factorReg = 2*mFactorObj*r;
238
      mRegionPoint.set(mFactorObj*x,mFactorObj*y, mFactorObj*z);
239
      mScaleRegion.set(factorReg,factorReg,factorReg);
240
      }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
    void useOIT(boolean use)
245
      {
246
      mScreen.setOrderIndependentTransparency(use);
247
      }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
    public void onDrawFrame(GL10 glUnused)
252
      {
253
      mScreen.render(System.currentTimeMillis());
254
      }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
259
      {
260
      mWidth = width;
261
      mScreenMin = Math.min(width, height);
262

    
263
      float factorCen;
264

    
265
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
266
        {
267
        mFactorObj = (mFactor*height)/mObjHeight;
268
        factorCen  = (0.08f  *height);
269
        }
270
      else
271
        {
272
        mFactorObj = (mFactor*width)/mObjWidth;
273
        factorCen  = (0.08f  *width);
274
        }
275

    
276
      setCenter(0,0,0);
277
      setRegion(0.0f,0.0f,0.0f,0.25f*(mObjWidth+mObjHeight));
278

    
279
      mMoveObject.set( 0, 0 , -mFactorObj*mObjDepth );
280
      mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
281
      mScaleCenter.set(factorCen,factorCen,factorCen);
282

    
283
      // quite tricky: move the background exactly to the FAR plane! (see InternalOutputSurface.setProjection() )
284
      mMoveBackground.set( 0,0, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
285
      mScaleBackground.set( 2*width, 2*height, 1.0f );
286

    
287
      mScreen.resize(width, height);
288
      }
289

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

    
292
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
293
      {
294
      GenericActivity2 act = (GenericActivity2)mView.getContext();
295

    
296
      InputStream isB = act.getResources().openRawResource(R.raw.water);
297
      InputStream isC = act.getResources().openRawResource(R.raw.center);
298
      InputStream isR = act.getResources().openRawResource(R.raw.region);
299

    
300
      Bitmap bitmapB,bitmapC,bitmapR;
301
        
302
      try 
303
        {
304
        bitmapB = BitmapFactory.decodeStream(isB);
305
        bitmapC = BitmapFactory.decodeStream(isC);
306
        bitmapR = BitmapFactory.decodeStream(isR);
307
        }
308
      finally 
309
        {
310
        try 
311
          {
312
          isB.close();
313
          isC.close();
314
          isR.close();
315
          }
316
        catch(IOException ignored) { }
317
        }  
318
      
319
      mObjectTexture.setTexture( act.getBitmap() );
320
      mBackgroundTexture.setTexture(bitmapB);
321
      mCenterTexture.setTexture(bitmapC);
322
      mRegionTexture.setTexture(bitmapR);
323

    
324
      Effect.enableEffects(EffectType.VERTEX);
325
      Effect.enableEffects(EffectType.FRAGMENT);
326
      Effect.enableEffects(EffectType.POSTPROCESS);
327

    
328
      DistortedLibrary.needTransformFeedback();
329
      DistortedLibrary.onSurfaceCreated(this);
330
      }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
   public void distortedException(Exception ex)
335
      {
336
      android.util.Log.e("Generic", ex.getMessage() );
337
      }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

    
341
   public InputStream localFile(int fileID)
342
      {
343
      return mResources.openRawResource(fileID);
344
      }
345

    
346
///////////////////////////////////////////////////////////////////////////////////////////////////
347

    
348
   public void logMessage(String message)
349
      {
350
      android.util.Log.e("Generic", message );
351
      }
352
}
(5-5/8)