Project

General

Profile

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

examples / src / main / java / org / distorted / examples / catanddog / CatAndDogRenderer.java @ 107e4b72

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