Project

General

Profile

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

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

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

    
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29
import org.distorted.library.DistortedTexture;
30
import org.distorted.library.GridFlat;
31
import org.distorted.library.DistortedEffectQueues;
32
import org.distorted.library.GridObject;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.type.Dynamic3D;
35
import org.distorted.library.type.DynamicQuat;
36
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
class Effects3DRenderer implements GLSurfaceView.Renderer
48
{
49
    private GLSurfaceView mView;
50
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
51
    private DistortedEffectQueues mObjectQueues, mBackgroundQueues, mCenterQueues, mRegionQueues;
52
    private GridFlat mQuad;
53
    private GridObject mObjectGrid;
54
    private int mObjWidth, mObjHeight, mObjDepth;
55
    private DynamicQuat mQuatInt1, mQuatInt2;
56

    
57
    private Dynamic3D mCenterInter, mRegionInter;
58
    private Static3D mCenterPoint, mRegionPoint;
59
    private Dynamic3D mRegionScaleInter;
60
    private Static3D mRegionScalePoint;
61

    
62
    private float mFactorObj, mFactorReg;
63

    
64
    Static4D mQuat1, mQuat2;
65
    int mScreenMin;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
    Effects3DRenderer(GLSurfaceView v)
70
      {
71
      mView = v;
72

    
73
      Effects3DActivity act = (Effects3DActivity)v.getContext();
74

    
75
      mObjectTexture     = act.getTexture();
76
      mObjectGrid        = act.getGrid();
77
      mObjectQueues      = act.getQueues();
78
      mBackgroundTexture = new DistortedTexture(100, 100, 0);
79
      mCenterTexture     = new DistortedTexture(100, 100, 0);
80
      mRegionTexture     = new DistortedTexture(100, 100, 0);
81
      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
      mObjDepth = mObjectTexture.getDepth();
89

    
90
      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

    
99
      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

    
107
      mRegionScaleInter = new Dynamic3D();
108
      mRegionScalePoint = new Static3D(0,0,0);
109
      mRegionScaleInter.add(mRegionScalePoint);
110
      }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
    void setCenter(float x, float y, float z)
115
      {
116
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
117
      }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
    void setRegion(float x, float y, float r)
122
      {
123
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
124
      mRegionPoint.set(mFactorObj*x,mFactorObj*y,0);
125
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
126
      }
127

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

    
130
    public void onDrawFrame(GL10 glUnused)
131
      {
132
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
133

    
134
      long time = System.currentTimeMillis();
135

    
136
      mBackgroundQueues.draw(time,mBackgroundTexture,mQuad);
137
      mObjectQueues.draw(time,mObjectTexture,mObjectGrid);
138

    
139
      if( Effects3DActivity.supportsCenter() )
140
        {
141
        mCenterQueues.draw(time, mCenterTexture,mQuad);
142
        if( Effects3DActivity.supportsRegion() ) mRegionQueues.draw(time, mRegionTexture,mQuad);
143
        }
144
      }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
149
      {
150
      mScreenMin = width<height ? width:height;
151

    
152
      mObjectQueues.abortEffects(EffectTypes.MATRIX);
153
      mBackgroundQueues.abortEffects(EffectTypes.MATRIX);
154
      mCenterQueues.abortEffects(EffectTypes.MATRIX);
155
      mRegionQueues.abortEffects(EffectTypes.MATRIX);
156

    
157
      float factorCen;
158
      int centerSize = mCenterTexture.getWidth();
159
      int regionSize = mRegionTexture.getWidth();
160

    
161
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
162
        {
163
        mFactorObj = (0.80f*height)/mObjHeight;
164
        factorCen  = (0.08f*height)/centerSize;
165
        }
166
      else
167
        {
168
        mFactorObj = (0.80f*width)/mObjWidth;
169
        factorCen  = (0.08f*width)/centerSize;
170
        }
171

    
172
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
173
      mCenterPoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
174
      mRegionPoint.set(mFactorObj*act.getRegionX(),mFactorObj*act.getRegionY(),0);
175

    
176
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
177
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
178

    
179
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
180

    
181
      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

    
186
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
187

    
188
      mCenterQueues.quaternion(mQuatInt1, rotateCen);
189
      mCenterQueues.quaternion(mQuatInt2, rotateCen);
190

    
191
      mCenterQueues.move( new Static3D( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
192
                                  (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+10) );
193
      mCenterQueues.move(mCenterInter);
194
      mCenterQueues.scale(factorCen);
195

    
196
      mRegionQueues.quaternion(mQuatInt1, rotateCen);
197
      mRegionQueues.quaternion(mQuatInt2, rotateCen);
198

    
199
      mRegionQueues.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
200
                                  (height-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+12) );
201
      mRegionQueues.move(mCenterInter);
202
      mRegionQueues.move(mRegionInter);
203
      mRegionQueues.scale(mRegionScaleInter);
204
      mRegionQueues.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
205

    
206
      int backgroundSize = mBackgroundTexture.getWidth();
207
      float factorBackX = ((float)width)/backgroundSize;
208
      float factorBackY = ((float)height)/backgroundSize;
209

    
210
      mBackgroundQueues.move(new Static3D( -width/2, -height/2,-mFactorObj*(mObjWidth+mObjHeight)/2) );
211
      mBackgroundQueues.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
212

    
213
      Distorted.onSurfaceChanged(width, height);
214
      }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
219
      {
220
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
221

    
222
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
223

    
224
      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

    
228
      Bitmap bitmapB,bitmapC,bitmapR;
229
        
230
      try 
231
        {
232
        bitmapB = BitmapFactory.decodeStream(isB);
233
        bitmapC = BitmapFactory.decodeStream(isC);
234
        bitmapR = BitmapFactory.decodeStream(isR);
235
        }
236
      finally 
237
        {
238
        try 
239
          {
240
          isB.close();
241
          isC.close();
242
          isR.close();
243
          }
244
        catch(IOException e) { }
245
        }  
246
      
247
      mObjectTexture.setTexture( act.getBitmap() );
248
      mBackgroundTexture.setTexture(bitmapB);
249
      mCenterTexture.setTexture(bitmapC);
250
      mRegionTexture.setTexture(bitmapR);
251

    
252
      try
253
        {
254
        Distorted.onSurfaceCreated(mView.getContext());
255
        }
256
      catch(Exception ex)
257
        {
258
        android.util.Log.e("Effects3D", ex.getMessage() );
259
        }
260
      }
261
}
(3-3/4)