Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differentbitmaps / DifferentBitmapsRenderer.java @ fe59d375

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.differentbitmaps;
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

    
30
import org.distorted.library.Distorted;
31
import org.distorted.library.DistortedScreen;
32
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.DistortedEffects;
34
import org.distorted.library.MeshFlat;
35
import org.distorted.library.EffectTypes;
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.GLES30;
44
import android.opengl.GLSurfaceView;
45

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

    
48
class DifferentBitmapsRenderer implements GLSurfaceView.Renderer 
49
{
50
   private static final int NUM = 3;
51
   
52
   private GLSurfaceView mView;
53
   private DistortedEffects[] mEffects;
54
   private DistortedScreen mScreen;
55
   private int bmpHeight, bmpWidth;
56
    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
   DifferentBitmapsRenderer(GLSurfaceView v)
60
      {     
61
      mView = v;
62
     
63
      Dynamic3D dDistort = new Dynamic3D(3000,0.0f);
64
      dDistort.add(new Static3D( 25,0,0));
65
      dDistort.add(new Static3D(-25,0,0));
66
      Static3D mPoint = new Static3D(305, 380, 0);
67

    
68
      mEffects = new DistortedEffects[NUM];
69
      mEffects[0] = new DistortedEffects();
70
      for(int i=1; i<NUM; i++)
71
        mEffects[i] = new DistortedEffects(mEffects[0], Distorted.CLONE_VERTEX|Distorted.CLONE_FRAGMENT);
72

    
73
      // Add the effects only to the first queue - all VERTEX and FRAGMENT effects are shared!
74
      // (Matrix effect cannot be shared as we have to display each Texture in a different location)
75
      mEffects[0].sink( new Static1D(8), mPoint, new Static4D(0,0,80,80));  // enlarge the nose
76
      mEffects[0].distort(dDistort,mPoint);                                 // keep moving the whole bitmap left and right.
77

    
78
      mScreen = new DistortedScreen();
79
      }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
   private Bitmap readBitmap(int id)
84
     {
85
     InputStream is = mView.getContext().getResources().openRawResource(id);
86
     Bitmap bitmap=null;
87
           
88
     try 
89
       {
90
       bitmap = BitmapFactory.decodeStream(is);
91
       } 
92
     finally 
93
       {
94
       try 
95
         {
96
         is.close();
97
         } 
98
       catch(IOException e) { }
99
       }
100
     
101
     return bitmap;
102
     }
103
   
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
   
106
    public void onDrawFrame(GL10 glUnused) 
107
      {
108
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
109
      mScreen.render( System.currentTimeMillis() );
110
      }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
    
114
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
115
      {  
116
      for(int i=NUM-1; i>=0; i--) 
117
        {   
118
        mEffects[i].abortEffects(EffectTypes.MATRIX);
119
        }
120
      
121
      if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
122
        {
123
        int w = (height*bmpWidth)/bmpHeight;
124
        float factor = (float)height/bmpHeight;
125

    
126
        for(int i=NUM-1; i>=0; i--) 
127
          {
128
          mEffects[i].move( new Static3D((width-NUM*w)/2 +i*w ,0,0) );
129
          mEffects[i].scale(factor);
130
          }
131
        }
132
      else
133
        {
134
        int w = width/NUM;  
135
        int h = (width*bmpHeight)/(bmpWidth*NUM);
136
        float factor = (float)width/(bmpWidth*NUM);
137

    
138
        for(int i=NUM-1; i>=0; i--) 
139
          {
140
          mEffects[i].move( new Static3D(i*w,(height-h)/2,0) );
141
          mEffects[i].scale(factor);
142
          }
143
        }
144
         
145
      mScreen.resize(width, height);
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
    
150
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
151
      {
152
      GLES30.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
153

    
154
      Bitmap bitmap0= readBitmap(R.raw.dog);
155
      Bitmap bitmap1= readBitmap(R.raw.face);
156
      Bitmap bitmap2= readBitmap(R.raw.cat);
157
      
158
      bmpHeight = bitmap0.getHeight();
159
      bmpWidth  = bitmap0.getWidth();
160
      
161
      DistortedTexture[] texture = new DistortedTexture[NUM];
162
      for(int i=0; i<NUM; i++)
163
        texture[i] = new DistortedTexture(bmpWidth,bmpHeight);
164
      
165
      texture[0].setTexture(bitmap0);
166
      texture[1].setTexture(bitmap1);
167
      texture[2].setTexture(bitmap2);
168

    
169
      MeshFlat mesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
170

    
171
      mScreen.detachAll();
172
      for(int i=NUM-1; i>=0; i--) mScreen.attach(texture[i], mEffects[i], mesh);
173

    
174
      try
175
        {
176
        Distorted.onCreate(mView.getContext());
177
        }
178
      catch(Exception ex)
179
        {
180
        android.util.Log.e("Renderer", ex.getMessage() );
181
        }
182
      }
183
}
(2-2/3)