Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 885b9cca

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.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25

    
26
import org.distorted.examples.R;
27
import org.distorted.library.effect.EffectName;
28
import org.distorted.library.effect.EffectType;
29
import org.distorted.library.effect.FragmentEffectChroma;
30
import org.distorted.library.effect.FragmentEffectContrast;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectDistort;
34
import org.distorted.library.effect.VertexEffectPinch;
35
import org.distorted.library.effect.VertexEffectSink;
36
import org.distorted.library.effect.VertexEffectSwirl;
37
import org.distorted.library.main.Distorted;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.main.DistortedScreen;
40
import org.distorted.library.main.MeshFlat;
41
import org.distorted.library.main.DistortedTexture;
42
import org.distorted.library.type.Static3D;
43

    
44
import java.io.IOException;
45
import java.io.InputStream;
46

    
47
import javax.microedition.khronos.egl.EGLConfig;
48
import javax.microedition.khronos.opengles.GL10;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
class AroundTheWorldRenderer implements GLSurfaceView.Renderer
53
{
54
   private GLSurfaceView mView;
55
   private DistortedEffects mEffects;
56
   private DistortedTexture mTexture;
57
   private DistortedScreen mScreen;
58
   private MeshFlat mMesh;
59
   private AroundTheWorldEffectsManager mManager;
60
   private int mObjWidth, mObjHeight;
61
   private Static3D mMove, mScale;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
   AroundTheWorldRenderer(GLSurfaceView view)
66
      {
67
      DistortedEffects.setMax(EffectType.VERTEX,12);
68
      DistortedEffects.setMax(EffectType.FRAGMENT,9);
69

    
70
      mMove = new Static3D(0,0,0);
71
      mScale= new Static3D(1,1,1);
72

    
73
      mView = view;
74
      mManager = new AroundTheWorldEffectsManager();
75
      mEffects = new DistortedEffects();
76
      mEffects.apply(new MatrixEffectMove(mMove));
77
      mEffects.apply(new MatrixEffectScale(mScale));
78

    
79
      mManager.apply(mEffects);
80
      mScreen = new DistortedScreen();
81
      mScreen.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
82
      }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
   AroundTheWorldEffectsManager getManager()
87
     {
88
     return mManager;
89
     }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
   public void onDrawFrame(GL10 glUnused) 
94
      {
95
      mScreen.render( System.currentTimeMillis() );
96
      }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
    
100
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
101
      {
102
      if( (float)mObjHeight/mObjWidth > (float)height/width )
103
        {
104
        int w = (height*mObjWidth)/mObjHeight;
105
        float factor = (float)height/mObjHeight;
106
        mMove.set((width-w)/2,0,0);
107
        mScale.set(factor,factor,factor);
108
        }
109
      else
110
        {
111
        int h = (width*mObjHeight)/mObjWidth;
112
        float factor = (float)width/mObjWidth;
113
        mMove.set(0,(height-h)/2,0);
114
        mScale.set(factor,factor,factor);
115
        }
116

    
117
      mScreen.resize(width,height);
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
    
122
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
123
      {
124
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.face);
125
      Bitmap bitmap;
126

    
127
      try
128
        {
129
        bitmap = BitmapFactory.decodeStream(is);
130
        }
131
      finally
132
        {
133
        try
134
          {
135
          is.close();
136
          }
137
        catch(IOException e) { }
138
        }
139

    
140
      mObjWidth = bitmap.getWidth();
141
      mObjHeight= bitmap.getHeight();
142

    
143
      if( mTexture==null ) mTexture = new DistortedTexture(mObjWidth,mObjHeight);
144
      mTexture.setTexture(bitmap);
145

    
146
      if( mMesh==null ) mMesh = new MeshFlat(30,30*mObjHeight/mObjWidth);
147

    
148
      mScreen.detachAll();
149
      mScreen.attach(mTexture, mEffects, mMesh);
150

    
151
      VertexEffectDistort.enable();
152
      VertexEffectSink.enable();
153
      VertexEffectPinch.enable();
154
      VertexEffectSwirl.enable();
155
      FragmentEffectChroma.enable();
156
      FragmentEffectContrast.enable();
157

    
158
      try
159
        {
160
        Distorted.onCreate(mView.getContext());
161
        }
162
      catch(Exception ex)
163
        {
164
        android.util.Log.e("AroundTheWorld", ex.getMessage() );
165
        }
166
      }
167
}
(3-3/6)