Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ e4330c89

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.effect.EffectName;
28
import org.distorted.library.effect.EffectType;
29
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.main.Distorted;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.main.MeshFlat;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.type.Static3D;
37

    
38
import java.io.IOException;
39
import java.io.InputStream;
40

    
41
import javax.microedition.khronos.egl.EGLConfig;
42
import javax.microedition.khronos.opengles.GL10;
43

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

    
46
class AroundTheWorldRenderer implements GLSurfaceView.Renderer
47
{
48
   private GLSurfaceView mView;
49
   private DistortedEffects mEffects;
50
   private DistortedTexture mTexture;
51
   private DistortedScreen mScreen;
52
   private MeshFlat mMesh;
53
   private AroundTheWorldEffectsManager mManager;
54
   private int mObjWidth, mObjHeight;
55
   private Static3D mMove, mScale;
56

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

    
59
   AroundTheWorldRenderer(GLSurfaceView view)
60
      {
61
      DistortedEffects.setMax(EffectType.VERTEX,12);
62
      DistortedEffects.setMax(EffectType.FRAGMENT,9);
63

    
64
      mMove = new Static3D(0,0,0);
65
      mScale= new Static3D(1,1,1);
66

    
67
      mView = view;
68
      mManager = new AroundTheWorldEffectsManager();
69
      mEffects = new DistortedEffects();
70
      mEffects.apply(new MatrixEffectMove(mMove));
71
      mEffects.apply(new MatrixEffectScale(mScale));
72

    
73
      mManager.apply(mEffects);
74
      mScreen = new DistortedScreen();
75
      mScreen.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
76
      }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
   AroundTheWorldEffectsManager getManager()
81
     {
82
     return mManager;
83
     }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
   public void onDrawFrame(GL10 glUnused) 
88
      {
89
      mScreen.render( System.currentTimeMillis() );
90
      }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
    
94
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
95
      {
96
      if( (float)mObjHeight/mObjWidth > (float)height/width )
97
        {
98
        int w = (height*mObjWidth)/mObjHeight;
99
        float factor = (float)height/mObjHeight;
100
        mMove.set((width-w)/2,0,0);
101
        mScale.set(factor,factor,factor);
102
        }
103
      else
104
        {
105
        int h = (width*mObjHeight)/mObjWidth;
106
        float factor = (float)width/mObjWidth;
107
        mMove.set(0,(height-h)/2,0);
108
        mScale.set(factor,factor,factor);
109
        }
110

    
111
      mScreen.resize(width,height);
112
      }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
    
116
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
117
      {
118
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.face);
119
      Bitmap bitmap;
120

    
121
      try
122
        {
123
        bitmap = BitmapFactory.decodeStream(is);
124
        }
125
      finally
126
        {
127
        try
128
          {
129
          is.close();
130
          }
131
        catch(IOException e) { }
132
        }
133

    
134
      mObjWidth = bitmap.getWidth();
135
      mObjHeight= bitmap.getHeight();
136

    
137
      if( mTexture==null ) mTexture = new DistortedTexture(mObjWidth,mObjHeight);
138
      mTexture.setTexture(bitmap);
139

    
140
      if( mMesh==null ) mMesh = new MeshFlat(30,30*mObjHeight/mObjWidth);
141

    
142
      mScreen.detachAll();
143
      mScreen.attach(mTexture, mEffects, mMesh);
144

    
145
      DistortedEffects.enableEffect(EffectName.DISTORT);
146
      DistortedEffects.enableEffect(EffectName.SINK);
147
      DistortedEffects.enableEffect(EffectName.PINCH);
148
      DistortedEffects.enableEffect(EffectName.SWIRL);
149
      DistortedEffects.enableEffect(EffectName.CHROMA);
150
      DistortedEffects.enableEffect(EffectName.SMOOTH_CHROMA);
151
      DistortedEffects.enableEffect(EffectName.CONTRAST);
152

    
153
      try
154
        {
155
        Distorted.onCreate(mView.getContext());
156
        }
157
      catch(Exception ex)
158
        {
159
        android.util.Log.e("AroundTheWorld", ex.getMessage() );
160
        }
161
      }
162
}
(3-3/6)