Project

General

Profile

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

examples / src / main / java / org / distorted / examples / transparency / TransparencyRenderer.java @ 8392dc7b

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
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 8392dc7b Leszek Koltunski
import org.distorted.library.effect.VertexEffectScale;
30 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
31 664a0e45 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedNode;
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.main.DistortedTexture;
35 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
36 664a0e45 Leszek Koltunski
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39
40
import javax.microedition.khronos.egl.EGLConfig;
41
import javax.microedition.khronos.opengles.GL10;
42
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45
class TransparencyRenderer implements GLSurfaceView.Renderer
46
{
47
    private static final int NUM = 4; // 4 ints (x,y,z, argb) each describe 1 object below
48
49
    private static final int[] OBJECTS =
50
        {
51 a4d59c0b Leszek Koltunski
         +9, +10, +2, 0xffff0000,
52
         -9, -10, -2, 0xffffff00
53 664a0e45 Leszek Koltunski
        };
54
55
    private static final int NUM_OBJECTS = OBJECTS.length/NUM;
56
    private static final int OBJ_SIZE    = 100;
57
58
    private GLSurfaceView mView;
59
    private DistortedNode[] mNode;
60
    private DistortedTexture[] mTex;
61
    private Static3D[]  mMoveVector;
62
    private Static1D[]  mAlphaVector;
63
    private DistortedScreen mScreen;
64 16b22aab Leszek Koltunski
    private Static3D mScale;
65 29aba2d1 Leszek Koltunski
    private PostprocessEffectBlur[] mBlur;
66
    private boolean[] mBlurApplied;
67
    private DistortedEffects[] mEffects;
68 664a0e45 Leszek Koltunski
69
    Static4D mQuat1, mQuat2;
70
    int mScreenMin;
71
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74
    TransparencyRenderer(GLSurfaceView v)
75
      {
76
      mView = v;
77
78 f3ca895f Leszek Koltunski
      MeshRectangles mesh = new MeshRectangles(1,1);
79 664a0e45 Leszek Koltunski
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 29aba2d1 Leszek Koltunski
      mBlur         = new PostprocessEffectBlur[NUM_OBJECTS];
91
      mEffects      = new DistortedEffects[NUM_OBJECTS];
92
      mBlurApplied  = new boolean[NUM_OBJECTS];
93 664a0e45 Leszek Koltunski
94 d7413405 Leszek Koltunski
      FragmentEffectAlpha[] alpha  = new FragmentEffectAlpha[NUM_OBJECTS];
95 664a0e45 Leszek Koltunski
96
      mScale = new Static3D(1.0f,1.0f,1.0f);
97 16b22aab Leszek Koltunski
      Static3D center= new Static3D(0,0,0);
98 664a0e45 Leszek Koltunski
99
      MatrixEffectScale scaleEffect      = new MatrixEffectScale(mScale);
100 16b22aab Leszek Koltunski
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, center);
101
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, center);
102 664a0e45 Leszek Koltunski
103
      for(int i=0; i<NUM_OBJECTS; i++)
104
        {
105 687263cc Leszek Koltunski
        mTex[i]          = new DistortedTexture();
106 664a0e45 Leszek Koltunski
        mMoveVector[i]   = new Static3D(0,0,0);
107
        mAlphaVector[i]  = new Static1D(0.5f);
108 29aba2d1 Leszek Koltunski
        mBlur[i]         = new PostprocessEffectBlur(new Static1D(0));
109
        mBlurApplied[i]  = true;
110 664a0e45 Leszek Koltunski
        alpha[i]         = new FragmentEffectAlpha(mAlphaVector[i]);
111 698ad0a8 Leszek Koltunski
        mEffects[i]      = new DistortedEffects();
112 29aba2d1 Leszek Koltunski
        mEffects[i].apply(mBlur[i]);
113
        mEffects[i].apply(alpha[i]);
114 8392dc7b Leszek Koltunski
        mEffects[i].apply( new VertexEffectScale(new Static3D(OBJ_SIZE,OBJ_SIZE,0) ) );
115 29aba2d1 Leszek Koltunski
        mEffects[i].apply(new MatrixEffectMove(mMoveVector[i]));
116 0ba5de00 Leszek Koltunski
        mEffects[i].apply(quatEffect2);
117
        mEffects[i].apply(quatEffect1);
118
        mEffects[i].apply(scaleEffect);
119 29aba2d1 Leszek Koltunski
120
        mNode[i] = new DistortedNode(mTex[i], mEffects[i], mesh );
121 664a0e45 Leszek Koltunski
        mScreen.attach(mNode[i]);
122
        }
123
      }
124
125 29aba2d1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
126
127
    void setPostprocess(int object, boolean doIt)
128
      {
129
      if( object>=0 && object<NUM_OBJECTS)
130
        {
131
        if( doIt && !mBlurApplied[object] )
132
          {
133
          mBlurApplied[object] = true;
134
          mEffects[object].apply(mBlur[object]);
135
          }
136
        if( !doIt && mBlurApplied[object] )
137
          {
138
          mBlurApplied[object] = false;
139
          mEffects[object].abortEffect(mBlur[object]);
140
          }
141
        }
142
      }
143
144 664a0e45 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
145
146
    void setTransparency(int object, float level)
147
      {
148
      if( object>=0 && object<NUM_OBJECTS) mAlphaVector[object].set(level);
149
      }
150
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
// no better way to attach mNode[object] as the first child than detaching all and reattaching back...
153
154
    void setRenderFirst(int object)
155
      {
156
      if( object>=0 && object<NUM_OBJECTS)
157
        {
158
        for(int i=0; i<NUM_OBJECTS; i++)
159
          {
160
          mScreen.detach(mNode[i]);
161
          }
162
163
        mScreen.attach(mNode[object]);
164
165
        for(int i=0; i<NUM_OBJECTS; i++)
166
          {
167
          if( i!=object )
168
            mScreen.attach(mNode[i]);
169
          }
170
        }
171
      }
172
173 a935e9db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
174
175
    void setRenderModeToOIT(boolean oit)
176
      {
177
      mScreen.setOrderIndependentTransparency(oit);
178
      }
179
180 664a0e45 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
181
182
    public void onDrawFrame(GL10 glUnused) 
183
      {
184
      mScreen.render(System.currentTimeMillis());
185
      }
186
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
    
189
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
190
      {
191
      for(int i=0; i<NUM_OBJECTS; i++)
192
        {
193
        mTex[i].setColor(OBJECTS[NUM * i + 3]);
194
        }
195
196 8392dc7b Leszek Koltunski
      VertexEffectScale.enable();
197 664a0e45 Leszek Koltunski
      PostprocessEffectBlur.enable();
198
      FragmentEffectAlpha.enable();
199
200
      try
201
        {
202 e3900503 Leszek Koltunski
        DistortedLibrary.onCreate(mView.getContext());
203 664a0e45 Leszek Koltunski
        }
204
      catch(Exception ex)
205
        {
206
        android.util.Log.e("Transparency", ex.getMessage() );
207
        }
208
      }
209
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
213
      {
214 b424b062 Leszek Koltunski
      float size= 0.02f*OBJ_SIZE;
215 8392dc7b Leszek Koltunski
      mScreenMin = Math.min(width, height);
216 664a0e45 Leszek Koltunski
217 a4d59c0b Leszek Koltunski
      float factor = 0.65f*mScreenMin/OBJ_SIZE;
218 664a0e45 Leszek Koltunski
      mScale.set(factor,factor,factor);
219
220
      for(int i=0; i<NUM_OBJECTS; i++)
221
        {
222
        mMoveVector[i].set(size*OBJECTS[NUM*i], size*OBJECTS[NUM*i+1], size*OBJECTS[NUM*i+2]);
223
        }
224
225
      mScreen.resize(width, height);
226
      }
227
}