Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 6637d0f2

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