Project

General

Profile

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

examples / src / main / java / org / distorted / examples / macroblock / MacroblockRenderer.java @ 59759251

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.macroblock;
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.DistortedBitmap;
31
import org.distorted.library.EffectTypes;
32
import org.distorted.library.type.Dynamic1D;
33
import org.distorted.library.type.Dynamic3D;
34
import org.distorted.library.type.Static1D;
35
import org.distorted.library.type.Static2D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38

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

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
class MacroblockRenderer implements GLSurfaceView.Renderer 
47
{
48
    private GLSurfaceView mView;
49
    private DistortedBitmap macroblock;
50
    private Static4D macRegion, alphaRegion;
51
    private int bmpHeight, bmpWidth;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
    public MacroblockRenderer(GLSurfaceView v) 
56
      {   
57
      mView = v;
58
      
59
        macRegion = new Static4D( 530, 200,100,100);
60
      alphaRegion = new Static4D( 230, 200,100,100);
61
      }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
   
65
    public void onDrawFrame(GL10 glUnused) 
66
      {
67
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
68
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
69
   
70
      macroblock.draw(System.currentTimeMillis());
71
      }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
    
75
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
76
      {  
77
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat_and_dog);
78
      Bitmap bitmap;
79
        
80
      try 
81
        {
82
        bitmap = BitmapFactory.decodeStream(is);
83
        } 
84
      finally 
85
        {
86
        try 
87
          {
88
          is.close();
89
          } 
90
        catch(IOException e) { }
91
        }  
92
      
93
      bmpHeight = bitmap.getHeight();
94
      bmpWidth  = bitmap.getWidth();
95
      
96
      macroblock = new DistortedBitmap(bitmap, 30);
97

    
98
      Dynamic1D macroblockDyn = new Dynamic1D();
99
      macroblockDyn.setDuration(3000);
100
      macroblockDyn.setCount(0);
101
      macroblockDyn.add(new Static1D( 1));
102
      macroblockDyn.add(new Static1D(30));
103

    
104
      macroblock.macroblock(macroblockDyn, macRegion);
105

    
106
      Dynamic1D alphaDyn = new Dynamic1D();
107
      alphaDyn.setDuration(3000);
108
      alphaDyn.setCount(0);
109
      alphaDyn.add(new Static1D(1));
110
      alphaDyn.add(new Static1D(0));
111

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

    
150
      macroblock.move(diMove);
151
      macroblock.scale(diScale);
152
      macroblock.rotate( new Static3D(bmpWidth/2,bmpHeight/2,0), diRotate, new Static3D(0,0,1) );
153

    
154
      Distorted.onSurfaceChanged(width, height); 
155
      }
156
}
(2-2/3)