Project

General

Profile

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

examples / src / main / java / org / distorted / examples / transparency / TransparencyRenderer.java @ 77e66c58

1 664a0e45 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 664a0e45 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 664a0e45 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 664a0e45 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 664a0e45 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.transparency;
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 664a0e45 Leszek Koltunski
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 8392dc7b Leszek Koltunski
import org.distorted.library.effect.VertexEffectScale;
34 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
35 664a0e45 Leszek Koltunski
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 f2085b96 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
40 664a0e45 Leszek Koltunski
import org.distorted.library.type.Static1D;
41 678c391d Leszek Koltunski
import org.distorted.library.type.Static2D;
42 664a0e45 Leszek Koltunski
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44
45 dc10a48d Leszek Koltunski
import java.io.InputStream;
46
47 664a0e45 Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
48
import javax.microedition.khronos.opengles.GL10;
49
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51
52 dc10a48d Leszek Koltunski
class TransparencyRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
53 664a0e45 Leszek Koltunski
{
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 a4d59c0b Leszek Koltunski
         +9, +10, +2, 0xffff0000,
59
         -9, -10, -2, 0xffffff00
60 664a0e45 Leszek Koltunski
        };
61
62
    private static final int NUM_OBJECTS = OBJECTS.length/NUM;
63
    private static final int OBJ_SIZE    = 100;
64
65 dc10a48d Leszek Koltunski
    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 664a0e45 Leszek Koltunski
77
    Static4D mQuat1, mQuat2;
78
    int mScreenMin;
79
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
82
    TransparencyRenderer(GLSurfaceView v)
83
      {
84
      mView = v;
85 dc10a48d Leszek Koltunski
      mResources = v.getResources();
86 664a0e45 Leszek Koltunski
87 f2085b96 Leszek Koltunski
      MeshSquare mesh = new MeshSquare(1,1);
88 664a0e45 Leszek Koltunski
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 29aba2d1 Leszek Koltunski
      mBlur         = new PostprocessEffectBlur[NUM_OBJECTS];
100
      mEffects      = new DistortedEffects[NUM_OBJECTS];
101
      mBlurApplied  = new boolean[NUM_OBJECTS];
102 664a0e45 Leszek Koltunski
103 d7413405 Leszek Koltunski
      FragmentEffectAlpha[] alpha  = new FragmentEffectAlpha[NUM_OBJECTS];
104 664a0e45 Leszek Koltunski
105
      mScale = new Static3D(1.0f,1.0f,1.0f);
106 16b22aab Leszek Koltunski
      Static3D center= new Static3D(0,0,0);
107 664a0e45 Leszek Koltunski
108
      MatrixEffectScale scaleEffect      = new MatrixEffectScale(mScale);
109 16b22aab Leszek Koltunski
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, center);
110
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, center);
111 664a0e45 Leszek Koltunski
112
      for(int i=0; i<NUM_OBJECTS; i++)
113
        {
114 687263cc Leszek Koltunski
        mTex[i]          = new DistortedTexture();
115 664a0e45 Leszek Koltunski
        mMoveVector[i]   = new Static3D(0,0,0);
116
        mAlphaVector[i]  = new Static1D(0.5f);
117 678c391d Leszek Koltunski
        mBlur[i]         = new PostprocessEffectBlur( new Static2D(0,5) );
118 29aba2d1 Leszek Koltunski
        mBlurApplied[i]  = true;
119 664a0e45 Leszek Koltunski
        alpha[i]         = new FragmentEffectAlpha(mAlphaVector[i]);
120 698ad0a8 Leszek Koltunski
        mEffects[i]      = new DistortedEffects();
121 29aba2d1 Leszek Koltunski
        mEffects[i].apply(mBlur[i]);
122
        mEffects[i].apply(alpha[i]);
123 8392dc7b Leszek Koltunski
        mEffects[i].apply( new VertexEffectScale(new Static3D(OBJ_SIZE,OBJ_SIZE,0) ) );
124 29aba2d1 Leszek Koltunski
        mEffects[i].apply(new MatrixEffectMove(mMoveVector[i]));
125 0ba5de00 Leszek Koltunski
        mEffects[i].apply(quatEffect2);
126
        mEffects[i].apply(quatEffect1);
127
        mEffects[i].apply(scaleEffect);
128 29aba2d1 Leszek Koltunski
129
        mNode[i] = new DistortedNode(mTex[i], mEffects[i], mesh );
130 664a0e45 Leszek Koltunski
        mScreen.attach(mNode[i]);
131
        }
132
      }
133
134 29aba2d1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 664a0e45 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 a935e9db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184
    void setRenderModeToOIT(boolean oit)
185
      {
186
      mScreen.setOrderIndependentTransparency(oit);
187
      }
188
189 664a0e45 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
190
191
    public void onDrawFrame(GL10 glUnused) 
192
      {
193
      mScreen.render(System.currentTimeMillis());
194
      }
195
196 061449ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 664a0e45 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
215
    
216
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
217
      {
218
      for(int i=0; i<NUM_OBJECTS; i++)
219
        {
220 e50e7ba7 Leszek Koltunski
        mTex[i].setColorARGB(OBJECTS[NUM * i + 3]);
221 664a0e45 Leszek Koltunski
        }
222
223 8392dc7b Leszek Koltunski
      VertexEffectScale.enable();
224 664a0e45 Leszek Koltunski
      PostprocessEffectBlur.enable();
225
      FragmentEffectAlpha.enable();
226 dc10a48d Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(this);
227 664a0e45 Leszek Koltunski
      }
228
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231 061449ed Leszek Koltunski
    public void distortedException(Exception ex)
232 664a0e45 Leszek Koltunski
      {
233 061449ed Leszek Koltunski
      android.util.Log.e("Transparency", ex.getMessage() );
234 664a0e45 Leszek Koltunski
      }
235 dc10a48d Leszek Koltunski
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
238
    public InputStream localFile(int fileID)
239
      {
240
      return mResources.openRawResource(fileID);
241
      }
242
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244
245
    public void logMessage(String message)
246
      {
247
      android.util.Log.e("Transparency", message );
248
      }
249 664a0e45 Leszek Koltunski
}