Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ 10b7e588

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

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

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

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
class Effects3DRenderer implements GLSurfaceView.Renderer
47
{
48
    private GLSurfaceView mView;
49
    private DistortedObject mObject;
50
    private DistortedObject mBackground;
51
    private DistortedObject mCenter;
52
    private DistortedObject mRegion;
53
    private GridFlat mQuad;
54
    private GridObject mGrid;
55
    private int mObjWidth, mObjHeight, mObjDepth;
56
    private DynamicQuat mQuatInt1, mQuatInt2;
57

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

    
63
    private float mFactorObj, mFactorReg;
64

    
65
    Static4D mQuat1, mQuat2;
66
    int mScreenMin;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

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

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

    
76
      mObject     = act.getObject();
77
      mGrid       = act.getGrid();
78
      mBackground = new DistortedObject(100, 100, 1);
79
      mCenter     = new DistortedObject(100, 100, 1);
80
      mRegion     = new DistortedObject(100, 100, 1);
81
      mQuad       = new GridFlat(1,1);
82

    
83
      mObjWidth = mObject.getWidth();
84
      mObjHeight= mObject.getHeight();
85
      mObjDepth = mObject.getDepth();
86

    
87
      mQuat1 = new Static4D(0,0,0,1);  // unity
88
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
89

    
90
      mQuatInt1 = new DynamicQuat(0,0.5f);
91
      mQuatInt2 = new DynamicQuat(0,0.5f);
92

    
93
      mQuatInt1.add(mQuat1);
94
      mQuatInt2.add(mQuat2);
95

    
96
      mCenterInter= new Dynamic3D();
97
      mCenterPoint= new Static3D(0,0,0);
98
      mCenterInter.add(mCenterPoint);
99

    
100
      mRegionInter= new Dynamic3D();
101
      mRegionPoint= new Static3D(0,0,0);
102
      mRegionInter.add(mRegionPoint);
103

    
104
      mRegionScaleInter = new Dynamic3D();
105
      mRegionScalePoint = new Static3D(0,0,0);
106
      mRegionScaleInter.add(mRegionScalePoint);
107
      }
108

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

    
111
    void setCenter(float x, float y, float z)
112
      {
113
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
    void setRegion(float x, float y, float r)
119
      {
120
      mFactorReg = 2*mFactorObj*r/mRegion.getWidth();
121
      mRegionPoint.set(mFactorObj*x,mFactorObj*y,0);
122
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
123
      }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
    public void onDrawFrame(GL10 glUnused)
128
      {
129
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
130

    
131
      long time = System.currentTimeMillis();
132

    
133
      mBackground.draw(time,mQuad);
134
      mObject.draw(time,mGrid);
135

    
136
      if( Effects3DActivity.supportsCenter() )
137
        {
138
        mCenter.draw(time, mQuad);
139
        if( Effects3DActivity.supportsRegion() ) mRegion.draw(time, mQuad);
140
        }
141
      }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
146
      {
147
      mScreenMin = width<height ? width:height;
148

    
149
      mObject.abortEffects(EffectTypes.MATRIX);
150
      mBackground.abortEffects(EffectTypes.MATRIX);
151
      mCenter.abortEffects(EffectTypes.MATRIX);
152
      mRegion.abortEffects(EffectTypes.MATRIX);
153

    
154
      float factorCen;
155
      int centerSize = mCenter.getWidth();
156
      int regionSize = mRegion.getWidth();
157

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

    
169
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
170
      mCenterPoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
171
      mRegionPoint.set(mFactorObj*act.getRegionX(),mFactorObj*act.getRegionY(),0);
172

    
173
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
174
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
175

    
176
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
177

    
178
      mObject.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
179
      mObject.scale(mFactorObj);
180
      mObject.quaternion(mQuatInt1, rotateObj);
181
      mObject.quaternion(mQuatInt2, rotateObj);
182

    
183
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
184

    
185
      mCenter.quaternion(mQuatInt1, rotateCen);
186
      mCenter.quaternion(mQuatInt2, rotateCen);
187

    
188
      mCenter.move( new Static3D( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
189
                                  (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+10) );
190
      mCenter.move(mCenterInter);
191
      mCenter.scale(factorCen);
192

    
193
      mRegion.quaternion(mQuatInt1, rotateCen);
194
      mRegion.quaternion(mQuatInt2, rotateCen);
195

    
196
      mRegion.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
197
                                  (height-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+12) );
198
      mRegion.move(mCenterInter);
199
      mRegion.move(mRegionInter);
200
      mRegion.scale(mRegionScaleInter);
201
      mRegion.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
202

    
203
      int backgroundSize = mBackground.getWidth();
204
      float factorBackX = ((float)width)/backgroundSize;
205
      float factorBackY = ((float)height)/backgroundSize;
206

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

    
210
      Distorted.onSurfaceChanged(width, height);
211
      }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
216
      {
217
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
218

    
219
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
220

    
221
      InputStream is1 = act.getResources().openRawResource(R.raw.water);
222
      InputStream is2 = act.getResources().openRawResource(R.raw.center);
223
      InputStream is3 = act.getResources().openRawResource(R.raw.region);
224

    
225
      Bitmap bitmap1,bitmap2,bitmap3;
226
        
227
      try 
228
        {
229
        bitmap1 = BitmapFactory.decodeStream(is1);
230
        bitmap2 = BitmapFactory.decodeStream(is2);
231
        bitmap3 = BitmapFactory.decodeStream(is3);
232
        }
233
      finally 
234
        {
235
        try 
236
          {
237
          is1.close();
238
          is2.close();
239
          is3.close();
240
          }
241
        catch(IOException e) { }
242
        }  
243
      
244
      mObject.setTexture( act.getBitmap() );
245
      mBackground.setTexture(bitmap1);
246
      mCenter.setTexture(bitmap2);
247
      mRegion.setTexture(bitmap3);
248

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