Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ af662543

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.MeshFlat;
34
import org.distorted.library.MeshObject;
35
import org.distorted.library.EffectTypes;
36
import org.distorted.library.type.Dynamic3D;
37
import org.distorted.library.type.DynamicQuat;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40

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

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

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

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

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

    
67
    private float mFactorObj, mFactorReg;
68

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

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

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

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

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

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

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

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

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

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

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

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

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

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

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

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

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

    
144
        mShowing = show;
145
        }
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

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

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

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

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

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

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

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

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

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

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

    
279
      try
280
        {
281
        Distorted.onCreate(mView.getContext());
282
        }
283
      catch(Exception ex)
284
        {
285
        android.util.Log.e("Effects3D", ex.getMessage() );
286
        }
287
      }
288
}
(3-3/4)