Project

General

Profile

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

examples / src / main / java / org / distorted / examples / catanddog / CatAndDogRenderer.java @ 061449ed

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 f26ab2fd Leszek Koltunski
package org.distorted.examples.catanddog;
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 fa9b6494 Leszek Koltunski
import org.distorted.library.effect.FragmentEffectAlpha;
30
import org.distorted.library.effect.FragmentEffectChroma;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectRotate;
33
import org.distorted.library.effect.MatrixEffectScale;
34 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
35 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedScreen;
37
import org.distorted.library.main.DistortedTexture;
38 936333aa Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
39 d381aa80 Leszek Koltunski
import org.distorted.library.type.Dynamic;
40 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
41
import org.distorted.library.type.Dynamic3D;
42
import org.distorted.library.type.Static1D;
43
import org.distorted.library.type.Static3D;
44 427ab7bf Leszek Koltunski
45
import android.graphics.Bitmap;
46
import android.graphics.BitmapFactory;
47
import android.opengl.GLSurfaceView;
48
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51 061449ed Leszek Koltunski
class CatAndDogRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
52 427ab7bf Leszek Koltunski
{
53
    private GLSurfaceView mView;
54 b0ebdf5e Leszek Koltunski
    private DistortedTexture mTexture;
55 d218d64e leszek
    private DistortedScreen mScreen;
56 936333aa Leszek Koltunski
    private Static3D mScaleStart, mScaleEnd, mMoveStart, mMoveEnd;
57
    private float mBmpRatio;
58 59759251 Leszek Koltunski
59 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 6f3024ae Leszek Koltunski
    CatAndDogRenderer(GLSurfaceView v)
62 936333aa Leszek Koltunski
      {
63
      final int DURATION_CHROMA_ALPHA   =  3000;
64
      final int DURATION_MATRIX_EFFECTS = 10000;
65 d381aa80 Leszek Koltunski
66 936333aa Leszek Koltunski
      mView = v;
67 fa9b6494 Leszek Koltunski
68 936333aa Leszek Koltunski
      DistortedEffects effects = new DistortedEffects();
69 40eef4b9 Leszek Koltunski
70 936333aa Leszek Koltunski
      mScaleStart= new Static3D(1,1,1);
71
      mScaleEnd  = new Static3D(1,1,1);
72 d381aa80 Leszek Koltunski
      Dynamic3D diScale = new Dynamic3D(DURATION_MATRIX_EFFECTS,0.0f);
73 936333aa Leszek Koltunski
      diScale.add(mScaleStart);
74
      diScale.add(mScaleEnd  );
75
      diScale.add(mScaleStart);
76 d381aa80 Leszek Koltunski
      diScale.setMode(Dynamic.MODE_PATH);
77 936333aa Leszek Koltunski
      effects.apply( new MatrixEffectScale( diScale ));
78
79 d381aa80 Leszek Koltunski
      Dynamic1D diRotate = new Dynamic1D(DURATION_MATRIX_EFFECTS,0.0f);
80 fa9b6494 Leszek Koltunski
      diRotate.add(new Static1D(  0));
81
      diRotate.add(new Static1D(360));
82 936333aa Leszek Koltunski
      effects.apply( new MatrixEffectRotate( diRotate, new Static3D(0,0,1), new Static3D(0,0,0)) );
83
84
      mMoveStart= new Static3D(0,0,0);
85
      mMoveEnd  = new Static3D(0,0,0);
86
      Dynamic3D diMove= new Dynamic3D(DURATION_MATRIX_EFFECTS,0.0f);
87
      diMove.add( mMoveStart );
88
      diMove.add( mMoveEnd   );
89
      effects.apply( new MatrixEffectMove( diMove ));
90
91
      Dynamic1D diChroma = new Dynamic1D(DURATION_CHROMA_ALPHA,0.0f);
92
      diChroma.add(new Static1D(1));
93
      diChroma.add(new Static1D(0));
94
      Static3D chromaCenter= new Static3D( -0.25f, 0, 0 );
95
      Static3D chromaRegion= new Static3D( 0.2f, 0.3f, 0.2f );
96
      effects.apply( new FragmentEffectChroma( diChroma, new Static3D(1,0,0), chromaCenter, chromaRegion ,true) );
97
98
      Dynamic1D diAlpha = new Dynamic1D(DURATION_CHROMA_ALPHA,0.0f);
99
      diAlpha.add(new Static1D(1));
100
      diAlpha.add(new Static1D(0));
101
      Static3D alphaCenter = new Static3D( +0.25f, 0, 0 );
102
      Static3D alphaRegion = new Static3D( 0.2f, 0.3f, 0.2f );
103
      effects.apply( new FragmentEffectAlpha( diAlpha, alphaCenter, alphaRegion, false) );
104
105
      mTexture = new DistortedTexture();
106
      mScreen  = new DistortedScreen();
107
108
      mScreen.attach(mTexture,effects,new MeshQuad());
109 427ab7bf Leszek Koltunski
      }
110
111 fa9b6494 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
112
113
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
114
      {
115 936333aa Leszek Koltunski
      final float SCALE_START = 0.3f;
116
      final float SCALE_END   = 0.6f;
117
      float min= (width>height ? height : width);
118
119
      mScaleStart.set( SCALE_START*min, SCALE_START*min*mBmpRatio, 1 );
120
      mScaleEnd  .set( SCALE_END  *min, SCALE_END  *min*mBmpRatio, 1 );
121
122
      mMoveStart.set( 0.5f*width - (SCALE_START/2)*min ,  0.5f*height - SCALE_START/2*min*mBmpRatio, 0);
123
      mMoveEnd  .set(-0.5f*width + (SCALE_START/2)*min , -0.5f*height + SCALE_START/2*min*mBmpRatio, 0);
124 d381aa80 Leszek Koltunski
125 fa9b6494 Leszek Koltunski
      mScreen.resize(width, height);
126
      }
127 c7a31368 Leszek Koltunski
128 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
129
   
130
    public void onDrawFrame(GL10 glUnused) 
131
      {
132 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
133 427ab7bf Leszek Koltunski
      }
134
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
    
137
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
138 e7a4ef16 Leszek Koltunski
      {
139 427ab7bf Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat_and_dog);
140
      Bitmap bitmap;
141
        
142
      try 
143
        {
144
        bitmap = BitmapFactory.decodeStream(is);
145
        } 
146
      finally 
147
        {
148
        try 
149
          {
150
          is.close();
151
          } 
152
        catch(IOException e) { }
153
        }  
154
      
155 936333aa Leszek Koltunski
      mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
156 b0ebdf5e Leszek Koltunski
      mTexture.setTexture(bitmap);
157 fe59d375 Leszek Koltunski
158 885b9cca leszek
      FragmentEffectChroma.enable();
159
      FragmentEffectAlpha.enable();
160 6637d0f2 Leszek Koltunski
161 061449ed Leszek Koltunski
      DistortedLibrary.onCreate(mView.getContext(),this);
162
      }
163
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
166
    public void distortedException(Exception ex)
167
      {
168
      android.util.Log.e("CatAndDog", ex.getMessage() );
169 427ab7bf Leszek Koltunski
      }
170
}