Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ f3ca895f

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.DistortedLibrary;
37
import org.distorted.library.main.DistortedEffects;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.mesh.MeshRectangles;
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 MeshRectangles mMesh;
58
   private AroundTheWorldEffectsManager mManager;
59
   private int mObjWidth, mObjHeight;
60
   private Static3D mMove, mScale;
61

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

    
64
   AroundTheWorldRenderer(GLSurfaceView view)
65
      {
66
      mMove = new Static3D(0,0,0);
67
      mScale= new Static3D(1,1,1);
68

    
69
      mView = view;
70
      mManager = new AroundTheWorldEffectsManager();
71
      mEffects = new DistortedEffects();
72
      mEffects.apply(new MatrixEffectScale(mScale));
73
      mEffects.apply(new MatrixEffectMove(mMove));
74

    
75
      mManager.apply(mEffects);
76
      mScreen = new DistortedScreen();
77
      mScreen.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
78
      }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
   AroundTheWorldEffectsManager getManager()
83
     {
84
     return mManager;
85
     }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
   public void onDrawFrame(GL10 glUnused) 
90
      {
91
      mScreen.render( System.currentTimeMillis() );
92
      }
93

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

    
113
      mScreen.resize(width,height);
114
      }
115

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

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

    
136
      mObjWidth = bitmap.getWidth();
137
      mObjHeight= bitmap.getHeight();
138

    
139
      if( mTexture==null ) mTexture = new DistortedTexture(mObjWidth,mObjHeight);
140
      mTexture.setTexture(bitmap);
141

    
142
      if( mMesh==null ) mMesh = new MeshRectangles(30,30*mObjHeight/mObjWidth);
143

    
144
      mScreen.detachAll();
145
      mScreen.attach(mTexture, mEffects, mMesh);
146

    
147
      DistortedLibrary.setMax(EffectType.VERTEX  ,12);
148
      DistortedLibrary.setMax(EffectType.FRAGMENT, 9);
149

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

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