Project

General

Profile

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

examples / src / main / java / org / distorted / examples / transparency / TransparencyRenderer.java @ 061449ed

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