Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericRenderer.java @ 75cc1461

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
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25
26
import org.distorted.examples.R;
27 b88ec561 Leszek Koltunski
import org.distorted.library.effect.Effect;
28
import org.distorted.library.effect.EffectType;
29 06366d12 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.effect.MatrixEffectQuaternion;
31
import org.distorted.library.effect.MatrixEffectScale;
32 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
33 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedNode;
35
import org.distorted.library.main.DistortedScreen;
36
import org.distorted.library.main.DistortedTexture;
37 57d7fdba Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
38 698ad0a8 Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
39 e83350b4 Leszek Koltunski
import org.distorted.library.mesh.MeshSphere;
40 08f92d82 Leszek Koltunski
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42
43
import java.io.IOException;
44
import java.io.InputStream;
45
46
import javax.microedition.khronos.egl.EGLConfig;
47
import javax.microedition.khronos.opengles.GL10;
48
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51 46b26cdc Leszek Koltunski
class GenericRenderer implements GLSurfaceView.Renderer
52 08f92d82 Leszek Koltunski
{
53 f86c9fb5 Leszek Koltunski
    private static final float FOV = 30.0f;
54 af662543 leszek
    private static final float NEAR = 0.1f;
55
56 08f92d82 Leszek Koltunski
    private GLSurfaceView mView;
57 f6d884d5 Leszek Koltunski
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
58 698ad0a8 Leszek Koltunski
    private DistortedEffects mObjectEffects;
59 d218d64e leszek
    private DistortedScreen mScreen;
60 fe59d375 Leszek Koltunski
    private DistortedNode mCenterNode, mRegionNode;
61 698ad0a8 Leszek Koltunski
    private float mObjWidth, mObjHeight, mObjDepth;
62 529054e9 Leszek Koltunski
    private Static3D mCenterPoint, mRegionPoint, mScaleRegion;
63
    private Static3D mMoveObject, mScaleObject, mScaleCenter, mMoveBackground, mScaleBackground;
64 fec27f16 Leszek Koltunski
    private boolean mShowingCenter=false;
65
    private boolean mShowingRegion=false;
66 1e7603bb Leszek Koltunski
    private float mFactorObj;
67 65f622c1 Leszek Koltunski
    private int mWidth;
68 e83350b4 Leszek Koltunski
    private float mFactor;
69 950511ed Leszek Koltunski
70 833685d0 Leszek Koltunski
    Static4D mQuat1, mQuat2;
71 d2337a3a Leszek Koltunski
    int mScreenMin;
72 833685d0 Leszek Koltunski
73 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75 46b26cdc Leszek Koltunski
    GenericRenderer(GLSurfaceView v)
76 08f92d82 Leszek Koltunski
      {
77
      mView = v;
78 d40cfeb2 Leszek Koltunski
79 46b26cdc Leszek Koltunski
      GenericActivity2 act = (GenericActivity2)v.getContext();
80 e8b6aa95 Leszek Koltunski
81 06366d12 Leszek Koltunski
      mMoveObject     = new Static3D(0,0,0);
82 fec27f16 Leszek Koltunski
      mScaleObject    = new Static3D(1,1,1);
83 06366d12 Leszek Koltunski
      mScaleCenter    = new Static3D(1,1,1);
84
      mMoveBackground = new Static3D(0,0,0);
85
      mScaleBackground= new Static3D(1,1,1);
86
87 f6d884d5 Leszek Koltunski
      mObjectTexture     = act.getTexture();
88 687263cc Leszek Koltunski
      mBackgroundTexture = new DistortedTexture();
89
      mCenterTexture     = new DistortedTexture();
90
      mRegionTexture     = new DistortedTexture();
91 06366d12 Leszek Koltunski
92 1e7603bb Leszek Koltunski
      mFactorObj = 1.0f;
93
94 698ad0a8 Leszek Koltunski
      mObjectEffects   = act.getEffects();
95
      DistortedEffects backgroundEffects = new DistortedEffects();
96
      DistortedEffects centerEffects     = new DistortedEffects();
97
      DistortedEffects regionEffects     = new DistortedEffects();
98 f6d884d5 Leszek Koltunski
99 698ad0a8 Leszek Koltunski
      MeshBase mesh   = act.getMesh();
100 75cc1461 Leszek Koltunski
      MeshQuad quad   = new MeshQuad();
101 aac5c562 leszek
102 e83350b4 Leszek Koltunski
      mFactor = mesh instanceof MeshSphere ? 1.0f : 0.7f;
103
104 687263cc Leszek Koltunski
      mObjWidth = act.getWidth();
105
      mObjHeight= act.getHeight();
106
      mObjDepth = act.getDepth();
107 d40cfeb2 Leszek Koltunski
108 833685d0 Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
109
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
110
111 24991bc2 Leszek Koltunski
      mCenterPoint= new Static3D(0,0,0);
112
      mRegionPoint= new Static3D(0,0,0);
113 529054e9 Leszek Koltunski
      mScaleRegion = new Static3D(0,0,0);
114 392e16fd Leszek Koltunski
115 75cc1461 Leszek Koltunski
      mCenterNode = new DistortedNode(mCenterTexture, centerEffects, quad);
116
      mRegionNode = new DistortedNode(mRegionTexture, regionEffects, quad);
117 fe59d375 Leszek Koltunski
118 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
119 af662543 leszek
      mScreen.setProjection(FOV, NEAR);
120 75cc1461 Leszek Koltunski
      mScreen.attach(mBackgroundTexture, backgroundEffects, quad );
121 698ad0a8 Leszek Koltunski
      mScreen.attach(mObjectTexture    , mObjectEffects   , mesh );
122 06366d12 Leszek Koltunski
123 529054e9 Leszek Koltunski
      Static3D rotateCen = new Static3D(0,0,0);
124 06366d12 Leszek Koltunski
125 529054e9 Leszek Koltunski
      MatrixEffectQuaternion quat1cen = new MatrixEffectQuaternion(mQuat1, rotateCen);
126
      MatrixEffectQuaternion quat2cen = new MatrixEffectQuaternion(mQuat2, rotateCen);
127
      MatrixEffectMove centerMove     = new MatrixEffectMove(mCenterPoint);
128 06366d12 Leszek Koltunski
129 698ad0a8 Leszek Koltunski
      centerEffects.apply( new MatrixEffectScale(mScaleCenter) );
130
      centerEffects.apply( centerMove );
131 529054e9 Leszek Koltunski
      centerEffects.apply( new MatrixEffectMove(new Static3D(0,0,12)) );
132 698ad0a8 Leszek Koltunski
      centerEffects.apply(quat2cen);
133
      centerEffects.apply(quat1cen);
134 06366d12 Leszek Koltunski
135 529054e9 Leszek Koltunski
      regionEffects.apply( new MatrixEffectScale(mScaleRegion) );
136 698ad0a8 Leszek Koltunski
      regionEffects.apply( centerMove );
137 529054e9 Leszek Koltunski
      regionEffects.apply( new MatrixEffectMove(mRegionPoint) );
138
      regionEffects.apply( new MatrixEffectMove(new Static3D(0,0,10)) );
139 698ad0a8 Leszek Koltunski
      regionEffects.apply(quat2cen);
140
      regionEffects.apply(quat1cen);
141 06366d12 Leszek Koltunski
142 65f622c1 Leszek Koltunski
      resetMatrixEffects();
143
144 e3900503 Leszek Koltunski
      // quite tricky: move the background exactly to the FAR plane! (see InternalOutputSurface.setProjection() )
145 698ad0a8 Leszek Koltunski
      backgroundEffects.apply(new MatrixEffectScale(mScaleBackground) );
146
      backgroundEffects.apply(new MatrixEffectMove(mMoveBackground) );
147 f338550a leszek
      }
148
149 65f622c1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
150
151
    void resetMatrixEffects()
152
      {
153 529054e9 Leszek Koltunski
      Static3D rotateObj = new Static3D(0,0,0);
154 65f622c1 Leszek Koltunski
155
      MatrixEffectQuaternion quat1obj = new MatrixEffectQuaternion(mQuat1,  rotateObj);
156
      MatrixEffectQuaternion quat2obj = new MatrixEffectQuaternion(mQuat2,  rotateObj);
157
158 687263cc Leszek Koltunski
      mObjectEffects.apply(quat2obj);
159
      mObjectEffects.apply(quat1obj);
160
      mObjectEffects.apply( new MatrixEffectScale(mScaleObject) );
161
      mObjectEffects.apply( new MatrixEffectMove(mMoveObject));
162 65f622c1 Leszek Koltunski
163
      mQuat1.set(0,0,0,1);
164
      mQuat2.set(0,0,0,1);
165
      }
166
167 f338550a leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
168
169 0ab55f0c Leszek Koltunski
    void showRegionAndCenter(boolean showRegion, boolean showCenter)
170 f338550a leszek
      {
171 0ab55f0c Leszek Koltunski
      if( mShowingCenter!=showCenter  )
172 98c04ab8 leszek
        {
173 1e7603bb Leszek Koltunski
        if( showCenter )
174
          {
175
          if( mShowingRegion ) mScreen.detach(mRegionNode);
176
          mScreen.attach(mCenterNode);
177
          if( mShowingRegion ) mScreen.attach(mRegionNode);
178
          }
179
        else
180
          {
181
          mScreen.detach(mCenterNode);
182
          }
183 0ab55f0c Leszek Koltunski
184
        mShowingCenter = showCenter;
185
        }
186
187
      if( mShowingRegion!=showRegion  )
188
        {
189
        if( showRegion ) mScreen.attach(mRegionNode);
190
        else             mScreen.detach(mRegionNode);
191
192
        mShowingRegion = showRegion;
193 98c04ab8 leszek
        }
194 950511ed Leszek Koltunski
      }
195
196 65f622c1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
197
198
    public int getWidth()
199
      {
200
      return mWidth;
201
      }
202
203 fc286c71 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
204
205
    float getScaleFactor()
206
      {
207
      return mFactorObj;
208
      }
209
210 950511ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212 334c13fa Leszek Koltunski
    void setCenter(float x, float y, float z)
213 950511ed Leszek Koltunski
      {
214 334c13fa Leszek Koltunski
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
215 08f92d82 Leszek Koltunski
      }
216
217 6f779cd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
218
219 9e7b6dbd Leszek Koltunski
    void setRegion(float x, float y, float z, float r)
220 6f779cd4 Leszek Koltunski
      {
221 75cc1461 Leszek Koltunski
      float factorReg = 2*mFactorObj*r;
222 1e7603bb Leszek Koltunski
      mRegionPoint.set(mFactorObj*x,mFactorObj*y, mFactorObj*z);
223 529054e9 Leszek Koltunski
      mScaleRegion.set(factorReg,factorReg,factorReg);
224 6f779cd4 Leszek Koltunski
      }
225
226 bddd4b2d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
227
228
    void useOIT(boolean use)
229
      {
230
      mScreen.setOrderIndependentTransparency(use);
231
      }
232
233 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
234 56cbe1cf Leszek Koltunski
235
    public void onDrawFrame(GL10 glUnused)
236 08f92d82 Leszek Koltunski
      {
237 fe59d375 Leszek Koltunski
      mScreen.render(System.currentTimeMillis());
238 08f92d82 Leszek Koltunski
      }
239
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241 56cbe1cf Leszek Koltunski
242
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
243 08f92d82 Leszek Koltunski
      {
244 65f622c1 Leszek Koltunski
      mWidth = width;
245 75cc1461 Leszek Koltunski
      mScreenMin = Math.min(width, height);
246 752c6b57 Leszek Koltunski
247 950511ed Leszek Koltunski
      float factorCen;
248 6f779cd4 Leszek Koltunski
249 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
250 d40cfeb2 Leszek Koltunski
        {
251 e83350b4 Leszek Koltunski
        mFactorObj = (mFactor*height)/mObjHeight;
252 75cc1461 Leszek Koltunski
        factorCen  = (0.08f  *height);
253 261fe5bd Leszek Koltunski
        }
254 d40cfeb2 Leszek Koltunski
      else
255
        {
256 e83350b4 Leszek Koltunski
        mFactorObj = (mFactor*width)/mObjWidth;
257 75cc1461 Leszek Koltunski
        factorCen  = (0.08f  *width);
258 d40cfeb2 Leszek Koltunski
        }
259
260 529054e9 Leszek Koltunski
      setCenter(0,0,0);
261 7126b262 Leszek Koltunski
      setRegion(0.0f,0.0f,0.0f,0.25f*(mObjWidth+mObjHeight));
262 1e7603bb Leszek Koltunski
263 529054e9 Leszek Koltunski
      mMoveObject.set( 0, 0 , -mFactorObj*mObjDepth );
264 fec27f16 Leszek Koltunski
      mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
265 06366d12 Leszek Koltunski
      mScaleCenter.set(factorCen,factorCen,factorCen);
266 fce25d04 leszek
267 e3900503 Leszek Koltunski
      // quite tricky: move the background exactly to the FAR plane! (see InternalOutputSurface.setProjection() )
268 529054e9 Leszek Koltunski
      mMoveBackground.set( 0,0, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
269 75cc1461 Leszek Koltunski
      mScaleBackground.set( 2*width, 2*height, 1.0f );
270 fce25d04 leszek
271 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
272 08f92d82 Leszek Koltunski
      }
273
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275 56cbe1cf Leszek Koltunski
276
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
277 08f92d82 Leszek Koltunski
      {
278 46b26cdc Leszek Koltunski
      GenericActivity2 act = (GenericActivity2)mView.getContext();
279 bcc8e016 Leszek Koltunski
280 f6d884d5 Leszek Koltunski
      InputStream isB = act.getResources().openRawResource(R.raw.water);
281
      InputStream isC = act.getResources().openRawResource(R.raw.center);
282
      InputStream isR = act.getResources().openRawResource(R.raw.region);
283 71d8ad03 Leszek Koltunski
284 f6d884d5 Leszek Koltunski
      Bitmap bitmapB,bitmapC,bitmapR;
285 08f92d82 Leszek Koltunski
        
286
      try 
287
        {
288 f6d884d5 Leszek Koltunski
        bitmapB = BitmapFactory.decodeStream(isB);
289
        bitmapC = BitmapFactory.decodeStream(isC);
290
        bitmapR = BitmapFactory.decodeStream(isR);
291 71d8ad03 Leszek Koltunski
        }
292 08f92d82 Leszek Koltunski
      finally 
293
        {
294
        try 
295
          {
296 f6d884d5 Leszek Koltunski
          isB.close();
297
          isC.close();
298
          isR.close();
299 9167cfd4 Leszek Koltunski
          }
300 75cc1461 Leszek Koltunski
        catch(IOException ignored) { }
301 08f92d82 Leszek Koltunski
        }  
302
      
303 f6d884d5 Leszek Koltunski
      mObjectTexture.setTexture( act.getBitmap() );
304
      mBackgroundTexture.setTexture(bitmapB);
305
      mCenterTexture.setTexture(bitmapC);
306
      mRegionTexture.setTexture(bitmapR);
307 950511ed Leszek Koltunski
308 e3900503 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX  ,10);
309
      DistortedLibrary.setMax(EffectType.FRAGMENT,10);
310 676c14da Leszek Koltunski
311 b88ec561 Leszek Koltunski
      Effect.enableEffects(EffectType.VERTEX);
312
      Effect.enableEffects(EffectType.FRAGMENT);
313
      Effect.enableEffects(EffectType.POSTPROCESS);
314 cdcbdbe3 Leszek Koltunski
315 08f92d82 Leszek Koltunski
      try
316
        {
317 e3900503 Leszek Koltunski
        DistortedLibrary.onCreate(mView.getContext());
318 08f92d82 Leszek Koltunski
        }
319
      catch(Exception ex)
320
        {
321 76a81b6a Leszek Koltunski
        android.util.Log.e("Effects3D", ex.getMessage() );
322 08f92d82 Leszek Koltunski
        }
323
      }
324 56cbe1cf Leszek Koltunski
}