Project

General

Profile

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

examples / src / main / java / org / distorted / examples / aroundtheworld / AroundTheWorldRenderer.java @ 40eef4b9

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
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
30 7ba38011 Leszek Koltunski
import org.distorted.library.DistortedObject;
31
import org.distorted.library.EffectTypes;
32
import org.distorted.library.type.Static3D;
33
34
import java.io.IOException;
35
import java.io.InputStream;
36
37
import javax.microedition.khronos.egl.EGLConfig;
38
import javax.microedition.khronos.opengles.GL10;
39
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
42
class AroundTheWorldRenderer implements GLSurfaceView.Renderer
43
{
44
   private GLSurfaceView mView;
45
   private DistortedObject mObject;
46 10b7e588 Leszek Koltunski
   private GridFlat mGrid;
47 7ba38011 Leszek Koltunski
   private AroundTheWorldEffectsManager mEffects;
48
   private int mObjWidth, mObjHeight;
49
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51
52
   AroundTheWorldRenderer(GLSurfaceView view)
53
      {
54
      mView = view;
55 2f20ae3a Leszek Koltunski
      mEffects = new AroundTheWorldEffectsManager();
56 4b34bb08 Leszek Koltunski
57 b3a555a7 Leszek Koltunski
      Distorted.setMaxVertex(12);
58 287a5475 Leszek Koltunski
      Distorted.setMaxFragment(9);
59 7ba38011 Leszek Koltunski
      }
60
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62 2f20ae3a Leszek Koltunski
63 4b34bb08 Leszek Koltunski
   AroundTheWorldEffectsManager getManager()
64 2f20ae3a Leszek Koltunski
     {
65
     return mEffects;
66
     }
67
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
70 7ba38011 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused) 
71
      {
72
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
73 e8b6aa95 Leszek Koltunski
      mObject.draw(System.currentTimeMillis(), mGrid);
74 7ba38011 Leszek Koltunski
      }
75
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
    
78
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
79
      {
80
      mObject.abortEffects(EffectTypes.MATRIX);
81
82 5055b5d4 Leszek Koltunski
      if( (float)mObjHeight/mObjWidth > (float)height/width )
83 7ba38011 Leszek Koltunski
        {
84
        int w = (height*mObjWidth)/mObjHeight;
85
        float factor = (float)height/mObjHeight;
86
        mObject.move( new Static3D((width-w)/2,0,0) );
87
        mObject.scale(factor);
88
        }
89
      else
90
        {
91
        int h = (width*mObjHeight)/mObjWidth;
92
        float factor = (float)width/mObjWidth;
93
        mObject.move( new Static3D(0,(height-h)/2,0) );
94
        mObject.scale(factor);
95
        }
96
97
      Distorted.onSurfaceChanged(width, height);
98
      }
99
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
    
102
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
103 e7a4ef16 Leszek Koltunski
      {
104
      GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
105
106 7ba38011 Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.face);
107
      Bitmap bitmap;
108
109
      try
110
        {
111
        bitmap = BitmapFactory.decodeStream(is);
112
        }
113
      finally
114
        {
115
        try
116
          {
117
          is.close();
118
          }
119
        catch(IOException e) { }
120
        }
121
122 10b7e588 Leszek Koltunski
      mObjWidth = bitmap.getWidth();
123
      mObjHeight= bitmap.getHeight();
124 7ba38011 Leszek Koltunski
125 40eef4b9 Leszek Koltunski
      mObject = new DistortedObject(mObjWidth,mObjHeight,0);
126 e8b6aa95 Leszek Koltunski
      mObject.setTexture(bitmap);
127 2f20ae3a Leszek Koltunski
      mEffects.apply(mObject);
128 7ba38011 Leszek Koltunski
129 10b7e588 Leszek Koltunski
      mGrid = new GridFlat(30,30*mObjHeight/mObjWidth);
130 e8b6aa95 Leszek Koltunski
131 7ba38011 Leszek Koltunski
      try
132
        {
133
        Distorted.onSurfaceCreated(mView.getContext());
134
        }
135
      catch(Exception ex)
136
        {
137
        android.util.Log.e("AroundTheWorld", ex.getMessage() );
138
        }
139
      }
140
}