Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fragment3d / Fragment3DRenderer.java @ 7e9be46d

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.fragment3d;
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.DistortedCubes;
31
import org.distorted.library.DistortedObject;
32
import org.distorted.library.EffectNames;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Dynamic2D;
36
import org.distorted.library.type.Dynamic3D;
37
import org.distorted.library.type.DynamicQuat;
38
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static2D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42

    
43
import java.io.IOException;
44
import java.io.InputStream;
45

    
46
import javax.microedition.khronos.egl.EGLConfig;
47
import javax.microedition.khronos.opengles.GL10;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
class Fragment3DRenderer implements GLSurfaceView.Renderer
52
{
53
    private static final int SIZE = 100;
54

    
55
    private GLSurfaceView mView;
56
    private DistortedObject mObject;
57
    private DistortedBitmap mCenter;
58
    private float mFactorCen, mFactorObj;
59

    
60
    private int mObjWidth, mObjHeight;
61

    
62
    private DynamicQuat mQuatInt1, mQuatInt2;
63

    
64
    private Dynamic3D mMoveInter;
65
    private Static3D mMovePoint;
66

    
67
    Static4D mQuat1, mQuat2;
68
    int mScreenMin;
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
    public void setCenter(float x, float y)
73
      {
74
      x = (0.012f*x-0.1f)*mObjWidth;
75
      y = (0.012f*y-0.1f)*mObjHeight;
76

    
77
      mMovePoint.set(mFactorObj*x,mFactorObj*y,0);
78
      }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
    public Fragment3DRenderer(GLSurfaceView v)
83
      {
84
      mView = v;
85

    
86
      mObject = ((Fragment3DActivity)v.getContext()).getObject();
87
      mCenter = new DistortedBitmap(SIZE, SIZE, 1);
88

    
89
      mObjWidth = mObject.getWidth();
90
      mObjHeight= mObject.getHeight();
91

    
92
      mMovePoint= new Static3D(0,0,0);
93
      mMoveInter= new Dynamic3D();
94
      mMoveInter.add(mMovePoint);
95

    
96
      mQuat1 = new Static4D(0,0,0,1);  // unity
97
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
98

    
99
      mQuatInt1 = new DynamicQuat(0,0.5f);
100
      mQuatInt2 = new DynamicQuat(0,0.5f);
101

    
102
      mQuatInt1.add(mQuat1);
103
      mQuatInt2.add(mQuat2);
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
   
108
    public void onDrawFrame(GL10 glUnused) 
109
      {
110
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
111
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
112

    
113
      long time = System.currentTimeMillis();
114

    
115
      mObject.draw(time);
116
      mCenter.draw(time);
117
      }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
    
121
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
122
      {
123
      mScreenMin = width<height ? width:height;
124

    
125
      mObject.abortEffects(EffectTypes.MATRIX);
126
      mCenter.abortEffects(EffectTypes.MATRIX);
127

    
128
      int centerSize = mCenter.getWidth();
129

    
130
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
131
        {
132
        mFactorObj = (0.70f*height)/mObjHeight;
133
        mFactorCen = (0.15f*height)/centerSize;
134
        }
135
      else
136
        {
137
        mFactorObj = (0.70f*width)/mObjWidth;
138
        mFactorCen = (0.15f*width)/centerSize;
139
        }
140

    
141
      Fragment3DActivity act = (Fragment3DActivity)mView.getContext();
142
      float cX = act.getCenterX();
143
      float cY = act.getCenterY();
144

    
145
      cX = (0.012f*cX-0.1f)*mObjWidth;
146
      cY = (0.012f*cY-0.1f)*mObjHeight;
147

    
148
      mMovePoint.set(cX*mFactorObj,cY*mFactorObj,0);
149

    
150
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
151

    
152
      mObject.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
153
      mObject.scale(mFactorObj);
154
      mObject.quaternion(mQuatInt1, rotateObj);
155
      mObject.quaternion(mQuatInt2, rotateObj);
156

    
157
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
158

    
159
      mCenter.quaternion(mQuatInt1, rotateCen);
160
      mCenter.quaternion(mQuatInt2, rotateCen);
161
      mCenter.move( new Static3D( (width -mFactorCen*centerSize-mFactorObj*mObjWidth )/2 ,
162
                                  (height-mFactorCen*centerSize-mFactorObj*mObjHeight)/2 , 10) );
163
      mCenter.move(mMoveInter);
164
      mCenter.scale(mFactorCen);
165

    
166
      Distorted.onSurfaceChanged(width, height); 
167
      }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
    
171
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
172
      {
173
      Fragment3DActivity act = (Fragment3DActivity)mView.getContext();
174

    
175
      InputStream is1 = act.getResources().openRawResource(act.getBitmap());
176
      InputStream is2 = act.getResources().openRawResource(R.raw.center);
177

    
178
      Bitmap bitmap1,bitmap2;
179
        
180
      try 
181
        {
182
        bitmap1 = BitmapFactory.decodeStream(is1);
183
        bitmap2 = BitmapFactory.decodeStream(is2);
184
        }
185
      finally 
186
        {
187
        try 
188
          {
189
          is1.close();
190
          is2.close();
191
          } 
192
        catch(IOException e) { }
193
        }  
194
      
195
      mObject.setBitmap(bitmap1);
196
      mCenter.setBitmap(bitmap2);
197

    
198
      try
199
        {
200
        Distorted.onSurfaceCreated(mView.getContext());
201
        }
202
      catch(Exception ex)
203
        {
204
        android.util.Log.e("Fragment3D", ex.getMessage() );
205
        }
206
      }
207
}
(3-3/4)