Project

General

Profile

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

examples / src / main / java / org / distorted / examples / transparency / TransparencyRenderer.java @ d7413405

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.Distorted;
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.main.MeshFlat;
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

    
65
    Static4D mQuat1, mQuat2;
66
    int mScreenMin;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
    TransparencyRenderer(GLSurfaceView v)
71
      {
72
      mView = v;
73

    
74
      MeshFlat mesh = new MeshFlat(1,1);
75

    
76
      mQuat1 = new Static4D(0,0,0,1);  // unity
77
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
78

    
79
      mScreen = new DistortedScreen();
80
      mScreen.glClearColor(1.0f,1.0f,1.0f,1.0f);
81

    
82
      mMoveVector   = new Static3D[NUM_OBJECTS];
83
      mAlphaVector  = new Static1D[NUM_OBJECTS];
84
      mNode         = new DistortedNode[NUM_OBJECTS];
85
      mTex          = new DistortedTexture[NUM_OBJECTS];
86

    
87
      FragmentEffectAlpha[] alpha  = new FragmentEffectAlpha[NUM_OBJECTS];
88
      DistortedEffects[] effects   = new DistortedEffects[NUM_OBJECTS];
89
      PostprocessEffectBlur[] blur = new PostprocessEffectBlur[NUM_OBJECTS];
90

    
91
      mMove  = new Static3D(0,0,0);
92
      mScale = new Static3D(1.0f,1.0f,1.0f);
93
      mCenter= new Static3D(0,0,0);
94

    
95
      MatrixEffectMove moveEffect        = new MatrixEffectMove(mMove);
96
      MatrixEffectScale scaleEffect      = new MatrixEffectScale(mScale);
97
      MatrixEffectQuaternion quatEffect1 = new MatrixEffectQuaternion(mQuat1, mCenter);
98
      MatrixEffectQuaternion quatEffect2 = new MatrixEffectQuaternion(mQuat2, mCenter);
99

    
100

    
101
      for(int i=0; i<NUM_OBJECTS; i++)
102
        {
103
        mTex[i]          = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
104
        mMoveVector[i]   = new Static3D(0,0,0);
105
        mAlphaVector[i]  = new Static1D(0.5f);
106
        alpha[i]         = new FragmentEffectAlpha(mAlphaVector[i]);
107
        blur[i]          = new PostprocessEffectBlur(new Static1D(0));
108
        effects[i]       = new DistortedEffects();
109

    
110
        effects[i].apply(blur[i]);
111
        effects[i].apply(alpha[i]);
112
        effects[i].apply(moveEffect);
113
        effects[i].apply(scaleEffect);
114
        effects[i].apply(quatEffect1);
115
        effects[i].apply(quatEffect2);
116
        effects[i].apply(new MatrixEffectMove(mMoveVector[i]));
117

    
118
        mNode[i] = new DistortedNode(mTex[i], effects[i], mesh );
119
        mScreen.attach(mNode[i]);
120
        }
121
      }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
    void setTransparency(int object, float level)
126
      {
127
      if( object>=0 && object<NUM_OBJECTS) mAlphaVector[object].set(level);
128
      }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
// no better way to attach mNode[object] as the first child than detaching all and reattaching back...
132

    
133
    void setRenderFirst(int object)
134
      {
135
      if( object>=0 && object<NUM_OBJECTS)
136
        {
137
        for(int i=0; i<NUM_OBJECTS; i++)
138
          {
139
          mScreen.detach(mNode[i]);
140
          }
141

    
142
        mScreen.attach(mNode[object]);
143

    
144
        for(int i=0; i<NUM_OBJECTS; i++)
145
          {
146
          if( i!=object )
147
            mScreen.attach(mNode[i]);
148
          }
149
        }
150
      }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
    public void onDrawFrame(GL10 glUnused) 
155
      {
156
      mScreen.render(System.currentTimeMillis());
157
      }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
    
161
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
162
      {
163
      for(int i=0; i<NUM_OBJECTS; i++)
164
        {
165
        mTex[i].setColor(OBJECTS[NUM * i + 3]);
166
        }
167

    
168
      PostprocessEffectBlur.enable();
169
      FragmentEffectAlpha.enable();
170

    
171
      try
172
        {
173
        Distorted.onCreate(mView.getContext());
174
        }
175
      catch(Exception ex)
176
        {
177
        android.util.Log.e("Transparency", ex.getMessage() );
178
        }
179
      }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
184
      {
185
      mScreenMin = width<height ? width:height;
186

    
187
      float factor = 0.70f*mScreenMin/OBJ_SIZE;
188
      mScale.set(factor,factor,factor);
189
      mCenter.set((float)OBJ_SIZE/2, (float)OBJ_SIZE/2, -(float)OBJ_SIZE/2 );
190
      mMove.set( (width -factor*OBJ_SIZE)/2 ,(height-factor*OBJ_SIZE)/2 ,0);
191

    
192
      float size= 0.02f*OBJ_SIZE;
193

    
194
      for(int i=0; i<NUM_OBJECTS; i++)
195
        {
196
        mMoveVector[i].set(size*OBJECTS[NUM*i], size*OBJECTS[NUM*i+1], size*OBJECTS[NUM*i+2]);
197
        }
198

    
199
      mScreen.resize(width, height);
200
      }
201
}
(2-2/3)