Project

General

Profile

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

examples / src / main / java / org / distorted / examples / transparency / TransparencyRenderer.java @ 664a0e45

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

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

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

    
99
      PostprocessEffectBlur blur = new PostprocessEffectBlur(new Static1D(0));
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
        effects[i]       = new DistortedEffects();
108

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

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

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

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

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

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

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

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

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

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

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

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

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

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

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

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

    
191
      float size= 0.02f*OBJ_SIZE;
192

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

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