Project

General

Profile

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

examples / src / main / java / org / distorted / examples / multiblur / MultiblurRenderer.java @ 9bea2f59

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.DistortedFramebuffer;
31
import org.distorted.library.DistortedTexture;
32
import org.distorted.library.EffectTypes;
33
import org.distorted.library.MeshCubes;
34
import org.distorted.library.MeshObject;
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 MeshObject mMesh;
70
    private DistortedFramebuffer mScreen;
71
    private DynamicQuat mQuatInt1, mQuatInt2;
72

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

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

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

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

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

    
90
      mMesh = new MeshCubes(1,1,false);
91

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

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

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

    
104
      mScreen = new DistortedFramebuffer(0);
105
      }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
   
109
    public void onDrawFrame(GL10 glUnused) 
110
      {
111
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
112

    
113
      long time = System.currentTimeMillis();
114

    
115
      for(int i=0; i<NUM_OBJECTS; i++)
116
        mScreen.renderTo( i<NUM_OBJECTS/2 ? mTex1:mTex2, mMesh, mEffects[i], time );
117
      }
118

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

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

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

    
144
      mScreen.resize(width, height);
145
      }
146

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

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

    
172
      mTex1.setTexture(bitmap1);
173
      mTex2.setTexture(bitmap2);
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)