Project

General

Profile

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

examples / src / main / java / org / distorted / examples / predeform / PredeformRenderer.java @ 641ea00c

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.main.DistortedEffects;
31
import org.distorted.library.main.DistortedLibrary;
32
import org.distorted.library.main.DistortedScreen;
33
import org.distorted.library.main.DistortedTexture;
34
import org.distorted.library.mesh.MeshBase;
35
import org.distorted.library.type.DynamicQuat;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38

    
39
import java.util.ArrayList;
40

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

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

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

    
51
    private GLSurfaceView mView;
52
    private DistortedTexture mTexture;
53
    private DistortedEffects mEffects1, mEffects2;
54
    private MeshBase mMesh1, mMesh2;
55
    private DistortedScreen mScreen;
56
    private Static3D mScale, mMove1, mMove2;
57

    
58
    Static4D mQuat1, mQuat2;
59
    int mScreenMin;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
    PredeformRenderer(GLSurfaceView v)
64
      {
65
      mView = v;
66

    
67
      mScale= new Static3D(1,1,1);
68
      mMove1= new Static3D(0,0,0);
69
      mMove2= new Static3D(0,0,0);
70

    
71
      Static3D center=new Static3D(0,0,0);
72

    
73
      PredeformActivity2 act = (PredeformActivity2)v.getContext();
74

    
75
      mTexture = act.getTexture();
76
      mMesh1   = act.getMesh();
77
      mMesh2   = mMesh1.deepCopy();
78

    
79
      mQuat1 = new Static4D(0,0,0,1);  // unity
80
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
81
      
82
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
83
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
84

    
85
      quatInt1.add(mQuat1);
86
      quatInt2.add(mQuat2);
87

    
88
      ArrayList<VertexEffect> effectList = PredeformEffectList.getEffectList();
89
      int numEffects = effectList.size();
90

    
91
      for(int i=0; i<numEffects; i++)
92
        {
93
        mMesh2.apply(effectList.get(i));
94
        }
95

    
96
      mEffects1 = new DistortedEffects();
97

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

    
103
      mEffects1.apply( new MatrixEffectScale(mScale));
104
      mEffects1.apply( new MatrixEffectQuaternion(quatInt2, center) );
105
      mEffects1.apply( new MatrixEffectQuaternion(quatInt1, center) );
106
      mEffects1.apply( new MatrixEffectMove(mMove1));
107

    
108
      mEffects2 = new DistortedEffects();
109
      mEffects2.apply( new MatrixEffectScale(mScale));
110
      mEffects2.apply( new MatrixEffectQuaternion(quatInt2, center) );
111
      mEffects2.apply( new MatrixEffectQuaternion(quatInt1, center) );
112
      mEffects2.apply( new MatrixEffectMove(mMove2));
113

    
114
      mScreen = new DistortedScreen();
115
      mScreen.glClearColor(1.0f,1.0f,1.0f,0.0f);
116
      mScreen.setProjection(FOV, NEAR);
117
      }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
   
121
    public void onDrawFrame(GL10 glUnused) 
122
      {
123
      mScreen.render( System.currentTimeMillis() );
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
129
      {
130
      final float SCALE = 0.6f;
131

    
132
      if( width<height )
133
        {
134
        float factor = SCALE*(Math.min(width, 0.5f*height));
135
        mScale.set(factor,factor,factor);
136
        mMove1.set(0,+height*0.25f,0);
137
        mMove2.set(0,-height*0.25f,0);
138
        }
139
      else
140
        {
141
        float factor = SCALE*(Math.min( 0.5f*width, height));
142
        mScale.set(factor,factor,factor);
143
        mMove1.set(+width*0.25f,0,0);
144
        mMove2.set(-width*0.25f,0,0);
145
        }
146

    
147
      mScreenMin = Math.min(width, height);
148
      mScreen.resize(width, height);
149
      }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
    void showNormal(boolean show)
154
      {
155
      mMesh1.setShowNormals(show);
156
      mMesh2.setShowNormals(show);
157
      }
158

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

    
161
    float setLevel(int level)
162
      {
163
      float inflateLevel = (level-50)/50.0f;
164
      mMesh1.setInflate(inflateLevel);
165
      mMesh2.setInflate(inflateLevel);
166

    
167
      return inflateLevel;
168
      }
169

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

    
176
      mTexture.setTexture( act.getBitmap() );
177
      mScreen.detachAll();
178
      mScreen.attach(mTexture,mEffects1,mMesh1);
179
      mScreen.attach(mTexture,mEffects2,mMesh2);
180

    
181
      Effect.enableEffects(EffectType.VERTEX);
182

    
183
      try
184
        {
185
        DistortedLibrary.onCreate(act);
186
        }
187
      catch(Exception ex)
188
        {
189
        android.util.Log.e("Inflate", ex.getMessage() );
190
        }
191
      }
192
}
(6-6/7)