Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericRenderer.java @ 625c67de

1 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 08f92d82 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 08f92d82 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 08f92d82 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 2ec2dc32 Leszek Koltunski
package org.distorted.examples.generic;
21 08f92d82 Leszek Koltunski
22 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26 08f92d82 Leszek Koltunski
import android.graphics.Bitmap;
27
import android.graphics.BitmapFactory;
28
import android.opengl.GLSurfaceView;
29
30
import org.distorted.examples.R;
31 b88ec561 Leszek Koltunski
import org.distorted.library.effect.Effect;
32
import org.distorted.library.effect.EffectType;
33 06366d12 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
34
import org.distorted.library.effect.MatrixEffectQuaternion;
35
import org.distorted.library.effect.MatrixEffectScale;
36 b1fca44e Leszek Koltunski
import org.distorted.library.effect.VertexEffectScale;
37 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
38 01782e85 Leszek Koltunski
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 57d7fdba Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
43 698ad0a8 Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
44 e83350b4 Leszek Koltunski
import org.distorted.library.mesh.MeshSphere;
45 08f92d82 Leszek Koltunski
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 dc10a48d Leszek Koltunski
class GenericRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
57 08f92d82 Leszek Koltunski
{
58 f86c9fb5 Leszek Koltunski
    private static final float FOV = 30.0f;
59 af662543 leszek
    private static final float NEAR = 0.1f;
60
61 ef02f5e0 Leszek Koltunski
    private final GLSurfaceView mView;
62 dc10a48d Leszek Koltunski
    private final Resources mResources;
63 ef02f5e0 Leszek Koltunski
    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 fec27f16 Leszek Koltunski
    private boolean mShowingCenter=false;
72
    private boolean mShowingRegion=false;
73 1e7603bb Leszek Koltunski
    private float mFactorObj;
74 65f622c1 Leszek Koltunski
    private int mWidth;
75 950511ed Leszek Koltunski
76 833685d0 Leszek Koltunski
    Static4D mQuat1, mQuat2;
77 d2337a3a Leszek Koltunski
    int mScreenMin;
78 833685d0 Leszek Koltunski
79 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
80
81 46b26cdc Leszek Koltunski
    GenericRenderer(GLSurfaceView v)
82 08f92d82 Leszek Koltunski
      {
83
      mView = v;
84 dc10a48d Leszek Koltunski
      mResources = v.getResources();
85 d40cfeb2 Leszek Koltunski
86 46b26cdc Leszek Koltunski
      GenericActivity2 act = (GenericActivity2)v.getContext();
87 e8b6aa95 Leszek Koltunski
88 06366d12 Leszek Koltunski
      mMoveObject     = new Static3D(0,0,0);
89 fec27f16 Leszek Koltunski
      mScaleObject    = new Static3D(1,1,1);
90 06366d12 Leszek Koltunski
      mScaleCenter    = new Static3D(1,1,1);
91
      mMoveBackground = new Static3D(0,0,0);
92
      mScaleBackground= new Static3D(1,1,1);
93
94 f6d884d5 Leszek Koltunski
      mObjectTexture     = act.getTexture();
95 687263cc Leszek Koltunski
      mBackgroundTexture = new DistortedTexture();
96
      mCenterTexture     = new DistortedTexture();
97
      mRegionTexture     = new DistortedTexture();
98 06366d12 Leszek Koltunski
99 1e7603bb Leszek Koltunski
      mFactorObj = 1.0f;
100
101 698ad0a8 Leszek Koltunski
      mObjectEffects   = act.getEffects();
102
      DistortedEffects backgroundEffects = new DistortedEffects();
103
      DistortedEffects centerEffects     = new DistortedEffects();
104
      DistortedEffects regionEffects     = new DistortedEffects();
105 f6d884d5 Leszek Koltunski
106 698ad0a8 Leszek Koltunski
      MeshBase mesh   = act.getMesh();
107 75cc1461 Leszek Koltunski
      MeshQuad quad   = new MeshQuad();
108 aac5c562 leszek
109 0a57f222 Leszek Koltunski
      mFactor = mesh instanceof MeshSphere ? 1.2f : 0.7f;
110 e83350b4 Leszek Koltunski
111 687263cc Leszek Koltunski
      mObjWidth = act.getWidth();
112
      mObjHeight= act.getHeight();
113
      mObjDepth = act.getDepth();
114 d40cfeb2 Leszek Koltunski
115 833685d0 Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
116
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
117
118 24991bc2 Leszek Koltunski
      mCenterPoint= new Static3D(0,0,0);
119
      mRegionPoint= new Static3D(0,0,0);
120 0a57f222 Leszek Koltunski
      mScaleRegion = new Static3D(1,1,1);
121 392e16fd Leszek Koltunski
122 75cc1461 Leszek Koltunski
      mCenterNode = new DistortedNode(mCenterTexture, centerEffects, quad);
123
      mRegionNode = new DistortedNode(mRegionTexture, regionEffects, quad);
124 fe59d375 Leszek Koltunski
125 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
126 af662543 leszek
      mScreen.setProjection(FOV, NEAR);
127 75cc1461 Leszek Koltunski
      mScreen.attach(mBackgroundTexture, backgroundEffects, quad );
128 698ad0a8 Leszek Koltunski
      mScreen.attach(mObjectTexture    , mObjectEffects   , mesh );
129 06366d12 Leszek Koltunski
130 529054e9 Leszek Koltunski
      Static3D rotateCen = new Static3D(0,0,0);
131 06366d12 Leszek Koltunski
132 529054e9 Leszek Koltunski
      MatrixEffectQuaternion quat1cen = new MatrixEffectQuaternion(mQuat1, rotateCen);
133
      MatrixEffectQuaternion quat2cen = new MatrixEffectQuaternion(mQuat2, rotateCen);
134
      MatrixEffectMove centerMove     = new MatrixEffectMove(mCenterPoint);
135 06366d12 Leszek Koltunski
136 698ad0a8 Leszek Koltunski
      centerEffects.apply( new MatrixEffectScale(mScaleCenter) );
137
      centerEffects.apply( centerMove );
138 529054e9 Leszek Koltunski
      centerEffects.apply( new MatrixEffectMove(new Static3D(0,0,12)) );
139 698ad0a8 Leszek Koltunski
      centerEffects.apply(quat2cen);
140
      centerEffects.apply(quat1cen);
141 06366d12 Leszek Koltunski
142 529054e9 Leszek Koltunski
      regionEffects.apply( new MatrixEffectScale(mScaleRegion) );
143 698ad0a8 Leszek Koltunski
      regionEffects.apply( centerMove );
144 529054e9 Leszek Koltunski
      regionEffects.apply( new MatrixEffectMove(mRegionPoint) );
145
      regionEffects.apply( new MatrixEffectMove(new Static3D(0,0,10)) );
146 698ad0a8 Leszek Koltunski
      regionEffects.apply(quat2cen);
147
      regionEffects.apply(quat1cen);
148 06366d12 Leszek Koltunski
149 65f622c1 Leszek Koltunski
      resetMatrixEffects();
150 b1fca44e Leszek Koltunski
      resetVertexEffects();
151 65f622c1 Leszek Koltunski
152 e3900503 Leszek Koltunski
      // quite tricky: move the background exactly to the FAR plane! (see InternalOutputSurface.setProjection() )
153 698ad0a8 Leszek Koltunski
      backgroundEffects.apply(new MatrixEffectScale(mScaleBackground) );
154
      backgroundEffects.apply(new MatrixEffectMove(mMoveBackground) );
155 f338550a leszek
      }
156
157 65f622c1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
158
159
    void resetMatrixEffects()
160
      {
161 529054e9 Leszek Koltunski
      Static3D rotateObj = new Static3D(0,0,0);
162 65f622c1 Leszek Koltunski
163
      MatrixEffectQuaternion quat1obj = new MatrixEffectQuaternion(mQuat1,  rotateObj);
164
      MatrixEffectQuaternion quat2obj = new MatrixEffectQuaternion(mQuat2,  rotateObj);
165
166 687263cc Leszek Koltunski
      mObjectEffects.apply(quat2obj);
167
      mObjectEffects.apply(quat1obj);
168
      mObjectEffects.apply( new MatrixEffectScale(mScaleObject) );
169
      mObjectEffects.apply( new MatrixEffectMove(mMoveObject));
170 65f622c1 Leszek Koltunski
171
      mQuat1.set(0,0,0,1);
172
      mQuat2.set(0,0,0,1);
173
      }
174
175 b1fca44e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
176
177
    void resetVertexEffects()
178
      {
179
      Static3D scale = new Static3D(mObjWidth, mObjHeight, mObjDepth);
180
      mObjectEffects.apply( new VertexEffectScale(scale) );
181
      }
182
183 f338550a leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
184
185 0ab55f0c Leszek Koltunski
    void showRegionAndCenter(boolean showRegion, boolean showCenter)
186 f338550a leszek
      {
187 0ab55f0c Leszek Koltunski
      if( mShowingCenter!=showCenter  )
188 98c04ab8 leszek
        {
189 1e7603bb Leszek Koltunski
        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 0ab55f0c Leszek Koltunski
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 98c04ab8 leszek
        }
210 950511ed Leszek Koltunski
      }
211
212 65f622c1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
213
214
    public int getWidth()
215
      {
216
      return mWidth;
217
      }
218
219 fc286c71 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
220
221
    float getScaleFactor()
222
      {
223
      return mFactorObj;
224
      }
225
226 950511ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
227
228 334c13fa Leszek Koltunski
    void setCenter(float x, float y, float z)
229 950511ed Leszek Koltunski
      {
230 334c13fa Leszek Koltunski
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
231 08f92d82 Leszek Koltunski
      }
232
233 6f779cd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
234
235 9e7b6dbd Leszek Koltunski
    void setRegion(float x, float y, float z, float r)
236 6f779cd4 Leszek Koltunski
      {
237 75cc1461 Leszek Koltunski
      float factorReg = 2*mFactorObj*r;
238 1e7603bb Leszek Koltunski
      mRegionPoint.set(mFactorObj*x,mFactorObj*y, mFactorObj*z);
239 529054e9 Leszek Koltunski
      mScaleRegion.set(factorReg,factorReg,factorReg);
240 6f779cd4 Leszek Koltunski
      }
241
242 bddd4b2d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
243
244
    void useOIT(boolean use)
245
      {
246
      mScreen.setOrderIndependentTransparency(use);
247
      }
248
249 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
250 56cbe1cf Leszek Koltunski
251
    public void onDrawFrame(GL10 glUnused)
252 08f92d82 Leszek Koltunski
      {
253 fe59d375 Leszek Koltunski
      mScreen.render(System.currentTimeMillis());
254 08f92d82 Leszek Koltunski
      }
255
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257 56cbe1cf Leszek Koltunski
258
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
259 08f92d82 Leszek Koltunski
      {
260 65f622c1 Leszek Koltunski
      mWidth = width;
261 75cc1461 Leszek Koltunski
      mScreenMin = Math.min(width, height);
262 752c6b57 Leszek Koltunski
263 950511ed Leszek Koltunski
      float factorCen;
264 6f779cd4 Leszek Koltunski
265 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
266 d40cfeb2 Leszek Koltunski
        {
267 e83350b4 Leszek Koltunski
        mFactorObj = (mFactor*height)/mObjHeight;
268 75cc1461 Leszek Koltunski
        factorCen  = (0.08f  *height);
269 261fe5bd Leszek Koltunski
        }
270 d40cfeb2 Leszek Koltunski
      else
271
        {
272 e83350b4 Leszek Koltunski
        mFactorObj = (mFactor*width)/mObjWidth;
273 75cc1461 Leszek Koltunski
        factorCen  = (0.08f  *width);
274 d40cfeb2 Leszek Koltunski
        }
275
276 529054e9 Leszek Koltunski
      setCenter(0,0,0);
277 7126b262 Leszek Koltunski
      setRegion(0.0f,0.0f,0.0f,0.25f*(mObjWidth+mObjHeight));
278 1e7603bb Leszek Koltunski
279 529054e9 Leszek Koltunski
      mMoveObject.set( 0, 0 , -mFactorObj*mObjDepth );
280 fec27f16 Leszek Koltunski
      mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
281 06366d12 Leszek Koltunski
      mScaleCenter.set(factorCen,factorCen,factorCen);
282 fce25d04 leszek
283 e3900503 Leszek Koltunski
      // quite tricky: move the background exactly to the FAR plane! (see InternalOutputSurface.setProjection() )
284 529054e9 Leszek Koltunski
      mMoveBackground.set( 0,0, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
285 75cc1461 Leszek Koltunski
      mScaleBackground.set( 2*width, 2*height, 1.0f );
286 fce25d04 leszek
287 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
288 08f92d82 Leszek Koltunski
      }
289
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291 56cbe1cf Leszek Koltunski
292
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
293 08f92d82 Leszek Koltunski
      {
294 46b26cdc Leszek Koltunski
      GenericActivity2 act = (GenericActivity2)mView.getContext();
295 bcc8e016 Leszek Koltunski
296 f6d884d5 Leszek Koltunski
      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 71d8ad03 Leszek Koltunski
300 f6d884d5 Leszek Koltunski
      Bitmap bitmapB,bitmapC,bitmapR;
301 08f92d82 Leszek Koltunski
        
302
      try 
303
        {
304 f6d884d5 Leszek Koltunski
        bitmapB = BitmapFactory.decodeStream(isB);
305
        bitmapC = BitmapFactory.decodeStream(isC);
306
        bitmapR = BitmapFactory.decodeStream(isR);
307 71d8ad03 Leszek Koltunski
        }
308 08f92d82 Leszek Koltunski
      finally 
309
        {
310
        try 
311
          {
312 f6d884d5 Leszek Koltunski
          isB.close();
313
          isC.close();
314
          isR.close();
315 9167cfd4 Leszek Koltunski
          }
316 75cc1461 Leszek Koltunski
        catch(IOException ignored) { }
317 08f92d82 Leszek Koltunski
        }  
318
      
319 f6d884d5 Leszek Koltunski
      mObjectTexture.setTexture( act.getBitmap() );
320
      mBackgroundTexture.setTexture(bitmapB);
321
      mCenterTexture.setTexture(bitmapC);
322
      mRegionTexture.setTexture(bitmapR);
323 950511ed Leszek Koltunski
324 b88ec561 Leszek Koltunski
      Effect.enableEffects(EffectType.VERTEX);
325
      Effect.enableEffects(EffectType.FRAGMENT);
326
      Effect.enableEffects(EffectType.POSTPROCESS);
327 cdcbdbe3 Leszek Koltunski
328 ef02f5e0 Leszek Koltunski
      DistortedLibrary.needTransformFeedback();
329 dc10a48d Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(this);
330 061449ed Leszek Koltunski
      }
331
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333
334
   public void distortedException(Exception ex)
335
      {
336
      android.util.Log.e("Generic", ex.getMessage() );
337 08f92d82 Leszek Koltunski
      }
338 dc10a48d Leszek Koltunski
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 56cbe1cf Leszek Koltunski
}