Project

General

Profile

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

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

1 58059374 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 d218d64e leszek
import org.distorted.library.DistortedScreen;
32 58059374 Leszek Koltunski
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.MeshCubes;
35
import org.distorted.library.MeshObject;
36
import org.distorted.library.type.DynamicQuat;
37 9bea2f59 Leszek Koltunski
import org.distorted.library.type.Static1D;
38 58059374 Leszek Koltunski
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40
41
import java.io.IOException;
42
import java.io.InputStream;
43
44
import javax.microedition.khronos.egl.EGLConfig;
45
import javax.microedition.khronos.opengles.GL10;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49
class MultiblurRenderer implements GLSurfaceView.Renderer
50
{
51
    private static final int[] MOVE_VEC =
52
        {
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
        -1,-1,-1
61
        };
62
63
    private static final int NUM_OBJECTS = MOVE_VEC.length/3;
64 e11b37f4 Leszek Koltunski
    private static final int OBJ_SIZE    = 100;
65 58059374 Leszek Koltunski
66
    private GLSurfaceView mView;
67
    private DistortedTexture mTex1, mTex2;
68
    private DistortedEffects[] mEffects;
69
    private Static3D[] mMoveVector;
70
    private MeshObject mMesh;
71 d218d64e leszek
    private DistortedScreen mScreen;
72 58059374 Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
73
74
    Static4D mQuat1, mQuat2;
75
    int mScreenMin;
76
    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
79
    MultiblurRenderer(GLSurfaceView v)
80
      {
81
      mView = v;
82
83
      mMoveVector = new Static3D[NUM_OBJECTS];
84
      for(int i=0; i<NUM_OBJECTS; i++) mMoveVector[i] = new Static3D(0,0,0);
85
86
      mEffects = new DistortedEffects[NUM_OBJECTS];
87
      for(int i=0; i<NUM_OBJECTS; i++) mEffects[i] = new DistortedEffects();
88
89 9bea2f59 Leszek Koltunski
      mEffects[0].blur(new Static1D(10));
90
91 58059374 Leszek Koltunski
      mMesh = new MeshCubes(1,1,false);
92
93 e11b37f4 Leszek Koltunski
      mTex1 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
94
      mTex2 = new DistortedTexture(OBJ_SIZE,OBJ_SIZE);
95 58059374 Leszek Koltunski
96
      mQuat1 = new Static4D(0,0,0,1);  // unity
97
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
98
      
99
      mQuatInt1 = new DynamicQuat(0,0.5f);
100
      mQuatInt2 = new DynamicQuat(0,0.5f);
101
102
      mQuatInt1.add(mQuat1);
103
      mQuatInt2.add(mQuat2);
104
105 d218d64e leszek
      mScreen = new DistortedScreen();
106 58059374 Leszek Koltunski
      }
107
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
   
110
    public void onDrawFrame(GL10 glUnused) 
111
      {
112
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
113
114
      long time = System.currentTimeMillis();
115
116
      for(int i=0; i<NUM_OBJECTS; i++)
117
        mScreen.renderTo( i<NUM_OBJECTS/2 ? mTex1:mTex2, mMesh, mEffects[i], time );
118
      }
119
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
    
122
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
123
      {
124
      mScreenMin = width<height ? width:height;
125
    	
126 e11b37f4 Leszek Koltunski
      Static3D center = new Static3D( (float)OBJ_SIZE/2, (float)OBJ_SIZE/2, 0.0f );
127
      float factor = 0.15f*mScreenMin/OBJ_SIZE;
128
      float xMove  =(width -factor*OBJ_SIZE)/2;
129
      float yMove  =(height-factor*OBJ_SIZE)/2;
130
      float size   = 1.3f*OBJ_SIZE;
131 58059374 Leszek Koltunski
132
      for(int i=0; i<NUM_OBJECTS; i++)
133
        {
134
        mEffects[i].abortEffects(EffectTypes.MATRIX);
135
136
        mMoveVector[i].set(xMove,yMove,0);
137
        mEffects[i].move(mMoveVector[i]);
138
        mEffects[i].scale(factor);
139
        mEffects[i].quaternion(mQuatInt1, center);
140
        mEffects[i].quaternion(mQuatInt2, center);
141
        mMoveVector[i].set(size*MOVE_VEC[3*i], size*MOVE_VEC[3*i+1], size*MOVE_VEC[3*i+2]);
142
        mEffects[i].move(mMoveVector[i]);
143
        }
144
145
      mScreen.resize(width, height);
146
      }
147
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
    
150
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
151
      {
152
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
153
154
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.cat);
155
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.dog);
156
      Bitmap bitmap1,bitmap2;
157
        
158
      try 
159
        {
160
        bitmap1 = BitmapFactory.decodeStream(is1);
161
        bitmap2 = BitmapFactory.decodeStream(is2);
162
        }
163
      finally 
164
        {
165
        try 
166
          {
167
          is1.close();
168
          is2.close();
169
          }
170
        catch(IOException e) { }
171
        }  
172
173
      mTex1.setTexture(bitmap1);
174
      mTex2.setTexture(bitmap2);
175
176
      try
177
        {
178
        Distorted.onCreate(mView.getContext());
179
        }
180
      catch(Exception ex)
181
        {
182
        android.util.Log.e("Multiblur", ex.getMessage() );
183
        }
184
      }
185
}