Project

General

Profile

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

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

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.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import android.graphics.Bitmap;
27
import android.graphics.BitmapFactory;
28
import android.opengl.GLSurfaceView;
29

    
30
import org.distorted.examples.R;
31
import org.distorted.library.effect.EffectType;
32
import org.distorted.library.effect.FragmentEffectChroma;
33
import org.distorted.library.effect.FragmentEffectContrast;
34
import org.distorted.library.effect.MatrixEffectScale;
35
import org.distorted.library.effect.VertexEffectDistort;
36
import org.distorted.library.effect.VertexEffectPinch;
37
import org.distorted.library.effect.VertexEffectScale;
38
import org.distorted.library.effect.VertexEffectSink;
39
import org.distorted.library.effect.VertexEffectSwirl;
40
import org.distorted.library.main.DistortedLibrary;
41
import org.distorted.library.main.DistortedEffects;
42
import org.distorted.library.main.DistortedScreen;
43
import org.distorted.library.mesh.MeshSquare;
44
import org.distorted.library.main.DistortedTexture;
45
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
class AroundTheWorldRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
56
{
57
   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
   private DistortedTexture mTexture;
65
   private MeshSquare mMesh;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
   AroundTheWorldRenderer(GLSurfaceView view)
70
      {
71
      mView = view;
72
      mResources = view.getResources();
73

    
74
      mScaleMatrix= new Static3D(1,1,1);
75
      mScaleVertex= new Static3D(1,1,1);
76

    
77
      mManager = new AroundTheWorldEffectsManager();
78
      mEffects = new DistortedEffects();
79
      mEffects.apply(new MatrixEffectScale(mScaleMatrix));
80
      mEffects.apply(new VertexEffectScale(mScaleVertex));
81

    
82
      mManager.apply(mEffects);
83
      mScreen = new DistortedScreen();
84
      mScreen.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
85
      }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
   AroundTheWorldEffectsManager getManager()
90
     {
91
     return mManager;
92
     }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
   public void onDrawFrame(GL10 glUnused) 
97
      {
98
      mScreen.render( System.currentTimeMillis() );
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
    
103
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
104
      {
105
      float horiRatio = (float)width / mScaleVertex.get0();
106
      float vertRatio = (float)height/ mScaleVertex.get1();
107
      float factor    = Math.min(horiRatio,vertRatio);
108

    
109
      mScaleMatrix.set( factor,factor,factor );
110
      mScreen.resize(width,height);
111
      }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
    
115
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
116
      {
117
      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
        catch(IOException ignored) { }
131
        }
132

    
133
      int objWidth = bitmap.getWidth();
134
      int objHeight= bitmap.getHeight();
135

    
136
      mScaleVertex.set(objWidth,objHeight,1);
137

    
138
      if( mTexture==null ) mTexture = new DistortedTexture();
139
      mTexture.setTexture(bitmap);
140

    
141
      if( mMesh==null ) mMesh = new MeshSquare(30,30*objHeight/objWidth);
142

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

    
146
      DistortedLibrary.setMax(EffectType.FRAGMENT, 9);
147

    
148
      VertexEffectScale.enable();
149
      VertexEffectDistort.enable();
150
      VertexEffectSink.enable();
151
      VertexEffectPinch.enable();
152
      VertexEffectSwirl.enable();
153
      FragmentEffectChroma.enable();
154
      FragmentEffectContrast.enable();
155

    
156
      DistortedLibrary.onSurfaceCreated(this);
157
      }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
    public void distortedException(Exception ex)
162
      {
163
      android.util.Log.e("AroundTheWorld", ex.getMessage() );
164
      }
165

    
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
}
(3-3/6)