Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 10b7e588

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.GridFlat;
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 GridFlat mGrid;
47
   private AroundTheWorldEffectsManager mEffects;
48
   private int mObjWidth, mObjHeight;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

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

    
57
      Distorted.setMaxVertex(12);
58
      Distorted.setMaxFragment(9);
59
      }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

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

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

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

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

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

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

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

    
122
      mObjWidth = bitmap.getWidth();
123
      mObjHeight= bitmap.getHeight();
124

    
125
      mObject = new DistortedObject(mObjWidth,mObjHeight,1);
126
      mObject.setTexture(bitmap);
127
      mEffects.apply(mObject);
128

    
129
      mGrid = new GridFlat(30,30*mObjHeight/mObjWidth);
130

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