Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 7451c98a

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.DistortedEffectQueues;
31
import org.distorted.library.DistortedTexture;
32
import org.distorted.library.EffectTypes;
33
import org.distorted.library.type.Static3D;
34

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

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

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
class AroundTheWorldRenderer implements GLSurfaceView.Renderer
44
{
45
   private GLSurfaceView mView;
46
   private DistortedEffectQueues mQueues;
47
   private DistortedTexture mTexture;
48
   private GridFlat mGrid;
49
   private AroundTheWorldEffectsManager mEffects;
50
   private int mObjWidth, mObjHeight;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
   AroundTheWorldRenderer(GLSurfaceView view)
55
      {
56
      Distorted.setMaxVertex(12);
57
      Distorted.setMaxFragment(9);
58

    
59
      mView = view;
60
      mEffects = new AroundTheWorldEffectsManager();
61
      mQueues = new DistortedEffectQueues();
62
      mEffects.apply(mQueues);
63
      }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
   AroundTheWorldEffectsManager getManager()
68
     {
69
     return mEffects;
70
     }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
   public void onDrawFrame(GL10 glUnused) 
75
      {
76
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
77
      mQueues.draw(System.currentTimeMillis(), mTexture, mGrid);
78
      }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
    
82
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
83
      {
84
      mQueues.abortEffects(EffectTypes.MATRIX);
85

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

    
101
      Distorted.onSurfaceChanged(width, height);
102
      }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
    
106
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
107
      {
108
      GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
109

    
110
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.face);
111
      Bitmap bitmap;
112

    
113
      try
114
        {
115
        bitmap = BitmapFactory.decodeStream(is);
116
        }
117
      finally
118
        {
119
        try
120
          {
121
          is.close();
122
          }
123
        catch(IOException e) { }
124
        }
125

    
126
      mObjWidth = bitmap.getWidth();
127
      mObjHeight= bitmap.getHeight();
128

    
129
      mTexture = new DistortedTexture(mObjWidth,mObjHeight);
130
      mTexture.setTexture(bitmap);
131

    
132
      mGrid = new GridFlat(30,30*mObjHeight/mObjWidth);
133

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