Project

General

Profile

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

examples / src / main / java / org / distorted / examples / catanddog / CatAndDogRenderer.java @ 6f3024ae

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

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

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
class CatAndDogRenderer implements GLSurfaceView.Renderer
49
{
50
    private GLSurfaceView mView;
51
    private DistortedEffects mEffects;
52
    private DistortedTexture mTexture;
53
    private GridFlat mGrid;
54
    private DistortedFramebuffer mScreen;
55
    private int bmpHeight, bmpWidth;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
    CatAndDogRenderer(GLSurfaceView v)
60
      {   
61
      mView = v;
62

    
63
      mGrid   = new GridFlat(1,1);  // no vertex effects, grid can be a (1x1) quad.
64
      mEffects = new DistortedEffects();
65

    
66
      Static4D chromaRegion= new Static4D( 530, 200,100,100);
67
      Static4D alphaRegion = new Static4D( 230, 200,100,100);
68

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

    
73
      mEffects.chroma(chromaDyn, new Static3D(1,0,0), chromaRegion ,true);
74

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

    
79
      mEffects.alpha( alphaDyn, alphaRegion, false );
80

    
81
      mScreen = new DistortedFramebuffer(0);
82
      }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
   
86
    public void onDrawFrame(GL10 glUnused) 
87
      {
88
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
89
      mScreen.renderTo(mTexture, mGrid, mEffects, System.currentTimeMillis() );
90
      }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
    
94
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
95
      {
96
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
97

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

    
117
      mTexture = new DistortedTexture(bmpWidth,bmpHeight);
118
      mTexture.setTexture(bitmap);
119

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

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

    
154
      mScreen.resize(width, height);
155
      }
156
}
(2-2/3)