Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ 01782e85

1 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 76a81b6a Leszek Koltunski
package org.distorted.examples.effects3d;
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 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
28
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.main.DistortedNode;
30
import org.distorted.library.main.DistortedScreen;
31
import org.distorted.library.main.DistortedTexture;
32 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
33 01782e85 Leszek Koltunski
import org.distorted.library.main.MeshFlat;
34
import org.distorted.library.main.MeshObject;
35 08f92d82 Leszek Koltunski
import org.distorted.library.EffectTypes;
36 950511ed Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
37 833685d0 Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
38 08f92d82 Leszek Koltunski
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 76a81b6a Leszek Koltunski
class Effects3DRenderer implements GLSurfaceView.Renderer
50 08f92d82 Leszek Koltunski
{
51 af662543 leszek
    private static final float FOV = 70.0f;
52
    private static final float NEAR = 0.1f;
53
54 08f92d82 Leszek Koltunski
    private GLSurfaceView mView;
55 f6d884d5 Leszek Koltunski
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
56 d04a4886 Leszek Koltunski
    private DistortedEffects mObjectEffects, mBackgroundEffects, mCenterEffects, mRegionEffects;
57 d218d64e leszek
    private DistortedScreen mScreen;
58 fe59d375 Leszek Koltunski
    private DistortedNode mCenterNode, mRegionNode;
59 8ff32d4d Leszek Koltunski
    private int mObjWidth, mObjHeight, mObjDepth;
60 833685d0 Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
61 950511ed Leszek Koltunski
62 24991bc2 Leszek Koltunski
    private Dynamic3D mCenterInter, mRegionInter;
63
    private Static3D mCenterPoint, mRegionPoint;
64 6f779cd4 Leszek Koltunski
    private Dynamic3D mRegionScaleInter;
65
    private Static3D mRegionScalePoint;
66
67
    private float mFactorObj, mFactorReg;
68 950511ed Leszek Koltunski
69 833685d0 Leszek Koltunski
    Static4D mQuat1, mQuat2;
70 d2337a3a Leszek Koltunski
    int mScreenMin;
71 0ab55f0c Leszek Koltunski
    private boolean mShowingCenter=false;
72
    private boolean mShowingRegion=false;
73 833685d0 Leszek Koltunski
74 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76 4f8ca8ff Leszek Koltunski
    Effects3DRenderer(GLSurfaceView v)
77 08f92d82 Leszek Koltunski
      {
78
      mView = v;
79 d40cfeb2 Leszek Koltunski
80 e8b6aa95 Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)v.getContext();
81
82 f6d884d5 Leszek Koltunski
      mObjectTexture     = act.getTexture();
83 d04a4886 Leszek Koltunski
      mObjectEffects     = act.getEffects();
84 7451c98a Leszek Koltunski
      mBackgroundTexture = new DistortedTexture(100,100);
85
      mCenterTexture     = new DistortedTexture(100,100);
86
      mRegionTexture     = new DistortedTexture(100,100);
87 d04a4886 Leszek Koltunski
      mBackgroundEffects = new DistortedEffects();
88
      mCenterEffects     = new DistortedEffects();
89
      mRegionEffects     = new DistortedEffects();
90 f6d884d5 Leszek Koltunski
91 aac5c562 leszek
      MeshObject meshO   = act.getMesh();
92
      MeshFlat quad      = new MeshFlat(1,1);
93
94 f6d884d5 Leszek Koltunski
      mObjWidth = mObjectTexture.getWidth();
95
      mObjHeight= mObjectTexture.getHeight();
96 aac5c562 leszek
      mObjDepth = mObjectTexture.getDepth(meshO);
97 d40cfeb2 Leszek Koltunski
98 833685d0 Leszek Koltunski
      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 950511ed Leszek Koltunski
107 24991bc2 Leszek Koltunski
      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 6f779cd4 Leszek Koltunski
115
      mRegionScaleInter = new Dynamic3D();
116
      mRegionScalePoint = new Static3D(0,0,0);
117
      mRegionScaleInter.add(mRegionScalePoint);
118 392e16fd Leszek Koltunski
119 aac5c562 leszek
      mCenterNode = new DistortedNode(mCenterTexture, mCenterEffects, quad);
120
      mRegionNode = new DistortedNode(mRegionTexture, mRegionEffects, quad);
121 fe59d375 Leszek Koltunski
122 1f9a52f1 leszek
      mScreen = new DistortedScreen(mView);
123 af662543 leszek
      mScreen.setProjection(FOV, NEAR);
124 aac5c562 leszek
      mScreen.attach(mBackgroundTexture, mBackgroundEffects, quad );
125
      mScreen.attach(mObjectTexture    , mObjectEffects    , meshO);
126 f338550a leszek
      }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130 0ab55f0c Leszek Koltunski
    void showRegionAndCenter(boolean showRegion, boolean showCenter)
131 f338550a leszek
      {
132 0ab55f0c Leszek Koltunski
      if( mShowingCenter!=showCenter  )
133 98c04ab8 leszek
        {
134 0ab55f0c Leszek Koltunski
        if( showCenter ) mScreen.attach(mCenterNode);
135
        else             mScreen.detach(mCenterNode);
136
137
        mShowingCenter = showCenter;
138
        }
139
140
      if( mShowingRegion!=showRegion  )
141
        {
142
        if( showRegion ) mScreen.attach(mRegionNode);
143
        else             mScreen.detach(mRegionNode);
144
145
        mShowingRegion = showRegion;
146 98c04ab8 leszek
        }
147 950511ed Leszek Koltunski
      }
148
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
151 334c13fa Leszek Koltunski
    void setCenter(float x, float y, float z)
152 950511ed Leszek Koltunski
      {
153 334c13fa Leszek Koltunski
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
154 08f92d82 Leszek Koltunski
      }
155
156 6f779cd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
157
158 4f8ca8ff Leszek Koltunski
    void setRegion(float x, float y, float r)
159 6f779cd4 Leszek Koltunski
      {
160 f6d884d5 Leszek Koltunski
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
161 24991bc2 Leszek Koltunski
      mRegionPoint.set(mFactorObj*x,mFactorObj*y,0);
162 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
163 6f779cd4 Leszek Koltunski
      }
164
165 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
166 56cbe1cf Leszek Koltunski
167
    public void onDrawFrame(GL10 glUnused)
168 08f92d82 Leszek Koltunski
      {
169 fe59d375 Leszek Koltunski
      mScreen.render(System.currentTimeMillis());
170 08f92d82 Leszek Koltunski
      }
171
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173 56cbe1cf Leszek Koltunski
174
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
175 08f92d82 Leszek Koltunski
      {
176 833685d0 Leszek Koltunski
      mScreenMin = width<height ? width:height;
177 752c6b57 Leszek Koltunski
178
      mObjectEffects.abortEffects(EffectTypes.MATRIX);
179 fce25d04 leszek
      mBackgroundEffects.abortEffects(EffectTypes.MATRIX);
180
      mCenterEffects.abortEffects(EffectTypes.MATRIX);
181
      mRegionEffects.abortEffects(EffectTypes.MATRIX);
182 752c6b57 Leszek Koltunski
183 950511ed Leszek Koltunski
      float factorCen;
184 f6d884d5 Leszek Koltunski
      int centerSize = mCenterTexture.getWidth();
185
      int regionSize = mRegionTexture.getWidth();
186 6f779cd4 Leszek Koltunski
187 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
188 d40cfeb2 Leszek Koltunski
        {
189 950511ed Leszek Koltunski
        mFactorObj = (0.80f*height)/mObjHeight;
190
        factorCen  = (0.08f*height)/centerSize;
191 261fe5bd Leszek Koltunski
        }
192 d40cfeb2 Leszek Koltunski
      else
193
        {
194 950511ed Leszek Koltunski
        mFactorObj = (0.80f*width)/mObjWidth;
195
        factorCen  = (0.08f*width)/centerSize;
196 d40cfeb2 Leszek Koltunski
        }
197
198 76a81b6a Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
199 24991bc2 Leszek Koltunski
      mCenterPoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
200
      mRegionPoint.set(mFactorObj*act.getRegionX(),mFactorObj*act.getRegionY(),0);
201 9ae05f6c Leszek Koltunski
202 17600407 Leszek Koltunski
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
203 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
204 950511ed Leszek Koltunski
205 8a99c681 Leszek Koltunski
      Static3D rotateObj = new Static3D( (float)mObjWidth/2, (float)mObjHeight/2, -(float)mObjDepth/2 );
206 950511ed Leszek Koltunski
207 fce25d04 leszek
      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 950511ed Leszek Koltunski
212 fce25d04 leszek
      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 0ab55f0c Leszek Koltunski
                                  (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , 15) );
219 392e16fd Leszek Koltunski
      mCenterEffects.move(mCenterInter);
220 fce25d04 leszek
      mCenterEffects.scale(factorCen);
221 950511ed Leszek Koltunski
222 392e16fd Leszek Koltunski
      mRegionEffects.quaternion(mQuatInt1, rotateCen);
223
      mRegionEffects.quaternion(mQuatInt2, rotateCen);
224 6f779cd4 Leszek Koltunski
225 392e16fd Leszek Koltunski
      mRegionEffects.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
226 8a99c681 Leszek Koltunski
                                  (height-mFactorObj*mObjHeight)/2 , 12) );
227 392e16fd Leszek Koltunski
      mRegionEffects.move(mCenterInter);
228
      mRegionEffects.move(mRegionInter);
229
      mRegionEffects.scale(mRegionScaleInter);
230
      mRegionEffects.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
231 fce25d04 leszek
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 1bbe1af2 leszek
      mBackgroundEffects.move(new Static3D( -width/2, -height/2, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360))) );
238 fce25d04 leszek
      mBackgroundEffects.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
239
240 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
241 08f92d82 Leszek Koltunski
      }
242
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244 56cbe1cf Leszek Koltunski
245
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
246 08f92d82 Leszek Koltunski
      {
247 76a81b6a Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
248 bcc8e016 Leszek Koltunski
249 f6d884d5 Leszek Koltunski
      InputStream isB = act.getResources().openRawResource(R.raw.water);
250
      InputStream isC = act.getResources().openRawResource(R.raw.center);
251
      InputStream isR = act.getResources().openRawResource(R.raw.region);
252 71d8ad03 Leszek Koltunski
253 f6d884d5 Leszek Koltunski
      Bitmap bitmapB,bitmapC,bitmapR;
254 08f92d82 Leszek Koltunski
        
255
      try 
256
        {
257 f6d884d5 Leszek Koltunski
        bitmapB = BitmapFactory.decodeStream(isB);
258
        bitmapC = BitmapFactory.decodeStream(isC);
259
        bitmapR = BitmapFactory.decodeStream(isR);
260 71d8ad03 Leszek Koltunski
        }
261 08f92d82 Leszek Koltunski
      finally 
262
        {
263
        try 
264
          {
265 f6d884d5 Leszek Koltunski
          isB.close();
266
          isC.close();
267
          isR.close();
268 9167cfd4 Leszek Koltunski
          }
269 08f92d82 Leszek Koltunski
        catch(IOException e) { }
270
        }  
271
      
272 f6d884d5 Leszek Koltunski
      mObjectTexture.setTexture( act.getBitmap() );
273
      mBackgroundTexture.setTexture(bitmapB);
274
      mCenterTexture.setTexture(bitmapC);
275
      mRegionTexture.setTexture(bitmapR);
276 950511ed Leszek Koltunski
277 6637d0f2 Leszek Koltunski
      EffectTypes type;
278
279
      for( EffectNames name : EffectNames.values() )
280
        {
281
        type = name.getType();
282
283
        if( type== EffectTypes.VERTEX || type==EffectTypes.FRAGMENT )
284
          {
285
          DistortedEffects.enableEffect(name);
286
          }
287
        }
288
289 08f92d82 Leszek Koltunski
      try
290
        {
291 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
292 08f92d82 Leszek Koltunski
        }
293
      catch(Exception ex)
294
        {
295 76a81b6a Leszek Koltunski
        android.util.Log.e("Effects3D", ex.getMessage() );
296 08f92d82 Leszek Koltunski
        }
297
      }
298 56cbe1cf Leszek Koltunski
}