Project

General

Profile

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

examples / src / main / java / org / distorted / examples / catanddog / CatAndDogRenderer.java @ 849e0034

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 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
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 f26ab2fd Leszek Koltunski
class CatAndDogRenderer implements GLSurfaceView.Renderer
52 427ab7bf Leszek Koltunski
{
53 d381aa80 Leszek Koltunski
    private static final int DURATION_CHROMA_ALPHA   =  3000;
54
    private static final int DURATION_MATRIX_EFFECTS = 10000;
55 fa9b6494 Leszek Koltunski
56 427ab7bf Leszek Koltunski
    private GLSurfaceView mView;
57 d04a4886 Leszek Koltunski
    private DistortedEffects mEffects;
58 f3ca895f Leszek Koltunski
    private MeshRectangles mMesh;
59 b0ebdf5e Leszek Koltunski
    private DistortedTexture mTexture;
60 d218d64e leszek
    private DistortedScreen mScreen;
61 fa9b6494 Leszek Koltunski
    private int mObjHeight, mObjWidth;
62 d381aa80 Leszek Koltunski
    private Static3D mMoveStartingPoint, mMoveEndPoint;
63 59759251 Leszek Koltunski
64 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66 6f3024ae Leszek Koltunski
    CatAndDogRenderer(GLSurfaceView v)
67 427ab7bf Leszek Koltunski
      {   
68
      mView = v;
69 40eef4b9 Leszek Koltunski
70 f3ca895f Leszek Koltunski
      mMesh = new MeshRectangles(1,1);  // no vertex effects, grid can be a (1x1) quad.
71 40eef4b9 Leszek Koltunski
72 d381aa80 Leszek Koltunski
      Dynamic3D moveDyn= new Dynamic3D(DURATION_MATRIX_EFFECTS,0.0f);
73 fa9b6494 Leszek Koltunski
74 d381aa80 Leszek Koltunski
      mMoveStartingPoint = new Static3D(0,0,0);
75
      mMoveEndPoint      = new Static3D(0,0,0);
76
77
      moveDyn.add(mMoveStartingPoint);
78
      moveDyn.add(mMoveEndPoint);
79 fa9b6494 Leszek Koltunski
80 849e0034 Leszek Koltunski
      Static3D chromaCenter= new Static3D( 130,  50, 0   );
81 1585ba24 Leszek Koltunski
      Static3D chromaRegion= new Static3D( 100, 100, 100 );
82 849e0034 Leszek Koltunski
      Static3D alphaCenter = new Static3D(-170,  50, 0   );
83 1585ba24 Leszek Koltunski
      Static3D alphaRegion = new Static3D( 100, 100, 100 );
84 40eef4b9 Leszek Koltunski
85 d381aa80 Leszek Koltunski
      Dynamic1D chromaDyn = new Dynamic1D(DURATION_CHROMA_ALPHA,0.0f);
86 40eef4b9 Leszek Koltunski
      chromaDyn.add(new Static1D(1));
87
      chromaDyn.add(new Static1D(0));
88 d381aa80 Leszek Koltunski
      Dynamic1D alphaDyn = new Dynamic1D(DURATION_CHROMA_ALPHA,0.0f);
89 40eef4b9 Leszek Koltunski
      alphaDyn.add(new Static1D(1));
90
      alphaDyn.add(new Static1D(0));
91 d381aa80 Leszek Koltunski
      Dynamic3D diScale = new Dynamic3D(DURATION_MATRIX_EFFECTS,0.0f);
92 fa9b6494 Leszek Koltunski
      diScale.add(new Static3D(1,1,1));
93
      diScale.add(new Static3D(0.33f,0.33f,1));
94 d381aa80 Leszek Koltunski
      diScale.add(new Static3D(1,1,1));
95
      diScale.setMode(Dynamic.MODE_PATH);
96
      Dynamic1D diRotate = new Dynamic1D(DURATION_MATRIX_EFFECTS,0.0f);
97 fa9b6494 Leszek Koltunski
      diRotate.add(new Static1D(  0));
98
      diRotate.add(new Static1D(360));
99 40eef4b9 Leszek Koltunski
100 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
101 1585ba24 Leszek Koltunski
      mEffects.apply( new FragmentEffectChroma( chromaDyn, new Static3D(1,0,0), chromaCenter, chromaRegion ,true) );
102
      mEffects.apply( new FragmentEffectAlpha(alphaDyn, alphaCenter, alphaRegion, false) );
103 81dd14c8 Leszek Koltunski
      mEffects.apply( new MatrixEffectMove(moveDyn));
104 fa9b6494 Leszek Koltunski
      mEffects.apply( new MatrixEffectScale(diScale));
105 d381aa80 Leszek Koltunski
      mEffects.apply( new MatrixEffectRotate( diRotate, new Static3D(0,0,1), new Static3D(0,0,0)) );
106 392e16fd Leszek Koltunski
107 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
108 427ab7bf Leszek Koltunski
      }
109
110 fa9b6494 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
113
      {
114 d381aa80 Leszek Koltunski
      mMoveStartingPoint.set( (width-mObjWidth)/2, (height-mObjHeight)/2, 0);
115
      mMoveEndPoint     .set(-(width-mObjWidth)/2,-(height-mObjHeight)/2, 0);
116
117 fa9b6494 Leszek Koltunski
      mScreen.resize(width, height);
118
      }
119 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
120
   
121
    public void onDrawFrame(GL10 glUnused) 
122
      {
123 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
124 427ab7bf Leszek Koltunski
      }
125
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
129 e7a4ef16 Leszek Koltunski
      {
130 427ab7bf Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat_and_dog);
131
      Bitmap bitmap;
132
        
133
      try 
134
        {
135
        bitmap = BitmapFactory.decodeStream(is);
136
        } 
137
      finally 
138
        {
139
        try 
140
          {
141
          is.close();
142
          } 
143
        catch(IOException e) { }
144
        }  
145
      
146 fa9b6494 Leszek Koltunski
      mObjHeight = bitmap.getHeight();
147
      mObjWidth  = bitmap.getWidth();
148 e8b6aa95 Leszek Koltunski
149 698ad0a8 Leszek Koltunski
      mMesh.setStretch(mObjWidth,mObjHeight,0);
150 687263cc Leszek Koltunski
151
      if( mTexture==null ) mTexture = new DistortedTexture();
152 b0ebdf5e Leszek Koltunski
      mTexture.setTexture(bitmap);
153 fe59d375 Leszek Koltunski
154
      mScreen.detachAll();
155 b0ebdf5e Leszek Koltunski
      mScreen.attach(mTexture,mEffects,mMesh);
156 59759251 Leszek Koltunski
157 885b9cca leszek
      FragmentEffectChroma.enable();
158
      FragmentEffectAlpha.enable();
159 6637d0f2 Leszek Koltunski
160 427ab7bf Leszek Koltunski
      try
161
        {
162 e3900503 Leszek Koltunski
        DistortedLibrary.onCreate(mView.getContext());
163 427ab7bf Leszek Koltunski
        }
164
      catch(Exception ex)
165
        {
166 a4d59c0b Leszek Koltunski
        android.util.Log.e("CatAndDog", ex.getMessage() );
167 427ab7bf Leszek Koltunski
        }
168
      }
169
}