Project

General

Profile

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

examples / src / main / java / org / distorted / examples / transparency / TransparencyRenderer.java @ 698ad0a8

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.opengl.GLSurfaceView;
23

    
24
import org.distorted.library.effect.FragmentEffectAlpha;
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.PostprocessEffectBlur;
29
import org.distorted.library.main.DistortedLibrary;
30
import org.distorted.library.main.DistortedEffects;
31
import org.distorted.library.main.DistortedNode;
32
import org.distorted.library.main.DistortedScreen;
33
import org.distorted.library.main.DistortedTexture;
34
import org.distorted.library.mesh.MeshRectangles;
35
import org.distorted.library.type.Static1D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38

    
39
import javax.microedition.khronos.egl.EGLConfig;
40
import javax.microedition.khronos.opengles.GL10;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
class TransparencyRenderer implements GLSurfaceView.Renderer
45
{
46
    private static final int NUM = 4; // 4 ints (x,y,z, argb) each describe 1 object below
47

    
48
    private static final int[] OBJECTS =
49
        {
50
         +9, +10, +2, 0xffff0000,
51
         -9, -10, -2, 0xffffff00
52
        };
53

    
54
    private static final int NUM_OBJECTS = OBJECTS.length/NUM;
55
    private static final int OBJ_SIZE    = 100;
56

    
57
    private GLSurfaceView mView;
58
    private DistortedNode[] mNode;
59
    private DistortedTexture[] mTex;
60
    private Static3D[]  mMoveVector;
61
    private Static1D[]  mAlphaVector;
62
    private DistortedScreen mScreen;
63
    private Static3D mMove, mScale, mCenter;
64
    private PostprocessEffectBlur[] mBlur;
65
    private boolean[] mBlurApplied;
66
    private DistortedEffects[] mEffects;
67

    
68
    Static4D mQuat1, mQuat2;
69
    int mScreenMin;
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
    TransparencyRenderer(GLSurfaceView v)
74
      {
75
      mView = v;
76

    
77
      MeshRectangles mesh = new MeshRectangles(1,1);
78
      mesh.setStretch(OBJ_SIZE,OBJ_SIZE,0);
79

    
80
      mQuat1 = new Static4D(0,0,0,1);  // unity
81
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
82

    
83
      mScreen = new DistortedScreen();
84
      mScreen.glClearColor(1.0f,1.0f,1.0f,1.0f);
85

    
86
      mMoveVector   = new Static3D[NUM_OBJECTS];
87
      mAlphaVector  = new Static1D[NUM_OBJECTS];
88
      mNode         = new DistortedNode[NUM_OBJECTS];
89
      mTex          = new DistortedTexture[NUM_OBJECTS];
90
      mBlur         = new PostprocessEffectBlur[NUM_OBJECTS];
91
      mEffects      = new DistortedEffects[NUM_OBJECTS];
92
      mBlurApplied  = new boolean[NUM_OBJECTS];
93

    
94
      FragmentEffectAlpha[] alpha  = new FragmentEffectAlpha[NUM_OBJECTS];
95

    
96
      mMove  = new Static3D(0,0,0);
97
      mScale = new Static3D(1.0f,1.0f,1.0f);
98
      mCenter= new Static3D(0,0,0);
99

    
100
      MatrixEffectMove moveEffect        = new MatrixEffectMove(mMove);
101
      MatrixEffectScale scaleEffect      = new MatrixEffectScale(mScale);
102
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, mCenter);
103
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, mCenter);
104

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

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

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

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

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

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

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

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

    
165
        mScreen.attach(mNode[object]);
166

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

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
    void setRenderModeToOIT(boolean oit)
178
      {
179
      mScreen.setOrderIndependentTransparency(oit);
180
      }
181

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

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

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
    
191
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
192
      {
193
      for(int i=0; i<NUM_OBJECTS; i++)
194
        {
195
        mTex[i].setColor(OBJECTS[NUM * i + 3]);
196
        }
197

    
198
      PostprocessEffectBlur.enable();
199
      FragmentEffectAlpha.enable();
200

    
201
      try
202
        {
203
        DistortedLibrary.onCreate(mView.getContext());
204
        }
205
      catch(Exception ex)
206
        {
207
        android.util.Log.e("Transparency", ex.getMessage() );
208
        }
209
      }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

    
213
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
214
      {
215
      float size= 0.02f*OBJ_SIZE;
216
      mScreenMin = width<height ? width:height;
217

    
218
      float factor = 0.65f*mScreenMin/OBJ_SIZE;
219
      mScale.set(factor,factor,factor);
220
      mCenter.set((float)OBJ_SIZE/2, (float)OBJ_SIZE/2, 0 );
221
      mMove.set( (width -factor*OBJ_SIZE)/2 ,(height-factor*OBJ_SIZE)/2 ,0);
222

    
223
      for(int i=0; i<NUM_OBJECTS; i++)
224
        {
225
        mMoveVector[i].set(size*OBJECTS[NUM*i], size*OBJECTS[NUM*i+1], size*OBJECTS[NUM*i+2]);
226
        }
227

    
228
      mScreen.resize(width, height);
229
      }
230
}
(2-2/3)