Project

General

Profile

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

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

1 bc0a685b 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 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.differenteffects;
21 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
29 427ab7bf Leszek Koltunski
30 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
31 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
32 d218d64e leszek
import org.distorted.library.DistortedScreen;
33 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
34 40eef4b9 Leszek Koltunski
import org.distorted.library.DistortedTexture;
35 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
36 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
37 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
38 59759251 Leszek Koltunski
import org.distorted.library.type.Static1D;
39 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41 427ab7bf Leszek Koltunski
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
45 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49
class DifferentEffectsRenderer implements GLSurfaceView.Renderer 
50
{
51
   private static final int NUM = 3;
52
   
53
   private GLSurfaceView mView;
54 d04a4886 Leszek Koltunski
   private DistortedEffects[] mEffects;
55 d218d64e leszek
   private DistortedScreen mScreen;
56 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
57
    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 6c097dd3 Leszek Koltunski
   DifferentEffectsRenderer(GLSurfaceView v)
61 427ab7bf Leszek Koltunski
      {     
62
      mView = v;
63
      
64 392e16fd Leszek Koltunski
      // mEffects[0] effects
65 40eef4b9 Leszek Koltunski
      Static3D pLeft = new Static3D(214, 206, 0);
66
      Static3D pRight= new Static3D(390, 212, 0);
67
      Static4D RegionEye = new Static4D(0,0,60,60);
68 427ab7bf Leszek Koltunski
      
69 392e16fd Leszek Koltunski
      // mEffects[1] effects
70 40eef4b9 Leszek Koltunski
      Dynamic3D dyn = new Dynamic3D(1000,0.0f);
71
      dyn.add(new Static3D( 50,0,0));
72
      dyn.add(new Static3D(-50,0,0));
73
      Static3D pNose1 = new Static3D(305, 340, 0);
74 427ab7bf Leszek Koltunski
      
75 392e16fd Leszek Koltunski
      // we don't need to prepare anything for mEffects[2] effects
76 40eef4b9 Leszek Koltunski
77 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects[NUM];
78 40eef4b9 Leszek Koltunski
79 d04a4886 Leszek Koltunski
      for(int i=0; i<NUM; i++) mEffects[i] = new DistortedEffects();
80 40eef4b9 Leszek Koltunski
81
      Dynamic1D sink = new Dynamic1D(2000,0.0f);
82
      sink.add(new Static1D( 1));
83
      sink.add(new Static1D(10));
84
85 392e16fd Leszek Koltunski
      mEffects[0].sink(sink, pLeft, RegionEye);
86
      mEffects[0].sink(sink, pRight,RegionEye);
87
      mEffects[1].distort(dyn, pNose1);
88 40eef4b9 Leszek Koltunski
89
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
90
      chromaDyn.add(new Static1D(0));
91
      chromaDyn.add(new Static1D(1));
92
93 392e16fd Leszek Koltunski
      mEffects[2].chroma(chromaDyn, new Static3D(0,1,0) );
94
95 d218d64e leszek
      mScreen = new DistortedScreen();
96 427ab7bf Leszek Koltunski
      }
97
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
   
100 5055b5d4 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
101
     {
102 41a81a14 Leszek Koltunski
     GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
103 fe59d375 Leszek Koltunski
     mScreen.render( System.currentTimeMillis() );
104 5055b5d4 Leszek Koltunski
     }
105 427ab7bf Leszek Koltunski
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
    
108 5055b5d4 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
109
     {
110
     for(int i=NUM-1; i>=0; i--)
111
       {
112 392e16fd Leszek Koltunski
       mEffects[i].abortEffects(EffectTypes.MATRIX);
113 5055b5d4 Leszek Koltunski
       }
114 427ab7bf Leszek Koltunski
      
115 5055b5d4 Leszek Koltunski
     if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
116
       {
117
       int w = (height*bmpWidth)/bmpHeight;
118
       float factor = (float)height/bmpHeight;
119
120
       for(int i=NUM-1; i>=0; i--)
121
         {
122 392e16fd Leszek Koltunski
         mEffects[i].move( new Static3D((width-NUM*w)/2 +i*w , 0, 0) );
123
         mEffects[i].scale(factor);
124 5055b5d4 Leszek Koltunski
         }
125
       }
126
     else
127
       {
128
       int w = width/NUM;
129
       int h = (width*bmpHeight)/(bmpWidth*NUM);
130
       float factor = (float)width/(bmpWidth*NUM);
131
132
       for(int i=NUM-1; i>=0; i--)
133
         {
134 392e16fd Leszek Koltunski
         mEffects[i].move( new Static3D(i*w, (height-h)/2, 0) );
135
         mEffects[i].scale(factor);
136 5055b5d4 Leszek Koltunski
         }
137
       }
138 427ab7bf Leszek Koltunski
       
139 392e16fd Leszek Koltunski
     mScreen.resize(width, height);
140 5055b5d4 Leszek Koltunski
     }
141 427ab7bf Leszek Koltunski
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
    
144 5055b5d4 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
145
     {
146 41a81a14 Leszek Koltunski
     GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
147 e7a4ef16 Leszek Koltunski
148 5055b5d4 Leszek Koltunski
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
149
     Bitmap bitmap;
150 427ab7bf Leszek Koltunski
        
151 5055b5d4 Leszek Koltunski
     try
152
       {
153
       bitmap = BitmapFactory.decodeStream(is);
154
       }
155
     finally
156
       {
157
       try
158
         {
159
         is.close();
160
         }
161
       catch(IOException e) { }
162
       }
163 427ab7bf Leszek Koltunski
      
164 5055b5d4 Leszek Koltunski
     bmpHeight = bitmap.getHeight();
165
     bmpWidth  = bitmap.getWidth();
166 e8b6aa95 Leszek Koltunski
167 fe59d375 Leszek Koltunski
     MeshFlat mesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
168
     DistortedTexture texture  = new DistortedTexture(bmpWidth,bmpHeight);
169
     texture.setTexture(bitmap);
170 e8b6aa95 Leszek Koltunski
171 fe59d375 Leszek Koltunski
     mScreen.detachAll();
172
     for(int i=NUM-1; i>=0; i--) mScreen.attach(texture, mEffects[i], mesh);
173 7bf107f7 Leszek Koltunski
174 5055b5d4 Leszek Koltunski
     try
175
       {
176 76f9798b Leszek Koltunski
       Distorted.onCreate(mView.getContext());
177 5055b5d4 Leszek Koltunski
       }
178
     catch(Exception ex)
179
       {
180
       android.util.Log.e("DifferentEffects", ex.getMessage() );
181
       }
182
     }
183 427ab7bf Leszek Koltunski
}