Project

General

Profile

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

examples / src / main / java / org / distorted / examples / vertex3d / Vertex3DRenderer.java @ 9ae05f6c

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.vertex3d;
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 Vertex3DRenderer 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;
53
    private DynamicQuat mQuatInt1, mQuatInt2;
54

    
55
    private Dynamic3D mMoveInter;
56
    private Static3D mMovePoint;
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
    public Vertex3DRenderer(GLSurfaceView v)
68
      {
69
      mView = v;
70

    
71
      mObject     = ((Vertex3DActivity)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

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

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

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

    
88
      mMoveInter= new Dynamic3D();
89
      mMovePoint= new Static3D(0,0,0);
90
      mMoveInter.add(mMovePoint);
91

    
92
      mRegionScaleInter = new Dynamic3D();
93
      mRegionScalePoint = new Static3D(0,0,0);
94
      mRegionScaleInter.add(mRegionScalePoint);
95
      }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
    public void setCenter(float x, float y)
100
      {
101
      mMovePoint.set(mFactorObj*x,mFactorObj*y,0);
102
      }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
    public void setRegion(float x, float y, float r)
107
      {
108
      mFactorReg = mFactorObj*r/mRegion.getWidth();
109
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
110
      }
111

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

    
114
    public void onDrawFrame(GL10 glUnused)
115
      {
116
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
117
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
118

    
119
      long time = System.currentTimeMillis();
120

    
121
      mBackground.draw(time);
122
      mObject.draw(time);
123
      mCenter.draw(time);
124
      mRegion.draw(time);
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
130
      {
131
      mScreenMin = width<height ? width:height;
132

    
133
      mObject.abortEffects(EffectTypes.MATRIX);
134
      mBackground.abortEffects(EffectTypes.MATRIX);
135
      mCenter.abortEffects(EffectTypes.MATRIX);
136
      mRegion.abortEffects(EffectTypes.MATRIX);
137

    
138
      float factorCen;
139
      int centerSize = mCenter.getWidth();
140
      int regionSize = mRegion.getWidth();
141

    
142
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
143
        {
144
        mFactorObj = (0.80f*height)/mObjHeight;
145
        factorCen  = (0.08f*height)/centerSize;
146
        }
147
      else
148
        {
149
        mFactorObj = (0.80f*width)/mObjWidth;
150
        factorCen  = (0.08f*width)/centerSize;
151
        }
152

    
153
      Vertex3DActivity act = (Vertex3DActivity)mView.getContext();
154
      mMovePoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
155

    
156
      mFactorReg = mFactorObj*act.getRegionR()/regionSize;
157
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
158

    
159
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
160

    
161
      mObject.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
162
      mObject.scale(mFactorObj);
163
      mObject.quaternion(mQuatInt1, rotateObj);
164
      mObject.quaternion(mQuatInt2, rotateObj);
165

    
166
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
167

    
168
      mCenter.quaternion(mQuatInt1, rotateCen);
169
      mCenter.quaternion(mQuatInt2, rotateCen);
170

    
171
      mCenter.move( new Static3D( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
172
                                  (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , 10) );
173
      mCenter.move(mMoveInter);
174
      mCenter.scale(factorCen);
175

    
176
      mRegion.quaternion(mQuatInt1, rotateCen);
177
      mRegion.quaternion(mQuatInt2, rotateCen);
178

    
179
      mRegion.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
180
                                  (height-mFactorObj*mObjHeight)/2 , 12) );
181
      mRegion.move(mMoveInter);
182
      mRegion.scale(mRegionScaleInter);
183
      mRegion.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
184

    
185
      int backgroundSize = mBackground.getWidth();
186
      float factorBackX = ((float)width)/backgroundSize;
187
      float factorBackY = ((float)height)/backgroundSize;
188

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

    
192
      Distorted.onSurfaceChanged(width, height);
193
      }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
198
      {
199
      Vertex3DActivity act = (Vertex3DActivity)mView.getContext();
200

    
201
      InputStream is1 = act.getResources().openRawResource(act.getBitmap());
202
      InputStream is2 = act.getResources().openRawResource(R.raw.water);
203
      InputStream is3 = act.getResources().openRawResource(R.raw.center);
204
      InputStream is4 = act.getResources().openRawResource(R.raw.region);
205

    
206
      Bitmap bitmap1,bitmap2,bitmap3,bitmap4;
207
        
208
      try 
209
        {
210
        bitmap1 = BitmapFactory.decodeStream(is1);
211
        bitmap2 = BitmapFactory.decodeStream(is2);
212
        bitmap3 = BitmapFactory.decodeStream(is3);
213
        bitmap4 = BitmapFactory.decodeStream(is4);
214
        }
215
      finally 
216
        {
217
        try 
218
          {
219
          is1.close();
220
          is2.close();
221
          is3.close();
222
          is4.close();
223
          } 
224
        catch(IOException e) { }
225
        }  
226
      
227
      mObject.setBitmap(bitmap1);
228
      mBackground.setBitmap(bitmap2);
229
      mCenter.setBitmap(bitmap3);
230
      mRegion.setBitmap(bitmap4);
231

    
232
      try
233
        {
234
        Distorted.onSurfaceCreated(mView.getContext());
235
        }
236
      catch(Exception ex)
237
        {
238
        android.util.Log.e("Vertex3D", ex.getMessage() );
239
        }
240
      }
241
}
(3-3/4)