Project

General

Profile

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

examples / src / main / java / org / distorted / examples / catanddog / CatAndDogRenderer.java @ 7451c98a

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.Distorted;
30
import org.distorted.library.DistortedEffectQueues;
31
import org.distorted.library.DistortedTexture;
32
import org.distorted.library.GridFlat;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Dynamic3D;
36
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39

    
40
import android.graphics.Bitmap;
41
import android.graphics.BitmapFactory;
42
import android.opengl.GLES20;
43
import android.opengl.GLSurfaceView;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
class CatAndDogRenderer implements GLSurfaceView.Renderer
48
{
49
    private GLSurfaceView mView;
50
    private DistortedEffectQueues mQueues;
51
    private DistortedTexture mTexture;
52
    private GridFlat mGrid;
53
    private int bmpHeight, bmpWidth;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
    public CatAndDogRenderer(GLSurfaceView v)
58
      {   
59
      mView = v;
60

    
61
      mGrid   = new GridFlat(1,1);  // no vertex effects, grid can be a (1x1) quad.
62
      mQueues = new DistortedEffectQueues();
63

    
64
      Static4D chromaRegion= new Static4D( 530, 200,100,100);
65
      Static4D alphaRegion = new Static4D( 230, 200,100,100);
66

    
67
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
68
      chromaDyn.add(new Static1D(1));
69
      chromaDyn.add(new Static1D(0));
70

    
71
      mQueues.chroma(chromaDyn, new Static3D(1,0,0), chromaRegion ,true);
72

    
73
      Dynamic1D alphaDyn = new Dynamic1D(3000,0.0f);
74
      alphaDyn.add(new Static1D(1));
75
      alphaDyn.add(new Static1D(0));
76

    
77
      mQueues.alpha( alphaDyn, alphaRegion, false );
78
      }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
   
82
    public void onDrawFrame(GL10 glUnused) 
83
      {
84
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
85
      mQueues.draw(System.currentTimeMillis(), mTexture, mGrid);
86
      }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
    
90
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
91
      {
92
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
93

    
94
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat_and_dog);
95
      Bitmap bitmap;
96
        
97
      try 
98
        {
99
        bitmap = BitmapFactory.decodeStream(is);
100
        } 
101
      finally 
102
        {
103
        try 
104
          {
105
          is.close();
106
          } 
107
        catch(IOException e) { }
108
        }  
109
      
110
      bmpHeight = bitmap.getHeight();
111
      bmpWidth  = bitmap.getWidth();
112

    
113
      mTexture = new DistortedTexture(bmpWidth,bmpHeight);
114
      mTexture.setTexture(bitmap);
115

    
116
      try
117
        {
118
        Distorted.onSurfaceCreated(mView.getContext());
119
        }
120
      catch(Exception ex)
121
        {
122
        android.util.Log.e("Renderer", ex.getMessage() );
123
        }
124
      }
125
    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
129
      {
130
      int duration = 10000;   
131
      
132
      Dynamic3D diMove = new Dynamic3D(duration,0.0f);
133
      diMove.add(new Static3D(width-bmpWidth,height-bmpHeight,0));
134
      diMove.add(new Static3D(0,0,0));
135
      
136
      Dynamic3D diScale = new Dynamic3D(duration,0.0f);
137
      diScale.add(new Static3D(1,1,1));
138
      diScale.add(new Static3D(0.33f,0.33f,1));
139
      
140
      Dynamic1D diRotate = new Dynamic1D(duration,0.0f);
141
      diRotate.add(new Static1D(  0));
142
      diRotate.add(new Static1D(360));
143
      
144
      mQueues.abortEffects(EffectTypes.MATRIX);
145

    
146
      mQueues.move(diMove);
147
      mQueues.scale(diScale);
148
      mQueues.rotate( diRotate, new Static3D(0,0,1), new Static3D(bmpWidth/2,bmpHeight/2,0) );
149

    
150
      Distorted.onSurfaceChanged(width, height); 
151
      }
152
}
(2-2/3)