Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 625c67de

1 7ba38011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 7ba38011 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 7ba38011 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 7ba38011 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 7ba38011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.aroundtheworld;
21
22 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26 7ba38011 Leszek Koltunski
import android.graphics.Bitmap;
27
import android.graphics.BitmapFactory;
28
import android.opengl.GLSurfaceView;
29
30
import org.distorted.examples.R;
31 d76638f1 leszek
import org.distorted.library.effect.EffectType;
32 885b9cca leszek
import org.distorted.library.effect.FragmentEffectChroma;
33
import org.distorted.library.effect.FragmentEffectContrast;
34 fa9b6494 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
35 885b9cca leszek
import org.distorted.library.effect.VertexEffectDistort;
36
import org.distorted.library.effect.VertexEffectPinch;
37 ba95a70b Leszek Koltunski
import org.distorted.library.effect.VertexEffectScale;
38 885b9cca leszek
import org.distorted.library.effect.VertexEffectSink;
39
import org.distorted.library.effect.VertexEffectSwirl;
40 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
41 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
42
import org.distorted.library.main.DistortedScreen;
43 f2085b96 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
44 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
45 7ba38011 Leszek Koltunski
import org.distorted.library.type.Static3D;
46
47
import java.io.IOException;
48
import java.io.InputStream;
49
50
import javax.microedition.khronos.egl.EGLConfig;
51
import javax.microedition.khronos.opengles.GL10;
52
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55 dc10a48d Leszek Koltunski
class AroundTheWorldRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
56 7ba38011 Leszek Koltunski
{
57 dc10a48d Leszek Koltunski
   private final GLSurfaceView mView;
58
   private final Resources mResources;
59
   private final DistortedEffects mEffects;
60
   private final DistortedScreen mScreen;
61
   private final AroundTheWorldEffectsManager mManager;
62
   private final Static3D mScaleMatrix, mScaleVertex;
63
64 59540ba2 Leszek Koltunski
   private DistortedTexture mTexture;
65 f2085b96 Leszek Koltunski
   private MeshSquare mMesh;
66 fa9b6494 Leszek Koltunski
67 7ba38011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69
   AroundTheWorldRenderer(GLSurfaceView view)
70
      {
71 dc10a48d Leszek Koltunski
      mView = view;
72
      mResources = view.getResources();
73
74 ba95a70b Leszek Koltunski
      mScaleMatrix= new Static3D(1,1,1);
75
      mScaleVertex= new Static3D(1,1,1);
76 fa9b6494 Leszek Koltunski
77 392e16fd Leszek Koltunski
      mManager = new AroundTheWorldEffectsManager();
78 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
79 ba95a70b Leszek Koltunski
      mEffects.apply(new MatrixEffectScale(mScaleMatrix));
80
      mEffects.apply(new VertexEffectScale(mScaleVertex));
81 fa9b6494 Leszek Koltunski
82 392e16fd Leszek Koltunski
      mManager.apply(mEffects);
83 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
84 855ba24e leszek
      mScreen.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
85 7ba38011 Leszek Koltunski
      }
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88 2f20ae3a Leszek Koltunski
89 4b34bb08 Leszek Koltunski
   AroundTheWorldEffectsManager getManager()
90 2f20ae3a Leszek Koltunski
     {
91 392e16fd Leszek Koltunski
     return mManager;
92 2f20ae3a Leszek Koltunski
     }
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
96 7ba38011 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused) 
97
      {
98 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
99 7ba38011 Leszek Koltunski
      }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
    
103
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
104
      {
105 ba95a70b Leszek Koltunski
      float horiRatio = (float)width / mScaleVertex.get0();
106
      float vertRatio = (float)height/ mScaleVertex.get1();
107
      float factor    = Math.min(horiRatio,vertRatio);
108 7ba38011 Leszek Koltunski
109 ba95a70b Leszek Koltunski
      mScaleMatrix.set( factor,factor,factor );
110 392e16fd Leszek Koltunski
      mScreen.resize(width,height);
111 7ba38011 Leszek Koltunski
      }
112
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
    
115
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
116 e7a4ef16 Leszek Koltunski
      {
117 7ba38011 Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.face);
118
      Bitmap bitmap;
119
120
      try
121
        {
122
        bitmap = BitmapFactory.decodeStream(is);
123
        }
124
      finally
125
        {
126
        try
127
          {
128
          is.close();
129
          }
130 dc10a48d Leszek Koltunski
        catch(IOException ignored) { }
131 7ba38011 Leszek Koltunski
        }
132
133 16b22aab Leszek Koltunski
      int objWidth = bitmap.getWidth();
134
      int objHeight= bitmap.getHeight();
135 7ba38011 Leszek Koltunski
136 ba95a70b Leszek Koltunski
      mScaleVertex.set(objWidth,objHeight,1);
137
138 687263cc Leszek Koltunski
      if( mTexture==null ) mTexture = new DistortedTexture();
139 59540ba2 Leszek Koltunski
      mTexture.setTexture(bitmap);
140 fe59d375 Leszek Koltunski
141 f2085b96 Leszek Koltunski
      if( mMesh==null ) mMesh = new MeshSquare(30,30*objHeight/objWidth);
142 5f2a53b6 leszek
143 fe59d375 Leszek Koltunski
      mScreen.detachAll();
144 5f2a53b6 leszek
      mScreen.attach(mTexture, mEffects, mMesh);
145 e8b6aa95 Leszek Koltunski
146 736a0b00 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.FRAGMENT, 9);
147
148 ba95a70b Leszek Koltunski
      VertexEffectScale.enable();
149 885b9cca leszek
      VertexEffectDistort.enable();
150
      VertexEffectSink.enable();
151
      VertexEffectPinch.enable();
152
      VertexEffectSwirl.enable();
153
      FragmentEffectChroma.enable();
154
      FragmentEffectContrast.enable();
155 6637d0f2 Leszek Koltunski
156 dc10a48d Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(this);
157 061449ed Leszek Koltunski
      }
158
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161
    public void distortedException(Exception ex)
162
      {
163
      android.util.Log.e("AroundTheWorld", ex.getMessage() );
164 7ba38011 Leszek Koltunski
      }
165 dc10a48d Leszek Koltunski
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
168
    public InputStream localFile(int fileID)
169
      {
170
      return mResources.openRawResource(fileID);
171
      }
172
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
175
    public void logMessage(String message)
176
      {
177
      android.util.Log.e("AroundTheWorld", message );
178
      }
179 7ba38011 Leszek Koltunski
}