Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differenteffects / DifferentEffectsRenderer.java @ 10b7e588

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