Project

General

Profile

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

examples / src / main / java / org / distorted / examples / transparency / TransparencyRenderer.java @ 0ba5de00

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 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
30 664a0e45 Leszek Koltunski
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 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
35 664a0e45 Leszek Koltunski
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 a4d59c0b Leszek Koltunski
         +9, +10, +2, 0xffff0000,
51
         -9, -10, -2, 0xffffff00
52 664a0e45 Leszek Koltunski
        };
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 16b22aab Leszek Koltunski
    private Static3D mScale;
64 29aba2d1 Leszek Koltunski
    private PostprocessEffectBlur[] mBlur;
65
    private boolean[] mBlurApplied;
66
    private DistortedEffects[] mEffects;
67 664a0e45 Leszek Koltunski
68
    Static4D mQuat1, mQuat2;
69
    int mScreenMin;
70
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
73
    TransparencyRenderer(GLSurfaceView v)
74
      {
75
      mView = v;
76
77 f3ca895f Leszek Koltunski
      MeshRectangles mesh = new MeshRectangles(1,1);
78 698ad0a8 Leszek Koltunski
      mesh.setStretch(OBJ_SIZE,OBJ_SIZE,0);
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
        mEffects[i].apply(new MatrixEffectMove(mMoveVector[i]));
115 0ba5de00 Leszek Koltunski
        mEffects[i].apply(quatEffect2);
116
        mEffects[i].apply(quatEffect1);
117
        mEffects[i].apply(scaleEffect);
118 29aba2d1 Leszek Koltunski
119
        mNode[i] = new DistortedNode(mTex[i], mEffects[i], mesh );
120 664a0e45 Leszek Koltunski
        mScreen.attach(mNode[i]);
121
        }
122
      }
123
124 29aba2d1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
125
126
    void setPostprocess(int object, boolean doIt)
127
      {
128
      if( object>=0 && object<NUM_OBJECTS)
129
        {
130
        if( doIt && !mBlurApplied[object] )
131
          {
132
          mBlurApplied[object] = true;
133
          mEffects[object].apply(mBlur[object]);
134
          }
135
        if( !doIt && mBlurApplied[object] )
136
          {
137
          mBlurApplied[object] = false;
138
          mEffects[object].abortEffect(mBlur[object]);
139
          }
140
        }
141
      }
142
143 664a0e45 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
144
145
    void setTransparency(int object, float level)
146
      {
147
      if( object>=0 && object<NUM_OBJECTS) mAlphaVector[object].set(level);
148
      }
149
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
// no better way to attach mNode[object] as the first child than detaching all and reattaching back...
152
153
    void setRenderFirst(int object)
154
      {
155
      if( object>=0 && object<NUM_OBJECTS)
156
        {
157
        for(int i=0; i<NUM_OBJECTS; i++)
158
          {
159
          mScreen.detach(mNode[i]);
160
          }
161
162
        mScreen.attach(mNode[object]);
163
164
        for(int i=0; i<NUM_OBJECTS; i++)
165
          {
166
          if( i!=object )
167
            mScreen.attach(mNode[i]);
168
          }
169
        }
170
      }
171
172 a935e9db Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
173
174
    void setRenderModeToOIT(boolean oit)
175
      {
176
      mScreen.setOrderIndependentTransparency(oit);
177
      }
178
179 664a0e45 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
180
181
    public void onDrawFrame(GL10 glUnused) 
182
      {
183
      mScreen.render(System.currentTimeMillis());
184
      }
185
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
    
188
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
189
      {
190
      for(int i=0; i<NUM_OBJECTS; i++)
191
        {
192
        mTex[i].setColor(OBJECTS[NUM * i + 3]);
193
        }
194
195
      PostprocessEffectBlur.enable();
196
      FragmentEffectAlpha.enable();
197
198
      try
199
        {
200 e3900503 Leszek Koltunski
        DistortedLibrary.onCreate(mView.getContext());
201 664a0e45 Leszek Koltunski
        }
202
      catch(Exception ex)
203
        {
204
        android.util.Log.e("Transparency", ex.getMessage() );
205
        }
206
      }
207
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
210
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
211
      {
212 b424b062 Leszek Koltunski
      float size= 0.02f*OBJ_SIZE;
213 664a0e45 Leszek Koltunski
      mScreenMin = width<height ? width:height;
214
215 a4d59c0b Leszek Koltunski
      float factor = 0.65f*mScreenMin/OBJ_SIZE;
216 664a0e45 Leszek Koltunski
      mScale.set(factor,factor,factor);
217
218
      for(int i=0; i<NUM_OBJECTS; i++)
219
        {
220
        mMoveVector[i].set(size*OBJECTS[NUM*i], size*OBJECTS[NUM*i+1], size*OBJECTS[NUM*i+2]);
221
        }
222
223
      mScreen.resize(width, height);
224
      }
225
}