Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ 6637d0f2

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.GLES30;
25
import android.opengl.GLSurfaceView;
26

    
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29
import org.distorted.library.DistortedEffects;
30
import org.distorted.library.DistortedNode;
31
import org.distorted.library.DistortedScreen;
32
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.EffectNames;
34
import org.distorted.library.MeshFlat;
35
import org.distorted.library.MeshObject;
36
import org.distorted.library.EffectTypes;
37
import org.distorted.library.type.Dynamic3D;
38
import org.distorted.library.type.DynamicQuat;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41

    
42
import java.io.IOException;
43
import java.io.InputStream;
44

    
45
import javax.microedition.khronos.egl.EGLConfig;
46
import javax.microedition.khronos.opengles.GL10;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
class Effects3DRenderer implements GLSurfaceView.Renderer
51
{
52
    private static final float FOV = 70.0f;
53
    private static final float NEAR = 0.1f;
54

    
55
    private GLSurfaceView mView;
56
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
57
    private DistortedEffects mObjectEffects, mBackgroundEffects, mCenterEffects, mRegionEffects;
58
    private DistortedScreen mScreen;
59
    private DistortedNode mCenterNode, mRegionNode;
60
    private int mObjWidth, mObjHeight, mObjDepth;
61
    private DynamicQuat mQuatInt1, mQuatInt2;
62

    
63
    private Dynamic3D mCenterInter, mRegionInter;
64
    private Static3D mCenterPoint, mRegionPoint;
65
    private Dynamic3D mRegionScaleInter;
66
    private Static3D mRegionScalePoint;
67

    
68
    private float mFactorObj, mFactorReg;
69

    
70
    Static4D mQuat1, mQuat2;
71
    int mScreenMin;
72
    private boolean mShowing=false;
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
    Effects3DRenderer(GLSurfaceView v)
77
      {
78
      mView = v;
79

    
80
      Effects3DActivity act = (Effects3DActivity)v.getContext();
81

    
82
      mObjectTexture     = act.getTexture();
83
      mObjectEffects     = act.getEffects();
84
      mBackgroundTexture = new DistortedTexture(100,100);
85
      mCenterTexture     = new DistortedTexture(100,100);
86
      mRegionTexture     = new DistortedTexture(100,100);
87
      mBackgroundEffects = new DistortedEffects();
88
      mCenterEffects     = new DistortedEffects();
89
      mRegionEffects     = new DistortedEffects();
90

    
91
      MeshObject meshO   = act.getMesh();
92
      MeshFlat quad      = new MeshFlat(1,1);
93

    
94
      mObjWidth = mObjectTexture.getWidth();
95
      mObjHeight= mObjectTexture.getHeight();
96
      mObjDepth = mObjectTexture.getDepth(meshO);
97

    
98
      mQuat1 = new Static4D(0,0,0,1);  // unity
99
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
100

    
101
      mQuatInt1 = new DynamicQuat(0,0.5f);
102
      mQuatInt2 = new DynamicQuat(0,0.5f);
103

    
104
      mQuatInt1.add(mQuat1);
105
      mQuatInt2.add(mQuat2);
106

    
107
      mCenterInter= new Dynamic3D();
108
      mCenterPoint= new Static3D(0,0,0);
109
      mCenterInter.add(mCenterPoint);
110

    
111
      mRegionInter= new Dynamic3D();
112
      mRegionPoint= new Static3D(0,0,0);
113
      mRegionInter.add(mRegionPoint);
114

    
115
      mRegionScaleInter = new Dynamic3D();
116
      mRegionScalePoint = new Static3D(0,0,0);
117
      mRegionScaleInter.add(mRegionScalePoint);
118

    
119
      mCenterNode = new DistortedNode(mCenterTexture, mCenterEffects, quad);
120
      mRegionNode = new DistortedNode(mRegionTexture, mRegionEffects, quad);
121

    
122
      mScreen = new DistortedScreen();
123
      mScreen.setProjection(FOV, NEAR);
124
      mScreen.attach(mBackgroundTexture, mBackgroundEffects, quad );
125
      mScreen.attach(mObjectTexture    , mObjectEffects    , meshO);
126
      }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
    void showRegionAndCenter(boolean show)
131
      {
132
      if( mShowing!=show )
133
        {
134
        if( show )
135
          {
136
          mScreen.attach(mCenterNode);
137
          mScreen.attach(mRegionNode);
138
          }
139
        else
140
          {
141
          mScreen.detach(mCenterNode);
142
          mScreen.detach(mRegionNode);
143
          }
144

    
145
        mShowing = show;
146
        }
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
    void setCenter(float x, float y, float z)
152
      {
153
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
    void setRegion(float x, float y, float r)
159
      {
160
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
161
      mRegionPoint.set(mFactorObj*x,mFactorObj*y,0);
162
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
163
      }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166

    
167
    public void onDrawFrame(GL10 glUnused)
168
      {
169
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
170
      mScreen.render(System.currentTimeMillis());
171
      }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
176
      {
177
      mScreenMin = width<height ? width:height;
178

    
179
      mObjectEffects.abortEffects(EffectTypes.MATRIX);
180
      mBackgroundEffects.abortEffects(EffectTypes.MATRIX);
181
      mCenterEffects.abortEffects(EffectTypes.MATRIX);
182
      mRegionEffects.abortEffects(EffectTypes.MATRIX);
183

    
184
      float factorCen;
185
      int centerSize = mCenterTexture.getWidth();
186
      int regionSize = mRegionTexture.getWidth();
187

    
188
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
189
        {
190
        mFactorObj = (0.80f*height)/mObjHeight;
191
        factorCen  = (0.08f*height)/centerSize;
192
        }
193
      else
194
        {
195
        mFactorObj = (0.80f*width)/mObjWidth;
196
        factorCen  = (0.08f*width)/centerSize;
197
        }
198

    
199
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
200
      mCenterPoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
201
      mRegionPoint.set(mFactorObj*act.getRegionX(),mFactorObj*act.getRegionY(),0);
202

    
203
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
204
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
205

    
206
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
207

    
208
      mObjectEffects.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
209
      mObjectEffects.scale(mFactorObj);
210
      mObjectEffects.quaternion(mQuatInt1, rotateObj);
211
      mObjectEffects.quaternion(mQuatInt2, rotateObj);
212

    
213
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
214

    
215
      mCenterEffects.quaternion(mQuatInt1, rotateCen);
216
      mCenterEffects.quaternion(mQuatInt2, rotateCen);
217

    
218
      mCenterEffects.move( new Static3D( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
219
                                  (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+10) );
220
      mCenterEffects.move(mCenterInter);
221
      mCenterEffects.scale(factorCen);
222

    
223
      mRegionEffects.quaternion(mQuatInt1, rotateCen);
224
      mRegionEffects.quaternion(mQuatInt2, rotateCen);
225

    
226
      mRegionEffects.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
227
                                  (height-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+12) );
228
      mRegionEffects.move(mCenterInter);
229
      mRegionEffects.move(mRegionInter);
230
      mRegionEffects.scale(mRegionScaleInter);
231
      mRegionEffects.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
232

    
233
      int backgroundSize = mBackgroundTexture.getWidth();
234
      float factorBackX = ((float)width)/backgroundSize;
235
      float factorBackY = ((float)height)/backgroundSize;
236

    
237
      // quite tricky: move the background exactly to the FAR plane! (see DistortedOutputSurface.setProjection() )
238
      mBackgroundEffects.move(new Static3D( -width/2, -height/2, -height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360))) );
239
      mBackgroundEffects.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
240

    
241
      mScreen.resize(width, height);
242
      }
243

    
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

    
246
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
247
      {
248
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
249

    
250
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
251

    
252
      InputStream isB = act.getResources().openRawResource(R.raw.water);
253
      InputStream isC = act.getResources().openRawResource(R.raw.center);
254
      InputStream isR = act.getResources().openRawResource(R.raw.region);
255

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

    
280
      EffectTypes type;
281

    
282
      for( EffectNames name : EffectNames.values() )
283
        {
284
        type = name.getType();
285

    
286
        if( type== EffectTypes.VERTEX || type==EffectTypes.FRAGMENT )
287
          {
288
          DistortedEffects.enableEffect(name);
289
          }
290
        }
291

    
292
      try
293
        {
294
        Distorted.onCreate(mView.getContext());
295
        }
296
      catch(Exception ex)
297
        {
298
        android.util.Log.e("Effects3D", ex.getMessage() );
299
        }
300
      }
301
}
(3-3/4)