Project

General

Profile

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

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

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 885b9cca leszek
import org.distorted.library.effect.FragmentEffectChroma;
30
import org.distorted.library.effect.FragmentEffectContrast;
31 fa9b6494 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectScale;
33 885b9cca leszek
import org.distorted.library.effect.VertexEffectDistort;
34
import org.distorted.library.effect.VertexEffectPinch;
35
import org.distorted.library.effect.VertexEffectSink;
36
import org.distorted.library.effect.VertexEffectSwirl;
37 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.main.DistortedScreen;
40
import org.distorted.library.main.MeshFlat;
41
import org.distorted.library.main.DistortedTexture;
42 7ba38011 Leszek Koltunski
import org.distorted.library.type.Static3D;
43
44
import java.io.IOException;
45
import java.io.InputStream;
46
47
import javax.microedition.khronos.egl.EGLConfig;
48
import javax.microedition.khronos.opengles.GL10;
49
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51
52
class AroundTheWorldRenderer implements GLSurfaceView.Renderer
53
{
54
   private GLSurfaceView mView;
55 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
56 59540ba2 Leszek Koltunski
   private DistortedTexture mTexture;
57 d218d64e leszek
   private DistortedScreen mScreen;
58 5f2a53b6 leszek
   private MeshFlat mMesh;
59 392e16fd Leszek Koltunski
   private AroundTheWorldEffectsManager mManager;
60 67c3a83b leszek
   private int mObjWidth, mObjHeight;
61 fa9b6494 Leszek Koltunski
   private Static3D mMove, mScale;
62
63 7ba38011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
   AroundTheWorldRenderer(GLSurfaceView view)
66
      {
67 d76638f1 leszek
      DistortedEffects.setMax(EffectType.VERTEX,12);
68
      DistortedEffects.setMax(EffectType.FRAGMENT,9);
69 f6d884d5 Leszek Koltunski
70 fa9b6494 Leszek Koltunski
      mMove = new Static3D(0,0,0);
71
      mScale= new Static3D(1,1,1);
72
73 f6d884d5 Leszek Koltunski
      mView = view;
74 392e16fd Leszek Koltunski
      mManager = new AroundTheWorldEffectsManager();
75 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
76 fa9b6494 Leszek Koltunski
      mEffects.apply(new MatrixEffectMove(mMove));
77
      mEffects.apply(new MatrixEffectScale(mScale));
78
79 392e16fd Leszek Koltunski
      mManager.apply(mEffects);
80 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
81 855ba24e leszek
      mScreen.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
82 7ba38011 Leszek Koltunski
      }
83
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85 2f20ae3a Leszek Koltunski
86 4b34bb08 Leszek Koltunski
   AroundTheWorldEffectsManager getManager()
87 2f20ae3a Leszek Koltunski
     {
88 392e16fd Leszek Koltunski
     return mManager;
89 2f20ae3a Leszek Koltunski
     }
90
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
93 7ba38011 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused) 
94
      {
95 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
96 7ba38011 Leszek Koltunski
      }
97
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
    
100
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
101
      {
102 67c3a83b leszek
      if( (float)mObjHeight/mObjWidth > (float)height/width )
103
        {
104
        int w = (height*mObjWidth)/mObjHeight;
105
        float factor = (float)height/mObjHeight;
106 fa9b6494 Leszek Koltunski
        mMove.set((width-w)/2,0,0);
107
        mScale.set(factor,factor,factor);
108 67c3a83b leszek
        }
109
      else
110
        {
111
        int h = (width*mObjHeight)/mObjWidth;
112
        float factor = (float)width/mObjWidth;
113 fa9b6494 Leszek Koltunski
        mMove.set(0,(height-h)/2,0);
114
        mScale.set(factor,factor,factor);
115 67c3a83b leszek
        }
116 7ba38011 Leszek Koltunski
117 392e16fd Leszek Koltunski
      mScreen.resize(width,height);
118 7ba38011 Leszek Koltunski
      }
119
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
    
122
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
123 e7a4ef16 Leszek Koltunski
      {
124 7ba38011 Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.face);
125
      Bitmap bitmap;
126
127
      try
128
        {
129
        bitmap = BitmapFactory.decodeStream(is);
130
        }
131
      finally
132
        {
133
        try
134
          {
135
          is.close();
136
          }
137
        catch(IOException e) { }
138
        }
139
140 67c3a83b leszek
      mObjWidth = bitmap.getWidth();
141
      mObjHeight= bitmap.getHeight();
142 7ba38011 Leszek Koltunski
143 67c3a83b leszek
      if( mTexture==null ) mTexture = new DistortedTexture(mObjWidth,mObjHeight);
144 59540ba2 Leszek Koltunski
      mTexture.setTexture(bitmap);
145 fe59d375 Leszek Koltunski
146 5f2a53b6 leszek
      if( mMesh==null ) mMesh = new MeshFlat(30,30*mObjHeight/mObjWidth);
147
148 fe59d375 Leszek Koltunski
      mScreen.detachAll();
149 5f2a53b6 leszek
      mScreen.attach(mTexture, mEffects, mMesh);
150 e8b6aa95 Leszek Koltunski
151 885b9cca leszek
      VertexEffectDistort.enable();
152
      VertexEffectSink.enable();
153
      VertexEffectPinch.enable();
154
      VertexEffectSwirl.enable();
155
      FragmentEffectChroma.enable();
156
      FragmentEffectContrast.enable();
157 6637d0f2 Leszek Koltunski
158 7ba38011 Leszek Koltunski
      try
159
        {
160 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
161 7ba38011 Leszek Koltunski
        }
162
      catch(Exception ex)
163
        {
164
        android.util.Log.e("AroundTheWorld", ex.getMessage() );
165
        }
166
      }
167
}