Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 107e4b72

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.EffectType;
28
import org.distorted.library.effect.FragmentEffectChroma;
29
import org.distorted.library.effect.FragmentEffectContrast;
30
import org.distorted.library.effect.MatrixEffectMove;
31
import org.distorted.library.effect.MatrixEffectScale;
32
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
import org.distorted.library.main.Distorted;
37
import org.distorted.library.main.DistortedEffects;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.mesh.MeshFlat;
40
import org.distorted.library.main.DistortedTexture;
41
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
   private DistortedEffects mEffects;
55
   private DistortedTexture mTexture;
56
   private DistortedScreen mScreen;
57
   private MeshFlat mMesh;
58
   private AroundTheWorldEffectsManager mManager;
59
   private int mObjWidth, mObjHeight;
60
   private Static3D mMove, mScale;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

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

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

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

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

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

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

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

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

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

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

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

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

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

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

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

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

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