Project

General

Profile

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

examples / src / main / java / org / distorted / examples / catanddog / CatAndDogRenderer.java @ 625c67de

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.app.ActivityManager;
46
import android.content.Context;
47
import android.content.pm.ConfigurationInfo;
48
import android.content.res.Resources;
49
import android.graphics.Bitmap;
50
import android.graphics.BitmapFactory;
51
import android.opengl.GLSurfaceView;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
class CatAndDogRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
56
{
57
    private final GLSurfaceView mView;
58
    private final Resources mResources;
59
    private final DistortedTexture mTexture;
60
    private final DistortedScreen mScreen;
61
    private final Static3D mScaleStart, mScaleEnd, mMoveStart, mMoveEnd;
62

    
63
    private float mBmpRatio;
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
    CatAndDogRenderer(GLSurfaceView v)
68
      {
69
      final int DURATION_CHROMA_ALPHA   =  3000;
70
      final int DURATION_MATRIX_EFFECTS = 10000;
71

    
72
      mView = v;
73
      mResources = v.getResources();
74

    
75
      DistortedEffects effects = new DistortedEffects();
76

    
77
      mScaleStart= new Static3D(1,1,1);
78
      mScaleEnd  = new Static3D(1,1,1);
79
      Dynamic3D diScale = new Dynamic3D(DURATION_MATRIX_EFFECTS,0.0f);
80
      diScale.add(mScaleStart);
81
      diScale.add(mScaleEnd  );
82
      diScale.add(mScaleStart);
83
      diScale.setMode(Dynamic.MODE_PATH);
84
      effects.apply( new MatrixEffectScale( diScale ));
85

    
86
      Dynamic1D diRotate = new Dynamic1D(DURATION_MATRIX_EFFECTS,0.0f);
87
      diRotate.add(new Static1D(  0));
88
      diRotate.add(new Static1D(360));
89
      effects.apply( new MatrixEffectRotate( diRotate, new Static3D(0,0,1), new Static3D(0,0,0)) );
90

    
91
      mMoveStart= new Static3D(0,0,0);
92
      mMoveEnd  = new Static3D(0,0,0);
93
      Dynamic3D diMove= new Dynamic3D(DURATION_MATRIX_EFFECTS,0.0f);
94
      diMove.add( mMoveStart );
95
      diMove.add( mMoveEnd   );
96
      effects.apply( new MatrixEffectMove( diMove ));
97

    
98
      Dynamic1D diChroma = new Dynamic1D(DURATION_CHROMA_ALPHA,0.0f);
99
      diChroma.add(new Static1D(1));
100
      diChroma.add(new Static1D(0));
101
      Static3D chromaCenter= new Static3D( -0.25f, 0, 0 );
102
      Static3D chromaRegion= new Static3D( 0.2f, 0.3f, 0.2f );
103
      effects.apply( new FragmentEffectChroma( diChroma, new Static3D(1,0,0), chromaCenter, chromaRegion ,true) );
104

    
105
      Dynamic1D diAlpha = new Dynamic1D(DURATION_CHROMA_ALPHA,0.0f);
106
      diAlpha.add(new Static1D(1));
107
      diAlpha.add(new Static1D(0));
108
      Static3D alphaCenter = new Static3D( +0.25f, 0, 0 );
109
      Static3D alphaRegion = new Static3D( 0.2f, 0.3f, 0.2f );
110
      effects.apply( new FragmentEffectAlpha( diAlpha, alphaCenter, alphaRegion, false) );
111

    
112
      mTexture = new DistortedTexture();
113
      mScreen  = new DistortedScreen();
114

    
115
      mScreen.attach(mTexture,effects,new MeshQuad());
116
      }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
121
      {
122
      final float SCALE_START = 0.3f;
123
      final float SCALE_END   = 0.6f;
124
      float min= (Math.min(width,height));
125

    
126
      mScaleStart.set( SCALE_START*min, SCALE_START*min*mBmpRatio, 1 );
127
      mScaleEnd  .set( SCALE_END  *min, SCALE_END  *min*mBmpRatio, 1 );
128

    
129
      mMoveStart.set( 0.5f*width - (SCALE_START/2)*min ,  0.5f*height - SCALE_START/2*min*mBmpRatio, 0);
130
      mMoveEnd  .set(-0.5f*width + (SCALE_START/2)*min , -0.5f*height + SCALE_START/2*min*mBmpRatio, 0);
131

    
132
      mScreen.resize(width, height);
133
      }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
   
137
    public void onDrawFrame(GL10 glUnused) 
138
      {
139
      mScreen.render( System.currentTimeMillis() );
140
      }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
    
144
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
145
      {
146
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat_and_dog);
147
      Bitmap bitmap;
148
        
149
      try 
150
        {
151
        bitmap = BitmapFactory.decodeStream(is);
152
        } 
153
      finally 
154
        {
155
        try 
156
          {
157
          is.close();
158
          } 
159
        catch(IOException ignored) { }
160
        }  
161
      
162
      mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
163
      mTexture.setTexture(bitmap);
164

    
165
      FragmentEffectChroma.enable();
166
      FragmentEffectAlpha.enable();
167

    
168
      DistortedLibrary.onSurfaceCreated(this);
169
      }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
    public void distortedException(Exception ex)
174
      {
175
      android.util.Log.e("CatAndDog", ex.getMessage() );
176
      }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
    public InputStream localFile(int fileID)
181
      {
182
      return mResources.openRawResource(fileID);
183
      }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
    public void logMessage(String message)
188
      {
189
      android.util.Log.e("CatAndDog", message );
190
      }
191
}
(2-2/3)