Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differenteffects / DifferentEffectsRenderer.java @ 5055b5d4

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