Project

General

Profile

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

examples / src / main / java / org / distorted / examples / transparency / TransparencyRenderer.java @ 30f337e5

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.content.res.Resources;
23
import android.opengl.GLSurfaceView;
24

    
25
import org.distorted.library.effect.FragmentEffectAlpha;
26
import org.distorted.library.effect.MatrixEffectMove;
27
import org.distorted.library.effect.MatrixEffectQuaternion;
28
import org.distorted.library.effect.MatrixEffectScale;
29
import org.distorted.library.effect.PostprocessEffectBlur;
30
import org.distorted.library.effect.VertexEffectScale;
31
import org.distorted.library.main.DistortedLibrary;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedNode;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.mesh.MeshSquare;
37
import org.distorted.library.type.Static1D;
38
import org.distorted.library.type.Static2D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41

    
42
import java.io.InputStream;
43

    
44
import javax.microedition.khronos.egl.EGLConfig;
45
import javax.microedition.khronos.opengles.GL10;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

    
53
    private static final int[] OBJECTS =
54
        {
55
         +9, +10, +2, 0xffff0000,
56
         -9, -10, -2, 0xffffff00
57
        };
58

    
59
    private static final int NUM_OBJECTS = OBJECTS.length/NUM;
60
    private static final int OBJ_SIZE    = 100;
61

    
62
    private final Resources mResources;
63
    private final DistortedNode[] mNode;
64
    private final DistortedTexture[] mTex;
65
    private final Static3D[]  mMoveVector;
66
    private final Static1D[]  mAlphaVector;
67
    private final DistortedScreen mScreen;
68
    private final Static3D mScale;
69
    private final PostprocessEffectBlur[] mBlur;
70
    private final boolean[] mBlurApplied;
71
    private final DistortedEffects[] mEffects;
72

    
73
    Static4D mQuat1, mQuat2;
74
    int mScreenMin;
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
    TransparencyRenderer(GLSurfaceView v)
79
      {
80
      mResources = v.getResources();
81

    
82
      MeshSquare mesh = new MeshSquare(1,1);
83

    
84
      mQuat1 = new Static4D(0,0,0,1);  // unity
85
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
86

    
87
      mScreen = new DistortedScreen();
88
      mScreen.glClearColor(1.0f,1.0f,1.0f,1.0f);
89

    
90
      mMoveVector   = new Static3D[NUM_OBJECTS];
91
      mAlphaVector  = new Static1D[NUM_OBJECTS];
92
      mNode         = new DistortedNode[NUM_OBJECTS];
93
      mTex          = new DistortedTexture[NUM_OBJECTS];
94
      mBlur         = new PostprocessEffectBlur[NUM_OBJECTS];
95
      mEffects      = new DistortedEffects[NUM_OBJECTS];
96
      mBlurApplied  = new boolean[NUM_OBJECTS];
97

    
98
      FragmentEffectAlpha[] alpha  = new FragmentEffectAlpha[NUM_OBJECTS];
99

    
100
      mScale = new Static3D(1.0f,1.0f,1.0f);
101
      Static3D center= new Static3D(0,0,0);
102

    
103
      MatrixEffectScale scaleEffect      = new MatrixEffectScale(mScale);
104
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, center);
105
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, center);
106

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

    
124
        mNode[i] = new DistortedNode(mTex[i], mEffects[i], mesh );
125
        mScreen.attach(mNode[i]);
126
        }
127
      }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

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

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
    void setTransparency(int object, float level)
151
      {
152
      if( object>=0 && object<NUM_OBJECTS) mAlphaVector[object].set(level);
153
      }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
// no better way to attach mNode[object] as the first child than detaching all and reattaching back...
157

    
158
    void setRenderFirst(int object)
159
      {
160
      if( object>=0 && object<NUM_OBJECTS)
161
        {
162
        for(int i=0; i<NUM_OBJECTS; i++)
163
          {
164
          mScreen.detach(mNode[i]);
165
          }
166

    
167
        mScreen.attach(mNode[object]);
168

    
169
        for(int i=0; i<NUM_OBJECTS; i++)
170
          {
171
          if( i!=object )
172
            mScreen.attach(mNode[i]);
173
          }
174
        }
175
      }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
    void setRenderModeToOIT(boolean oit)
180
      {
181
      mScreen.setOrderIndependentTransparency(oit);
182
      }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
    public void onDrawFrame(GL10 glUnused) 
187
      {
188
      mScreen.render(System.currentTimeMillis());
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
194
      {
195
      float size= 0.02f*OBJ_SIZE;
196
      mScreenMin = Math.min(width, height);
197

    
198
      float factor = 0.65f*mScreenMin/OBJ_SIZE;
199
      mScale.set(factor,factor,factor);
200

    
201
      for(int i=0; i<NUM_OBJECTS; i++)
202
        {
203
        mMoveVector[i].set(size*OBJECTS[NUM*i], size*OBJECTS[NUM*i+1], size*OBJECTS[NUM*i+2]);
204
        }
205

    
206
      mScreen.resize(width, height);
207
      }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210
    
211
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
212
      {
213
      for(int i=0; i<NUM_OBJECTS; i++)
214
        {
215
        mTex[i].setColorARGB(OBJECTS[NUM * i + 3]);
216
        }
217

    
218
      VertexEffectScale.enable();
219
      PostprocessEffectBlur.enable();
220
      FragmentEffectAlpha.enable();
221
      DistortedLibrary.onSurfaceCreated(this);
222
      }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
    public void distortedException(Exception ex)
227
      {
228
      android.util.Log.e("Transparency", ex.getMessage() );
229
      }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
    public InputStream localFile(int fileID)
234
      {
235
      return mResources.openRawResource(fileID);
236
      }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

    
240
    public void logMessage(String message)
241
      {
242
      android.util.Log.e("Transparency", message );
243
      }
244
}
(2-2/3)