Project

General

Profile

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

examples / src / main / java / org / distorted / examples / catanddog / CatAndDogRenderer.java @ f3ca895f

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