Project

General

Profile

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

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

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.opengl.GLSurfaceView;
23

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

    
40
import java.util.ArrayList;
41

    
42
import javax.microedition.khronos.egl.EGLConfig;
43
import javax.microedition.khronos.opengles.GL10;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
class PredeformRenderer implements GLSurfaceView.Renderer
48
{
49
    private static final float FOV = 30.0f;
50
    private static final float NEAR = 0.1f;
51

    
52
    private GLSurfaceView mView;
53
    private DistortedTexture mTexture;
54
    private DistortedEffects mEffects1, mEffects2;
55
    private MeshBase mMesh1, mMesh2;
56
    private DistortedScreen mScreen;
57
    private float mObjWidth, mObjHeight, mObjDepth;
58
    private Static3D mScale, mMove1, mMove2;
59

    
60
    Static4D mQuat1, mQuat2;
61
    int mScreenMin;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
    PredeformRenderer(GLSurfaceView v)
66
      {
67
      mView = v;
68

    
69
      mScale= new Static3D(1,1,1);
70
      mMove1= new Static3D(0,0,0);
71
      mMove2= new Static3D(0,0,0);
72

    
73
      Static3D center=new Static3D(0,0,0);
74

    
75
      PredeformActivity2 act = (PredeformActivity2)v.getContext();
76

    
77
      mTexture = act.getTexture();
78
      mMesh1   = act.getMesh();
79
      mMesh2   = mMesh1.deepCopy();
80

    
81
      mObjWidth = act.getCols();
82
      mObjHeight= act.getRows();
83
      mObjDepth = act.getSlic();
84

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

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

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

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

    
102
      mEffects1 = new DistortedEffects();
103
      mEffects1.apply( new VertexEffectScale(new Static3D(mObjWidth,mObjHeight,mObjDepth) ) );
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 VertexEffectScale(new Static3D(mObjWidth,mObjHeight,mObjDepth) ) );
117
      mEffects2.apply( new MatrixEffectScale(mScale));
118
      mEffects2.apply( new MatrixEffectQuaternion(quatInt2, center) );
119
      mEffects2.apply( new MatrixEffectQuaternion(quatInt1, center) );
120
      mEffects2.apply( new MatrixEffectMove(mMove2));
121

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

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

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

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

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

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

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

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

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

    
175
      return inflateLevel;
176
      }
177

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

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

    
189
      Effect.enableEffects(EffectType.VERTEX);
190

    
191
      try
192
        {
193
        DistortedLibrary.onCreate(act);
194
        }
195
      catch(Exception ex)
196
        {
197
        android.util.Log.e("Inflate", ex.getMessage() );
198
        }
199
      }
200
}
(6-6/7)