Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 392e16fd

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.DistortedFramebuffer;
30
import org.distorted.library.GridFlat;
31
import org.distorted.library.DistortedEffectQueues;
32
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.EffectTypes;
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 DistortedEffectQueues mEffects;
48
   private DistortedTexture mTexture;
49
   private GridFlat mGrid;
50
   private DistortedFramebuffer mScreen;
51
   private AroundTheWorldEffectsManager mManager;
52
   private int mObjWidth, mObjHeight;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

    
61
      mView = view;
62
      mManager = new AroundTheWorldEffectsManager();
63
      mEffects = new DistortedEffectQueues();
64
      mManager.apply(mEffects);
65
      mScreen = new DistortedFramebuffer(0);
66
      }
67

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

    
70
   AroundTheWorldEffectsManager getManager()
71
     {
72
     return mManager;
73
     }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
   public void onDrawFrame(GL10 glUnused) 
78
      {
79
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
80
      mEffects.draw(System.currentTimeMillis(), mTexture, mGrid, mScreen);
81
      }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
    
85
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
86
      {
87
      mEffects.abortEffects(EffectTypes.MATRIX);
88

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

    
104
      mScreen.resize(width,height);
105
      }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
110
      {
111
      GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
112

    
113
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.face);
114
      Bitmap bitmap;
115

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

    
129
      mObjWidth = bitmap.getWidth();
130
      mObjHeight= bitmap.getHeight();
131

    
132
      mTexture = new DistortedTexture(mObjWidth,mObjHeight);
133
      mTexture.setTexture(bitmap);
134

    
135
      mGrid = new GridFlat(30,30*mObjHeight/mObjWidth);
136

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