Project

General

Profile

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

examples / src / main / java / org / distorted / examples / vertex3d / Vertex3DRenderer.java @ 950511ed

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 int mObjWidth, mObjHeight;
52
    private DynamicQuat mQuatInt1, mQuatInt2;
53

    
54
    private Dynamic3D mMoveInter;
55
    private Static3D mMovePoint;
56
    private float mFactorObj;
57

    
58
    Static4D mQuat1, mQuat2;
59
    int mScreenMin;
60

    
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
    public Vertex3DRenderer(GLSurfaceView v)
65
      {
66
      mView = v;
67

    
68
      mObject     = ((Vertex3DActivity)v.getContext()).getObject();
69
      mBackground = new DistortedBitmap(100, 100, 1);
70
      mCenter     = new DistortedBitmap(100, 100, 1);
71

    
72
      mObjWidth = mObject.getWidth();
73
      mObjHeight= mObject.getHeight();
74

    
75
      mQuat1 = new Static4D(0,0,0,1);  // unity
76
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
77

    
78
      mQuatInt1 = new DynamicQuat(0,0.5f);
79
      mQuatInt2 = new DynamicQuat(0,0.5f);
80

    
81
      mQuatInt1.add(mQuat1);
82
      mQuatInt2.add(mQuat2);
83

    
84
      mMovePoint= new Static3D(0,0,0);
85
      mMoveInter= new Dynamic3D();
86
      mMoveInter.add(mMovePoint);
87
      }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
    public void setCenter(float x, float y)
92
      {
93
      mMovePoint.set(mFactorObj*x,mFactorObj*y,0);
94
      }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
    public void onDrawFrame(GL10 glUnused)
99
      {
100
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
101
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
102

    
103
      long time = System.currentTimeMillis();
104

    
105
      mBackground.draw(time);
106
      mObject.draw(time);
107
      mCenter.draw(time);
108
      }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
113
      {
114
      mScreenMin = width<height ? width:height;
115

    
116
      mObject.abortEffects(EffectTypes.MATRIX);
117
      mBackground.abortEffects(EffectTypes.MATRIX);
118
      mCenter.abortEffects(EffectTypes.MATRIX);
119

    
120
      float factorCen;
121
      int centerSize = mCenter.getWidth();
122

    
123
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
124
        {
125
        mFactorObj = (0.80f*height)/mObjHeight;
126
        factorCen  = (0.08f*height)/centerSize;
127
        }
128
      else
129
        {
130
        mFactorObj = (0.80f*width)/mObjWidth;
131
        factorCen  = (0.08f*width)/centerSize;
132
        }
133

    
134
      Vertex3DActivity act = (Vertex3DActivity)mView.getContext();
135
      mMovePoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
136

    
137
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
138

    
139
      mObject.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
140
      mObject.scale(mFactorObj);
141
      mObject.quaternion(mQuatInt1, rotateObj);
142
      mObject.quaternion(mQuatInt2, rotateObj);
143

    
144
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
145

    
146
      mCenter.quaternion(mQuatInt1, rotateCen);
147
      mCenter.quaternion(mQuatInt2, rotateCen);
148

    
149
      mCenter.move( new Static3D( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
150
                                  (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , 10) );
151
      mCenter.move(mMoveInter);
152
      mCenter.scale(factorCen);
153

    
154
      int backgroundSize = mBackground.getWidth();
155
      float factorBackX = ((float)width)/backgroundSize;
156
      float factorBackY = ((float)height)/backgroundSize;
157

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

    
161
      Distorted.onSurfaceChanged(width, height);
162
      }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
167
      {
168
      Vertex3DActivity act = (Vertex3DActivity)mView.getContext();
169

    
170
      InputStream is1 = act.getResources().openRawResource(act.getBitmap());
171
      InputStream is2 = act.getResources().openRawResource(R.raw.water);
172
      InputStream is3 = act.getResources().openRawResource(R.raw.center);
173

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

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