Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ 4f8ca8ff

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.DistortedBitmap;
30
import org.distorted.library.DistortedObject;
31
import org.distorted.library.EffectTypes;
32
import org.distorted.library.type.Dynamic3D;
33
import org.distorted.library.type.DynamicQuat;
34
import org.distorted.library.type.Static3D;
35
import org.distorted.library.type.Static4D;
36

    
37
import java.io.IOException;
38
import java.io.InputStream;
39

    
40
import javax.microedition.khronos.egl.EGLConfig;
41
import javax.microedition.khronos.opengles.GL10;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
class Effects3DRenderer implements GLSurfaceView.Renderer
46
{
47
    private GLSurfaceView mView;
48
    private DistortedObject mObject;
49
    private DistortedBitmap mBackground;
50
    private DistortedBitmap mCenter;
51
    private DistortedBitmap mRegion;
52
    private int mObjWidth, mObjHeight, mObjDepth;
53
    private DynamicQuat mQuatInt1, mQuatInt2;
54

    
55
    private Dynamic3D mCenterInter, mRegionInter;
56
    private Static3D mCenterPoint, mRegionPoint;
57
    private Dynamic3D mRegionScaleInter;
58
    private Static3D mRegionScalePoint;
59

    
60
    private float mFactorObj, mFactorReg;
61

    
62
    Static4D mQuat1, mQuat2;
63
    int mScreenMin;
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
    Effects3DRenderer(GLSurfaceView v)
68
      {
69
      mView = v;
70

    
71
      mObject     = ((Effects3DActivity)v.getContext()).getObject();
72
      mBackground = new DistortedBitmap(100, 100, 1);
73
      mCenter     = new DistortedBitmap(100, 100, 1);
74
      mRegion     = new DistortedBitmap(100, 100, 1);
75

    
76
      mObjWidth = mObject.getWidth();
77
      mObjHeight= mObject.getHeight();
78
      mObjDepth = mObject.getDepth();
79

    
80
      mQuat1 = new Static4D(0,0,0,1);  // unity
81
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
82

    
83
      mQuatInt1 = new DynamicQuat(0,0.5f);
84
      mQuatInt2 = new DynamicQuat(0,0.5f);
85

    
86
      mQuatInt1.add(mQuat1);
87
      mQuatInt2.add(mQuat2);
88

    
89
      mCenterInter= new Dynamic3D();
90
      mCenterPoint= new Static3D(0,0,0);
91
      mCenterInter.add(mCenterPoint);
92

    
93
      mRegionInter= new Dynamic3D();
94
      mRegionPoint= new Static3D(0,0,0);
95
      mRegionInter.add(mRegionPoint);
96

    
97
      mRegionScaleInter = new Dynamic3D();
98
      mRegionScalePoint = new Static3D(0,0,0);
99
      mRegionScaleInter.add(mRegionScalePoint);
100
      }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
    void setCenter(float x, float y)
105
      {
106
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,0);
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
    void setRegion(float x, float y, float r)
112
      {
113
      mFactorReg = 2*mFactorObj*r/mRegion.getWidth();
114
      mRegionPoint.set(mFactorObj*x,mFactorObj*y,0);
115
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
116
      }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
    public void onDrawFrame(GL10 glUnused)
121
      {
122
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
123
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
124

    
125
      long time = System.currentTimeMillis();
126

    
127
      mBackground.draw(time);
128
      mObject.draw(time);
129

    
130
      if( Effects3DActivity.isTypeVertex() )
131
        {
132
        mCenter.draw(time);
133
        if( Effects3DActivity.supportsRegion() ) mRegion.draw(time);
134
        }
135
      }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
140
      {
141
      mScreenMin = width<height ? width:height;
142

    
143
      mObject.abortEffects(EffectTypes.MATRIX);
144
      mBackground.abortEffects(EffectTypes.MATRIX);
145
      mCenter.abortEffects(EffectTypes.MATRIX);
146
      mRegion.abortEffects(EffectTypes.MATRIX);
147

    
148
      float factorCen;
149
      int centerSize = mCenter.getWidth();
150
      int regionSize = mRegion.getWidth();
151

    
152
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
153
        {
154
        mFactorObj = (0.80f*height)/mObjHeight;
155
        factorCen  = (0.08f*height)/centerSize;
156
        }
157
      else
158
        {
159
        mFactorObj = (0.80f*width)/mObjWidth;
160
        factorCen  = (0.08f*width)/centerSize;
161
        }
162

    
163
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
164
      mCenterPoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
165
      mRegionPoint.set(mFactorObj*act.getRegionX(),mFactorObj*act.getRegionY(),0);
166

    
167
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
168
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
169

    
170
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
171

    
172
      mObject.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
173
      mObject.scale(mFactorObj);
174
      mObject.quaternion(mQuatInt1, rotateObj);
175
      mObject.quaternion(mQuatInt2, rotateObj);
176

    
177
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
178

    
179
      mCenter.quaternion(mQuatInt1, rotateCen);
180
      mCenter.quaternion(mQuatInt2, rotateCen);
181

    
182
      mCenter.move( new Static3D( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
183
                                  (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+10) );
184
      mCenter.move(mCenterInter);
185
      mCenter.scale(factorCen);
186

    
187
      mRegion.quaternion(mQuatInt1, rotateCen);
188
      mRegion.quaternion(mQuatInt2, rotateCen);
189

    
190
      mRegion.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
191
                                  (height-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+12) );
192
      mRegion.move(mCenterInter);
193
      mRegion.move(mRegionInter);
194
      mRegion.scale(mRegionScaleInter);
195
      mRegion.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
196

    
197
      int backgroundSize = mBackground.getWidth();
198
      float factorBackX = ((float)width)/backgroundSize;
199
      float factorBackY = ((float)height)/backgroundSize;
200

    
201
      mBackground.move(new Static3D( -width/2, -height/2,-mFactorObj*(mObjWidth+mObjHeight)/2) );
202
      mBackground.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
203

    
204
      Distorted.onSurfaceChanged(width, height);
205
      }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
210
      {
211
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
212

    
213
      InputStream is1 = act.getResources().openRawResource(R.raw.water);
214
      InputStream is2 = act.getResources().openRawResource(R.raw.center);
215
      InputStream is3 = act.getResources().openRawResource(R.raw.region);
216

    
217
      Bitmap bitmap1,bitmap2,bitmap3;
218
        
219
      try 
220
        {
221
        bitmap1 = BitmapFactory.decodeStream(is1);
222
        bitmap2 = BitmapFactory.decodeStream(is2);
223
        bitmap3 = BitmapFactory.decodeStream(is3);
224
        }
225
      finally 
226
        {
227
        try 
228
          {
229
          is1.close();
230
          is2.close();
231
          is3.close();
232
          }
233
        catch(IOException e) { }
234
        }  
235
      
236
      mObject.setBitmap( act.getBitmap() );
237
      mBackground.setBitmap(bitmap1);
238
      mCenter.setBitmap(bitmap2);
239
      mRegion.setBitmap(bitmap3);
240

    
241
      try
242
        {
243
        Distorted.onSurfaceCreated(mView.getContext());
244
        }
245
      catch(Exception ex)
246
        {
247
        android.util.Log.e("Effects3D", ex.getMessage() );
248
        }
249
      }
250
}
(3-3/4)