Project

General

Profile

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

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

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.effect.VertexEffectScale;
30
import org.distorted.library.main.DistortedLibrary;
31
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
import org.distorted.library.mesh.MeshRectangles;
36
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
         +9, +10, +2, 0xffff0000,
52
         -9, -10, -2, 0xffffff00
53
        };
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
    private Static3D mScale;
65
    private PostprocessEffectBlur[] mBlur;
66
    private boolean[] mBlurApplied;
67
    private DistortedEffects[] mEffects;
68

    
69
    Static4D mQuat1, mQuat2;
70
    int mScreenMin;
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

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

    
78
      MeshRectangles mesh = new MeshRectangles(1,1);
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
      mScale = new Static3D(1.0f,1.0f,1.0f);
97
      Static3D center= new Static3D(0,0,0);
98

    
99
      MatrixEffectScale scaleEffect      = new MatrixEffectScale(mScale);
100
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, center);
101
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, center);
102

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

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

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
    void setRenderModeToOIT(boolean oit)
176
      {
177
      mScreen.setOrderIndependentTransparency(oit);
178
      }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
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
      VertexEffectScale.enable();
197
      PostprocessEffectBlur.enable();
198
      FragmentEffectAlpha.enable();
199

    
200
      try
201
        {
202
        DistortedLibrary.onCreate(mView.getContext());
203
        }
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
      float size= 0.02f*OBJ_SIZE;
215
      mScreenMin = Math.min(width, height);
216

    
217
      float factor = 0.65f*mScreenMin/OBJ_SIZE;
218
      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
}
(2-2/3)