Project

General

Profile

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

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

1 7ba38011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 fa9b6494 Leszek Koltunski
import org.distorted.library.effect.EffectName;
28 d76638f1 leszek
import org.distorted.library.effect.EffectType;
29 fa9b6494 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.effect.MatrixEffectScale;
31 01782e85 Leszek Koltunski
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 7ba38011 Leszek Koltunski
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 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
50 59540ba2 Leszek Koltunski
   private DistortedTexture mTexture;
51 d218d64e leszek
   private DistortedScreen mScreen;
52 5f2a53b6 leszek
   private MeshFlat mMesh;
53 392e16fd Leszek Koltunski
   private AroundTheWorldEffectsManager mManager;
54 67c3a83b leszek
   private int mObjWidth, mObjHeight;
55 fa9b6494 Leszek Koltunski
   private Static3D mMove, mScale;
56
57 7ba38011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
58
59
   AroundTheWorldRenderer(GLSurfaceView view)
60
      {
61 d76638f1 leszek
      DistortedEffects.setMax(EffectType.VERTEX,12);
62
      DistortedEffects.setMax(EffectType.FRAGMENT,9);
63 f6d884d5 Leszek Koltunski
64 fa9b6494 Leszek Koltunski
      mMove = new Static3D(0,0,0);
65
      mScale= new Static3D(1,1,1);
66
67 f6d884d5 Leszek Koltunski
      mView = view;
68 392e16fd Leszek Koltunski
      mManager = new AroundTheWorldEffectsManager();
69 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
70 fa9b6494 Leszek Koltunski
      mEffects.apply(new MatrixEffectMove(mMove));
71
      mEffects.apply(new MatrixEffectScale(mScale));
72
73 392e16fd Leszek Koltunski
      mManager.apply(mEffects);
74 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
75 855ba24e leszek
      mScreen.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
76 7ba38011 Leszek Koltunski
      }
77
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79 2f20ae3a Leszek Koltunski
80 4b34bb08 Leszek Koltunski
   AroundTheWorldEffectsManager getManager()
81 2f20ae3a Leszek Koltunski
     {
82 392e16fd Leszek Koltunski
     return mManager;
83 2f20ae3a Leszek Koltunski
     }
84
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87 7ba38011 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused) 
88
      {
89 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
90 7ba38011 Leszek Koltunski
      }
91
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
    
94
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
95
      {
96 67c3a83b leszek
      if( (float)mObjHeight/mObjWidth > (float)height/width )
97
        {
98
        int w = (height*mObjWidth)/mObjHeight;
99
        float factor = (float)height/mObjHeight;
100 fa9b6494 Leszek Koltunski
        mMove.set((width-w)/2,0,0);
101
        mScale.set(factor,factor,factor);
102 67c3a83b leszek
        }
103
      else
104
        {
105
        int h = (width*mObjHeight)/mObjWidth;
106
        float factor = (float)width/mObjWidth;
107 fa9b6494 Leszek Koltunski
        mMove.set(0,(height-h)/2,0);
108
        mScale.set(factor,factor,factor);
109 67c3a83b leszek
        }
110 7ba38011 Leszek Koltunski
111 392e16fd Leszek Koltunski
      mScreen.resize(width,height);
112 7ba38011 Leszek Koltunski
      }
113
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
    
116
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
117 e7a4ef16 Leszek Koltunski
      {
118 7ba38011 Leszek Koltunski
      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 67c3a83b leszek
      mObjWidth = bitmap.getWidth();
135
      mObjHeight= bitmap.getHeight();
136 7ba38011 Leszek Koltunski
137 67c3a83b leszek
      if( mTexture==null ) mTexture = new DistortedTexture(mObjWidth,mObjHeight);
138 59540ba2 Leszek Koltunski
      mTexture.setTexture(bitmap);
139 fe59d375 Leszek Koltunski
140 5f2a53b6 leszek
      if( mMesh==null ) mMesh = new MeshFlat(30,30*mObjHeight/mObjWidth);
141
142 fe59d375 Leszek Koltunski
      mScreen.detachAll();
143 5f2a53b6 leszek
      mScreen.attach(mTexture, mEffects, mMesh);
144 e8b6aa95 Leszek Koltunski
145 fa9b6494 Leszek Koltunski
      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 6637d0f2 Leszek Koltunski
153 7ba38011 Leszek Koltunski
      try
154
        {
155 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
156 7ba38011 Leszek Koltunski
        }
157
      catch(Exception ex)
158
        {
159
        android.util.Log.e("AroundTheWorld", ex.getMessage() );
160
        }
161
      }
162
}