Project

General

Profile

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

examples / src / main / java / org / distorted / examples / transparency / TransparencyRenderer.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.transparency;
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.FragmentEffectAlpha;
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.PostprocessEffectBlur;
33
import org.distorted.library.effect.VertexEffectScale;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedNode;
37
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.mesh.MeshSquare;
40
import org.distorted.library.type.Static1D;
41
import org.distorted.library.type.Static2D;
42
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44

    
45
import java.io.InputStream;
46

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

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
class TransparencyRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
53
{
54
    private static final int NUM = 4; // 4 ints (x,y,z, argb) each describe 1 object below
55

    
56
    private static final int[] OBJECTS =
57
        {
58
         +9, +10, +2, 0xffff0000,
59
         -9, -10, -2, 0xffffff00
60
        };
61

    
62
    private static final int NUM_OBJECTS = OBJECTS.length/NUM;
63
    private static final int OBJ_SIZE    = 100;
64

    
65
    private final GLSurfaceView mView;
66
    private final Resources mResources;
67
    private final DistortedNode[] mNode;
68
    private final DistortedTexture[] mTex;
69
    private final Static3D[]  mMoveVector;
70
    private final Static1D[]  mAlphaVector;
71
    private final DistortedScreen mScreen;
72
    private final Static3D mScale;
73
    private final PostprocessEffectBlur[] mBlur;
74
    private final boolean[] mBlurApplied;
75
    private final DistortedEffects[] mEffects;
76

    
77
    Static4D mQuat1, mQuat2;
78
    int mScreenMin;
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
    TransparencyRenderer(GLSurfaceView v)
83
      {
84
      mView = v;
85
      mResources = v.getResources();
86

    
87
      MeshSquare mesh = new MeshSquare(1,1);
88

    
89
      mQuat1 = new Static4D(0,0,0,1);  // unity
90
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
91

    
92
      mScreen = new DistortedScreen();
93
      mScreen.glClearColor(1.0f,1.0f,1.0f,1.0f);
94

    
95
      mMoveVector   = new Static3D[NUM_OBJECTS];
96
      mAlphaVector  = new Static1D[NUM_OBJECTS];
97
      mNode         = new DistortedNode[NUM_OBJECTS];
98
      mTex          = new DistortedTexture[NUM_OBJECTS];
99
      mBlur         = new PostprocessEffectBlur[NUM_OBJECTS];
100
      mEffects      = new DistortedEffects[NUM_OBJECTS];
101
      mBlurApplied  = new boolean[NUM_OBJECTS];
102

    
103
      FragmentEffectAlpha[] alpha  = new FragmentEffectAlpha[NUM_OBJECTS];
104

    
105
      mScale = new Static3D(1.0f,1.0f,1.0f);
106
      Static3D center= new Static3D(0,0,0);
107

    
108
      MatrixEffectScale scaleEffect      = new MatrixEffectScale(mScale);
109
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, center);
110
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, center);
111

    
112
      for(int i=0; i<NUM_OBJECTS; i++)
113
        {
114
        mTex[i]          = new DistortedTexture();
115
        mMoveVector[i]   = new Static3D(0,0,0);
116
        mAlphaVector[i]  = new Static1D(0.5f);
117
        mBlur[i]         = new PostprocessEffectBlur( new Static2D(0,5) );
118
        mBlurApplied[i]  = true;
119
        alpha[i]         = new FragmentEffectAlpha(mAlphaVector[i]);
120
        mEffects[i]      = new DistortedEffects();
121
        mEffects[i].apply(mBlur[i]);
122
        mEffects[i].apply(alpha[i]);
123
        mEffects[i].apply( new VertexEffectScale(new Static3D(OBJ_SIZE,OBJ_SIZE,0) ) );
124
        mEffects[i].apply(new MatrixEffectMove(mMoveVector[i]));
125
        mEffects[i].apply(quatEffect2);
126
        mEffects[i].apply(quatEffect1);
127
        mEffects[i].apply(scaleEffect);
128

    
129
        mNode[i] = new DistortedNode(mTex[i], mEffects[i], mesh );
130
        mScreen.attach(mNode[i]);
131
        }
132
      }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
    void setPostprocess(int object, boolean doIt)
137
      {
138
      if( object>=0 && object<NUM_OBJECTS)
139
        {
140
        if( doIt && !mBlurApplied[object] )
141
          {
142
          mBlurApplied[object] = true;
143
          mEffects[object].apply(mBlur[object]);
144
          }
145
        if( !doIt && mBlurApplied[object] )
146
          {
147
          mBlurApplied[object] = false;
148
          mEffects[object].abortEffect(mBlur[object]);
149
          }
150
        }
151
      }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
    void setTransparency(int object, float level)
156
      {
157
      if( object>=0 && object<NUM_OBJECTS) mAlphaVector[object].set(level);
158
      }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161
// no better way to attach mNode[object] as the first child than detaching all and reattaching back...
162

    
163
    void setRenderFirst(int object)
164
      {
165
      if( object>=0 && object<NUM_OBJECTS)
166
        {
167
        for(int i=0; i<NUM_OBJECTS; i++)
168
          {
169
          mScreen.detach(mNode[i]);
170
          }
171

    
172
        mScreen.attach(mNode[object]);
173

    
174
        for(int i=0; i<NUM_OBJECTS; i++)
175
          {
176
          if( i!=object )
177
            mScreen.attach(mNode[i]);
178
          }
179
        }
180
      }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
    void setRenderModeToOIT(boolean oit)
185
      {
186
      mScreen.setOrderIndependentTransparency(oit);
187
      }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
    public void onDrawFrame(GL10 glUnused) 
192
      {
193
      mScreen.render(System.currentTimeMillis());
194
      }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
199
      {
200
      float size= 0.02f*OBJ_SIZE;
201
      mScreenMin = Math.min(width, height);
202

    
203
      float factor = 0.65f*mScreenMin/OBJ_SIZE;
204
      mScale.set(factor,factor,factor);
205

    
206
      for(int i=0; i<NUM_OBJECTS; i++)
207
        {
208
        mMoveVector[i].set(size*OBJECTS[NUM*i], size*OBJECTS[NUM*i+1], size*OBJECTS[NUM*i+2]);
209
        }
210

    
211
      mScreen.resize(width, height);
212
      }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215
    
216
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
217
      {
218
      for(int i=0; i<NUM_OBJECTS; i++)
219
        {
220
        mTex[i].setColorARGB(OBJECTS[NUM * i + 3]);
221
        }
222

    
223
      VertexEffectScale.enable();
224
      PostprocessEffectBlur.enable();
225
      FragmentEffectAlpha.enable();
226
      DistortedLibrary.onSurfaceCreated(this);
227
      }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
    public void distortedException(Exception ex)
232
      {
233
      android.util.Log.e("Transparency", ex.getMessage() );
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    public int openGlVersion()
239
      {
240
      Context context = mView.getContext();
241
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
242
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
243
      int glESversion = configurationInfo.reqGlEsVersion;
244
      int major = glESversion >> 16;
245
      int minor = glESversion & 0xff;
246

    
247
      return 100*major + 10*minor;
248
      }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
    public InputStream localFile(int fileID)
253
      {
254
      return mResources.openRawResource(fileID);
255
      }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
    public void logMessage(String message)
260
      {
261
      android.util.Log.e("Transparency", message );
262
      }
263
}
(2-2/3)