Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 37b324c4

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.MatrixEffectMove;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.main.Distorted;
31
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedScreen;
33
import org.distorted.library.main.MeshFlat;
34
import org.distorted.library.main.DistortedTexture;
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
   private DistortedEffects mEffects;
49
   private DistortedTexture mTexture;
50
   private DistortedScreen mScreen;
51
   private MeshFlat mMesh;
52
   private AroundTheWorldEffectsManager mManager;
53
   private int mObjWidth, mObjHeight;
54
   private Static3D mMove, mScale;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
   AroundTheWorldRenderer(GLSurfaceView view)
59
      {
60
      DistortedEffects.setMaxVertex(12);
61
      DistortedEffects.setMaxFragment(9);
62

    
63
      mMove = new Static3D(0,0,0);
64
      mScale= new Static3D(1,1,1);
65

    
66
      mView = view;
67
      mManager = new AroundTheWorldEffectsManager();
68
      mEffects = new DistortedEffects();
69
      mEffects.apply(new MatrixEffectMove(mMove));
70
      mEffects.apply(new MatrixEffectScale(mScale));
71

    
72
      mManager.apply(mEffects);
73
      mScreen = new DistortedScreen(mView);
74
      mScreen.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
75
      }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
   AroundTheWorldEffectsManager getManager()
80
     {
81
     return mManager;
82
     }
83

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

    
86
   public void onDrawFrame(GL10 glUnused) 
87
      {
88
      mScreen.render( System.currentTimeMillis() );
89
      }
90

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

    
110
      mScreen.resize(width,height);
111
      }
112

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

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

    
133
      mObjWidth = bitmap.getWidth();
134
      mObjHeight= bitmap.getHeight();
135

    
136
      if( mTexture==null ) mTexture = new DistortedTexture(mObjWidth,mObjHeight);
137
      mTexture.setTexture(bitmap);
138

    
139
      if( mMesh==null ) mMesh = new MeshFlat(30,30*mObjHeight/mObjWidth);
140

    
141
      mScreen.detachAll();
142
      mScreen.attach(mTexture, mEffects, mMesh);
143

    
144
      DistortedEffects.enableEffect(EffectName.DISTORT);
145
      DistortedEffects.enableEffect(EffectName.SINK);
146
      DistortedEffects.enableEffect(EffectName.PINCH);
147
      DistortedEffects.enableEffect(EffectName.SWIRL);
148
      DistortedEffects.enableEffect(EffectName.CHROMA);
149
      DistortedEffects.enableEffect(EffectName.SMOOTH_CHROMA);
150
      DistortedEffects.enableEffect(EffectName.CONTRAST);
151

    
152
      try
153
        {
154
        Distorted.onCreate(mView.getContext());
155
        }
156
      catch(Exception ex)
157
        {
158
        android.util.Log.e("AroundTheWorld", ex.getMessage() );
159
        }
160
      }
161
}
(3-3/6)