Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ 76f9798b

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.GLES20;
25
import android.opengl.GLSurfaceView;
26
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
30 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
31 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
32 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
33
import org.distorted.library.GridObject;
34 08f92d82 Leszek Koltunski
import org.distorted.library.EffectTypes;
35 950511ed Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
36 833685d0 Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
37 08f92d82 Leszek Koltunski
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39
40
import java.io.IOException;
41
import java.io.InputStream;
42
43
import javax.microedition.khronos.egl.EGLConfig;
44
import javax.microedition.khronos.opengles.GL10;
45
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48 76a81b6a Leszek Koltunski
class Effects3DRenderer implements GLSurfaceView.Renderer
49 08f92d82 Leszek Koltunski
{
50
    private GLSurfaceView mView;
51 f6d884d5 Leszek Koltunski
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
52 d04a4886 Leszek Koltunski
    private DistortedEffects mObjectEffects, mBackgroundEffects, mCenterEffects, mRegionEffects;
53 392e16fd Leszek Koltunski
    private DistortedFramebuffer mScreen;
54 10b7e588 Leszek Koltunski
    private GridFlat mQuad;
55 f6d884d5 Leszek Koltunski
    private GridObject mObjectGrid;
56 8ff32d4d Leszek Koltunski
    private int mObjWidth, mObjHeight, mObjDepth;
57 833685d0 Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
58 950511ed Leszek Koltunski
59 24991bc2 Leszek Koltunski
    private Dynamic3D mCenterInter, mRegionInter;
60
    private Static3D mCenterPoint, mRegionPoint;
61 6f779cd4 Leszek Koltunski
    private Dynamic3D mRegionScaleInter;
62
    private Static3D mRegionScalePoint;
63
64
    private float mFactorObj, mFactorReg;
65 950511ed Leszek Koltunski
66 833685d0 Leszek Koltunski
    Static4D mQuat1, mQuat2;
67 d2337a3a Leszek Koltunski
    int mScreenMin;
68 833685d0 Leszek Koltunski
69 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71 4f8ca8ff Leszek Koltunski
    Effects3DRenderer(GLSurfaceView v)
72 08f92d82 Leszek Koltunski
      {
73
      mView = v;
74 d40cfeb2 Leszek Koltunski
75 e8b6aa95 Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)v.getContext();
76
77 f6d884d5 Leszek Koltunski
      mObjectTexture     = act.getTexture();
78
      mObjectGrid        = act.getGrid();
79 d04a4886 Leszek Koltunski
      mObjectEffects     = act.getEffects();
80 7451c98a Leszek Koltunski
      mBackgroundTexture = new DistortedTexture(100,100);
81
      mCenterTexture     = new DistortedTexture(100,100);
82
      mRegionTexture     = new DistortedTexture(100,100);
83 f6d884d5 Leszek Koltunski
      mQuad              = new GridFlat(1,1);
84 d04a4886 Leszek Koltunski
      mBackgroundEffects = new DistortedEffects();
85
      mCenterEffects     = new DistortedEffects();
86
      mRegionEffects     = new DistortedEffects();
87 f6d884d5 Leszek Koltunski
88
      mObjWidth = mObjectTexture.getWidth();
89
      mObjHeight= mObjectTexture.getHeight();
90 7451c98a Leszek Koltunski
      mObjDepth = mObjectTexture.getDepth(mObjectGrid);
91 d40cfeb2 Leszek Koltunski
92 833685d0 Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
93
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
94
95
      mQuatInt1 = new DynamicQuat(0,0.5f);
96
      mQuatInt2 = new DynamicQuat(0,0.5f);
97
98
      mQuatInt1.add(mQuat1);
99
      mQuatInt2.add(mQuat2);
100 950511ed Leszek Koltunski
101 24991bc2 Leszek Koltunski
      mCenterInter= new Dynamic3D();
102
      mCenterPoint= new Static3D(0,0,0);
103
      mCenterInter.add(mCenterPoint);
104
105
      mRegionInter= new Dynamic3D();
106
      mRegionPoint= new Static3D(0,0,0);
107
      mRegionInter.add(mRegionPoint);
108 6f779cd4 Leszek Koltunski
109
      mRegionScaleInter = new Dynamic3D();
110
      mRegionScalePoint = new Static3D(0,0,0);
111
      mRegionScaleInter.add(mRegionScalePoint);
112 392e16fd Leszek Koltunski
113
      mScreen = new DistortedFramebuffer(0);
114 950511ed Leszek Koltunski
      }
115
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
118 334c13fa Leszek Koltunski
    void setCenter(float x, float y, float z)
119 950511ed Leszek Koltunski
      {
120 334c13fa Leszek Koltunski
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
121 08f92d82 Leszek Koltunski
      }
122
123 6f779cd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
124
125 4f8ca8ff Leszek Koltunski
    void setRegion(float x, float y, float r)
126 6f779cd4 Leszek Koltunski
      {
127 f6d884d5 Leszek Koltunski
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
128 24991bc2 Leszek Koltunski
      mRegionPoint.set(mFactorObj*x,mFactorObj*y,0);
129 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
130 6f779cd4 Leszek Koltunski
      }
131
132 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
133 56cbe1cf Leszek Koltunski
134
    public void onDrawFrame(GL10 glUnused)
135 08f92d82 Leszek Koltunski
      {
136
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
137 71d8ad03 Leszek Koltunski
138
      long time = System.currentTimeMillis();
139
140 bc29e409 Leszek Koltunski
      mScreen.renderTo(mBackgroundTexture,mQuad      ,mBackgroundEffects,time);
141
      mScreen.renderTo(mObjectTexture    ,mObjectGrid,mObjectEffects    ,time);
142 24991bc2 Leszek Koltunski
143 4d5b37fe Leszek Koltunski
      if( Effects3DActivity.supportsCenter() )
144 76a81b6a Leszek Koltunski
        {
145 bc29e409 Leszek Koltunski
        mScreen.renderTo(mCenterTexture,mQuad,mCenterEffects, time);
146
        if( Effects3DActivity.supportsRegion() ) mScreen.renderTo(mRegionTexture,mQuad,mRegionEffects,time);
147 76a81b6a Leszek Koltunski
        }
148 08f92d82 Leszek Koltunski
      }
149
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151 56cbe1cf Leszek Koltunski
152
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
153 08f92d82 Leszek Koltunski
      {
154 833685d0 Leszek Koltunski
      mScreenMin = width<height ? width:height;
155
156 392e16fd Leszek Koltunski
      mObjectEffects.abortEffects(EffectTypes.MATRIX);
157
      mBackgroundEffects.abortEffects(EffectTypes.MATRIX);
158
      mCenterEffects.abortEffects(EffectTypes.MATRIX);
159
      mRegionEffects.abortEffects(EffectTypes.MATRIX);
160 950511ed Leszek Koltunski
161
      float factorCen;
162 f6d884d5 Leszek Koltunski
      int centerSize = mCenterTexture.getWidth();
163
      int regionSize = mRegionTexture.getWidth();
164 6f779cd4 Leszek Koltunski
165 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
166 d40cfeb2 Leszek Koltunski
        {
167 950511ed Leszek Koltunski
        mFactorObj = (0.80f*height)/mObjHeight;
168
        factorCen  = (0.08f*height)/centerSize;
169 261fe5bd Leszek Koltunski
        }
170 d40cfeb2 Leszek Koltunski
      else
171
        {
172 950511ed Leszek Koltunski
        mFactorObj = (0.80f*width)/mObjWidth;
173
        factorCen  = (0.08f*width)/centerSize;
174 d40cfeb2 Leszek Koltunski
        }
175
176 76a81b6a Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
177 24991bc2 Leszek Koltunski
      mCenterPoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
178
      mRegionPoint.set(mFactorObj*act.getRegionX(),mFactorObj*act.getRegionY(),0);
179 9ae05f6c Leszek Koltunski
180 17600407 Leszek Koltunski
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
181 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
182 950511ed Leszek Koltunski
183 2bf60c29 Leszek Koltunski
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
184
185 392e16fd Leszek Koltunski
      mObjectEffects.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
186
      mObjectEffects.scale(mFactorObj);
187
      mObjectEffects.quaternion(mQuatInt1, rotateObj);
188
      mObjectEffects.quaternion(mQuatInt2, rotateObj);
189 833685d0 Leszek Koltunski
190 950511ed Leszek Koltunski
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
191
192 392e16fd Leszek Koltunski
      mCenterEffects.quaternion(mQuatInt1, rotateCen);
193
      mCenterEffects.quaternion(mQuatInt2, rotateCen);
194 950511ed Leszek Koltunski
195 392e16fd Leszek Koltunski
      mCenterEffects.move( new Static3D( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
196 8ff32d4d Leszek Koltunski
                                  (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+10) );
197 392e16fd Leszek Koltunski
      mCenterEffects.move(mCenterInter);
198
      mCenterEffects.scale(factorCen);
199 950511ed Leszek Koltunski
200 392e16fd Leszek Koltunski
      mRegionEffects.quaternion(mQuatInt1, rotateCen);
201
      mRegionEffects.quaternion(mQuatInt2, rotateCen);
202 6f779cd4 Leszek Koltunski
203 392e16fd Leszek Koltunski
      mRegionEffects.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
204 8ff32d4d Leszek Koltunski
                                  (height-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+12) );
205 392e16fd Leszek Koltunski
      mRegionEffects.move(mCenterInter);
206
      mRegionEffects.move(mRegionInter);
207
      mRegionEffects.scale(mRegionScaleInter);
208
      mRegionEffects.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
209 6f779cd4 Leszek Koltunski
210 f6d884d5 Leszek Koltunski
      int backgroundSize = mBackgroundTexture.getWidth();
211 56cbe1cf Leszek Koltunski
      float factorBackX = ((float)width)/backgroundSize;
212
      float factorBackY = ((float)height)/backgroundSize;
213 833685d0 Leszek Koltunski
214 392e16fd Leszek Koltunski
      mBackgroundEffects.move(new Static3D( -width/2, -height/2,-mFactorObj*(mObjWidth+mObjHeight)/2) );
215
      mBackgroundEffects.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
216 71d8ad03 Leszek Koltunski
217 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
218 08f92d82 Leszek Koltunski
      }
219
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221 56cbe1cf Leszek Koltunski
222
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
223 08f92d82 Leszek Koltunski
      {
224 e7a4ef16 Leszek Koltunski
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
225
226 76a81b6a Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
227 bcc8e016 Leszek Koltunski
228 f6d884d5 Leszek Koltunski
      InputStream isB = act.getResources().openRawResource(R.raw.water);
229
      InputStream isC = act.getResources().openRawResource(R.raw.center);
230
      InputStream isR = act.getResources().openRawResource(R.raw.region);
231 71d8ad03 Leszek Koltunski
232 f6d884d5 Leszek Koltunski
      Bitmap bitmapB,bitmapC,bitmapR;
233 08f92d82 Leszek Koltunski
        
234
      try 
235
        {
236 f6d884d5 Leszek Koltunski
        bitmapB = BitmapFactory.decodeStream(isB);
237
        bitmapC = BitmapFactory.decodeStream(isC);
238
        bitmapR = BitmapFactory.decodeStream(isR);
239 71d8ad03 Leszek Koltunski
        }
240 08f92d82 Leszek Koltunski
      finally 
241
        {
242
        try 
243
          {
244 f6d884d5 Leszek Koltunski
          isB.close();
245
          isC.close();
246
          isR.close();
247 9167cfd4 Leszek Koltunski
          }
248 08f92d82 Leszek Koltunski
        catch(IOException e) { }
249
        }  
250
      
251 f6d884d5 Leszek Koltunski
      mObjectTexture.setTexture( act.getBitmap() );
252
      mBackgroundTexture.setTexture(bitmapB);
253
      mCenterTexture.setTexture(bitmapC);
254
      mRegionTexture.setTexture(bitmapR);
255 950511ed Leszek Koltunski
256 08f92d82 Leszek Koltunski
      try
257
        {
258 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
259 08f92d82 Leszek Koltunski
        }
260
      catch(Exception ex)
261
        {
262 76a81b6a Leszek Koltunski
        android.util.Log.e("Effects3D", ex.getMessage() );
263 08f92d82 Leszek Koltunski
        }
264
      }
265 56cbe1cf Leszek Koltunski
}