Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 80508ef5

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
      }
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
   
58
   public void onDrawFrame(GL10 glUnused) 
59
      {
60
      GLES20.glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
61
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
62
     
63
      mObject.draw(System.currentTimeMillis());
64
      }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
    
68
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
69
      {
70
      mObject.abortEffects(EffectTypes.MATRIX);
71

    
72
      if( mObjHeight/mObjWidth > height/width )
73
        {
74
        int w = (height*mObjWidth)/mObjHeight;
75
        float factor = (float)height/mObjHeight;
76
        mObject.move( new Static3D((width-w)/2,0,0) );
77
        mObject.scale(factor);
78
        }
79
      else
80
        {
81
        int h = (width*mObjHeight)/mObjWidth;
82
        float factor = (float)width/mObjWidth;
83
        mObject.move( new Static3D(0,(height-h)/2,0) );
84
        mObject.scale(factor);
85
        }
86

    
87
      Distorted.onSurfaceChanged(width, height);
88
      }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
    
92
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
93
      {  
94
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.face);
95
      Bitmap bitmap;
96

    
97
      try
98
        {
99
        bitmap = BitmapFactory.decodeStream(is);
100
        }
101
      finally
102
        {
103
        try
104
          {
105
          is.close();
106
          }
107
        catch(IOException e) { }
108
        }
109

    
110
      mObject = new DistortedBitmap(bitmap.getWidth(),bitmap.getHeight(),30);
111
      mEffects = new AroundTheWorldEffectsManager(mObject);
112

    
113
      mObjWidth = mObject.getWidth();
114
      mObjHeight= mObject.getHeight();
115

    
116
      mObject.setBitmap(bitmap);
117

    
118
      try
119
        {
120
        Distorted.onSurfaceCreated(mView.getContext());
121
        }
122
      catch(Exception ex)
123
        {
124
        android.util.Log.e("AroundTheWorld", ex.getMessage() );
125
        }
126
      }
127
}
(3-3/6)