Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 126393c8

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