Project

General

Profile

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

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

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.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

    
63
    private GLSurfaceView mView;
64
    private DistortedTexture mTex1, mTex2;
65
    private DistortedEffects[] mEffects;
66
    private Static3D[] mMoveVector;
67
    private MeshObject mMesh;
68
    private DistortedFramebuffer mScreen;
69
    private DynamicQuat mQuatInt1, mQuatInt2;
70
    private int mObjWidth, mObjHeight;
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
      mMesh = new MeshCubes(1,1,false);
88

    
89
      mTex1 = new DistortedTexture(100,100);
90
      mTex2 = new DistortedTexture(100,100);
91

    
92
      mObjWidth = mTex1.getWidth();
93
      mObjHeight= mTex2.getHeight();
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)mObjWidth/2, (float)mObjHeight/2, 0.0f );
126
      float factor = Math.min((0.15f*height)/mObjHeight,(0.15f*width )/mObjWidth);
127
      float xMove  =(width -factor*mObjWidth )/2;
128
      float yMove  =(height-factor*mObjHeight)/2;
129
      float size   = 1.3f*mObjWidth;
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)