Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 7ba38011

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.DistortedCubes;
31
import org.distorted.library.DistortedObject;
32
import org.distorted.library.EffectTypes;
33
import org.distorted.library.type.Static1D;
34
import org.distorted.library.type.Static3D;
35

    
36
import java.io.IOException;
37
import java.io.InputStream;
38

    
39
import javax.microedition.khronos.egl.EGLConfig;
40
import javax.microedition.khronos.opengles.GL10;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

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

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
   AroundTheWorldRenderer(GLSurfaceView view)
54
      {
55
      mView = view;
56
      }
57

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

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

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

    
89
      Distorted.onSurfaceChanged(width, height);
90
      }
91

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

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

    
112

    
113
      mObject = new DistortedBitmap(bitmap.getWidth(),bitmap.getHeight(),30);
114
      mEffects = new AroundTheWorldEffectsManager(mObject);
115

    
116
      mObjWidth = mObject.getWidth();
117
      mObjHeight= mObject.getHeight();
118

    
119
      mObject.setBitmap(bitmap);
120

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