Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differenteffects / DifferentEffectsRenderer.java @ d218d64e

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

    
22
import java.io.IOException;
23
import java.io.InputStream;
24

    
25
import javax.microedition.khronos.egl.EGLConfig;
26
import javax.microedition.khronos.opengles.GL10;
27

    
28
import org.distorted.examples.R;
29

    
30
import org.distorted.library.Distorted;
31
import org.distorted.library.DistortedEffects;
32
import org.distorted.library.DistortedFramebuffer;
33
import org.distorted.library.DistortedScreen;
34
import org.distorted.library.MeshFlat;
35
import org.distorted.library.DistortedTexture;
36
import org.distorted.library.EffectTypes;
37
import org.distorted.library.type.Dynamic1D;
38
import org.distorted.library.type.Dynamic3D;
39
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42

    
43
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
45
import android.opengl.GLES30;
46
import android.opengl.GLSurfaceView;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
class DifferentEffectsRenderer implements GLSurfaceView.Renderer 
51
{
52
   private static final int NUM = 3;
53
   
54
   private GLSurfaceView mView;
55
   private DistortedEffects[] mEffects;
56
   private DistortedTexture mTexture;
57
   private MeshFlat mMesh;
58
   private DistortedScreen mScreen;
59
   private int bmpHeight, bmpWidth;
60
    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
   DifferentEffectsRenderer(GLSurfaceView v)
64
      {     
65
      mView = v;
66
      
67
      // mEffects[0] effects
68
      Static3D pLeft = new Static3D(214, 206, 0);
69
      Static3D pRight= new Static3D(390, 212, 0);
70
      Static4D RegionEye = new Static4D(0,0,60,60);
71
      
72
      // mEffects[1] effects
73
      Dynamic3D dyn = new Dynamic3D(1000,0.0f);
74
      dyn.add(new Static3D( 50,0,0));
75
      dyn.add(new Static3D(-50,0,0));
76
      Static3D pNose1 = new Static3D(305, 340, 0);
77
      
78
      // we don't need to prepare anything for mEffects[2] effects
79

    
80
      mEffects = new DistortedEffects[NUM];
81

    
82
      for(int i=0; i<NUM; i++) mEffects[i] = new DistortedEffects();
83

    
84
      Dynamic1D sink = new Dynamic1D(2000,0.0f);
85
      sink.add(new Static1D( 1));
86
      sink.add(new Static1D(10));
87

    
88
      mEffects[0].sink(sink, pLeft, RegionEye);
89
      mEffects[0].sink(sink, pRight,RegionEye);
90
      mEffects[1].distort(dyn, pNose1);
91

    
92
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
93
      chromaDyn.add(new Static1D(0));
94
      chromaDyn.add(new Static1D(1));
95

    
96
      mEffects[2].chroma(chromaDyn, new Static3D(0,1,0) );
97

    
98
      mScreen = new DistortedScreen();
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
   
103
   public void onDrawFrame(GL10 glUnused)
104
     {
105
     GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
106
      
107
     long time = System.currentTimeMillis();
108
   
109
     for(int i=NUM-1; i>=0; i--) mScreen.renderTo(mTexture, mMesh, mEffects[i], time);
110
     }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
    
114
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
115
     {
116
     for(int i=NUM-1; i>=0; i--)
117
       {
118
       mEffects[i].abortEffects(EffectTypes.MATRIX);
119
       }
120
      
121
     if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
122
       {
123
       int w = (height*bmpWidth)/bmpHeight;
124
       float factor = (float)height/bmpHeight;
125

    
126
       for(int i=NUM-1; i>=0; i--)
127
         {
128
         mEffects[i].move( new Static3D((width-NUM*w)/2 +i*w , 0, 0) );
129
         mEffects[i].scale(factor);
130
         }
131
       }
132
     else
133
       {
134
       int w = width/NUM;
135
       int h = (width*bmpHeight)/(bmpWidth*NUM);
136
       float factor = (float)width/(bmpWidth*NUM);
137

    
138
       for(int i=NUM-1; i>=0; i--)
139
         {
140
         mEffects[i].move( new Static3D(i*w, (height-h)/2, 0) );
141
         mEffects[i].scale(factor);
142
         }
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 is = mView.getContext().getResources().openRawResource(R.raw.dog);
155
     Bitmap bitmap;
156
        
157
     try
158
       {
159
       bitmap = BitmapFactory.decodeStream(is);
160
       }
161
     finally
162
       {
163
       try
164
         {
165
         is.close();
166
         }
167
       catch(IOException e) { }
168
       }
169
      
170
     bmpHeight = bitmap.getHeight();
171
     bmpWidth  = bitmap.getWidth();
172

    
173
     mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
174
     mTexture  = new DistortedTexture(bmpWidth,bmpHeight);
175

    
176
     mTexture.setTexture(bitmap);
177

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