Project

General

Profile

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

examples / src / main / java / org / distorted / examples / multiblur / MultiblurRenderer.java @ fe59d375

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.EffectTypes;
33
import org.distorted.library.MeshCubes;
34
import org.distorted.library.type.DynamicQuat;
35
import org.distorted.library.type.Static1D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38

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

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

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
class MultiblurRenderer implements GLSurfaceView.Renderer
48
{
49
    private static final int[] MOVE_VEC =
50
        {
51
        +1,+1,+1,
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
        };
60

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

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

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

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

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

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

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

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

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

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

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

    
102
      mScreen = new DistortedScreen();
103

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

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

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

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

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

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

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

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

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

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