Project

General

Profile

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

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

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 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
25 7ba38011 Leszek Koltunski
import android.opengl.GLSurfaceView;
26
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
30 d218d64e leszek
import org.distorted.library.DistortedScreen;
31 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
32 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
33 7ba38011 Leszek Koltunski
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 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
48 d218d64e leszek
   private DistortedScreen mScreen;
49 392e16fd Leszek Koltunski
   private AroundTheWorldEffectsManager mManager;
50 7ba38011 Leszek Koltunski
   private int mObjWidth, mObjHeight;
51
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54
   AroundTheWorldRenderer(GLSurfaceView view)
55
      {
56 76f9798b Leszek Koltunski
      DistortedEffects.setMaxVertex(12);
57
      DistortedEffects.setMaxFragment(9);
58 f6d884d5 Leszek Koltunski
59
      mView = view;
60 392e16fd Leszek Koltunski
      mManager = new AroundTheWorldEffectsManager();
61 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
62 392e16fd Leszek Koltunski
      mManager.apply(mEffects);
63 d218d64e leszek
      mScreen = new DistortedScreen();
64 7ba38011 Leszek Koltunski
      }
65
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67 2f20ae3a Leszek Koltunski
68 4b34bb08 Leszek Koltunski
   AroundTheWorldEffectsManager getManager()
69 2f20ae3a Leszek Koltunski
     {
70 392e16fd Leszek Koltunski
     return mManager;
71 2f20ae3a Leszek Koltunski
     }
72
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75 7ba38011 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused) 
76
      {
77 41a81a14 Leszek Koltunski
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
78 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
79 7ba38011 Leszek Koltunski
      }
80
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
    
83
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
84
      {
85 392e16fd Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
86 7ba38011 Leszek Koltunski
87 5055b5d4 Leszek Koltunski
      if( (float)mObjHeight/mObjWidth > (float)height/width )
88 7ba38011 Leszek Koltunski
        {
89
        int w = (height*mObjWidth)/mObjHeight;
90
        float factor = (float)height/mObjHeight;
91 392e16fd Leszek Koltunski
        mEffects.move( new Static3D((width-w)/2,0,0) );
92
        mEffects.scale(factor);
93 7ba38011 Leszek Koltunski
        }
94
      else
95
        {
96
        int h = (width*mObjHeight)/mObjWidth;
97
        float factor = (float)width/mObjWidth;
98 392e16fd Leszek Koltunski
        mEffects.move( new Static3D(0,(height-h)/2,0) );
99
        mEffects.scale(factor);
100 7ba38011 Leszek Koltunski
        }
101
102 392e16fd Leszek Koltunski
      mScreen.resize(width,height);
103 7ba38011 Leszek Koltunski
      }
104
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
    
107
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
108 e7a4ef16 Leszek Koltunski
      {
109 41a81a14 Leszek Koltunski
      GLES30.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
110 e7a4ef16 Leszek Koltunski
111 7ba38011 Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.face);
112
      Bitmap bitmap;
113
114
      try
115
        {
116
        bitmap = BitmapFactory.decodeStream(is);
117
        }
118
      finally
119
        {
120
        try
121
          {
122
          is.close();
123
          }
124
        catch(IOException e) { }
125
        }
126
127 10b7e588 Leszek Koltunski
      mObjWidth = bitmap.getWidth();
128
      mObjHeight= bitmap.getHeight();
129 7ba38011 Leszek Koltunski
130 fe59d375 Leszek Koltunski
      DistortedTexture texture = new DistortedTexture(mObjWidth,mObjHeight);
131
      texture.setTexture(bitmap);
132 7ba38011 Leszek Koltunski
133 fe59d375 Leszek Koltunski
      MeshFlat mesh = new MeshFlat(30,30*mObjHeight/mObjWidth);
134
135
      mScreen.detachAll();
136
      mScreen.attach(texture, mEffects, mesh );
137 e8b6aa95 Leszek Koltunski
138 7ba38011 Leszek Koltunski
      try
139
        {
140 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
141 7ba38011 Leszek Koltunski
        }
142
      catch(Exception ex)
143
        {
144
        android.util.Log.e("AroundTheWorld", ex.getMessage() );
145
        }
146
      }
147
}