Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 51554e47

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.aroundtheworld;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25

    
26
import org.distorted.examples.R;
27
import org.distorted.library.Distorted;
28
import org.distorted.library.DistortedEffects;
29
import org.distorted.library.DistortedScreen;
30
import org.distorted.library.EffectNames;
31
import org.distorted.library.MeshFlat;
32
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.type.Static3D;
35

    
36
import java.io.IOException;
37
import java.io.InputStream;
38

    
39
import javax.microedition.khronos.egl.EGLConfig;
40
import javax.microedition.khronos.opengles.GL10;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
class AroundTheWorldRenderer implements GLSurfaceView.Renderer
45
{
46
   private GLSurfaceView mView;
47
   private DistortedEffects mEffects;
48
   private DistortedTexture mTexture;
49
   private DistortedScreen mScreen;
50
   private AroundTheWorldEffectsManager mManager;
51
   private int bmpWidth, bmpHeight;
52

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

    
55
   AroundTheWorldRenderer(GLSurfaceView view)
56
      {
57
      DistortedEffects.setMaxVertex(12);
58
      DistortedEffects.setMaxFragment(9);
59

    
60
      mView = view;
61
      mManager = new AroundTheWorldEffectsManager();
62
      mEffects = new DistortedEffects();
63
      mManager.apply(mEffects);
64
      mScreen = new DistortedScreen();
65
      mScreen.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
66
      }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
   AroundTheWorldEffectsManager getManager()
71
     {
72
     return mManager;
73
     }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
   public void onDrawFrame(GL10 glUnused) 
78
      {
79
      mScreen.render( System.currentTimeMillis() );
80
      }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
    
84
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
85
      {
86
      float qx = (float)width /bmpWidth;
87
      float qy = (float)height/bmpHeight;
88

    
89
      mEffects.abortEffects(EffectTypes.MATRIX);
90
      mEffects.scale(  qx<qy ? (new Static3D(1,qx/qy,1)) : (new Static3D(qy/qx,1,1)) );
91

    
92
      mScreen.resize(width,height);
93
      }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
    
97
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
98
      {
99
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.face);
100
      Bitmap bitmap;
101

    
102
      try
103
        {
104
        bitmap = BitmapFactory.decodeStream(is);
105
        }
106
      finally
107
        {
108
        try
109
          {
110
          is.close();
111
          }
112
        catch(IOException e) { }
113
        }
114

    
115
      bmpWidth = bitmap.getWidth();
116
      bmpHeight= bitmap.getHeight();
117

    
118
      if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
119
      mTexture.setTexture(bitmap);
120

    
121
      mScreen.detachAll();
122
      mScreen.attach(mTexture, mEffects, new MeshFlat(30,30*bmpHeight/bmpWidth));
123

    
124
      DistortedEffects.enableEffect(EffectNames.DISTORT);
125
      DistortedEffects.enableEffect(EffectNames.SINK);
126
      DistortedEffects.enableEffect(EffectNames.PINCH);
127
      DistortedEffects.enableEffect(EffectNames.SWIRL);
128
      DistortedEffects.enableEffect(EffectNames.CHROMA);
129
      DistortedEffects.enableEffect(EffectNames.SMOOTH_CHROMA);
130
      DistortedEffects.enableEffect(EffectNames.CONTRAST);
131

    
132
      try
133
        {
134
        Distorted.onCreate(mView.getContext());
135
        }
136
      catch(Exception ex)
137
        {
138
        android.util.Log.e("AroundTheWorld", ex.getMessage() );
139
        }
140
      }
141
}
(3-3/6)