Project

General

Profile

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

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

1 7ba38011 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 bc29e409 Leszek Koltunski
import android.provider.Settings;
27 7ba38011 Leszek Koltunski
28
import org.distorted.examples.R;
29
import org.distorted.library.Distorted;
30 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
31 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
32 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
33 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
34 7ba38011 Leszek Koltunski
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 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
49 f6d884d5 Leszek Koltunski
   private DistortedTexture mTexture;
50 10b7e588 Leszek Koltunski
   private GridFlat mGrid;
51 392e16fd Leszek Koltunski
   private DistortedFramebuffer mScreen;
52
   private AroundTheWorldEffectsManager mManager;
53 7ba38011 Leszek Koltunski
   private int mObjWidth, mObjHeight;
54
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
57
   AroundTheWorldRenderer(GLSurfaceView view)
58
      {
59 b3a555a7 Leszek Koltunski
      Distorted.setMaxVertex(12);
60 287a5475 Leszek Koltunski
      Distorted.setMaxFragment(9);
61 f6d884d5 Leszek Koltunski
62
      mView = view;
63 392e16fd Leszek Koltunski
      mManager = new AroundTheWorldEffectsManager();
64 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
65 392e16fd Leszek Koltunski
      mManager.apply(mEffects);
66
      mScreen = new DistortedFramebuffer(0);
67 7ba38011 Leszek Koltunski
      }
68
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70 2f20ae3a Leszek Koltunski
71 4b34bb08 Leszek Koltunski
   AroundTheWorldEffectsManager getManager()
72 2f20ae3a Leszek Koltunski
     {
73 392e16fd Leszek Koltunski
     return mManager;
74 2f20ae3a Leszek Koltunski
     }
75
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
78 7ba38011 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused) 
79
      {
80
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
81 bc29e409 Leszek Koltunski
      mScreen.renderTo(mTexture, mGrid, mEffects, System.currentTimeMillis() );
82 7ba38011 Leszek Koltunski
      }
83
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
    
86
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
87
      {
88 392e16fd Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
89 7ba38011 Leszek Koltunski
90 5055b5d4 Leszek Koltunski
      if( (float)mObjHeight/mObjWidth > (float)height/width )
91 7ba38011 Leszek Koltunski
        {
92
        int w = (height*mObjWidth)/mObjHeight;
93
        float factor = (float)height/mObjHeight;
94 392e16fd Leszek Koltunski
        mEffects.move( new Static3D((width-w)/2,0,0) );
95
        mEffects.scale(factor);
96 7ba38011 Leszek Koltunski
        }
97
      else
98
        {
99
        int h = (width*mObjHeight)/mObjWidth;
100
        float factor = (float)width/mObjWidth;
101 392e16fd Leszek Koltunski
        mEffects.move( new Static3D(0,(height-h)/2,0) );
102
        mEffects.scale(factor);
103 7ba38011 Leszek Koltunski
        }
104
105 392e16fd Leszek Koltunski
      mScreen.resize(width,height);
106 7ba38011 Leszek Koltunski
      }
107
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
    
110
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
111 e7a4ef16 Leszek Koltunski
      {
112
      GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
113
114 7ba38011 Leszek Koltunski
      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 10b7e588 Leszek Koltunski
      mObjWidth = bitmap.getWidth();
131
      mObjHeight= bitmap.getHeight();
132 7ba38011 Leszek Koltunski
133 7451c98a Leszek Koltunski
      mTexture = new DistortedTexture(mObjWidth,mObjHeight);
134 f6d884d5 Leszek Koltunski
      mTexture.setTexture(bitmap);
135 7ba38011 Leszek Koltunski
136 10b7e588 Leszek Koltunski
      mGrid = new GridFlat(30,30*mObjHeight/mObjWidth);
137 e8b6aa95 Leszek Koltunski
138 7ba38011 Leszek Koltunski
      try
139
        {
140
        Distorted.onSurfaceCreated(mView.getContext());
141
        }
142
      catch(Exception ex)
143
        {
144
        android.util.Log.e("AroundTheWorld", ex.getMessage() );
145
        }
146
      }
147
}