Project

General

Profile

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

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

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.EffectName;
30
import org.distorted.library.effect.FragmentEffectAlpha;
31
import org.distorted.library.effect.FragmentEffectChroma;
32
import org.distorted.library.effect.MatrixEffectMove;
33
import org.distorted.library.effect.MatrixEffectRotate;
34
import org.distorted.library.effect.MatrixEffectScale;
35
import org.distorted.library.main.Distorted;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.main.MeshFlat;
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
import org.distorted.library.type.Static4D;
45

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

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
class CatAndDogRenderer implements GLSurfaceView.Renderer
53
{
54
    private static final int DURATION = 10000;  // 10 seconds
55

    
56
    private GLSurfaceView mView;
57
    private DistortedEffects mEffects;
58
    private MeshFlat mMesh;
59
    private DistortedTexture mTexture;
60
    private DistortedScreen mScreen;
61
    private int mObjHeight, mObjWidth;
62
    private Static3D mMove, mRotate;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
    CatAndDogRenderer(GLSurfaceView v)
67
      {   
68
      mView = v;
69

    
70
      mMesh = new MeshFlat(1,1);  // no vertex effects, grid can be a (1x1) quad.
71
      mEffects = new DistortedEffects();
72

    
73
      Dynamic3D move   = new Dynamic3D(DURATION,0.0f);
74
      mRotate = new Static3D(0,0,0);
75
      mMove   = new Static3D(0,0,0);
76
      Static3D move2  = new Static3D(0,0,0);
77

    
78
      move.add(mMove);
79
      move.add(move2);
80

    
81
      Static4D chromaRegion= new Static4D( 530, 200,100,100);
82
      Static4D alphaRegion = new Static4D( 230, 200,100,100);
83

    
84
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
85
      chromaDyn.add(new Static1D(1));
86
      chromaDyn.add(new Static1D(0));
87
      Dynamic1D alphaDyn = new Dynamic1D(3000,0.0f);
88
      alphaDyn.add(new Static1D(1));
89
      alphaDyn.add(new Static1D(0));
90
      Dynamic3D diScale = new Dynamic3D(DURATION,0.0f);
91
      diScale.add(new Static3D(1,1,1));
92
      diScale.add(new Static3D(0.33f,0.33f,1));
93
      Dynamic1D diRotate = new Dynamic1D(DURATION,0.0f);
94
      diRotate.add(new Static1D(  0));
95
      diRotate.add(new Static1D(360));
96

    
97
      mEffects.apply( new FragmentEffectChroma( chromaDyn, new Static3D(1,0,0), chromaRegion ,true) );
98
      mEffects.apply( new FragmentEffectAlpha(alphaDyn, alphaRegion, false) );
99
      mEffects.apply( new MatrixEffectMove(mMove));
100
      mEffects.apply( new MatrixEffectScale(diScale));
101
      mEffects.apply( new MatrixEffectRotate( diRotate, new Static3D(0,0,1), mRotate) );
102

    
103
      mScreen = new DistortedScreen(mView);
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
109
      {
110
      mMove.set(width-mObjWidth,height-mObjHeight,0);
111
      mRotate.set(mObjWidth/2,mObjHeight/2,0);
112
      mScreen.resize(width, height);
113
      }
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
   
116
    public void onDrawFrame(GL10 glUnused) 
117
      {
118
      mScreen.render( System.currentTimeMillis() );
119
      }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
    
123
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
124
      {
125
      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
      mObjHeight = bitmap.getHeight();
142
      mObjWidth  = bitmap.getWidth();
143

    
144
      if( mTexture==null ) mTexture = new DistortedTexture(mObjWidth,mObjHeight);
145
      mTexture.setTexture(bitmap);
146

    
147
      mScreen.detachAll();
148
      mScreen.attach(mTexture,mEffects,mMesh);
149

    
150
      DistortedEffects.enableEffect(EffectName.SMOOTH_CHROMA);
151
      DistortedEffects.enableEffect(EffectName.ALPHA);
152

    
153
      try
154
        {
155
        Distorted.onCreate(mView.getContext());
156
        }
157
      catch(Exception ex)
158
        {
159
        android.util.Log.e("Renderer", ex.getMessage() );
160
        }
161
      }
162
}
(2-2/3)