Project

General

Profile

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

examples / src / main / java / org / distorted / examples / predeform / PredeformRenderer.java @ dc10a48d

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.predeform;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import android.opengl.GLSurfaceView;
27

    
28
import org.distorted.library.effect.EffectType;
29
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.effect.MatrixEffectQuaternion;
31
import org.distorted.library.effect.MatrixEffectScale;
32
import org.distorted.library.effect.VertexEffect;
33
import org.distorted.library.effect.Effect;
34
import org.distorted.library.main.DistortedEffects;
35
import org.distorted.library.main.DistortedLibrary;
36
import org.distorted.library.main.DistortedScreen;
37
import org.distorted.library.main.DistortedTexture;
38
import org.distorted.library.mesh.MeshBase;
39
import org.distorted.library.type.DynamicQuat;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42

    
43
import java.io.InputStream;
44
import java.util.ArrayList;
45

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

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

    
51
class PredeformRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
52
{
53
    private static final float FOV = 30.0f;
54
    private static final float NEAR = 0.1f;
55

    
56
    private final GLSurfaceView mView;
57
    private final Resources mResources;
58
    private final DistortedTexture mTexture;
59
    private final DistortedEffects mEffects1, mEffects2;
60
    private final MeshBase mMesh1, mMesh2;
61
    private final DistortedScreen mScreen;
62
    private final Static3D mScale, mMove1, mMove2;
63

    
64
    Static4D mQuat1, mQuat2;
65
    int mScreenMin;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
    PredeformRenderer(GLSurfaceView v)
70
      {
71
      mView = v;
72
      mResources = v.getResources();
73

    
74
      mScale= new Static3D(1,1,1);
75
      mMove1= new Static3D(0,0,0);
76
      mMove2= new Static3D(0,0,0);
77

    
78
      Static3D center=new Static3D(0,0,0);
79

    
80
      PredeformActivity2 act = (PredeformActivity2)v.getContext();
81

    
82
      mTexture = act.getTexture();
83
      mMesh1   = act.getMesh();
84
      mMesh2   = mMesh1.copy(true);
85

    
86
      mQuat1 = new Static4D(0,0,0,1);  // unity
87
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
88
      
89
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
90
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
91

    
92
      quatInt1.add(mQuat1);
93
      quatInt2.add(mQuat2);
94

    
95
      ArrayList<VertexEffect> effectList = PredeformEffectList.getEffectList();
96
      int numEffects = effectList.size();
97

    
98
      for(int i=0; i<numEffects; i++)
99
        {
100
        mMesh2.apply(effectList.get(i));
101
        }
102

    
103
      mEffects1 = new DistortedEffects();
104

    
105
      for(int i=0; i<numEffects; i++)
106
        {
107
        mEffects1.apply(effectList.get(i));
108
        }
109

    
110
      mEffects1.apply( new MatrixEffectScale(mScale));
111
      mEffects1.apply( new MatrixEffectQuaternion(quatInt2, center) );
112
      mEffects1.apply( new MatrixEffectQuaternion(quatInt1, center) );
113
      mEffects1.apply( new MatrixEffectMove(mMove1));
114

    
115
      mEffects2 = new DistortedEffects();
116
      mEffects2.apply( new MatrixEffectScale(mScale));
117
      mEffects2.apply( new MatrixEffectQuaternion(quatInt2, center) );
118
      mEffects2.apply( new MatrixEffectQuaternion(quatInt1, center) );
119
      mEffects2.apply( new MatrixEffectMove(mMove2));
120

    
121
      mScreen = new DistortedScreen();
122
      mScreen.glClearColor(1.0f,1.0f,1.0f,0.0f);
123
      mScreen.setProjection(FOV, NEAR);
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
   
128
    public void onDrawFrame(GL10 glUnused) 
129
      {
130
      mScreen.render( System.currentTimeMillis() );
131
      }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134
    
135
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
136
      {
137
      final float SCALE = 0.6f;
138

    
139
      if( width<height )
140
        {
141
        float factor = SCALE*(Math.min(width, 0.5f*height));
142
        mScale.set(factor,factor,factor);
143
        mMove1.set(0, height*0.25f,0);
144
        mMove2.set(0,-height*0.25f,0);
145
        }
146
      else
147
        {
148
        float factor = SCALE*(Math.min( 0.5f*width, height));
149
        mScale.set(factor,factor,factor);
150
        mMove1.set( width*0.25f,0,0);
151
        mMove2.set(-width*0.25f,0,0);
152
        }
153

    
154
      mScreenMin = Math.min(width, height);
155
      mScreen.resize(width, height);
156
      }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
    void showNormal(boolean show)
161
      {
162
      mMesh1.setShowNormals(show);
163
      mMesh2.setShowNormals(show);
164
      }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
    float setLevel(int level)
169
      {
170
      float inflateLevel = (level-50)/50.0f;
171
      mMesh1.setInflate(inflateLevel);
172
      mMesh2.setInflate(inflateLevel);
173

    
174
      return inflateLevel;
175
      }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
    
179
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
180
      {
181
      PredeformActivity2 act = (PredeformActivity2)mView.getContext();
182

    
183
      mTexture.setTexture( act.getBitmap() );
184
      mScreen.detachAll();
185
      mScreen.attach(mTexture,mEffects1,mMesh1);
186
      mScreen.attach(mTexture,mEffects2,mMesh2);
187

    
188
      Effect.enableEffects(EffectType.VERTEX);
189

    
190
      DistortedLibrary.needTransformFeedback();
191
      DistortedLibrary.onSurfaceCreated(this);
192
      }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
    public void distortedException(Exception ex)
197
      {
198
      android.util.Log.e("Predeform", ex.getMessage() );
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
    public int openGlVersion()
204
      {
205
      Context context = mView.getContext();
206
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
207
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
208
      int glESversion = configurationInfo.reqGlEsVersion;
209
      int major = glESversion >> 16;
210
      int minor = glESversion & 0xff;
211

    
212
      return 100*major + 10*minor;
213
      }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
    public InputStream localFile(int fileID)
218
      {
219
      return mResources.openRawResource(fileID);
220
      }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
    public void logMessage(String message)
225
      {
226
      android.util.Log.e("Predeform", message );
227
      }
228
}
(6-6/7)