Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 5055b5d4

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
      Distorted.setMaxVertex(12);
57
      Distorted.setMaxFragment(9);
58
      }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
   AroundTheWorldEffectsManager getManager()
63
     {
64
     return mEffects;
65
     }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
   public void onDrawFrame(GL10 glUnused) 
70
      {
71
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
72
      mObject.draw(System.currentTimeMillis());
73
      }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
    
77
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
78
      {
79
      mObject.abortEffects(EffectTypes.MATRIX);
80

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

    
96
      Distorted.onSurfaceChanged(width, height);
97
      }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
    
101
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
102
      {
103
      GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
104

    
105
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.face);
106
      Bitmap bitmap;
107

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

    
121
      mObject = new DistortedBitmap(bitmap.getWidth(),bitmap.getHeight(),30);
122

    
123
      mObjWidth = mObject.getWidth();
124
      mObjHeight= mObject.getHeight();
125

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

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