Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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

    
20
package org.distorted.examples.catanddog;
21

    
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
import org.distorted.examples.R;
29
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
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedScreen;
37
import org.distorted.library.main.DistortedTexture;
38
import org.distorted.library.mesh.MeshQuad;
39
import org.distorted.library.type.Dynamic;
40
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

    
45
import android.graphics.Bitmap;
46
import android.graphics.BitmapFactory;
47
import android.opengl.GLSurfaceView;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
class CatAndDogRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
52
{
53
    private GLSurfaceView mView;
54
    private DistortedTexture mTexture;
55
    private DistortedScreen mScreen;
56
    private Static3D mScaleStart, mScaleEnd, mMoveStart, mMoveEnd;
57
    private float mBmpRatio;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
    CatAndDogRenderer(GLSurfaceView v)
62
      {
63
      final int DURATION_CHROMA_ALPHA   =  3000;
64
      final int DURATION_MATRIX_EFFECTS = 10000;
65

    
66
      mView = v;
67

    
68
      DistortedEffects effects = new DistortedEffects();
69

    
70
      mScaleStart= new Static3D(1,1,1);
71
      mScaleEnd  = new Static3D(1,1,1);
72
      Dynamic3D diScale = new Dynamic3D(DURATION_MATRIX_EFFECTS,0.0f);
73
      diScale.add(mScaleStart);
74
      diScale.add(mScaleEnd  );
75
      diScale.add(mScaleStart);
76
      diScale.setMode(Dynamic.MODE_PATH);
77
      effects.apply( new MatrixEffectScale( diScale ));
78

    
79
      Dynamic1D diRotate = new Dynamic1D(DURATION_MATRIX_EFFECTS,0.0f);
80
      diRotate.add(new Static1D(  0));
81
      diRotate.add(new Static1D(360));
82
      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
      }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
114
      {
115
      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

    
125
      mScreen.resize(width, height);
126
      }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
   
130
    public void onDrawFrame(GL10 glUnused) 
131
      {
132
      mScreen.render( System.currentTimeMillis() );
133
      }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
    
137
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
138
      {
139
      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
      mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
156
      mTexture.setTexture(bitmap);
157

    
158
      FragmentEffectChroma.enable();
159
      FragmentEffectAlpha.enable();
160

    
161
      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
      }
170
}
(2-2/3)