Project

General

Profile

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

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

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 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
33 d218d64e leszek
import org.distorted.library.DistortedScreen;
34 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
35 40eef4b9 Leszek Koltunski
import org.distorted.library.DistortedTexture;
36 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
37 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
38 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
39 59759251 Leszek Koltunski
import org.distorted.library.type.Static1D;
40 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42 427ab7bf Leszek Koltunski
43
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
45 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
46 427ab7bf Leszek Koltunski
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 d04a4886 Leszek Koltunski
   private DistortedEffects[] mEffects;
56 40eef4b9 Leszek Koltunski
   private DistortedTexture mTexture;
57 b01acdaf Leszek Koltunski
   private MeshFlat mMesh;
58 d218d64e leszek
   private DistortedScreen mScreen;
59 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
60
    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 6c097dd3 Leszek Koltunski
   DifferentEffectsRenderer(GLSurfaceView v)
64 427ab7bf Leszek Koltunski
      {     
65
      mView = v;
66
      
67 392e16fd Leszek Koltunski
      // mEffects[0] effects
68 40eef4b9 Leszek Koltunski
      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 427ab7bf Leszek Koltunski
      
72 392e16fd Leszek Koltunski
      // mEffects[1] effects
73 40eef4b9 Leszek Koltunski
      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 427ab7bf Leszek Koltunski
      
78 392e16fd Leszek Koltunski
      // we don't need to prepare anything for mEffects[2] effects
79 40eef4b9 Leszek Koltunski
80 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects[NUM];
81 40eef4b9 Leszek Koltunski
82 d04a4886 Leszek Koltunski
      for(int i=0; i<NUM; i++) mEffects[i] = new DistortedEffects();
83 40eef4b9 Leszek Koltunski
84
      Dynamic1D sink = new Dynamic1D(2000,0.0f);
85
      sink.add(new Static1D( 1));
86
      sink.add(new Static1D(10));
87
88 392e16fd Leszek Koltunski
      mEffects[0].sink(sink, pLeft, RegionEye);
89
      mEffects[0].sink(sink, pRight,RegionEye);
90
      mEffects[1].distort(dyn, pNose1);
91 40eef4b9 Leszek Koltunski
92
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
93
      chromaDyn.add(new Static1D(0));
94
      chromaDyn.add(new Static1D(1));
95
96 392e16fd Leszek Koltunski
      mEffects[2].chroma(chromaDyn, new Static3D(0,1,0) );
97
98 d218d64e leszek
      mScreen = new DistortedScreen();
99 427ab7bf Leszek Koltunski
      }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
   
103 5055b5d4 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
104
     {
105 41a81a14 Leszek Koltunski
     GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
106 427ab7bf Leszek Koltunski
      
107 5055b5d4 Leszek Koltunski
     long time = System.currentTimeMillis();
108 427ab7bf Leszek Koltunski
   
109 b01acdaf Leszek Koltunski
     for(int i=NUM-1; i>=0; i--) mScreen.renderTo(mTexture, mMesh, mEffects[i], time);
110 5055b5d4 Leszek Koltunski
     }
111 427ab7bf Leszek Koltunski
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
    
114 5055b5d4 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
115
     {
116
     for(int i=NUM-1; i>=0; i--)
117
       {
118 392e16fd Leszek Koltunski
       mEffects[i].abortEffects(EffectTypes.MATRIX);
119 5055b5d4 Leszek Koltunski
       }
120 427ab7bf Leszek Koltunski
      
121 5055b5d4 Leszek Koltunski
     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 392e16fd Leszek Koltunski
         mEffects[i].move( new Static3D((width-NUM*w)/2 +i*w , 0, 0) );
129
         mEffects[i].scale(factor);
130 5055b5d4 Leszek Koltunski
         }
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 392e16fd Leszek Koltunski
         mEffects[i].move( new Static3D(i*w, (height-h)/2, 0) );
141
         mEffects[i].scale(factor);
142 5055b5d4 Leszek Koltunski
         }
143
       }
144 427ab7bf Leszek Koltunski
       
145 392e16fd Leszek Koltunski
     mScreen.resize(width, height);
146 5055b5d4 Leszek Koltunski
     }
147 427ab7bf Leszek Koltunski
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
    
150 5055b5d4 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
151
     {
152 41a81a14 Leszek Koltunski
     GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
153 e7a4ef16 Leszek Koltunski
154 5055b5d4 Leszek Koltunski
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
155
     Bitmap bitmap;
156 427ab7bf Leszek Koltunski
        
157 5055b5d4 Leszek Koltunski
     try
158
       {
159
       bitmap = BitmapFactory.decodeStream(is);
160
       }
161
     finally
162
       {
163
       try
164
         {
165
         is.close();
166
         }
167
       catch(IOException e) { }
168
       }
169 427ab7bf Leszek Koltunski
      
170 5055b5d4 Leszek Koltunski
     bmpHeight = bitmap.getHeight();
171
     bmpWidth  = bitmap.getWidth();
172 e8b6aa95 Leszek Koltunski
173 b01acdaf Leszek Koltunski
     mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
174 7451c98a Leszek Koltunski
     mTexture  = new DistortedTexture(bmpWidth,bmpHeight);
175 e8b6aa95 Leszek Koltunski
176 40eef4b9 Leszek Koltunski
     mTexture.setTexture(bitmap);
177 7bf107f7 Leszek Koltunski
178 5055b5d4 Leszek Koltunski
     try
179
       {
180 76f9798b Leszek Koltunski
       Distorted.onCreate(mView.getContext());
181 5055b5d4 Leszek Koltunski
       }
182
     catch(Exception ex)
183
       {
184
       android.util.Log.e("DifferentEffects", ex.getMessage() );
185
       }
186
     }
187 427ab7bf Leszek Koltunski
}