Project

General

Profile

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

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

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
import android.opengl.GLSurfaceView;
23
24 e6129574 Leszek Koltunski
import org.distorted.library.effect.EffectType;
25
import org.distorted.library.effect.MatrixEffectMove;
26 4b7c432e Leszek Koltunski
import org.distorted.library.effect.MatrixEffectQuaternion;
27 59835a0a Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
28 e6129574 Leszek Koltunski
import org.distorted.library.effect.VertexEffect;
29
import org.distorted.library.effect.Effect;
30 59835a0a Leszek Koltunski
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 4b7c432e Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
36 59835a0a Leszek Koltunski
import org.distorted.library.type.Static3D;
37 4b7c432e Leszek Koltunski
import org.distorted.library.type.Static4D;
38 59835a0a Leszek Koltunski
39 e6129574 Leszek Koltunski
import java.util.ArrayList;
40
41 59835a0a Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
42
import javax.microedition.khronos.opengles.GL10;
43
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46
class PredeformRenderer implements GLSurfaceView.Renderer
47
{
48 4b7c432e Leszek Koltunski
    private static final float FOV = 30.0f;
49 59835a0a Leszek Koltunski
    private static final float NEAR = 0.1f;
50
51
    private GLSurfaceView mView;
52
    private DistortedTexture mTexture;
53 e6129574 Leszek Koltunski
    private DistortedEffects mEffects1, mEffects2;
54
    private MeshBase mMesh1, mMesh2;
55 59835a0a Leszek Koltunski
    private DistortedScreen mScreen;
56 e6129574 Leszek Koltunski
    private Static3D mScale, mMove1, mMove2;
57 4b7c432e Leszek Koltunski
58
    Static4D mQuat1, mQuat2;
59
    int mScreenMin;
60 59835a0a Leszek Koltunski
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63
    PredeformRenderer(GLSurfaceView v)
64
      {
65
      mView = v;
66
67
      mScale= new Static3D(1,1,1);
68 e6129574 Leszek Koltunski
      mMove1= new Static3D(0,0,0);
69
      mMove2= new Static3D(0,0,0);
70 4b7c432e Leszek Koltunski
71
      Static3D center=new Static3D(0,0,0);
72
73
      PredeformActivity2 act = (PredeformActivity2)v.getContext();
74
75
      mTexture = act.getTexture();
76 e6129574 Leszek Koltunski
      mMesh1   = act.getMesh();
77
      mMesh2   = mMesh1.deepCopy();
78 4b7c432e Leszek Koltunski
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 e6129574 Leszek Koltunski
      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 59835a0a Leszek Koltunski
114
      mScreen = new DistortedScreen();
115
      mScreen.glClearColor(1.0f,1.0f,1.0f,0.0f);
116
      mScreen.setProjection(FOV, NEAR);
117
      }
118
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120 4b7c432e Leszek Koltunski
   
121
    public void onDrawFrame(GL10 glUnused) 
122
      {
123
      mScreen.render( System.currentTimeMillis() );
124
      }
125 59835a0a Leszek Koltunski
126 4b7c432e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
129 59835a0a Leszek Koltunski
      {
130 e6129574 Leszek Koltunski
      final float SCALE = 0.6f;
131
132
      if( width<height )
133
        {
134 641ea00c Leszek Koltunski
        float factor = SCALE*(Math.min(width, 0.5f*height));
135 e6129574 Leszek Koltunski
        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 641ea00c Leszek Koltunski
        float factor = SCALE*(Math.min( 0.5f*width, height));
142 e6129574 Leszek Koltunski
        mScale.set(factor,factor,factor);
143
        mMove1.set(+width*0.25f,0,0);
144
        mMove2.set(-width*0.25f,0,0);
145
        }
146 4b7c432e Leszek Koltunski
147
      mScreenMin = Math.min(width, height);
148
      mScreen.resize(width, height);
149 59835a0a Leszek Koltunski
      }
150
151 e6129574 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
152
153
    void showNormal(boolean show)
154
      {
155
      mMesh1.setShowNormals(show);
156
      mMesh2.setShowNormals(show);
157
      }
158
159 59835a0a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
160 4b7c432e Leszek Koltunski
161
    float setLevel(int level)
162 59835a0a Leszek Koltunski
      {
163 4b7c432e Leszek Koltunski
      float inflateLevel = (level-50)/50.0f;
164 e6129574 Leszek Koltunski
      mMesh1.setInflate(inflateLevel);
165
      mMesh2.setInflate(inflateLevel);
166 59835a0a Leszek Koltunski
167 4b7c432e Leszek Koltunski
      return inflateLevel;
168 59835a0a Leszek Koltunski
      }
169
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
    
172
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
173
      {
174 4b7c432e Leszek Koltunski
      PredeformActivity2 act = (PredeformActivity2)mView.getContext();
175 59835a0a Leszek Koltunski
176
      mTexture.setTexture( act.getBitmap() );
177
      mScreen.detachAll();
178 e6129574 Leszek Koltunski
      mScreen.attach(mTexture,mEffects1,mMesh1);
179
      mScreen.attach(mTexture,mEffects2,mMesh2);
180 59835a0a Leszek Koltunski
181 e6129574 Leszek Koltunski
      Effect.enableEffects(EffectType.VERTEX);
182 4b7c432e Leszek Koltunski
183 59835a0a Leszek Koltunski
      try
184
        {
185
        DistortedLibrary.onCreate(act);
186
        }
187
      catch(Exception ex)
188
        {
189
        android.util.Log.e("Inflate", ex.getMessage() );
190
        }
191
      }
192
}