Project

General

Profile

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

examples / src / main / java / org / distorted / examples / predeform / PredeformRenderer.java @ 77e66c58

1 59835a0a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26 59835a0a Leszek Koltunski
import android.opengl.GLSurfaceView;
27
28 e6129574 Leszek Koltunski
import org.distorted.library.effect.EffectType;
29
import org.distorted.library.effect.MatrixEffectMove;
30 4b7c432e Leszek Koltunski
import org.distorted.library.effect.MatrixEffectQuaternion;
31 59835a0a Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
32 e6129574 Leszek Koltunski
import org.distorted.library.effect.VertexEffect;
33
import org.distorted.library.effect.Effect;
34 59835a0a Leszek Koltunski
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 4b7c432e Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
40 59835a0a Leszek Koltunski
import org.distorted.library.type.Static3D;
41 4b7c432e Leszek Koltunski
import org.distorted.library.type.Static4D;
42 59835a0a Leszek Koltunski
43 dc10a48d Leszek Koltunski
import java.io.InputStream;
44 e6129574 Leszek Koltunski
import java.util.ArrayList;
45
46 59835a0a Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
47
import javax.microedition.khronos.opengles.GL10;
48
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51 dc10a48d Leszek Koltunski
class PredeformRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
52 59835a0a Leszek Koltunski
{
53 4b7c432e Leszek Koltunski
    private static final float FOV = 30.0f;
54 59835a0a Leszek Koltunski
    private static final float NEAR = 0.1f;
55
56 ef02f5e0 Leszek Koltunski
    private final GLSurfaceView mView;
57 dc10a48d Leszek Koltunski
    private final Resources mResources;
58 ef02f5e0 Leszek Koltunski
    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 4b7c432e Leszek Koltunski
64
    Static4D mQuat1, mQuat2;
65
    int mScreenMin;
66 59835a0a Leszek Koltunski
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69
    PredeformRenderer(GLSurfaceView v)
70
      {
71
      mView = v;
72 dc10a48d Leszek Koltunski
      mResources = v.getResources();
73 59835a0a Leszek Koltunski
74
      mScale= new Static3D(1,1,1);
75 e6129574 Leszek Koltunski
      mMove1= new Static3D(0,0,0);
76
      mMove2= new Static3D(0,0,0);
77 4b7c432e Leszek Koltunski
78
      Static3D center=new Static3D(0,0,0);
79
80
      PredeformActivity2 act = (PredeformActivity2)v.getContext();
81
82
      mTexture = act.getTexture();
83 e6129574 Leszek Koltunski
      mMesh1   = act.getMesh();
84 ca4b22de Leszek Koltunski
      mMesh2   = mMesh1.copy(true);
85 4b7c432e Leszek Koltunski
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 e6129574 Leszek Koltunski
      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 59835a0a Leszek Koltunski
121
      mScreen = new DistortedScreen();
122
      mScreen.glClearColor(1.0f,1.0f,1.0f,0.0f);
123
      mScreen.setProjection(FOV, NEAR);
124
      }
125
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127 4b7c432e Leszek Koltunski
   
128
    public void onDrawFrame(GL10 glUnused) 
129
      {
130
      mScreen.render( System.currentTimeMillis() );
131
      }
132 59835a0a Leszek Koltunski
133 4b7c432e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
134
    
135
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
136 59835a0a Leszek Koltunski
      {
137 e6129574 Leszek Koltunski
      final float SCALE = 0.6f;
138
139
      if( width<height )
140
        {
141 641ea00c Leszek Koltunski
        float factor = SCALE*(Math.min(width, 0.5f*height));
142 e6129574 Leszek Koltunski
        mScale.set(factor,factor,factor);
143 dc10a48d Leszek Koltunski
        mMove1.set(0, height*0.25f,0);
144 e6129574 Leszek Koltunski
        mMove2.set(0,-height*0.25f,0);
145
        }
146
      else
147
        {
148 641ea00c Leszek Koltunski
        float factor = SCALE*(Math.min( 0.5f*width, height));
149 e6129574 Leszek Koltunski
        mScale.set(factor,factor,factor);
150 dc10a48d Leszek Koltunski
        mMove1.set( width*0.25f,0,0);
151 e6129574 Leszek Koltunski
        mMove2.set(-width*0.25f,0,0);
152
        }
153 4b7c432e Leszek Koltunski
154
      mScreenMin = Math.min(width, height);
155
      mScreen.resize(width, height);
156 59835a0a Leszek Koltunski
      }
157
158 e6129574 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
159
160
    void showNormal(boolean show)
161
      {
162
      mMesh1.setShowNormals(show);
163
      mMesh2.setShowNormals(show);
164
      }
165
166 59835a0a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
167 4b7c432e Leszek Koltunski
168
    float setLevel(int level)
169 59835a0a Leszek Koltunski
      {
170 4b7c432e Leszek Koltunski
      float inflateLevel = (level-50)/50.0f;
171 e6129574 Leszek Koltunski
      mMesh1.setInflate(inflateLevel);
172
      mMesh2.setInflate(inflateLevel);
173 59835a0a Leszek Koltunski
174 4b7c432e Leszek Koltunski
      return inflateLevel;
175 59835a0a Leszek Koltunski
      }
176
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
    
179
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
180
      {
181 4b7c432e Leszek Koltunski
      PredeformActivity2 act = (PredeformActivity2)mView.getContext();
182 59835a0a Leszek Koltunski
183
      mTexture.setTexture( act.getBitmap() );
184
      mScreen.detachAll();
185 e6129574 Leszek Koltunski
      mScreen.attach(mTexture,mEffects1,mMesh1);
186
      mScreen.attach(mTexture,mEffects2,mMesh2);
187 59835a0a Leszek Koltunski
188 e6129574 Leszek Koltunski
      Effect.enableEffects(EffectType.VERTEX);
189 4b7c432e Leszek Koltunski
190 ef02f5e0 Leszek Koltunski
      DistortedLibrary.needTransformFeedback();
191 dc10a48d Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(this);
192 061449ed Leszek Koltunski
      }
193
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
196
    public void distortedException(Exception ex)
197
      {
198
      android.util.Log.e("Predeform", ex.getMessage() );
199 59835a0a Leszek Koltunski
      }
200 dc10a48d Leszek Koltunski
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202
203
    public InputStream localFile(int fileID)
204
      {
205
      return mResources.openRawResource(fileID);
206
      }
207
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
210
    public void logMessage(String message)
211
      {
212
      android.util.Log.e("Predeform", message );
213
      }
214 59835a0a Leszek Koltunski
}