Project

General

Profile

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

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

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