Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ 7451c98a

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