Project

General

Profile

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

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

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

    
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44
import android.opengl.GLES30;
45
import android.opengl.GLSurfaceView;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

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

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

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

    
81
      mScreen = new DistortedScreen();
82
      }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
   private Bitmap readBitmap(int id)
87
     {
88
     InputStream is = mView.getContext().getResources().openRawResource(id);
89
     Bitmap bitmap=null;
90
           
91
     try 
92
       {
93
       bitmap = BitmapFactory.decodeStream(is);
94
       } 
95
     finally 
96
       {
97
       try 
98
         {
99
         is.close();
100
         } 
101
       catch(IOException e) { }
102
       }
103
     
104
     return bitmap;
105
     }
106
   
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
   
109
    public void onDrawFrame(GL10 glUnused) 
110
      {
111
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
112
      
113
      long time = System.currentTimeMillis();
114
      
115
      for(int i=NUM-1; i>=0; i--) mScreen.renderTo(mTexture[i], mMesh, mEffects[i], time);
116
      }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
    
120
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
121
      {  
122
      for(int i=NUM-1; i>=0; i--) 
123
        {   
124
        mEffects[i].abortEffects(EffectTypes.MATRIX);
125
        }
126
      
127
      if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
128
        {
129
        int w = (height*bmpWidth)/bmpHeight;
130
        float factor = (float)height/bmpHeight;
131

    
132
        for(int i=NUM-1; i>=0; i--) 
133
          {
134
          mEffects[i].move( new Static3D((width-NUM*w)/2 +i*w ,0,0) );
135
          mEffects[i].scale(factor);
136
          }
137
        }
138
      else
139
        {
140
        int w = width/NUM;  
141
        int h = (width*bmpHeight)/(bmpWidth*NUM);
142
        float factor = (float)width/(bmpWidth*NUM);
143

    
144
        for(int i=NUM-1; i>=0; i--) 
145
          {
146
          mEffects[i].move( new Static3D(i*w,(height-h)/2,0) );
147
          mEffects[i].scale(factor);
148
          }
149
        }
150
         
151
      mScreen.resize(width, height);
152
      }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
    
156
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
157
      {
158
      GLES30.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
159

    
160
      Bitmap bitmap0= readBitmap(R.raw.dog);
161
      Bitmap bitmap1= readBitmap(R.raw.face);
162
      Bitmap bitmap2= readBitmap(R.raw.cat);
163
      
164
      bmpHeight = bitmap0.getHeight();
165
      bmpWidth  = bitmap0.getWidth();
166
      
167
      mTexture = new DistortedTexture[NUM];
168
      for(int i=0; i<NUM; i++)
169
        mTexture[i] = new DistortedTexture(bmpWidth,bmpHeight);
170
      
171
      mTexture[0].setTexture(bitmap0);
172
      mTexture[1].setTexture(bitmap1);
173
      mTexture[2].setTexture(bitmap2);
174

    
175
      mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
176

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