Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericRenderer.java @ f3ca895f

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