Project

General

Profile

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

examples / src / main / java / org / distorted / examples / multiblur / MultiblurRenderer.java @ 6637d0f2

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.multiblur;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLES30;
25
import android.opengl.GLSurfaceView;
26

    
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29
import org.distorted.library.DistortedEffects;
30
import org.distorted.library.DistortedScreen;
31
import org.distorted.library.DistortedTexture;
32
import org.distorted.library.EffectNames;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.MeshCubes;
35
import org.distorted.library.type.DynamicQuat;
36
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39

    
40
import java.io.IOException;
41
import java.io.InputStream;
42

    
43
import javax.microedition.khronos.egl.EGLConfig;
44
import javax.microedition.khronos.opengles.GL10;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
class MultiblurRenderer implements GLSurfaceView.Renderer
49
{
50
    private static final int[] MOVE_VEC =
51
        {
52
        +1,+1,+1,
53
        +1,-1,+1,
54
        +1,+1,-1,
55
        +1,-1,-1,
56
        -1,+1,+1,
57
        -1,-1,+1,
58
        -1,+1,-1,
59
        -1,-1,-1
60
        };
61

    
62
    private static final int NUM_OBJECTS = MOVE_VEC.length/3;
63
    private static final int OBJ_SIZE    = 100;
64

    
65
    private GLSurfaceView mView;
66
    private DistortedTexture mTex1, mTex2;
67
    private DistortedEffects[] mEffects;
68
    private Static3D[] mMoveVector;
69
    private DistortedScreen mScreen;
70
    private DynamicQuat mQuatInt1, mQuatInt2;
71

    
72
    Static4D mQuat1, mQuat2;
73
    int mScreenMin;
74
    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
    MultiblurRenderer(GLSurfaceView v)
78
      {
79
      mView = v;
80

    
81
      mMoveVector = new Static3D[NUM_OBJECTS];
82
      for(int i=0; i<NUM_OBJECTS; i++) mMoveVector[i] = new Static3D(0,0,0);
83

    
84
      mEffects = new DistortedEffects[NUM_OBJECTS];
85
      for(int i=0; i<NUM_OBJECTS; i++) mEffects[i] = new DistortedEffects();
86

    
87
      mEffects[0].blur(new Static1D(10));
88

    
89
      MeshCubes mesh = new MeshCubes(1,1,false);
90

    
91
      mTex1 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
92
      mTex2 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
93

    
94
      mQuat1 = new Static4D(0,0,0,1);  // unity
95
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
96
      
97
      mQuatInt1 = new DynamicQuat(0,0.5f);
98
      mQuatInt2 = new DynamicQuat(0,0.5f);
99

    
100
      mQuatInt1.add(mQuat1);
101
      mQuatInt2.add(mQuat2);
102

    
103
      mScreen = new DistortedScreen();
104

    
105
      for(int i=0; i<NUM_OBJECTS; i++)
106
        mScreen.attach( i<NUM_OBJECTS/2 ? mTex1:mTex2, mEffects[i], mesh);
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
   
111
    public void onDrawFrame(GL10 glUnused) 
112
      {
113
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
114
      mScreen.render( System.currentTimeMillis() );
115
      }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
    
119
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
120
      {
121
      mScreenMin = width<height ? width:height;
122
    	
123
      Static3D center = new Static3D( (float)OBJ_SIZE/2, (float)OBJ_SIZE/2, 0.0f );
124
      float factor = 0.15f*mScreenMin/OBJ_SIZE;
125
      float xMove  =(width -factor*OBJ_SIZE)/2;
126
      float yMove  =(height-factor*OBJ_SIZE)/2;
127
      float size   = 1.3f*OBJ_SIZE;
128

    
129
      for(int i=0; i<NUM_OBJECTS; i++)
130
        {
131
        mEffects[i].abortEffects(EffectTypes.MATRIX);
132

    
133
        mMoveVector[i].set(xMove,yMove,0);
134
        mEffects[i].move(mMoveVector[i]);
135
        mEffects[i].scale(factor);
136
        mEffects[i].quaternion(mQuatInt1, center);
137
        mEffects[i].quaternion(mQuatInt2, center);
138
        mMoveVector[i].set(size*MOVE_VEC[3*i], size*MOVE_VEC[3*i+1], size*MOVE_VEC[3*i+2]);
139
        mEffects[i].move(mMoveVector[i]);
140
        }
141

    
142
      mScreen.resize(width, height);
143
      }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
    
147
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
148
      {
149
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
150

    
151
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.cat);
152
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.dog);
153
      Bitmap bitmap1,bitmap2;
154
        
155
      try 
156
        {
157
        bitmap1 = BitmapFactory.decodeStream(is1);
158
        bitmap2 = BitmapFactory.decodeStream(is2);
159
        }
160
      finally 
161
        {
162
        try 
163
          {
164
          is1.close();
165
          is2.close();
166
          }
167
        catch(IOException e) { }
168
        }  
169

    
170
      mTex1.setTexture(bitmap1);
171
      mTex2.setTexture(bitmap2);
172

    
173
      DistortedEffects.enableEffect(EffectNames.BLUR);
174

    
175
      try
176
        {
177
        Distorted.onCreate(mView.getContext());
178
        }
179
      catch(Exception ex)
180
        {
181
        android.util.Log.e("Multiblur", ex.getMessage() );
182
        }
183
      }
184
}
(2-2/3)