Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ bc29e409

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
import android.provider.Settings;
27

    
28
import org.distorted.examples.R;
29
import org.distorted.library.Distorted;
30
import org.distorted.library.DistortedEffects;
31
import org.distorted.library.DistortedFramebuffer;
32
import org.distorted.library.GridFlat;
33
import org.distorted.library.DistortedTexture;
34
import org.distorted.library.EffectTypes;
35
import org.distorted.library.type.Static3D;
36

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

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

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
class AroundTheWorldRenderer implements GLSurfaceView.Renderer
46
{
47
   private GLSurfaceView mView;
48
   private DistortedEffects mEffects;
49
   private DistortedTexture mTexture;
50
   private GridFlat mGrid;
51
   private DistortedFramebuffer mScreen;
52
   private AroundTheWorldEffectsManager mManager;
53
   private int mObjWidth, mObjHeight;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

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

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

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

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

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

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

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

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

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

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

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

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

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

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

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