Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ 4dd7ca16

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

    
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25

    
26
import org.distorted.examples.R;
27
import org.distorted.library.effect.FragmentEffectAlpha;
28
import org.distorted.library.effect.FragmentEffectBrightness;
29
import org.distorted.library.effect.FragmentEffectChroma;
30
import org.distorted.library.effect.FragmentEffectContrast;
31
import org.distorted.library.effect.FragmentEffectSaturation;
32
import org.distorted.library.effect.MatrixEffectMove;
33
import org.distorted.library.effect.MatrixEffectQuaternion;
34
import org.distorted.library.effect.MatrixEffectScale;
35
import org.distorted.library.effect.VertexEffectDeform;
36
import org.distorted.library.effect.VertexEffectDistort;
37
import org.distorted.library.effect.VertexEffectPinch;
38
import org.distorted.library.effect.VertexEffectSink;
39
import org.distorted.library.effect.VertexEffectSwirl;
40
import org.distorted.library.effect.VertexEffectWave;
41
import org.distorted.library.main.Distorted;
42
import org.distorted.library.main.DistortedEffects;
43
import org.distorted.library.main.DistortedNode;
44
import org.distorted.library.main.DistortedScreen;
45
import org.distorted.library.main.DistortedTexture;
46
import org.distorted.library.main.MeshFlat;
47
import org.distorted.library.main.MeshObject;
48
import org.distorted.library.type.Static3D;
49
import org.distorted.library.type.Static4D;
50

    
51
import java.io.IOException;
52
import java.io.InputStream;
53

    
54
import javax.microedition.khronos.egl.EGLConfig;
55
import javax.microedition.khronos.opengles.GL10;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
class Effects3DRenderer implements GLSurfaceView.Renderer
60
{
61
    private static final float FOV = 70.0f;
62
    private static final float NEAR = 0.1f;
63

    
64
    private GLSurfaceView mView;
65
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
66
    private DistortedScreen mScreen;
67
    private DistortedNode mCenterNode, mRegionNode;
68
    private int mObjWidth, mObjHeight;
69
    private Static3D mCenterPoint, mRegionPoint, mRegionScalePoint;
70
    private Static3D mRotateCen, mMoveObject, mScaleObject, mMoveCenter, mScaleCenter, mMoveRegion, mMoveBackground, mScaleBackground;
71
    private boolean mShowingCenter=false;
72
    private boolean mShowingRegion=false;
73
    private float mFactorObj, mFactorReg;
74

    
75
    Static4D mQuat1, mQuat2;
76
    int mScreenMin;
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
    Effects3DRenderer(GLSurfaceView v)
81
      {
82
      mView = v;
83

    
84
      Effects3DActivity act = (Effects3DActivity)v.getContext();
85

    
86
      mRotateCen      = new Static3D(0,0,0);
87
      mMoveObject     = new Static3D(0,0,0);
88
      mScaleObject    = new Static3D(1,1,1);
89
      mMoveCenter     = new Static3D(0,0,0);
90
      mScaleCenter    = new Static3D(1,1,1);
91
      mMoveRegion     = new Static3D(0,0,0);
92
      mMoveBackground = new Static3D(0,0,0);
93
      mScaleBackground= new Static3D(1,1,1);
94

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

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

    
105
      MeshObject meshO   = act.getMesh();
106
      MeshFlat quad      = new MeshFlat(1,1);
107

    
108
      mObjWidth = mObjectTexture.getWidth();
109
      mObjHeight= mObjectTexture.getHeight();
110
      int objDepth = mObjectTexture.getDepth(meshO);
111

    
112
      mQuat1 = new Static4D(0,0,0,1);  // unity
113
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
114

    
115
      mCenterPoint= new Static3D(0,0,0);
116
      mRegionPoint= new Static3D(0,0,0);
117
      mRegionScalePoint = new Static3D(0,0,0);
118

    
119
      mCenterNode = new DistortedNode(mCenterTexture, centerEffects, quad);
120
      mRegionNode = new DistortedNode(mRegionTexture, regionEffects, quad);
121

    
122
      mScreen = new DistortedScreen();
123
      mScreen.setProjection(FOV, NEAR);
124
      mScreen.attach(mBackgroundTexture, backgroundEffects, quad );
125
      mScreen.attach(mObjectTexture    , objectEffects    , meshO);
126

    
127
      int regionSize = mRegionTexture.getWidth();
128
      Static3D rotateObj = new Static3D( (float)mObjWidth/2, (float)mObjHeight/2, -(float)objDepth/2 );
129
      mRotateCen = new Static3D(0 ,0, 0);
130

    
131
      MatrixEffectQuaternion quat1obj = new MatrixEffectQuaternion(mQuat1,  rotateObj);
132
      MatrixEffectQuaternion quat2obj = new MatrixEffectQuaternion(mQuat2,  rotateObj);
133
      MatrixEffectQuaternion quat1cen = new MatrixEffectQuaternion(mQuat1, mRotateCen);
134
      MatrixEffectQuaternion quat2cen = new MatrixEffectQuaternion(mQuat2, mRotateCen);
135
      MatrixEffectMove centerMove = new MatrixEffectMove(mCenterPoint);
136

    
137
      objectEffects.apply( new MatrixEffectMove(mMoveObject));
138
      objectEffects.apply( new MatrixEffectScale(mScaleObject) );
139
      objectEffects.apply(quat1obj);
140
      objectEffects.apply(quat2obj);
141

    
142
      centerEffects.apply(quat1cen);
143
      centerEffects.apply(quat2cen);
144
      centerEffects.apply( new MatrixEffectMove(mMoveCenter) );
145
      centerEffects.apply( centerMove );
146
      centerEffects.apply( new MatrixEffectScale(mScaleCenter) );
147

    
148
      regionEffects.apply(quat1cen);
149
      regionEffects.apply(quat2cen);
150
      regionEffects.apply( new MatrixEffectMove(mMoveRegion) );
151
      regionEffects.apply( centerMove );
152
      regionEffects.apply( new MatrixEffectMove(mRegionPoint) );
153
      regionEffects.apply( new MatrixEffectScale(mRegionScalePoint) );
154
      regionEffects.apply( new MatrixEffectMove(new Static3D( -regionSize/2 , -regionSize/2 , 0)) );
155

    
156
      // quite tricky: move the background exactly to the FAR plane! (see DistortedOutputSurface.setProjection() )
157
      backgroundEffects.apply(new MatrixEffectMove(mMoveBackground) );
158
      backgroundEffects.apply(new MatrixEffectScale(mScaleBackground) );
159
      }
160

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
    void showRegionAndCenter(boolean showRegion, boolean showCenter)
164
      {
165
      if( mShowingCenter!=showCenter  )
166
        {
167
        if( showCenter ) mScreen.attach(mCenterNode);
168
        else             mScreen.detach(mCenterNode);
169

    
170
        mShowingCenter = showCenter;
171
        }
172

    
173
      if( mShowingRegion!=showRegion  )
174
        {
175
        if( showRegion ) mScreen.attach(mRegionNode);
176
        else             mScreen.detach(mRegionNode);
177

    
178
        mShowingRegion = showRegion;
179
        }
180
      }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
    void setCenter(float x, float y, float z)
185
      {
186
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
187
      }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
    void setRegion(float x, float y, float r)
192
      {
193
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
194
      mRegionPoint.set(mFactorObj*x,-mFactorObj*y,0);
195
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
196
      }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
    public void onDrawFrame(GL10 glUnused)
201
      {
202
      mScreen.render(System.currentTimeMillis());
203
      }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
208
      {
209
      mScreenMin = width<height ? width:height;
210

    
211
      float factorCen;
212
      int centerSize = mCenterTexture.getWidth();
213
      int regionSize = mRegionTexture.getWidth();
214

    
215
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
216
        {
217
        mFactorObj = (0.80f*height)/mObjHeight;
218
        factorCen  = (0.08f*height)/centerSize;
219
        }
220
      else
221
        {
222
        mFactorObj = (0.80f*width)/mObjWidth;
223
        factorCen  = (0.08f*width)/centerSize;
224
        }
225

    
226
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
227
      mCenterPoint.set(mFactorObj*act.getCenterX(),+mFactorObj*act.getCenterY(),0);
228
      mRegionPoint.set(mFactorObj*act.getRegionX(),-mFactorObj*act.getRegionY(),0);
229
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
230
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
231
      mMoveObject.set( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0 );
232
      mRotateCen.set(width/2,height/2, 0);
233
      mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
234
      mMoveCenter.set( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
235
                       (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , 15 );
236
      mScaleCenter.set(factorCen,factorCen,factorCen);
237
      mMoveRegion.set( (width -mFactorObj*mObjWidth )/2 ,(height-mFactorObj*mObjHeight)/2 , 12 );
238

    
239
      int backgroundSize = mBackgroundTexture.getWidth();
240
      float factorBackX = ((float)width)/backgroundSize;
241
      float factorBackY = ((float)height)/backgroundSize;
242

    
243
      // quite tricky: move the background exactly to the FAR plane! (see DistortedOutputSurface.setProjection() )
244
      mMoveBackground.set( -width/2, -height/2, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
245
      mScaleBackground.set( 2*factorBackX, 2*factorBackY, 1.0f );
246

    
247
      mScreen.resize(width, height);
248
      }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
253
      {
254
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
255

    
256
      InputStream isB = act.getResources().openRawResource(R.raw.water);
257
      InputStream isC = act.getResources().openRawResource(R.raw.center);
258
      InputStream isR = act.getResources().openRawResource(R.raw.region);
259

    
260
      Bitmap bitmapB,bitmapC,bitmapR;
261
        
262
      try 
263
        {
264
        bitmapB = BitmapFactory.decodeStream(isB);
265
        bitmapC = BitmapFactory.decodeStream(isC);
266
        bitmapR = BitmapFactory.decodeStream(isR);
267
        }
268
      finally 
269
        {
270
        try 
271
          {
272
          isB.close();
273
          isC.close();
274
          isR.close();
275
          }
276
        catch(IOException e) { }
277
        }  
278
      
279
      mObjectTexture.setTexture( act.getBitmap() );
280
      mBackgroundTexture.setTexture(bitmapB);
281
      mCenterTexture.setTexture(bitmapC);
282
      mRegionTexture.setTexture(bitmapR);
283

    
284
      VertexEffectDeform.enable();
285
      VertexEffectDistort.enable();
286
      VertexEffectPinch.enable();
287
      VertexEffectSink.enable();
288
      VertexEffectSwirl.enable();
289
      VertexEffectWave.enable();
290

    
291
      FragmentEffectAlpha.enable();
292
      FragmentEffectBrightness.enable();
293
      FragmentEffectChroma.enable();
294
      FragmentEffectContrast.enable();
295
      FragmentEffectSaturation.enable();
296

    
297
      try
298
        {
299
        Distorted.onCreate(mView.getContext());
300
        }
301
      catch(Exception ex)
302
        {
303
        android.util.Log.e("Effects3D", ex.getMessage() );
304
        }
305
      }
306
}
(3-3/4)