Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 01782e85

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