Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 2f20ae3a

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.GLES20;
25
import android.opengl.GLSurfaceView;
26

    
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29
import org.distorted.library.DistortedBitmap;
30
import org.distorted.library.DistortedObject;
31
import org.distorted.library.EffectTypes;
32
import org.distorted.library.type.Static3D;
33

    
34
import java.io.IOException;
35
import java.io.InputStream;
36

    
37
import javax.microedition.khronos.egl.EGLConfig;
38
import javax.microedition.khronos.opengles.GL10;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
class AroundTheWorldRenderer implements GLSurfaceView.Renderer
43
{
44
   private GLSurfaceView mView;
45
   private DistortedObject mObject;
46
   private AroundTheWorldEffectsManager mEffects;
47
   private int mObjWidth, mObjHeight;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
   AroundTheWorldRenderer(GLSurfaceView view)
52
      {
53
      mView = view;
54
      mEffects = new AroundTheWorldEffectsManager();
55
      }
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
   public AroundTheWorldEffectsManager getManager()
60
     {
61
     return mEffects;
62
     }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
   public void onDrawFrame(GL10 glUnused) 
67
      {
68
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
69
      mObject.draw(System.currentTimeMillis());
70
      }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
    
74
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
75
      {
76
      mObject.abortEffects(EffectTypes.MATRIX);
77

    
78
      if( mObjHeight/mObjWidth > height/width )
79
        {
80
        int w = (height*mObjWidth)/mObjHeight;
81
        float factor = (float)height/mObjHeight;
82
        mObject.move( new Static3D((width-w)/2,0,0) );
83
        mObject.scale(factor);
84
        }
85
      else
86
        {
87
        int h = (width*mObjHeight)/mObjWidth;
88
        float factor = (float)width/mObjWidth;
89
        mObject.move( new Static3D(0,(height-h)/2,0) );
90
        mObject.scale(factor);
91
        }
92

    
93
      Distorted.onSurfaceChanged(width, height);
94
      }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
    
98
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
99
      {
100
      GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
101

    
102
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.face);
103
      Bitmap bitmap;
104

    
105
      try
106
        {
107
        bitmap = BitmapFactory.decodeStream(is);
108
        }
109
      finally
110
        {
111
        try
112
          {
113
          is.close();
114
          }
115
        catch(IOException e) { }
116
        }
117

    
118
      mObject = new DistortedBitmap(bitmap.getWidth(),bitmap.getHeight(),30);
119

    
120
      mObjWidth = mObject.getWidth();
121
      mObjHeight= mObject.getHeight();
122

    
123
      mEffects.setSize(mObjWidth,mObjHeight);
124

    
125
      mObject.setBitmap(bitmap);
126
      mEffects.apply(mObject);
127

    
128
      try
129
        {
130
        Distorted.onSurfaceCreated(mView.getContext());
131
        }
132
      catch(Exception ex)
133
        {
134
        android.util.Log.e("AroundTheWorld", ex.getMessage() );
135
        }
136
      }
137
}
(3-3/6)