Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differenteffects / DifferentEffectsRenderer.java @ 7451c98a

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 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
32 40eef4b9 Leszek Koltunski
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.DistortedEffectQueues;
34 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
35 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
36 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
37 59759251 Leszek Koltunski
import org.distorted.library.type.Static1D;
38 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40 427ab7bf Leszek Koltunski
41
import android.graphics.Bitmap;
42
import android.graphics.BitmapFactory;
43
import android.opengl.GLES20;
44
import android.opengl.GLSurfaceView;
45
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48
class DifferentEffectsRenderer implements GLSurfaceView.Renderer 
49
{
50
   private static final int NUM = 3;
51
   
52
   private GLSurfaceView mView;
53 40eef4b9 Leszek Koltunski
   private DistortedEffectQueues[] mQueue;
54
   private DistortedTexture mTexture;
55 10b7e588 Leszek Koltunski
   private GridFlat mGrid;
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 40eef4b9 Leszek Koltunski
      // mQueue[0] effects
65
      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 40eef4b9 Leszek Koltunski
      // mQueue[1] effects
70
      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 40eef4b9 Leszek Koltunski
      // we don't need to prepare anything for mQueue[2] effects
76
77
      mQueue= new DistortedEffectQueues[NUM];
78
79
      for(int i=0; i<NUM; i++) mQueue[i] = new DistortedEffectQueues();
80
81
      Dynamic1D sink = new Dynamic1D(2000,0.0f);
82
      sink.add(new Static1D( 1));
83
      sink.add(new Static1D(10));
84
85
      mQueue[0].sink(sink, pLeft, RegionEye);
86
      mQueue[0].sink(sink, pRight,RegionEye);
87
      mQueue[1].distort(dyn, pNose1);
88
89
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
90
      chromaDyn.add(new Static1D(0));
91
      chromaDyn.add(new Static1D(1));
92
93
      mQueue[2].chroma(chromaDyn, new Static3D(0,1,0) );
94 427ab7bf Leszek Koltunski
      }
95
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
   
98 5055b5d4 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
99
     {
100
     GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
101 427ab7bf Leszek Koltunski
      
102 5055b5d4 Leszek Koltunski
     long time = System.currentTimeMillis();
103 427ab7bf Leszek Koltunski
   
104 40eef4b9 Leszek Koltunski
     for(int i=NUM-1; i>=0; i--) mQueue[i].draw(time, mTexture, mGrid);
105 5055b5d4 Leszek Koltunski
     }
106 427ab7bf Leszek Koltunski
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109 5055b5d4 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
110
     {
111
     for(int i=NUM-1; i>=0; i--)
112
       {
113 40eef4b9 Leszek Koltunski
       mQueue[i].abortEffects(EffectTypes.MATRIX);
114 5055b5d4 Leszek Koltunski
       }
115 427ab7bf Leszek Koltunski
      
116 5055b5d4 Leszek Koltunski
     if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
117
       {
118
       int w = (height*bmpWidth)/bmpHeight;
119
       float factor = (float)height/bmpHeight;
120
121
       for(int i=NUM-1; i>=0; i--)
122
         {
123 40eef4b9 Leszek Koltunski
         mQueue[i].move( new Static3D((width-NUM*w)/2 +i*w , 0, 0) );
124
         mQueue[i].scale(factor);
125 5055b5d4 Leszek Koltunski
         }
126
       }
127
     else
128
       {
129
       int w = width/NUM;
130
       int h = (width*bmpHeight)/(bmpWidth*NUM);
131
       float factor = (float)width/(bmpWidth*NUM);
132
133
       for(int i=NUM-1; i>=0; i--)
134
         {
135 40eef4b9 Leszek Koltunski
         mQueue[i].move( new Static3D(i*w, (height-h)/2, 0) );
136
         mQueue[i].scale(factor);
137 5055b5d4 Leszek Koltunski
         }
138
       }
139 427ab7bf Leszek Koltunski
       
140 5055b5d4 Leszek Koltunski
     Distorted.onSurfaceChanged(width, height);
141
     }
142 427ab7bf Leszek Koltunski
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
    
145 5055b5d4 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
146
     {
147
     GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
148 e7a4ef16 Leszek Koltunski
149 5055b5d4 Leszek Koltunski
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
150
     Bitmap bitmap;
151 427ab7bf Leszek Koltunski
        
152 5055b5d4 Leszek Koltunski
     try
153
       {
154
       bitmap = BitmapFactory.decodeStream(is);
155
       }
156
     finally
157
       {
158
       try
159
         {
160
         is.close();
161
         }
162
       catch(IOException e) { }
163
       }
164 427ab7bf Leszek Koltunski
      
165 5055b5d4 Leszek Koltunski
     bmpHeight = bitmap.getHeight();
166
     bmpWidth  = bitmap.getWidth();
167 e8b6aa95 Leszek Koltunski
168 40eef4b9 Leszek Koltunski
     mGrid     = new GridFlat(30,30*bmpHeight/bmpWidth);
169 7451c98a Leszek Koltunski
     mTexture  = new DistortedTexture(bmpWidth,bmpHeight);
170 e8b6aa95 Leszek Koltunski
171 40eef4b9 Leszek Koltunski
     mTexture.setTexture(bitmap);
172 7bf107f7 Leszek Koltunski
173 5055b5d4 Leszek Koltunski
     try
174
       {
175
       Distorted.onSurfaceCreated(mView.getContext());
176
       }
177
     catch(Exception ex)
178
       {
179
       android.util.Log.e("DifferentEffects", ex.getMessage() );
180
       }
181
     }
182 427ab7bf Leszek Koltunski
}